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