// Get a list of Wifi Networks from the last scan
std::vector<NetworkInfo> getWifiNetworks(HANDLE hClient, GUID ifGuid) {
	// Return variable
	std::vector<NetworkInfo> ns;

	// Result variable
	DWORD dwResult;

	// List of interfaces
	PWLAN_INTERFACE_INFO_LIST ifList = NULL;
	// Interface info
	PWLAN_INTERFACE_INFO ifInfo = NULL;
	// Service list
	PWLAN_BSS_LIST bssList = NULL;
	// Network service
	PWLAN_BSS_ENTRY bssEntry = NULL;

	// Retrieve a list of available networks
	dwResult = WlanGetNetworkBssList(hClient, &ifGuid, NULL, dot11_BSS_type_any, 0, NULL, &bssList);
	if (dwResult != ERROR_SUCCESS) {
	}
	else {
		// For each network
		for (unsigned int j = 0; j < bssList->dwNumberOfItems; j++) {
			// Grab the network
			bssEntry = (WLAN_BSS_ENTRY *)&bssList->wlanBssEntries[j];

			// Network info struct
			NetworkInfo ni;
			// SSID
			for (int k = 0, l = bssEntry->dot11Ssid.uSSIDLength; k < 32; k++) {
				if (k < l) ni.ssid[k] = (int)bssEntry->dot11Ssid.ucSSID[k];
				else ni.ssid[k] = 0;
			}
			// Hardware MAC Address
			for (int k = 0, l = 6; k < 32; k++) {
				if (k < l) ni.bssid[k] = (int)bssEntry->dot11Bssid[k];
				else ni.bssid[k] = 0;
			}
			// Channel Frequency
			ni.frequency = bssEntry->ulChCenterFrequency;
			// Rssi
			ni.rssi = bssEntry->lRssi;
			// Signal quality
			ni.quality = bssEntry->uLinkQuality;

			// Add to the list
			ns.push_back(ni);
		}
	}

	// Clean up
	if (bssList != NULL) {
		WlanFreeMemory(bssList);
		bssList = NULL;
	}

	// Finish
	return ns;
}
int get_ap_rssi_data(RADIOMAP &result_map)
{
	HANDLE hClient = NULL;
	DWORD dwMaxClient = 2;
	DWORD dwCurVersion = 0;
	DWORD dwResult = 0;
	DWORD dwRetVal = 0;
	WCHAR GuidString[39] = { 0 };
	PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
	PWLAN_INTERFACE_INFO pIfInfo = NULL;
	PWLAN_BSS_ENTRY pBssEntry = NULL;
	PWLAN_BSS_LIST pBssList = NULL;
	LocalizationNode *pLocalizationNode = NULL;
	std::string mac_id;
	std::string ret_ssid;
	int iRet = 0;
	int ret;
	uint i;

	dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
	if (dwResult != ERROR_SUCCESS)
	{
		wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
		return 1;
	}
	dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
	if (dwResult != ERROR_SUCCESS)
	{
		wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
		return 1;
	}
	else
	{
		wprintf(L"Nunber Entries: %lu\n", pIfList->dwNumberOfItems);
		wprintf(L"Current Index: %lu\n", pIfList->dwIndex);
		for (i = 0; i < (int)pIfList->dwNumberOfItems; i++)
		{
			pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[i];
			wprintf(L" Interface Index[%u]:\t %lu\n", i, i);
			iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
				sizeof(GuidString) / sizeof(*GuidString));

			if (iRet == 0)
				wprintf(L"StringFromGUID2 Failed\n");
			else
				wprintf(L"  InterfaceGUID[%d]: %ws\n", i, GuidString);

			wprintf(L"  Interface Description[%d]: %ws", i,
				pIfInfo->strInterfaceDescription);
			wprintf(L"\n");
			wprintf(L"  Interface State[%d]:\t ", i);
			switch (pIfInfo->isState) {
			case wlan_interface_state_not_ready:
				wprintf(L"Not ready\n");
				break;
			case wlan_interface_state_connected:
				wprintf(L"Connected\n");
				break;
			case wlan_interface_state_ad_hoc_network_formed:
				wprintf(L"First node in a ad hoc network\n");
				break;
			case wlan_interface_state_disconnecting:
				wprintf(L"Disconnecting\n");
				break;
			case wlan_interface_state_disconnected:
				wprintf(L"Not connected\n");
				break;
			case wlan_interface_state_associating:
				wprintf(L"Attempting to associate with a network\n");
				break;
			case wlan_interface_state_discovering:
				wprintf(L"Auto configuration is discovering settings for the network\n");
				break;
			case wlan_interface_state_authenticating:
				wprintf(L"In process of authenticating\n");
				break;
			default:
				wprintf(L"Unknown state %ld\n", pIfInfo->isState);
				break;
			}
			wprintf(L"\n");

			const GUID * pGUID = &pIfInfo->InterfaceGuid;
			dwResult = WlanGetNetworkBssList(hClient, pGUID, NULL, dot11_BSS_type_any, 0, NULL, &pBssList);
			if (dwResult != ERROR_SUCCESS)
			{
				wprintf(L"Wlan get network bss list error: %lu\n", dwResult);
				return 1;
			}
			else
			{
				for (uint i = 0; i < pBssList->dwNumberOfItems; i++)
				{
					pBssEntry = &pBssList->wlanBssEntries[i];
					get_mac_id(pBssEntry->dot11Bssid, mac_id);
					if (find_at_radiomap(mac_id, result_map, pLocalizationNode) == 0)
					{
						pLocalizationNode->add_recoder(pBssEntry->lRssi);
					}
					else if (get_ssid(pBssEntry->dot11Ssid, ret_ssid) == 0)
					{
						LocalizationNode new_ap(mac_id, ret_ssid, pBssEntry->lRssi);
						ret = add_to_radiomap(new_ap, result_map);
						if (ret != 0)
						{
							printf("ERROR: add_to_radiomap error");
							exit(1);
						}
					}
				}
			}
		}
	}
	if (pBssList != NULL)
	{
		WlanFreeMemory(pBssList);
		pBssList = NULL;
	}
	if (pIfList != NULL)
	{
		WlanFreeMemory(pIfList);
		pIfList = NULL;
	}
	WlanCloseHandle(hClient, 0);
	return 0;
}
Beispiel #3
0
int foo() {

  WindowsNdisApi* w = WindowsNdisApi::Create();
  std::vector<AccessPoint> apData;
  w->GetAccessPointData(apData);


  ///////////////////////////////////////////////////////////
	HANDLE hClient;
	PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
	PWLAN_INTERFACE_INFO pIfConnInfo = NULL;
	PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;

	PWLAN_BSS_LIST pBssList=NULL;
	PWLAN_BSS_ENTRY  pBssEntry=NULL;
	WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;

	DWORD dwResult = 0;
	DWORD dwMaxClient = 2;         
	DWORD dwCurVersion = 0;
	DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);

	int i;

	// Initialise the Handle
	dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
	if (dwResult != ERROR_SUCCESS) 
	{    
		return 0;
	}

	// Get the Interface List
	dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
	if (dwResult != ERROR_SUCCESS) 
	{    
		return 0;
	}

	//Loop through the List to find the connected Interface
	PWLAN_INTERFACE_INFO pIfInfo = NULL;
	for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) 
	{
		pIfInfo = (WLAN_INTERFACE_INFO *) & pIfList->InterfaceInfo[i];    
		if (pIfInfo->isState == wlan_interface_state_connected) 
		{
			pIfConnInfo = pIfInfo;
			break;
		}
	}

	if ( pIfConnInfo == NULL )
		return 0;

	// Query the Interface
	dwResult = WlanQueryInterface(hClient,&pIfConnInfo->InterfaceGuid,wlan_intf_opcode_current_connection,NULL,&connectInfoSize,(PVOID *) &pConnectInfo,&opCode);
	if (dwResult != ERROR_SUCCESS) 
	{    
		return 0;
	}

	// Scan the connected SSID
	dwResult = WlanScan(hClient, &pIfConnInfo->InterfaceGuid,
		                NULL/*&pConnectInfo->wlanAssociationAttributes.dot11Ssid*/,
						NULL, NULL);
	if (dwResult != ERROR_SUCCESS) 
	{    
		return 0;
	}

	// Get the BSS Entry
	dwResult = WlanGetNetworkBssList(hClient, &pIfConnInfo->InterfaceGuid,
		NULL /*&pConnectInfo->wlanAssociationAttributes.dot11Ssid */,
		dot11_BSS_type_any,
		TRUE, NULL, &pBssList);

	if (dwResult != ERROR_SUCCESS) 
	{    
		return 0;
	}

	// Get the RSSI value
	pBssEntry=&pBssList->wlanBssEntries[0];
	return pBssEntry->lRssi;
}
Beispiel #4
0
int _tmain(int argc, _TCHAR* argv[])
{
	bool first_run=true;
	DWORD dwRetVal = 0;
	while(1)
	{
	/*Test per funzione di errore*/
	std::map<std::string, int> lMacAddress_TEST;
	
	// Declare and initialize variables.

    HANDLE hClient = NULL;		//Handle del client utilizzato nella sessione corrente
    DWORD dwMaxClient = 2;      //E' una costante che va inserita in WlanOpenHandle (1->Win XP SP2/3 | 2->Win Vista/Server 2008)    
    DWORD dwCurVersion = 0;		//Versione della WLAN API attualmente utilizzata da inserire nella WlanOpenHandle
    DWORD dwResult = 0;
    
    int iRet = 0;
    
    WCHAR GuidString[39] = {0};		//Conterrà un GUID sottoforma di stringa di caratteri

    unsigned int i, j, k;

    /* variables used for WlanEnumInterfaces  */

    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;	//Contiene un array di NIC con relative informazioni
    PWLAN_INTERFACE_INFO pIfInfo = NULL;		//Contiene le informazioni relative ad una NIC (ID, Descrizione, Stato)

    PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;	//Contiene un array di reti con relative informazioni
    PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;		//Contiene info relative ad una rete raggiungibile (es. SSID, qualità segnale, algoritmo cifratura)
    
    int iRSSI = 0;

    dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); //Apre una connessione con il server
    if (dwResult != ERROR_SUCCESS)
		{
        wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
        return 1;
        // You can use FormatMessage here to find out why the function failed
		}

    dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); //Inserisce in pIfList l'elenco delle NIC abilitate sul pc
    if (dwResult != ERROR_SUCCESS) 
		{
        wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
        return 1;
        // You can use FormatMessage here to find out why the function failed
		} 
	else 
		{
        wprintf(L"Num Entries: %lu\n", pIfList->dwNumberOfItems); //Numero NIC trovate
        wprintf(L"Current Index: %lu\n", pIfList->dwIndex);	//Indice NIC corrente
        for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) 
			{
            pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[i]; //pIfInfo punta alla struttura con le informazioni relative alla NIC corrente
            wprintf(L"  Interface Index[%u]:\t %lu\n", i, i);
            iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString,	//Trasforma un GUID in stringa di caratteri 
                sizeof(GuidString)/sizeof(*GuidString));  //Numero di caratteri massimo per la stringa finale
            // For c rather than C++ source code, the above line needs to be
            // iRet = StringFromGUID2(&pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, 
            //     sizeof(GuidString)/sizeof(*GuidString)); 
            if (iRet == 0)	//Se StringFromGUID2 fallisce ritorna 0
                wprintf(L"StringFromGUID2 failed\n");
            else 
				{		//Altrimenti il numero di caratteri della stringa 
                wprintf(L"  InterfaceGUID[%d]: %ws\n",i, GuidString);
				}    
            wprintf(L"  Interface Description[%d]: %ws", i, pIfInfo->strInterfaceDescription);
            wprintf(L"\n");
            wprintf(L"  Interface State[%d]:\t ", i);
            switch (pIfInfo->isState) 
				{
				case wlan_interface_state_not_ready:
					wprintf(L"Not ready\n");
					break;
				case wlan_interface_state_connected:
					wprintf(L"Connected\n");
					break;
				case wlan_interface_state_ad_hoc_network_formed:
					wprintf(L"First node in a ad hoc network\n");
					break;
				case wlan_interface_state_disconnecting:
					wprintf(L"Disconnecting\n");
					break;
				case wlan_interface_state_disconnected:
					wprintf(L"Not connected\n");
					break;
				case wlan_interface_state_associating:
					wprintf(L"Attempting to associate with a network\n");
					break;
				case wlan_interface_state_discovering:
					wprintf(L"Auto configuration is discovering settings for the network\n");
					break;
				case wlan_interface_state_authenticating:
					wprintf(L"In process of authenticating\n");
					break;
				default:
					wprintf(L"Unknown state %ld\n", pIfInfo->isState);
					break;
				}
            wprintf(L"\n");

			//Inserisce in pBssList i dati delle reti disponibili
            dwResult = WlanGetAvailableNetworkList(hClient,		//Client handle ottenuto all'inizio
                                             &pIfInfo->InterfaceGuid,	//Interface GUID
                                             0, 
                                             NULL, 
                                             &pBssList);	//Puntatore alla lista delle entry trovate

            if (dwResult != ERROR_SUCCESS) 
				{
                wprintf(L"WlanGetAvailableNetworkList failed with error: %u\n", dwResult);
                dwRetVal = 1;
                // You can use FormatMessage to find out why the function failed
				} 
			else 
				{
                wprintf(L"WLAN_AVAILABLE_NETWORK_LIST for this interface\n");

                wprintf(L"  Num Entries: %lu\n\n", pBssList->dwNumberOfItems);

                for (j = 0; j < pBssList->dwNumberOfItems; j++) 
					{
                    pBssEntry = (WLAN_AVAILABLE_NETWORK *) & pBssList->Network[j]; //pBssEntry punta alla struttura di informazioni relative alla rete corrente
                    wprintf(L"  Profile Name[%u]:  %ws\n", j, pBssEntry->strProfileName);
                    wprintf(L"  SSID[%u]:\t\t ", j);
                    if (pBssEntry->dot11Ssid.uSSIDLength == 0) //Se l'SSID ha lunghezza nulla va a capo
                        wprintf(L"\n");
                    else 
						{   
                        for (k = 0; k < pBssEntry->dot11Ssid.uSSIDLength; k++) 
							{
                            wprintf(L"%c", (int) pBssEntry->dot11Ssid.ucSSID[k]); //Scrittura a video dell SSID
							}
                        wprintf(L"\n");
						}
					
					//Prova per ottenere il MAC Address-----------------------------------------------
					//--------------------------------------------------------------------------------
					PWLAN_BSS_LIST pWlanBssList=NULL;
					DWORD uscita=0;
					DOT11_SSID Dot11Ssid=pBssEntry->dot11Ssid;
					
					WlanGetNetworkBssList(
						hClient,		//Client inizializzato precedentemente
						&(pIfInfo->InterfaceGuid),	//Interfaccia sul quale si fanno le misurazioni
						&(pBssEntry->dot11Ssid),	//Struttura del SSID correntemente analizzato
						dot11_BSS_type_infrastructure, //Cerca tra le interfacce di infrastruttura
						pBssEntry->bSecurityEnabled,	//Security policy dell'interfaccia corrente
						NULL,	//DEVE ESSERE NULL
						&pWlanBssList	//Struttura che sarà riempita con info opportune
						);
					if(uscita==ERROR_SUCCESS)
					wprintf(L"\n  GOOD Exit status Bss\n");
					wprintf(L"\n  Number of Bss entries: %u\n", pWlanBssList->dwNumberOfItems);
					//for(int z=0; z<(int)pWlanBssList->dwNumberOfItems;z++)
					//{
						PWLAN_BSS_ENTRY px = (WLAN_BSS_ENTRY *) & pWlanBssList->wlanBssEntries;
						std::string sMACAddress = PrintMACaddress(px->dot11Bssid);
					//}
					//std::cout << sMACAddress;

					/*----------------------------------------------------------------------------------*/
					/*----------------------------------------------------------------------------------*/
                    wprintf(L"  BSS Network type[%u]:\t ", j);
                    switch (pBssEntry->dot11BssType) 
						{
						case dot11_BSS_type_infrastructure   :
							wprintf(L"Infrastructure (%u)\n", pBssEntry->dot11BssType);
							break;
						case dot11_BSS_type_independent:
							wprintf(L"Infrastructure (%u)\n", pBssEntry->dot11BssType);
							break;
						default:
							wprintf(L"Other (%lu)\n", pBssEntry->dot11BssType);
							break;
						}
                                
                    wprintf(L"  Number of BSSIDs[%u]:\t %u\n", j, pBssEntry->uNumberOfBssids);	
                    wprintf(L"  Connectable[%u]:\t ", j);
                    if (pBssEntry->bNetworkConnectable)
                        wprintf(L"Yes\n");
                    else 
						{
                        wprintf(L"No\n");
                        wprintf(L"  Not connectable WLAN_REASON_CODE value[%u]:\t %u\n", j, pBssEntry->wlanNotConnectableReason);
						}        
                    wprintf(L"  Number of PHY types supported[%u]:\t %u\n", j, pBssEntry->uNumberOfPhyTypes);

                    if (pBssEntry->wlanSignalQuality == 0)
                        iRSSI = -100;
                    else if (pBssEntry->wlanSignalQuality == 100)   
                        iRSSI = -50;
                    else
                        iRSSI = -100 + (pBssEntry->wlanSignalQuality/2);    
                        
                    wprintf(L"  Signal Quality[%u]:\t %u (RSSI: %i dBm)\n", j, pBssEntry->wlanSignalQuality, iRSSI);
					//Prova con memorizzazione dei MAC
					lMacAddress_TEST.insert(std::make_pair(sMACAddress,iRSSI));
                    if (first_run==true)
					{
						luogo.InsertElement(sMACAddress,iRSSI);
					}
					wprintf(L"  Security Enabled[%u]:\t ", j);
                    if (pBssEntry->bSecurityEnabled)
                        wprintf(L"Yes\n");
                    else
                        wprintf(L"No\n");
         
                    wprintf(L"  Default AuthAlgorithm[%u]: ", j);
                    switch (pBssEntry->dot11DefaultAuthAlgorithm) 
						{
						case DOT11_AUTH_ALGO_80211_OPEN:
							wprintf(L"802.11 Open (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_80211_SHARED_KEY:
							wprintf(L"802.11 Shared (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_WPA:
							wprintf(L"WPA (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_WPA_PSK:
							wprintf(L"WPA-PSK (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_WPA_NONE:
							wprintf(L"WPA-None (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_RSNA:
							wprintf(L"RSNA (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						case DOT11_AUTH_ALGO_RSNA_PSK:
							wprintf(L"RSNA with PSK(%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						default:
							wprintf(L"Other (%lu)\n", pBssEntry->dot11DefaultAuthAlgorithm);
							break;
						}
                        
                    wprintf(L"  Default CipherAlgorithm[%u]: ", j);
                    switch (pBssEntry->dot11DefaultCipherAlgorithm) 
						{
						case DOT11_CIPHER_ALGO_NONE:
							wprintf(L"None (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						case DOT11_CIPHER_ALGO_WEP40:
							wprintf(L"WEP-40 (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						case DOT11_CIPHER_ALGO_TKIP:
							wprintf(L"TKIP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						case DOT11_CIPHER_ALGO_CCMP:
							wprintf(L"CCMP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						case DOT11_CIPHER_ALGO_WEP104:
							wprintf(L"WEP-104 (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						case DOT11_CIPHER_ALGO_WEP:
							wprintf(L"WEP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						default:
							wprintf(L"Other (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
							break;
						}

                    wprintf(L"  Flags[%u]:\t 0x%x", j, pBssEntry->dwFlags);
                    if (pBssEntry->dwFlags) 
						{
                        if (pBssEntry->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
                            wprintf(L" - Currently connected");
                        if (pBssEntry->dwFlags & WLAN_AVAILABLE_NETWORK_HAS_PROFILE)
                            wprintf(L" - Has profile");
						}   
                    wprintf(L"\n");
                    
                    wprintf(L"\n");
					if (pWlanBssList!=NULL)
					{
        WlanFreeMemory(pWlanBssList);	//Libera la zona di memoria preposta alla lista di reti disponibili appena utilizzata
        pWlanBssList = NULL;
		}
                }
            }
        }

    }
    if (pBssList != NULL) 
		{
        WlanFreeMemory(pBssList);	//Libera la zona di memoria preposta alla lista di reti disponibili appena utilizzata
        pBssList = NULL;
		}
	

    if (pIfList != NULL) 
		{
        WlanFreeMemory(pIfList);	//Libera la zona di memoria preposta alla lista di NIC appena utilizzata
        pIfList = NULL;
		}
	
		if (first_run==true)
		{
			first_run=false;
		}
		else
		{
			float error;
			error=luogo.ErrorValue(lMacAddress_TEST);
			wprintf(L"Errore con i dati memorizzati: %f\n\n", error);
		}
		WlanCloseHandle(hClient, NULL);
	system("pause");
	}
	return dwRetVal;
}