|
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; |
|
39 static final short TC_BIT = (short) 0x0200; |
|
40 static final short RD_BIT = (short) 0x0100; |
|
41 static final short RA_BIT = (short) 0x0080; |
|
42 static final short RCODE_MASK = (short) 0x000F; |
|
43 |
|
44 int xid; // ID: 16-bit query identifier |
|
45 boolean query; // QR: true if query, false if response |
|
46 int opcode; // OPCODE: 4-bit opcode |
|
47 boolean authoritative; // AA |
|
48 boolean truncated; // TC |
|
49 boolean recursionDesired; // RD |
|
50 boolean recursionAvail; // RA |
|
51 int rcode; // RCODE: 4-bit response code |
|
52 int numQuestions; |
|
53 int numAnswers; |
|
54 int numAuthorities; |
|
55 int numAdditionals; |
|
56 |
|
57 /* |
|
58 * Returns a representation of a decoded DNS message header. |
|
59 * Does not modify or store a reference to the msg array. |
|
60 */ |
|
61 Header(byte[] msg, int msgLen) throws DnsResolverException { |
|
62 decode(msg, msgLen); |
|
63 } |
|
64 |
|
65 /* |
|
66 * Decodes a DNS message header. Does not modify or store a |
|
67 * reference to the msg array. |
|
68 */ |
|
69 private void decode(byte[] msg, int msgLen) throws DnsResolverException { |
|
70 |
|
71 try { |
|
72 int pos = 0; // current offset into msg |
|
73 |
|
74 if (msgLen < HEADER_SIZE) { |
|
75 throw new DnsResolverException( |
|
76 "DNS error: corrupted message header"); |
|
77 } |
|
78 |
|
79 xid = getShort(msg, pos); |
|
80 pos += 2; |
|
81 |
|
82 // Flags |
|
83 short flags = (short) getShort(msg, pos); |
|
84 pos += 2; |
|
85 query = (flags & QR_BIT) == 0; |
|
86 opcode = (flags & OPCODE_MASK) >>> OPCODE_SHIFT; |
|
87 authoritative = (flags & AA_BIT) != 0; |
|
88 truncated = (flags & TC_BIT) != 0; |
|
89 recursionDesired = (flags & RD_BIT) != 0; |
|
90 recursionAvail = (flags & RA_BIT) != 0; |
|
91 rcode = (flags & RCODE_MASK); |
|
92 |
|
93 // RR counts |
|
94 numQuestions = getShort(msg, pos); |
|
95 pos += 2; |
|
96 numAnswers = getShort(msg, pos); |
|
97 pos += 2; |
|
98 numAuthorities = getShort(msg, pos); |
|
99 pos += 2; |
|
100 numAdditionals = getShort(msg, pos); |
|
101 pos += 2; |
|
102 |
|
103 } catch (IndexOutOfBoundsException e) { |
|
104 throw new DnsResolverException("DNS error: corrupted message header"); |
|
105 } |
|
106 } |
|
107 |
|
108 /* |
|
109 * Returns the 2-byte unsigned value at msg[pos]. The high |
|
110 * order byte comes first. |
|
111 */ |
|
112 private static int getShort(byte[] msg, int pos) { |
|
113 return (((msg[pos] & 0xFF) << 8) | |
|
114 (msg[pos + 1] & 0xFF)); |
|
115 } |
|
116 } |