Annotate

util/dns.lua @ 12236:d0dfd48806f9

util.dns: Move DNS parameters details into util.dnsregistry Goal is to regenerate this file from the IANA registry using a tool. Having it in a separate file will reduce vcs noise in util.dns
author Kim Alvefur <zash@zash.se>
date Sun, 04 Oct 2020 19:23:16 +0200
parent 10972:b3773b1b90a1
child 12239:578ce0415398
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if s_byte(packet, pos) == 0 then return "."; end
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
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 local params = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 TLSA = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 use = tlsa_usages;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 select = tlsa_selectors;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 match = tlsa_match_types;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 local fallback_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 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
212 end;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 local function fallback_parser(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 return setmetatable({ raw = packet },fallback_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 setmetatable(parsers, { __index = function() return fallback_parser end });
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 return {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 parsers = parsers;
12236
d0dfd48806f9 util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents: 10972
diff changeset
221 classes = iana_data.classes;
d0dfd48806f9 util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents: 10972
diff changeset
222 types = iana_data.types;
d0dfd48806f9 util.dns: Move DNS parameters details into util.dnsregistry
Kim Alvefur <zash@zash.se>
parents: 10972
diff changeset
223 errors = iana_data.errors;
10961
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 params = params;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 };