comparison net/dns.lua @ 14078:d649d011df45

Use util.time.now() consistently instead of socket.gettime()
author Link Mauve <linkmauve@linkmauve.fr>
date Fri, 30 Jan 2026 14:08:53 +0100
parents a4c47203a9eb
children
comparison
equal deleted inserted replaced
14077:c83aee2a0b11 14078:d649d011df45
14 14
15 local socket = require "socket"; 15 local socket = require "socket";
16 local have_timer, timer = pcall(require, "prosody.util.timer"); 16 local have_timer, timer = pcall(require, "prosody.util.timer");
17 local new_ip = require "prosody.util.ip".new_ip; 17 local new_ip = require "prosody.util.ip".new_ip;
18 local have_util_net, util_net = pcall(require, "prosody.util.net"); 18 local have_util_net, util_net = pcall(require, "prosody.util.net");
19 local time_now = require "prosody.util.time".now;
19 20
20 local log = require "prosody.util.logger".init("dns"); 21 local log = require "prosody.util.logger".init("dns");
21 22
22 local _, windows = pcall(require, "prosody.util.windows"); 23 local _, windows = pcall(require, "prosody.util.windows");
23 local is_windows = (_ and windows) or os.getenv("WINDIR"); 24 local is_windows = (_ and windows) or os.getenv("WINDIR");
235 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN']; 236 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
236 end 237 end
237 238
238 239
239 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune 240 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
240 time = time or socket.gettime(); 241 time = time or time_now();
241 for i,rr in ipairs(rrs) do 242 for i,rr in ipairs(rrs) do
242 if rr.tod then 243 if rr.tod then
243 if rr.tod < time then 244 if rr.tod < time then
244 rrs[rr[rr.type:lower()]] = nil; 245 rrs[rr[rr.type:lower()]] = nil;
245 table.remove(rrs, i); 246 table.remove(rrs, i);
298 end 299 end
299 300
300 301
301 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable 302 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
302 function cache_metatable.__tostring(cache) 303 function cache_metatable.__tostring(cache)
303 local time = socket.gettime(); 304 local time = time_now();
304 local t = {}; 305 local t = {};
305 for class,types in pairs(cache) do 306 for class,types in pairs(cache) do
306 for type,names in pairs(types) do 307 for type,names in pairs(types) do
307 for name,rrs in pairs(names) do 308 for name,rrs in pairs(names) do
308 prune(rrs, time); 309 prune(rrs, time);
316 317
317 -- packet layer -------------------------------------------------- packet layer 318 -- packet layer -------------------------------------------------- packet layer
318 319
319 320
320 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random 321 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
321 math.randomseed(math.floor(10000*socket.gettime()) % 0x80000000); 322 math.randomseed(math.floor(10000 * time_now()) % 0x80000000);
322 dns.random = math.random; 323 dns.random = math.random;
323 return dns.random(...); 324 return dns.random(...);
324 end 325 end
325 326
326 327
811 if n then if n <= 0 then return end else n = 3 end 812 if n then if n <= 0 then return end else n = 3 end
812 rrs = get(self.cache, qclass, "CNAME", qname); 813 rrs = get(self.cache, qclass, "CNAME", qname);
813 if not (rrs and rrs[1]) then return end 814 if not (rrs and rrs[1]) then return end
814 return self:peek(rrs[1].cname, qtype, qclass, n - 1); 815 return self:peek(rrs[1].cname, qtype, qclass, n - 1);
815 end 816 end
816 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then 817 if prune(rrs, time_now()) and qtype == '*' or not next(rrs) then
817 set(self.cache, qclass, qtype, qname, nil); 818 set(self.cache, qclass, qtype, qname, nil);
818 return nil; 819 return nil;
819 end 820 end
820 if self.unsorted[rrs] then table.sort (rrs, comp_mx); self.unsorted[rrs] = nil; end 821 if self.unsorted[rrs] then table.sort (rrs, comp_mx); self.unsorted[rrs] = nil; end
821 return rrs; 822 return rrs;
822 end 823 end
823 824
824 825
825 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge 826 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
826 if soft == 'soft' then 827 if soft == 'soft' then
827 self.time = socket.gettime(); 828 self.time = time_now();
828 for class,types in pairs(self.cache or {}) do 829 for class,types in pairs(self.cache or {}) do
829 for type,names in pairs(types) do 830 for type,names in pairs(types) do
830 for name,rrs in pairs(names) do 831 for name,rrs in pairs(names) do
831 prune(rrs, self.time, 'soft') 832 prune(rrs, self.time, 'soft')
832 end 833 end
857 --print ('query id', id, qclass, qtype, qname) 858 --print ('query id', id, qclass, qtype, qname)
858 local o = { 859 local o = {
859 packet = header..question, 860 packet = header..question,
860 server = self.best_server, 861 server = self.best_server,
861 delay = 1, 862 delay = 1,
862 retry = socket.gettime() + self.delays[1]; 863 retry = time_now() + self.delays[1];
863 qclass = qclass; 864 qclass = qclass;
864 qtype = qtype; 865 qtype = qtype;
865 qname = qname; 866 qname = qname;
866 }; 867 };
867 868
912 913
913 -- Socket is dead now 914 -- Socket is dead now
914 sock = self:voidsocket(sock); 915 sock = self:voidsocket(sock);
915 916
916 -- Find all requests to the down server, and retry on the next server 917 -- Find all requests to the down server, and retry on the next server
917 self.time = socket.gettime(); 918 self.time = time_now();
918 log("debug", "servfail %d (of %d)", num, #self.server); 919 log("debug", "servfail %d (of %d)", num, #self.server);
919 for id,queries in pairs(self.active) do 920 for id,queries in pairs(self.active) do
920 for question,o in pairs(queries) do 921 for question,o in pairs(queries) do
921 if o.server == num then -- This request was to the broken server 922 if o.server == num then -- This request was to the broken server
922 o.server = o.server + 1 -- Use next server 923 o.server = o.server + 1 -- Use next server
968 self.timeout = seconds; 969 self.timeout = seconds;
969 end 970 end
970 971
971 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive 972 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
972 --print('receive'); print(self.socket); 973 --print('receive'); print(self.socket);
973 self.time = socket.gettime(); 974 self.time = time_now();
974 rset = rset or self.socket; 975 rset = rset or self.socket;
975 976
976 local response; 977 local response;
977 for _, sock in pairs(rset) do 978 for _, sock in pairs(rset) do
978 979
1017 end 1018 end
1018 1019
1019 1020
1020 function resolver:feed(sock, packet, force) 1021 function resolver:feed(sock, packet, force)
1021 --print('receive'); print(self.socket); 1022 --print('receive'); print(self.socket);
1022 self.time = socket.gettime(); 1023 self.time = time_now();
1023 1024
1024 local response = self:decode(packet, force); 1025 local response = self:decode(packet, force);
1025 if response and self.active[response.header.id] 1026 if response and self.active[response.header.id]
1026 and self.active[response.header.id][response.question.raw] then 1027 and self.active[response.header.id][response.question.raw] then
1027 --print('received response'); 1028 --print('received response');
1070 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse 1071 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
1071 --print(':pulse'); 1072 --print(':pulse');
1072 while self:receive() do end 1073 while self:receive() do end
1073 if not next(self.active) then return nil; end 1074 if not next(self.active) then return nil; end
1074 1075
1075 self.time = socket.gettime(); 1076 self.time = time_now();
1076 for id,queries in pairs(self.active) do 1077 for id,queries in pairs(self.active) do
1077 for question,o in pairs(queries) do 1078 for question,o in pairs(queries) do
1078 if self.time >= o.retry then 1079 if self.time >= o.retry then
1079 1080
1080 o.server = o.server + 1; 1081 o.server = o.server + 1;