src/jdk.dns.client/share/classes/jdk/dns/client/internal/ResourceRecords.java
branchaefimov-dns-client-branch
changeset 58870 35c438a6d45c
child 58971 465a15dd6bed
equal deleted inserted replaced
58869:cc66ac8c7646 58870:35c438a6d45c
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.dns.client.internal;
       
    27 
       
    28 import jdk.dns.client.ex.DnsResolverException;
       
    29 
       
    30 import java.util.Vector;
       
    31 
       
    32 
       
    33 /**
       
    34  * The ResourceRecords class represents the resource records in the
       
    35  * four sections of a DNS message.
       
    36  * <p>
       
    37  * The additional records section is currently ignored.
       
    38  *
       
    39  * @author Scott Seligman
       
    40  */
       
    41 
       
    42 
       
    43 public class ResourceRecords {
       
    44 
       
    45     // Four sections:  question, answer, authority, additional.
       
    46     // The question section is treated as being made up of (shortened)
       
    47     // resource records, although this isn't technically how it's defined.
       
    48     Vector<ResourceRecord> question = new Vector<>();
       
    49     public Vector<ResourceRecord> answer = new Vector<>();
       
    50     Vector<ResourceRecord> authority = new Vector<>();
       
    51     // TODO: Try to reuse additional section to avoid additional CNAME lookups
       
    52     public Vector<ResourceRecord> additional = new Vector<>();
       
    53 
       
    54 
       
    55     public boolean hasAddressOrAlias() {
       
    56         return answer.stream().anyMatch(
       
    57                 rr -> rr.rrtype == ResourceRecord.TYPE_A
       
    58                         || rr.rrtype == ResourceRecord.TYPE_AAAA
       
    59                         || rr.rrtype == ResourceRecord.TYPE_CNAME);
       
    60     }
       
    61 
       
    62     /*
       
    63      * True if these resource records are from a zone transfer.  In
       
    64      * that case only answer records are read (as per
       
    65      * draft-ietf-dnsext-axfr-clarify-02.txt).  Also, the rdata of
       
    66      * those answer records is not decoded (for efficiency) except
       
    67      * for SOA records.
       
    68      */
       
    69     boolean zoneXfer;
       
    70 
       
    71     /*
       
    72      * Returns a representation of the resource records in a DNS message.
       
    73      * Does not modify or store a reference to the msg array.
       
    74      */
       
    75     ResourceRecords(byte[] msg, int msgLen, Header hdr, boolean zoneXfer)
       
    76             throws DnsResolverException {
       
    77         if (zoneXfer) {
       
    78             answer.ensureCapacity(8192);        // an arbitrary "large" number
       
    79         }
       
    80         this.zoneXfer = zoneXfer;
       
    81         add(msg, msgLen, hdr);
       
    82     }
       
    83 
       
    84     /*
       
    85      * Returns the type field of the first answer record, or -1 if
       
    86      * there are no answer records.
       
    87      */
       
    88     int getFirstAnsType() {
       
    89         if (answer.size() == 0) {
       
    90             return -1;
       
    91         }
       
    92         return answer.firstElement().getType();
       
    93     }
       
    94 
       
    95     /*
       
    96      * Returns the type field of the last answer record, or -1 if
       
    97      * there are no answer records.
       
    98      */
       
    99     int getLastAnsType() {
       
   100         if (answer.size() == 0) {
       
   101             return -1;
       
   102         }
       
   103         return answer.lastElement().getType();
       
   104     }
       
   105 
       
   106     /*
       
   107      * Decodes the resource records in a DNS message and adds
       
   108      * them to this object.
       
   109      * Does not modify or store a reference to the msg array.
       
   110      */
       
   111     void add(byte[] msg, int msgLen, Header hdr) throws DnsResolverException {
       
   112 
       
   113         ResourceRecord rr;
       
   114         int pos = Header.HEADER_SIZE;   // current offset into msg
       
   115 
       
   116         try {
       
   117             for (int i = 0; i < hdr.numQuestions; i++) {
       
   118                 rr = new ResourceRecord(msg, msgLen, pos, true, false);
       
   119                 if (!zoneXfer) {
       
   120                     question.addElement(rr);
       
   121                 }
       
   122                 pos += rr.size();
       
   123             }
       
   124 
       
   125             for (int i = 0; i < hdr.numAnswers; i++) {
       
   126                 rr = new ResourceRecord(
       
   127                         msg, msgLen, pos, false, !zoneXfer);
       
   128                 answer.addElement(rr);
       
   129                 pos += rr.size();
       
   130             }
       
   131 
       
   132             if (zoneXfer) {
       
   133                 return;
       
   134             }
       
   135 
       
   136             for (int i = 0; i < hdr.numAuthorities; i++) {
       
   137                 rr = new ResourceRecord(msg, msgLen, pos, false, true);
       
   138                 authority.addElement(rr);
       
   139                 pos += rr.size();
       
   140             }
       
   141 
       
   142             // TODO: Might be useful
       
   143             // The additional records section is currently ignored.
       
   144 //            for (int i = 0; i < hdr.numAdditionals; i++) {
       
   145 //                rr = new ResourceRecord(msg, msgLen, pos, false, true);
       
   146 //                additional.addElement(rr);
       
   147 //                pos += rr.size();
       
   148 //            }
       
   149 
       
   150         } catch (IndexOutOfBoundsException e) {
       
   151             throw new DnsResolverException(
       
   152                     "DNS error: corrupted message");
       
   153         }
       
   154     }
       
   155 }