author | aefimov |
Thu, 07 Nov 2019 18:46:06 +0000 | |
branch | aefimov-dns-client-branch |
changeset 58971 | 465a15dd6bed |
parent 58870 | 35c438a6d45c |
permissions | -rw-r--r-- |
58870 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.dns.client.internal; |
|
27 |
||
28 |
import jdk.dns.client.ex.DnsResolverException; |
|
29 |
||
30 |
class Header { |
|
31 |
||
32 |
static final int HEADER_SIZE = 12; // octets in a DNS header |
|
33 |
||
34 |
// Masks and shift amounts for DNS header flag fields. |
|
35 |
static final short QR_BIT = (short) 0x8000; |
|
36 |
static final short OPCODE_MASK = (short) 0x7800; |
|
37 |
static final int OPCODE_SHIFT = 11; |
|
38 |
static final short AA_BIT = (short) 0x0400; |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
39 |
static final short AD_BIT = (short) 0x0020; |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
40 |
static final short CD_BIT = (short) 0x0010; |
58870 | 41 |
static final short TC_BIT = (short) 0x0200; |
42 |
static final short RD_BIT = (short) 0x0100; |
|
43 |
static final short RA_BIT = (short) 0x0080; |
|
44 |
static final short RCODE_MASK = (short) 0x000F; |
|
45 |
||
46 |
int xid; // ID: 16-bit query identifier |
|
47 |
boolean query; // QR: true if query, false if response |
|
48 |
int opcode; // OPCODE: 4-bit opcode |
|
49 |
boolean authoritative; // AA |
|
50 |
boolean truncated; // TC |
|
51 |
boolean recursionDesired; // RD |
|
52 |
boolean recursionAvail; // RA |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
53 |
boolean authenticData; // AD |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
54 |
boolean checkingDisabled; // CD |
58870 | 55 |
int rcode; // RCODE: 4-bit response code |
56 |
int numQuestions; |
|
57 |
int numAnswers; |
|
58 |
int numAuthorities; |
|
59 |
int numAdditionals; |
|
60 |
||
61 |
/* |
|
62 |
* Returns a representation of a decoded DNS message header. |
|
63 |
* Does not modify or store a reference to the msg array. |
|
64 |
*/ |
|
65 |
Header(byte[] msg, int msgLen) throws DnsResolverException { |
|
66 |
decode(msg, msgLen); |
|
67 |
} |
|
68 |
||
69 |
/* |
|
70 |
* Decodes a DNS message header. Does not modify or store a |
|
71 |
* reference to the msg array. |
|
72 |
*/ |
|
73 |
private void decode(byte[] msg, int msgLen) throws DnsResolverException { |
|
74 |
||
75 |
try { |
|
76 |
int pos = 0; // current offset into msg |
|
77 |
||
78 |
if (msgLen < HEADER_SIZE) { |
|
79 |
throw new DnsResolverException( |
|
80 |
"DNS error: corrupted message header"); |
|
81 |
} |
|
82 |
||
83 |
xid = getShort(msg, pos); |
|
84 |
pos += 2; |
|
85 |
||
86 |
// Flags |
|
87 |
short flags = (short) getShort(msg, pos); |
|
88 |
pos += 2; |
|
89 |
query = (flags & QR_BIT) == 0; |
|
90 |
opcode = (flags & OPCODE_MASK) >>> OPCODE_SHIFT; |
|
91 |
authoritative = (flags & AA_BIT) != 0; |
|
92 |
truncated = (flags & TC_BIT) != 0; |
|
93 |
recursionDesired = (flags & RD_BIT) != 0; |
|
94 |
recursionAvail = (flags & RA_BIT) != 0; |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
95 |
authenticData = (flags & AD_BIT) != 0; |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
96 |
checkingDisabled = (flags & CD_BIT) != 0; |
58870 | 97 |
rcode = (flags & RCODE_MASK); |
98 |
||
99 |
// RR counts |
|
100 |
numQuestions = getShort(msg, pos); |
|
101 |
pos += 2; |
|
102 |
numAnswers = getShort(msg, pos); |
|
103 |
pos += 2; |
|
104 |
numAuthorities = getShort(msg, pos); |
|
105 |
pos += 2; |
|
106 |
numAdditionals = getShort(msg, pos); |
|
107 |
pos += 2; |
|
108 |
||
109 |
} catch (IndexOutOfBoundsException e) { |
|
110 |
throw new DnsResolverException("DNS error: corrupted message header"); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
/* |
|
115 |
* Returns the 2-byte unsigned value at msg[pos]. The high |
|
116 |
* order byte comes first. |
|
117 |
*/ |
|
118 |
private static int getShort(byte[] msg, int pos) { |
|
119 |
return (((msg[pos] & 0xFF) << 8) | |
|
120 |
(msg[pos + 1] & 0xFF)); |
|
121 |
} |
|
122 |
} |