From 2579cad7a973072619a7fc17ff6af9cc75264972 Mon Sep 17 00:00:00 2001
From: beware <beware@bircd.org>
Date: Fri, 24 Feb 2012 02:57:51 +0000
Subject: [PATCH] dnssync lookup retry to listen in case port is in use

git-svn-id: file:///svnroot/lcore/trunk@113 b1de8a11-f9be-4011-bde0-cc7ace90066a
---
 dnssync.pas | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/dnssync.pas b/dnssync.pas
index 60af580..79ace01 100644
--- a/dnssync.pas
+++ b/dnssync.pas
@@ -132,7 +132,8 @@ procedure setupsocket;
 var
   inAddrtemp : TInetSockAddrV;
   biniptemp:tbinip;
-  a:integer;
+  a,retrycount:integer;
+  bindresult:boolean;
 begin
   biniptemp := getcurrentsystemnameserverbin(id);
   //must get the DNS server here so we know to init v4 or v6
@@ -141,11 +142,16 @@ begin
 
 
   for a := 0 to numsockused-1 do begin
-    makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
+    retrycount := 5;
+    repeat
+      makeinaddrv(biniptemp,inttostr( 1024 + randominteger(65536 - 1024) ),inaddrtemp);
 
-    fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
+      fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
+      bindresult := {$ifdef win32}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
+    If (not bindresult) Then begin
       {$ifdef win32}
         raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
       {$else}
-- 
2.30.2