Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/dns/Header.java
38924 views
/*1* Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.dns;262728import javax.naming.*;293031/**32* The Header class represents the header of a DNS message.33*34* @author Scott Seligman35*/363738class Header {3940static final int HEADER_SIZE = 12; // octets in a DNS header4142// Masks and shift amounts for DNS header flag fields.43static final short QR_BIT = (short) 0x8000;44static final short OPCODE_MASK = (short) 0x7800;45static final int OPCODE_SHIFT = 11;46static final short AA_BIT = (short) 0x0400;47static final short TC_BIT = (short) 0x0200;48static final short RD_BIT = (short) 0x0100;49static final short RA_BIT = (short) 0x0080;50static final short RCODE_MASK = (short) 0x000F;5152int xid; // ID: 16-bit query identifier53boolean query; // QR: true if query, false if response54int opcode; // OPCODE: 4-bit opcode55boolean authoritative; // AA56boolean truncated; // TC57boolean recursionDesired; // RD58boolean recursionAvail; // RA59int rcode; // RCODE: 4-bit response code60int numQuestions;61int numAnswers;62int numAuthorities;63int numAdditionals;6465/*66* Returns a representation of a decoded DNS message header.67* Does not modify or store a reference to the msg array.68*/69Header(byte[] msg, int msgLen) throws NamingException {70decode(msg, msgLen);71}7273/*74* Decodes a DNS message header. Does not modify or store a75* reference to the msg array.76*/77private void decode(byte[] msg, int msgLen) throws NamingException {7879try {80int pos = 0; // current offset into msg8182if (msgLen < HEADER_SIZE) {83throw new CommunicationException(84"DNS error: corrupted message header");85}8687xid = getShort(msg, pos);88pos += 2;8990// Flags91short flags = (short) getShort(msg, pos);92pos += 2;93query = (flags & QR_BIT) == 0;94opcode = (flags & OPCODE_MASK) >>> OPCODE_SHIFT;95authoritative = (flags & AA_BIT) != 0;96truncated = (flags & TC_BIT) != 0;97recursionDesired = (flags & RD_BIT) != 0;98recursionAvail = (flags & RA_BIT) != 0;99rcode = (flags & RCODE_MASK);100101// RR counts102numQuestions = getShort(msg, pos);103pos += 2;104numAnswers = getShort(msg, pos);105pos += 2;106numAuthorities = getShort(msg, pos);107pos += 2;108numAdditionals = getShort(msg, pos);109pos += 2;110111} catch (IndexOutOfBoundsException e) {112throw new CommunicationException(113"DNS error: corrupted message header");114}115}116117/*118* Returns the 2-byte unsigned value at msg[pos]. The high119* order byte comes first.120*/121private static int getShort(byte[] msg, int pos) {122return (((msg[pos] & 0xFF) << 8) |123(msg[pos + 1] & 0xFF));124}125}126127128