0N/A/*
2362N/A * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.dns;
0N/A
0N/A
0N/Aimport javax.naming.*;
0N/A
0N/A
0N/A/**
0N/A * The Resolver class performs DNS client operations in support of DnsContext.
0N/A *
0N/A * <p> Every DnsName instance passed to or returned from a method of
0N/A * this class should be fully-qualified and contain a root label (an
0N/A * empty component at position 0).
0N/A *
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/Aclass Resolver {
0N/A
0N/A private DnsClient dnsClient;
0N/A private int timeout; // initial timeout on UDP queries in ms
0N/A private int retries; // number of UDP retries
0N/A
0N/A
0N/A /*
0N/A * Constructs a new Resolver given its servers and timeout parameters.
0N/A * Each server is of the form "server[:port]".
0N/A * IPv6 literal host names include delimiting brackets.
0N/A * There must be at least one server.
0N/A * "timeout" is the initial timeout interval (in ms) for UDP queries,
0N/A * and "retries" gives the number of retries per server.
0N/A */
0N/A Resolver(String[] servers, int timeout, int retries)
0N/A throws NamingException {
0N/A this.timeout = timeout;
0N/A this.retries = retries;
0N/A dnsClient = new DnsClient(servers, timeout, retries);
0N/A }
0N/A
0N/A public void close() {
0N/A dnsClient.close();
0N/A dnsClient = null;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Queries resource records of a particular class and type for a
0N/A * given domain name.
0N/A * Useful values of rrclass are ResourceRecord.[Q]CLASS_xxx.
0N/A * Useful values of rrtype are ResourceRecord.[Q]TYPE_xxx.
0N/A * If recursion is true, recursion is requested on the query.
0N/A * If auth is true, only authoritative responses are accepted.
0N/A */
0N/A ResourceRecords query(DnsName fqdn, int rrclass, int rrtype,
0N/A boolean recursion, boolean auth)
0N/A throws NamingException {
0N/A return dnsClient.query(fqdn, rrclass, rrtype, recursion, auth);
0N/A }
0N/A
0N/A /*
0N/A * Queries all resource records of a zone given its domain name and class.
0N/A * If recursion is true, recursion is requested on the query to find
0N/A * the name server (and also on the zone transfer, but it won't matter).
0N/A */
0N/A ResourceRecords queryZone(DnsName zone, int rrclass, boolean recursion)
0N/A throws NamingException {
0N/A
0N/A DnsClient cl =
0N/A new DnsClient(findNameServers(zone, recursion), timeout, retries);
0N/A try {
0N/A return cl.queryZone(zone, rrclass, recursion);
0N/A } finally {
0N/A cl.close();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Finds the zone of a given domain name. The method is to look
0N/A * for the first SOA record on the path from the given domain to
0N/A * the root. This search may be partially bypassed if the zone's
0N/A * SOA record is received in the authority section of a response.
0N/A * If recursion is true, recursion is requested on any queries.
0N/A */
0N/A DnsName findZoneName(DnsName fqdn, int rrclass, boolean recursion)
0N/A throws NamingException {
0N/A
0N/A fqdn = (DnsName) fqdn.clone();
0N/A while (fqdn.size() > 1) { // while below root
0N/A ResourceRecords rrs = null;
0N/A try {
0N/A rrs = query(fqdn, rrclass, ResourceRecord.TYPE_SOA,
0N/A recursion, false);
0N/A } catch (NameNotFoundException e) {
0N/A throw e;
0N/A } catch (NamingException e) {
0N/A // Ignore error and keep searching up the tree.
0N/A }
0N/A if (rrs != null) {
0N/A if (rrs.answer.size() > 0) { // found zone's SOA
0N/A return fqdn;
0N/A }
0N/A // Look for an SOA record giving the zone's top node.
0N/A for (int i = 0; i < rrs.authority.size(); i++) {
0N/A ResourceRecord rr = (ResourceRecord)
0N/A rrs.authority.elementAt(i);
0N/A if (rr.getType() == ResourceRecord.TYPE_SOA) {
0N/A DnsName zone = rr.getName();
0N/A if (fqdn.endsWith(zone)) {
0N/A return zone;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A fqdn.remove(fqdn.size() - 1); // one step rootward
0N/A }
0N/A return fqdn; // no SOA found below root, so
0N/A // return root
0N/A }
0N/A
0N/A /*
0N/A * Finds a zone's SOA record. Returns null if no SOA is found (in
0N/A * which case "zone" is not actually a zone).
0N/A * If recursion is true, recursion is requested on the query.
0N/A */
0N/A ResourceRecord findSoa(DnsName zone, int rrclass, boolean recursion)
0N/A throws NamingException {
0N/A
0N/A ResourceRecords rrs = query(zone, rrclass, ResourceRecord.TYPE_SOA,
0N/A recursion, false);
0N/A for (int i = 0; i < rrs.answer.size(); i++) {
0N/A ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i);
0N/A if (rr.getType() == ResourceRecord.TYPE_SOA) {
0N/A return rr;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Finds the name servers of a zone. <tt>zone</tt> is a fully-qualified
0N/A * domain name at the top of a zone.
0N/A * If recursion is true, recursion is requested on the query.
0N/A */
0N/A private String[] findNameServers(DnsName zone, boolean recursion)
0N/A throws NamingException {
0N/A
0N/A // %%% As an optimization, could look in authority section of
0N/A // findZoneName() response first.
0N/A ResourceRecords rrs =
0N/A query(zone, ResourceRecord.CLASS_INTERNET, ResourceRecord.TYPE_NS,
0N/A recursion, false);
0N/A String[] ns = new String[rrs.answer.size()];
0N/A for (int i = 0; i < ns.length; i++) {
0N/A ResourceRecord rr = (ResourceRecord)
0N/A rrs.answer.elementAt(i);
0N/A if (rr.getType() != ResourceRecord.TYPE_NS) {
0N/A throw new CommunicationException("Corrupted DNS message");
0N/A }
0N/A ns[i] = (String) rr.getRdata();
0N/A
0N/A // Server name will be passed to InetAddress.getByName(), which
0N/A // may not be able to handle a trailing dot.
0N/A // assert ns[i].endsWith(".");
0N/A ns[i] = ns[i].substring(0, ns[i].length() - 1);
0N/A }
0N/A return ns;
0N/A }
0N/A}