X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/e27ef2c6aab3a2a8051314bd37bb3b2386775f36..ed1d49cbe548d9fb26a50f1b2381b693c3c01790:/dnssync.pas?ds=inline diff --git a/dnssync.pas b/dnssync.pas index 7d6631c..f6b0281 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} @@ -75,14 +71,11 @@ implementation {$i unixstuff.inc} -var - numsockused:integer; - fd:array[0..numsock-1] of integer; - state:array[0..numsock-1] of tdnsstate; - toaddr:array[0..numsock-1] of tbinip; +type tdnsstatearr=array[0..numsock-1] of tdnsstate; {$ifdef syncdnscore} + {$ifdef win32} const winsocket = 'wsock32.dll'; @@ -106,57 +99,65 @@ begin {$endif} end; - -function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean; +procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer); var - a:integer; - addr : ansistring; - 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; - if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(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] := ipstrtobinf(addr); - 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; - addr:ansistring; + a,retrycount,porttemp:integer; + bindresult:boolean; begin - //init both sockets smultaneously, always, so they get succesive fd's - if fd[0] > 0 then exit; - - if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(id); + biniptemp := getcurrentsystemnameserverbin(id); //must get the DNS server here so we know to init v4 or v6 - ipstrtobin(addr,biniptemp); - if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0'); for a := 0 to numsockused-1 do begin - makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp); + retrycount := 5; + repeat + if (retrycount <= 1) then begin + porttemp := 0; //for the last attempt let the OS decide + end else begin + porttemp := 1024 + randominteger(65536 - 1024); + end; + + makeinaddrv(biniptemp,inttostr( porttemp ),inaddrtemp); - fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0); + fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0); + bindresult := {$ifdef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)); + dec(retrycount); + until (retrycount <= 0) or (bindresult); - If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin + If (not bindresult) Then begin {$ifdef win32} raise Exception.create('unable to bind '+inttostr(WSAGetLastError)); {$else} @@ -166,43 +167,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; - currenttimeout : 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 +234,7 @@ begin if finished[a] then inc(b); end; if (b = numsockused) then begin + cleanupsockets; exit; end; //onrequestdone(self,0); @@ -257,7 +269,7 @@ begin fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0); msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask); - if overridednsserver = '' then reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec); + reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec); SrcLen := SizeOf(Src); state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen); @@ -280,8 +292,9 @@ begin currenttime := getts; - if overridednsserver = '' then reportlag(id,-1); + reportlag(id,-1); if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin + cleanupsockets; exit; end else begin //resend @@ -299,9 +312,13 @@ end; function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist; var dummy : integer; - a,b:integer; + a:integer; biniptemp:tbinip; l:tbiniplist; + + numsockused:integer; + state:tdnsstatearr; + begin ipstrtobin(name,biniptemp); if biniptemp.family <> 0 then begin @@ -350,7 +367,7 @@ begin end; {$endif} - resolveloop(timeout); + resolveloop(timeout,state,numsockused); if (numsockused = 1) then begin biniplist_addlist(result,state[0].resultlist); @@ -378,6 +395,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 +407,7 @@ begin {$ifdef syncdnscore} setstate_reverse(ip,state[0]); numsockused := 1; - resolveloop(timeout); + resolveloop(timeout,state,numsockused); result := state[0].resultstr; {$endif} end;