jdk/src/share/classes/com/sun/jndi/dns/ResourceRecords.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10324 e28265130e4f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2001, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The ResourceRecords class represents the resource records in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * four sections of a DNS message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The additional records section is currently ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class ResourceRecords {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // Four sections:  question, answer, authority, additional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // The question section is treated as being made up of (shortened)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // resource records, although this isn't technically how it's defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    Vector question = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    Vector answer = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    Vector authority = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Vector additional = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * True if these resource records are from a zone transfer.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * that case only answer records are read (as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * draft-ietf-dnsext-axfr-clarify-02.txt).  Also, the rdata of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * those answer records is not decoded (for efficiency) except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * for SOA records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    boolean zoneXfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Returns a representation of the resource records in a DNS message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Does not modify or store a reference to the msg array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    ResourceRecords(byte[] msg, int msgLen, Header hdr, boolean zoneXfer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (zoneXfer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            answer.ensureCapacity(8192);        // an arbitrary "large" number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.zoneXfer = zoneXfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        add(msg, msgLen, hdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Returns the type field of the first answer record, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * there are no answer records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int getFirstAnsType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (answer.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return ((ResourceRecord) answer.firstElement()).getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Returns the type field of the last answer record, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * there are no answer records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int getLastAnsType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (answer.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return ((ResourceRecord) answer.lastElement()).getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Decodes the resource records in a DNS message and adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * them to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Does not modify or store a reference to the msg array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void add(byte[] msg, int msgLen, Header hdr) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ResourceRecord rr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int pos = Header.HEADER_SIZE;   // current offset into msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            for (int i = 0; i < hdr.numQuestions; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                rr = new ResourceRecord(msg, msgLen, pos, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (!zoneXfer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    question.addElement(rr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                pos += rr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            for (int i = 0; i < hdr.numAnswers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                rr = new ResourceRecord(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        msg, msgLen, pos, false, !zoneXfer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                answer.addElement(rr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                pos += rr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (zoneXfer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            for (int i = 0; i < hdr.numAuthorities; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                rr = new ResourceRecord(msg, msgLen, pos, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                authority.addElement(rr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                pos += rr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // The additional records section is currently ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new CommunicationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    "DNS error: corrupted message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}