//////////////////////////////////////////////////////////////////////// // isTxAdapter //////////////////////////////////////////////////////////////////////// // Determines whether a given device supports Tx or not. // // Params: Name - The formal name of the device. // // Returns - True if the given device supports Tx, false otherwise. //////////////////////////////////////////////////////////////////////// boolean isTxAdapter(PCHAR Name) { PAirpcapHandle Ad; CHAR Ebuf[AIRPCAP_ERRBUF_SIZE]; PAirpcapChannelInfo frequencyList, tmpFrequency; UINT numFrequencies; UINT i; // // Always return false for the Multi-Channel Aggregator // if (strncmp(Name, "\\\\.\\airpcap_any", 16) == 0) return FALSE; // // Open adapter // Ad = AirpcapOpen(Name, Ebuf); if(!Ad) { printf("Error opening the adapter: %s\n", Ebuf); return FALSE; } // // Get the list of supported channels. This returns an array of structs // that contains a flag for Tx. // if (!AirpcapGetDeviceSupportedChannels(Ad, &frequencyList, &numFrequencies)) { printf("Error retrieving list of supported channels for adapter %s\n", Name); AirpcapClose(Ad); return FALSE; } // // Loop through the array of returned channels and look for one thats // supports Tx. // for(i=0; i<numFrequencies; i++) { tmpFrequency = frequencyList + i; if (tmpFrequency->Flags & AIRPCAP_CIF_TX_ENABLED) { AirpcapClose(Ad); return TRUE; } } // // No channels supported Tx. // AirpcapClose(Ad); return FALSE; }
//////////////////////////////////////////////////////////////////////// // getTxFrequenciesByName //////////////////////////////////////////////////////////////////////// // Gets the list of supported Tx channels. // // Params: name - The formal name of the device. // frequencyList - The array of supported channels // numFrequencies - The number of elements in the frequencyList // array. Also the number of Tx channels the // device supports. // // Returns - True on success, False otherwise. //////////////////////////////////////////////////////////////////////// boolean getTxFrequenciesByName(PCHAR name, PUINT *frequencyList, PUINT numFrequencies) { PAirpcapHandle Ad; CHAR Ebuf[AIRPCAP_ERRBUF_SIZE]; boolean response; // // Open the device // Ad = AirpcapOpen(name, Ebuf); if(!Ad) { printf("Error opening the adapter: %s\n", Ebuf); frequencyList = NULL; *numFrequencies = 0; return FALSE; } // // Toss the request over to the sister function for processing. // response = getTxFrequenciesByHandle(Ad, frequencyList, numFrequencies); // // Make sure to close the adapter we opened. // AirpcapClose(Ad); return response; }
void cygwin_close(void) { // By default, when plugged in, the adapter is set in monitor mode; // Application may assume it's already in monitor mode and forget to set it // So, do not remove monitor mode. if (airpcap_handle != NULL) { AirpcapClose(airpcap_handle); } }
//////////////////////////////////////////////////////////////////////// // getAirPcapAdapter //////////////////////////////////////////////////////////////////////// // Lists the adapters and asks the user to select and adapter and a // frequency. // // Returns - Handle to the selected adapter with the channel already // set and PPI selected. //////////////////////////////////////////////////////////////////////// PAirpcapHandle getAirPcapAdapter() { PAirpcapHandle Ad; CHAR Ebuf[AIRPCAP_ERRBUF_SIZE]; UINT freq_chan = 8; UINT i, Inum, range; AirpcapDeviceDescription *AllDevs, *TmpDev; AirpcapChannelInfo tchaninfo; // // Get the device list // if(!AirpcapGetDeviceList(&AllDevs, Ebuf)) { printf("Unable to retrieve the device list: %s\n", Ebuf); return NULL; } // // Make sure that the device list is valid // if(AllDevs == NULL) { printf("No interfaces found! Make sure the airpcap software is installed and your adapter is properly plugged in.\n"); return NULL; } // // Print the list // for(TmpDev = AllDevs, i = 0; TmpDev; TmpDev = TmpDev->next) { printf("%d. ", ++i); // // If the adapter is a Tx adapter, say so. // if (isTxAdapter(TmpDev->Name)) { printf("Tx - "); } // // Print adapter name and description // printf("%s", TmpDev->Name); if(TmpDev->Description) { printf(" (%s)\n", TmpDev->Description); } else { printf(" (No description available)\n"); } } printf("\n"); // // Store the range of valid adapters for future use. // range = i; // // If there are no valid adapters // if(range == 0) { printf("\nNo interfaces found! Make sure the airpcap software is installed and your adapter is properly plugged in.\n"); AirpcapFreeDeviceList(AllDevs); return NULL; } // // Loop because the user may have selected an invalid adapter. // while (1) { // // Ask for the user to select an adapter. // printf("Enter the adapter number (1-%d): ",range); flushall(); /* kill any characters left in buffer */ scanf("%d", &Inum); // // Check if the user specified a valid adapter // if(Inum < 1 || Inum > range) { printf("Error: Invalid adapter selection.\n\n"); continue; } // // Jump to the selected adapter // for(TmpDev = AllDevs, i = 0; i < Inum-1 ;TmpDev = TmpDev->next, i++); // // Make sure the adapter is a Tx capable one. // if (!isTxAdapter(TmpDev->Name)) { printf("Error: The selected adapter does not support transmission.\n\n"); } else { break; } } // // Loop, as the user may specify an invalid channel. // while (1) { // // Ask for a channel to listen to // printf("Enter the channel or frequency: ",i); scanf("%d", &freq_chan); // // Check if the user specified a valid channel // if(freq_chan < 1 || freq_chan > 8000) { printf("\nChannel or frequency out of range.\n"); AirpcapFreeDeviceList(AllDevs); return NULL; } // // Make sure the user picked a valid Tx channel. // if (!isTxFrequencySupported(TmpDev->Name, freq_chan)) { printf("\nThe selected frequency does not support transmission.\nPlease select from the list of supported channels below:\n\n"); // // Print the list of channels the adapter supports Tx on. // printTxFrequenciesByName(TmpDev->Name); printf("\n\n"); } else { break; } } // // Open the adapter // Ad = AirpcapOpen(TmpDev->Name, Ebuf); if(!Ad) { printf("Error opening the adapter: %s\n", Ebuf); return NULL; } // // We don't need the device list any more, free it // AirpcapFreeDeviceList(AllDevs); // // Set the link layer to 802.11 plus PPI headers // if(!AirpcapSetLinkType(Ad, AIRPCAP_LT_802_11_PLUS_PPI)) { printf("Error setting the link layer: %s\n", AirpcapGetLastError(Ad)); AirpcapClose(Ad); return NULL; } // Set the channel. // If the user provides a value below 500, we assume it's a channel number, otherwise we assume it's a frequency. if(freq_chan < 500) { if(!AirpcapSetDeviceChannel(Ad, freq_chan)) { printf("Error setting the channel: %s\n", AirpcapGetLastError(Ad)); AirpcapClose(Ad); return NULL; } } else { memset(&tchaninfo, sizeof(tchaninfo), 0); tchaninfo.Frequency = freq_chan; if(!AirpcapSetDeviceChannelEx(Ad, tchaninfo)) { printf("Error setting the channel: %s\n", AirpcapGetLastError(Ad)); AirpcapClose(Ad); return NULL; } } return Ad; }
//////////////////////////////////////////////////////////////////////// // listenForProbeRequest //////////////////////////////////////////////////////////////////////// // Listens for a probe request and sends the response. // // Params: Ad - An open handle to a device. // // Returns - None. //////////////////////////////////////////////////////////////////////// void listenForProbeRequest(PAirpcapHandle Ad) { HANDLE ReadEvent; p_ppi_packet_header p_ppi; BYTE* PacketBuffer; UINT BytesReceived; // // Get the read event // if(!AirpcapGetReadEvent(Ad, &ReadEvent)) { printf("Error getting the read event: %s\n", AirpcapGetLastError(Ad)); AirpcapClose(Ad); exitApp(-1); } // // Allocate a 256k packet buffer // PacketBuffer = (BYTE*)malloc(PACKET_BUFFER_SIZE); if(!PacketBuffer) { printf("No memory for the packet buffer\n"); AirpcapClose(Ad); exitApp(-1); } // // Everything is ok! // Look for Probe Request // while (TRUE) { // capture the packets if(!AirpcapRead(Ad, PacketBuffer, PACKET_BUFFER_SIZE, &BytesReceived)) { printf("Error receiving packets: %s\n", AirpcapGetLastError(Ad)); free(PacketBuffer); AirpcapClose(Ad); exitApp(-1); } // Look for Echo Request. When found, take it from there. if(getProbeRequest(Ad, PacketBuffer, BytesReceived, &p_ppi)) { sendProbeResponse(Ad, p_ppi); } // wait until some packets are available. This prevents polling and keeps the CPU low. WaitForSingleObject(ReadEvent, WAIT_INTERVAL_MS); } // // Make sure we clean up after // free(PacketBuffer); // // Exit, printing the exit message // exitApp(0); }