From: beware Date: Mon, 12 Dec 2011 03:01:06 +0000 (+0000) Subject: removed use of global vars in dnssync, should be reentrant safe X-Git-Url: http://www.lcore.org/git/lcore.git/commitdiff_plain/f3321a0eafbeda0e8ebfb420974d391d7904ed44 removed use of global vars in dnssync, should be reentrant safe git-svn-id: file:///svnroot/lcore/trunk@111 b1de8a11-f9be-4011-bde0-cc7ace90066a --- diff --git a/dnssync.pas b/dnssync.pas index 9a0c2c1..d037a68 100644 --- a/dnssync.pas +++ b/dnssync.pas @@ -58,10 +58,6 @@ const toport='53'; -var - id:integer; - - sendquerytime:array[0..numsock-1] of integer; implementation {$ifdef win32} @@ -77,11 +73,7 @@ implementation {$ifdef syncdnscore} -var - numsockused:integer; - fd:array[0..numsock-1] of integer; - state:array[0..numsock-1] of tdnsstate; - toaddr:array[0..numsock-1] of tbinip; + {$ifdef win32} const @@ -107,45 +99,43 @@ begin end; -function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean; +type tdnsstatearr=array[0..numsock-1] of tdnsstate; + +procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer); var - ip : tbinip; - port : ansistring; - inaddr : TInetSockAddrV; -begin -{ writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);} - result := false; - if len = 0 then exit; {no packet} + selectresult : integer; + fds : fdset; - ip := getcurrentsystemnameserverbin(id); + endtime : longint; + starttime : longint; + wrapmode : boolean; + currenttime : integer; - {$ifdef ipv6}{$ifdef win32} - if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6; - {$endif}{$endif} + lag : ttimeval; + selecttimeout : ttimeval; + socknum:integer; + needprocessing:array[0..numsock-1] of boolean; + finished:array[0..numsock-1] of boolean; + a,b:integer; - port := toport; - toaddr[socknum] := ip; - makeinaddrv(toaddr[socknum],port,inaddr); + Src : TInetSockAddrV; + Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src; + SrcLen : Integer; + fromip:tbinip; + fromport:ansistring; + + fd:array[0..numsock-1] of integer; + toaddr:array[0..numsock-1] of tbinip; + id:integer; + sendquerytime:array[0..numsock-1] of integer; - sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr)); - sendquerytime[socknum] := getts; - result := true; -end; procedure setupsocket; var inAddrtemp : TInetSockAddrV; - a:integer; biniptemp:tbinip; - + a:integer; begin - //init both sockets smultaneously, always, so they get succesive fd's - - {recreate sockets every time, reusing them will fail (hang) if the nameserver is changed - also changing the nameserver can't possibly work} - {if fd[0] > 0 then exit;} - for a := 0 to numsock-1 do if (fd[a] > 0) then closesocket(fd[a]); - biniptemp := getcurrentsystemnameserverbin(id); //must get the DNS server here so we know to init v4 or v6 @@ -167,42 +157,53 @@ begin end; end; -procedure resolveloop(timeout:integer); +procedure cleanupsockets; var - selectresult : integer; - fds : fdset; + a:integer; +begin + for a := 0 to numsockused-1 do closesocket(fd[a]); +end; - endtime : longint; - starttime : longint; - wrapmode : boolean; - currenttime : integer; +function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean; +var + ip : tbinip; + port : ansistring; + inaddr : TInetSockAddrV; +begin +{ writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);} + result := false; + if len = 0 then exit; {no packet} - lag : ttimeval; - selecttimeout : ttimeval; - socknum:integer; - needprocessing:array[0..numsock-1] of boolean; - finished:array[0..numsock-1] of boolean; - a,b:integer; + ip := getcurrentsystemnameserverbin(id); - Src : TInetSockAddrV; - Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src; - SrcLen : Integer; - fromip:tbinip; - fromport:ansistring; + {$ifdef ipv6}{$ifdef win32} + if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6; + {$endif}{$endif} + + port := toport; + toaddr[socknum] := ip; + makeinaddrv(toaddr[socknum],port,inaddr); + + sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr)); + sendquerytime[socknum] := getts; + result := true; +end; begin if timeout < mintimeout then timeout := defaulttimeout; - starttime := getts; - endtime := starttime + timeout; - if (endtime and tswrap)=0 then begin - wrapmode := false; - end else begin - wrapmode := true; - end; - endtime := endtime and tsmask; + starttime := getts; + endtime := starttime + timeout; + if (endtime and tswrap)=0 then begin + wrapmode := false; + end else begin + wrapmode := true; + end; + endtime := endtime and tsmask; setupsocket; + + for socknum := 0 to numsockused-1 do begin needprocessing[socknum] := true; finished[socknum] := false; @@ -223,6 +224,7 @@ begin if finished[a] then inc(b); end; if (b = numsockused) then begin + cleanupsockets; exit; end; //onrequestdone(self,0); @@ -282,6 +284,7 @@ begin reportlag(id,-1); if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin + cleanupsockets; exit; end else begin //resend @@ -302,6 +305,10 @@ var a:integer; biniptemp:tbinip; l:tbiniplist; + + numsockused:integer; + state:tdnsstatearr; + begin ipstrtobin(name,biniptemp); if biniptemp.family <> 0 then begin @@ -350,7 +357,7 @@ begin end; {$endif} - resolveloop(timeout); + resolveloop(timeout,state,numsockused); if (numsockused = 1) then begin biniplist_addlist(result,state[0].resultlist); @@ -378,6 +385,8 @@ end; function reverselookup(ip:tbinip;timeout:integer):ansistring; var dummy : integer; + numsockused:integer; + state:tdnsstatearr; begin {$ifdef win32} if usewindns then begin @@ -388,7 +397,7 @@ begin {$ifdef syncdnscore} setstate_reverse(ip,state[0]); numsockused := 1; - resolveloop(timeout); + resolveloop(timeout,state,numsockused); result := state[0].resultstr; {$endif} end;