X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/eaa75975b23ce60360526c08628f2b0651c95167..560d0547386a23a8fb79d1919d60dfdd04b49f62:/dnsasync.pas diff --git a/dnsasync.pas b/dnsasync.pas index b6e8941..d387059 100644 --- a/dnsasync.pas +++ b/dnsasync.pas @@ -7,18 +7,20 @@ //not seem to have any form of retry code. unit dnsasync; - +{$ifdef fpc} + {$mode delphi} +{$endif} interface +{$include lcoreconfig.inc} + uses - {$ifdef win32} + {$ifdef winasyncdns} dnswin, {$endif} lsocket,lcore, classes,binipstuff,dnscore,btime,lcorernd; -{$include lcoreconfig.inc} - const numsock=1{$ifdef ipv6}+1{$endif}; @@ -39,7 +41,7 @@ type dnsserverids : array[0..numsock-1] of integer; startts:double; - {$ifdef win32} + {$ifdef winasyncdns} dwas : tdnswinasync; {$endif} @@ -49,7 +51,7 @@ type procedure asyncprocess(socketno:integer); procedure receivehandler(sender:tobject;error:word); function sendquery(socketno:integer;const packet:tdnspacket;len:integer):boolean; - {$ifdef win32} + {$ifdef winasyncdns} procedure winrequestdone(sender:tobject;error:word); {$endif} @@ -57,7 +59,7 @@ type onrequestdone:tsocketevent; //addr and port allow the application to specify a dns server specifically - //for this dnsasync object. This is not a reccomended mode of operation + //for this dnsasync object. This is not a recommended mode of operation //because it limits the app to one dns server but is kept for compatibility //and special uses. addr,port:ansistring; @@ -69,7 +71,7 @@ type procedure dnsresultbin(var binip:tbinip); //get result of dnslookup as a tbinip property dnsresultlist : tbiniplist read fresultlist; procedure forwardlookup(const name:ansistring); //start forward lookup, - //preffering ipv4 + //preferring ipv4 procedure reverselookup(const binip:tbinip); //start reverse lookup procedure customlookup(const name:ansistring;querytype:integer); //start custom type lookup @@ -78,10 +80,21 @@ type end; +//function that the app can use to know whether the builtin or system resolver is being used +function willusebuiltindns_async:boolean; + implementation uses sysutils; + +function willusebuiltindns_async:boolean; +begin + result := true; + {$ifdef winasyncdns}if usewindns and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then result := false;{$endif} +end; + + constructor tdnsasync.create; begin inherited create(aowner); @@ -109,6 +122,14 @@ begin setstate_request_init('',states[socketno]); end; end; + + {$ifdef winasyncdns} + if assigned(dwas) then begin + dwas.release; + dwas := nil; + end; + {$endif} + inherited destroy; end; @@ -139,7 +160,7 @@ begin if states[socketno].resultaction <> action_done then begin //we ignore packets that come after we are done if dnsserverids[socketno] >= 0 then begin - reportlag(dnsserverids[socketno],trunc((unixtimefloat-startts)*1000000)); + reportlag(dnsserverids[socketno],trunc((wintimefloat-startts)*1000000)); dnsserverids[socketno] := -1; end; { writeln('received reply');} @@ -162,7 +183,7 @@ begin result := false; if len = 0 then exit; {no packet} if sockets[socketno].state <> wsconnected then begin - startts := unixtimefloat; + startts := wintimefloat; if port = '' then port := '53'; sockets[socketno].Proto := 'udp'; sockets[socketno].ondataavailable := receivehandler; @@ -191,7 +212,7 @@ begin end; destinations[socketno] := destination; - {$ifdef ipv6}{$ifdef win32} + {$ifdef ipv6}{$ifdef mswindows} if destinations[socketno].family = AF_INET6 then if (requestaf = useaf_default) then requestaf := useaf_preferv6; {$endif}{$endif} @@ -244,6 +265,7 @@ procedure tdnsasync.forwardlookup; var bip : tbinip; i : integer; + willusewindns:boolean; begin ipstrtobin(name,bip); @@ -255,18 +277,22 @@ begin exit; end; + willusewindns := false; + {$ifdef winasyncdns} + if usewindns and (addr = '') and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then willusewindns := true; + {$endif} + if overrideaf = useaf_default then begin {$ifdef ipv6} - {$ifdef win32}if not (usewindns and (addr = '')) then{$endif} - initpreferredmode; + if not willusewindns then initpreferredmode; {$endif} requestaf := useaf; end else begin requestaf := overrideaf; end; - {$ifdef win32} - if usewindns and (addr = '') then begin + {$ifdef winasyncdns} + if willusewindns then begin dwas := tdnswinasync.create; dwas.onrequestdone := winrequestdone; @@ -276,6 +302,15 @@ begin end; {$endif} + if (((overridednsserver = '') and (addr = '')) or hostsfile_alsocustomserver) and (not hostsfile_disabled) then begin + //try a hosts file lookup + fresultlist := hostsfile_forwardlookuplist(name); + if (biniplist_getcount(fresultlist) > 0) then begin + onrequestdone(self,0); + exit; + end; + end; + numsockused := 0; fresultlist := biniplist_new; if (requestaf <> useaf_v6) then begin @@ -297,8 +332,8 @@ end; procedure tdnsasync.reverselookup; begin - {$ifdef win32} - if usewindns and (addr = '') then begin + {$ifdef winasyncdns} + if usewindns and (addr = '') and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then begin dwas := tdnswinasync.create; dwas.onrequestdone := winrequestdone; dwas.reverselookup(binip); @@ -306,6 +341,15 @@ begin end; {$endif} + if (((overridednsserver = '') and (addr = '')) or hostsfile_alsocustomserver) and (not hostsfile_disabled) then begin + //try a hosts file lookup + states[0].resultstr := hostsfile_reverselookup(binip); + if (states[0].resultstr <> '') then begin + onrequestdone(self,0); + exit; + end; + end; + setstate_reverse(binip,states[0]); numsockused := 1; asyncprocess(0); @@ -334,7 +378,7 @@ procedure tdnsasync.cancel; var socketno : integer; begin - {$ifdef win32} + {$ifdef winasyncdns} if assigned(dwas) then begin dwas.release; dwas := nil; @@ -357,7 +401,7 @@ begin onrequestdone(self,0); end; -{$ifdef win32} +{$ifdef winasyncdns} procedure tdnsasync.winrequestdone(sender:tobject;error:word); begin