https://git.reactos.org/?p=reactos.git;a=commitdiff;h=2047cf361327fe1efc46ef58e2ac0e8595d7d468

commit 2047cf361327fe1efc46ef58e2ac0e8595d7d468
Author:     Eric Kohl <[email protected]>
AuthorDate: Mon May 24 15:18:58 2021 +0200
Commit:     Eric Kohl <[email protected]>
CommitDate: Mon May 24 15:18:58 2021 +0200

    [DHCPCSRV] Send an ARP request to check if an automatic private address is 
available
---
 base/services/dhcpcsvc/dhcp/dhclient.c | 48 +++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/base/services/dhcpcsvc/dhcp/dhclient.c 
b/base/services/dhcpcsvc/dhcp/dhclient.c
index 81fcd9c5000..f97a7ca83df 100644
--- a/base/services/dhcpcsvc/dhcp/dhclient.c
+++ b/base/services/dhcpcsvc/dhcp/dhclient.c
@@ -1071,26 +1071,50 @@ state_panic(void *ipp)
        struct interface_info *ip = ipp;
        uint16_t address_low;
        int i;
-        PDHCP_ADAPTER Adapter = AdapterFindInfo(ip);
+    IPAddr IpAddress;
+    ULONG Buffer[20];
+    ULONG BufferSize;
+    DWORD ret;
+    PDHCP_ADAPTER Adapter = AdapterFindInfo(ip);
 
        note("No DHCPOFFERS received.");
 
-        if (!Adapter->NteContext)
-        {
-            /* Generate an automatic private address */
-            DbgPrint("DHCPCSVC: Failed to receive a response from a DHCP 
server. An automatic private address will be assigned.\n");
+    if (!Adapter->NteContext)
+    {
+        /* Generate an automatic private address */
+        DbgPrint("DHCPCSVC: Failed to receive a response from a DHCP server. 
An automatic private address will be assigned.\n");
+
+        /* FIXME: The address generation code sucks */
+        srand(0);
 
-            /* FIXME: The address generation code sucks */
-            srand(0);
+        for (;;)
+        {
             address_low = rand();
             for (i = 0; i < ip->hw_address.hlen; i++)
                 address_low += ip->hw_address.haddr[i];
-            AddIPAddress(htonl(0xA9FE0000 | address_low), //169.254.X.X
-                         htonl(0xFFFF0000), //255.255.0.0
-                         Adapter->IfMib.dwIndex,
-                         &Adapter->NteContext,
-                         &Adapter->NteInstance);
+
+            IpAddress = htonl(0xA9FE0000 | address_low);  // 169.254.X.X
+
+            /* Send an ARP request to check if the IP address is already in 
use */
+            BufferSize = sizeof(Buffer);
+            ret = SendARP(IpAddress,
+                          IpAddress,
+                          Buffer,
+                          &BufferSize);
+            DH_DbgPrint(MID_TRACE,("DHCPCSVC: SendARP returned %lu\n", ret));
+            if (ret != 0)
+            {
+                /* The IP address is not in use */
+                DH_DbgPrint(MID_TRACE,("DHCPCSVC: Using automatic private 
address\n"));
+                AddIPAddress(IpAddress,
+                             htonl(0xFFFF0000), // 255.255.0.0
+                             Adapter->IfMib.dwIndex,
+                             &Adapter->NteContext,
+                             &Adapter->NteInstance);
+                return;
+            }
         }
+    }
 }
 
 void

Reply via email to