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 ip := getcurrentsystemnameserverbin(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] := ip;
\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 biniptemp := getcurrentsystemnameserverbin(id);
\r
146 //must get the DNS server here so we know to init v4 or v6
\r
148 if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
\r
151 for a := 0 to numsockused-1 do begin
\r
152 makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
\r
154 fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
\r
156 If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin
\r
158 raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
\r
160 raise Exception.create('unable to bind '+inttostr(socketError));
\r
166 procedure resolveloop(timeout:integer);
\r
168 selectresult : integer;
\r
172 starttime : longint;
\r
173 wrapmode : boolean;
\r
174 currenttime : integer;
\r
177 selecttimeout : ttimeval;
\r
179 needprocessing:array[0..numsock-1] of boolean;
\r
180 finished:array[0..numsock-1] of boolean;
\r
183 Src : TInetSockAddrV;
\r
184 Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;
\r
187 fromport:ansistring;
\r
190 if timeout < mintimeout then timeout := defaulttimeout;
\r
192 starttime := getts;
\r
193 endtime := starttime + timeout;
\r
194 if (endtime and tswrap)=0 then begin
\r
199 endtime := endtime and tsmask;
\r
202 for socknum := 0 to numsockused-1 do begin
\r
203 needprocessing[socknum] := true;
\r
204 finished[socknum] := false;
\r
208 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
209 state_process(state[socknum]);
\r
210 case state[socknum].resultaction of
\r
211 action_ignore: begin
\r
215 finished[socknum] := true;
\r
216 //exit if all resolvers are finished
\r
218 for a := 0 to numsockused-1 do begin
\r
219 if finished[a] then inc(b);
\r
221 if (b = numsockused) then begin
\r
224 //onrequestdone(self,0);
\r
226 action_sendquery:begin
\r
227 { writeln('send query');}
\r
228 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
231 needprocessing[socknum] := false;
\r
234 currenttime := getts;
\r
235 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
238 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
239 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
240 selecttimeout.tv_sec := 0;
\r
241 selecttimeout.tv_usec := retryafter;
\r
243 //find the highest of the used fd's
\r
245 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
246 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
247 if selectresult > 0 then begin
\r
248 currenttime := getts;
\r
249 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
250 { writeln('selectresult>0');}
\r
251 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
253 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
254 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
256 reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
258 SrcLen := SizeOf(Src);
\r
259 state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
\r
261 if (state[socknum].recvpacketlen > 0) then begin
\r
262 fromip := inaddrvtobinip(Src);
\r
263 fromport := inttostr(htons(src.InAddr.port));
\r
264 if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
\r
265 // writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
\r
266 state[socknum].recvpacketlen := 0;
\r
268 state[socknum].parsepacket := true;
\r
269 needprocessing[socknum] := true;
\r
274 if selectresult < 0 then exit;
\r
275 if selectresult = 0 then begin
\r
277 currenttime := getts;
\r
280 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
284 for socknum := numsockused-1 downto 0 do begin
\r
285 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
295 function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
\r
302 ipstrtobin(name,biniptemp);
\r
303 if biniptemp.family <> 0 then begin
\r
304 result := biniplist_new;
\r
305 biniplist_add(result,biniptemp);
\r
306 exit; //it was an IP address, no need for dns
\r
310 if usewindns then begin
\r
311 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
312 result := winforwardlookuplist(name,a,dummy);
\r
314 if (useaf = useaf_preferv4) then begin
\r
315 {prefer mode: sort the IP's}
\r
316 l := biniplist_new;
\r
317 addipsoffamily(l,result,af_inet);
\r
318 addipsoffamily(l,result,af_inet6);
\r
321 if (useaf = useaf_preferv6) then begin
\r
322 {prefer mode: sort the IP's}
\r
323 l := biniplist_new;
\r
324 addipsoffamily(l,result,af_inet6);
\r
325 addipsoffamily(l,result,af_inet);
\r
332 {$ifdef syncdnscore}
\r
333 {$ifdef ipv6}initpreferredmode;{$endif}
\r
337 result := biniplist_new;
\r
338 if (useaf <> useaf_v6) then begin
\r
339 setstate_forward(name,state[numsockused],af_inet);
\r
343 if (useaf <> useaf_v4) then begin
\r
344 setstate_forward(name,state[numsockused],af_inet6);
\r
349 resolveloop(timeout);
\r
351 if (numsockused = 1) then begin
\r
352 biniplist_addlist(result,state[0].resultlist);
\r
354 end else if (useaf = useaf_preferv6) then begin
\r
355 biniplist_addlist(result,state[1].resultlist);
\r
356 biniplist_addlist(result,state[0].resultlist);
\r
358 biniplist_addlist(result,state[0].resultlist);
\r
359 biniplist_addlist(result,state[1].resultlist);
\r
366 function forwardlookup(name:ansistring;timeout:integer):tbinip;
\r
368 listtemp:tbiniplist;
\r
370 listtemp := forwardlookuplist(name,timeout);
\r
371 result := biniplist_get(listtemp,0);
\r
374 function reverselookup(ip:tbinip;timeout:integer):ansistring;
\r
379 if usewindns then begin
\r
380 result := winreverselookup(ip,dummy);
\r
384 {$ifdef syncdnscore}
\r
385 setstate_reverse(ip,state[0]);
\r
387 resolveloop(timeout);
\r
388 result := state[0].resultstr;
\r
394 wsadata : twsadata;
\r
397 WSAStartUp($2,wsadata);
\r