Annotate

net/dns.lua @ 1788:45779d67c26c

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