src/share/classes/com/sun/jndi/dns/Resolver.java

Print this page




 108     DnsName findZoneName(DnsName fqdn, int rrclass, boolean recursion)
 109             throws NamingException {
 110 
 111         fqdn = (DnsName) fqdn.clone();
 112         while (fqdn.size() > 1) {       // while below root
 113             ResourceRecords rrs = null;
 114             try {
 115                 rrs = query(fqdn, rrclass, ResourceRecord.TYPE_SOA,
 116                             recursion, false);
 117             } catch (NameNotFoundException e) {
 118                 throw e;
 119             } catch (NamingException e) {
 120                 // Ignore error and keep searching up the tree.
 121             }
 122             if (rrs != null) {
 123                 if (rrs.answer.size() > 0) {    // found zone's SOA
 124                     return fqdn;
 125                 }
 126                 // Look for an SOA record giving the zone's top node.
 127                 for (int i = 0; i < rrs.authority.size(); i++) {
 128                     ResourceRecord rr = (ResourceRecord)
 129                         rrs.authority.elementAt(i);
 130                     if (rr.getType() == ResourceRecord.TYPE_SOA) {
 131                         DnsName zone = rr.getName();
 132                         if (fqdn.endsWith(zone)) {
 133                             return zone;
 134                         }
 135                     }
 136                 }
 137             }
 138             fqdn.remove(fqdn.size() - 1);       // one step rootward
 139         }
 140         return fqdn;                    // no SOA found below root, so
 141                                         // return root
 142     }
 143 
 144     /*
 145      * Finds a zone's SOA record.  Returns null if no SOA is found (in
 146      * which case "zone" is not actually a zone).
 147      * If recursion is true, recursion is requested on the query.
 148      */
 149      ResourceRecord findSoa(DnsName zone, int rrclass, boolean recursion)
 150             throws NamingException {
 151 
 152         ResourceRecords rrs = query(zone, rrclass, ResourceRecord.TYPE_SOA,
 153                                     recursion, false);
 154         for (int i = 0; i < rrs.answer.size(); i++) {
 155             ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i);
 156             if (rr.getType() == ResourceRecord.TYPE_SOA) {
 157                 return rr;
 158             }
 159         }
 160         return null;
 161     }
 162 
 163     /*
 164      * Finds the name servers of a zone.  <tt>zone</tt> is a fully-qualified
 165      * domain name at the top of a zone.
 166      * If recursion is true, recursion is requested on the query.
 167      */
 168     private String[] findNameServers(DnsName zone, boolean recursion)
 169             throws NamingException {
 170 
 171         // %%% As an optimization, could look in authority section of
 172         // findZoneName() response first.
 173         ResourceRecords rrs =
 174             query(zone, ResourceRecord.CLASS_INTERNET, ResourceRecord.TYPE_NS,
 175                   recursion, false);
 176         String[] ns = new String[rrs.answer.size()];
 177         for (int i = 0; i < ns.length; i++) {
 178             ResourceRecord rr = (ResourceRecord)
 179                 rrs.answer.elementAt(i);
 180             if (rr.getType() != ResourceRecord.TYPE_NS) {
 181                 throw new CommunicationException("Corrupted DNS message");
 182             }
 183             ns[i] = (String) rr.getRdata();
 184 
 185             // Server name will be passed to InetAddress.getByName(), which
 186             // may not be able to handle a trailing dot.
 187             // assert ns[i].endsWith(".");
 188             ns[i] = ns[i].substring(0, ns[i].length() - 1);
 189         }
 190         return ns;
 191     }
 192 }


 108     DnsName findZoneName(DnsName fqdn, int rrclass, boolean recursion)
 109             throws NamingException {
 110 
 111         fqdn = (DnsName) fqdn.clone();
 112         while (fqdn.size() > 1) {       // while below root
 113             ResourceRecords rrs = null;
 114             try {
 115                 rrs = query(fqdn, rrclass, ResourceRecord.TYPE_SOA,
 116                             recursion, false);
 117             } catch (NameNotFoundException e) {
 118                 throw e;
 119             } catch (NamingException e) {
 120                 // Ignore error and keep searching up the tree.
 121             }
 122             if (rrs != null) {
 123                 if (rrs.answer.size() > 0) {    // found zone's SOA
 124                     return fqdn;
 125                 }
 126                 // Look for an SOA record giving the zone's top node.
 127                 for (int i = 0; i < rrs.authority.size(); i++) {
 128                     ResourceRecord rr = rrs.authority.elementAt(i);

 129                     if (rr.getType() == ResourceRecord.TYPE_SOA) {
 130                         DnsName zone = rr.getName();
 131                         if (fqdn.endsWith(zone)) {
 132                             return zone;
 133                         }
 134                     }
 135                 }
 136             }
 137             fqdn.remove(fqdn.size() - 1);       // one step rootward
 138         }
 139         return fqdn;                    // no SOA found below root, so
 140                                         // return root
 141     }
 142 
 143     /*
 144      * Finds a zone's SOA record.  Returns null if no SOA is found (in
 145      * which case "zone" is not actually a zone).
 146      * If recursion is true, recursion is requested on the query.
 147      */
 148      ResourceRecord findSoa(DnsName zone, int rrclass, boolean recursion)
 149             throws NamingException {
 150 
 151         ResourceRecords rrs = query(zone, rrclass, ResourceRecord.TYPE_SOA,
 152                                     recursion, false);
 153         for (int i = 0; i < rrs.answer.size(); i++) {
 154             ResourceRecord rr = rrs.answer.elementAt(i);
 155             if (rr.getType() == ResourceRecord.TYPE_SOA) {
 156                 return rr;
 157             }
 158         }
 159         return null;
 160     }
 161 
 162     /*
 163      * Finds the name servers of a zone.  <tt>zone</tt> is a fully-qualified
 164      * domain name at the top of a zone.
 165      * If recursion is true, recursion is requested on the query.
 166      */
 167     private String[] findNameServers(DnsName zone, boolean recursion)
 168             throws NamingException {
 169 
 170         // %%% As an optimization, could look in authority section of
 171         // findZoneName() response first.
 172         ResourceRecords rrs =
 173             query(zone, ResourceRecord.CLASS_INTERNET, ResourceRecord.TYPE_NS,
 174                   recursion, false);
 175         String[] ns = new String[rrs.answer.size()];
 176         for (int i = 0; i < ns.length; i++) {
 177             ResourceRecord rr = rrs.answer.elementAt(i);

 178             if (rr.getType() != ResourceRecord.TYPE_NS) {
 179                 throw new CommunicationException("Corrupted DNS message");
 180             }
 181             ns[i] = (String) rr.getRdata();
 182 
 183             // Server name will be passed to InetAddress.getByName(), which
 184             // may not be able to handle a trailing dot.
 185             // assert ns[i].endsWith(".");
 186             ns[i] = ns[i].substring(0, ns[i].length() - 1);
 187         }
 188         return ns;
 189     }
 190 }