-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
175 lines (141 loc) · 5.39 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
)
const maxHeaderSize = 12
// Header -- The header contains the following fields:
//
// 1 1 1 1 1 1
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | ID |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | QDCOUNT |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | ANCOUNT |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | NSCOUNT |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | ARCOUNT |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
type Header struct {
// A 16 bit identifier assigned by the program that generates any kind of
// query. This identifier is copied the corresponding reply and can be used
// by the requester to match up replies to outstanding queries.
ID uint16
// A one bit field that specifies whether this message is a query (0), or a
// response (1).
QR QRType
// A four bit field that specifies kind of query in this message. This
// value is set by the originator of a query and copied into the response.
OPCODE Opcode
// Authoritative Answer - this bit is valid in responses, and specifies
// that the responding name server is an authority for the domain name in
// question section.
//
// Note that the contents of the answer section may have multiple owner
// names because of aliases. The AA bit corresponds to the name which
// matches the query name, or the first owner name in the answer section.
AA byte
// TrunCation - specifies that this message was truncated due to length
// greater than that permitted on the transmission channel.
TC byte
// Recursion Desired - this bit may be set in a query and is copied into
// the response. If RD is set, it directs the name server to pursue the
// query recursively. Recursive query support is optional.
RD byte
// Recursion Available - this bit is set or cleared in a response, and
// denotes whether recursive query support is available in the name server.
RA byte
// Reserved for future use. Must be zero in all queries and responses.
Z byte
// Response code - this 4 bit field is set as part of responses.
RCODE ResponseCode
// an unsigned 16 bit integer specifying the number of entries in the
// question section.
QDCOUNT uint16
// an unsigned 16 bit integer specifying the number of resource records in
// the answer section.
ANCOUNT uint16
// an unsigned 16 bit integer specifying the number of name server resource
// records in the authority records section.
NSCOUNT uint16
// an unsigned 16 bit integer specifying the number of resource records in
// the additional records section.
ARCOUNT uint16
}
// Encode formats a header accetable to be sent to a DNS server
func (h Header) Encode() ([]byte, error) {
var buf bytes.Buffer
// ID does not need bit shifting
binary.Write(&buf, binary.BigEndian, h.ID)
// Create the third octet of the header
resBytes1 := setBitsAtIdx(byte(h.QR), 0, 1)
resBytes1 |= setBitsAtIdx(byte(h.OPCODE), 1, 4)
resBytes1 |= setBitsAtIdx(h.AA, 5, 1)
resBytes1 |= setBitsAtIdx(h.TC, 6, 1)
resBytes1 |= setBitsAtIdx(h.RD, 7, 1)
buf.WriteByte(resBytes1)
// Create the fourth octet of the header
resBytes2 := setBitsAtIdx(h.RA, 0, 1)
// Z should always be 0 for future use
resBytes2 |= setBitsAtIdx(h.Z, 1, 3)
resBytes2 |= setBitsAtIdx(byte(h.RCODE), 4, 4)
buf.WriteByte(resBytes2)
// Create the final pieces of the header
binary.Write(&buf, binary.BigEndian, h.QDCOUNT)
binary.Write(&buf, binary.BigEndian, h.ANCOUNT)
binary.Write(&buf, binary.BigEndian, h.NSCOUNT)
binary.Write(&buf, binary.BigEndian, h.ARCOUNT)
return buf.Bytes(), nil
}
// DecodeHeader translates a bytes buffer into a Header instance understandable
// by a client
func DecodeHeader(data []byte, bytesRead int, h *Header) (int, error) {
if h == nil {
return 0, errors.New("Cannot decode bytes to nil Header")
}
if len(data) < maxHeaderSize {
return 0, fmt.Errorf("Header bytes should be %d bytes, found %d", maxHeaderSize, len(data))
}
var err error
h.ID, bytesRead, err = decodeUint16(data, bytesRead)
if err != nil {
return bytesRead, err
}
currentByte := data[bytesRead]
h.QR = QRType(getBitsAtIdx(currentByte, 0, 1))
h.OPCODE = Opcode(getBitsAtIdx(currentByte, 1, 4))
h.AA = getBitsAtIdx(currentByte, 5, 1)
h.TC = getBitsAtIdx(currentByte, 6, 1)
h.RD = getBitsAtIdx(currentByte, 7, 1)
bytesRead++
currentByte = data[bytesRead]
h.RA = getBitsAtIdx(currentByte, 0, 1)
h.Z = getBitsAtIdx(currentByte, 1, 3)
h.RCODE = ResponseCode(getBitsAtIdx(currentByte, 4, 4))
bytesRead++
// Set the remaining data
h.QDCOUNT, bytesRead, err = decodeUint16(data, bytesRead)
if err != nil {
return bytesRead, err
}
h.ANCOUNT, bytesRead, err = decodeUint16(data, bytesRead)
if err != nil {
return bytesRead, err
}
h.NSCOUNT, bytesRead, err = decodeUint16(data, bytesRead)
if err != nil {
return bytesRead, err
}
h.ARCOUNT, bytesRead, err = decodeUint16(data, bytesRead)
if err != nil {
return bytesRead, err
}
return bytesRead, err
}