[NETCFGX]
authorCameron Gutman <aicommander@gmail.com>
Fri, 23 Jul 2010 22:30:27 +0000 (22:30 +0000)
committerCameron Gutman <aicommander@gmail.com>
Fri, 23 Jul 2010 22:30:27 +0000 (22:30 +0000)
- Create the DhcpEnabled value in the registry
[TCPIP]
- Apply the static IP address configuration data
[DHCPCSVC]
- Remove the static IP address code

svn path=/trunk/; revision=48215

reactos/dll/win32/dhcpcsvc/dhcp/adapter.c
reactos/dll/win32/netcfgx/netcfgx.c
reactos/drivers/network/tcpip/datalink/lan.c

index 075e080..ad18dfa 100644 (file)
@@ -102,7 +102,8 @@ HKEY FindAdapterKey( PDHCP_ADAPTER Adapter ) {
         "SYSTEM\\CurrentControlSet\\Control\\Class\\"
         "{4D36E972-E325-11CE-BFC1-08002BE10318}";
     PCHAR TargetKeyNameStart =
-        "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
+        "SYSTEM\\CurrentControlSet\\Services\\";
+    PCHAR TargetKeyNameEnd = "\\Parameters\\Tcpip";
     PCHAR TargetKeyName = NULL;
     PCHAR *EnumKeysLinkage = GetSubkeyNames( EnumKeyName, "\\Linkage" );
     PCHAR *EnumKeysTop     = GetSubkeyNames( EnumKeyName, "" );
@@ -124,10 +125,10 @@ HKEY FindAdapterKey( PDHCP_ADAPTER Adapter ) {
             !strcmp( RootDevice, Adapter->DhclientInfo.name ) ) {
             TargetKeyName =
                 (CHAR*) malloc( strlen( TargetKeyNameStart ) +
-                        strlen( RootDevice ) + 1);
+                        strlen( RootDevice ) + strlen( TargetKeyNameEnd ) + 1);
             if( !TargetKeyName ) goto cleanup;
-            sprintf( TargetKeyName, "%s%s",
-                     TargetKeyNameStart, RootDevice );
+            sprintf( TargetKeyName, "%s%s%s",
+                     TargetKeyNameStart, RootDevice, TargetKeyNameEnd );
             Error = RegCreateKeyExA( HKEY_LOCAL_MACHINE, TargetKeyName, 0, NULL, 0, KEY_READ, NULL, &OutKey, NULL );
             break;
         } else {
@@ -145,10 +146,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;
 
     Adapter->DhclientState.config = &Adapter->DhclientConfig;
     strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
@@ -156,54 +155,32 @@ BOOL PrepareAdapterForService( PDHCP_ADAPTER Adapter ) {
 
     AdapterKey = FindAdapterKey( Adapter );
     if( AdapterKey )
-        IPAddress = RegReadString( AdapterKey, NULL, "IPAddress" );
+    {
+        Error = RegQueryValueEx(AdapterKey, "DhcpEnabled", NULL, NULL, (LPBYTE)&DhcpEnabled, NULL);
 
-    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)
+            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;
 }
 
@@ -350,7 +327,6 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
 
                     ApiLock();
                     InsertTailList( &AdapterList, &Adapter->ListEntry );
-                    DbgPrint("DHCPCSVC: Discovered new adapter [%s]\n", Adapter->DhclientInfo.name);
                     AdapterCount++;
                     SetEvent(AdapterStateChangedEvent);
                     ApiUnlock();
index 175e208..d0df451 100644 (file)
@@ -336,7 +336,7 @@ InstallNetDevice(
        HKEY hNetworkKey = NULL;
        HKEY hLinkageKey = NULL;
        HKEY hConnectionKey = NULL;
-       DWORD dwShowIcon, dwLength;
+       DWORD dwShowIcon, dwLength, dwValue;
 
        /* Get Instance ID */
        if (SetupDiGetDeviceInstanceIdW(DeviceInfoSet, DeviceInfoData, NULL, 0, &dwLength))
@@ -422,6 +422,13 @@ InstallNetDevice(
                DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc);
                goto cleanup;
        }
+        dwValue = 1;
+       rc = RegSetValueExW(hKey, L"DhcpEnabled", 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(DWORD));
+       if (rc != ERROR_SUCCESS)
+       {
+               DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc);
+               goto cleanup;
+       }
        RegCloseKey(hKey);
        hKey = NULL;
 
