/* * Finds the zone of a given domain name. The method is to look * for the first SOA record on the path from the given domain to * the root. This search may be partially bypassed if the zone's * SOA record is received in the authority section of a response. * If recursion is true, recursion is requested on any queries. */ DnsName findZoneName(DnsName fqdn, int rrclass, boolean recursion) throws NamingException { fqdn = (DnsName) fqdn.clone(); while (fqdn.size() > 1) { // while below root ResourceRecords rrs = null; try { rrs = query(fqdn, rrclass, ResourceRecord.TYPE_SOA, recursion, false); } catch (NameNotFoundException e) { throw e; } catch (NamingException e) { // Ignore error and keep searching up the tree. } if (rrs != null) { if (rrs.answer.size() > 0) { // found zone's SOA return fqdn; } // Look for an SOA record giving the zone's top node. for (int i = 0; i < rrs.authority.size(); i++) { ResourceRecord rr = (ResourceRecord) rrs.authority.elementAt(i); if (rr.getType() == ResourceRecord.TYPE_SOA) { DnsName zone = rr.getName(); if (fqdn.endsWith(zone)) { return zone; } } } } fqdn.remove(fqdn.size() - 1); // one step rootward } return fqdn; // no SOA found below root, so // return root }
/* * Finds a zone's SOA record. Returns null if no SOA is found (in * which case "zone" is not actually a zone). * If recursion is true, recursion is requested on the query. */ ResourceRecord findSoa(DnsName zone, int rrclass, boolean recursion) throws NamingException { ResourceRecords rrs = query(zone, rrclass, ResourceRecord.TYPE_SOA, recursion, false); for (int i = 0; i < rrs.answer.size(); i++) { ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i); if (rr.getType() == ResourceRecord.TYPE_SOA) { return rr; } } return null; }
/* * Finds the name servers of a zone. <tt>zone</tt> is a fully-qualified * domain name at the top of a zone. * If recursion is true, recursion is requested on the query. */ private String[] findNameServers(DnsName zone, boolean recursion) throws NamingException { // %%% As an optimization, could look in authority section of // findZoneName() response first. ResourceRecords rrs = query(zone, ResourceRecord.CLASS_INTERNET, ResourceRecord.TYPE_NS, recursion, false); String[] ns = new String[rrs.answer.size()]; for (int i = 0; i < ns.length; i++) { ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i); if (rr.getType() != ResourceRecord.TYPE_NS) { throw new CommunicationException("Corrupted DNS message"); } ns[i] = (String) rr.getRdata(); // Server name will be passed to InetAddress.getByName(), which // may not be able to handle a trailing dot. // assert ns[i].endsWith("."); ns[i] = ns[i].substring(0, ns[i].length() - 1); } return ns; }