1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
2 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
3 which is included in the package
\r
4 ----------------------------------------------------------------------------- }
\r
10 {$include lcoreconfig.inc}
\r
23 baseunix,unix,unixutil,
\r
32 //convert a name to an IP
\r
33 //will return v4 or v6 depending on what seems favorable, or manual preference setting
\r
34 //on error the binip will have a family of 0 (other fiels are also currently
\r
35 //zeroed out but may be used for further error information in future)
\r
36 //timeout is in miliseconds, it is ignored when using windows dns
\r
37 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
39 //convert a name to a list of all IP's returned
\r
40 //this returns both v4 and v6 IP's, or possibly only v4 or v6, depending on settings
\r
41 //on error, returns an empty list
\r
42 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
45 //convert an IP to a name, on error a null string will be returned, other
\r
47 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
55 numsock=1{$ifdef ipv6}+1{$endif};
\r
56 defaulttimeout=10000;
\r
57 const mintimeout=16;
\r
64 sendquerytime:array[0..numsock-1] of integer;
\r
73 {$define syncdnscore}
\r
79 {$ifdef syncdnscore}
\r
81 numsockused:integer;
\r
82 fd:array[0..numsock-1] of integer;
\r
83 state:array[0..numsock-1] of tdnsstate;
\r
84 toaddr:array[0..numsock-1] of tbinip;
\r
88 winsocket = 'wsock32.dll';
\r
89 function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto';
\r
90 function bind(s: TSocket; var addr: TinetSockAddrV; namelen: Integer): Longbool; stdcall; external winsocket name 'bind';
\r
96 function getts:integer;
\r
99 result := GetTickCount and tsmask;
\r
104 gettimeofday(temp);
\r
105 result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask;
\r
110 function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
\r
114 inaddr : TInetSockAddrV;
\r
116 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
118 if len = 0 then exit; {no packet}
\r
120 if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(id);
\r
122 {$ifdef ipv6}{$ifdef win32}
\r
123 if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;
\r
127 toaddr[socknum] := ipstrtobinf(addr);
\r
128 makeinaddrv(toaddr[socknum],port,inaddr);
\r
130 sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
\r
131 sendquerytime[socknum] := getts;
\r
135 procedure setupsocket;
\r
137 inAddrtemp : TInetSockAddrV;
\r
142 //init both sockets smultaneously, always, so they get succesive fd's
\r
143 if fd[0] > 0 then exit;
\r
145 if overridednsserver <> '' then addr := overridednsserver else addr := getcurrentsystemnameserver(id);
\r
146 //must get the DNS server here so we know to init v4 or v6
\r
148 ipstrtobin(addr,biniptemp);
\r
150 if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
\r
153 for a := 0 to numsockused-1 do begin
\r
154 makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
\r
156 fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
\r
158 If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin
\r
160 raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
\r
162 raise Exception.create('unable to bind '+inttostr(socketError));
\r
168 procedure resolveloop(timeout:integer);
\r
170 selectresult : integer;
\r
174 starttime : longint;
\r
175 wrapmode : boolean;
\r
176 currenttime : integer;
\r
179 selecttimeout : ttimeval;
\r
181 needprocessing:array[0..numsock-1] of boolean;
\r
182 finished:array[0..numsock-1] of boolean;
\r
185 Src : TInetSockAddrV;
\r
186 Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;
\r
189 fromport:ansistring;
\r
192 if timeout < mintimeout then timeout := defaulttimeout;
\r
194 starttime := getts;
\r
195 endtime := starttime + timeout;
\r
196 if (endtime and tswrap)=0 then begin
\r
201 endtime := endtime and tsmask;
\r
204 for socknum := 0 to numsockused-1 do begin
\r
205 needprocessing[socknum] := true;
\r
206 finished[socknum] := false;
\r
210 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
211 state_process(state[socknum]);
\r
212 case state[socknum].resultaction of
\r
213 action_ignore: begin
\r
217 finished[socknum] := true;
\r
218 //exit if all resolvers are finished
\r
220 for a := 0 to numsockused-1 do begin
\r
221 if finished[a] then inc(b);
\r
223 if (b = numsockused) then begin
\r
226 //onrequestdone(self,0);
\r
228 action_sendquery:begin
\r
229 { writeln('send query');}
\r
230 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
233 needprocessing[socknum] := false;
\r
236 currenttime := getts;
\r
237 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
240 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
241 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
242 selecttimeout.tv_sec := 0;
\r
243 selecttimeout.tv_usec := retryafter;
\r
245 //find the highest of the used fd's
\r
247 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
248 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
249 if selectresult > 0 then begin
\r
250 currenttime := getts;
\r
251 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
252 { writeln('selectresult>0');}
\r
253 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
255 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
256 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
258 if overridednsserver = '' then reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
260 SrcLen := SizeOf(Src);
\r
261 state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
\r
263 if (state[socknum].recvpacketlen > 0) then begin
\r
264 fromip := inaddrvtobinip(Src);
\r
265 fromport := inttostr(htons(src.InAddr.port));
\r
266 if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
\r
267 // writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
\r
268 state[socknum].recvpacketlen := 0;
\r
270 state[socknum].parsepacket := true;
\r
271 needprocessing[socknum] := true;
\r
276 if selectresult < 0 then exit;
\r
277 if selectresult = 0 then begin
\r
279 currenttime := getts;
\r
281 if overridednsserver = '' then reportlag(id,-1);
\r
282 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
286 for socknum := numsockused-1 downto 0 do begin
\r
287 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
297 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
304 ipstrtobin(name,biniptemp);
\r
305 if biniptemp.family <> 0 then begin
\r
306 result := biniplist_new;
\r
307 biniplist_add(result,biniptemp);
\r
308 exit; //it was an IP address, no need for dns
\r
312 if usewindns then begin
\r
313 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
314 result := winforwardlookuplist(name,a,dummy);
\r
316 if (useaf = useaf_preferv4) then begin
\r
317 {prefer mode: sort the IP's}
\r
318 l := biniplist_new;
\r
319 addipsoffamily(l,result,af_inet);
\r
320 addipsoffamily(l,result,af_inet6);
\r
323 if (useaf = useaf_preferv6) then begin
\r
324 {prefer mode: sort the IP's}
\r
325 l := biniplist_new;
\r
326 addipsoffamily(l,result,af_inet6);
\r
327 addipsoffamily(l,result,af_inet);
\r
334 {$ifdef syncdnscore}
\r
335 {$ifdef ipv6}initpreferredmode;{$endif}
\r
339 result := biniplist_new;
\r
340 if (useaf <> useaf_v6) then begin
\r
341 setstate_forward(name,state[numsockused],af_inet);
\r
345 if (useaf <> useaf_v4) then begin
\r
346 setstate_forward(name,state[numsockused],af_inet6);
\r
351 resolveloop(timeout);
\r
353 if (numsockused = 1) then begin
\r
354 biniplist_addlist(result,state[0].resultlist);
\r
356 end else if (useaf = useaf_preferv6) then begin
\r
357 biniplist_addlist(result,state[1].resultlist);
\r
358 biniplist_addlist(result,state[0].resultlist);
\r
360 biniplist_addlist(result,state[0].resultlist);
\r
361 biniplist_addlist(result,state[1].resultlist);
\r
368 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
370 listtemp:tbiniplist;
\r
372 listtemp := forwardlookuplist(name,timeout);
\r
373 result := biniplist_get(listtemp,0);
\r
376 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
381 if usewindns then begin
\r
382 result := winreverselookup(ip,dummy);
\r
386 {$ifdef syncdnscore}
\r
387 setstate_reverse(ip,state[0]);
\r
389 resolveloop(timeout);
\r
390 result := state[0].resultstr;
\r
396 wsadata : twsadata;
\r
399 WSAStartUp($2,wsadata);
\r