[WLANCONF]
[reactos.git] / base / applications / network / wlanconf / wlanconf.c
index b412fce..397048c 100644 (file)
@@ -116,32 +116,36 @@ OpenAdapterHandle(DWORD Index)
     HANDLE hDriver;
     BOOL bSuccess;
     DWORD dwBytesReturned;
-    char Buffer[1024];
-    PNDISUIO_QUERY_BINDING QueryBinding = (PNDISUIO_QUERY_BINDING)Buffer;
+    DWORD QueryBindingSize = sizeof(NDISUIO_QUERY_BINDING) + (1024 * sizeof(WCHAR));
+    PNDISUIO_QUERY_BINDING QueryBinding;
     
     /* Open the driver handle */
     hDriver = OpenDriverHandle();
     if (hDriver == INVALID_HANDLE_VALUE)
         return INVALID_HANDLE_VALUE;
+    
+    /* Allocate the binding struct */
+    QueryBinding = HeapAlloc(GetProcessHeap(), 0, QueryBindingSize);
+    if (!QueryBinding)
+    {
+        CloseHandle(hDriver);
+        return INVALID_HANDLE_VALUE;
+    }
 
-    /* Query for bindable adapters */
-    QueryBinding->BindingIndex = 0;
-    do {
-        bSuccess = DeviceIoControl(hDriver,
-                                   IOCTL_NDISUIO_QUERY_BINDING,
-                                   NULL,
-                                   0,
-                                   NULL,
-                                   0,
-                                   &dwBytesReturned,
-                                   NULL);
-        if (QueryBinding->BindingIndex == Index)
-            break;
-        QueryBinding->BindingIndex++;
-    } while (bSuccess);
+    /* Query the adapter binding information */
+    QueryBinding->BindingIndex = Index;
+    bSuccess = DeviceIoControl(hDriver,
+                               IOCTL_NDISUIO_QUERY_BINDING,
+                               QueryBinding,
+                               QueryBindingSize,
+                               QueryBinding,
+                               QueryBindingSize,
+                               &dwBytesReturned,
+                               NULL);
 
     if (!bSuccess)
     {
+        HeapFree(GetProcessHeap(), 0, QueryBinding);
         CloseHandle(hDriver);
         return INVALID_HANDLE_VALUE;
     }
@@ -155,6 +159,8 @@ OpenAdapterHandle(DWORD Index)
                                0,
                                &dwBytesReturned,
                                NULL);
+    HeapFree(GetProcessHeap(), 0, QueryBinding);
+
     if (!bSuccess)
     {
         CloseHandle(hDriver);
@@ -309,7 +315,7 @@ WlanConnect(HANDLE hAdapter)
         HeapFree(GetProcessHeap(), 0, SetOid);
 
         SetOidSize = sizeof(NDISUIO_SET_OID) + FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial) +
-                     (strlen(sWepKey) >> 2);
+                     (strlen(sWepKey) >> 1);
         SetOid = HeapAlloc(GetProcessHeap(), 0, SetOidSize);
         if (!SetOid)
             return FALSE;
@@ -319,15 +325,15 @@ WlanConnect(HANDLE hAdapter)
         WepData = (PNDIS_802_11_WEP)SetOid->Data;
 
         WepData->KeyIndex = 0x80000000;
-        WepData->KeyLength = strlen(sWepKey) >> 2;
+        WepData->KeyLength = strlen(sWepKey) >> 1;
         WepData->Length = FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial) + WepData->KeyLength;
         
         /* Assemble the hex key */
         i = 0;
-        while (sWepKey[i << 2] != '\0')
+        while (sWepKey[i << 1] != '\0')
         {
-            WepData->KeyMaterial[i] = CharToHex(sWepKey[i << 2]) << 4;
-            WepData->KeyMaterial[i] |= CharToHex(sWepKey[(i << 2) + 1]);
+            WepData->KeyMaterial[i] = CharToHex(sWepKey[i << 1]) << 4;
+            WepData->KeyMaterial[i] |= CharToHex(sWepKey[(i << 1) + 1]);
             i++;
         }
         
@@ -476,13 +482,14 @@ WlanScan(HANDLE hAdapter)
             NDIS_802_11_RSSI Rssi = BssidInfo->Rssi;
             NDIS_802_11_NETWORK_INFRASTRUCTURE NetworkType = BssidInfo->InfrastructureMode;
             CHAR SsidBuffer[NDIS_802_11_LENGTH_SSID + 1];
+            UINT Rate;
 
             /* SSID member is a non-null terminated ASCII string */
             RtlCopyMemory(SsidBuffer, Ssid->Ssid, Ssid->SsidLength);
             SsidBuffer[Ssid->SsidLength] = 0;
             
             _tprintf(_T("\nSSID: %s\n"
-                        "Encrypted: %s"
+                        "Encrypted: %s\n"
                         "Network Type: %s\n"
                         "RSSI: %i dBm\n"
                         "Supported Rates (Mbps): "),
@@ -493,10 +500,16 @@ WlanScan(HANDLE hAdapter)
             
             for (j = 0; j < NDIS_802_11_LENGTH_RATES; j++)
             {
-                if (BssidInfo->SupportedRates[j] != 0)
+                Rate = BssidInfo->SupportedRates[j];
+                if (Rate != 0)
                 {
+                    /* Bit 7 is the basic rates bit */
+                    Rate = Rate & 0x7F;
+
                     /* SupportedRates are in units of .5 */
-                    _tprintf(_T("%d "), (BssidInfo->SupportedRates[j] << 2));
+                    Rate = Rate << 1;
+
+                    _tprintf(_T("%u "), Rate);
                 }
             }
             _tprintf(_T("\n"));
@@ -529,35 +542,46 @@ BOOL ParseCmdline(int argc, char* argv[])
     
     for (i = 1; i < argc; i++)
     {
-        if ((argc > 1) && (argv[i][0] == '-'))
+        if (argv[i][0] == '-')
         {
-            TCHAR c;
-            
-            while ((c = *++argv[i]) != '\0')
+            switch (argv[i][1])
             {
-                switch (c)
-                {
-                    case 's':
-                        bScan = TRUE;
-                        break;
-                    case 'd':
-                        bDisconnect = TRUE;
-                        break;
-                    case 'c':
-                        bConnect = TRUE;
-                        sSsid = argv[++i];
-                        break;
-                    case 'w':
-                        sWepKey = argv[++i];
-                        break;
-                    case 'a':
-                        bAdhoc = TRUE;
-                        break;
-                    default :
+                case 's':
+                    bScan = TRUE;
+                    break;
+                case 'd':
+                    bDisconnect = TRUE;
+                    break;
+                case 'c':
+                    if (i == argc - 1)
+                    {
                         Usage();
                         return FALSE;
-                }
+                    }
+                    bConnect = TRUE;
+                    sSsid = argv[++i];
+                    break;
+                case 'w':
+                    if (i == argc - 1)
+                    {
+                        Usage();
+                        return FALSE;
+                    }
+                    sWepKey = argv[++i];
+                    break;
+                case 'a':
+                    bAdhoc = TRUE;
+                    break;
+                default :
+                    Usage();
+                    return FALSE;
             }
+
+        }
+        else
+        {
+            Usage();
+            return FALSE;
         }
     }