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