jdk/src/share/classes/com/sun/jndi/dns/Header.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
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, 2002, 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 javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The Header class represents the header of a DNS message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class Header {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static final int HEADER_SIZE = 12;  // octets in a DNS header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // Masks and shift amounts for DNS header flag fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final short QR_BIT =         (short) 0x8000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final short OPCODE_MASK =    (short) 0x7800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int   OPCODE_SHIFT =   11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final short AA_BIT =         (short) 0x0400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final short TC_BIT =         (short) 0x0200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final short RD_BIT =         (short) 0x0100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final short RA_BIT =         (short) 0x0080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final short RCODE_MASK =     (short) 0x000F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int xid;                    // ID:  16-bit query identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    boolean query;              // QR:  true if query, false if response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int opcode;                 // OPCODE:  4-bit opcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    boolean authoritative;      // AA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    boolean truncated;          // TC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    boolean recursionDesired;   // RD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    boolean recursionAvail;     // RA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int rcode;                  // RCODE:  4-bit response code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int numQuestions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int numAnswers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int numAuthorities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int numAdditionals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Returns a representation of a decoded DNS message header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Does not modify or store a reference to the msg array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Header(byte[] msg, int msgLen) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        decode(msg, msgLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Decodes a DNS message header.  Does not modify or store a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * reference to the msg array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private void decode(byte[] msg, int msgLen) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int pos = 0;        // current offset into msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (msgLen < HEADER_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new CommunicationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        "DNS error: corrupted message header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            xid = getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // Flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            short flags = (short) getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            query = (flags & QR_BIT) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            opcode = (flags & OPCODE_MASK) >>> OPCODE_SHIFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            authoritative = (flags & AA_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            truncated = (flags & TC_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            recursionDesired = (flags & RD_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            recursionAvail = (flags & RA_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            rcode = (flags & RCODE_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // RR counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            numQuestions = getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            numAnswers = getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            numAuthorities = getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            numAdditionals = getShort(msg, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new CommunicationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    "DNS error: corrupted message header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns the 2-byte unsigned value at msg[pos].  The high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * order byte comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static int getShort(byte[] msg, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return (((msg[pos] & 0xFF) << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                (msg[pos + 1] & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}