Annotate

util/dns.lua @ 10961:f93dce30089a

util.dns: Library for decoding DNS records Imported from luaunbound-prosody 5f7c771138b1
author Kim Alvefur <zash@zash.se>
date Sat, 09 Mar 2019 21:16:27 +0100
child 10972:b3773b1b90a1
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
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 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
22 have_net = false;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local chartohex = {};
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 for c = 0, 255 do
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 chartohex[s_char(c)] = s_format("%02X", c);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local function tohex(s)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return (s_gsub(s, ".", chartohex));
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- Converted from
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- http://www.iana.org/assignments/dns-parameters
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 -- 2015-05-19
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 classes = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 IN = 1; "IN";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 nil;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 CH = 3; "CH";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 HS = 4; "HS";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local types = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 "A";"NS";"MD";"MF";"CNAME";"SOA";"MB";"MG";"MR";"NULL";"WKS";"PTR";"HINFO";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 "MINFO";"MX";"TXT";"RP";"AFSDB";"X25";"ISDN";"RT";"NSAP";"NSAP-PTR";"SIG";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 "KEY";"PX";"GPOS";"AAAA";"LOC";"NXT";"EID";"NIMLOC";"SRV";"ATMA";"NAPTR";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 "KX";"CERT";"A6";"DNAME";"SINK";"OPT";"APL";"DS";"SSHFP";"IPSECKEY";"RRSIG";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 "NSEC";"DNSKEY";"DHCID";"NSEC3";"NSEC3PARAM";"TLSA";[55]="HIP";[56]="NINFO";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 [57]="RKEY";[58]="TALINK";[59]="CDS";[60]="CDNSKEY";[61]="OPENPGPKEY";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 [62]="CSYNC";TLSA=52;NS=2;[249]="TKEY";[251]="IXFR";NSAP=22;UID=101;APL=42;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 MG=8;NIMLOC=32;DHCID=49;TALINK=58;HINFO=13;MINFO=14;EID=31;DS=43;CSYNC=62;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 RKEY=57;TKEY=249;NID=104;NAPTR=35;RT=21;LP=107;L32=105;KEY=25;MD=3;MX=15;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 A6=38;KX=36;PX=26;CAA=257;WKS=11;TSIG=250;MAILA=254;CDS=59;SINK=40;LOC=29;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 DLV=32769;[32769]="DLV";TA=32768;[32768]="TA";GID=102;IXFR=251;MAILB=253;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 [256]="URI";[250]="TSIG";[252]="AXFR";NSEC=47;HIP=55;[254]="MAILA";[255]="*";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 NSEC3PARAM=51;["*"]=255;URI=256;[253]="MAILB";AXFR=252;SPF=99;NXT=30;AFSDB=18;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 EUI48=108;NINFO=56;CDNSKEY=60;ISDN=20;L64=106;SRV=33;DNSKEY=48;X25=19;TXT=16;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 RRSIG=46;OPENPGPKEY=61;DNAME=39;CNAME=5;EUI64=109;A=1;MR=9;IPSECKEY=45;OPT=41;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 UNSPEC=103;["NSAP-PTR"]=23;[103]="UNSPEC";[257]="CAA";UINFO=100;[99]="SPF";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 MF=4;[101]="UID";[102]="GID";SOA=6;[104]="NID";[105]="L32";[106]="L64";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 [107]="LP";[108]="EUI48";[109]="EUI64";NSEC3=50;RP=17;PTR=12;[100]="UINFO";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 NULL=10;AAAA=28;MB=7;GPOS=27;SSHFP=44;CERT=37;SIG=24;ATMA=34
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
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local errors = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 NoError = "No Error"; [0] = "NoError";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 FormErr = "Format Error"; "FormErr";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 ServFail = "Server Failure"; "ServFail";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 NXDomain = "Non-Existent Domain"; "NXDomain";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 NotImp = "Not Implemented"; "NotImp";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 Refused = "Query Refused"; "Refused";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 YXDomain = "Name Exists when it should not"; "YXDomain";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 YXRRSet = "RR Set Exists when it should not"; "YXRRSet";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 NXRRSet = "RR Set that should exist does not"; "NXRRSet";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 NotAuth = "Server Not Authoritative for zone"; "NotAuth";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 NotZone = "Name not contained in zone"; "NotZone";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 -- Simplified versions of Waqas DNS parsers
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 -- 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
84
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local parsers = {};
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 -- 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
88 local function readDnsName(packet, pos)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 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
90 local pack_len, r, len = #packet, {};
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 pos = pos or 1;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 repeat
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 len = s_byte(packet, pos) or 0;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 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
95 pos = pos + len + 1;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 until len == 0 or pos >= pack_len;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 return t_concat(r, "."), pos;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 -- These are just simple names.
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 parsers.CNAME = readDnsName;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 parsers.NS = readDnsName
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 parsers.PTR = readDnsName;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local soa_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 end;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 function parsers.SOA(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 local mname, rname, offset;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 mname, offset = readDnsName(packet, 1);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 rname, offset = readDnsName(packet, offset);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 -- 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
117 local
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 s1, s2, s3, s4, -- serial
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 r1, r2, r3, r4, -- refresh
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 t1, t2, t3, t4, -- retry
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 e1, e2, e3, e4, -- expire
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 m1, m2, m3, m4 -- minimum
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 = s_byte(packet, offset, offset + 19);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 return setmetatable({
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 mname = mname;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 rname = rname;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 serial = s1*0x1000000 + s2*0x10000 + s3*0x100 + s4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 refresh = r1*0x1000000 + r2*0x10000 + r3*0x100 + r4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 retry = t1*0x1000000 + t2*0x10000 + t3*0x100 + t4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 expire = e1*0x1000000 + e2*0x10000 + e3*0x100 + e4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 minimum = m1*0x1000000 + m2*0x10000 + m3*0x100 + m4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 }, soa_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 function parsers.A(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 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
138 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 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
141 function parsers.AAAA(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 local hi, lo, ip, len, token;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 for i = 1, 8 do
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 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
145 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
146 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 ip = t_concat(aaaa, ":", 1, 8);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 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
149 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
150 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
151 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 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
153 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 if have_net then
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 parsers.A = net_util.ntop;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 parsers.AAAA = net_util.ntop;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 local mx_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 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
163 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 function parsers.MX(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 local name = readDnsName(packet, 3);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local b1,b2 = s_byte(packet, 1, 2);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 return setmetatable({
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 pref = b1*256+b2;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 mx = name;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 }, mx_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 local srv_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 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
177 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 function parsers.SRV(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 local name = readDnsName(packet, 7);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 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
182 return setmetatable({
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 priority = b1*256+b2;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 weight = b3*256+b4;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 port = b5*256+b6;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 target = name;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 }, srv_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 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 local txt_mt = { __tostring = t_concat };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 function parsers.TXT(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local pack_len = #packet;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 local r, pos, len = {}, 1;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 repeat
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 len = s_byte(packet, pos) or 0;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 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
197 pos = pos + len + 1;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 until pos >= pack_len;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 return setmetatable(r, txt_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 parsers.SPF = parsers.TXT;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 -- Acronyms from RFC 7218
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local tlsa_usages = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 [0] = "PKIX-CA";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 [1] = "PKIX-EE";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 [2] = "DANE-TA";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 [3] = "DANE-EE";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 [255] = "PrivCert";
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 local tlsa_selectors = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 [0] = "Cert",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 [1] = "SPKI",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 [255] = "PrivSel",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 local tlsa_match_types = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 [0] = "Full",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 [1] = "SHA2-256",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 [2] = "SHA2-512",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 [255] = "PrivMatch",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 local tlsa_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 return s_format("%s %s %s %s",
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 tlsa_usages[rr.use] or rr.use,
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 tlsa_selectors[rr.select] or rr.select,
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 tlsa_match_types[rr.match] or rr.match,
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 tohex(rr.data));
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 __index = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 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
233 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
234 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
235 }
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 function parsers.TLSA(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 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
239 return setmetatable({
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 use = use;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 select = select;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 match = match;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 data = s_sub(packet, 4);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 }, tlsa_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local params = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 TLSA = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 use = tlsa_usages;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 select = tlsa_selectors;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 match = tlsa_match_types;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 local fallback_mt = {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 __tostring = function(rr)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 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
258 end;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 };
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 local function fallback_parser(packet)
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 return setmetatable({ raw = packet },fallback_mt);
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 setmetatable(parsers, { __index = function() return fallback_parser end });
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 return {
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 parsers = parsers;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 classes = classes;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 types = types;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 errors = errors;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 params = params;
f93dce30089a util.dns: Library for decoding DNS records
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 };