forked from iriscouch/dnsd
-
Notifications
You must be signed in to change notification settings - Fork 23
/
parse.js
305 lines (244 loc) · 7.43 KB
/
parse.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2012 Iris Couch, all rights reserved.
//
// Parse DNS messages
var util = require('util')
var constants = require('./constants')
module.exports = { 'id': id
, 'qr': qr
, 'aa': aa
, 'tc': tc
, 'rd': rd
, 'ra': ra
, 'ad': ad
, 'cd': cd
, 'rcode': rcode
, 'opcode': opcode
, 'record_count': record_count
, 'record_name' : record_name
, 'record_class': record_class
, 'record_ttl' : record_ttl
, 'record_type' : record_type
, 'record_data' : record_data
, 'uncompress' : uncompress
, 'sections' : sections
, 'mx': mx
, 'srv': srv
, 'soa': soa
, 'txt': txt
}
function id(msg) {
return msg.readUInt16BE(0)
}
function qr(msg) {
return msg.readUInt8(2) >> 7
}
function opcode(msg) {
return (msg.readUInt8(2) >> 3) & 0x0f
}
function aa(msg) {
return (msg.readUInt8(2) >> 2) & 0x01
}
function tc(msg) {
return (msg.readUInt8(2) >> 1) & 0x01
}
function rd(msg) {
return msg.readUInt8(2) & 0x01
}
function ra(msg) {
return msg.readUInt8(3) >> 7
}
function ad(msg) {
return msg.readUInt8(3) >> 5 & 0x01
}
function cd(msg) {
return msg.readUInt8(3) >> 4 & 0x01
}
function rcode(msg) {
return msg.readUInt8(3) & 0x0f
}
function record_count(msg, name) {
if(name == 'question')
return msg.readUInt16BE(4)
else if(name == 'answer')
return msg.readUInt16BE(6)
else if(name == 'authority')
return msg.readUInt16BE(8)
else if(name == 'additional')
return msg.readUInt16BE(10)
else
throw new Error('Unknown section name: ' + name)
}
function record_name(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.name
}
function record_class(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.class
}
function record_type(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.type
}
function record_ttl(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.ttl
}
function record_data(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.data
}
function record_class(msg, section_name, offset) {
var rec = record(msg, section_name, offset)
return rec.class
}
function record(msg, section_name, offset) {
if(typeof offset != 'number' || isNaN(offset) || offset < 0)
throw new Error('Offset must be a natural number')
// Support msg being a previously-parsed sections object.
var sects = Buffer.isBuffer(msg)
? sections(msg)
: msg
var records = sects[section_name]
if(!records)
throw new Error('No such section: "'+section_name+'"')
var rec = records[offset]
if(!rec)
throw new Error('Bad offset for section "'+section_name+'": ' + offset)
return rec
}
function sections(msg) {
// Count the times this message has been parsed, for debugging and testing purposes.
if('__parsed' in msg)
msg.__parsed += 1
var position = 12 // First byte of the first section
, result = {'question':[], 'answer':[], 'authority':[], 'additional':[]}
, need = { 'question' : record_count(msg, 'question')
, 'answer' : record_count(msg, 'answer')
, 'authority' : record_count(msg, 'authority')
, 'additional': record_count(msg, 'additional')
}
var states = ['question', 'answer', 'authority', 'additional', 'done']
, state = states.shift()
while(true) {
if(state == 'done')
return result
else if(result[state].length == need[state])
state = states.shift()
else if(!state)
throw new Error('Unknown parsing state at position '+position+': '+JSON.stringify(state))
else
add_record()
}
function add_record() {
var record = {}
var data = domain_parts(msg, position)
record.name = data.parts.join('.')
position += data.length
record.type = msg.readUInt16BE(position + 0)
record.class = msg.readUInt16BE(position + 2)
position += 4
if(state != 'question') {
record.ttl = msg.readUInt32BE(position + 0)
var rdata_len = msg.readUInt16BE(position + 4)
position += 6
record.data = msg.slice(position, position + rdata_len)
position += rdata_len
if(constants.type(record.type) === 'OPT') {
// EDNS
if(record.name !== '')
throw new Error('EDNS record option for non-root domain: ' + record.name)
record.udp_size = record.class
delete record.class
record.extended_rcode = (record.ttl >> 24)
record.edns_version = (record.ttl >> 16) & 0xff
record.zero = (record.ttl >> 8)
delete record.ttl
record.data = Array.prototype.slice.call(record.data)
}
}
result[state] = result[state] || []
result[state].push(record)
}
}
function mx(msg, data) {
return [ data.readUInt16BE(0)
, uncompress(msg, data.slice(2))
]
}
function srv(msg, data) {
return { 'priority': data.readUInt16BE(0)
, 'weight' : data.readUInt16BE(2)
, 'port' : data.readUInt16BE(4)
, 'target' : uncompress(msg, data.slice(6)) // Techncially compression is not allowed in RFC 2782.
}
}
function soa(msg, data) {
var result = domain_parts(msg, data)
, offset = result.length
, mname = result.parts.join('.')
result = domain_parts(msg, data.slice(offset))
var rname = result.parts.join('.')
offset += result.length
return { 'mname' : mname
, 'rname' : rname //.replace(/\./, '@')
, 'serial' : data.readUInt32BE(offset + 0)
, 'refresh': data.readUInt32BE(offset + 4)
, 'retry' : data.readUInt32BE(offset + 8)
, 'expire' : data.readUInt32BE(offset + 12)
, 'ttl' : data.readUInt32BE(offset + 16)
}
}
function txt(msg, data) {
var parts = []
while(data.length) {
var len = data.readUInt8(0)
parts.push(data.slice(1, 1+len).toString('ascii'))
data = data.slice(1+len)
}
return parts
}
function uncompress(msg, offset) {
var data = domain_parts(msg, offset)
return data.parts.join('.')
}
function domain_parts(msg, offset) {
if(Buffer.isBuffer(offset)) {
var full_message = msg
msg = offset
offset = 0
}
if(typeof offset != 'number' || isNaN(offset) || offset < 0 || offset > msg.length)
throw new Error('Bad offset: ' + offset)
var parts = []
, real_length = 0
, jumped = false
var i = 0
while(true) {
if(++i >= 100)
throw new Error('Too many iterations uncompressing name')
var byte = msg.readUInt8(offset)
, flags = byte >> 6
, len = byte & 0x3f // 0 - 63
offset += 1
add_length(1)
if(flags === 0x03) {
offset = (len << 8) + msg.readUInt8(offset)
add_length(1)
jumped = true
// If processing so far has just been on some given fragment, begin using the full message now.
msg = full_message || msg
}
else if(len == 0)
return {'parts':parts, 'length':real_length}
else {
parts.push(msg.toString('ascii', offset, offset + len))
offset += len
add_length(len)
}
}
function add_length(amount) {
if(! jumped)
real_length += amount
}
}