src/jdk.naming.dns/share/classes/com/sun/jndi/dns/ResourceRecord.java
author rriggs
Fri, 07 Dec 2018 11:51:17 -0500
changeset 52902 e3398b2e1ab0
parent 48568 0255315ac8d4
permissions -rw-r--r--
8214971: Replace use of string.equals("") with isEmpty() Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48568
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.dns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    28
import javax.naming.CommunicationException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.InvalidNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    31
import java.io.IOException;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    32
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    33
import java.nio.charset.StandardCharsets;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The ResourceRecord class represents a DNS resource record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The string format is based on the master file representation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * RFC 1035.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class ResourceRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Resource record type codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int TYPE_A     =  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final int TYPE_NS    =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int TYPE_CNAME =  5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final int TYPE_SOA   =  6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int TYPE_PTR   = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final int TYPE_HINFO = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final int TYPE_MX    = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final int TYPE_TXT   = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final int TYPE_AAAA  = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final int TYPE_SRV   = 33;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final int TYPE_NAPTR = 35;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final int QTYPE_AXFR = 252;          // zone transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int QTYPE_STAR = 255;          // query type "*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Mapping from resource record type codes to type name strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final String rrTypeNames[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        null, "A", "NS", null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "CNAME", "SOA", null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        null, null, "PTR", "HINFO", null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "MX", "TXT", null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        null, null, null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        null, null, null, "AAAA", null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        null, null, null, "SRV", null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "NAPTR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Resource record class codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final int CLASS_INTERNET = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int CLASS_HESIOD   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final int QCLASS_STAR    = 255;      // query class "*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Mapping from resource record type codes to class name strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final String rrClassNames[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        null, "IN", null, null, "HS"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    92
    /*
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    93
     * Maximum number of compression references in labels.
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    94
     * Used to detect compression loops.
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    95
     */
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
    96
    private static final int MAXIMUM_COMPRESSION_REFERENCES = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    byte[] msg;                 // DNS message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int msgLen;                 // msg size (in octets)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    boolean qSection;           // true if this RR is part of question section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                // and therefore has no ttl or rdata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int offset;                 // offset of RR w/in msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int rrlen;                  // number of octets in encoded RR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    DnsName name;               // name field of RR, including root label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    int rrtype;                 // type field of RR
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   106
    String rrtypeName;          // name of rrtype
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    int rrclass;                // class field of RR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    String rrclassName;         // name of rrclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    int ttl = 0;                // ttl field of RR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int rdlen = 0;              // number of octets of rdata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    Object rdata = null;        // rdata -- most are String, unknown are byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Constructs a new ResourceRecord.  The encoded data of the DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * message is contained in msg; data for this RR begins at msg[offset].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * If qSection is true this RR is part of a question section.  It's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * not a true resource record in that case, but is treated as if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * were a shortened one (with no ttl or rdata).  If decodeRdata is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * false, the rdata is not decoded (and getRdata() will return null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * unless this is an SOA record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   123
     * @throws CommunicationException if a decoded domain name isn't valid.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws ArrayIndexOutOfBoundsException given certain other corrupt data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    ResourceRecord(byte[] msg, int msgLen, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                   boolean qSection, boolean decodeRdata)
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   128
            throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.msg = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.msgLen = msgLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.qSection = qSection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        decode(decodeRdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String text = name + " " + rrclassName + " " + rrtypeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (!qSection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            text += " " + ttl + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ((rdata != null) ? rdata : "[n/a]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns the name field of this RR, including the root label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public DnsName getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the number of octets in the encoded RR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return rrlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public int getRrclass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public Object getRdata() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return rdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static String getTypeName(int rrtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return valueToName(rrtype, rrTypeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static int getType(String typeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return nameToValue(typeName, rrTypeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static String getRrclassName(int rrclass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return valueToName(rrclass, rrClassNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public static int getRrclass(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return nameToValue(className, rrClassNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static String valueToName(int val, String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if ((val > 0) && (val < names.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            name = names[val];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else if (val == QTYPE_STAR) {         // QTYPE_STAR == QCLASS_STAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            name = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            name = Integer.toString(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static int nameToValue(String name, String[] names) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 48568
diff changeset
   203
        if (name.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return -1;                          // invalid name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (name.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return QTYPE_STAR;                  // QTYPE_STAR == QCLASS_STAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (Character.isDigit(name.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return Integer.parseInt(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        for (int i = 1; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if ((names[i] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    name.equalsIgnoreCase(names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return -1;                              // unknown name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Compares two SOA record serial numbers using 32-bit serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * arithmetic as defined in RFC 1982.  Serial numbers are unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * 32-bit quantities.  Returns a negative, zero, or positive value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * as the first serial number is less than, equal to, or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * than the second.  If the serial numbers are not comparable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * result is undefined.  Note that the relation is not transitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public static int compareSerialNumbers(long s1, long s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        long diff = s2 - s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (diff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else if ((diff > 0 &&  diff <= 0x7FFFFFFF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                   (diff < 0 && -diff >  0x7FFFFFFF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Decodes the binary format of the RR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * May throw ArrayIndexOutOfBoundsException given corrupt data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   248
    private void decode(boolean decodeRdata) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int pos = offset;       // index of next unread octet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        name = new DnsName();                           // NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        pos = decodeName(pos, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        rrtype = getUShort(pos);                        // TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        rrtypeName = (rrtype < rrTypeNames.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ? rrTypeNames[rrtype]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (rrtypeName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            rrtypeName = Integer.toString(rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        rrclass = getUShort(pos);                       // CLASS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        rrclassName = (rrclass < rrClassNames.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            ? rrClassNames[rrclass]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (rrclassName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            rrclassName = Integer.toString(rrclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (!qSection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            ttl = getInt(pos);                          // TTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            rdlen = getUShort(pos);                     // RDLENGTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            rdata = (decodeRdata ||                     // RDATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                     (rrtype == TYPE_SOA))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                ? decodeRdata(pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (rdata instanceof DnsName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                rdata = rdata.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            pos += rdlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        rrlen = pos - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        msg = null;     // free up for GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the 1-byte unsigned value at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private int getUByte(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return (msg[pos] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns the 2-byte unsigned value at msg[pos].  The high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * order byte comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private int getUShort(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return (((msg[pos] & 0xFF) << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                (msg[pos + 1] & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns the 4-byte signed value at msg[pos].  The high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * order byte comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private int getInt(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return ((getUShort(pos) << 16) | getUShort(pos + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns the 4-byte unsigned value at msg[pos].  The high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * order byte comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private long getUInt(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return (getInt(pos) & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the name encoded at msg[pos], including the root label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   329
    private DnsName decodeName(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        DnsName n = new DnsName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        decodeName(pos, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Prepends to "n" the domain name encoded at msg[pos], including the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * label.  Returns the index into "msg" following the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   339
    private int decodeName(int pos, DnsName n) throws CommunicationException {
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   340
        int endPos = -1;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   341
        int level = 0;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   342
        try {
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   343
            while (true) {
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   344
                if (level > MAXIMUM_COMPRESSION_REFERENCES)
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   345
                    throw new IOException("Too many compression references");
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   346
                int typeAndLen = msg[pos] & 0xFF;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   347
                if (typeAndLen == 0) {                  // end of name
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   348
                    ++pos;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   349
                    n.add(0, "");
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   350
                    break;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   351
                } else if (typeAndLen <= 63) {          // regular label
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   352
                    ++pos;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   353
                    n.add(0, new String(msg, pos, typeAndLen,
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   354
                        StandardCharsets.ISO_8859_1));
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   355
                    pos += typeAndLen;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   356
                } else if ((typeAndLen & 0xC0) == 0xC0) { // name compression
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   357
                    ++level;
48568
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   358
                    // cater for the case where the name pointed to is itself
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   359
                    // compressed: we don't want endPos to be reset by the second
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   360
                    // compression level.
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   361
                    int ppos = pos;
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   362
                    if (endPos == -1) endPos = pos + 2;
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   363
                    pos = getUShort(pos) & 0x3FFF;
48568
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   364
                    if (debug) {
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   365
                        dprint("decode: name compression at " + ppos
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   366
                                + " -> " + pos + " endPos=" + endPos);
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   367
                        assert endPos > 0;
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   368
                        assert pos < ppos;
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   369
                        assert pos >= Header.HEADER_SIZE;
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   370
                    }
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   371
                } else
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   372
                    throw new IOException("Invalid label type: " + typeAndLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   374
        } catch (IOException | InvalidNameException e) {
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   375
            CommunicationException ce =new CommunicationException(
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   376
                "DNS error: malformed packet");
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   377
            ce.initCause(e);
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   378
            throw ce;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   380
        if (endPos == -1)
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   381
            endPos = pos;
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   382
        return endPos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns the rdata encoded at msg[pos].  The format is dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * on the rrtype and rrclass values, which have already been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The length of the encoded data is rdlen, which has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * The rdata of records with unknown type/class combinations is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * returned in a newly-allocated byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   393
    private Object decodeRdata(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (rrclass == CLASS_INTERNET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            switch (rrtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            case TYPE_A:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return decodeA(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            case TYPE_AAAA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return decodeAAAA(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            case TYPE_CNAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            case TYPE_NS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            case TYPE_PTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return decodeName(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            case TYPE_MX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return decodeMx(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            case TYPE_SOA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                return decodeSoa(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            case TYPE_SRV:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return decodeSrv(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            case TYPE_NAPTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                return decodeNaptr(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            case TYPE_TXT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return decodeTxt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            case TYPE_HINFO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return decodeHinfo(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Unknown RR type/class
48568
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   419
        if (debug) {
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   420
            dprint("Unknown RR type for RR data: " + rrtype + " rdlen=" + rdlen
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   421
                   + ", pos=" + pos +", msglen=" + msg.length + ", remaining="
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   422
                   + (msg.length-pos));
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   423
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        byte[] rd = new byte[rdlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        System.arraycopy(msg, pos, rd, 0, rdlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return rd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Returns the rdata of an MX record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   432
    private String decodeMx(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int preference = getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        DnsName name = decodeName(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return (preference + " " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Returns the rdata of an SOA record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   442
    private String decodeSoa(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        DnsName mname = new DnsName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        pos = decodeName(pos, mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        DnsName rname = new DnsName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        pos = decodeName(pos, rname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        long serial = getUInt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        long refresh = getUInt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        long retry = getUInt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        long expire = getUInt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        long minimum = getUInt(pos);    // now used as negative TTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return (mname + " " + rname + " " + serial + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                refresh + " " + retry + " " + expire + " " + minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Returns the rdata of an SRV record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * See RFC 2782.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   467
    private String decodeSrv(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int priority = getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int weight =   getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int port =     getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        DnsName target = decodeName(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return (priority + " " + weight + " " + port + " " + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Returns the rdata of an NAPTR record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * See RFC 2915.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
22992
e7fcc52b1b29 8035105: DNS provider cleanups
alanb
parents: 5506
diff changeset
   482
    private String decodeNaptr(int pos) throws CommunicationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int order = getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int preference = getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        StringBuffer flags = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        pos += decodeCharString(pos, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        StringBuffer services = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        pos += decodeCharString(pos, services);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        StringBuffer regexp = new StringBuffer(rdlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        pos += decodeCharString(pos, regexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        DnsName replacement = decodeName(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return (order + " " + preference + " " + flags + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                services + " " + regexp + " " + replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Returns the rdata of a TXT record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * The rdata consists of one or more <character-string>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private String decodeTxt(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        StringBuffer buf = new StringBuffer(rdlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int end = pos + rdlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        while (pos < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            pos += decodeCharString(pos, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (pos < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                buf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns the rdata of an HINFO record that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * The rdata consists of two <character-string>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private String decodeHinfo(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        StringBuffer buf = new StringBuffer(rdlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        pos += decodeCharString(pos, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        buf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        pos += decodeCharString(pos, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Decodes the <character-string> at msg[pos] and adds it to buf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * If the string contains one of the meta-characters ' ', '\\', or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * '"', then the result is quoted and any embedded '\\' or '"'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * chars are escaped with '\\'.  Empty strings are also quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Returns the size of the encoded string, including the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * length octet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private int decodeCharString(int pos, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int start = buf.length();       // starting index of this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int len = getUByte(pos++);      // encoded string length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        boolean quoted = (len == 0);    // quote string if empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            int c = getUByte(pos++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            quoted |= (c == ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if ((c == '\\') || (c == '"')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                quoted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                buf.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            buf.append((char) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (quoted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            buf.insert(start, '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            buf.append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return (len + 1);       // size includes initial octet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Returns the rdata of an A record, in dotted-decimal format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * that is encoded at msg[pos].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private String decodeA(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return ((msg[pos] & 0xff) + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                (msg[pos + 1] & 0xff) + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                (msg[pos + 2] & 0xff) + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                (msg[pos + 3] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Returns the rdata of an AAAA record, in colon-separated format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * that is encoded at msg[pos].  For example:  4321:0:1:2:3:4:567:89ab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * See RFCs 1886 and 2373.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private String decodeAAAA(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int[] addr6 = new int[8];  // the unsigned 16-bit words of the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            addr6[i] = getUShort(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Find longest sequence of two or more zeros, to compress them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int curBase = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        int bestBase = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int bestLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            if (addr6[i] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if (curBase == -1) {    // new sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    curBase = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    curLen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                } else {                // extend sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    ++curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    if ((curLen >= 2) && (curLen > bestLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        bestBase = curBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        bestLen = curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } else {                    // not in sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                curBase = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // If addr begins with at least 6 zeros and is not :: or ::1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        // or with 5 zeros followed by 0xffff, use the text format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // IPv4-compatible or IPv4-mapped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (bestBase == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if ((bestLen == 6) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    ((bestLen == 7) && (addr6[7] > 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                return ("::" + decodeA(pos - 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            } else if ((bestLen == 5) && (addr6[5] == 0xffff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                return ("::ffff:" + decodeA(pos - 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // If bestBase != -1, compress zeros in [bestBase, bestBase+bestLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        boolean compress = (bestBase != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   615
        StringBuilder sb = new StringBuilder(40);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (bestBase == 0) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   617
            sb.append(':');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (!compress || (i < bestBase) || (i >= bestBase + bestLen)) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   621
                sb.append(Integer.toHexString(addr6[i]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                if (i < 7) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   623
                    sb.append(':');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            } else if (compress && (i == bestBase)) {  // first compressed zero
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   626
                sb.append(':');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22992
diff changeset
   630
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
48568
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   632
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   633
    //-------------------------------------------------------------------------
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   634
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   635
    private static final boolean debug = false;
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   636
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   637
    private static void dprint(String mess) {
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   638
        if (debug) {
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   639
            System.err.println("DNS: " + mess);
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   640
        }
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   641
    }
0255315ac8d4 8182125: Improve reliability of DNS lookups
vtewari
parents: 47216
diff changeset
   642
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
}