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 mswindows}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
135 a,retrycount,porttemp:integer;
\r
136 bindresult:boolean;
\r
138 biniptemp := getcurrentsystemnameserverbin(id);
\r
139 //must get the DNS server here so we know to init v4 or v6
\r
141 if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
\r
144 for a := 0 to numsockused-1 do begin
\r
147 if (retrycount <= 1) then begin
\r
148 porttemp := 0; //for the last attempt let the OS decide
\r
150 porttemp := 1024 + randominteger(65536 - 1024);
\r
153 makeinaddrv(biniptemp,inttostr( porttemp ),inaddrtemp);
\r
155 fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
\r
156 bindresult := {$ifdef mswindows}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp));
\r
158 until (retrycount <= 0) or (bindresult);
\r
160 If (not bindresult) Then begin
\r
162 raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
\r
164 raise Exception.create('unable to bind '+inttostr(socketError));
\r
170 procedure cleanupsockets;
\r
174 for a := 0 to numsockused-1 do closesocket(fd[a]);
\r
177 function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
\r
181 inaddr : TInetSockAddrV;
\r
183 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
185 if len = 0 then exit; {no packet}
\r
187 ip := getcurrentsystemnameserverbin(id);
\r
189 {$ifdef ipv6}{$ifdef mswindows}
\r
190 if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;
\r
194 toaddr[socknum] := ip;
\r
195 makeinaddrv(toaddr[socknum],port,inaddr);
\r
197 sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
\r
198 sendquerytime[socknum] := getts;
\r
203 if timeout < mintimeout then timeout := defaulttimeout;
\r
205 starttime := getts;
\r
206 endtime := starttime + timeout;
\r
207 if (endtime and tswrap)=0 then begin
\r
212 endtime := endtime and tsmask;
\r
217 for socknum := 0 to numsockused-1 do begin
\r
218 needprocessing[socknum] := true;
\r
219 finished[socknum] := false;
\r
223 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
224 state_process(state[socknum]);
\r
225 case state[socknum].resultaction of
\r
226 action_ignore: begin
\r
230 finished[socknum] := true;
\r
231 //exit if all resolvers are finished
\r
233 for a := 0 to numsockused-1 do begin
\r
234 if finished[a] then inc(b);
\r
236 if (b = numsockused) then begin
\r
240 //onrequestdone(self,0);
\r
242 action_sendquery:begin
\r
243 { writeln('send query');}
\r
244 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
247 needprocessing[socknum] := false;
\r
250 currenttime := getts;
\r
251 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
254 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
255 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
256 selecttimeout.tv_sec := 0;
\r
257 selecttimeout.tv_usec := retryafter;
\r
259 //find the highest of the used fd's
\r
261 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
262 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
263 if selectresult > 0 then begin
\r
264 currenttime := getts;
\r
265 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
266 { writeln('selectresult>0');}
\r
267 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
269 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
270 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
272 reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
274 SrcLen := SizeOf(Src);
\r
275 state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
\r
277 if (state[socknum].recvpacketlen > 0) then begin
\r
278 fromip := inaddrvtobinip(Src);
\r
279 fromport := inttostr(htons(src.InAddr.port));
\r
280 if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
\r
281 // writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
\r
282 state[socknum].recvpacketlen := 0;
\r
284 state[socknum].parsepacket := true;
\r
285 needprocessing[socknum] := true;
\r
290 if selectresult < 0 then exit;
\r
291 if selectresult = 0 then begin
\r
293 currenttime := getts;
\r
296 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
301 for socknum := numsockused-1 downto 0 do begin
\r
302 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
312 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
319 numsockused:integer;
\r
320 state:tdnsstatearr;
\r
323 ipstrtobin(name,biniptemp);
\r
324 if biniptemp.family <> 0 then begin
\r
325 result := biniplist_new;
\r
326 biniplist_add(result,biniptemp);
\r
327 exit; //it was an IP address, no need for dns
\r
331 if usewindns then begin
\r
332 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
333 result := winforwardlookuplist(name,a,dummy);
\r
335 if (useaf = useaf_preferv4) then begin
\r
336 {prefer mode: sort the IP's}
\r
337 l := biniplist_new;
\r
338 addipsoffamily(l,result,af_inet);
\r
339 addipsoffamily(l,result,af_inet6);
\r
342 if (useaf = useaf_preferv6) then begin
\r
343 {prefer mode: sort the IP's}
\r
344 l := biniplist_new;
\r
345 addipsoffamily(l,result,af_inet6);
\r
346 addipsoffamily(l,result,af_inet);
\r
353 {$ifdef syncdnscore}
\r
354 {$ifdef ipv6}initpreferredmode;{$endif}
\r
358 result := biniplist_new;
\r
359 if (useaf <> useaf_v6) then begin
\r
360 setstate_forward(name,state[numsockused],af_inet);
\r
364 if (useaf <> useaf_v4) then begin
\r
365 setstate_forward(name,state[numsockused],af_inet6);
\r
370 resolveloop(timeout,state,numsockused);
\r
372 if (numsockused = 1) then begin
\r
373 biniplist_addlist(result,state[0].resultlist);
\r
375 end else if (useaf = useaf_preferv6) then begin
\r
376 biniplist_addlist(result,state[1].resultlist);
\r
377 biniplist_addlist(result,state[0].resultlist);
\r
379 biniplist_addlist(result,state[0].resultlist);
\r
380 biniplist_addlist(result,state[1].resultlist);
\r
387 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
389 listtemp:tbiniplist;
\r
391 listtemp := forwardlookuplist(name,timeout);
\r
392 result := biniplist_get(listtemp,0);
\r
395 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
398 numsockused:integer;
\r
399 state:tdnsstatearr;
\r
402 if usewindns then begin
\r
403 result := winreverselookup(ip,dummy);
\r
407 {$ifdef syncdnscore}
\r
408 setstate_reverse(ip,state[0]);
\r
410 resolveloop(timeout,state,numsockused);
\r
411 result := state[0].resultstr;
\r
417 wsadata : twsadata;
\r
420 WSAStartUp($2,wsadata);
\r