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
69 {$define syncdnscore}
\r
75 {$ifdef syncdnscore}
\r
80 winsocket = 'wsock32.dll';
\r
81 function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto';
\r
82 function bind(s: TSocket; var addr: TinetSockAddrV; namelen: Integer): Longbool; stdcall; external winsocket name 'bind';
\r
88 function getts:integer;
\r
91 result := GetTickCount and tsmask;
\r
97 result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask;
\r
102 type tdnsstatearr=array[0..numsock-1] of tdnsstate;
\r
104 procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer);
\r
106 selectresult : integer;
\r
110 starttime : longint;
\r
111 wrapmode : boolean;
\r
112 currenttime : integer;
\r
115 selecttimeout : ttimeval;
\r
117 needprocessing:array[0..numsock-1] of boolean;
\r
118 finished:array[0..numsock-1] of boolean;
\r
121 Src : TInetSockAddrV;
\r
122 Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;
\r
125 fromport:ansistring;
\r
127 fd:array[0..numsock-1] of integer;
\r
128 toaddr:array[0..numsock-1] of tbinip;
\r
130 sendquerytime:array[0..numsock-1] of integer;
\r
133 procedure setupsocket;
\r
135 inAddrtemp : TInetSockAddrV;
\r
139 biniptemp := getcurrentsystemnameserverbin(id);
\r
140 //must get the DNS server here so we know to init v4 or v6
\r
142 if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
\r
145 for a := 0 to numsockused-1 do begin
\r
146 makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
\r
148 fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
\r
150 If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin
\r
152 raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
\r
154 raise Exception.create('unable to bind '+inttostr(socketError));
\r
160 procedure cleanupsockets;
\r
164 for a := 0 to numsockused-1 do closesocket(fd[a]);
\r
167 function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
\r
171 inaddr : TInetSockAddrV;
\r
173 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
175 if len = 0 then exit; {no packet}
\r
177 ip := getcurrentsystemnameserverbin(id);
\r
179 {$ifdef ipv6}{$ifdef win32}
\r
180 if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;
\r
184 toaddr[socknum] := ip;
\r
185 makeinaddrv(toaddr[socknum],port,inaddr);
\r
187 sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
\r
188 sendquerytime[socknum] := getts;
\r
193 if timeout < mintimeout then timeout := defaulttimeout;
\r
195 starttime := getts;
\r
196 endtime := starttime + timeout;
\r
197 if (endtime and tswrap)=0 then begin
\r
202 endtime := endtime and tsmask;
\r
207 for socknum := 0 to numsockused-1 do begin
\r
208 needprocessing[socknum] := true;
\r
209 finished[socknum] := false;
\r
213 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
214 state_process(state[socknum]);
\r
215 case state[socknum].resultaction of
\r
216 action_ignore: begin
\r
220 finished[socknum] := true;
\r
221 //exit if all resolvers are finished
\r
223 for a := 0 to numsockused-1 do begin
\r
224 if finished[a] then inc(b);
\r
226 if (b = numsockused) then begin
\r
230 //onrequestdone(self,0);
\r
232 action_sendquery:begin
\r
233 { writeln('send query');}
\r
234 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
237 needprocessing[socknum] := false;
\r
240 currenttime := getts;
\r
241 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
244 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
245 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
246 selecttimeout.tv_sec := 0;
\r
247 selecttimeout.tv_usec := retryafter;
\r
249 //find the highest of the used fd's
\r
251 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
252 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
253 if selectresult > 0 then begin
\r
254 currenttime := getts;
\r
255 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
256 { writeln('selectresult>0');}
\r
257 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
259 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
260 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
262 reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
264 SrcLen := SizeOf(Src);
\r
265 state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
\r
267 if (state[socknum].recvpacketlen > 0) then begin
\r
268 fromip := inaddrvtobinip(Src);
\r
269 fromport := inttostr(htons(src.InAddr.port));
\r
270 if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
\r
271 // writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
\r
272 state[socknum].recvpacketlen := 0;
\r
274 state[socknum].parsepacket := true;
\r
275 needprocessing[socknum] := true;
\r
280 if selectresult < 0 then exit;
\r
281 if selectresult = 0 then begin
\r
283 currenttime := getts;
\r
286 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
291 for socknum := numsockused-1 downto 0 do begin
\r
292 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
302 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
309 numsockused:integer;
\r
310 state:tdnsstatearr;
\r
313 ipstrtobin(name,biniptemp);
\r
314 if biniptemp.family <> 0 then begin
\r
315 result := biniplist_new;
\r
316 biniplist_add(result,biniptemp);
\r
317 exit; //it was an IP address, no need for dns
\r
321 if usewindns then begin
\r
322 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
323 result := winforwardlookuplist(name,a,dummy);
\r
325 if (useaf = useaf_preferv4) then begin
\r
326 {prefer mode: sort the IP's}
\r
327 l := biniplist_new;
\r
328 addipsoffamily(l,result,af_inet);
\r
329 addipsoffamily(l,result,af_inet6);
\r
332 if (useaf = useaf_preferv6) then begin
\r
333 {prefer mode: sort the IP's}
\r
334 l := biniplist_new;
\r
335 addipsoffamily(l,result,af_inet6);
\r
336 addipsoffamily(l,result,af_inet);
\r
343 {$ifdef syncdnscore}
\r
344 {$ifdef ipv6}initpreferredmode;{$endif}
\r
348 result := biniplist_new;
\r
349 if (useaf <> useaf_v6) then begin
\r
350 setstate_forward(name,state[numsockused],af_inet);
\r
354 if (useaf <> useaf_v4) then begin
\r
355 setstate_forward(name,state[numsockused],af_inet6);
\r
360 resolveloop(timeout,state,numsockused);
\r
362 if (numsockused = 1) then begin
\r
363 biniplist_addlist(result,state[0].resultlist);
\r
365 end else if (useaf = useaf_preferv6) then begin
\r
366 biniplist_addlist(result,state[1].resultlist);
\r
367 biniplist_addlist(result,state[0].resultlist);
\r
369 biniplist_addlist(result,state[0].resultlist);
\r
370 biniplist_addlist(result,state[1].resultlist);
\r
377 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
379 listtemp:tbiniplist;
\r
381 listtemp := forwardlookuplist(name,timeout);
\r
382 result := biniplist_get(listtemp,0);
\r
385 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
388 numsockused:integer;
\r
389 state:tdnsstatearr;
\r
392 if usewindns then begin
\r
393 result := winreverselookup(ip,dummy);
\r
397 {$ifdef syncdnscore}
\r
398 setstate_reverse(ip,state[0]);
\r
400 resolveloop(timeout,state,numsockused);
\r
401 result := state[0].resultstr;
\r
407 wsadata : twsadata;
\r
410 WSAStartUp($2,wsadata);
\r