Comparison

net/dns.lua @ 3747:7d5b135bf268

net.dns: Clean up tostring() of returned records, as a result PTR records can now be tostring()'d
author Matthew Wild <mwild1@gmail.com>
date Tue, 14 Dec 2010 18:29:40 +0000
parent 3746:9719316c854e
child 3954:6e22b0cf3d72
comparison
equal deleted inserted replaced
3746:9719316c854e 3747:7d5b135bf268
19 local is_windows = (_ and windows) or os.getenv("WINDIR"); 19 local is_windows = (_ and windows) or os.getenv("WINDIR");
20 20
21 local coroutine, io, math, string, table = 21 local coroutine, io, math, string, table =
22 coroutine, io, math, string, table; 22 coroutine, io, math, string, table;
23 23
24 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select = 24 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
25 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select; 25 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
26 26
27 local ztact = { -- public domain 20080404 lua@ztact.com 27 local ztact = { -- public domain 20080404 lua@ztact.com
28 get = function(parent, ...) 28 get = function(parent, ...)
29 local len = select('#', ...); 29 local len = select('#', ...);
30 for i=1,len do 30 for i=1,len do
158 158
159 resolver.timeout = default_timeout; 159 resolver.timeout = default_timeout;
160 160
161 local SRV_tostring; 161 local SRV_tostring;
162 162
163 local function default_rr_tostring(rr)
164 local rr_val = rr.type and rr[rr.type:lower()];
165 if type(rr_val) ~= "string" then
166 return "<UNKNOWN RDATA TYPE>";
167 end
168 return rr_val;
169 end
170
171 local special_tostrings = {
172 LOC = resolver.LOC_tostring;
173 MX = function (rr) return string.format('%2i %s', rr.pref, rr.mx); end;
174 SRV = SRV_tostring;
175 };
163 176
164 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable 177 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
165 function rr_metatable.__tostring(rr) 178 function rr_metatable.__tostring(rr)
166 local s0 = string.format('%2s %-5s %6i %-28s', rr.class, rr.type, rr.ttl, rr.name); 179 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
167 local s1 = ''; 180 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
168 if rr.type == 'A' then
169 s1 = ' '..rr.a;
170 elseif rr.type == 'MX' then
171 s1 = string.format(' %2i %s', rr.pref, rr.mx);
172 elseif rr.type == 'CNAME' then
173 s1 = ' '..rr.cname;
174 elseif rr.type == 'LOC' then
175 s1 = ' '..resolver.LOC_tostring(rr);
176 elseif rr.type == 'NS' then
177 s1 = ' '..rr.ns;
178 elseif rr.type == 'SRV' then
179 s1 = ' '..SRV_tostring(rr);
180 elseif rr.type == 'TXT' then
181 s1 = ' '..rr.txt;
182 else
183 s1 = ' <UNKNOWN RDATA TYPE>';
184 end
185 return s0..s1;
186 end 181 end
187 182
188 183
189 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable 184 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
190 function rrs_metatable.__tostring(rrs) 185 function rrs_metatable.__tostring(rrs)