Comparison

net/dns.lua @ 4125:5cf13260edec

net.dns: Fix tostring() for SRV records
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 Jan 2011 14:02:02 +0000
parent 4023:7910f36e2791
child 4251:96e9d5f9ab3b
comparison
equal deleted inserted replaced
4124:3761aa844d65 4125:5cf13260edec
156 local resolver = {}; 156 local resolver = {};
157 resolver.__index = resolver; 157 resolver.__index = resolver;
158 158
159 resolver.timeout = default_timeout; 159 resolver.timeout = default_timeout;
160 160
161 local SRV_tostring;
162
163 local function default_rr_tostring(rr) 161 local function default_rr_tostring(rr)
164 local rr_val = rr.type and rr[rr.type:lower()]; 162 local rr_val = rr.type and rr[rr.type:lower()];
165 if type(rr_val) ~= "string" then 163 if type(rr_val) ~= "string" then
166 return "<UNKNOWN RDATA TYPE>"; 164 return "<UNKNOWN RDATA TYPE>";
167 end 165 end
168 return rr_val; 166 return rr_val;
169 end 167 end
170 168
171 local special_tostrings = { 169 local special_tostrings = {
172 LOC = resolver.LOC_tostring; 170 LOC = resolver.LOC_tostring;
173 MX = function (rr) return string.format('%2i %s', rr.pref, rr.mx); end; 171 MX = function (rr)
174 SRV = SRV_tostring; 172 return string.format('%2i %s', rr.pref, rr.mx);
173 end;
174 SRV = function (rr)
175 local s = rr.srv;
176 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
177 end;
175 }; 178 };
176 179
177 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable 180 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
178 function rr_metatable.__tostring(rr) 181 function rr_metatable.__tostring(rr)
179 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr); 182 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
472 end 475 end
473 476
474 function resolver:PTR(rr) 477 function resolver:PTR(rr)
475 rr.ptr = self:name(); 478 rr.ptr = self:name();
476 end 479 end
477
478 function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
479 local s = rr.srv;
480 return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
481 end
482
483 480
484 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT 481 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
485 rr.txt = self:sub (rr.rdlength); 482 rr.txt = self:sub (rr.rdlength);
486 end 483 end
487 484