2 * PROJECT: ReactOS WLAN command-line configuration utility
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/network/wlanconf/wlanconf.c
5 * PURPOSE: Allows WLAN configuration via the command prompt
6 * COPYRIGHT: Copyright 2012 Cameron Gutman (cameron.gutman@reactos.org)
18 BOOL bConnect
= FALSE
;
23 BOOL bDisconnect
= FALSE
;
25 DWORD
DoFormatMessage(DWORD ErrorCode
)
30 if ((RetVal
= FormatMessage(
31 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
32 FORMAT_MESSAGE_FROM_SYSTEM
|
33 FORMAT_MESSAGE_IGNORE_INSERTS
,
36 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
41 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
45 /* return number of TCHAR's stored in output buffer
46 * excluding '\0' - as FormatMessage does*/
54 OpenDriverHandle(VOID
)
57 DWORD dwBytesReturned
;
60 /* Open a handle to the NDISUIO driver */
61 hDriver
= CreateFileW(NDISUIO_DEVICE_NAME
,
62 GENERIC_READ
| GENERIC_WRITE
,
66 FILE_ATTRIBUTE_NORMAL
,
68 if (hDriver
== INVALID_HANDLE_VALUE
)
69 return INVALID_HANDLE_VALUE
;
72 bSuccess
= DeviceIoControl(hDriver
,
73 IOCTL_NDISUIO_BIND_WAIT
,
83 return INVALID_HANDLE_VALUE
;
90 IsWlanAdapter(HANDLE hAdapter
)
93 DWORD dwBytesReturned
;
94 NDISUIO_QUERY_OID QueryOid
;
96 /* NDIS 5.1 WLAN drivers must support this OID */
97 QueryOid
.Oid
= OID_GEN_PHYSICAL_MEDIUM
;
99 bSuccess
= DeviceIoControl(hAdapter
,
100 IOCTL_NDISUIO_QUERY_OID_VALUE
,
107 if (!bSuccess
|| *(PULONG
)QueryOid
.Data
!= NdisPhysicalMediumWirelessLan
)
114 OpenAdapterHandle(DWORD Index
)
118 DWORD dwBytesReturned
;
120 PNDISUIO_QUERY_BINDING QueryBinding
= (PNDISUIO_QUERY_BINDING
)Buffer
;
122 /* Open the driver handle */
123 hDriver
= OpenDriverHandle();
124 if (hDriver
== INVALID_HANDLE_VALUE
)
125 return INVALID_HANDLE_VALUE
;
127 /* Query for bindable adapters */
128 QueryBinding
->BindingIndex
= 0;
130 bSuccess
= DeviceIoControl(hDriver
,
131 IOCTL_NDISUIO_QUERY_BINDING
,
138 if (QueryBinding
->BindingIndex
== Index
)
140 QueryBinding
->BindingIndex
++;
145 CloseHandle(hDriver
);
146 return INVALID_HANDLE_VALUE
;
149 /* Bind to the adapter */
150 bSuccess
= DeviceIoControl(hDriver
,
151 IOCTL_NDISUIO_OPEN_DEVICE
,
152 (PUCHAR
)QueryBinding
+ QueryBinding
->DeviceNameOffset
,
153 QueryBinding
->DeviceNameLength
,
160 CloseHandle(hDriver
);
161 return INVALID_HANDLE_VALUE
;
167 /* Only works with the first adapter for now */
169 OpenWlanAdapter(VOID
)
171 DWORD dwCurrentIndex
;
172 HANDLE hCurrentAdapter
;
174 for (dwCurrentIndex
= 0; ; dwCurrentIndex
++)
176 hCurrentAdapter
= OpenAdapterHandle(dwCurrentIndex
);
177 if (hCurrentAdapter
== INVALID_HANDLE_VALUE
)
180 if (IsWlanAdapter(hCurrentAdapter
))
181 return hCurrentAdapter
;
183 CloseHandle(hCurrentAdapter
);
186 return INVALID_HANDLE_VALUE
;
190 WlanDisconnect(HANDLE hAdapter
)
193 DWORD dwBytesReturned
;
194 NDISUIO_SET_OID SetOid
;
196 SetOid
.Oid
= OID_802_11_DISASSOCIATE
;
198 bSuccess
= DeviceIoControl(hAdapter
,
199 IOCTL_NDISUIO_SET_OID_VALUE
,
214 Char
= toupper(Char
);
256 WlanConnect(HANDLE hAdapter
)
259 DWORD dwBytesReturned
, SetOidSize
;
260 PNDISUIO_SET_OID SetOid
;
261 PNDIS_802_11_SSID Ssid
;
264 SetOidSize
= sizeof(NDISUIO_SET_OID
);
265 SetOid
= HeapAlloc(GetProcessHeap(), 0, SetOidSize
);
269 /* Set the network mode */
270 SetOid
->Oid
= OID_802_11_INFRASTRUCTURE_MODE
;
271 *(PULONG
)SetOid
->Data
= bAdhoc
? Ndis802_11IBSS
: Ndis802_11Infrastructure
;
273 bSuccess
= DeviceIoControl(hAdapter
,
274 IOCTL_NDISUIO_SET_OID_VALUE
,
283 HeapFree(GetProcessHeap(), 0, SetOid
);
287 /* Set the authentication mode */
288 SetOid
->Oid
= OID_802_11_AUTHENTICATION_MODE
;
289 *(PULONG
)SetOid
->Data
= sWepKey
? Ndis802_11AuthModeShared
: Ndis802_11AuthModeOpen
;
291 bSuccess
= DeviceIoControl(hAdapter
,
292 IOCTL_NDISUIO_SET_OID_VALUE
,
301 HeapFree(GetProcessHeap(), 0, SetOid
);
307 PNDIS_802_11_WEP WepData
;
309 HeapFree(GetProcessHeap(), 0, SetOid
);
311 SetOidSize
= sizeof(NDISUIO_SET_OID
) + FIELD_OFFSET(NDIS_802_11_WEP
, KeyMaterial
) +
312 (strlen(sWepKey
) >> 2);
313 SetOid
= HeapAlloc(GetProcessHeap(), 0, SetOidSize
);
317 /* Add the WEP key */
318 SetOid
->Oid
= OID_802_11_ADD_WEP
;
319 WepData
= (PNDIS_802_11_WEP
)SetOid
->Data
;
321 WepData
->KeyIndex
= 0x80000000;
322 WepData
->KeyLength
= strlen(sWepKey
) >> 2;
323 WepData
->Length
= FIELD_OFFSET(NDIS_802_11_WEP
, KeyMaterial
) + WepData
->KeyLength
;
325 /* Assemble the hex key */
327 while (sWepKey
[i
<< 2] != '\0')
329 WepData
->KeyMaterial
[i
] = CharToHex(sWepKey
[i
<< 2]) << 4;
330 WepData
->KeyMaterial
[i
] |= CharToHex(sWepKey
[(i
<< 2) + 1]);
334 bSuccess
= DeviceIoControl(hAdapter
,
335 IOCTL_NDISUIO_SET_OID_VALUE
,
344 HeapFree(GetProcessHeap(), 0, SetOid
);
349 /* Set the encryption status */
350 SetOid
->Oid
= OID_802_11_WEP_STATUS
;
351 *(PULONG
)SetOid
->Data
= sWepKey
? Ndis802_11WEPEnabled
: Ndis802_11WEPDisabled
;
353 bSuccess
= DeviceIoControl(hAdapter
,
354 IOCTL_NDISUIO_SET_OID_VALUE
,
363 HeapFree(GetProcessHeap(), 0, SetOid
);
367 HeapFree(GetProcessHeap(), 0, SetOid
);
368 SetOidSize
= sizeof(NDISUIO_SET_OID
) + sizeof(NDIS_802_11_MAC_ADDRESS
);
369 SetOid
= HeapAlloc(GetProcessHeap(), 0, SetOidSize
);
374 SetOid
->Oid
= OID_802_11_BSSID
;
375 RtlFillMemory(SetOid
->Data
, sizeof(NDIS_802_11_MAC_ADDRESS
), 0xFF);
377 bSuccess
= DeviceIoControl(hAdapter
,
378 IOCTL_NDISUIO_SET_OID_VALUE
,
387 HeapFree(GetProcessHeap(), 0, SetOid
);
391 HeapFree(GetProcessHeap(), 0, SetOid
);
392 SetOidSize
= sizeof(NDISUIO_SET_OID
) + sizeof(NDIS_802_11_SSID
);
393 SetOid
= HeapAlloc(GetProcessHeap(), 0, SetOidSize
);
397 /* Finally, set the SSID */
398 SetOid
->Oid
= OID_802_11_SSID
;
399 Ssid
= (PNDIS_802_11_SSID
)SetOid
->Data
;
401 RtlCopyMemory(Ssid
->Ssid
, sSsid
, strlen(sSsid
));
402 Ssid
->SsidLength
= strlen(sSsid
);
404 bSuccess
= DeviceIoControl(hAdapter
,
405 IOCTL_NDISUIO_SET_OID_VALUE
,
413 HeapFree(GetProcessHeap(), 0, SetOid
);
419 WlanScan(HANDLE hAdapter
)
422 DWORD dwBytesReturned
;
423 NDISUIO_SET_OID SetOid
;
424 PNDISUIO_QUERY_OID QueryOid
;
426 PNDIS_802_11_BSSID_LIST BssidList
;
429 SetOid
.Oid
= OID_802_11_BSSID_LIST_SCAN
;
431 /* Send the scan OID */
432 bSuccess
= DeviceIoControl(hAdapter
,
433 IOCTL_NDISUIO_SET_OID_VALUE
,
443 /* Allocate space for 15 networks to be returned */
444 QueryOidSize
= sizeof(NDISUIO_QUERY_OID
) + (sizeof(NDIS_WLAN_BSSID
) * 15);
445 QueryOid
= HeapAlloc(GetProcessHeap(), 0, QueryOidSize
);
449 QueryOid
->Oid
= OID_802_11_BSSID_LIST
;
450 BssidList
= (PNDIS_802_11_BSSID_LIST
)QueryOid
->Data
;
452 bSuccess
= DeviceIoControl(hAdapter
,
453 IOCTL_NDISUIO_QUERY_OID_VALUE
,
462 HeapFree(GetProcessHeap(), 0, QueryOid
);
466 if (BssidList
->NumberOfItems
== 0)
468 _tprintf(_T("No networks found in range\n"));
472 PNDIS_WLAN_BSSID BssidInfo
= BssidList
->Bssid
;
473 for (i
= 0; i
< BssidList
->NumberOfItems
; i
++)
475 PNDIS_802_11_SSID Ssid
= &BssidInfo
->Ssid
;
476 NDIS_802_11_RSSI Rssi
= BssidInfo
->Rssi
;
477 NDIS_802_11_NETWORK_INFRASTRUCTURE NetworkType
= BssidInfo
->InfrastructureMode
;
478 CHAR SsidBuffer
[NDIS_802_11_LENGTH_SSID
+ 1];
480 /* SSID member is a non-null terminated ASCII string */
481 RtlCopyMemory(SsidBuffer
, Ssid
->Ssid
, Ssid
->SsidLength
);
482 SsidBuffer
[Ssid
->SsidLength
] = 0;
484 _tprintf(_T("\nSSID: %s\n"
488 "Supported Rates (Mbps): "),
490 BssidInfo
->Privacy
== 0 ? "No" : "Yes",
491 NetworkType
== Ndis802_11IBSS
? "Adhoc" : "Infrastructure",
494 for (j
= 0; j
< NDIS_802_11_LENGTH_RATES
; j
++)
496 if (BssidInfo
->SupportedRates
[j
] != 0)
498 /* SupportedRates are in units of .5 */
499 _tprintf(_T("%d "), (BssidInfo
->SupportedRates
[j
] << 2));
504 /* Move to the next entry */
505 BssidInfo
= (PNDIS_WLAN_BSSID
)((PUCHAR
)BssidInfo
+ BssidInfo
->Length
);
509 HeapFree(GetProcessHeap(), 0, QueryOid
);
516 _tprintf(_T("\nConfigures a WLAN adapter.\n\n"
517 "WLANCONF [-c SSID [-w WEP] [-a]] [-d] [-s]\n\n"
518 " -c SSID Connects to a supplied SSID.\n"
519 " -w WEP Specifies a WEP key to use.\n"
520 " -a Specifies the target network is ad-hoc\n"
521 " -d Disconnects from the current AP.\n"
522 " -s Scans and displays a list of access points in range.\n"));
526 BOOL
ParseCmdline(int argc
, char* argv
[])
530 for (i
= 1; i
< argc
; i
++)
532 if ((argc
> 1) && (argv
[i
][0] == '-'))
536 while ((c
= *++argv
[i
]) != '\0')
564 if (!bScan
&& !bDisconnect
&& !bConnect
)
573 int main(int argc
, char* argv
[])
577 if (!ParseCmdline(argc
, argv
))
580 hAdapter
= OpenWlanAdapter();
581 if (hAdapter
== INVALID_HANDLE_VALUE
)
583 DoFormatMessage(GetLastError());
589 if (!WlanScan(hAdapter
))
591 DoFormatMessage(GetLastError());
592 CloseHandle(hAdapter
);
596 else if (bDisconnect
)
598 if (!WlanDisconnect(hAdapter
))
600 DoFormatMessage(GetLastError());
601 CloseHandle(hAdapter
);
607 if (!WlanConnect(hAdapter
))
609 DoFormatMessage(GetLastError());
610 CloseHandle(hAdapter
);
615 CloseHandle(hAdapter
);