You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dnsasync.pas 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. { Copyright (C) 2005 Bas Steendijk and Peter Green
  2. For conditions of distribution and use, see copyright notice in zlib_license.txt
  3. which is included in the package
  4. ----------------------------------------------------------------------------- }
  5. //FIXME: this code only ever seems to use one dns server for a request and does
  6. //not seem to have any form of retry code.
  7. unit dnsasync;
  8. interface
  9. uses
  10. {$ifdef win32}
  11. dnswin,
  12. {$endif}
  13. lsocket,lcore,
  14. classes,binipstuff,dnscore,btime,lcorernd;
  15. {$include lcoreconfig.inc}
  16. const
  17. numsock=1{$ifdef ipv6}+1{$endif};
  18. type
  19. //after completion or cancelation a dnswinasync may be reused
  20. tdnsasync=class(tcomponent)
  21. private
  22. //made a load of stuff private that does not appear to be part of the main
  23. //public interface. If you make any of it public again please consider the
  24. //consequences when using windows dns. --plugwash.
  25. sockets: array[0..numsock-1] of tlsocket;
  26. states: array[0..numsock-1] of tdnsstate;
  27. destinations: array[0..numsock-1] of tbinip;
  28. dnsserverids : array[0..numsock-1] of integer;
  29. startts:double;
  30. {$ifdef win32}
  31. dwas : tdnswinasync;
  32. {$endif}
  33. numsockused : integer;
  34. fresultlist : tbiniplist;
  35. requestaf : integer;
  36. procedure asyncprocess(socketno:integer);
  37. procedure receivehandler(sender:tobject;error:word);
  38. function sendquery(socketno:integer;const packet:tdnspacket;len:integer):boolean;
  39. {$ifdef win32}
  40. procedure winrequestdone(sender:tobject;error:word);
  41. {$endif}
  42. public
  43. onrequestdone:tsocketevent;
  44. //addr and port allow the application to specify a dns server specifically
  45. //for this dnsasync object. This is not a reccomended mode of operation
  46. //because it limits the app to one dns server but is kept for compatibility
  47. //and special uses.
  48. addr,port:string;
  49. overrideaf : integer;
  50. procedure cancel;//cancel an outstanding dns request
  51. function dnsresult:string; //get result of dnslookup as a string
  52. procedure dnsresultbin(var binip:tbinip); //get result of dnslookup as a tbinip
  53. property dnsresultlist : tbiniplist read fresultlist;
  54. procedure forwardlookup(const name:string); //start forward lookup,
  55. //preffering ipv4
  56. procedure reverselookup(const binip:tbinip); //start reverse lookup
  57. procedure customlookup(const name:string;querytype:integer); //start custom type lookup
  58. constructor create(aowner:tcomponent); override;
  59. destructor destroy; override;
  60. end;
  61. implementation
  62. uses sysutils;
  63. constructor tdnsasync.create;
  64. begin
  65. inherited create(aowner);
  66. dnsserverids[0] := -1;
  67. sockets[0] := twsocket.create(self);
  68. sockets[0].tag := 0;
  69. {$ifdef ipv6}
  70. dnsserverids[1] := -1;
  71. sockets[1] := twsocket.Create(self);
  72. sockets[1].tag := 1;
  73. {$endif}
  74. end;
  75. destructor tdnsasync.destroy;
  76. var
  77. socketno : integer;
  78. begin
  79. for socketno := 0 to numsock -1 do begin
  80. if dnsserverids[socketno] >= 0 then begin
  81. reportlag(dnsserverids[socketno],-1);
  82. dnsserverids[socketno] := -1;
  83. end;
  84. sockets[socketno].release;
  85. setstate_request_init('',states[socketno]);
  86. end;
  87. inherited destroy;
  88. end;
  89. procedure tdnsasync.receivehandler(sender:tobject;error:word);
  90. var
  91. socketno : integer;
  92. Src : TInetSockAddrV;
  93. SrcLen : Integer;
  94. fromip:tbinip;
  95. fromport:string;
  96. begin
  97. socketno := tlsocket(sender).tag;
  98. //writeln('got a reply on socket number ',socketno);
  99. fillchar(states[socketno].recvpacket,sizeof(states[socketno].recvpacket),0);
  100. SrcLen := SizeOf(Src);
  101. states[socketno].recvpacketlen := twsocket(sender).ReceiveFrom(@(states[socketno].recvpacket), SizeOf(states[socketno].recvpacket), Src, SrcLen);
  102. fromip := inaddrvtobinip(Src);
  103. fromport := inttostr(htons(src.InAddr.port));
  104. if ((not comparebinip(fromip,destinations[socketno])) or (fromport <> port)) then begin
  105. // writeln('dnsasync received from wrong IP:port ',ipbintostr(fromip),'#',fromport,', expected ',ipbintostr(destinations[socketno]),'#',port);
  106. exit;
  107. end;
  108. states[socketno].parsepacket := true;
  109. if states[socketno].resultaction <> action_done then begin
  110. //we ignore packets that come after we are done
  111. if dnsserverids[socketno] >= 0 then begin
  112. reportlag(dnsserverids[socketno],trunc((unixtimefloat-startts)*1000000));
  113. dnsserverids[socketno] := -1;
  114. end;
  115. { writeln('received reply');}
  116. asyncprocess(socketno);
  117. //writeln('processed it');
  118. end else begin
  119. //writeln('ignored it because request is done');
  120. end;
  121. end;
  122. function tdnsasync.sendquery(socketno:integer;const packet:tdnspacket;len:integer):boolean;
  123. var
  124. destination : string;
  125. inaddr : tinetsockaddrv;
  126. trytolisten:integer;
  127. begin
  128. { writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
  129. //writeln('trying to send query on socket number ',socketno);
  130. result := false;
  131. if len = 0 then exit; {no packet}
  132. if sockets[socketno].state <> wsconnected then begin
  133. startts := unixtimefloat;
  134. if port = '' then port := '53';
  135. sockets[socketno].Proto := 'udp';
  136. sockets[socketno].ondataavailable := receivehandler;
  137. {we are going to bind on a random local port for the DNS request, against the kaminsky attack
  138. there is a small chance that we're trying to bind on an already used port, so retry a few times}
  139. for trytolisten := 3 downto 0 do begin
  140. try
  141. sockets[socketno].port := inttostr(1024 + randominteger(65536 - 1024));
  142. sockets[socketno].listen;
  143. except
  144. {writeln('failed to listen ',sockets[socketno].localport,' ',trytolisten);}
  145. if (trytolisten = 0) then begin
  146. result := false;
  147. exit;
  148. end;
  149. end;
  150. end;
  151. end;
  152. if addr <> '' then begin
  153. dnsserverids[socketno] := -1;
  154. destination := addr
  155. end else begin
  156. destination := getcurrentsystemnameserver(dnsserverids[socketno]);
  157. end;
  158. destinations[socketno] := ipstrtobinf(destination);
  159. {$ifdef ipv6}{$ifdef win32}
  160. if destinations[socketno].family = AF_INET6 then if (requestaf = useaf_default) then requestaf := useaf_preferv6;
  161. {$endif}{$endif}
  162. makeinaddrv(destinations[socketno],port,inaddr);
  163. sockets[socketno].sendto(inaddr,sizeof(inaddr), @packet,len);
  164. result := true;
  165. end;
  166. procedure tdnsasync.asyncprocess(socketno:integer);
  167. begin
  168. state_process(states[socketno]);
  169. case states[socketno].resultaction of
  170. action_ignore: begin {do nothing} end;
  171. action_done: begin
  172. {$ifdef ipv6}
  173. if (numsockused = 1) or (states[socketno xor 1].resultaction=action_done) then
  174. //if using two sockets we need to wait until both sockets are in the done
  175. //state before firing the event
  176. {$endif}
  177. begin
  178. fresultlist := biniplist_new;
  179. if (numsockused = 1) then begin
  180. //writeln('processing for one state');
  181. biniplist_addlist(fresultlist,states[0].resultlist);
  182. {$ifdef ipv6}
  183. end else if (requestaf = useaf_preferv6) then begin
  184. //writeln('processing for two states, ipv6 preference');
  185. //writeln('merging lists '+biniplist_tostr(states[1].resultlist)+' and '+biniplist_tostr(states[0].resultlist));
  186. biniplist_addlist(fresultlist,states[1].resultlist);
  187. biniplist_addlist(fresultlist,states[0].resultlist);
  188. end else begin
  189. //writeln('processing for two states, ipv4 preference');
  190. biniplist_addlist(fresultlist,states[0].resultlist);
  191. biniplist_addlist(fresultlist,states[1].resultlist);
  192. {$endif}
  193. end;
  194. //writeln(biniplist_tostr(fresultlist));
  195. onrequestdone(self,0);
  196. end;
  197. end;
  198. action_sendquery:begin
  199. sendquery(socketno,states[socketno].sendpacket,states[socketno].sendpacketlen);
  200. end;
  201. end;
  202. end;
  203. procedure tdnsasync.forwardlookup;
  204. var
  205. bip : tbinip;
  206. i : integer;
  207. begin
  208. ipstrtobin(name,bip);
  209. if bip.family <> 0 then begin
  210. // it was an IP address
  211. fresultlist := biniplist_new;
  212. biniplist_add(fresultlist,bip);
  213. onrequestdone(self,0);
  214. exit;
  215. end;
  216. if (overridednsserver <> '') and (addr = '') then addr := overridednsserver;
  217. if overrideaf = useaf_default then begin
  218. {$ifdef ipv6}
  219. {$ifdef win32}if not (usewindns and (addr = '')) then{$endif}
  220. initpreferredmode;
  221. {$endif}
  222. requestaf := useaf;
  223. end else begin
  224. requestaf := overrideaf;
  225. end;
  226. {$ifdef win32}
  227. if usewindns and (addr = '') then begin
  228. dwas := tdnswinasync.create;
  229. dwas.onrequestdone := winrequestdone;
  230. dwas.forwardlookup(name);
  231. exit;
  232. end;
  233. {$endif}
  234. numsockused := 0;
  235. fresultlist := biniplist_new;
  236. if (requestaf <> useaf_v6) then begin
  237. setstate_forward(name,states[numsockused],af_inet);
  238. inc(numsockused);
  239. end;
  240. {$ifdef ipv6}
  241. if (requestaf <> useaf_v4) then begin
  242. setstate_forward(name,states[numsockused],af_inet6);
  243. inc(numsockused);
  244. end;
  245. {$endif}
  246. for i := 0 to numsockused-1 do begin
  247. asyncprocess(i);
  248. end;
  249. end;
  250. procedure tdnsasync.reverselookup;
  251. begin
  252. if (overridednsserver <> '') and (addr = '') then addr := overridednsserver;
  253. {$ifdef win32}
  254. if usewindns and (addr = '') then begin
  255. dwas := tdnswinasync.create;
  256. dwas.onrequestdone := winrequestdone;
  257. dwas.reverselookup(binip);
  258. exit;
  259. end;
  260. {$endif}
  261. setstate_reverse(binip,states[0]);
  262. numsockused := 1;
  263. asyncprocess(0);
  264. end;
  265. procedure tdnsasync.customlookup;
  266. begin
  267. if (overridednsserver <> '') and (addr = '') then addr := overridednsserver;
  268. setstate_custom(name,querytype,states[0]);
  269. numsockused := 1;
  270. asyncprocess(0);
  271. end;
  272. function tdnsasync.dnsresult;
  273. begin
  274. if states[0].resultstr <> '' then result := states[0].resultstr else begin
  275. result := ipbintostr(biniplist_get(fresultlist,0));
  276. end;
  277. end;
  278. procedure tdnsasync.dnsresultbin(var binip:tbinip);
  279. begin
  280. binip := biniplist_get(fresultlist,0);
  281. end;
  282. procedure tdnsasync.cancel;
  283. var
  284. socketno : integer;
  285. begin
  286. {$ifdef win32}
  287. if assigned(dwas) then begin
  288. dwas.release;
  289. dwas := nil;
  290. end else
  291. {$endif}
  292. begin
  293. for socketno := 0 to numsock-1 do begin
  294. reportlag(dnsserverids[socketno],-1);
  295. dnsserverids[socketno] := -1;
  296. sockets[socketno].close;
  297. end;
  298. end;
  299. for socketno := 0 to numsock-1 do begin
  300. setstate_failure(states[socketno]);
  301. end;
  302. fresultlist := biniplist_new;
  303. onrequestdone(self,0);
  304. end;
  305. {$ifdef win32}
  306. procedure tdnsasync.winrequestdone(sender:tobject;error:word);
  307. begin
  308. if dwas.reverse then begin
  309. states[0].resultstr := dwas.name;
  310. end else begin
  311. {$ifdef ipv6}
  312. if (requestaf = useaf_preferv4) then begin
  313. {prefer mode: sort the IP's}
  314. fresultlist := biniplist_new;
  315. addipsoffamily(fresultlist,dwas.iplist,af_inet);
  316. addipsoffamily(fresultlist,dwas.iplist,af_inet6);
  317. end else if (requestaf = useaf_preferv6) then begin
  318. {prefer mode: sort the IP's}
  319. fresultlist := biniplist_new;
  320. addipsoffamily(fresultlist,dwas.iplist,af_inet6);
  321. addipsoffamily(fresultlist,dwas.iplist,af_inet);
  322. end else
  323. {$endif}
  324. begin
  325. fresultlist := dwas.iplist;
  326. end;
  327. end;
  328. dwas.release;
  329. onrequestdone(self,error);
  330. end;
  331. {$endif}
  332. end.