net/dns.lua

Thu, 25 Dec 2014 10:48:06 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 25 Dec 2014 10:48:06 +0000
changeset 0
d363a6692a10
permissions
-rw-r--r--

Initial commit. Tortoises are fun.

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- This file is included with Prosody IM. It has modifications,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- which are hereby placed in the public domain.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- todo: quick (default) header generation
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- todo: nxdomain, error handling
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- todo: cache results of encodeName
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- reference: http://tools.ietf.org/html/rfc1035
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local socket = require "socket";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local timer = require "util.timer";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local new_ip = require "util.ip".new_ip;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local _, windows = pcall(require, "util.windows");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local is_windows = (_ and windows) or os.getenv("WINDIR");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local coroutine, io, math, string, table =
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 coroutine, io, math, string, table;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local ztact = { -- public domain 20080404 lua@ztact.com
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 get = function(parent, ...)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local len = select('#', ...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for i=1,len do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 parent = parent[select(i, ...)];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if parent == nil then break; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return parent;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 set = function(parent, ...)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local len = select('#', ...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local key, value = select(len-1, ...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local cutpoint, cutkey;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 for i=1,len-2 do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local key = select (i, ...)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local child = parent[key]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if value == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if child == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 elseif next(child, next(child)) then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 cutpoint = nil; cutkey = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 elseif cutpoint == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 cutpoint = parent; cutkey = key;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 elseif child == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 child = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 parent[key] = child;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 parent = child
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if value == nil and cutpoint then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 cutpoint[cutkey] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 parent[key] = value;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 return value;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local get, set = ztact.get, ztact.set;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local default_timeout = 15;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 -------------------------------------------------- module dns
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 module('dns')
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local dns = _M;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- dns type & class codes ------------------------------ dns type & class codes
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local append = table.insert
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 return (i-(i%0x100))/0x100;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local a = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 for i,s in pairs(t) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 a[i] = s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 a[s] = s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 a[string.lower(s)] = s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 return a;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local code = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 for i,s in pairs(t) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local word = string.char(highbyte(i), i%0x100);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 code[i] = word;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 code[s] = word;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 code[string.lower(s)] = word;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 return code;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 dns.types = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 dns.type = augment (dns.types);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 dns.class = augment (dns.classes);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 dns.typecode = encode (dns.types);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 dns.classcode = encode (dns.classes);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 qname = string.lower(qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 time = time or socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 for i,rr in ipairs(rrs) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if rr.tod then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 -- rr.tod = rr.tod - 50 -- accelerated decripitude
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 rr.ttl = math.floor(rr.tod - time);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 if rr.ttl <= 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 rrs[rr[rr.type:lower()]] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 table.remove(rrs, i);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return prune(rrs, time, soft); -- Re-iterate
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 elseif soft == 'soft' then -- What is this? I forget!
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 assert(rr.ttl == 0);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 rrs[rr[rr.type:lower()]] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 table.remove(rrs, i);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 -- metatables & co. ------------------------------------------ metatables & co.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local resolver = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 resolver.__index = resolver;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 resolver.timeout = default_timeout;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 local function default_rr_tostring(rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 local rr_val = rr.type and rr[rr.type:lower()];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 if type(rr_val) ~= "string" then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 return "<UNKNOWN RDATA TYPE>";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 return rr_val;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 local special_tostrings = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 LOC = resolver.LOC_tostring;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 MX = function (rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 return string.format('%2i %s', rr.pref, rr.mx);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 SRV = function (rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 local s = rr.srv;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 function rr_metatable.__tostring(rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 function rrs_metatable.__tostring(rrs)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 local t = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 for i,rr in ipairs(rrs) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 append(t, tostring(rr)..'\n');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 return table.concat(t);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 function cache_metatable.__tostring(cache)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 local time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 local t = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 for class,types in pairs(cache) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 for type,names in pairs(types) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 for name,rrs in pairs(names) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 prune(rrs, time);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 append(t, tostring(rrs));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 return table.concat(t);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 local r = { active = {}, cache = {}, unsorted = {} };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 setmetatable(r, resolver);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 setmetatable(r.cache, cache_metatable);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 setmetatable(r.unsorted, { __mode = 'kv' });
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 return r;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 -- packet layer -------------------------------------------------- packet layer
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 math.randomseed(math.floor(10000*socket.gettime()) % 0x100000000);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 dns.random = math.random;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 return dns.random(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 o = o or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 o.rd = o.rd or 1; -- 1b 1 recursion desired
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 o.tc = o.tc or 0; -- 1b 1 truncated response
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 o.aa = o.aa or 0; -- 1b 1 authoritative response
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 o.opcode = o.opcode or 0; -- 4b 0 query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 -- 1 inverse query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 -- 2 server status request
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 -- 3-15 reserved
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 o.qr = o.qr or 0; -- 1b 0 query, 1 response
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 o.rcode = o.rcode or 0; -- 4b 0 no error
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 -- 1 format error
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 -- 2 server failure
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 -- 3 name error
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 -- 4 not implemented
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 -- 5 refused
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 -- 6-15 reserved
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 o.z = o.z or 0; -- 3b 0 resvered
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 o.ra = o.ra or 0; -- 1b 1 recursion available
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 o.ancount = o.ancount or 0; -- 16b number of answers RRs
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 o.arcount = o.arcount or 0; -- 16b number of additional RRs
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 -- string.char() rounds, so prevent roundup with -0.4999
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 local header = string.char(
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 highbyte(o.id), o.id %0x100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 o.rcode + 16*o.z + 128*o.ra,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 highbyte(o.qdcount), o.qdcount %0x100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 highbyte(o.ancount), o.ancount %0x100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 highbyte(o.nscount), o.nscount %0x100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 highbyte(o.arcount), o.arcount %0x100
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 );
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 return header, o.id;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 local t = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 for part in string.gmatch(name, '[^.]+') do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 append(t, string.char(string.len(part)));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 append(t, part);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 append(t, string.char(0));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 return table.concat(t);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 qname = encodeName(qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 qtype = dns.typecode[qtype or 'a'];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 qclass = dns.classcode[qclass or 'in'];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 return qname..qtype..qclass;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 len = len or 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 local offset = self.offset;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 local last = offset + len - 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 if last > #self.packet then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 error(string.format('out of bounds: %i>%i', last, #self.packet));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 self.offset = offset + len;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 return string.byte(self.packet, offset, last);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 local b1, b2 = self:byte(2);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 return 0x100*b1 + b2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 local b1, b2, b3, b4 = self:byte(4);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 --print('dword', b1, b2, b3, b4);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 len = len or 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 self.offset = self.offset + len;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 return s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 local id = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 --print(string.format(':header id %x', id));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 if not self.active[id] and not force then return nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 local h = { id = id };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 local b1, b2 = self:byte(2);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 h.rd = b1 %2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 h.tc = b1 /2%2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 h.aa = b1 /4%2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 h.opcode = b1 /8%16;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 h.qr = b1 /128;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 h.rcode = b2 %16;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 h.z = b2 /16%8;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 h.ra = b2 /128;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 h.qdcount = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 h.ancount = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 h.nscount = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 h.arcount = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 for k,v in pairs(h) do h[k] = v-v%1; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 return h;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 local remember, pointers = nil, 0;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 local len = self:byte();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 local n = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 if len == 0 then return "." end -- Root label
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 while len > 0 do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 if len >= 0xc0 then -- name is "compressed"
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 pointers = pointers + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 if pointers >= 20 then error('dns error: 20 pointers'); end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 local offset = ((len-0xc0)*0x100) + self:byte();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 remember = remember or self.offset;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 self.offset = offset + 1; -- +1 for lua
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 else -- name is not compressed
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 append(n, self:sub(len)..'.');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 len = self:byte();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 self.offset = remember or self.offset;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 return table.concat(n);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 function resolver:question() -- - - - - - - - - - - - - - - - - - question
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 local q = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 q.name = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 q.type = dns.type[self:word()];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 q.class = dns.class[self:word()];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 return q;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 local b1, b2, b3, b4 = self:byte(4);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 function resolver:AAAA(rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 local addr = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 for i = 1, rr.rdlength, 2 do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 local b1, b2 = self:byte(2);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 table.insert(addr, ("%02x%02x"):format(b1, b2));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 addr = table.concat(addr, ":"):gsub("%f[%x]0+(%x)","%1");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 local zeros = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 for item in addr:gmatch(":[0:]+:") do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 table.insert(zeros, item)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 if #zeros == 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 rr.aaaa = addr;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 return
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 elseif #zeros > 1 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 table.sort(zeros, function(a, b) return #a > #b end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 rr.cname = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 rr.pref = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 rr.mx = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 local b = self:byte();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 rr.version = self:byte();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 if rr.version == 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 rr.loc = rr.loc or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 rr.loc.size = self:LOC_nibble_power();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 rr.loc.horiz_pre = self:LOC_nibble_power();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 rr.loc.vert_pre = self:LOC_nibble_power();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 rr.loc.latitude = self:dword();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 rr.loc.longitude = self:dword();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 rr.loc.altitude = self:dword();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 f = f - 0x80000000;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 if f < 0 then pos = neg; f = -f; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 local deg, min, msec;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 msec = f%60000;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 f = (f-msec)/60000;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 min = f%60;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 deg = (f-min)/60;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 local t = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 --[[
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 --]]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 append(t, string.format(
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 '%s %s %.2fm %.2fm %.2fm %.2fm',
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 (rr.loc.altitude - 10000000) / 100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 rr.loc.size / 100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 rr.loc.horiz_pre / 100,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 rr.loc.vert_pre / 100
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 ));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 return table.concat(t);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 rr.ns = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 rr.srv = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 rr.srv.priority = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 rr.srv.weight = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 rr.srv.port = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 rr.srv.target = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 function resolver:PTR(rr)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 rr.ptr = self:name();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 rr.txt = self:sub (self:byte());
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 local rr = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 setmetatable(rr, rr_metatable);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 rr.name = self:name(self);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 rr.type = dns.type[self:word()] or rr.type;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 rr.class = dns.class[self:word()] or rr.class;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 rr.ttl = 0x10000*self:word() + self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 rr.rdlength = self:word();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 if rr.ttl <= 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519 rr.tod = self.time + 30;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 rr.tod = self.time + rr.ttl;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 local remember = self.offset;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 local rr_parser = self[dns.type[rr.type]];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 if rr_parser then rr_parser(self, rr); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 self.offset = remember;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 rr.rdata = self:sub(rr.rdlength);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 return rr;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 local rrs = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 for i = 1,count do append(rrs, self:rr()); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 return rrs;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 self.packet, self.offset = packet, 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542 local header = self:header(force);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 if not header then return nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 local response = { header = header };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 response.question = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 local offset = self.offset;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 for i = 1,response.header.qdcount do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 append(response.question, self:question());
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
550 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 if not force then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
555 self.active[response.header.id] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 return nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 response.answer = self:rrs(response.header.ancount);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 response.authority = self:rrs(response.header.nscount);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 response.additional = self:rrs(response.header.arcount);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 return response;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 -- socket layer -------------------------------------------------- socket layer
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 resolver.delays = { 1, 3 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 self.server = self.server or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576 append(self.server, address);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 self.server = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
582 self:addnameserver(address);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
583 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
586 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587 if is_windows then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
588 if windows and windows.get_nameservers then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589 for _, server in ipairs(windows.get_nameservers()) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 self:addnameserver(server);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
593 if not self.server or #self.server == 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
594 -- TODO log warning about no nameservers, adding opendns servers as fallback
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
595 self:addnameserver("208.67.222.222");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
596 self:addnameserver("208.67.220.220");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
597 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598 else -- posix
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
599 local resolv_conf = io.open("/etc/resolv.conf");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 if resolv_conf then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
601 for line in resolv_conf:lines() do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
602 line = line:gsub("#.*$", "")
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
603 :match('^%s*nameserver%s+([%x:%.]*)%s*$');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
604 if line then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
605 local ip = new_ip(line);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 if ip then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
607 self:addnameserver(ip.addr);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
608 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
609 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
610 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
611 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
612 if not self.server or #self.server == 0 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
613 -- TODO log warning about no nameservers, adding localhost as the default nameserver
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 self:addnameserver("127.0.0.1");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
615 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
616 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
617 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
618
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
619
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
620 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
621 self.socket = self.socket or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
622 self.socketset = self.socketset or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
623
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
624 local sock = self.socket[servernum];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
625 if sock then return sock; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
626
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
627 local ok, err;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
628 local peer = self.server[servernum];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
629 if peer:find(":") then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
630 sock, err = socket.udp6();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
631 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
632 sock, err = socket.udp();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
633 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
634 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
635 if not sock then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
636 return nil, err;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
637 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
638 sock:settimeout(0);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
639 -- todo: attempt to use a random port, fallback to 0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
640 self.socket[servernum] = sock;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
641 self.socketset[sock] = servernum;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
642 -- set{sock,peer}name can fail, eg because of local routing table
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
643 -- if so, try the next server
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
644 ok, err = sock:setsockname('*', 0);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
645 if not ok then return self:servfail(sock, err); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
646 ok, err = sock:setpeername(peer, 53);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
647 if not ok then return self:servfail(sock, err); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
648 return sock;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
649 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
650
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
651 function resolver:voidsocket(sock)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
652 if self.socket[sock] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
653 self.socketset[self.socket[sock]] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
654 self.socket[sock] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
655 elseif self.socketset[sock] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
656 self.socket[self.socketset[sock]] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
657 self.socketset[sock] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
658 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
659 sock:close();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
660 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
661
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
662 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
663 self.socket_wrapper = func;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
664 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
665
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
666
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
667 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
668 for i,sock in ipairs(self.socket) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
669 self.socket[i] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
670 self.socketset[sock] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
671 sock:close();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
672 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
673 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
674
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
675
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
676 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
677 --print ('remember', type, rr.class, rr.type, rr.name)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
678 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
679
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
680 if type ~= '*' then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
681 type = qtype;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
682 local all = get(self.cache, qclass, '*', qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
683 --print('remember all', all);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
684 if all then append(all, rr); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
685 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
686
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
687 self.cache = self.cache or setmetatable({}, cache_metatable);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
688 local rrs = get(self.cache, qclass, type, qname) or
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
689 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
690 if not rrs[rr[qtype:lower()]] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
691 rrs[rr[qtype:lower()]] = true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
692 append(rrs, rr);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
693 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
694
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
695 if type == 'MX' then self.unsorted[rrs] = true; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
696 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
697
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
698
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
699 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
700 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
701 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
702
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
703
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
704 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
705 qname, qtype, qclass = standardize(qname, qtype, qclass);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
706 local rrs = get(self.cache, qclass, qtype, qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
707 if not rrs then return nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
708 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
709 set(self.cache, qclass, qtype, qname, nil);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
710 return nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
711 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
712 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
713 return rrs;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
714 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
715
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
716
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
717 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
718 if soft == 'soft' then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
719 self.time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
720 for class,types in pairs(self.cache or {}) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
721 for type,names in pairs(types) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
722 for name,rrs in pairs(names) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
723 prune(rrs, self.time, 'soft')
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
724 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
725 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
726 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
727 else self.cache = setmetatable({}, cache_metatable); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
728 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
729
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
730
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
731 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
732 qname, qtype, qclass = standardize(qname, qtype, qclass)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
733
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
734 local co = coroutine.running();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
735 local q = get(self.wanted, qclass, qtype, qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
736 if co and q then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
737 -- We are already waiting for a reply to an identical query.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
738 set(self.wanted, qclass, qtype, qname, co, true);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
739 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
740 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
741
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
742 if not self.server then self:adddefaultnameservers(); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
743
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
744 local question = encodeQuestion(qname, qtype, qclass);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
745 local peek = self:peek (qname, qtype, qclass);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
746 if peek then return peek; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
747
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
748 local header, id = encodeHeader();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
749 --print ('query id', id, qclass, qtype, qname)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
750 local o = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
751 packet = header..question,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
752 server = self.best_server,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
753 delay = 1,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
754 retry = socket.gettime() + self.delays[1]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
755 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
756
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
757 -- remember the query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
758 self.active[id] = self.active[id] or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
759 self.active[id][question] = o;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
760
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
761 -- remember which coroutine wants the answer
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
762 if co then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
763 set(self.wanted, qclass, qtype, qname, co, true);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
764 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
765
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
766 local conn, err = self:getsocket(o.server)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
767 if not conn then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
768 return nil, err;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
769 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
770 conn:send (o.packet)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
771
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
772 if timer and self.timeout then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
773 local num_servers = #self.server;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
774 local i = 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
775 timer.add_task(self.timeout, function ()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
776 if get(self.wanted, qclass, qtype, qname, co) then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
777 if i < num_servers then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
778 i = i + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
779 self:servfail(conn);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
780 o.server = self.best_server;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
781 conn, err = self:getsocket(o.server);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
782 if conn then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
783 conn:send(o.packet);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
784 return self.timeout;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
785 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
786 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
787 -- Tried everything, failed
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
788 self:cancel(qclass, qtype, qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
789 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
790 end)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
791 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
792 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
793 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
794
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
795 function resolver:servfail(sock, err)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
796 -- Resend all queries for this server
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
797
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
798 local num = self.socketset[sock]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
799
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
800 -- Socket is dead now
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
801 sock = self:voidsocket(sock);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
802
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
803 -- Find all requests to the down server, and retry on the next server
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
804 self.time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
805 for id,queries in pairs(self.active) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
806 for question,o in pairs(queries) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
807 if o.server == num then -- This request was to the broken server
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
808 o.server = o.server + 1 -- Use next server
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
809 if o.server > #self.server then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
810 o.server = 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
811 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
812
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
813 o.retries = (o.retries or 0) + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
814 if o.retries >= #self.server then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
815 --print('timeout');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
816 queries[question] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
817 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
818 sock, err = self:getsocket(o.server);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
819 if sock then sock:send(o.packet); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
820 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
821 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
822 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
823 if next(queries) == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
824 self.active[id] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
825 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
826 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
827
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
828 if num == self.best_server then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
829 self.best_server = self.best_server + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
830 if self.best_server > #self.server then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
831 -- Exhausted all servers, try first again
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
832 self.best_server = 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
833 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
834 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
835 return sock, err;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
836 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
837
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
838 function resolver:settimeout(seconds)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
839 self.timeout = seconds;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
840 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
841
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
842 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
843 --print('receive'); print(self.socket);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
844 self.time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
845 rset = rset or self.socket;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
846
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
847 local response;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
848 for i,sock in pairs(rset) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
849
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
850 if self.socketset[sock] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
851 local packet = sock:receive();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
852 if packet then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
853 response = self:decode(packet);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
854 if response and self.active[response.header.id]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
855 and self.active[response.header.id][response.question.raw] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
856 --print('received response');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
857 --self.print(response);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
858
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
859 for j,rr in pairs(response.answer) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
860 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
861 self:remember(rr, response.question[1].type)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
862 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
863 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
864
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
865 -- retire the query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
866 local queries = self.active[response.header.id];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
867 queries[response.question.raw] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
868
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
869 if not next(queries) then self.active[response.header.id] = nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
870 if not next(self.active) then self:closeall(); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
871
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
872 -- was the query on the wanted list?
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
873 local q = response.question[1];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
874 local cos = get(self.wanted, q.class, q.type, q.name);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
875 if cos then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
876 for co in pairs(cos) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
877 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
878 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
879 set(self.wanted, q.class, q.type, q.name, nil);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
880 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
881 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
882
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
883 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
884 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
885 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
886
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
887 return response;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
888 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
889
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
890
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
891 function resolver:feed(sock, packet, force)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
892 --print('receive'); print(self.socket);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
893 self.time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
894
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
895 local response = self:decode(packet, force);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
896 if response and self.active[response.header.id]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
897 and self.active[response.header.id][response.question.raw] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
898 --print('received response');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
899 --self.print(response);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
900
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
901 for j,rr in pairs(response.answer) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
902 self:remember(rr, response.question[1].type);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
903 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
904
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
905 -- retire the query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
906 local queries = self.active[response.header.id];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
907 queries[response.question.raw] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
908 if not next(queries) then self.active[response.header.id] = nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
909 if not next(self.active) then self:closeall(); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
910
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
911 -- was the query on the wanted list?
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
912 local q = response.question[1];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
913 if q then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
914 local cos = get(self.wanted, q.class, q.type, q.name);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
915 if cos then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
916 for co in pairs(cos) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
917 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
918 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
919 set(self.wanted, q.class, q.type, q.name, nil);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
920 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
921 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
922 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
923
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
924 return response;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
925 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
926
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
927 function resolver:cancel(qclass, qtype, qname)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
928 local cos = get(self.wanted, qclass, qtype, qname);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
929 if cos then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
930 for co in pairs(cos) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
931 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
932 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
933 set(self.wanted, qclass, qtype, qname, nil);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
934 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
935 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
936
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
937 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
938 --print(':pulse');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
939 while self:receive() do end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
940 if not next(self.active) then return nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
941
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
942 self.time = socket.gettime();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
943 for id,queries in pairs(self.active) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
944 for question,o in pairs(queries) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
945 if self.time >= o.retry then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
946
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
947 o.server = o.server + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
948 if o.server > #self.server then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
949 o.server = 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
950 o.delay = o.delay + 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
951 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
952
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
953 if o.delay > #self.delays then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
954 --print('timeout');
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
955 queries[question] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
956 if not next(queries) then self.active[id] = nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
957 if not next(self.active) then return nil; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
958 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
959 --print('retry', o.server, o.delay);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
960 local _a = self.socket[o.server];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
961 if _a then _a:send(o.packet); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
962 o.retry = self.time + self.delays[o.delay];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
963 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
964 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
965 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
966 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
967
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
968 if next(self.active) then return true; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
969 return nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
970 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
971
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
972
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
973 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
974 self:query (qname, qtype, qclass)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
975 while self:pulse() do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
976 local recvt = {}
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
977 for i, s in ipairs(self.socket) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
978 recvt[i] = s
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
979 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
980 socket.select(recvt, nil, 4)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
981 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
982 --print(self.cache);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
983 return self:peek(qname, qtype, qclass);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
984 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
985
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
986 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
987 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
988 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
989
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
990 function resolver:tohostname(ip)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
991 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
992 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
993
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
994 --print ---------------------------------------------------------------- print
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
995
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
996
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
997 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
998 qr = { [0]='query', 'response' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
999 opcode = { [0]='query', 'inverse query', 'server status request' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1000 aa = { [0]='non-authoritative', 'authoritative' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1001 tc = { [0]='complete', 'truncated' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1002 rd = { [0]='recursion not desired', 'recursion desired' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1003 ra = { [0]='recursion not available', 'recursion available' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1004 z = { [0]='(reserved)' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1005 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1006
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1007 type = dns.type,
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1008 class = dns.class
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1009 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1010
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1011
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1012 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1013 return (hints[s] and hints[s][p[s]]) or '';
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1014 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1015
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1016
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1017 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1018 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1019 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1020 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1021 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1022
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1023 for i,question in ipairs(response.question) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1024 print(string.format ('question[%i].name ', i), question.name);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1025 print(string.format ('question[%i].type ', i), question.type);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1026 print(string.format ('question[%i].class ', i), question.class);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1027 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1028
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1029 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1030 local tmp;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1031 for s,s in pairs({'answer', 'authority', 'additional'}) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1032 for i,rr in pairs(response[s]) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1033 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1034 tmp = string.format('%s[%i].%s', s, i, t);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1035 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1036 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1037 for j,t in pairs(rr) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1038 if not common[j] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1039 tmp = string.format('%s[%i].%s', s, i, j);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1040 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1041 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1042 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1043 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1044 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1045 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1046
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1047
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1048 -- module api ------------------------------------------------------ module api
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1049
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1050
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1051 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1052 -- this function seems to be redundant with resolver.new ()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1053
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1054 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, best_server = 1 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1055 setmetatable (r, resolver);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1056 setmetatable (r.cache, cache_metatable);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1057 setmetatable (r.unsorted, { __mode = 'kv' });
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1058 return r;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1059 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1060
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1061 local _resolver = dns.resolver();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1062 dns._resolver = _resolver;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1063
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1064 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1065 return _resolver:lookup(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1066 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1067
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1068 function dns.tohostname(...)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1069 return _resolver:tohostname(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1070 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1071
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1072 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1073 return _resolver:purge(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1074 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1075
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1076 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1077 return _resolver:peek(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1078 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1079
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1080 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1081 return _resolver:query(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1082 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1083
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1084 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1085 return _resolver:feed(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1086 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1087
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1088 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1089 return _resolver:cancel(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1090 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1091
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1092 function dns.settimeout(...)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1093 return _resolver:settimeout(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1094 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1095
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1096 function dns.cache()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1097 return _resolver.cache;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1098 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1099
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1100 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1101 return _resolver:socket_wrapper_set(...);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1102 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1103
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1104 return dns;

mercurial