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
|
// shunt - DNS message parser
//
// Parses a response far enough to answer: which name was asked for, and
// which A/AAAA addresses came back. Never trusts a length off the wire.
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 Dirk Brenken <dev@brenken.org>
// Hard limits: message size, name length, answers processed per message.
export const LIM = {
msg: 4096,
labels: 63,
name: 255,
answers: 64
};
export const TYPE = {
A: 1,
NS: 2,
CNAME: 5,
SOA: 6,
TXT: 16,
AAAA: 28,
OPT: 41
};
// Parser verdicts. These identifiers are contract - fixtures and the LuCI
// labels compare them verbatim.
export const ERR = {
SHORT: 'E_SHORT',
MSGLEN: 'E_MSGLEN',
NOTRESP: 'E_NOTRESP',
TRUNC: 'E_TRUNC',
RCODE: 'E_RCODE',
QDCOUNT: 'E_QDCOUNT',
QPTR: 'E_QPTR',
LABEL: 'E_LABEL',
NAMELEN: 'E_NAMELEN',
CHARSET: 'E_CHARSET',
RDLEN: 'E_RDLEN',
ANSMAX: 'E_ANSMAX'
};
const HDR_LEN = 12;
const RR_FIXED = 10;
const F_QR = 0x8000;
const F_TC = 0x0200;
const M_RCODE = 0x000f;
const LBL_MASK = 0xc0;
const LBL_PTR = 0xc0;
function u16at(buf, off) {
return (ord(buf, off) << 8) | ord(buf, off + 1);
}
function fmt4(buf, off) {
return sprintf('%d.%d.%d.%d',
ord(buf, off), ord(buf, off + 1),
ord(buf, off + 2), ord(buf, off + 3));
}
function fmt6(buf, off) {
let g = [];
for (let i = 0; i < 8; i++)
push(g, u16at(buf, off + i * 2));
let bs = -1, bl = 0, cs = -1, cl = 0;
for (let i = 0; i < 8; i++) {
if (g[i] != 0) {
cs = -1;
cl = 0;
continue;
}
if (cs < 0)
cs = i;
cl++;
if (cl > bl) {
bs = cs;
bl = cl;
}
}
if (bl < 2) {
bs = -1;
bl = 0;
}
let parts = [], i = 0;
while (i < 8) {
if (i == bs) {
push(parts, '');
i += bl;
continue;
}
push(parts, sprintf('%x', g[i]));
i++;
}
let out = join(':', parts);
if (bs == 0)
out = ':' + out;
if (bs >= 0 && bs + bl == 8)
out = out + ':';
return out;
}
export function decode_name(buf, off) {
let blen = length(buf), labels = [], total = 1;
while (true) {
if (off >= blen)
return { err: ERR.SHORT };
let len = ord(buf, off);
if ((len & LBL_MASK) == LBL_PTR)
return { err: ERR.QPTR };
if (len & LBL_MASK)
return { err: ERR.LABEL };
off++;
if (!len)
break;
total += len + 1;
if (total > LIM.name)
return { err: ERR.NAMELEN };
if (off + len > blen)
return { err: ERR.SHORT };
let lbl = lc(substr(buf, off, len));
for (let i = 0; i < len; i++) {
let c = ord(lbl, i);
if ((c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39) ||
c == 0x2d || c == 0x5f)
continue;
return { err: ERR.CHARSET };
}
push(labels, lbl);
off += len;
}
return { name: join('.', labels), next: off };
};
export function skip_name(buf, off) {
let blen = length(buf);
while (true) {
if (off >= blen)
return null;
let len = ord(buf, off);
if ((len & LBL_MASK) == LBL_PTR)
return (off + 2 <= blen) ? off + 2 : null;
if (len & LBL_MASK)
return null;
off++;
if (!len)
return off;
off += len;
if (off > blen)
return null;
}
};
export function parse(buf) {
let blen = length(buf ?? '');
if (blen > LIM.msg)
return { ok: false, err: ERR.MSGLEN };
if (blen < HDR_LEN)
return { ok: false, err: ERR.SHORT };
let flags = u16at(buf, 2);
if (!(flags & F_QR))
return { ok: false, err: ERR.NOTRESP };
if (flags & F_TC)
return { ok: false, err: ERR.TRUNC };
if (flags & M_RCODE)
return { ok: false, err: ERR.RCODE };
if (u16at(buf, 4) != 1)
return { ok: false, err: ERR.QDCOUNT };
let ancount = u16at(buf, 6);
if (ancount > LIM.answers)
return { ok: false, err: ERR.ANSMAX };
let q = decode_name(buf, HDR_LEN);
if (q.err)
return { ok: false, err: q.err };
let off = q.next;
if (off + 4 > blen)
return { ok: false, err: ERR.SHORT };
let qtype = u16at(buf, off);
off += 4;
let a = [], aaaa = [];
for (let i = 0; i < ancount; i++) {
off = skip_name(buf, off);
if (off === null)
return { ok: false, err: ERR.SHORT };
if (off + RR_FIXED > blen)
return { ok: false, err: ERR.SHORT };
let rtype = u16at(buf, off);
let rdlen = u16at(buf, off + 8);
off += RR_FIXED;
if (off + rdlen > blen)
return { ok: false, err: ERR.RDLEN };
if (rtype == TYPE.A) {
if (rdlen != 4)
return { ok: false, err: ERR.RDLEN };
push(a, fmt4(buf, off));
}
else if (rtype == TYPE.AAAA) {
if (rdlen != 16)
return { ok: false, err: ERR.RDLEN };
push(aaaa, fmt6(buf, off));
}
off += rdlen;
}
return {
ok: true,
id: u16at(buf, 0),
qname: q.name,
qtype,
a,
aaaa
};
};
|