HRESULT CWlanManager::ForceStopHostedNetwork() { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; Lock(); if (!m_Initialized) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } if (m_HostedNetworkState != wlan_hosted_network_active) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } // // Stop hosted network // dwError = WlanHostedNetworkForceStop( m_WlanHandle, NULL, NULL ); BAIL_ON_WIN32_ERROR(dwError, hr); error: Unlock(); return hr; }
HRESULT CWlanManager::StartHostedNetwork() { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; Lock(); if (!m_Initialized) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } // if (wlan_hosted_network_active == m_HostedNetworkState) // { // BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); // } // // Start hosted network // dwError = WlanHostedNetworkStartUsing( m_WlanHandle, NULL, NULL ); BAIL_ON_WIN32_ERROR(dwError, hr); error: Unlock(); return hr; }
// Pre-association when the profile does not have the key. DWORD WINAPI DoMissingKeyWepPreAssociate ( PADAPTER_DETAILS pAdapterDetails, DWORD* pdwReasonCode ) { DWORD dwResult = ERROR_SUCCESS; DWORD dwKeyLen = ERROR_SUCCESS; BYTE* pbKeyData = NULL; ASSERT( pAdapterDetails ); ASSERT( pdwReasonCode ); // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_BAD_USER_KEY; // Possible enhancement - try to call Dot11ExtGetProfileCustomUserData // function in IHV Framework to see if the key is // available there. Else, send UI Request. // try to obtain the key through an UI request. dwResult = SendUIRequestToReceiveKey ( pAdapterDetails, &dwKeyLen, &pbKeyData ); BAIL_ON_WIN32_ERROR(dwResult); ASSERT ( pbKeyData ); ASSERT ( dwKeyLen ); // use the key for connection. dwResult = DoWepPreAssociateCommon ( pAdapterDetails, dwKeyLen, pbKeyData, pdwReasonCode ); BAIL_ON_WIN32_ERROR(dwResult); // Possible enhancement - try to call Dot11ExtSetProfileCustomUserData // to store the key if the key was obtained by a UI request. error: PrivateMemoryFree( pbKeyData ); return dwResult; }
HRESULT CWlanManager::GetHostedNetworkInterfaceGuid( GUID& InterfaceGuid ) { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; PWLAN_HOSTED_NETWORK_STATUS pAPStatus = NULL; // hosted network status // // get the hosted network status // dwError = WlanHostedNetworkQueryStatus( m_WlanHandle, &pAPStatus, NULL // reserved ); BAIL_ON_WIN32_ERROR(dwError, hr); InterfaceGuid = pAPStatus->IPDeviceID; error: if (pAPStatus != NULL) { WlanFreeMemory(pAPStatus); pAPStatus = NULL; } return hr; }
HRESULT CWlanManager::GetStaionList( CRefObjList<CWlanStation*>& StationList ) { HRESULT hr = S_OK; Lock(); if (!m_Initialized) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } if (m_HostedNetworkState != wlan_hosted_network_active) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } // // Remove old entries // StationList.RemoveAllEntries(); // // Copy all the stations to the list // for (size_t i = 0; i < m_StationList.GetCount(); i++) { CWlanStation* pStation = m_StationList.GetAt(m_StationList.FindIndex(i)); _ASSERT(pStation != NULL); pStation->AddRef(); StationList.AddTail(pStation); } error: Unlock(); return hr; }
HRESULT CWlanManager::SetHostedNetworkKey( CAtlString& strKey ) { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; UCHAR strKeyBuf[WLAN_MAX_NAME_LENGTH]; DWORD dwKeyBufLen = WLAN_MAX_NAME_LENGTH; dwError = ConvertPassPhraseKeyStringToBuffer( strKey, strKey.GetLength(), DOT11_AUTH_ALGO_RSNA_PSK, strKeyBuf, &dwKeyBufLen ); BAIL_ON_WIN32_ERROR(dwError, hr); if (0 == dwKeyBufLen) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_PARAMETER, hr); } dwError = WlanHostedNetworkSetSecondaryKey( m_WlanHandle, dwKeyBufLen, strKeyBuf, TRUE, // passphrase TRUE, // persistent NULL, // not interested in failure reason NULL // reserved ); BAIL_ON_WIN32_ERROR(dwError, hr); error: return hr; }
HRESULT CWlanManager::SetHostedNetworkName( CAtlString& strSsid ) { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; DOT11_SSID ssid, oldSsid; // // Convert string to SSID // dwError = StringToSsid(strSsid, &ssid); BAIL_ON_WIN32_ERROR(dwError, hr); Lock(); if (m_Initialized) { // save old SSID oldSsid = m_HostedNetworkConnSettings.hostedNetworkSSID; m_HostedNetworkConnSettings.hostedNetworkSSID = ssid; // set the new connection setttings dwError = WlanHostedNetworkSetProperty( m_WlanHandle, wlan_hosted_network_opcode_connection_settings, sizeof( WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS), (PVOID)&m_HostedNetworkConnSettings, NULL, NULL ); if (dwError != ERROR_SUCCESS) { // revert back to old SSID m_HostedNetworkConnSettings.hostedNetworkSSID = oldSsid; hr = HRESULT_FROM_WIN32(dwError); } } else { hr = HRESULT_FROM_WIN32(ERROR_INVALID_STATE); } Unlock(); error: return hr; }
DWORD ConvertHexCharToNibble ( CHAR chData, BOOL bUpper, PBYTE pbtData ) { DWORD dwResult = ERROR_SUCCESS; BYTE btNibble = 0xF0; ASSERT( pbtData ); if ( (chData >= '0') && (chData <= '9') ) { btNibble = (BYTE) (chData - '0'); } else if ( (chData >= 'a') && (chData <= 'f') ) { btNibble = (BYTE) (chData - 'a') + 0xA; } else if ( (chData >= 'A') && (chData <= 'F') ) { btNibble = (BYTE) (chData - 'A') + 0xA; } else { // Wrong type input dwResult = ERROR_BAD_FORMAT; BAIL_ON_WIN32_ERROR( dwResult ); } ASSERT( btNibble < 0xF0 ); if ( bUpper ) { (*pbtData) = (*pbtData) & 0x0F; (*pbtData) = (*pbtData) | (btNibble << 4); } else { (*pbtData) = (*pbtData) & 0xF0; (*pbtData) = (*pbtData) | btNibble; } error: return dwResult; }
// // This function is responsible for the post association // operations and completing the post association call // for WEP scenario. // DWORD WINAPI DoWepPostAssociate ( LPVOID pvPostAssociate ) { DWORD dwResult = ERROR_SUCCESS; DWORD dwStatus = ERROR_SUCCESS; DWORD dwReasonCode = L2_REASON_CODE_IHV_INVALID_STATE; BOOL bLocked = FALSE; PPOST_ASSOC_DATA ppostAssocData = (PPOST_ASSOC_DATA) pvPostAssociate; PADAPTER_DETAILS pAdapterDetails = NULL; ASSERT( ppostAssocData ); EnterCriticalSection( &g_csSynch ); bLocked = TRUE; dwResult = ReferenceAdapterDetails ( ppostAssocData->hIhvExtAdapter, &pAdapterDetails ); BAIL_ON_WIN32_ERROR( dwResult ); ASSERT( pAdapterDetails ); if ( nic_state_post_assoc_started != pAdapterDetails->NicState ) { dwResult = ERROR_INVALID_STATE; BAIL_ON_WIN32_ERROR( dwResult ); } // This could be an appropriate place to modify the current profile if ( pAdapterDetails->bModifyCurrentProfile ) { pAdapterDetails->bModifyCurrentProfile = FALSE; dwResult = (g_pDot11ExtApi->Dot11ExtSetCurrentProfile) ( pAdapterDetails->hDot11SvcHandle, pAdapterDetails->hConnectSession, &(g_IhvDiscoveryProfiles[1].IhvConnectivityProfile), &(g_IhvDiscoveryProfiles[1].IhvSecurityProfile) ); BAIL_ON_WIN32_ERROR( dwResult ); } // In wep connection case, function only changes the adapter state. pAdapterDetails->NicState = nic_state_post_assoc_ended; // Reason Code is set to success. dwReasonCode = L2_REASON_CODE_SUCCESS; error: if ( pAdapterDetails ) { DerefenceAdapterDetails( ppostAssocData->hIhvExtAdapter ); } if ( bLocked ) { LeaveCriticalSection( &g_csSynch ); } // call completion function. dwStatus = (g_pDot11ExtApi->Dot11ExtPostAssociateCompletion) ( ppostAssocData->hDot11SvcHandle, ppostAssocData->hSecuritySessionId, NULL, dwReasonCode, dwResult ); if ( ERROR_SUCCESS != dwStatus ) { // IHV specific logging can happen here. } PrivateMemoryFree( ppostAssocData ); return dwResult; }
// Pre-association when the profile does have the key. DWORD WINAPI DoWepPreAssociate ( PADAPTER_DETAILS pAdapterDetails, DWORD* pdwReasonCode ) { DWORD dwResult = ERROR_SUCCESS; BOOL bLocked = FALSE; DWORD dwKeyLen = ERROR_SUCCESS; CHAR szKey[MAX_RESPONSE_SIZE+1] = {0}; ASSERT( pAdapterDetails ); ASSERT( pdwReasonCode ); EnterCriticalSection( &g_csSynch ); bLocked = TRUE; // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_BAD_USER_KEY; if ( (!( pAdapterDetails->pConnectivityProfile )) || (!( pAdapterDetails->pConnectivityProfile->pszParam2 )) || ( 0 == pAdapterDetails->pConnectivityProfile->pszParam2[0] ) ) { dwResult = ERROR_INVALID_STATE; BAIL_ON_WIN32_ERROR(dwResult); } // Convert the profile string to a usable key. dwKeyLen = (DWORD)wcslen( pAdapterDetails->pConnectivityProfile->pszParam2 ); dwKeyLen = sizeof(WCHAR) * ( 1 + dwKeyLen ); if ( dwKeyLen > sizeof( szKey ) ) { dwResult = ERROR_BAD_PROFILE; BAIL_ON_WIN32_ERROR(dwResult); } // Copy string. CopyMemory ( (BYTE*) szKey, pAdapterDetails->pConnectivityProfile->pszParam2, dwKeyLen ); // Convert UNICODE to ASCII. dwResult = ConvertStringToKey ( (BYTE*) szKey, &dwKeyLen ); BAIL_ON_WIN32_ERROR(dwResult); ASSERT ( dwKeyLen ); // Do pre-association with the key in the profile. dwResult = DoWepPreAssociateCommon ( pAdapterDetails, dwKeyLen, (BYTE*) szKey, pdwReasonCode ); BAIL_ON_WIN32_ERROR(dwResult); error: if ( bLocked ) { LeaveCriticalSection( &g_csSynch ); } return dwResult; }
// // Perform Wep based pre-association once the key is known. // DWORD WINAPI DoWepPreAssociateCommon ( PADAPTER_DETAILS pAdapterDetails, DWORD dwKeyLen, BYTE* pbKeyData, DWORD* pdwReasonCode ) { DWORD dwResult = ERROR_SUCCESS; BOOL bLocked = FALSE; LONG lIndex = 0; PDOT11_CIPHER_DEFAULT_KEY_VALUE pKey = 0; ULONG uLen = 0; ASSERT( pAdapterDetails ); ASSERT( dwKeyLen ); ASSERT( pbKeyData ); ASSERT( pdwReasonCode ); // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_OUTOFMEMORY; uLen = FIELD_OFFSET(DOT11_CIPHER_DEFAULT_KEY_VALUE, ucKey) + dwKeyLen * sizeof(UCHAR); pKey = (PDOT11_CIPHER_DEFAULT_KEY_VALUE) PrivateMemoryAlloc(uLen); if (!pKey) { dwResult = ERROR_OUTOFMEMORY; BAIL_ON_WIN32_ERROR(dwResult); } CopyMemory( &(pKey->ucKey), pbKeyData, dwKeyLen ); // Prepare the key. pKey->AlgorithmId = DOT11_CIPHER_ALGO_WEP; pKey->usKeyLength = (USHORT) dwKeyLen; pKey->bStatic = TRUE; pKey->Header.Type = NDIS_OBJECT_TYPE_DEFAULT; pKey->Header.Revision = DOT11_CIPHER_DEFAULT_KEY_VALUE_REVISION_1; pKey->Header.Size = sizeof(DOT11_CIPHER_DEFAULT_KEY_VALUE); EnterCriticalSection( &g_csSynch ); bLocked = TRUE; // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_INVALID_STATE; if ( nic_state_pre_assoc_started != pAdapterDetails->NicState ) { dwResult = ERROR_INVALID_STATE; BAIL_ON_WIN32_ERROR( dwResult ); } // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_HARDWARE_FAILURE; // plumb the settings and keys down. TRACE_MESSAGE( "Setting Auth Algorithm." ); dwResult = (g_pDot11ExtApi->Dot11ExtSetAuthAlgorithm) ( pAdapterDetails->hDot11SvcHandle, DOT11_AUTH_ALGO_80211_OPEN ); BAIL_ON_WIN32_ERROR(dwResult); TRACE_MESSAGE( "Setting Unicast cipher algorithm." ); dwResult = (g_pDot11ExtApi->Dot11ExtSetUnicastCipherAlgorithm) ( pAdapterDetails->hDot11SvcHandle, DOT11_CIPHER_ALGO_WEP ); BAIL_ON_WIN32_ERROR(dwResult); TRACE_MESSAGE( "Setting exclude unencrypted flag." ); dwResult = (g_pDot11ExtApi->Dot11ExtSetExcludeUnencrypted) ( pAdapterDetails->hDot11SvcHandle, TRUE ); BAIL_ON_WIN32_ERROR(dwResult); for ( lIndex = MAX_KEY_INDEX; lIndex >= MIN_KEY_INDEX; lIndex-- ) { pKey->uKeyIndex = lIndex; TRACE_MESSAGE( "Setting default key." ); dwResult = (g_pDot11ExtApi->Dot11ExtSetDefaultKey) ( pAdapterDetails->hDot11SvcHandle, pKey, DOT11_DIR_BOTH ); BAIL_ON_WIN32_ERROR(dwResult); } TRACE_MESSAGE( "Setting default key ID." ); dwResult = (g_pDot11ExtApi->Dot11ExtSetDefaultKeyId) ( pAdapterDetails->hDot11SvcHandle, 0 ); BAIL_ON_WIN32_ERROR(dwResult); // Verified before, just after acquiring lock. ASSERT( nic_state_pre_assoc_started == pAdapterDetails->NicState ); pAdapterDetails->NicState = nic_state_pre_assoc_ended; // Reason code is set to SUCCESS. (*pdwReasonCode) = L2_REASON_CODE_SUCCESS; // register the post-association handlers with the adapter. pAdapterDetails->pPerformPostAssociateCompletionRoutine = DoWepPostAssociate; pAdapterDetails->pPerformPostAssociateRoutine = NULL; pAdapterDetails->pStopPostAssociateRoutine = NULL; error: if ( bLocked ) { LeaveCriticalSection( &g_csSynch ); } PrivateMemoryFree( pKey ); return dwResult; }
// Send the UI request and wait for the UI response. DWORD SendUIRequestToReceiveKey ( PADAPTER_DETAILS pAdapterDetails, DWORD* pdwKeyLen, BYTE** ppbKeyData ) { DWORD dwResult = ERROR_SUCCESS; DOT11EXT_IHV_UI_REQUEST uiRequest = {0}; PIHV_UI_REQUEST pIHVRequest = NULL; CHAR szTitle[] = UI_TITLE_STRING; CHAR szHelp[] = UI_HELP_STRING; BOOL bLocked = FALSE; HANDLE hUIResponse = NULL; // CLSID of COM class that implements the UI page. In a real // implementation this GUID could be dynamically obtained. CLSID uiPageClsid = { /* 4A01F9F9-6012-4343-A8C4-10B5DF32672A */ 0x4A01F9F9, 0x6012, 0x4343, {0xA8, 0xC4, 0x10, 0xB5, 0xDF, 0x32, 0x67, 0x2A} }; ASSERT( pAdapterDetails ); // prepare the IHV request. uiRequest.dwByteCount = sizeof(IHV_UI_REQUEST); uiRequest.pvUIRequest = (BYTE*) PrivateMemoryAlloc( sizeof(IHV_UI_REQUEST) ); if ( !(uiRequest.pvUIRequest) ) { dwResult = ERROR_OUTOFMEMORY; BAIL( ); } pIHVRequest = (IHV_UI_REQUEST*)uiRequest.pvUIRequest; memcpy( pIHVRequest->szTitle , szTitle , sizeof(szTitle) ); memcpy( pIHVRequest->szHelp , szHelp , sizeof(szHelp) ); uiRequest.dwSessionId = WTSGetActiveConsoleSessionId( ); uiRequest.UIPageClsid = uiPageClsid; // acquire the lock to register the request. EnterCriticalSection( &g_csSynch ); bLocked = TRUE; // create new request guid. dwResult = UuidCreate( &(uiRequest.guidUIRequest) ); BAIL_ON_WIN32_ERROR(dwResult); // free the existing response. PrivateMemoryFree( pAdapterDetails->pbResponse ); pAdapterDetails->pbResponse = NULL; // register the guid. pAdapterDetails->currentGuidUIRequest = uiRequest.guidUIRequest; // Initializing the event this thread // would be waiting on later. ResetEvent( pAdapterDetails->hUIResponse ); hUIResponse = pAdapterDetails->hUIResponse; // leave the lock since this thread needs // to wait for the response. LeaveCriticalSection( &g_csSynch ); bLocked = FALSE; // send the request. dwResult = (g_pDot11ExtApi->Dot11ExtSendUIRequest) ( pAdapterDetails->hDot11SvcHandle, &uiRequest ); BAIL_ON_WIN32_ERROR(dwResult); TRACE_MESSAGE( "Sent UI request to receive key." ); // Waiting for UI response. // This would be triggered // off if no UI response // is received. dwResult = WaitForSingleObject ( hUIResponse, 1000 * 60 * 5 // 5 minutes ); // acquire the lock - required for both success and failure. EnterCriticalSection( &g_csSynch ); bLocked = TRUE; ZeroMemory( &(pAdapterDetails->currentGuidUIRequest), sizeof( GUID ) ); if ( WAIT_OBJECT_0 == dwResult ) { dwResult = ERROR_SUCCESS; } BAIL_ON_WIN32_ERROR(dwResult); if ( NULL == pAdapterDetails->pbResponse ) { dwResult = ERROR_INVALID_STATE; BAIL_ON_WIN32_ERROR(dwResult); } // At this point in the code a response // has been received, and the thread // has not been aborted. (*ppbKeyData) = pAdapterDetails->pbResponse; pAdapterDetails->pbResponse = NULL; (*pdwKeyLen) = pAdapterDetails->dwResponseLen; // Convert the Unicode string to ASCII. dwResult = ConvertStringToKey ( *ppbKeyData, pdwKeyLen ); BAIL_ON_WIN32_ERROR(dwResult); pAdapterDetails->bModifyCurrentProfile = TRUE; error: if ( bLocked ) { LeaveCriticalSection( &g_csSynch ); } PrivateMemoryFree( uiRequest.pvUIRequest ); return dwResult; }
HRESULT CWlanManager::GetHostedNetworkKey( CAtlString& strKey ) { HRESULT hr = S_OK; DWORD dwError = ERROR_SUCCESS; BOOL bIsPassPhrase = FALSE; BOOL bPersistent = FALSE; PUCHAR pucSecondaryKey = NULL; DWORD dwSecondaryKeyLength = 0; WCHAR strSecondaryKey[WLAN_MAX_NAME_LENGTH]; // get the user security key dwError = WlanHostedNetworkQuerySecondaryKey( m_WlanHandle, &dwSecondaryKeyLength, &pucSecondaryKey, &bIsPassPhrase, &bPersistent, NULL, NULL ); BAIL_ON_WIN32_ERROR(dwError, hr); int cchKey = 0; if (dwSecondaryKeyLength > 0) { // Must be passphrase _ASSERT(bIsPassPhrase); // convert the key if (bIsPassPhrase) { #pragma prefast(suppress:26035, "If the key is a pass phrase, it is guaranteed to be null-terminated.") cchKey = MultiByteToWideChar( CP_ACP, MB_ERR_INVALID_CHARS, (LPCSTR)pucSecondaryKey, dwSecondaryKeyLength, strSecondaryKey, sizeof(strSecondaryKey) / sizeof(strSecondaryKey[0]) -1 ); } } if(cchKey == 0) { // secondary key is not set or not passphrase // set a temporary one CAtlString strTmpKey = L"HostedNetwork12345"; hr = SetHostedNetworkKey(strTmpKey); BAIL_ON_FAILURE(hr); strKey = strTmpKey; } else { // got the key strKey = strSecondaryKey; } error: if (pucSecondaryKey != NULL) { WlanFreeMemory(pucSecondaryKey); pucSecondaryKey = NULL; } return hr; }
// no op function for preassociation when // ihv is used only for connectivity. in a // realistic implementation there would probably // be calls to Dot11ExtNicSpecificExtension // in this function to prepare the driver for // additional connectivity settings. DWORD WINAPI DoIhvConnPreAssociate ( PADAPTER_DETAILS pAdapterDetails, DWORD* pdwReasonCode ) { DWORD dwResult = ERROR_SUCCESS; DWORD dwKeyLen = 0; PBYTE pbKeyData = NULL; BOOL bLocked = FALSE; ASSERT( pAdapterDetails ); ASSERT( pdwReasonCode ); // try to send UI request to obtain some data that could be useful here. // the sample does not really use the data. dwResult = SendUIRequestToReceiveKey ( pAdapterDetails, &dwKeyLen, &pbKeyData ); BAIL_ON_WIN32_ERROR(dwResult); ASSERT ( pbKeyData ); PrivateMemoryFree( pbKeyData ); pbKeyData = NULL; // try to send UI request to obtain some data that could be useful here. // the sample does not really use the data. dwResult = SendUIRequestToReceiveKey ( pAdapterDetails, &dwKeyLen, &pbKeyData ); BAIL_ON_WIN32_ERROR(dwResult); ASSERT ( pbKeyData ); EnterCriticalSection( &g_csSynch ); bLocked = TRUE; // Reason code is set before making calls that could fail. (*pdwReasonCode) = L2_REASON_CODE_IHV_INVALID_STATE; if ( nic_state_pre_assoc_started != pAdapterDetails->NicState ) { dwResult = ERROR_INVALID_STATE; BAIL_ON_WIN32_ERROR( dwResult ); } pAdapterDetails->NicState = nic_state_pre_assoc_ended; // Reason code is set to SUCCESS. (*pdwReasonCode) = L2_REASON_CODE_SUCCESS; pAdapterDetails->pPerformPostAssociateCompletionRoutine = NULL; pAdapterDetails->pPerformPostAssociateRoutine = NULL; pAdapterDetails->pStopPostAssociateRoutine = NULL; error: if ( bLocked ) { LeaveCriticalSection( &g_csSynch ); } PrivateMemoryFree( pbKeyData ); return dwResult; }
HRESULT CWlanManager::AdviseHostedNetworkNotification( CHostedNetworkNotificationSink * pSink ) { HRESULT hr = S_OK; Lock(); if (NULL == pSink) { BAIL_ON_HRESULT_ERROR(hr, E_INVALIDARG); } if (!m_Initialized) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_STATE, hr); } if (m_NotificationSink != NULL) { BAIL_ON_HRESULT_ERROR(hr, E_FAIL); } // // Create callback complete event if needed // if (NULL == m_CallbackComplete) { m_CallbackComplete = CreateEvent( NULL, FALSE, // manual reset TRUE, // initial state NULL ); if (NULL == m_CallbackComplete) { BAIL_ON_LAST_ERROR(hr); } } // // Set notification sink // m_NotificationSink = pSink; // // check if hosted network is supported // if (wlan_hosted_network_unavailable == m_HostedNetworkState) { pSink->OnHostedNetworkNotAvailable(); } // // Check if hosted network is enabled // if (wlan_hosted_network_active == m_HostedNetworkState) { for (unsigned int i = 0; i < m_StationList.GetCount(); i++) { CWlanStation * pStation = m_StationList.GetAt(m_StationList.FindIndex(i)); _ASSERT(pStation != NULL); pSink->OnStationJoin(pStation); } } error: Unlock(); return hr; }
DWORD ConvertStringToKey ( BYTE* pbKeyData, DWORD* pdwKeyLen ) { DWORD dwResult = ERROR_SUCCESS; HRESULT hr = S_OK; CHAR szKey[ MAX_KEY_STRING_LENGTH + 2 ] = {0}; DWORD dwKeyStringLen = 0; DWORD dwIndex = 0; if ( (!pbKeyData) || (!pdwKeyLen) || ( 0 == (*pdwKeyLen) ) || ( (*pdwKeyLen) % sizeof( WCHAR ) ) ) { dwResult = ERROR_INVALID_PARAMETER; BAIL_ON_WIN32_ERROR( dwResult ); } dwKeyStringLen = (DWORD) wcslen( (LPWSTR) pbKeyData ); if ( MAX_KEY_STRING_LENGTH < dwKeyStringLen ) { dwResult = ERROR_BAD_FORMAT; BAIL_ON_WIN32_ERROR( dwResult ); } // Converting the UNICODE string to // ANSI string in scratch pad. hr = StringCchPrintfA ( szKey, MAX_KEY_STRING_LENGTH + 1, "%S", (WCHAR*) pbKeyData ); BAIL_ON_FAILURE( hr ); ASSERT( dwKeyStringLen == (DWORD) strlen( szKey )); if ( ( 5 == dwKeyStringLen ) || ( 13 == dwKeyStringLen ) ) { // Copying the ANSI string back to original buffer. hr = StringCchPrintfA ( (CHAR*) pbKeyData, (*pdwKeyLen), "%s", szKey ); BAIL_ON_FAILURE( hr ); // The strings are direct representations // of the Wep Key and can be directly returned. (*pdwKeyLen) = dwKeyStringLen; TRACE_MESSAGE_VAL( "Received WEP key of length = ", (*pdwKeyLen) ); BAIL( ); } if (( 10 != dwKeyStringLen ) && ( 26 != dwKeyStringLen )) { // Wrong length input dwResult = ERROR_BAD_FORMAT; BAIL_ON_WIN32_ERROR( dwResult ); } for( dwIndex = 0; dwIndex < (dwKeyStringLen / 2); dwIndex++ ) { dwResult = ConvertHexCharToNibble ( szKey[ 2 * dwIndex ], TRUE, &(pbKeyData[ dwIndex ]) ); BAIL_ON_WIN32_ERROR( dwResult ); dwResult = ConvertHexCharToNibble ( szKey[ 1 + (2 * dwIndex) ], FALSE, &(pbKeyData[ dwIndex ]) ); BAIL_ON_WIN32_ERROR( dwResult ); } (*pdwKeyLen) = dwKeyStringLen / 2; TRACE_MESSAGE_VAL( "Received WEP key of length = ", (*pdwKeyLen) ); error: return WIN32_COMBINED_ERROR( dwResult, hr ); }
HRESULT CWlanManager::Init() { HRESULT hr = S_OK; DWORD retCode = ERROR_SUCCESS; DWORD dwDataSize = 0; BOOL *pbMode = NULL; // whether hosted network is allowed or not PWLAN_HOSTED_NETWORK_CONNECTION_SETTINGS pConnSettings = NULL; // hosted network connectivity settings PWLAN_HOSTED_NETWORK_SECURITY_SETTINGS pSecSettings = NULL; // hosted network security settings PWLAN_HOSTED_NETWORK_STATUS pAPStatus = NULL; // hosted network status WLAN_OPCODE_VALUE_TYPE valueType; Lock(); if (m_Initialized) { // // no-op because it is already initialized // BAIL(); } // open a wlan handle first retCode = WlanOpenHandle( WLAN_API_VERSION, NULL, // reserved &m_ServerVersion, &m_WlanHandle ); BAIL_ON_WIN32_ERROR(retCode, hr); // register notifications retCode = WlanRegisterNotification( m_WlanHandle, WLAN_NOTIFICATION_SOURCE_HNWK, TRUE, &CWlanManager::WlanNotificationCallback, this, NULL, // reserved NULL ); BAIL_ON_WIN32_ERROR(retCode, hr); // // Initialize the hosted network. // It is a no-op if the hosted network is already initialized. // Bail out if it fails. // retCode = WlanHostedNetworkInitSettings( m_WlanHandle, NULL, NULL // reserved ); BAIL_ON_WIN32_ERROR(retCode, hr); // // Is hosted network enabled? // retCode = WlanHostedNetworkQueryProperty( m_WlanHandle, wlan_hosted_network_opcode_enable, &dwDataSize, (PVOID *)&pbMode, &valueType, NULL // reserved ); BAIL_ON_WIN32_ERROR(retCode, hr); if(!pbMode || dwDataSize < sizeof(BOOL)) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_DATA, hr); } // // get the hosted network connectivity settings // retCode = WlanHostedNetworkQueryProperty( m_WlanHandle, wlan_hosted_network_opcode_connection_settings, &dwDataSize, (PVOID *)&pConnSettings, &valueType, NULL // reserved ); BAIL_ON_WIN32_ERROR(retCode, hr); if( !pConnSettings || dwDataSize < sizeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS)) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_DATA, hr); } // // get the hosted network seucrity settings // retCode = WlanHostedNetworkQueryProperty( m_WlanHandle, wlan_hosted_network_opcode_security_settings, &dwDataSize, (PVOID *)&pSecSettings, &valueType, NULL // reserved ); BAIL_ON_WIN32_ERROR(retCode, hr); if( !pSecSettings || dwDataSize < sizeof(WLAN_HOSTED_NETWORK_SECURITY_SETTINGS)) { BAIL_ON_WIN32_ERROR(ERROR_INVALID_DATA, hr); } // // get the hosted network status // retCode = WlanHostedNetworkQueryStatus( m_WlanHandle, &pAPStatus, NULL // reserved ); BAIL_ON_WIN32_ERROR(retCode, hr); // // save the values // m_HostedNetworkAllowed = *pbMode; m_HostedNetworkConnSettings = *pConnSettings; m_HostedNetworkSecSettings = *pSecSettings; m_HostedNetworkState = pAPStatus->HostedNetworkState; // // add existing stations if the hosted network has started already // if (wlan_hosted_network_active == pAPStatus->HostedNetworkState) { for (DWORD i = 0; i < pAPStatus->dwNumberOfPeers; i++) { OnStationJoin(pAPStatus->PeerList[i]); } } m_Initialized = true; error: if (retCode != ERROR_SUCCESS && m_WlanHandle != NULL) { // // Close WLAN handle in failure cases // WlanCloseHandle(m_WlanHandle, NULL); m_WlanHandle = NULL; } Unlock(); if (pbMode != NULL) { WlanFreeMemory(pbMode); pbMode = NULL; } if (pConnSettings != NULL) { WlanFreeMemory(pConnSettings); pConnSettings = NULL; } if (pSecSettings != NULL) { WlanFreeMemory(pSecSettings); pSecSettings = NULL; } if (pAPStatus != NULL) { WlanFreeMemory(pAPStatus); pAPStatus = NULL; } return hr; }