Sync with trunk (r48545)
[reactos.git] / dll / win32 / dhcpcsvc / dhcp / adapter.c
index b6801e5..890972f 100644 (file)
@@ -145,10 +145,8 @@ cleanup:
 }
 
 BOOL PrepareAdapterForService( PDHCP_ADAPTER Adapter ) {
-    HKEY AdapterKey = NULL;
-    PCHAR IPAddress = NULL, Netmask = NULL, DefaultGateway = NULL;
-    NTSTATUS Status = STATUS_SUCCESS;
-    DWORD Error = ERROR_SUCCESS;
+    HKEY AdapterKey;
+    DWORD Error = ERROR_SUCCESS, DhcpEnabled, Length = sizeof(DWORD);
 
     Adapter->DhclientState.config = &Adapter->DhclientConfig;
     strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
@@ -156,54 +154,32 @@ BOOL PrepareAdapterForService( PDHCP_ADAPTER Adapter ) {
 
     AdapterKey = FindAdapterKey( Adapter );
     if( AdapterKey )
-        IPAddress = RegReadString( AdapterKey, NULL, "IPAddress" );
+    {
+        Error = RegQueryValueEx(AdapterKey, "EnableDHCP", NULL, NULL, (LPBYTE)&DhcpEnabled, &Length);
 
-    if( IPAddress && strcmp( IPAddress, "0.0.0.0" ) ) {
-        /* Non-automatic case */
-        DH_DbgPrint
-            (MID_TRACE,("Adapter Name: [%s] (Bind Status %x) (static %s)\n",
-                        Adapter->DhclientInfo.name,
-                        Adapter->BindStatus,
-                        IPAddress));
+        if (Error != ERROR_SUCCESS || Length != sizeof(DWORD))
+            DhcpEnabled = 1;
 
-        Adapter->DhclientState.state = S_STATIC;
+        CloseHandle(AdapterKey);
+    }
+    else
+    {
+        /* DHCP enabled by default */
+        DhcpEnabled = 1;
+    }           
 
-        Netmask = RegReadString( AdapterKey, NULL, "Subnetmask" );
-
-        Status = AddIPAddress( inet_addr( IPAddress ),
-                               inet_addr( Netmask ? Netmask : "255.255.255.0" ),
-                               Adapter->IfMib.dwIndex,
-                               &Adapter->NteContext,
-                               &Adapter->NteInstance );
-
-        DefaultGateway = RegReadString( AdapterKey, NULL, "DefaultGateway" );
-
-        if( DefaultGateway ) {
-            Adapter->RouterMib.dwForwardDest = 0;
-            Adapter->RouterMib.dwForwardMask = 0;
-            Adapter->RouterMib.dwForwardMetric1 = 1;
-            Adapter->RouterMib.dwForwardIfIndex = Adapter->IfMib.dwIndex;
-            Adapter->RouterMib.dwForwardNextHop = inet_addr(DefaultGateway);
-            Error = CreateIpForwardEntry( &Adapter->RouterMib );
-            if( Error )
-                warning("Failed to set default gateway %s: %ld\n",
-                        DefaultGateway, Error);
-        }
+    if( !DhcpEnabled ) {
+        /* Non-automatic case */
+        DbgPrint("DHCPCSVC: Adapter Name: [%s] (static)\n", Adapter->DhclientInfo.name);
 
-        if( DefaultGateway ) free( DefaultGateway );
-        if( Netmask ) free( Netmask );
+        Adapter->DhclientState.state = S_STATIC;
     } else {
         /* Automatic case */
-        DH_DbgPrint
-            (MID_TRACE,("Adapter Name: [%s] (Bind Status %x) (dynamic)\n",
-                        Adapter->DhclientInfo.name,
-                        Adapter->BindStatus));
+        DbgPrint("DHCPCSVC: Adapter Name: [%s] (dynamic)\n", Adapter->DhclientInfo.name);
 
        Adapter->DhclientInfo.client->state = S_INIT;
     }
 
-    if( IPAddress ) free( IPAddress );
-
     return TRUE;
 }
 
@@ -233,7 +209,7 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
     PDHCP_ADAPTER Adapter = NULL;
     HANDLE AdapterStateChangedEvent = (HANDLE)Context;
     struct interface_info *ifi = NULL;
-    int i;
+    int i, AdapterCount = 0, Broadcast;
 
     /* FIXME: Kill this thread when the service is stopped */
 
@@ -248,7 +224,11 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
        }
 
        if( Error != NO_ERROR )
-           break;
+       {
+           /* HACK: We are waiting until TCP/IP starts */
+           Sleep(2000);
+           continue;
+       }
 
        DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
 
@@ -305,6 +285,15 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
                         socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
 
                     if (DhcpSocket != INVALID_SOCKET) {
+                                               
+                                               /* Allow broadcast on this socket */
+                                               Broadcast = 1;
+                                               setsockopt(DhcpSocket,
+                                                                  SOL_SOCKET,
+                                                                  SO_BROADCAST,
+                                                                  (const char *)&Broadcast,
+                                                                  sizeof(Broadcast));
+                                               
                         Adapter->ListenAddr.sin_family = AF_INET;
                         Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
                         Adapter->BindStatus =
@@ -346,7 +335,7 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
 
                     ApiLock();
                     InsertTailList( &AdapterList, &Adapter->ListEntry );
-                    DbgPrint("DHCPCSVC: Discovered new adapter [%s]\n", Adapter->DhclientInfo.name);
+                    AdapterCount++;
                     SetEvent(AdapterStateChangedEvent);
                     ApiUnlock();
                 } else { free( Adapter ); Adapter = 0; }
@@ -356,7 +345,17 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
                 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
                                        Table->table[i].dwIndex));
         }
-    } while ((Error = NotifyAddrChange(NULL, NULL)) == NO_ERROR);
+        Error = NotifyAddrChange(NULL, NULL);
+#if 0
+        if (Error != NO_ERROR)
+            break;
+#else
+        if (AdapterCount)
+            break;
+        else
+            Sleep(3000);
+#endif
+    } while (TRUE);
 
     DbgPrint("DHCPCSVC: Adapter discovery thread is terminating! (Error: %d)\n", Error);