X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/2579cad7a973072619a7fc17ff6af9cc75264972..560d0547386a23a8fb79d1919d60dfdd04b49f62:/dnssync.pas diff --git a/dnssync.pas b/dnssync.pas index 79ace01..c2b7456 100644 --- a/dnssync.pas +++ b/dnssync.pas @@ -13,7 +13,7 @@ interface uses dnscore, binipstuff, - {$ifdef win32} + {$ifdef mswindows} winsock, windows, {$else} @@ -31,9 +31,9 @@ interface //convert a name to an IP //will return v4 or v6 depending on what seems favorable, or manual preference setting -//on error the binip will have a family of 0 (other fiels are also currently +//on error the binip will have a family of 0 (other fields are also currently //zeroed out but may be used for further error information in future) -//timeout is in miliseconds, it is ignored when using windows dns +//timeout is in milliseconds, it is ignored when using windows dns function forwardlookup(name:ansistring;timeout:integer):tbinip; //convert a name to a list of all IP's returned @@ -46,6 +46,8 @@ function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist; //details as above function reverselookup(ip:tbinip;timeout:integer):ansistring; +//function that the app can use to know whether the builtin or system resolver is being used +function willusebuiltindns_sync:boolean; const @@ -60,12 +62,12 @@ const implementation -{$ifdef win32} +{$ifdef mswindows} uses dnswin; {$endif} -{$ifndef win32} +{$ifndef mswindows} {$define syncdnscore} {$endif} @@ -73,10 +75,28 @@ implementation type tdnsstatearr=array[0..numsock-1] of tdnsstate; + +function willusebuiltindns_sync:boolean; +begin + result := false; + {$ifdef mswindows} + if usewindns and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then begin + // + end else + {$endif} + begin + {$ifdef syncdnscore} + result := true; + {$endif} + end; +end; + + + {$ifdef syncdnscore} -{$ifdef win32} +{$ifdef mswindows} const winsocket = 'wsock32.dll'; function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto'; @@ -87,14 +107,14 @@ type tdnsstatearr=array[0..numsock-1] of tdnsstate; function getts:integer; -{$ifdef win32} +{$ifdef mswindows} begin result := GetTickCount and tsmask; {$else} var temp:ttimeval; begin - gettimeofday(temp); + gettimemonotonic(temp); result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask; {$endif} end; @@ -117,7 +137,7 @@ var a,b:integer; Src : TInetSockAddrV; - Srcx : {$ifdef win32}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src; + Srcx : {$ifdef mswindows}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src; SrcLen : Integer; fromip:tbinip; fromport:ansistring; @@ -132,7 +152,7 @@ procedure setupsocket; var inAddrtemp : TInetSockAddrV; biniptemp:tbinip; - a,retrycount:integer; + a,retrycount,porttemp:integer; bindresult:boolean; begin biniptemp := getcurrentsystemnameserverbin(id); @@ -144,15 +164,21 @@ begin for a := 0 to numsockused-1 do begin retrycount := 5; repeat - makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp); + if (retrycount <= 1) then begin + porttemp := 0; //for the last attempt let the OS decide + end else begin + porttemp := 1024 + randominteger(65536 - 1024); + end; + + makeinaddrv(biniptemp,inttostr( porttemp ),inaddrtemp); fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0); - bindresult := {$ifdef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)); + bindresult := {$ifdef mswindows}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)); dec(retrycount); until (retrycount <= 0) or (bindresult); If (not bindresult) Then begin - {$ifdef win32} + {$ifdef mswindows} raise Exception.create('unable to bind '+inttostr(WSAGetLastError)); {$else} raise Exception.create('unable to bind '+inttostr(socketError)); @@ -180,7 +206,7 @@ begin ip := getcurrentsystemnameserverbin(id); - {$ifdef ipv6}{$ifdef win32} + {$ifdef ipv6}{$ifdef mswindows} if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6; {$endif}{$endif} @@ -250,7 +276,7 @@ begin selecttimeout.tv_sec := 0; selecttimeout.tv_usec := retryafter; end; - //find the highest of the used fd's + //find the highest of the used fds b := 0; for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum]; selectresult := select(b+1,@fds,nil,nil,@selecttimeout); @@ -321,8 +347,8 @@ begin exit; //it was an IP address, no need for dns end; - {$ifdef win32} - if usewindns then begin + {$ifdef mswindows} + if usewindns and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then begin if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0; result := winforwardlookuplist(name,a,dummy); {$ifdef ipv6} @@ -347,6 +373,15 @@ begin {$ifdef syncdnscore} {$ifdef ipv6}initpreferredmode;{$endif} + if ((overridednsserver = '') or hostsfile_alsocustomserver) and (not hostsfile_disabled) then begin + //try a hosts file lookup + l := hostsfile_forwardlookuplist(name); + if (biniplist_getcount(l) > 0) then begin + result := l; + exit; + end; + end; + numsockused := 0; result := biniplist_new; @@ -374,7 +409,9 @@ begin biniplist_addlist(result,state[1].resultlist); {$endif} end; - {$endif} + {$else} + result := biniplist_new; + {$endif} end; end; @@ -392,21 +429,30 @@ var numsockused:integer; state:tdnsstatearr; begin - {$ifdef win32} - if usewindns then begin + {$ifdef mswindows} + if usewindns and (overridednsserver = '') and not (hostsfile_disabled or hostsfile_onlylocalhost) then begin result := winreverselookup(ip,dummy); exit; end; {$endif} {$ifdef syncdnscore} + + if ((overridednsserver = '') or hostsfile_alsocustomserver) and (not hostsfile_disabled) then begin + //try a hosts file lookup + result := hostsfile_reverselookup(ip); + if (result <> '') then exit; + end; + setstate_reverse(ip,state[0]); numsockused := 1; resolveloop(timeout,state,numsockused); result := state[0].resultstr; + {$else} + result := ''; {$endif} end; -{$ifdef win32} +{$ifdef mswindows} var wsadata : twsadata;