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
6 //FIXME: this code only ever seems to use one dns server for a request and does
\r
7 //not seem to have any form of retry code.
\r
16 {$ifdef winasyncdns}
\r
20 classes,binipstuff,dnscore,btime,lcorernd;
\r
22 {$include lcoreconfig.inc}
\r
25 numsock=1{$ifdef ipv6}+1{$endif};
\r
29 //after completion or cancelation a dnswinasync may be reused
\r
30 tdnsasync=class(tcomponent)
\r
33 //made a load of stuff private that does not appear to be part of the main
\r
34 //public interface. If you make any of it public again please consider the
\r
35 //consequences when using windows dns. --plugwash.
\r
36 sockets: array[0..numsock-1] of tlsocket;
\r
38 states: array[0..numsock-1] of tdnsstate;
\r
40 destinations: array[0..numsock-1] of tbinip;
\r
42 dnsserverids : array[0..numsock-1] of integer;
\r
44 {$ifdef winasyncdns}
\r
45 dwas : tdnswinasync;
\r
48 numsockused : integer;
\r
49 fresultlist : tbiniplist;
\r
50 requestaf : integer;
\r
51 procedure asyncprocess(socketno:integer);
\r
52 procedure receivehandler(sender:tobject;error:word);
\r
53 function sendquery(socketno:integer;const packet:tdnspacket;len:integer):boolean;
\r
54 {$ifdef winasyncdns}
\r
55 procedure winrequestdone(sender:tobject;error:word);
\r
59 onrequestdone:tsocketevent;
\r
61 //addr and port allow the application to specify a dns server specifically
\r
62 //for this dnsasync object. This is not a recommended mode of operation
\r
63 //because it limits the app to one dns server but is kept for compatibility
\r
65 addr,port:ansistring;
\r
67 overrideaf : integer;
\r
69 procedure cancel;//cancel an outstanding dns request
\r
70 function dnsresult:ansistring; //get result of dnslookup as a string
\r
71 procedure dnsresultbin(var binip:tbinip); //get result of dnslookup as a tbinip
\r
72 property dnsresultlist : tbiniplist read fresultlist;
\r
73 procedure forwardlookup(const name:ansistring); //start forward lookup,
\r
75 procedure reverselookup(const binip:tbinip); //start reverse lookup
\r
76 procedure customlookup(const name:ansistring;querytype:integer); //start custom type lookup
\r
78 constructor create(aowner:tcomponent); override;
\r
79 destructor destroy; override;
\r
87 constructor tdnsasync.create;
\r
89 inherited create(aowner);
\r
90 dnsserverids[0] := -1;
\r
91 sockets[0] := twsocket.create(self);
\r
92 sockets[0].tag := 0;
\r
94 dnsserverids[1] := -1;
\r
95 sockets[1] := twsocket.Create(self);
\r
96 sockets[1].tag := 1;
\r
100 destructor tdnsasync.destroy;
\r
102 socketno : integer;
\r
104 for socketno := 0 to numsock -1 do begin
\r
105 if assigned(sockets[socketno]) then begin
\r
106 if dnsserverids[socketno] >= 0 then begin
\r
107 reportlag(dnsserverids[socketno],-1);
\r
108 dnsserverids[socketno] := -1;
\r
110 sockets[socketno].release;
\r
111 setstate_request_init('',states[socketno]);
\r
115 {$ifdef winasyncdns}
\r
116 if assigned(dwas) then begin
\r
125 procedure tdnsasync.receivehandler(sender:tobject;error:word);
\r
127 socketno : integer;
\r
128 Src : TInetSockAddrV;
\r
131 fromport:ansistring;
\r
133 socketno := tlsocket(sender).tag;
\r
134 //writeln('got a reply on socket number ',socketno);
\r
135 fillchar(states[socketno].recvpacket,sizeof(states[socketno].recvpacket),0);
\r
137 SrcLen := SizeOf(Src);
\r
138 states[socketno].recvpacketlen := twsocket(sender).ReceiveFrom(@(states[socketno].recvpacket), SizeOf(states[socketno].recvpacket), Src, SrcLen);
\r
140 fromip := inaddrvtobinip(Src);
\r
141 fromport := inttostr(htons(src.InAddr.port));
\r
143 if ((not comparebinip(fromip,destinations[socketno])) or (fromport <> port)) then begin
\r
144 // writeln('dnsasync received from wrong IP:port ',ipbintostr(fromip),'#',fromport,', expected ',ipbintostr(destinations[socketno]),'#',port);
\r
148 states[socketno].parsepacket := true;
\r
149 if states[socketno].resultaction <> action_done then begin
\r
150 //we ignore packets that come after we are done
\r
151 if dnsserverids[socketno] >= 0 then begin
\r
152 reportlag(dnsserverids[socketno],trunc((unixtimefloat-startts)*1000000));
\r
153 dnsserverids[socketno] := -1;
\r
155 { writeln('received reply');}
\r
157 asyncprocess(socketno);
\r
158 //writeln('processed it');
\r
160 //writeln('ignored it because request is done');
\r
164 function tdnsasync.sendquery(socketno:integer;const packet:tdnspacket;len:integer):boolean;
\r
166 destination : tbinip;
\r
167 inaddr : tinetsockaddrv;
\r
168 trytolisten:integer;
\r
170 { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
\r
171 //writeln('trying to send query on socket number ',socketno);
\r
173 if len = 0 then exit; {no packet}
\r
174 if sockets[socketno].state <> wsconnected then begin
\r
175 startts := unixtimefloat;
\r
176 if port = '' then port := '53';
\r
177 sockets[socketno].Proto := 'udp';
\r
178 sockets[socketno].ondataavailable := receivehandler;
\r
180 {we are going to bind on a random local port for the DNS request, against the kaminsky attack
\r
181 there is a small chance that we're trying to bind on an already used port, so retry a few times}
\r
182 for trytolisten := 3 downto 0 do begin
\r
184 sockets[socketno].port := inttostr(1024 + randominteger(65536 - 1024));
\r
185 sockets[socketno].listen;
\r
187 {writeln('failed to listen ',sockets[socketno].localport,' ',trytolisten);}
\r
188 if (trytolisten = 0) then begin
\r
196 if addr <> '' then begin
\r
197 dnsserverids[socketno] := -1;
\r
198 destination := ipstrtobinf(addr);
\r
200 destination := getcurrentsystemnameserverbin(dnsserverids[socketno]);
\r
202 destinations[socketno] := destination;
\r
204 {$ifdef ipv6}{$ifdef mswindows}
\r
205 if destinations[socketno].family = AF_INET6 then if (requestaf = useaf_default) then requestaf := useaf_preferv6;
\r
208 makeinaddrv(destinations[socketno],port,inaddr);
\r
209 sockets[socketno].sendto(inaddr,sizeof(inaddr), @packet,len);
\r
215 procedure tdnsasync.asyncprocess(socketno:integer);
\r
217 state_process(states[socketno]);
\r
218 case states[socketno].resultaction of
\r
219 action_ignore: begin {do nothing} end;
\r
222 if (numsockused = 1) or (states[socketno xor 1].resultaction=action_done) then
\r
223 //if using two sockets we need to wait until both sockets are in the done
\r
224 //state before firing the event
\r
227 fresultlist := biniplist_new;
\r
228 if (numsockused = 1) then begin
\r
229 //writeln('processing for one state');
\r
230 biniplist_addlist(fresultlist,states[0].resultlist);
\r
232 end else if (requestaf = useaf_preferv6) then begin
\r
233 //writeln('processing for two states, ipv6 preference');
\r
234 //writeln('merging lists '+biniplist_tostr(states[1].resultlist)+' and '+biniplist_tostr(states[0].resultlist));
\r
235 biniplist_addlist(fresultlist,states[1].resultlist);
\r
236 biniplist_addlist(fresultlist,states[0].resultlist);
\r
238 //writeln('processing for two states, ipv4 preference');
\r
239 biniplist_addlist(fresultlist,states[0].resultlist);
\r
240 biniplist_addlist(fresultlist,states[1].resultlist);
\r
243 //writeln(biniplist_tostr(fresultlist));
\r
244 onrequestdone(self,0);
\r
247 action_sendquery:begin
\r
248 sendquery(socketno,states[socketno].sendpacket,states[socketno].sendpacketlen);
\r
253 procedure tdnsasync.forwardlookup;
\r
258 ipstrtobin(name,bip);
\r
260 if bip.family <> 0 then begin
\r
261 // it was an IP address
\r
262 fresultlist := biniplist_new;
\r
263 biniplist_add(fresultlist,bip);
\r
264 onrequestdone(self,0);
\r
268 if overrideaf = useaf_default then begin
\r
270 {$ifdef mswindows}if not (usewindns and (addr = '')) then{$endif}
\r
273 requestaf := useaf;
\r
275 requestaf := overrideaf;
\r
278 {$ifdef winasyncdns}
\r
279 if usewindns and (addr = '') then begin
\r
280 dwas := tdnswinasync.create;
\r
281 dwas.onrequestdone := winrequestdone;
\r
283 dwas.forwardlookup(name);
\r
290 fresultlist := biniplist_new;
\r
291 if (requestaf <> useaf_v6) then begin
\r
292 setstate_forward(name,states[numsockused],af_inet);
\r
297 if (requestaf <> useaf_v4) then begin
\r
298 setstate_forward(name,states[numsockused],af_inet6);
\r
303 for i := 0 to numsockused-1 do begin
\r
308 procedure tdnsasync.reverselookup;
\r
310 {$ifdef winasyncdns}
\r
311 if usewindns and (addr = '') then begin
\r
312 dwas := tdnswinasync.create;
\r
313 dwas.onrequestdone := winrequestdone;
\r
314 dwas.reverselookup(binip);
\r
319 setstate_reverse(binip,states[0]);
\r
324 procedure tdnsasync.customlookup;
\r
326 setstate_custom(name,querytype,states[0]);
\r
331 function tdnsasync.dnsresult;
\r
333 if states[0].resultstr <> '' then result := states[0].resultstr else begin
\r
334 result := ipbintostr(biniplist_get(fresultlist,0));
\r
338 procedure tdnsasync.dnsresultbin(var binip:tbinip);
\r
340 binip := biniplist_get(fresultlist,0);
\r
343 procedure tdnsasync.cancel;
\r
345 socketno : integer;
\r
347 {$ifdef winasyncdns}
\r
348 if assigned(dwas) then begin
\r
354 for socketno := 0 to numsock-1 do begin
\r
355 reportlag(dnsserverids[socketno],-1);
\r
356 dnsserverids[socketno] := -1;
\r
358 sockets[socketno].close;
\r
362 for socketno := 0 to numsock-1 do begin
\r
363 setstate_failure(states[socketno]);
\r
366 fresultlist := biniplist_new;
\r
367 onrequestdone(self,0);
\r
370 {$ifdef winasyncdns}
\r
371 procedure tdnsasync.winrequestdone(sender:tobject;error:word);
\r
374 if dwas.reverse then begin
\r
375 states[0].resultstr := dwas.name;
\r
379 if (requestaf = useaf_preferv4) then begin
\r
380 {prefer mode: sort the IP's}
\r
381 fresultlist := biniplist_new;
\r
382 addipsoffamily(fresultlist,dwas.iplist,af_inet);
\r
383 addipsoffamily(fresultlist,dwas.iplist,af_inet6);
\r
385 end else if (requestaf = useaf_preferv6) then begin
\r
386 {prefer mode: sort the IP's}
\r
387 fresultlist := biniplist_new;
\r
388 addipsoffamily(fresultlist,dwas.iplist,af_inet6);
\r
389 addipsoffamily(fresultlist,dwas.iplist,af_inet);
\r
394 fresultlist := dwas.iplist;
\r
399 onrequestdone(self,error);
\r