sockets,\r
fd_utils,\r
{$endif}\r
- sysutils;\r
+ lcorernd,\r
+ sysutils,\r
+ ltimevalstuff;\r
\r
//convert a name to an IP\r
//will return v4 or v6 depending on what seems favorable, or manual preference setting\r
defaulttimeout=10000;\r
const mintimeout=16;\r
\r
+ toport='53';\r
+\r
var\r
- dnssyncserver:string;\r
id:integer;\r
\r
sendquerytime:array[0..numsock-1] of integer;\r
{$endif}\r
\r
{$i unixstuff.inc}\r
-{$i ltimevalstuff.inc}\r
\r
var\r
numsockused:integer;\r
fd:array[0..numsock-1] of integer;\r
state:array[0..numsock-1] of tdnsstate;\r
+ toaddr:array[0..numsock-1] of tbinip;\r
\r
{$ifdef syncdnscore}\r
\r
result := false;\r
if len = 0 then exit; {no packet}\r
\r
- if dnssyncserver <> '' then addr := dnssyncserver else addr := getcurrentsystemnameserver(id);\r
- port := '53';\r
+ if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(id);\r
+\r
+ {$ifdef ipv6}{$ifdef win32}\r
+ if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;\r
+ {$endif}{$endif}\r
\r
- makeinaddrv(ipstrtobinf(addr),port,inaddr);\r
+ port := toport;\r
+ toaddr[socknum] := ipstrtobinf(addr);\r
+ makeinaddrv(toaddr[socknum],port,inaddr);\r
\r
sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));\r
sendquerytime[socknum] := getts;\r
//init both sockets smultaneously, always, so they get succesive fd's\r
if fd[0] > 0 then exit;\r
\r
- if dnssyncserver <> '' then addr := dnssyncserver else addr := getcurrentsystemnameserver(id);\r
+ if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(id);\r
//must get the DNS server here so we know to init v4 or v6\r
\r
- fillchar(inaddrtemp,sizeof(inaddrtemp),0);\r
ipstrtobin(addr,biniptemp);\r
- if biniptemp.family = 0 then biniptemp.family := AF_INET;\r
\r
- inaddrtemp.inaddr.family := biniptemp.family;\r
+ if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');\r
+\r
\r
for a := 0 to numsockused-1 do begin\r
+ makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);\r
+\r
fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);\r
\r
If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin\r
finished:array[0..numsock-1] of boolean;\r
a,b:integer;\r
\r
+ Src : TInetSockAddrV;\r
+ Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;\r
+ SrcLen : Integer;\r
+ fromip:tbinip;\r
+ fromport:string;\r
+\r
begin\r
if timeout < mintimeout then timeout := defaulttimeout;\r
\r
fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);\r
msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);\r
\r
- if dnssyncserver = '' then reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);\r
- state[socknum].recvpacketlen := recv(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0);\r
- state[socknum].parsepacket := true;\r
- needprocessing[socknum] := true;\r
+ if overridednsserver = '' then reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);\r
+\r
+ SrcLen := SizeOf(Src);\r
+ state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);\r
+\r
+ if (state[socknum].recvpacketlen > 0) then begin\r
+ fromip := inaddrvtobinip(Src);\r
+ fromport := inttostr(htons(src.InAddr.port));\r
+ if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin\r
+// writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);\r
+ state[socknum].recvpacketlen := 0;\r
+ end else begin\r
+ state[socknum].parsepacket := true;\r
+ needprocessing[socknum] := true;\r
+ end;\r
+ end;\r
end;\r
end;\r
if selectresult < 0 then exit;\r
\r
currenttime := getts;\r
\r
- if dnssyncserver = '' then reportlag(id,-1);\r
+ if overridednsserver = '' then reportlag(id,-1);\r
if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin\r
exit;\r
end else begin\r
{$endif}\r
begin\r
{$ifdef syncdnscore}\r
- {$ifdef linux}{$ifdef ipv6}initpreferredmode;{$endif}{$endif}\r
+ {$ifdef ipv6}initpreferredmode;{$endif}\r
\r
numsockused := 0;\r
\r
end else begin\r
biniplist_addlist(result,state[0].resultlist);\r
biniplist_addlist(result,state[1].resultlist);\r
- {$endif} \r
+ {$endif}\r
end;\r
{$endif}\r
end;\r