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
74 type tdnsstatearr=array[0..numsock-1] of tdnsstate;
\r
76 {$ifdef syncdnscore}
\r
81 winsocket = 'wsock32.dll';
\r
82 function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto';
\r
83 function bind(s: TSocket; var addr: TinetSockAddrV; namelen: Integer): Longbool; stdcall; external winsocket name 'bind';
\r
89 function getts:integer;
\r
92 result := GetTickCount and tsmask;
\r
98 result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask;
\r
102 procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer);
\r
104 selectresult : integer;
\r
108 starttime : longint;
\r
109 wrapmode : boolean;
\r
110 currenttime : integer;
\r
113 selecttimeout : ttimeval;
\r
115 needprocessing:array[0..numsock-1] of boolean;
\r
116 finished:array[0..numsock-1] of boolean;
\r
119 Src : TInetSockAddrV;
\r
120 Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;
\r
123 fromport:ansistring;
\r
125 fd:array[0..numsock-1] of integer;
\r
126 toaddr:array[0..numsock-1] of tbinip;
\r
128 sendquerytime:array[0..numsock-1] of integer;
\r
131 procedure setupsocket;
\r
133 inAddrtemp : TInetSockAddrV;
\r
137 biniptemp := getcurrentsystemnameserverbin(id);
\r
138 //must get the DNS server here so we know to init v4 or v6
\r
140 if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
\r
143 for a := 0 to numsockused-1 do begin
\r
144 makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
\r
146 fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
\r
148 If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin
\r
150 raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
\r
152 raise Exception.create('unable to bind '+inttostr(socketError));
\r
158 procedure cleanupsockets;
\r
162 for a := 0 to numsockused-1 do closesocket(fd[a]);
\r
165 function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
\r
169 inaddr : TInetSockAddrV;
\r
171 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
173 if len = 0 then exit; {no packet}
\r
175 ip := getcurrentsystemnameserverbin(id);
\r
177 {$ifdef ipv6}{$ifdef win32}
\r
178 if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;
\r
182 toaddr[socknum] := ip;
\r
183 makeinaddrv(toaddr[socknum],port,inaddr);
\r
185 sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
\r
186 sendquerytime[socknum] := getts;
\r
191 if timeout < mintimeout then timeout := defaulttimeout;
\r
193 starttime := getts;
\r
194 endtime := starttime + timeout;
\r
195 if (endtime and tswrap)=0 then begin
\r
200 endtime := endtime and tsmask;
\r
205 for socknum := 0 to numsockused-1 do begin
\r
206 needprocessing[socknum] := true;
\r
207 finished[socknum] := false;
\r
211 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
212 state_process(state[socknum]);
\r
213 case state[socknum].resultaction of
\r
214 action_ignore: begin
\r
218 finished[socknum] := true;
\r
219 //exit if all resolvers are finished
\r
221 for a := 0 to numsockused-1 do begin
\r
222 if finished[a] then inc(b);
\r
224 if (b = numsockused) then begin
\r
228 //onrequestdone(self,0);
\r
230 action_sendquery:begin
\r
231 { writeln('send query');}
\r
232 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
235 needprocessing[socknum] := false;
\r
238 currenttime := getts;
\r
239 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
242 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
243 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
244 selecttimeout.tv_sec := 0;
\r
245 selecttimeout.tv_usec := retryafter;
\r
247 //find the highest of the used fd's
\r
249 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
250 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
251 if selectresult > 0 then begin
\r
252 currenttime := getts;
\r
253 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
254 { writeln('selectresult>0');}
\r
255 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
257 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
258 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
260 reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
262 SrcLen := SizeOf(Src);
\r
263 state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
\r
265 if (state[socknum].recvpacketlen > 0) then begin
\r
266 fromip := inaddrvtobinip(Src);
\r
267 fromport := inttostr(htons(src.InAddr.port));
\r
268 if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
\r
269 // writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
\r
270 state[socknum].recvpacketlen := 0;
\r
272 state[socknum].parsepacket := true;
\r
273 needprocessing[socknum] := true;
\r
278 if selectresult < 0 then exit;
\r
279 if selectresult = 0 then begin
\r
281 currenttime := getts;
\r
284 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
289 for socknum := numsockused-1 downto 0 do begin
\r
290 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
300 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
307 numsockused:integer;
\r
308 state:tdnsstatearr;
\r
311 ipstrtobin(name,biniptemp);
\r
312 if biniptemp.family <> 0 then begin
\r
313 result := biniplist_new;
\r
314 biniplist_add(result,biniptemp);
\r
315 exit; //it was an IP address, no need for dns
\r
319 if usewindns then begin
\r
320 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
321 result := winforwardlookuplist(name,a,dummy);
\r
323 if (useaf = useaf_preferv4) then begin
\r
324 {prefer mode: sort the IP's}
\r
325 l := biniplist_new;
\r
326 addipsoffamily(l,result,af_inet);
\r
327 addipsoffamily(l,result,af_inet6);
\r
330 if (useaf = useaf_preferv6) then begin
\r
331 {prefer mode: sort the IP's}
\r
332 l := biniplist_new;
\r
333 addipsoffamily(l,result,af_inet6);
\r
334 addipsoffamily(l,result,af_inet);
\r
341 {$ifdef syncdnscore}
\r
342 {$ifdef ipv6}initpreferredmode;{$endif}
\r
346 result := biniplist_new;
\r
347 if (useaf <> useaf_v6) then begin
\r
348 setstate_forward(name,state[numsockused],af_inet);
\r
352 if (useaf <> useaf_v4) then begin
\r
353 setstate_forward(name,state[numsockused],af_inet6);
\r
358 resolveloop(timeout,state,numsockused);
\r
360 if (numsockused = 1) then begin
\r
361 biniplist_addlist(result,state[0].resultlist);
\r
363 end else if (useaf = useaf_preferv6) then begin
\r
364 biniplist_addlist(result,state[1].resultlist);
\r
365 biniplist_addlist(result,state[0].resultlist);
\r
367 biniplist_addlist(result,state[0].resultlist);
\r
368 biniplist_addlist(result,state[1].resultlist);
\r
375 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
377 listtemp:tbiniplist;
\r
379 listtemp := forwardlookuplist(name,timeout);
\r
380 result := biniplist_get(listtemp,0);
\r
383 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
386 numsockused:integer;
\r
387 state:tdnsstatearr;
\r
390 if usewindns then begin
\r
391 result := winreverselookup(ip,dummy);
\r
395 {$ifdef syncdnscore}
\r
396 setstate_reverse(ip,state[0]);
\r
398 resolveloop(timeout,state,numsockused);
\r
399 result := state[0].resultstr;
\r
405 wsadata : twsadata;
\r
408 WSAStartUp($2,wsadata);
\r