X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/f3321a0eafbeda0e8ebfb420974d391d7904ed44..3bf8ed9f9545a956985b6c3c0658893910e01f75:/dnssync.pas?ds=inline

diff --git a/dnssync.pas b/dnssync.pas
index d037a68..84caf9a 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
@@ -60,22 +60,23 @@ const
 
 implementation
 
-{$ifdef win32}
+{$ifdef mswindows}
   uses dnswin;
 {$endif}
 
 
-{$ifndef win32}
+{$ifndef mswindows}
 {$define syncdnscore}
 {$endif}
 
 {$i unixstuff.inc}
 
+type tdnsstatearr=array[0..numsock-1] of tdnsstate;
 
 {$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';
@@ -86,21 +87,18 @@ implementation
 
 
 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;
 
-
-type tdnsstatearr=array[0..numsock-1] of tdnsstate;
-
 procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer);
 var
   selectresult   : integer;
@@ -119,7 +117,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;
@@ -134,7 +132,8 @@ procedure setupsocket;
 var
   inAddrtemp : TInetSockAddrV;
   biniptemp:tbinip;
-  a:integer;
+  a,retrycount,porttemp:integer;
+  bindresult:boolean;
 begin
   biniptemp := getcurrentsystemnameserverbin(id);
   //must get the DNS server here so we know to init v4 or v6
@@ -143,12 +142,23 @@ begin
 
 
   for a := 0 to numsockused-1 do begin
-    makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
+    retrycount := 5;
+    repeat
+      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);
+      fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
+      bindresult := {$ifdef mswindows}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp));
+      dec(retrycount);
+    until (retrycount <= 0) or (bindresult);
 
-    If {$ifndef win32}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp)) Then begin
-      {$ifdef win32}
+    If (not bindresult) Then begin
+      {$ifdef mswindows}
         raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
       {$else}
         raise Exception.create('unable to bind '+inttostr(socketError));
@@ -176,7 +186,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}
 
@@ -246,7 +256,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);
@@ -317,7 +327,7 @@ begin
     exit; //it was an IP address, no need for dns
   end;
 
-  {$ifdef win32}
+  {$ifdef mswindows}
   if usewindns 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);
@@ -388,7 +398,7 @@ var
   numsockused:integer;
   state:tdnsstatearr;
 begin
-  {$ifdef win32}
+  {$ifdef mswindows}
     if usewindns then begin
       result := winreverselookup(ip,dummy);
       exit;
@@ -402,7 +412,7 @@ begin
   {$endif}
 end;
 
-{$ifdef win32}
+{$ifdef mswindows}
   var
     wsadata : twsadata;