3 * Giorgio Tino <giorgio.tino@cacetech.com>
4 * Copyright (c) CACE Technologies, LLC 2006
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2000 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later*/
16 #include <epan/crypt/dot11decrypt_ws.h>
17 #include <epan/strutil.h>
18 #include <wsutil/file_util.h>
19 #include <wsutil/frequency-utils.h>
21 #include <caputils/airpcap.h>
22 #include <caputils/airpcap_loader.h>
26 * Set to TRUE if the DLL was successfully loaded AND all functions
29 static gboolean AirpcapLoaded = FALSE;
33 * We load dynamically the dag library in order link it only when
34 * it's present on the system
36 static void * AirpcapLib = NULL;
38 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
39 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
40 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
41 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
42 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
43 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
44 static AirpcapReadHandler g_PAirpcapRead;
45 static AirpcapGetStatsHandler g_PAirpcapGetStats;
48 static int AirpcapVersion = 3;
50 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
51 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
52 static AirpcapOpenHandler g_PAirpcapOpen;
53 static AirpcapCloseHandler g_PAirpcapClose;
54 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
55 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
56 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
57 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
58 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
59 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
60 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
61 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
62 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
63 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
64 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
65 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
66 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
67 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
68 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
69 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
70 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
71 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
72 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
73 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
74 static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
75 static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
76 static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
78 /* Airpcap interface list */
79 GList *g_airpcap_if_list = NULL;
81 /* Airpcap current selected interface */
82 airpcap_if_info_t *airpcap_if_selected = NULL;
84 /* Airpcap current active interface */
85 airpcap_if_info_t *airpcap_if_active = NULL;
87 Dot11Channel *pSupportedChannels;
88 guint numSupportedChannels;
90 static AirpcapChannelInfo LegacyChannels[] =
108 static guint num_legacy_channels = 14;
111 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
112 * "get_airpcap_interface_list()".
115 cant_get_airpcap_if_list_error_message(const char *err_str)
117 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
121 * Airpcap wrapper, used to store the current settings for the selected adapter
124 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
126 if (!AirpcapLoaded) return FALSE;
127 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
131 * Airpcap wrapper, used to open an airpcap adapter
134 airpcap_if_open(gchar * name, gchar * err)
136 if (!AirpcapLoaded) return NULL;
137 if (name == NULL) return NULL;
138 return g_PAirpcapOpen(name,err);
142 * Airpcap wrapper, used to close an airpcap adapter
145 airpcap_if_close(PAirpcapHandle handle)
147 if (!AirpcapLoaded) return;
148 g_PAirpcapClose(handle);
152 * Retrieve the state of the Airpcap DLL
155 airpcap_get_dll_state(void)
157 return AirpcapVersion;
161 * Airpcap wrapper, used to turn on the led of an airpcap adapter
164 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
166 if (!AirpcapLoaded) return FALSE;
167 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
171 * Airpcap wrapper, used to turn off the led of an airpcap adapter
174 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
176 if (!AirpcapLoaded) return FALSE;
177 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
181 * Airpcap wrapper, used to get the channel of an airpcap adapter
184 airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
186 if (!AirpcapLoaded) return FALSE;
187 return g_PAirpcapGetDeviceChannel(ah,ch);
191 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
194 airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
196 if (!AirpcapLoaded) return FALSE;
197 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
198 *nInfo = num_legacy_channels;
199 *cInfo = (AirpcapChannelInfo*)&LegacyChannels;
202 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
203 return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
209 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
212 airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
214 AirpcapChannelInfo *chanInfo;
215 guint i=0, j=0, numInfo = 0;
219 if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
221 numSupportedChannels = 0;
224 * allocate a bigger array
229 pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
231 for (i = 0; i < numInfo; i++)
233 guint supportedChannel = G_MAXUINT;
236 * search if we have it already
238 for (j = 0; j < numSupportedChannels; j++)
240 if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
242 supportedChannel = j;
247 if (supportedChannel == G_MAXUINT)
250 * not found, create a new item
252 pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
254 switch(chanInfo[i].ExtChannel)
257 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
260 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
264 pSupportedChannels[numSupportedChannels].Flags = 0;
268 * Gather channel information
271 pSupportedChannels[numSupportedChannels].Flags |=
272 FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
273 FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
274 pSupportedChannels[numSupportedChannels].Channel =
275 ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
276 numSupportedChannels++;
281 * just update the ext channel flags
283 switch(chanInfo[i].ExtChannel)
286 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
289 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
298 if (numSupportedChannels < 1)
301 * Now sort the list by frequency
303 for (i = 0 ; i < numSupportedChannels - 1; i++)
305 for (j = i + 1; j < numSupportedChannels; j++)
307 if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
309 Dot11Channel temp = pSupportedChannels[i];
310 pSupportedChannels[i] = pSupportedChannels[j];
311 pSupportedChannels[j] = temp;
316 *pNumSupportedChannels = numSupportedChannels;
317 return pSupportedChannels;
321 * Airpcap wrapper, used to set the channel of an airpcap adapter
324 airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
326 if (!AirpcapLoaded) return FALSE;
327 return g_PAirpcapSetDeviceChannel(ah,ch);
331 * Airpcap wrapper, used to set the frequency of an airpcap adapter
334 airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
336 if (!AirpcapLoaded) return FALSE;
337 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
339 channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
344 return airpcap_if_set_device_channel(ah, channel);
346 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
347 return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
354 * Airpcap wrapper, used to get the frequency of an airpcap adapter
357 airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
359 if (!AirpcapLoaded) return FALSE;
361 pChannelInfo->Frequency = 0;
362 pChannelInfo->ExtChannel = 0;
363 pChannelInfo->Reserved[0] = 0;
364 pChannelInfo->Reserved[1] = 0;
365 pChannelInfo->Reserved[2] = 0;
367 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
371 if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
373 chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
374 if (chan_freq == 0) return FALSE;
375 pChannelInfo->Frequency = chan_freq;
378 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
379 return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
385 * Airpcap wrapper, used to get the link type of an airpcap adapter
388 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
390 if (!AirpcapLoaded) return FALSE;
391 return g_PAirpcapGetLinkType(ah,lt);
395 * Airpcap wrapper, used to set the link type of an airpcap adapter
398 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
400 if (!AirpcapLoaded) return FALSE;
401 return g_PAirpcapSetLinkType(ah,lt);
405 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
408 airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
410 if (!AirpcapLoaded) return FALSE;
411 return g_PAirpcapGetFcsPresence(ah,fcs);
415 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
418 airpcap_if_set_fcs_presence(PAirpcapHandle ah, gboolean fcs)
420 if (!AirpcapLoaded) return FALSE;
421 return g_PAirpcapSetFcsPresence(ah,fcs);
425 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
428 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
430 if (!AirpcapLoaded) return FALSE;
431 return g_PAirpcapGetDecryptionState(ah,PEnable);
435 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
438 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
440 if (!AirpcapLoaded) return FALSE;
441 return g_PAirpcapSetDecryptionState(ah,Enable);
445 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
448 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
450 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
451 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
455 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
458 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
460 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
461 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
465 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
468 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
470 if (!AirpcapLoaded) return FALSE;
471 return g_PAirpcapGetFcsValidation(ah,val);
475 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
478 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
480 if (!AirpcapLoaded) return FALSE;
481 return g_PAirpcapSetFcsValidation(ah,val);
485 * Airpcap wrapper, used to save the settings for the selected_if
488 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
490 if (!AirpcapLoaded) return FALSE;
491 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
495 * Airpcap wrapper, used to save the settings for the selected_if
498 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
500 if (!AirpcapLoaded) return FALSE;
501 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
505 * Airpcap wrapper, used to save the driver's set of keys
508 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
510 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
511 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
515 * Airpcap wrapper, used to load the driver's set of keys
518 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
520 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
521 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
525 * This function will create a new airpcap_if_info_t using a name and a description
528 airpcap_if_info_new(char *name, char *description)
531 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
533 airpcap_if_info_t *if_info = NULL;
535 /* Probably I have to switch on the leds!!! */
536 ad = airpcap_if_open(name, ebuf);
539 if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
540 if_info->name = g_strdup(name);
541 if (description == NULL){
542 if_info->description = NULL;
544 if_info->description = g_strdup(description);
547 if_info->ip_addr = NULL;
548 if_info->loopback = FALSE;
549 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
550 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
551 airpcap_if_get_link_type(ad,&(if_info->linkType));
552 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
553 if_info->pSupportedChannels = airpcap_if_get_device_supported_channels_array(ad, &(if_info->numSupportedChannels));
554 airpcap_if_turn_led_on(ad, 0);
555 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
557 if_info->blinking = FALSE;
558 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
560 /* get the keys, if everything is ok, close the adapter */
561 if (airpcap_if_load_keys(ad,if_info))
563 airpcap_if_close(ad);
570 * This function will create a new fake drivers' interface, to load global keys...
573 airpcap_driver_fake_if_info_new(void)
576 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
578 airpcap_if_info_t *if_info = NULL;
579 airpcap_if_info_t *fake_if_info = NULL;
581 /* Maybe for some reason no airpcap adapter is found */
582 if (g_airpcap_if_list == NULL)
586 * Retrieve the first AirPcap adapter available. If no interface is found,
587 * it is not possible to retrieve the driver's settings, so return NULL.
589 if_info = (airpcap_if_info_t *)g_list_nth_data(g_airpcap_if_list,0);
593 /* Open the 'fake' adapter */
594 ad = airpcap_if_open(if_info->name, ebuf);
597 fake_if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
598 fake_if_info->name = g_strdup(if_info->name);
599 fake_if_info->description = g_strdup(if_info->description);
600 fake_if_info->loopback = FALSE;
601 fake_if_info->ip_addr = NULL;
602 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
603 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
604 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
605 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
606 airpcap_if_get_device_channel_ex(ad,&(fake_if_info->channelInfo));
607 airpcap_if_turn_led_on(ad, 0);
608 fake_if_info->led = TRUE;
609 fake_if_info->blinking = FALSE;
610 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
612 /* get the keys, if everything is ok, close the adapter */
613 if (airpcap_if_load_driver_keys(ad,fake_if_info))
615 airpcap_if_close(ad);
624 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
627 airpcap_if_info_print(airpcap_if_info_t* if_info)
632 g_print("\nWARNING : AirPcap Interface pointer is NULL.\n");
636 g_print("\n----------------- AirPcap Interface \n");
637 g_print(" NAME: %s\n",if_info->name);
638 g_print(" DESCRIPTION: %s\n",if_info->description);
639 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
640 g_print(" channelInfo.Frequency: %u\n",if_info->channelInfo.Frequency);
641 g_print(" channelInfo.ExtChannel: %d\n",if_info->channelInfo.ExtChannel);
642 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
643 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
644 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
645 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
646 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
647 g_print(" KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
648 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
649 g_print(" LINKTYPE: %d\n",if_info->linkType);
650 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
651 g_print(" (GTK) TAG: %d\n",if_info->tag);
652 g_print("SUPPORTED CHANNELS POINTER: %p\n",if_info->pSupportedChannels);
653 g_print(" NUM SUPPORTED CHANNELS: %u\n",if_info->numSupportedChannels);
655 for(i=0; i<(if_info->numSupportedChannels); i++){
656 g_print("\n SUPPORTED CHANNEL #%u\n",i+1);
657 g_print(" CHANNEL: %u\n",if_info->pSupportedChannels[i].Channel);
658 g_print(" FREQUENCY: %u\n",if_info->pSupportedChannels[i].Frequency);
659 g_print(" FLAGS: %u\n",if_info->pSupportedChannels[i].Flags);
663 #endif /* AIRPCAP_DEBUG */
666 * Function used to load the WEP keys for a selected interface
669 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
671 if (!if_info) return FALSE;
673 if_info->keysCollectionSize = 0;
674 if_info->keysCollection = NULL;
676 if (!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
678 if (if_info->keysCollectionSize == 0)
680 if_info->keysCollection = NULL;
681 airpcap_if_close(ad);
685 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
686 if (!if_info->keysCollection)
688 if_info->keysCollectionSize = 0;
689 if_info->keysCollection = NULL;
690 airpcap_if_close(ad);
694 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
698 airpcap_if_close(ad);
703 * Function used to load the WEP keys for a selected interface
706 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
708 if_info->keysCollectionSize = 0;
709 if_info->keysCollection = NULL;
711 if (!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
713 if (if_info->keysCollectionSize == 0)
715 if_info->keysCollection = NULL;
716 airpcap_if_close(ad);
720 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
721 if (!if_info->keysCollection)
723 if_info->keysCollectionSize = 0;
724 if_info->keysCollection = NULL;
725 airpcap_if_close(ad);
729 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
733 airpcap_if_close(ad);
738 * Function used to save the WEP keys for a selected interface
741 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
743 if (!if_info || !AirpcapLoaded) return;
745 if (if_info->keysCollection != NULL)
746 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
750 * Function used to save the WEP keys for a selected interface
753 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
755 if (if_info->keysCollection != NULL)
756 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
760 * Callback used to free an instance of airpcap_if_info_t
763 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
765 airpcap_if_info_t *if_info = (airpcap_if_info_t *)data;
770 g_free(if_info->name);
772 g_free(if_info->description);
774 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
775 if (if_info->keysCollection != NULL)
777 g_free(if_info->keysCollection);
778 if_info->keysCollection = NULL;
781 g_slist_free(if_info->ip_addr);
787 * Function used to free the airpcap interface list
790 free_airpcap_interface_list(GList *if_list)
792 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
793 g_list_free(if_list);
797 * This function will use the airpcap.dll to find all the airpcap devices.
798 * Will return null if no device is found.
801 get_airpcap_interface_list(int *err, char **err_str)
804 airpcap_if_info_t *if_info;
806 AirpcapDeviceDescription *devsList, *adListEntry;
807 char errbuf[AIRPCAP_ERRBUF_SIZE];
813 *err = AIRPCAP_NOT_LOADED;
817 if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
819 /* No interfaces, return il = NULL; */
820 *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
822 *err_str = cant_get_airpcap_if_list_error_message(errbuf);
829 adListEntry = devsList;
834 adListEntry = adListEntry->next;
839 /* No interfaces, return il= NULL */
840 g_PAirpcapFreeDeviceList(devsList);
841 *err = NO_AIRPCAP_INTERFACES_FOUND;
848 * Insert the adapters in our list
850 adListEntry = devsList;
853 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
854 if (if_info != NULL){
855 il = g_list_append(il, if_info);
858 adListEntry = adListEntry->next;
861 g_PAirpcapFreeDeviceList(devsList);
867 * Used to retrieve the interface given the name
868 * (the name is used in AirpcapOpen)
870 airpcap_if_info_t* get_airpcap_if_from_name(GList* if_list, const gchar* name)
873 airpcap_if_info_t* if_info;
875 for (curr = g_list_first(if_list); curr; curr = g_list_next(curr)) {
876 if_info = (airpcap_if_info_t *)curr->data;
877 if (if_info && (g_ascii_strcasecmp(if_info->name, name) == 0)) {
880 /* Try the name without the "\\.\" prefix. */
881 if (strlen(if_info->name) > 4 && (g_ascii_strcasecmp(if_info->name + 4, name) == 0)) {
889 * Clear keys and decryption status for the specified interface
892 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
896 if (info_if->keysCollection != NULL)
898 g_free(info_if->keysCollection);
899 info_if->keysCollection = NULL;
902 info_if->keysCollectionSize = 0;
904 info_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
905 info_if->saved = FALSE;
910 * Used to retrieve the two chars string from interface
913 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
919 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
921 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
922 * Otherwise, check if it is the "Any" adapter...
926 if (g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
927 number = g_strdup("??");
929 number = g_strdup(AIRPCAP_CHANNEL_ANY_NAME);
933 number = g_strdup_printf("%.2u",n);
940 * Used to retrieve the two chars string from interface
943 airpcap_get_if_string_number_from_description(gchar* description)
948 number = (gchar*)g_malloc(sizeof(gchar)*3);
950 pointer = g_strrstr(description,"#\0");
952 number[0] = *(pointer+1);
953 number[1] = *(pointer+2);
960 * Load the configuration for the specified interface
963 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
965 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
970 ad = airpcap_if_open(if_info->name, ebuf);
974 /* Stop blinking (if it was blinking!)*/
975 if (if_info->blinking)
977 /* Turn on the light (if it was off) */
978 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
981 /* Apply settings... */
982 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
983 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
984 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
985 airpcap_if_get_link_type(ad,&(if_info->linkType));
986 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
987 /* get the keys, if everything is ok, close the adapter */
988 if (airpcap_if_load_keys(ad,if_info))
989 airpcap_if_close(ad);
991 if_info->saved = TRUE;
996 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1003 * Save the configuration for the specified interface
1006 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1008 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1011 if (if_info != NULL)
1013 ad = airpcap_if_open(if_info->name, ebuf);
1017 /* Stop blinking (if it was blinking!)*/
1018 if (if_info->blinking)
1020 /* Turn on the light (if it was off) */
1021 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1024 /* Apply settings... */
1025 airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
1026 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1027 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1028 airpcap_if_set_link_type(ad,if_info->linkType);
1029 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1030 airpcap_if_save_keys(ad,if_info);
1032 /* ... and save them */
1033 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
1036 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save Wireless configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
1038 if_info->saved = FALSE;
1039 airpcap_if_close(ad);
1043 if_info->saved = TRUE;
1044 airpcap_if_close(ad);
1049 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1056 * Save the configuration for the specified interface
1059 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1061 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1064 if (fake_if_info != NULL)
1066 ad = airpcap_if_open(fake_if_info->name, ebuf);
1070 /* Apply decryption settings... */
1071 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1072 airpcap_if_save_driver_keys(ad,fake_if_info);
1073 airpcap_if_close(ad);
1078 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1087 * Free an instance of airpcap_if_info_t
1090 airpcap_if_info_free(airpcap_if_info_t *if_info)
1092 if (if_info != NULL)
1094 g_free(if_info->name);
1096 g_free(if_info->description);
1098 if (if_info->keysCollection != NULL)
1100 g_free(if_info->keysCollection);
1101 if_info->keysCollection = NULL;
1104 if (if_info->ip_addr != NULL)
1106 g_slist_free(if_info->ip_addr);
1107 if_info->ip_addr = NULL;
1115 /* DYNAMIC LIBRARY LOADER */
1117 * Used to dynamically load the airpcap library in order link it only when
1118 * it's present on the system
1120 int load_airpcap(void)
1123 gboolean base_functions = TRUE;
1124 gboolean eleven_n_functions = TRUE;
1126 if ((AirpcapLib = ws_load_library("airpcap.dll")) == NULL)
1128 /* Report the error but go on */
1129 AirpcapVersion = AIRPCAP_DLL_NOT_FOUND;
1130 return AirpcapVersion;
1134 if ((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
1135 if ((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
1136 if ((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
1137 if ((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
1138 if ((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
1139 if ((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
1140 if ((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
1141 if ((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
1142 if ((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
1143 if ((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
1144 if ((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
1145 if ((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
1146 if ((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
1147 if ((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
1148 if ((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
1149 if ((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
1150 if ((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
1151 if ((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
1152 if ((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
1153 if ((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
1154 if ((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
1155 if ((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
1156 if ((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
1157 if ((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
1158 if ((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
1159 if ((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
1160 if ((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
1161 if ((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
1162 if ((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) base_functions = FALSE;
1163 if ((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) base_functions = FALSE;
1164 if ((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) base_functions = FALSE;
1165 if ((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) base_functions = FALSE;
1167 /* TEST IF AIRPCAP SUPPORTS 11N */
1168 if ((g_PAirpcapSetDeviceChannelEx = (AirpcapSetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1169 if ((g_PAirpcapGetDeviceChannelEx = (AirpcapGetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1170 if ((g_PAirpcapGetDeviceSupportedChannels = (AirpcapGetDeviceSupportedChannelsHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceSupportedChannels")) == NULL) eleven_n_functions = FALSE;
1172 if (base_functions && eleven_n_functions){
1173 AirpcapLoaded = TRUE;
1174 AirpcapVersion = AIRPCAP_DLL_OK;
1175 } else if (base_functions){
1176 AirpcapLoaded = TRUE;
1177 AirpcapVersion = AIRPCAP_DLL_OLD;
1178 return AIRPCAP_DLL_OK;
1180 AirpcapLoaded = FALSE;
1181 AirpcapVersion = AIRPCAP_DLL_ERROR;
1184 return AirpcapVersion;
1186 return AIRPCAP_DLL_NOT_FOUND;
1191 * Append the version of AirPcap with which we were compiled to a GString.
1194 get_compiled_airpcap_version(GString *str)
1196 g_string_append(str, "with AirPcap");
1200 * Append the version of AirPcap with which we we're running to a GString.
1203 get_runtime_airpcap_version(GString *str)
1205 guint vmaj, vmin, vrev, build;
1207 /* See if the DLL has been loaded successfully. Bail if it hasn't */
1208 if (AirpcapLoaded == FALSE) {
1209 g_string_append(str, "without AirPcap");
1213 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
1214 g_string_append_printf(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
1219 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1224 * indent-tabs-mode: nil
1227 * vi: set shiftwidth=4 tabstop=8 expandtab:
1228 * :indentSize=4:tabSize=8:noTabs=true: