Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenwlanconf.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS WLAN command-line configuration utility 00003 * LICENSE: GPL - See COPYING in the top level directory 00004 * FILE: base/applications/network/wlanconf/wlanconf.c 00005 * PURPOSE: Allows WLAN configuration via the command prompt 00006 * COPYRIGHT: Copyright 2012 Cameron Gutman (cameron.gutman@reactos.org) 00007 */ 00008 00009 #include <windows.h> 00010 #include <tchar.h> 00011 #include <stdio.h> 00012 #include <stdlib.h> 00013 #include <ntddndis.h> 00014 #include <nuiouser.h> 00015 #include <iphlpapi.h> 00016 00017 BOOL bScan = FALSE; 00018 00019 BOOL bConnect = FALSE; 00020 char* sSsid = NULL; 00021 char* sWepKey = NULL; 00022 BOOL bAdhoc = FALSE; 00023 00024 BOOL bDisconnect = FALSE; 00025 00026 DWORD DoFormatMessage(DWORD ErrorCode) 00027 { 00028 LPVOID lpMsgBuf; 00029 DWORD RetVal; 00030 00031 if ((RetVal = FormatMessage( 00032 FORMAT_MESSAGE_ALLOCATE_BUFFER | 00033 FORMAT_MESSAGE_FROM_SYSTEM | 00034 FORMAT_MESSAGE_IGNORE_INSERTS, 00035 NULL, 00036 ErrorCode, 00037 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ 00038 (LPTSTR) &lpMsgBuf, 00039 0, 00040 NULL ))) 00041 { 00042 _tprintf(_T("%s"), (LPTSTR)lpMsgBuf); 00043 00044 LocalFree(lpMsgBuf); 00045 00046 /* return number of TCHAR's stored in output buffer 00047 * excluding '\0' - as FormatMessage does*/ 00048 return RetVal; 00049 } 00050 else 00051 return 0; 00052 } 00053 00054 HANDLE 00055 OpenDriverHandle(VOID) 00056 { 00057 HANDLE hDriver; 00058 DWORD dwBytesReturned; 00059 BOOL bSuccess; 00060 00061 /* Open a handle to the NDISUIO driver */ 00062 hDriver = CreateFileW(NDISUIO_DEVICE_NAME, 00063 GENERIC_READ | GENERIC_WRITE, 00064 0, 00065 NULL, 00066 OPEN_EXISTING, 00067 FILE_ATTRIBUTE_NORMAL, 00068 NULL); 00069 if (hDriver == INVALID_HANDLE_VALUE) 00070 return INVALID_HANDLE_VALUE; 00071 00072 /* Wait for binds */ 00073 bSuccess = DeviceIoControl(hDriver, 00074 IOCTL_NDISUIO_BIND_WAIT, 00075 NULL, 00076 0, 00077 NULL, 00078 0, 00079 &dwBytesReturned, 00080 NULL); 00081 if (!bSuccess) 00082 { 00083 CloseHandle(hDriver); 00084 return INVALID_HANDLE_VALUE; 00085 } 00086 00087 return hDriver; 00088 } 00089 00090 BOOL 00091 IsWlanAdapter(HANDLE hAdapter) 00092 { 00093 BOOL bSuccess; 00094 DWORD dwBytesReturned; 00095 NDISUIO_QUERY_OID QueryOid; 00096 00097 /* WLAN drivers must support this OID */ 00098 QueryOid.Oid = OID_GEN_PHYSICAL_MEDIUM; 00099 00100 bSuccess = DeviceIoControl(hAdapter, 00101 IOCTL_NDISUIO_QUERY_OID_VALUE, 00102 &QueryOid, 00103 sizeof(QueryOid), 00104 &QueryOid, 00105 sizeof(QueryOid), 00106 &dwBytesReturned, 00107 NULL); 00108 if (!bSuccess || *(PULONG)QueryOid.Data != NdisPhysicalMediumWirelessLan) 00109 return FALSE; 00110 00111 return TRUE; 00112 } 00113 00114 BOOL 00115 OpenAdapterHandle(DWORD Index, HANDLE *hAdapter, IP_ADAPTER_INDEX_MAP *IpInfo) 00116 { 00117 HANDLE hDriver; 00118 BOOL bSuccess; 00119 DWORD dwBytesReturned; 00120 DWORD QueryBindingSize = sizeof(NDISUIO_QUERY_BINDING) + (1024 * sizeof(WCHAR)); 00121 PNDISUIO_QUERY_BINDING QueryBinding; 00122 DWORD dwStatus, dwSize, i; 00123 PIP_INTERFACE_INFO InterfaceInfo = NULL; 00124 00125 /* Open the driver handle */ 00126 hDriver = OpenDriverHandle(); 00127 if (hDriver == INVALID_HANDLE_VALUE) 00128 return FALSE; 00129 00130 /* Allocate the binding struct */ 00131 QueryBinding = HeapAlloc(GetProcessHeap(), 0, QueryBindingSize); 00132 if (!QueryBinding) 00133 { 00134 CloseHandle(hDriver); 00135 return FALSE; 00136 } 00137 00138 /* Query the adapter binding information */ 00139 QueryBinding->BindingIndex = Index; 00140 bSuccess = DeviceIoControl(hDriver, 00141 IOCTL_NDISUIO_QUERY_BINDING, 00142 QueryBinding, 00143 QueryBindingSize, 00144 QueryBinding, 00145 QueryBindingSize, 00146 &dwBytesReturned, 00147 NULL); 00148 00149 if (!bSuccess) 00150 { 00151 HeapFree(GetProcessHeap(), 0, QueryBinding); 00152 CloseHandle(hDriver); 00153 return FALSE; 00154 } 00155 00156 /* Bind to the adapter */ 00157 bSuccess = DeviceIoControl(hDriver, 00158 IOCTL_NDISUIO_OPEN_DEVICE, 00159 (PUCHAR)QueryBinding + QueryBinding->DeviceNameOffset, 00160 QueryBinding->DeviceNameLength, 00161 NULL, 00162 0, 00163 &dwBytesReturned, 00164 NULL); 00165 00166 if (!bSuccess) 00167 { 00168 HeapFree(GetProcessHeap(), 0, QueryBinding); 00169 CloseHandle(hDriver); 00170 return FALSE; 00171 } 00172 00173 /* Get interface info from the IP helper */ 00174 dwSize = sizeof(IP_INTERFACE_INFO); 00175 do { 00176 if (InterfaceInfo) HeapFree(GetProcessHeap(), 0, InterfaceInfo); 00177 InterfaceInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(IP_INTERFACE_INFO)); 00178 if (!InterfaceInfo) 00179 { 00180 HeapFree(GetProcessHeap(), 0, QueryBinding); 00181 CloseHandle(hDriver); 00182 return FALSE; 00183 } 00184 dwStatus = GetInterfaceInfo(InterfaceInfo, &dwSize); 00185 } while (dwStatus == ERROR_INSUFFICIENT_BUFFER); 00186 00187 if (dwStatus != NO_ERROR) 00188 { 00189 HeapFree(GetProcessHeap(), 0, QueryBinding); 00190 HeapFree(GetProcessHeap(), 0, InterfaceInfo); 00191 return FALSE; 00192 } 00193 00194 for (i = 0; i < InterfaceInfo->NumAdapters; i++) 00195 { 00196 if (wcsstr((PWCHAR)((PUCHAR)QueryBinding + QueryBinding->DeviceNameOffset), 00197 InterfaceInfo->Adapter[i].Name)) 00198 { 00199 *IpInfo = InterfaceInfo->Adapter[i]; 00200 *hAdapter = hDriver; 00201 00202 HeapFree(GetProcessHeap(), 0, QueryBinding); 00203 HeapFree(GetProcessHeap(), 0, InterfaceInfo); 00204 00205 return TRUE; 00206 } 00207 } 00208 00209 HeapFree(GetProcessHeap(), 0, QueryBinding); 00210 HeapFree(GetProcessHeap(), 0, InterfaceInfo); 00211 CloseHandle(hDriver); 00212 00213 return FALSE; 00214 } 00215 00216 /* Only works with the first adapter for now */ 00217 BOOL 00218 OpenWlanAdapter(HANDLE *hAdapter, IP_ADAPTER_INDEX_MAP *IpInfo) 00219 { 00220 DWORD dwCurrentIndex; 00221 00222 for (dwCurrentIndex = 0; ; dwCurrentIndex++) 00223 { 00224 if (!OpenAdapterHandle(dwCurrentIndex, hAdapter, IpInfo)) 00225 break; 00226 00227 if (IsWlanAdapter(*hAdapter)) 00228 return TRUE; 00229 else 00230 CloseHandle(*hAdapter); 00231 } 00232 00233 return FALSE; 00234 } 00235 00236 BOOL 00237 WlanDisconnect(HANDLE hAdapter, PIP_ADAPTER_INDEX_MAP IpInfo) 00238 { 00239 BOOL bSuccess; 00240 DWORD dwBytesReturned; 00241 NDISUIO_SET_OID SetOid; 00242 00243 /* Release this IP address */ 00244 IpReleaseAddress(IpInfo); 00245 00246 /* Disassociate from the AP */ 00247 SetOid.Oid = OID_802_11_DISASSOCIATE; 00248 bSuccess = DeviceIoControl(hAdapter, 00249 IOCTL_NDISUIO_SET_OID_VALUE, 00250 &SetOid, 00251 sizeof(SetOid), 00252 NULL, 00253 0, 00254 &dwBytesReturned, 00255 NULL); 00256 if (!bSuccess) 00257 return FALSE; 00258 00259 _tprintf(_T("The operation completed successfully.\n")); 00260 return TRUE; 00261 } 00262 00263 static 00264 UCHAR 00265 CharToHex(CHAR Char) 00266 { 00267 Char = toupper(Char); 00268 00269 switch (Char) 00270 { 00271 case '0': 00272 return 0x0; 00273 case '1': 00274 return 0x1; 00275 case '2': 00276 return 0x2; 00277 case '3': 00278 return 0x3; 00279 case '4': 00280 return 0x4; 00281 case '5': 00282 return 0x5; 00283 case '6': 00284 return 0x6; 00285 case '7': 00286 return 0x7; 00287 case '8': 00288 return 0x8; 00289 case '9': 00290 return 0x9; 00291 case 'A': 00292 return 0xA; 00293 case 'B': 00294 return 0xB; 00295 case 'C': 00296 return 0xC; 00297 case 'D': 00298 return 0xD; 00299 case 'E': 00300 return 0xE; 00301 case 'F': 00302 return 0xF; 00303 default: 00304 return 0; 00305 } 00306 } 00307 00308 BOOL 00309 WlanPrintCurrentStatus(HANDLE hAdapter) 00310 { 00311 PNDISUIO_QUERY_OID QueryOid; 00312 DWORD QueryOidSize; 00313 BOOL bSuccess; 00314 DWORD dwBytesReturned; 00315 PNDIS_802_11_SSID SsidInfo; 00316 CHAR SsidBuffer[NDIS_802_11_LENGTH_SSID + 1]; 00317 DWORD i; 00318 00319 QueryOidSize = FIELD_OFFSET(NDISUIO_QUERY_OID, Data) + sizeof(NDIS_802_11_SSID); 00320 QueryOid = HeapAlloc(GetProcessHeap(), 0, QueryOidSize); 00321 if (!QueryOid) 00322 return FALSE; 00323 00324 QueryOid->Oid = OID_802_11_SSID; 00325 SsidInfo = (PNDIS_802_11_SSID)QueryOid->Data; 00326 00327 bSuccess = DeviceIoControl(hAdapter, 00328 IOCTL_NDISUIO_QUERY_OID_VALUE, 00329 QueryOid, 00330 QueryOidSize, 00331 QueryOid, 00332 QueryOidSize, 00333 &dwBytesReturned, 00334 NULL); 00335 if (!bSuccess) 00336 { 00337 HeapFree(GetProcessHeap(), 0, QueryOid); 00338 return FALSE; 00339 } 00340 00341 /* Copy the SSID to our internal buffer and terminate it */ 00342 RtlCopyMemory(SsidBuffer, SsidInfo->Ssid, SsidInfo->SsidLength); 00343 SsidBuffer[SsidInfo->SsidLength] = 0; 00344 00345 HeapFree(GetProcessHeap(), 0, QueryOid); 00346 QueryOidSize = FIELD_OFFSET(NDISUIO_QUERY_OID, Data) + sizeof(NDIS_802_11_MAC_ADDRESS); 00347 QueryOid = HeapAlloc(GetProcessHeap(), 0, QueryOidSize); 00348 if (!QueryOid) 00349 return FALSE; 00350 00351 QueryOid->Oid = OID_802_11_BSSID; 00352 00353 bSuccess = DeviceIoControl(hAdapter, 00354 IOCTL_NDISUIO_QUERY_OID_VALUE, 00355 QueryOid, 00356 QueryOidSize, 00357 QueryOid, 00358 QueryOidSize, 00359 &dwBytesReturned, 00360 NULL); 00361 if (SsidInfo->SsidLength == 0 || !bSuccess) 00362 { 00363 _tprintf(_T("\nWLAN disconnected\n")); 00364 HeapFree(GetProcessHeap(), 0, QueryOid); 00365 return TRUE; 00366 } 00367 else 00368 { 00369 _tprintf(_T("\nCurrent wireless configuration information:\n\n")); 00370 } 00371 00372 _tprintf(_T("SSID: %s\n"), SsidBuffer); 00373 00374 _tprintf(_T("BSSID: ")); 00375 for (i = 0; i < sizeof(NDIS_802_11_MAC_ADDRESS); i++) 00376 { 00377 UINT BssidData = QueryOid->Data[i]; 00378 00379 _tprintf(_T("%.2x"), BssidData); 00380 00381 if (i != sizeof(NDIS_802_11_MAC_ADDRESS) - 1) 00382 _tprintf(_T(":")); 00383 } 00384 _tprintf(_T("\n")); 00385 00386 HeapFree(GetProcessHeap(), 0, QueryOid); 00387 QueryOidSize = sizeof(NDISUIO_QUERY_OID); 00388 QueryOid = HeapAlloc(GetProcessHeap(), 0, QueryOidSize); 00389 if (!QueryOid) 00390 return FALSE; 00391 00392 QueryOid->Oid = OID_802_11_INFRASTRUCTURE_MODE; 00393 00394 bSuccess = DeviceIoControl(hAdapter, 00395 IOCTL_NDISUIO_QUERY_OID_VALUE, 00396 QueryOid, 00397 QueryOidSize, 00398 QueryOid, 00399 QueryOidSize, 00400 &dwBytesReturned, 00401 NULL); 00402 if (!bSuccess) 00403 { 00404 HeapFree(GetProcessHeap(), 0, QueryOid); 00405 return FALSE; 00406 } 00407 00408 _tprintf(_T("Network mode: %s\n"), (*(PUINT)QueryOid->Data == Ndis802_11IBSS) ? "Adhoc" : "Infrastructure"); 00409 00410 QueryOid->Oid = OID_802_11_WEP_STATUS; 00411 00412 bSuccess = DeviceIoControl(hAdapter, 00413 IOCTL_NDISUIO_QUERY_OID_VALUE, 00414 QueryOid, 00415 QueryOidSize, 00416 QueryOid, 00417 QueryOidSize, 00418 &dwBytesReturned, 00419 NULL); 00420 if (!bSuccess) 00421 { 00422 HeapFree(GetProcessHeap(), 0, QueryOid); 00423 return FALSE; 00424 } 00425 00426 _tprintf(_T("WEP enabled: %s\n"), (*(PUINT)QueryOid->Data == Ndis802_11WEPEnabled) ? "Yes" : "No"); 00427 00428 _tprintf("\n"); 00429 QueryOid->Oid = OID_802_11_RSSI; 00430 00431 bSuccess = DeviceIoControl(hAdapter, 00432 IOCTL_NDISUIO_QUERY_OID_VALUE, 00433 QueryOid, 00434 QueryOidSize, 00435 QueryOid, 00436 QueryOidSize, 00437 &dwBytesReturned, 00438 NULL); 00439 if (bSuccess) 00440 { 00441 /* This OID is optional */ 00442 _tprintf(_T("RSSI: %i dBm\n"), *(PINT)QueryOid->Data); 00443 } 00444 00445 QueryOid->Oid = OID_802_11_TX_POWER_LEVEL; 00446 00447 bSuccess = DeviceIoControl(hAdapter, 00448 IOCTL_NDISUIO_QUERY_OID_VALUE, 00449 QueryOid, 00450 QueryOidSize, 00451 QueryOid, 00452 QueryOidSize, 00453 &dwBytesReturned, 00454 NULL); 00455 if (bSuccess) 00456 { 00457 /* This OID is optional */ 00458 _tprintf(_T("Transmission power: %d mW\n"), *(PUINT)QueryOid->Data); 00459 } 00460 00461 _tprintf(_T("\n")); 00462 00463 QueryOid->Oid = OID_802_11_NUMBER_OF_ANTENNAS; 00464 00465 bSuccess = DeviceIoControl(hAdapter, 00466 IOCTL_NDISUIO_QUERY_OID_VALUE, 00467 QueryOid, 00468 QueryOidSize, 00469 QueryOid, 00470 QueryOidSize, 00471 &dwBytesReturned, 00472 NULL); 00473 if (bSuccess) 00474 { 00475 /* This OID is optional */ 00476 _tprintf(_T("Antenna count: %d\n"), *(PUINT)QueryOid->Data); 00477 } 00478 00479 QueryOid->Oid = OID_802_11_TX_ANTENNA_SELECTED; 00480 00481 bSuccess = DeviceIoControl(hAdapter, 00482 IOCTL_NDISUIO_QUERY_OID_VALUE, 00483 QueryOid, 00484 QueryOidSize, 00485 QueryOid, 00486 QueryOidSize, 00487 &dwBytesReturned, 00488 NULL); 00489 if (bSuccess) 00490 { 00491 UINT TransmitAntenna = *(PUINT)QueryOid->Data; 00492 00493 if (TransmitAntenna != 0xFFFFFFFF) 00494 _tprintf(_T("Transmit antenna: %d\n"), TransmitAntenna); 00495 else 00496 _tprintf(_T("Transmit antenna: Any\n")); 00497 } 00498 00499 QueryOid->Oid = OID_802_11_RX_ANTENNA_SELECTED; 00500 00501 bSuccess = DeviceIoControl(hAdapter, 00502 IOCTL_NDISUIO_QUERY_OID_VALUE, 00503 QueryOid, 00504 QueryOidSize, 00505 QueryOid, 00506 QueryOidSize, 00507 &dwBytesReturned, 00508 NULL); 00509 if (bSuccess) 00510 { 00511 UINT ReceiveAntenna = *(PUINT)QueryOid->Data; 00512 00513 if (ReceiveAntenna != 0xFFFFFFFF) 00514 _tprintf(_T("Receive antenna: %d\n"), ReceiveAntenna); 00515 else 00516 _tprintf(_T("Receive antenna: Any\n")); 00517 } 00518 00519 _tprintf(_T("\n")); 00520 00521 QueryOid->Oid = OID_802_11_FRAGMENTATION_THRESHOLD; 00522 00523 bSuccess = DeviceIoControl(hAdapter, 00524 IOCTL_NDISUIO_QUERY_OID_VALUE, 00525 QueryOid, 00526 QueryOidSize, 00527 QueryOid, 00528 QueryOidSize, 00529 &dwBytesReturned, 00530 NULL); 00531 if (bSuccess) 00532 { 00533 /* This OID is optional */ 00534 _tprintf(_T("Fragmentation threshold: %d bytes\n"), *(PUINT)QueryOid->Data); 00535 } 00536 00537 QueryOid->Oid = OID_802_11_RTS_THRESHOLD; 00538 00539 bSuccess = DeviceIoControl(hAdapter, 00540 IOCTL_NDISUIO_QUERY_OID_VALUE, 00541 QueryOid, 00542 QueryOidSize, 00543 QueryOid, 00544 QueryOidSize, 00545 &dwBytesReturned, 00546 NULL); 00547 if (bSuccess) 00548 { 00549 /* This OID is optional */ 00550 _tprintf(_T("RTS threshold: %d bytes\n"), *(PUINT)QueryOid->Data); 00551 } 00552 00553 HeapFree(GetProcessHeap(), 0, QueryOid); 00554 00555 _tprintf(_T("\n")); 00556 return TRUE; 00557 } 00558 00559 BOOL 00560 WlanConnect(HANDLE hAdapter) 00561 { 00562 BOOL bSuccess; 00563 DWORD dwBytesReturned, SetOidSize; 00564 PNDISUIO_SET_OID SetOid; 00565 PNDIS_802_11_SSID Ssid; 00566 DWORD i; 00567 00568 SetOidSize = sizeof(NDISUIO_SET_OID); 00569 SetOid = HeapAlloc(GetProcessHeap(), 0, SetOidSize); 00570 if (!SetOid) 00571 return FALSE; 00572 00573 /* Set the network mode */ 00574 SetOid->Oid = OID_802_11_INFRASTRUCTURE_MODE; 00575 *(PULONG)SetOid->Data = bAdhoc ? Ndis802_11IBSS : Ndis802_11Infrastructure; 00576 00577 bSuccess = DeviceIoControl(hAdapter, 00578 IOCTL_NDISUIO_SET_OID_VALUE, 00579 SetOid, 00580 SetOidSize, 00581 NULL, 00582 0, 00583 &dwBytesReturned, 00584 NULL); 00585 if (!bSuccess) 00586 { 00587 HeapFree(GetProcessHeap(), 0, SetOid); 00588 return FALSE; 00589 } 00590 00591 /* Set the authentication mode */ 00592 SetOid->Oid = OID_802_11_AUTHENTICATION_MODE; 00593 *(PULONG)SetOid->Data = sWepKey ? Ndis802_11AuthModeShared : Ndis802_11AuthModeOpen; 00594 00595 bSuccess = DeviceIoControl(hAdapter, 00596 IOCTL_NDISUIO_SET_OID_VALUE, 00597 SetOid, 00598 SetOidSize, 00599 NULL, 00600 0, 00601 &dwBytesReturned, 00602 NULL); 00603 if (!bSuccess) 00604 { 00605 HeapFree(GetProcessHeap(), 0, SetOid); 00606 return FALSE; 00607 } 00608 00609 if (sWepKey) 00610 { 00611 PNDIS_802_11_WEP WepData; 00612 00613 HeapFree(GetProcessHeap(), 0, SetOid); 00614 00615 SetOidSize = FIELD_OFFSET(NDISUIO_SET_OID, Data) + 00616 FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial) + 00617 (strlen(sWepKey) >> 1); 00618 SetOid = HeapAlloc(GetProcessHeap(), 0, SetOidSize); 00619 if (!SetOid) 00620 return FALSE; 00621 00622 /* Add the WEP key */ 00623 SetOid->Oid = OID_802_11_ADD_WEP; 00624 WepData = (PNDIS_802_11_WEP)SetOid->Data; 00625 00626 WepData->KeyIndex = 0x80000000; 00627 WepData->KeyLength = strlen(sWepKey) >> 1; 00628 WepData->Length = FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial) + WepData->KeyLength; 00629 00630 /* Assemble the hex key */ 00631 i = 0; 00632 while (sWepKey[i << 1] != '\0') 00633 { 00634 WepData->KeyMaterial[i] = CharToHex(sWepKey[i << 1]) << 4; 00635 WepData->KeyMaterial[i] |= CharToHex(sWepKey[(i << 1) + 1]); 00636 i++; 00637 } 00638 00639 bSuccess = DeviceIoControl(hAdapter, 00640 IOCTL_NDISUIO_SET_OID_VALUE, 00641 SetOid, 00642 SetOidSize, 00643 NULL, 00644 0, 00645 &dwBytesReturned, 00646 NULL); 00647 if (!bSuccess) 00648 { 00649 HeapFree(GetProcessHeap(), 0, SetOid); 00650 return FALSE; 00651 } 00652 } 00653 00654 /* Set the encryption status */ 00655 SetOid->Oid = OID_802_11_WEP_STATUS; 00656 *(PULONG)SetOid->Data = sWepKey ? Ndis802_11WEPEnabled : Ndis802_11WEPDisabled; 00657 00658 bSuccess = DeviceIoControl(hAdapter, 00659 IOCTL_NDISUIO_SET_OID_VALUE, 00660 SetOid, 00661 SetOidSize, 00662 NULL, 00663 0, 00664 &dwBytesReturned, 00665 NULL); 00666 if (!bSuccess) 00667 { 00668 HeapFree(GetProcessHeap(), 0, SetOid); 00669 return FALSE; 00670 } 00671 00672 HeapFree(GetProcessHeap(), 0, SetOid); 00673 SetOidSize = FIELD_OFFSET(NDISUIO_SET_OID, Data) + sizeof(NDIS_802_11_MAC_ADDRESS); 00674 SetOid = HeapAlloc(GetProcessHeap(), 0, SetOidSize); 00675 if (!SetOid) 00676 return FALSE; 00677 00678 /* Set the BSSID */ 00679 SetOid->Oid = OID_802_11_BSSID; 00680 RtlFillMemory(SetOid->Data, sizeof(NDIS_802_11_MAC_ADDRESS), 0xFF); 00681 00682 bSuccess = DeviceIoControl(hAdapter, 00683 IOCTL_NDISUIO_SET_OID_VALUE, 00684 SetOid, 00685 SetOidSize, 00686 NULL, 00687 0, 00688 &dwBytesReturned, 00689 NULL); 00690 if (!bSuccess) 00691 { 00692 HeapFree(GetProcessHeap(), 0, SetOid); 00693 return FALSE; 00694 } 00695 00696 HeapFree(GetProcessHeap(), 0, SetOid); 00697 SetOidSize = FIELD_OFFSET(NDISUIO_SET_OID, Data) + sizeof(NDIS_802_11_SSID); 00698 SetOid = HeapAlloc(GetProcessHeap(), 0, SetOidSize); 00699 if (!SetOid) 00700 return FALSE; 00701 00702 /* Finally, set the SSID */ 00703 SetOid->Oid = OID_802_11_SSID; 00704 Ssid = (PNDIS_802_11_SSID)SetOid->Data; 00705 00706 RtlCopyMemory(Ssid->Ssid, sSsid, strlen(sSsid)); 00707 Ssid->SsidLength = strlen(sSsid); 00708 00709 bSuccess = DeviceIoControl(hAdapter, 00710 IOCTL_NDISUIO_SET_OID_VALUE, 00711 SetOid, 00712 SetOidSize, 00713 NULL, 00714 0, 00715 &dwBytesReturned, 00716 NULL); 00717 00718 HeapFree(GetProcessHeap(), 0, SetOid); 00719 00720 if (!bSuccess) 00721 return FALSE; 00722 00723 _tprintf(_T("The operation completed successfully.\n")); 00724 return TRUE; 00725 } 00726 00727 BOOL 00728 WlanScan(HANDLE hAdapter) 00729 { 00730 BOOL bSuccess; 00731 DWORD dwBytesReturned; 00732 NDISUIO_SET_OID SetOid; 00733 PNDISUIO_QUERY_OID QueryOid; 00734 DWORD QueryOidSize; 00735 PNDIS_802_11_BSSID_LIST BssidList; 00736 DWORD i, j; 00737 00738 SetOid.Oid = OID_802_11_BSSID_LIST_SCAN; 00739 00740 /* Send the scan OID */ 00741 bSuccess = DeviceIoControl(hAdapter, 00742 IOCTL_NDISUIO_SET_OID_VALUE, 00743 &SetOid, 00744 sizeof(SetOid), 00745 NULL, 00746 0, 00747 &dwBytesReturned, 00748 NULL); 00749 if (!bSuccess) 00750 return FALSE; 00751 00752 /* Allocate space for 15 networks to be returned */ 00753 QueryOidSize = sizeof(NDISUIO_QUERY_OID) + (sizeof(NDIS_WLAN_BSSID) * 15); 00754 QueryOid = HeapAlloc(GetProcessHeap(), 0, QueryOidSize); 00755 if (!QueryOid) 00756 return FALSE; 00757 00758 QueryOid->Oid = OID_802_11_BSSID_LIST; 00759 BssidList = (PNDIS_802_11_BSSID_LIST)QueryOid->Data; 00760 00761 bSuccess = DeviceIoControl(hAdapter, 00762 IOCTL_NDISUIO_QUERY_OID_VALUE, 00763 QueryOid, 00764 QueryOidSize, 00765 QueryOid, 00766 QueryOidSize, 00767 &dwBytesReturned, 00768 NULL); 00769 if (!bSuccess) 00770 { 00771 HeapFree(GetProcessHeap(), 0, QueryOid); 00772 return FALSE; 00773 } 00774 00775 if (BssidList->NumberOfItems == 0) 00776 { 00777 _tprintf(_T("No networks found in range\n")); 00778 } 00779 else 00780 { 00781 PNDIS_WLAN_BSSID BssidInfo = BssidList->Bssid; 00782 for (i = 0; i < BssidList->NumberOfItems; i++) 00783 { 00784 PNDIS_802_11_SSID Ssid = &BssidInfo->Ssid; 00785 NDIS_802_11_RSSI Rssi = BssidInfo->Rssi; 00786 NDIS_802_11_NETWORK_INFRASTRUCTURE NetworkType = BssidInfo->InfrastructureMode; 00787 CHAR SsidBuffer[NDIS_802_11_LENGTH_SSID + 1]; 00788 UINT Rate; 00789 00790 /* SSID member is a non-null terminated ASCII string */ 00791 RtlCopyMemory(SsidBuffer, Ssid->Ssid, Ssid->SsidLength); 00792 SsidBuffer[Ssid->SsidLength] = 0; 00793 00794 _tprintf(_T("\n")); 00795 00796 _tprintf(_T("SSID: %s\n"), SsidBuffer); 00797 00798 _tprintf(_T("BSSID: ")); 00799 for (j = 0; j < sizeof(NDIS_802_11_MAC_ADDRESS); j++) 00800 { 00801 UINT BssidData = BssidInfo->MacAddress[j]; 00802 00803 _tprintf(_T("%.2x"), BssidData); 00804 00805 if (j != sizeof(NDIS_802_11_MAC_ADDRESS) - 1) 00806 _tprintf(_T(":")); 00807 } 00808 _tprintf(_T("\n")); 00809 00810 _tprintf(_T("Encrypted: %s\n" 00811 "Network Type: %s\n" 00812 "RSSI: %i dBm\n" 00813 "Supported Rates (Mbps): "), 00814 BssidInfo->Privacy == 0 ? "No" : "Yes", 00815 NetworkType == Ndis802_11IBSS ? "Adhoc" : "Infrastructure", 00816 (int)Rssi); 00817 00818 for (j = 0; j < NDIS_802_11_LENGTH_RATES; j++) 00819 { 00820 Rate = BssidInfo->SupportedRates[j]; 00821 if (Rate != 0) 00822 { 00823 /* Bit 7 is the basic rates bit */ 00824 Rate = Rate & 0x7F; 00825 00826 /* SupportedRates are in units of .5 */ 00827 if (Rate & 0x01) 00828 { 00829 /* Bit 0 is set so we need to add 0.5 */ 00830 _tprintf(_T("%u.5 "), (Rate >> 1)); 00831 } 00832 else 00833 { 00834 /* Bit 0 is clear so just print the conversion */ 00835 _tprintf(_T("%u "), (Rate >> 1)); 00836 } 00837 } 00838 } 00839 _tprintf(_T("\n")); 00840 00841 /* Move to the next entry */ 00842 BssidInfo = (PNDIS_WLAN_BSSID)((PUCHAR)BssidInfo + BssidInfo->Length); 00843 } 00844 } 00845 00846 HeapFree(GetProcessHeap(), 0, QueryOid); 00847 00848 return bSuccess; 00849 } 00850 00851 VOID Usage() 00852 { 00853 _tprintf(_T("\nConfigures a WLAN adapter.\n\n" 00854 "WLANCONF [-c SSID [-w WEP] [-a]] [-d] [-s]\n\n" 00855 " -c SSID Connects to a supplied SSID.\n" 00856 " -w WEP Specifies a WEP key to use.\n" 00857 " -a Specifies the target network is ad-hoc\n" 00858 " -d Disconnects from the current AP.\n" 00859 " -s Scans and displays a list of access points in range.\n\n" 00860 " Passing no parameters will print information about the current WLAN connection\n")); 00861 } 00862 00863 00864 BOOL ParseCmdline(int argc, char* argv[]) 00865 { 00866 INT i; 00867 00868 for (i = 1; i < argc; i++) 00869 { 00870 if (argv[i][0] == '-') 00871 { 00872 switch (argv[i][1]) 00873 { 00874 case 's': 00875 bScan = TRUE; 00876 break; 00877 case 'd': 00878 bDisconnect = TRUE; 00879 break; 00880 case 'c': 00881 if (i == argc - 1) 00882 { 00883 Usage(); 00884 return FALSE; 00885 } 00886 bConnect = TRUE; 00887 sSsid = argv[++i]; 00888 break; 00889 case 'w': 00890 if (i == argc - 1) 00891 { 00892 Usage(); 00893 return FALSE; 00894 } 00895 sWepKey = argv[++i]; 00896 break; 00897 case 'a': 00898 bAdhoc = TRUE; 00899 break; 00900 default : 00901 Usage(); 00902 return FALSE; 00903 } 00904 00905 } 00906 else 00907 { 00908 Usage(); 00909 return FALSE; 00910 } 00911 } 00912 00913 return TRUE; 00914 } 00915 00916 int main(int argc, char* argv[]) 00917 { 00918 HANDLE hAdapter; 00919 IP_ADAPTER_INDEX_MAP IpInfo; 00920 00921 if (!ParseCmdline(argc, argv)) 00922 return -1; 00923 00924 if (!OpenWlanAdapter(&hAdapter, &IpInfo)) 00925 { 00926 _tprintf(_T("Unable to find a WLAN adapter on the system\n")); 00927 return -1; 00928 } 00929 00930 if (bScan) 00931 { 00932 if (!WlanScan(hAdapter)) 00933 { 00934 DoFormatMessage(GetLastError()); 00935 CloseHandle(hAdapter); 00936 return -1; 00937 } 00938 } 00939 else if (bDisconnect) 00940 { 00941 if (!WlanDisconnect(hAdapter, &IpInfo)) 00942 { 00943 DoFormatMessage(GetLastError()); 00944 CloseHandle(hAdapter); 00945 return -1; 00946 } 00947 } 00948 else if (bConnect) 00949 { 00950 if (!WlanConnect(hAdapter)) 00951 { 00952 DoFormatMessage(GetLastError()); 00953 CloseHandle(hAdapter); 00954 return -1; 00955 } 00956 } 00957 else 00958 { 00959 if (!WlanPrintCurrentStatus(hAdapter)) 00960 { 00961 DoFormatMessage(GetLastError()); 00962 CloseHandle(hAdapter); 00963 return -1; 00964 } 00965 } 00966 00967 CloseHandle(hAdapter); 00968 return 0; 00969 } Generated on Sat May 26 2012 04:16:15 for ReactOS by
1.7.6.1
|