Software /
code /
prosody
Annotate
util/dns.lua @ 12255:a3ad9cf740d6
util.pluginloader: Fix method to return any module metadata (luacheck)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 04 Feb 2022 15:04:20 +0000 |
parent | 12241:dd15f42f6312 |
child | 12288:08a933450922 |
rev | line source |
---|---|
10961
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- libunbound based net.adns replacement for Prosody IM |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2012-2015 Kim Alvefur |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2012 Waqas Hussain |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- This file is MIT licensed. |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local setmetatable = setmetatable; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local table = table; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local t_concat = table.concat; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local t_insert = table.insert; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local s_byte = string.byte; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local s_char = string.char; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local s_format = string.format; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local s_gsub = string.gsub; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local s_sub = string.sub; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local s_match = string.match; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local s_gmatch = string.gmatch; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local have_net, net_util = pcall(require, "util.net"); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
12236
d0dfd48806f9
util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents:
10972
diff
changeset
|
21 local iana_data = require "util.dnsregistry"; |
10961
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 if have_net and not net_util.ntop then -- Added in Prosody 0.11 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 have_net = false; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local chartohex = {}; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 for c = 0, 255 do |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 chartohex[s_char(c)] = s_format("%02X", c); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local function tohex(s) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return (s_gsub(s, ".", chartohex)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 -- Simplified versions of Waqas DNS parsers |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 -- Only the per RR parsers are needed and only feed a single RR |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local parsers = {}; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- No support for pointers, but libunbound appears to take care of that. |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local function readDnsName(packet, pos) |
12239
578ce0415398
util.dns: Fix returning read position after zero-length name
Kim Alvefur <zash@zash.se>
parents:
12236
diff
changeset
|
43 if s_byte(packet, pos) == 0 then return ".", pos+1; end |
10961
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local pack_len, r, len = #packet, {}; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 pos = pos or 1; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 repeat |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 len = s_byte(packet, pos) or 0; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 t_insert(r, s_sub(packet, pos + 1, pos + len)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 pos = pos + len + 1; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 until len == 0 or pos >= pack_len; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 return t_concat(r, "."), pos; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 -- These are just simple names. |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 parsers.CNAME = readDnsName; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 parsers.NS = readDnsName |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 parsers.PTR = readDnsName; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local soa_mt = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 __tostring = function(rr) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return s_format("%s %s %d %d %d %d %d", rr.mname, rr.rname, rr.serial, rr.refresh, rr.retry, rr.expire, rr.minimum); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 function parsers.SOA(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local mname, rname, offset; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 mname, offset = readDnsName(packet, 1); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 rname, offset = readDnsName(packet, offset); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 -- Extract all the bytes of these fields in one call |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 s1, s2, s3, s4, -- serial |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 r1, r2, r3, r4, -- refresh |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 t1, t2, t3, t4, -- retry |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 e1, e2, e3, e4, -- expire |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 m1, m2, m3, m4 -- minimum |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 = s_byte(packet, offset, offset + 19); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 return setmetatable({ |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 mname = mname; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 rname = rname; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 serial = s1*0x1000000 + s2*0x10000 + s3*0x100 + s4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 refresh = r1*0x1000000 + r2*0x10000 + r3*0x100 + r4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 retry = t1*0x1000000 + t2*0x10000 + t3*0x100 + t4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 expire = e1*0x1000000 + e2*0x10000 + e3*0x100 + e4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 minimum = m1*0x1000000 + m2*0x10000 + m3*0x100 + m4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 }, soa_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 function parsers.A(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 return s_format("%d.%d.%d.%d", s_byte(packet, 1, 4)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local aaaa = { nil, nil, nil, nil, nil, nil, nil, nil, }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 function parsers.AAAA(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 local hi, lo, ip, len, token; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 for i = 1, 8 do |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 hi, lo = s_byte(packet, i * 2 - 1, i * 2); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 aaaa[i] = s_format("%x", hi * 256 + lo); -- skips leading zeros |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 ip = t_concat(aaaa, ":", 1, 8); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 len = (s_match(ip, "^0:[0:]+()") or 1) - 1; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 for s in s_gmatch(ip, ":0:[0:]+") do |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if len < #s then len, token = #s, s; end -- find longest sequence of zeros |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 return (s_gsub(ip, token or "^0:[0:]+", "::", 1)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 if have_net then |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 parsers.A = net_util.ntop; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 parsers.AAAA = net_util.ntop; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 local mx_mt = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 __tostring = function(rr) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 return s_format("%d %s", rr.pref, rr.mx) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 function parsers.MX(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 local name = readDnsName(packet, 3); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 local b1,b2 = s_byte(packet, 1, 2); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 return setmetatable({ |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 pref = b1*256+b2; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 mx = name; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 }, mx_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 local srv_mt = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 __tostring = function(rr) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 return s_format("%d %d %d %s", rr.priority, rr.weight, rr.port, rr.target); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 function parsers.SRV(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 local name = readDnsName(packet, 7); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 local b1, b2, b3, b4, b5, b6 = s_byte(packet, 1, 6); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 return setmetatable({ |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 priority = b1*256+b2; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 weight = b3*256+b4; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 port = b5*256+b6; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 target = name; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 }, srv_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 local txt_mt = { __tostring = t_concat }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 function parsers.TXT(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 local pack_len = #packet; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 local r, pos, len = {}, 1; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 repeat |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 len = s_byte(packet, pos) or 0; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 t_insert(r, s_sub(packet, pos + 1, pos + len)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 pos = pos + len + 1; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 until pos >= pack_len; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 return setmetatable(r, txt_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 parsers.SPF = parsers.TXT; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 -- Acronyms from RFC 7218 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 local tlsa_usages = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 [0] = "PKIX-CA"; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 [1] = "PKIX-EE"; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 [2] = "DANE-TA"; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 [3] = "DANE-EE"; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 [255] = "PrivCert"; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local tlsa_selectors = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 [0] = "Cert", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 [1] = "SPKI", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 [255] = "PrivSel", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 local tlsa_match_types = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 [0] = "Full", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 [1] = "SHA2-256", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 [2] = "SHA2-512", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 [255] = "PrivMatch", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 local tlsa_mt = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 __tostring = function(rr) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 return s_format("%s %s %s %s", |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 tlsa_usages[rr.use] or rr.use, |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 tlsa_selectors[rr.select] or rr.select, |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 tlsa_match_types[rr.match] or rr.match, |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 tohex(rr.data)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 __index = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 getUsage = function(rr) return tlsa_usages[rr.use] end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 getSelector = function(rr) return tlsa_selectors[rr.select] end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 getMatchType = function(rr) return tlsa_match_types[rr.match] end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 } |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 function parsers.TLSA(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 local use, select, match = s_byte(packet, 1,3); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 return setmetatable({ |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 use = use; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 select = select; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 match = match; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 data = s_sub(packet, 4); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 }, tlsa_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 |
12241
dd15f42f6312
util.dns: Minor updates of SVCB parser
Kim Alvefur <zash@zash.se>
parents:
12240
diff
changeset
|
201 local svcb_params = {"alpn"; "no-default-alpn"; "port"; "ipv4hint"; "ech"; "ipv6hint"}; |
12240
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
202 setmetatable(svcb_params, {__index = function(_, n) return "key" .. tostring(n); end}); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
203 |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
204 local svcb_mt = { |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
205 __tostring = function (rr) |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
206 local kv = {}; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
207 for i = 1, #rr.fields do |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
208 t_insert(kv, s_format("%s=%q", svcb_params[rr.fields[i].key], tostring(rr.fields[i].value))); |
12241
dd15f42f6312
util.dns: Minor updates of SVCB parser
Kim Alvefur <zash@zash.se>
parents:
12240
diff
changeset
|
209 -- FIXME the =value part may be omitted when the value is "empty" |
12240
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
210 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
211 return s_format("%d %s %s", rr.prio, rr.name, t_concat(kv, " ")); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
212 end; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
213 }; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
214 local svbc_ip_mt = {__tostring = function(ip) return t_concat(ip, ", "); end} |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
215 |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
216 function parsers.SVCB(packet) |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
217 local prio_h, prio_l = packet:byte(1,2); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
218 local prio = prio_h*256+prio_l; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
219 local name, pos = readDnsName(packet, 3); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
220 local fields = {}; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
221 while #packet > pos do |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
222 local key_h, key_l = packet:byte(pos+0,pos+1); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
223 local len_h, len_l = packet:byte(pos+2,pos+3); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
224 local key = key_h*256+key_l; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
225 local len = len_h*256+len_l; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
226 local value = packet:sub(pos+4,pos+4-1+len) |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
227 if key == 1 then |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
228 value = setmetatable(parsers.TXT(value), svbc_ip_mt); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
229 elseif key == 3 then |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
230 local port_h, port_l = value:byte(1,2); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
231 local port = port_h*256+port_l; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
232 value = port; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
233 elseif key == 4 then |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
234 local ip = {}; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
235 for i = 1, #value, 4 do |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
236 t_insert(ip, parsers.A(value:sub(i, i+3))); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
237 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
238 value = setmetatable(ip, svbc_ip_mt); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
239 elseif key == 6 then |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
240 local ip = {}; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
241 for i = 1, #value, 16 do |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
242 t_insert(ip, parsers.AAAA(value:sub(i, i+15))); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
243 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
244 value = setmetatable(ip, svbc_ip_mt); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
245 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
246 t_insert(fields, { key = key, value = value, len = len }); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
247 pos = pos+len+4; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
248 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
249 return setmetatable({ |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
250 prio = prio, name = name, fields = fields, |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
251 }, svcb_mt); |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
252 end |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
253 |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
254 parsers.HTTPS = parsers.SVCB; |
ffd66b461f6a
util.dns: Implement SVCB record parser
Kim Alvefur <zash@zash.se>
parents:
12239
diff
changeset
|
255 |
10961
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 local params = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 TLSA = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 use = tlsa_usages; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 select = tlsa_selectors; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 match = tlsa_match_types; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 local fallback_mt = { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 __tostring = function(rr) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 return s_format([[\# %d %s]], #rr.raw, tohex(rr.raw)); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 end; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 }; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 local function fallback_parser(packet) |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 return setmetatable({ raw = packet },fallback_mt); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 end |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 setmetatable(parsers, { __index = function() return fallback_parser end }); |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 return { |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 parsers = parsers; |
12236
d0dfd48806f9
util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents:
10972
diff
changeset
|
276 classes = iana_data.classes; |
d0dfd48806f9
util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents:
10972
diff
changeset
|
277 types = iana_data.types; |
d0dfd48806f9
util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents:
10972
diff
changeset
|
278 errors = iana_data.errors; |
10961
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 params = params; |
f93dce30089a
util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 }; |