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
30 //convert a name to an IP
\r
31 //will return v4 or v6 depending on what seems favorable, or manual preference setting
\r
32 //on error the binip will have a family of 0 (other fiels are also currently
\r
33 //zeroed out but may be used for further error information in future)
\r
34 //timeout is in miliseconds, it is ignored when using windows dns
\r
35 function forwardlookup(name:string;timeout:integer):tbinip;
\r
37 //convert a name to a list of all IP's returned
\r
38 //this returns both v4 and v6 IP's, or possibly only v4 or v6, depending on settings
\r
39 //on error, returns an empty list
\r
40 function forwardlookuplist(name:string;timeout:integer):tbiniplist;
\r
43 //convert an IP to a name, on error a null string will be returned, other
\r
45 function reverselookup(ip:tbinip;timeout:integer):string;
\r
53 numsock=1{$ifdef ipv6}+1{$endif};
\r
54 defaulttimeout=10000;
\r
55 const mintimeout=16;
\r
58 dnssyncserver:string;
\r
61 sendquerytime:array[0..numsock-1] of integer;
\r
70 {$define syncdnscore}
\r
74 {$i ltimevalstuff.inc}
\r
77 numsockused:integer;
\r
78 fd:array[0..numsock-1] of integer;
\r
79 state:array[0..numsock-1] of tdnsstate;
\r
81 {$ifdef syncdnscore}
\r
85 winsocket = 'wsock32.dll';
\r
86 function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto';
\r
87 function bind(s: TSocket; var addr: TinetSockAddrV; namelen: Integer): Longbool; stdcall; external winsocket name 'bind';
\r
93 function getts:integer;
\r
96 result := GetTickCount and tsmask;
\r
101 gettimeofday(temp);
\r
102 result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask;
\r
107 function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
\r
112 inaddr : TInetSockAddrV;
\r
114 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
116 if len = 0 then exit; {no packet}
\r
118 if dnssyncserver <> '' then addr := dnssyncserver else addr := getcurrentsystemnameserver(id);
\r
121 makeinaddrv(ipstrtobinf(addr),port,inaddr);
\r
123 sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
\r
124 sendquerytime[socknum] := getts;
\r
128 procedure setupsocket;
\r
130 inAddrtemp : TInetSockAddrV;
\r
135 //init both sockets smultaneously, always, so they get succesive fd's
\r
136 if fd[0] > 0 then exit;
\r
138 if dnssyncserver <> '' then addr := dnssyncserver else addr := getcurrentsystemnameserver(id);
\r
139 //must get the DNS server here so we know to init v4 or v6
\r
141 fillchar(inaddrtemp,sizeof(inaddrtemp),0);
\r
142 ipstrtobin(addr,biniptemp);
\r
143 if biniptemp.family = 0 then biniptemp.family := AF_INET;
\r
145 inaddrtemp.inaddr.family := biniptemp.family;
\r
147 for a := 0 to numsockused-1 do begin
\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 resolveloop(timeout:integer);
\r
162 selectresult : integer;
\r
166 starttime : longint;
\r
167 wrapmode : boolean;
\r
168 currenttime : integer;
\r
171 currenttimeout : ttimeval;
\r
172 selecttimeout : ttimeval;
\r
174 needprocessing:array[0..numsock-1] of boolean;
\r
175 finished:array[0..numsock-1] of boolean;
\r
179 if timeout < mintimeout then timeout := defaulttimeout;
\r
181 starttime := getts;
\r
182 endtime := starttime + timeout;
\r
183 if (endtime and tswrap)=0 then begin
\r
188 endtime := endtime and tsmask;
\r
191 for socknum := 0 to numsockused-1 do begin
\r
192 needprocessing[socknum] := true;
\r
193 finished[socknum] := false;
\r
197 for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
\r
198 state_process(state[socknum]);
\r
199 case state[socknum].resultaction of
\r
200 action_ignore: begin
\r
204 finished[socknum] := true;
\r
205 //exit if all resolvers are finished
\r
207 for a := 0 to numsockused-1 do begin
\r
208 if finished[a] then inc(b);
\r
210 if (b = numsockused) then begin
\r
213 //onrequestdone(self,0);
\r
215 action_sendquery:begin
\r
216 { writeln('send query');}
\r
217 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
220 needprocessing[socknum] := false;
\r
223 currenttime := getts;
\r
224 msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
\r
227 for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
\r
228 if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
\r
229 selecttimeout.tv_sec := 0;
\r
230 selecttimeout.tv_usec := retryafter;
\r
232 //find the highest of the used fd's
\r
234 for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
\r
235 selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
\r
236 if selectresult > 0 then begin
\r
237 currenttime := getts;
\r
238 for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
\r
239 { writeln('selectresult>0');}
\r
240 //why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
\r
242 fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
\r
243 msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
\r
245 if dnssyncserver = '' then reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
\r
246 state[socknum].recvpacketlen := recv(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0);
\r
247 state[socknum].parsepacket := true;
\r
248 needprocessing[socknum] := true;
\r
251 if selectresult < 0 then exit;
\r
252 if selectresult = 0 then begin
\r
254 currenttime := getts;
\r
256 if dnssyncserver = '' then reportlag(id,-1);
\r
257 if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
\r
261 for socknum := numsockused-1 downto 0 do begin
\r
262 sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
\r
272 function forwardlookuplist(name:string;timeout:integer):tbiniplist;
\r
279 ipstrtobin(name,biniptemp);
\r
280 if biniptemp.family <> 0 then begin
\r
281 result := biniplist_new;
\r
282 biniplist_add(result,biniptemp);
\r
283 exit; //it was an IP address, no need for dns
\r
287 if usewindns then begin
\r
288 if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
\r
289 result := winforwardlookuplist(name,a,dummy);
\r
291 if (useaf = useaf_preferv4) then begin
\r
292 {prefer mode: sort the IP's}
\r
293 l := biniplist_new;
\r
294 addipsoffamily(l,result,af_inet);
\r
295 addipsoffamily(l,result,af_inet6);
\r
298 if (useaf = useaf_preferv6) then begin
\r
299 {prefer mode: sort the IP's}
\r
300 l := biniplist_new;
\r
301 addipsoffamily(l,result,af_inet6);
\r
302 addipsoffamily(l,result,af_inet);
\r
309 {$ifdef syncdnscore}
\r
310 {$ifdef linux}{$ifdef ipv6}initpreferredmode;{$endif}{$endif}
\r
314 result := biniplist_new;
\r
315 if (useaf <> useaf_v6) then begin
\r
316 setstate_forward(name,state[numsockused],af_inet);
\r
320 if (useaf <> useaf_v4) then begin
\r
321 setstate_forward(name,state[numsockused],af_inet6);
\r
326 resolveloop(timeout);
\r
328 if (numsockused = 1) then begin
\r
329 biniplist_addlist(result,state[0].resultlist);
\r
331 end else if (useaf = useaf_preferv6) then begin
\r
332 biniplist_addlist(result,state[1].resultlist);
\r
333 biniplist_addlist(result,state[0].resultlist);
\r
335 biniplist_addlist(result,state[0].resultlist);
\r
336 biniplist_addlist(result,state[1].resultlist);
\r
343 function forwardlookup(name:string;timeout:integer):tbinip;
\r
345 listtemp:tbiniplist;
\r
347 listtemp := forwardlookuplist(name,timeout);
\r
348 result := biniplist_get(listtemp,0);
\r
351 function reverselookup(ip:tbinip;timeout:integer):string;
\r
356 if usewindns then begin
\r
357 result := winreverselookup(ip,dummy);
\r
361 {$ifdef syncdnscore}
\r
362 setstate_reverse(ip,state[0]);
\r
364 resolveloop(timeout);
\r
365 result := state[0].resultstr;
\r
371 wsadata : twsadata;
\r
374 WSAStartUp($2,wsadata);
\r