| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use ascii; |
| 5 |
use endian; |
| 6 |
use net::ip; |
| 7 |
use strings; |
| 8 |
|
| 9 |
type decoder = struct { |
| 10 |
buf: []u8, |
| 11 |
cur: []u8, |
| 12 |
}; |
| 13 |
|
| 14 |
// Decodes a DNS message, heap allocating the resources necessary to represent |
| 15 |
// it in Hare's type system. The caller must use [[message_free]] to free the |
| 16 |
// return value. |
| 17 |
export fn decode(buf: []u8) (*message | format | nomem) = { |
| 18 |
let success = false; |
| 19 |
let msg = alloc(message { ... })?; |
| 20 |
defer if (!success) message_free(msg); |
| 21 |
let dec = decoder_init(buf); |
| 22 |
decode_header(&dec, &msg.header)?; |
| 23 |
for (let i = 0z; i < msg.header.qdcount; i += 1) { |
| 24 |
let question = decode_question(&dec)?; |
| 25 |
if (append(msg.questions, question) is nomem) { |
| 26 |
strings::freeall(question.qname); |
| 27 |
}; |
| 28 |
}; |
| 29 |
decode_rrecords(&dec, msg.header.ancount, &msg.answers)?; |
| 30 |
decode_rrecords(&dec, msg.header.nscount, &msg.authority)?; |
| 31 |
decode_rrecords(&dec, msg.header.arcount, &msg.additional)?; |
| 32 |
success = true; |
| 33 |
return msg; |
| 34 |
}; |
| 35 |
|
| 36 |
fn decode_rrecords( |
| 37 |
dec: *decoder, |
| 38 |
count: u16, |
| 39 |
out: *[]rrecord, |
| 40 |
) (void | format | nomem) = { |
| 41 |
for (let i = 0z; i < count; i += 1) { |
| 42 |
let r = decode_rrecord(dec)?; |
| 43 |
if (append(out, r) is nomem) { |
| 44 |
rrecord_finish(&r); |
| 45 |
return nomem; |
| 46 |
}; |
| 47 |
}; |
| 48 |
}; |
| 49 |
|
| 50 |
fn decoder_init(buf: []u8) decoder = decoder { |
| 51 |
buf = buf, |
| 52 |
cur = buf, |
| 53 |
... |
| 54 |
}; |
| 55 |
|
| 56 |
fn decode_u8(dec: *decoder) (u8 | format) = { |
| 57 |
if (len(dec.cur) < 1) { |
| 58 |
return format; |
| 59 |
}; |
| 60 |
const val = dec.cur[0]; |
| 61 |
dec.cur = dec.cur[1..]; |
| 62 |
return val; |
| 63 |
}; |
| 64 |
|
| 65 |
fn decode_u16(dec: *decoder) (u16 | format) = { |
| 66 |
if (len(dec.cur) < 2) { |
| 67 |
return format; |
| 68 |
}; |
| 69 |
const val = endian::begetu16(dec.cur); |
| 70 |
dec.cur = dec.cur[2..]; |
| 71 |
return val; |
| 72 |
}; |
| 73 |
|
| 74 |
fn decode_u32(dec: *decoder) (u32 | format) = { |
| 75 |
if (len(dec.cur) < 4) { |
| 76 |
return format; |
| 77 |
}; |
| 78 |
const val = endian::begetu32(dec.cur); |
| 79 |
dec.cur = dec.cur[4..]; |
| 80 |
return val; |
| 81 |
}; |
| 82 |
|
| 83 |
fn decode_u48(dec: *decoder) (u64 | format) = { |
| 84 |
if (len(dec.cur) < 6) { |
| 85 |
return format; |
| 86 |
}; |
| 87 |
let buf: [8]u8 = [0...]; |
| 88 |
buf[2..] = dec.cur[..6]; |
| 89 |
const val = endian::begetu64(buf[..]); |
| 90 |
dec.cur = dec.cur[6..]; |
| 91 |
return val; |
| 92 |
}; |
| 93 |
|
| 94 |
fn decode_header(dec: *decoder, head: *header) (void | format) = { |
| 95 |
head.id = decode_u16(dec)?; |
| 96 |
const rawop = decode_u16(dec)?; |
| 97 |
decode_op(rawop, &head.op); |
| 98 |
head.qdcount = decode_u16(dec)?; |
| 99 |
head.ancount = decode_u16(dec)?; |
| 100 |
head.nscount = decode_u16(dec)?; |
| 101 |
head.arcount = decode_u16(dec)?; |
| 102 |
}; |
| 103 |
|
| 104 |
fn decode_op(in: u16, out: *op) void = { |
| 105 |
out.qr = ((in & 0b1000000000000000) >> 15): qr; |
| 106 |
out.opcode = ((in & 0b0111100000000000u16) >> 11): opcode; |
| 107 |
out.aa = in & 0b0000010000000000u16 != 0; |
| 108 |
out.tc = in & 0b0000001000000000u16 != 0; |
| 109 |
out.rd = in & 0b0000000100000000u16 != 0; |
| 110 |
out.ra = in & 0b0000000010000000u16 != 0; |
| 111 |
out.rcode = (in & 0b1111): rcode; |
| 112 |
}; |
| 113 |
|
| 114 |
fn decode_name(dec: *decoder) ([]str | format | nomem) = { |
| 115 |
let success = false; |
| 116 |
let names: []str = []; |
| 117 |
defer if (!success) strings::freeall(names); |
| 118 |
let totalsize = 0z; |
| 119 |
let sub = decoder { |
| 120 |
buf = dec.buf, |
| 121 |
... |
| 122 |
}; |
| 123 |
for (let i = 0z; i < len(dec.buf); i += 2) { |
| 124 |
if (len(dec.cur) < 1) { |
| 125 |
return format; |
| 126 |
}; |
| 127 |
const z = dec.cur[0]; |
| 128 |
if (z & 0b11000000 == 0b11000000) { |
| 129 |
const offs = decode_u16(dec)? & ~0b1100000000000000u16; |
| 130 |
if (len(dec.buf) < offs) { |
| 131 |
return format; |
| 132 |
}; |
| 133 |
sub.cur = dec.buf[offs..]; |
| 134 |
dec = ⊂ |
| 135 |
continue; |
| 136 |
}; |
| 137 |
dec.cur = dec.cur[1..]; |
| 138 |
totalsize += z + 1; |
| 139 |
if (totalsize > 255) { |
| 140 |
return format; |
| 141 |
}; |
| 142 |
if (z == 0) { |
| 143 |
success = true; |
| 144 |
return names; |
| 145 |
}; |
| 146 |
|
| 147 |
if (len(dec.cur) < z) { |
| 148 |
return format; |
| 149 |
}; |
| 150 |
const name = match (strings::fromutf8(dec.cur[..z])) { |
| 151 |
case let name: str => |
| 152 |
yield name; |
| 153 |
case => |
| 154 |
return format; |
| 155 |
}; |
| 156 |
dec.cur = dec.cur[z..]; |
| 157 |
if (!ascii::validstr(name)) { |
| 158 |
return format; |
| 159 |
}; |
| 160 |
|
| 161 |
const name = strings::dup(name)?; |
| 162 |
match (append(names, name)) { |
| 163 |
case void => void; |
| 164 |
case nomem => |
| 165 |
free(name); |
| 166 |
return nomem; |
| 167 |
}; |
| 168 |
}; |
| 169 |
return format; |
| 170 |
}; |
| 171 |
|
| 172 |
fn decode_question(dec: *decoder) (question | format | nomem) = { |
| 173 |
let success = false; |
| 174 |
const qname = decode_name(dec)?; |
| 175 |
defer if (!success) strings::freeall(qname); |
| 176 |
const qtype = decode_u16(dec)?: qtype; |
| 177 |
const qclass = decode_u16(dec)?: qclass; |
| 178 |
success = true; |
| 179 |
return question { |
| 180 |
qname = qname, |
| 181 |
qtype = qtype, |
| 182 |
qclass = qclass, |
| 183 |
}; |
| 184 |
}; |
| 185 |
|
| 186 |
fn decode_rrecord(dec: *decoder) (rrecord | format | nomem) = { |
| 187 |
let success = false; |
| 188 |
const name = decode_name(dec)?; |
| 189 |
defer if (!success) strings::freeall(name); |
| 190 |
const rtype = decode_u16(dec)?: rtype; |
| 191 |
const class = decode_u16(dec)?: class; |
| 192 |
const ttl = decode_u32(dec)?; |
| 193 |
const rlen = decode_u16(dec)?; |
| 194 |
const rdata = decode_rdata(dec, rtype, rlen)?; |
| 195 |
success = true; |
| 196 |
return rrecord { |
| 197 |
name = name, |
| 198 |
rtype = rtype, |
| 199 |
class = class, |
| 200 |
ttl = ttl, |
| 201 |
rdata = rdata |
| 202 |
}; |
| 203 |
}; |
| 204 |
|
| 205 |
fn decode_rdata(dec: *decoder, rtype: rtype, rlen: size) (rdata | format | nomem) = { |
| 206 |
if (len(dec.cur) < rlen) { |
| 207 |
return format; |
| 208 |
}; |
| 209 |
let sub = decoder { |
| 210 |
cur = dec.cur[..rlen], |
| 211 |
buf = dec.buf, |
| 212 |
}; |
| 213 |
dec.cur = dec.cur[rlen..]; |
| 214 |
switch (rtype) { |
| 215 |
case rtype::A => |
| 216 |
return decode_a(&sub); |
| 217 |
case rtype::AAAA => |
| 218 |
return decode_aaaa(&sub); |
| 219 |
case rtype::CAA => |
| 220 |
return decode_caa(&sub); |
| 221 |
case rtype::CNAME => |
| 222 |
return decode_cname(&sub); |
| 223 |
case rtype::DNSKEY => |
| 224 |
return decode_dnskey(&sub); |
| 225 |
case rtype::MX => |
| 226 |
return decode_mx(&sub); |
| 227 |
case rtype::NS => |
| 228 |
return decode_ns(&sub); |
| 229 |
case rtype::OPT => |
| 230 |
return decode_opt(&sub); |
| 231 |
case rtype::NSEC => |
| 232 |
return decode_nsec(&sub); |
| 233 |
case rtype::PTR => |
| 234 |
return decode_ptr(&sub); |
| 235 |
case rtype::RRSIG => |
| 236 |
return decode_rrsig(&sub); |
| 237 |
case rtype::SOA => |
| 238 |
return decode_soa(&sub); |
| 239 |
case rtype::SRV => |
| 240 |
return decode_srv(&sub); |
| 241 |
case rtype::SSHFP => |
| 242 |
return decode_sshfp(&sub); |
| 243 |
case rtype::TSIG => |
| 244 |
return decode_tsig(&sub); |
| 245 |
case rtype::TXT => |
| 246 |
return decode_txt(&sub); |
| 247 |
case => |
| 248 |
return sub.cur: unknown_rdata; |
| 249 |
}; |
| 250 |
}; |
| 251 |
|
| 252 |
fn decode_a(dec: *decoder) (rdata | format) = { |
| 253 |
if (len(dec.cur) < 4) { |
| 254 |
return format; |
| 255 |
}; |
| 256 |
let ip: ip::addr4 = [0...]; |
| 257 |
ip[..] = dec.cur[..4]; |
| 258 |
dec.cur = dec.cur[4..]; |
| 259 |
return ip: a; |
| 260 |
}; |
| 261 |
|
| 262 |
fn decode_aaaa(dec: *decoder) (rdata | format) = { |
| 263 |
if (len(dec.cur) < 16) { |
| 264 |
return format; |
| 265 |
}; |
| 266 |
let ip: ip::addr6 = [0...]; |
| 267 |
ip[..] = dec.cur[..16]; |
| 268 |
dec.cur = dec.cur[16..]; |
| 269 |
return ip: aaaa; |
| 270 |
}; |
| 271 |
|
| 272 |
fn decode_caa(dec: *decoder) (rdata | format | nomem) = { |
| 273 |
let flags = decode_u8(dec)?; |
| 274 |
let tag_len = decode_u8(dec)?; |
| 275 |
|
| 276 |
if (len(dec.cur) < tag_len) { |
| 277 |
return format; |
| 278 |
}; |
| 279 |
let tag = match(strings::fromutf8(dec.cur[..tag_len])) { |
| 280 |
case let t: str => |
| 281 |
yield t; |
| 282 |
case => |
| 283 |
return format; |
| 284 |
}; |
| 285 |
let value = match (strings::fromutf8(dec.cur[tag_len..])) { |
| 286 |
case let v: str => |
| 287 |
yield v; |
| 288 |
case => |
| 289 |
return format; |
| 290 |
}; |
| 291 |
|
| 292 |
let caa = caa { |
| 293 |
flags = flags, |
| 294 |
tag = strings::dup(tag)?, |
| 295 |
... |
| 296 |
}; |
| 297 |
|
| 298 |
match (strings::dup(value)) { |
| 299 |
case let s: str => |
| 300 |
caa.value = s; |
| 301 |
case nomem => |
| 302 |
free(caa.tag); |
| 303 |
return nomem; |
| 304 |
}; |
| 305 |
|
| 306 |
return caa; |
| 307 |
}; |
| 308 |
|
| 309 |
fn decode_cname(dec: *decoder) (rdata | format | nomem) = { |
| 310 |
return cname { |
| 311 |
name = decode_name(dec)?, |
| 312 |
}; |
| 313 |
}; |
| 314 |
|
| 315 |
fn decode_dnskey(dec: *decoder) (rdata | format | nomem) = { |
| 316 |
let r = dnskey { |
| 317 |
flags = decode_u16(dec)?, |
| 318 |
protocol = decode_u8(dec)?, |
| 319 |
algorithm = decode_u8(dec)?, |
| 320 |
key = [], |
| 321 |
}; |
| 322 |
append(r.key, dec.cur[..]...)?; |
| 323 |
return r; |
| 324 |
}; |
| 325 |
|
| 326 |
fn decode_mx(dec: *decoder) (rdata | format | nomem) = { |
| 327 |
return mx { |
| 328 |
priority = decode_u16(dec)?, |
| 329 |
name = decode_name(dec)?, |
| 330 |
}; |
| 331 |
}; |
| 332 |
|
| 333 |
fn decode_ns(dec: *decoder) (rdata | format | nomem) = { |
| 334 |
return ns { |
| 335 |
name = decode_name(dec)?, |
| 336 |
}; |
| 337 |
}; |
| 338 |
|
| 339 |
fn decode_opt(dec: *decoder) (rdata | format | nomem) = { |
| 340 |
let success = false; |
| 341 |
let r = opt { |
| 342 |
options = [], |
| 343 |
}; |
| 344 |
defer if (!success) { |
| 345 |
for (let i = 0z; i < len(r.options); i += 1) { |
| 346 |
free(r.options[i].data); |
| 347 |
}; |
| 348 |
free(r.options); |
| 349 |
}; |
| 350 |
for (len(dec.cur) > 0) { |
| 351 |
let o = edns_opt { |
| 352 |
code = decode_u16(dec)?, |
| 353 |
data = [], |
| 354 |
}; |
| 355 |
let sz = decode_u16(dec)?; |
| 356 |
if (len(dec.cur) < sz) { |
| 357 |
return format; |
| 358 |
}; |
| 359 |
append(o.data, dec.cur[..sz]...)?; |
| 360 |
dec.cur = dec.cur[sz..]; |
| 361 |
if (append(r.options, o) is nomem) { |
| 362 |
free(o.data); |
| 363 |
return nomem; |
| 364 |
}; |
| 365 |
}; |
| 366 |
success = true; |
| 367 |
return r; |
| 368 |
}; |
| 369 |
|
| 370 |
fn decode_nsec(dec: *decoder) (rdata | format | nomem) = { |
| 371 |
let r = nsec { |
| 372 |
next_domain = decode_name(dec)?, |
| 373 |
type_bitmaps = [], |
| 374 |
}; |
| 375 |
if (append(r.type_bitmaps, dec.cur[..]...) is nomem) { |
| 376 |
strings::freeall(r.next_domain); |
| 377 |
return nomem; |
| 378 |
}; |
| 379 |
return r; |
| 380 |
}; |
| 381 |
|
| 382 |
fn decode_ptr(dec: *decoder) (rdata | format | nomem) = { |
| 383 |
return ptr { |
| 384 |
name = decode_name(dec)?, |
| 385 |
}; |
| 386 |
}; |
| 387 |
|
| 388 |
fn decode_rrsig(dec: *decoder) (rdata | format | nomem) = { |
| 389 |
let r = rrsig { |
| 390 |
type_covered = decode_u16(dec)?, |
| 391 |
algorithm = decode_u8(dec)?, |
| 392 |
labels = decode_u8(dec)?, |
| 393 |
orig_ttl = decode_u32(dec)?, |
| 394 |
sig_expiration = decode_u32(dec)?, |
| 395 |
sig_inception = decode_u32(dec)?, |
| 396 |
key_tag = decode_u16(dec)?, |
| 397 |
signer_name = decode_name(dec)?, |
| 398 |
signature = [], |
| 399 |
}; |
| 400 |
|
| 401 |
if (append(r.signature, dec.cur[..]...) is nomem) { |
| 402 |
strings::freeall(r.signer_name); |
| 403 |
return nomem; |
| 404 |
}; |
| 405 |
return r; |
| 406 |
}; |
| 407 |
|
| 408 |
fn decode_soa(dec: *decoder) (rdata | format | nomem) = { |
| 409 |
let s = soa { ... }; |
| 410 |
let success = true; |
| 411 |
defer if (!success) { |
| 412 |
strings::freeall(s.mname); |
| 413 |
strings::freeall(s.rname); |
| 414 |
}; |
| 415 |
|
| 416 |
s.mname = decode_name(dec)?; |
| 417 |
s.rname = decode_name(dec)?; |
| 418 |
s.serial = decode_u32(dec)?; |
| 419 |
s.refresh = decode_u32(dec)?; |
| 420 |
s.retry = decode_u32(dec)?; |
| 421 |
s.expire = decode_u32(dec)?; |
| 422 |
|
| 423 |
success = true; |
| 424 |
return s; |
| 425 |
}; |
| 426 |
|
| 427 |
fn decode_srv(dec: *decoder) (rdata | format | nomem) = { |
| 428 |
return srv { |
| 429 |
priority = decode_u16(dec)?, |
| 430 |
weight = decode_u16(dec)?, |
| 431 |
port = decode_u16(dec)?, |
| 432 |
target = decode_name(dec)?, |
| 433 |
}; |
| 434 |
}; |
| 435 |
|
| 436 |
fn decode_sshfp(dec: *decoder) (rdata | format | nomem) = { |
| 437 |
let r = sshfp { |
| 438 |
algorithm = decode_u8(dec)?, |
| 439 |
fp_type = decode_u8(dec)?, |
| 440 |
fingerprint = [], |
| 441 |
}; |
| 442 |
append(r.fingerprint, dec.cur[..]...)?; |
| 443 |
return r; |
| 444 |
}; |
| 445 |
|
| 446 |
fn decode_tsig(dec: *decoder) (rdata | format | nomem) = { |
| 447 |
let success = false; |
| 448 |
let r = tsig { |
| 449 |
algorithm = decode_name(dec)?, |
| 450 |
... |
| 451 |
}; |
| 452 |
defer if (!success) strings::freeall(r.algorithm); |
| 453 |
|
| 454 |
r.time_signed = decode_u48(dec)?; |
| 455 |
r.fudge = decode_u16(dec)?; |
| 456 |
r.mac_len = decode_u16(dec)?; |
| 457 |
|
| 458 |
if (len(dec.cur) < r.mac_len) { |
| 459 |
return format; |
| 460 |
}; |
| 461 |
append(r.mac, dec.cur[..r.mac_len]...)?; |
| 462 |
defer if (!success) free(r.mac); |
| 463 |
dec.cur = dec.cur[r.mac_len..]; |
| 464 |
|
| 465 |
r.orig_id = decode_u16(dec)?; |
| 466 |
r.error = decode_u16(dec)?; |
| 467 |
r.other_len = decode_u16(dec)?; |
| 468 |
|
| 469 |
if (len(dec.cur) != r.other_len) { |
| 470 |
return format; |
| 471 |
}; |
| 472 |
if (r.other_len > 0) { |
| 473 |
append(r.other_data, dec.cur[..]...)?; |
| 474 |
}; |
| 475 |
|
| 476 |
success = true; |
| 477 |
return r; |
| 478 |
}; |
| 479 |
|
| 480 |
fn decode_txt(dec: *decoder) (rdata | format | nomem) = { |
| 481 |
let success = false; |
| 482 |
let items: txt = []; |
| 483 |
defer if (!success) bytes_free(items); |
| 484 |
for (len(dec.cur) != 0) { |
| 485 |
const ln = decode_u8(dec)?; |
| 486 |
if (len(dec.cur) < ln) { |
| 487 |
return format; |
| 488 |
}; |
| 489 |
let item: []u8 = []; |
| 490 |
append(item, dec.cur[..ln]...)?; |
| 491 |
dec.cur = dec.cur[ln..]; |
| 492 |
if (append(items, item) is nomem) { |
| 493 |
free(item); |
| 494 |
return nomem; |
| 495 |
}; |
| 496 |
}; |
| 497 |
success = true; |
| 498 |
return items; |
| 499 |
}; |
| 500 |
|
| 501 |
// TODO: Expand breadth of supported rdata decoders |