index 890330e..165ec6b 100644 (file)
@@ -946,9 +946,17 @@ BOOLEAN BindAdapter(
     PIP_INTERFACE IF;
     NDIS_STATUS NdisStatus;
     LLIP_BIND_INFO BindInfo;
-    IP_ADDRESS DefaultMask;
-    ULONG Lookahead = LOOKAHEAD_SIZE;
+    IP_ADDRESS DefaultMask, Router;
+    ULONG Lookahead = LOOKAHEAD_SIZE, Unused;
     NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    HANDLE ParameterHandle;
+    PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo;
+    UNICODE_STRING IPAddress = RTL_CONSTANT_STRING(L"IPAddress");
+    UNICODE_STRING Netmask = RTL_CONSTANT_STRING(L"SubnetMask");
+    UNICODE_STRING Gateway = RTL_CONSTANT_STRING(L"DefaultGateway");
+    UNICODE_STRING RegistryDataU;
+    ANSI_STRING RegistryDataA;
 
     TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
 
@@ -1001,10 +1009,105 @@ BOOLEAN BindAdapter(
     TI_DbgPrint(DEBUG_DATALINK,("Adapter Description: %wZ\n",
                 &IF->Description));
 
+    DbgPrint("Opening %wZ\n", RegistryPath);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               RegistryPath,
+                               OBJ_CASE_INSENSITIVE,
+                               0,
+                               NULL);
+
     AddrInitIPv4(&DefaultMask, 0);
 
-    IF->Unicast = DefaultMask;
-    IF->Netmask = DefaultMask;
+    Status = ZwOpenKey(&ParameterHandle, KEY_READ, &ObjectAttributes);
+    if (!NT_SUCCESS(Status))
+    {
+        IF->Unicast = DefaultMask;
+        IF->Netmask = DefaultMask;
+    }
+    else
+    {
+        KeyValueInfo = ExAllocatePool(PagedPool, sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR));
+        if (!KeyValueInfo)
+        {
+            ZwClose(ParameterHandle);
+            IPDestroyInterface(IF);
+            return FALSE;
+        }
+
+        RegistryDataU.MaximumLength = 16 + sizeof(WCHAR);
+        RegistryDataU.Buffer = (PWCHAR)KeyValueInfo->Data;
+
+        Status = ZwQueryValueKey(ParameterHandle,
+                                 &IPAddress,
+                                 KeyValuePartialInformation,
+                                 KeyValueInfo,
+                                 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
+                                 &Unused);
+        if (NT_SUCCESS(Status))
+        {
+            RegistryDataU.Length = KeyValueInfo->DataLength;
+
+            RtlUnicodeStringToAnsiString(&RegistryDataA,
+                                         &RegistryDataU,
+                                         TRUE);
+
+            AddrInitIPv4(&IF->Unicast, inet_addr(RegistryDataA.Buffer));
+
+            RtlFreeAnsiString(&RegistryDataA);
+
+        }
+        else
+        {
+            IF->Unicast = DefaultMask;
+        }
+
+        Status = ZwQueryValueKey(ParameterHandle,
+                                 &Netmask,
+                                 KeyValuePartialInformation,
+                                 KeyValueInfo,
+                                 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
+                                 &Unused);
+        if (NT_SUCCESS(Status))
+        {
+            RegistryDataU.Length = KeyValueInfo->DataLength;
+
+            RtlUnicodeStringToAnsiString(&RegistryDataA,
+                                         &RegistryDataU,
+                                         TRUE);
+
+            AddrInitIPv4(&IF->Netmask, inet_addr(RegistryDataA.Buffer));
+
+            RtlFreeAnsiString(&RegistryDataA);
+        }
+        else
+        {
+            IF->Netmask = DefaultMask;
+        }
+
+        Status = ZwQueryValueKey(ParameterHandle,
+                                 &Gateway,
+                                 KeyValuePartialInformation,
+                                 KeyValueInfo,
+                                 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
+                                 &Unused);
+        if (NT_SUCCESS(Status))
+        {
+            RegistryDataU.Length = KeyValueInfo->DataLength;
+
+            RtlUnicodeStringToAnsiString(&RegistryDataA,
+                                         &RegistryDataU,
+                                         TRUE);
+
+            AddrInitIPv4(&Router, inet_addr(RegistryDataA.Buffer));
+
+            RtlFreeAnsiString(&RegistryDataA);
+
+            if (!AddrIsUnspecified(&Router)) RouterCreateRoute(&DefaultMask, &DefaultMask, &Router, IF, 1);
+        }
+
+        ZwClose(ParameterHandle);
+    }
 
     IF->Broadcast.Type = IP_ADDRESS_V4;
     IF->Broadcast.Address.IPv4Address =