Prev: How to build an OS image which can interact with cellular emulator
Next: GPSID and altitudeWRTSeaLevel in win mobile 6.1
From: Abhi on 26 Jun 2008 20:35 Hi, I am trying to connect to WPA2 using WZCSetInterfaceEx. Unfortunately, OpenNETCFdoes not provide a wrapper to connect to WPA2 as far as I know. So I have to use the native API to do so. Anyway, I looked at the wzctool.cpp from WinCE installation and I selected the relevant lines to connect to WPA2. I am doing WZCPassword2Key for the keyMaterial before passing it to WZCSetInterfaceEx. Is this ok? Anyway, I am not able to connect using my below program, but I was able to do so using the phone UI. Could anyone please let me know what I could be doing wrong? Thanks, Abhi #include <windows.h> #include <stdio.h> #include <stdarg.h> #include <eapol.h> #include <wzcsapi.h> #include <ntddndis.h> WCHAR g_WirelessCard1[MAX_PATH] = L""; // 1st wireless card found by WZC query void GetFirstWirelessNetworkCard // find the first wireless network cards // found card name goes to g_WirelessCard1 ( // arg none ) { g_WirelessCard1[0] = L'\0'; INTFS_KEY_TABLE IntfsTable; IntfsTable.dwNumIntfs = 0; IntfsTable.pIntfs = NULL; DWORD dwStatus = WZCEnumInterfaces(NULL, &IntfsTable); if(dwStatus != ERROR_SUCCESS) { wprintf(L"WZCEnumInterfaces() error 0x%08X\n", dwStatus); return; } // print the GUIDs // note that in CE the GUIDs are simply the device instance name // i.e XWIFI11B1, CISCO1, ISLP2, ... // if(!IntfsTable.dwNumIntfs) { wprintf(L"system has no wireless card.\n"); return; } wcsncpy(g_WirelessCard1, IntfsTable.pIntfs[0].wszGuid, MAX_PATH-1); wprintf(L"wireless card found: %s\n", g_WirelessCard1); // need to free memory allocated by WZC for us. LocalFree(IntfsTable.pIntfs); } // GetFirstWirelessNetworkCard void InterpretEncryptionKeyValue ( IN OUT WZC_WLAN_CONFIG& wzcConfig1, IN WCHAR *szEncryptionKey, IN BOOL& bNeed8021X // this becomes TRUE if szEncryptionKey is "auto" ) { wzcConfig1.KeyLength = wcslen(szEncryptionKey); if((wzcConfig1.KeyLength<8) || (wzcConfig1.KeyLength>63)) { wprintf(L"WPA-PSK/TKIP key should be 8-63 char long string \n"); return; } // WPA/TKIP pre-shared key takes 256 bit key. // Everything else is incorrect format. // Translates a user password (8 to 63 ascii chars) into a 256 bit network key. // We do this for WPA-PSK and WPA-None. char szEncryptionKeyValue8[64]; // longest key is 63 memset(szEncryptionKeyValue8, 0, sizeof(szEncryptionKeyValue8)); WideCharToMultiByte(CP_ACP, 0, szEncryptionKey, wzcConfig1.KeyLength+1, szEncryptionKeyValue8, wzcConfig1.KeyLength+1, NULL, NULL); WZCPassword2Key(&wzcConfig1, szEncryptionKeyValue8); wzcConfig1.dwCtlFlags |= WZCCTL_WEPK_XFORMAT | WZCCTL_WEPK_PRESENT | WZCCTL_ONEX_ENABLED; wzcConfig1.EapolParams.dwEapFlags = EAPOL_ENABLED; wzcConfig1.EapolParams.dwEapType = DEFAULT_EAP_TYPE; wzcConfig1.EapolParams.bEnable8021x = TRUE; wzcConfig1.WPAMCastCipher = Ndis802_11Encryption2Enabled; } // InterpretEncryptionKeyValue() void AddToPreferredNetworkList // adding to the [Preferred Networks] // [Preferred Networks] is a list of SSIDs in preference order. // WZC continuously scans available SSIDs and attempt to connect to the most preferable SSID. ( IN WCHAR *szWiFiCard, IN WZC_WLAN_CONFIG& wzcConfig1, IN WCHAR *szSsidToConnect ) { DWORD dwOutFlags = 0; INTF_ENTRY_EX Intf; memset(&Intf, 0x00, sizeof(INTF_ENTRY_EX)); Intf.wszGuid = szWiFiCard; DWORD dwStatus = WZCQueryInterfaceEx( NULL, INTF_ALL, &Intf, &dwOutFlags); if(dwStatus) { wprintf(L"WZCQueryInterfaceEx() error dwStatus=0x%0X, dwOutFlags=0x%0X", dwStatus, dwOutFlags); WZCDeleteIntfObjEx(&Intf); return; } WZC_802_11_CONFIG_LIST *pConfigList = (PWZC_802_11_CONFIG_LIST)Intf.rdStSSIDList.pData; if(!pConfigList) // empty [Preferred Networks] list case { DWORD dwDataLen = sizeof(WZC_802_11_CONFIG_LIST); WZC_802_11_CONFIG_LIST *pNewConfigList = (WZC_802_11_CONFIG_LIST *)LocalAlloc(LPTR, dwDataLen); pNewConfigList->NumberOfItems = 1; pNewConfigList->Index = 0; memcpy(pNewConfigList->Config, &wzcConfig1, sizeof(wzcConfig1)); Intf.rdStSSIDList.pData = (BYTE*)pNewConfigList; Intf.rdStSSIDList.dwDataLen = dwDataLen; } else { ULONG uiNumberOfItems = pConfigList->NumberOfItems; for(UINT i=0; i<uiNumberOfItems; i++) { if(memcmp(&wzcConfig1.Ssid, &pConfigList->Config[i].Ssid, sizeof(NDIS_802_11_SSID)) == 0) { wprintf(L"%s is already in the [Preferred Networks] list", szSsidToConnect); WZCDeleteIntfObjEx(&Intf); return; } } wprintf(L"SSID List has [%d] entries.\n", uiNumberOfItems); wprintf(L"adding %s to the top of [Preferred Networks]\n", szSsidToConnect); // this will be the most preferable SSID DWORD dwDataLen = sizeof(WZC_802_11_CONFIG_LIST) + (uiNumberOfItems+1)*sizeof(WZC_WLAN_CONFIG); WZC_802_11_CONFIG_LIST *pNewConfigList = (WZC_802_11_CONFIG_LIST *)LocalAlloc(LPTR, dwDataLen); pNewConfigList->NumberOfItems = uiNumberOfItems + 1; pNewConfigList->Index = 0; memcpy(pNewConfigList->Config, &wzcConfig1, sizeof(wzcConfig1)); if(pConfigList->NumberOfItems) { pNewConfigList->Index = pConfigList->Index; memcpy(pNewConfigList->Config+1, pConfigList->Config, (uiNumberOfItems)*sizeof(WZC_WLAN_CONFIG)); LocalFree(pConfigList); pConfigList = NULL; } Intf.rdStSSIDList.pData = (BYTE*)pNewConfigList; Intf.rdStSSIDList.dwDataLen = dwDataLen; } dwStatus = WZCSetInterfaceEx(NULL, INTF_PREFLIST, &Intf, &dwOutFlags); if(dwStatus) wprintf(L"WZCSetInterfaceEx() error dwStatus=0x%0X, dwOutFlags=0x%0X", dwStatus, dwOutFlags); WZCDeleteIntfObjEx(&Intf); } // AddToPreferredNetworkList() void DoConnectWPA2 // wzctool -c cisco1 -ssid TEST1 -auth wpa-psk -encr tkip -key abcdefgh ( IN WCHAR *szSsidToConnect, IN WCHAR *szEncryptionKey ) { WZC_WLAN_CONFIG wzcConfig1; memset(&wzcConfig1, 0, sizeof(wzcConfig1)); wzcConfig1.Length = sizeof(wzcConfig1); wzcConfig1.dwCtlFlags = 0; WCHAR *szWiFiCard = NULL; GetFirstWirelessNetworkCard(); if(!*g_WirelessCard1) // wifi card not found return; szWiFiCard = g_WirelessCard1; // SSID wzcConfig1.Ssid.SsidLength = wcslen(szSsidToConnect); for(UINT i=0; i<wzcConfig1.Ssid.SsidLength; i++) wzcConfig1.Ssid.Ssid[i] = (char)szSsidToConnect[i]; // infrastructure net. assume that it will never be ad hoc wzcConfig1.InfrastructureMode =Ndis802_11Infrastructure; // Authentication assume wpa2psk wzcConfig1.AuthenticationMode = Ndis802_11AuthModeWPA2PSK; // Encryption wzcConfig1.Privacy = Ndis802_11WEPDisabled; WCHAR *szEncryptionMode = NULL; //else if(!_wcsicmp(szEncryptionMode, L"tkip")) wzcConfig1.Privacy = Ndis802_11Encryption2Enabled; //else if(!_wcsicmp(szEncryptionMode, L"aes")) //wzcConfig1.Privacy = Ndis802_11Encryption3Enabled; // Key BOOL bNeed8021X = FALSE; InterpretEncryptionKeyValue(wzcConfig1, szEncryptionKey, bNeed8021X); AddToPreferredNetworkList(szWiFiCard, wzcConfig1, szSsidToConnect); } // DoConnect() int wmain // main function for WCHAR console application ( IN int argc, // number of args IN WCHAR* argv[] // arg array ) { DoConnectWPA2(L"cellport", L"abbreviations"); return 0; } // wmain() |