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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/crypt/airpdcap_ws.h>
30 #include <epan/strutil.h>
31 #include <wsutil/file_util.h>
32 #include <wsutil/frequency-utils.h>
34 #include <caputils/airpcap.h>
35 #include <caputils/airpcap_loader.h>
39 * Set to TRUE if the DLL was successfully loaded AND all functions
42 static gboolean AirpcapLoaded = FALSE;
46 * We load dynamically the dag library in order link it only when
47 * it's present on the system
49 static void * AirpcapLib = NULL;
51 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
52 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
53 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
54 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
55 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
56 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
57 static AirpcapReadHandler g_PAirpcapRead;
58 static AirpcapGetStatsHandler g_PAirpcapGetStats;
61 static int AirpcapVersion = 3;
63 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
64 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
65 static AirpcapOpenHandler g_PAirpcapOpen;
66 static AirpcapCloseHandler g_PAirpcapClose;
67 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
68 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
69 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
70 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
71 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
72 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
73 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
74 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
75 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
76 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
77 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
78 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
79 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
80 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
81 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
82 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
83 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
84 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
85 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
86 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
87 static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
88 static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
89 static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
91 /* Airpcap interface list */
92 GList *g_airpcap_if_list = NULL;
94 /* Airpcap current selected interface */
95 airpcap_if_info_t *airpcap_if_selected = NULL;
97 /* Airpcap current active interface */
98 airpcap_if_info_t *airpcap_if_active = NULL;
100 Dot11Channel *pSupportedChannels;
101 guint numSupportedChannels;
103 static AirpcapChannelInfo LegacyChannels[] =
121 static guint num_legacy_channels = 14;
124 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
125 * "get_airpcap_interface_list()".
128 cant_get_airpcap_if_list_error_message(const char *err_str)
130 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
134 * Airpcap wrapper, used to store the current settings for the selected adapter
137 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
139 if (!AirpcapLoaded) return FALSE;
140 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
144 * Airpcap wrapper, used to open an airpcap adapter
147 airpcap_if_open(gchar * name, gchar * err)
149 if (!AirpcapLoaded) return NULL;
150 if (name == NULL) return NULL;
151 return g_PAirpcapOpen(name,err);
155 * Airpcap wrapper, used to close an airpcap adapter
158 airpcap_if_close(PAirpcapHandle handle)
160 if (!AirpcapLoaded) return;
161 g_PAirpcapClose(handle);
165 * Retrieve the state of the Airpcap DLL
168 airpcap_get_dll_state(void)
170 return AirpcapVersion;
174 * Airpcap wrapper, used to turn on the led of an airpcap adapter
177 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
179 if (!AirpcapLoaded) return FALSE;
180 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
184 * Airpcap wrapper, used to turn off the led of an airpcap adapter
187 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
189 if (!AirpcapLoaded) return FALSE;
190 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
194 * Airpcap wrapper, used to get the channel of an airpcap adapter
197 airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
199 if (!AirpcapLoaded) return FALSE;
200 return g_PAirpcapGetDeviceChannel(ah,ch);
204 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
207 airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
209 if (!AirpcapLoaded) return FALSE;
210 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
211 *nInfo = num_legacy_channels;
212 *cInfo = (AirpcapChannelInfo*)&LegacyChannels;
215 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
216 return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
222 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
225 airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
227 AirpcapChannelInfo *chanInfo;
228 guint i=0, j=0, numInfo = 0;
232 if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
234 numSupportedChannels = 0;
237 * allocate a bigger array
242 pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
244 for (i = 0; i < numInfo; i++)
246 guint supportedChannel = G_MAXUINT;
249 * search if we have it already
251 for (j = 0; j < numSupportedChannels; j++)
253 if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
255 supportedChannel = j;
260 if (supportedChannel == G_MAXUINT)
263 * not found, create a new item
265 pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
267 switch(chanInfo[i].ExtChannel)
270 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
273 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
277 pSupportedChannels[numSupportedChannels].Flags = 0;
281 * Gather channel information
284 pSupportedChannels[numSupportedChannels].Flags |=
285 FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
286 FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
287 pSupportedChannels[numSupportedChannels].Channel =
288 ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
289 numSupportedChannels++;
294 * just update the ext channel flags
296 switch(chanInfo[i].ExtChannel)
299 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
302 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
311 if (numSupportedChannels < 1)
314 * Now sort the list by frequency
316 for (i = 0 ; i < numSupportedChannels - 1; i++)
318 for (j = i + 1; j < numSupportedChannels; j++)
320 if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
322 Dot11Channel temp = pSupportedChannels[i];
323 pSupportedChannels[i] = pSupportedChannels[j];
324 pSupportedChannels[j] = temp;
329 *pNumSupportedChannels = numSupportedChannels;
330 return pSupportedChannels;
334 * Airpcap wrapper, used to set the channel of an airpcap adapter
337 airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
339 if (!AirpcapLoaded) return FALSE;
340 return g_PAirpcapSetDeviceChannel(ah,ch);
344 * Airpcap wrapper, used to set the frequency of an airpcap adapter
347 airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
349 if (!AirpcapLoaded) return FALSE;
350 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
352 channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
357 return airpcap_if_set_device_channel(ah, channel);
359 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
360 return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
367 * Airpcap wrapper, used to get the frequency of an airpcap adapter
370 airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
372 if (!AirpcapLoaded) return FALSE;
374 pChannelInfo->Frequency = 0;
375 pChannelInfo->ExtChannel = 0;
376 pChannelInfo->Reserved[0] = 0;
377 pChannelInfo->Reserved[1] = 0;
378 pChannelInfo->Reserved[2] = 0;
380 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
384 if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
386 chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
387 if (chan_freq == 0) return FALSE;
388 pChannelInfo->Frequency = chan_freq;
391 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
392 return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
398 * Airpcap wrapper, used to get the link type of an airpcap adapter
401 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
403 if (!AirpcapLoaded) return FALSE;
404 return g_PAirpcapGetLinkType(ah,lt);
408 * Airpcap wrapper, used to set the link type of an airpcap adapter
411 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
413 if (!AirpcapLoaded) return FALSE;
414 return g_PAirpcapSetLinkType(ah,lt);
418 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
421 airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
423 if (!AirpcapLoaded) return FALSE;
424 return g_PAirpcapGetFcsPresence(ah,fcs);
428 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
431 airpcap_if_set_fcs_presence(PAirpcapHandle ah, gboolean fcs)
433 if (!AirpcapLoaded) return FALSE;
434 return g_PAirpcapSetFcsPresence(ah,fcs);
438 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
441 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
443 if (!AirpcapLoaded) return FALSE;
444 return g_PAirpcapGetDecryptionState(ah,PEnable);
448 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
451 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
453 if (!AirpcapLoaded) return FALSE;
454 return g_PAirpcapSetDecryptionState(ah,Enable);
458 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
461 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
463 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
464 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
468 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
471 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
473 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
474 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
478 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
481 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
483 if (!AirpcapLoaded) return FALSE;
484 return g_PAirpcapGetFcsValidation(ah,val);
488 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
491 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
493 if (!AirpcapLoaded) return FALSE;
494 return g_PAirpcapSetFcsValidation(ah,val);
498 * Airpcap wrapper, used to save the settings for the selected_if
501 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
503 if (!AirpcapLoaded) return FALSE;
504 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
508 * Airpcap wrapper, used to save the settings for the selected_if
511 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
513 if (!AirpcapLoaded) return FALSE;
514 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
518 * Airpcap wrapper, used to save the driver's set of keys
521 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
523 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
524 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
528 * Airpcap wrapper, used to load the driver's set of keys
531 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
533 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
534 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
538 * This function will create a new airpcap_if_info_t using a name and a description
541 airpcap_if_info_new(char *name, char *description)
544 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
546 airpcap_if_info_t *if_info = NULL;
548 /* Probably I have to switch on the leds!!! */
549 ad = airpcap_if_open(name, ebuf);
552 if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
553 if_info->name = g_strdup(name);
554 if (description == NULL){
555 if_info->description = NULL;
557 if_info->description = g_strdup(description);
560 if_info->ip_addr = NULL;
561 if_info->loopback = FALSE;
562 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
563 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
564 airpcap_if_get_link_type(ad,&(if_info->linkType));
565 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
566 if_info->pSupportedChannels = airpcap_if_get_device_supported_channels_array(ad, &(if_info->numSupportedChannels));
567 airpcap_if_turn_led_on(ad, 0);
568 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
570 if_info->blinking = FALSE;
571 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
573 /* get the keys, if everything is ok, close the adapter */
574 if (airpcap_if_load_keys(ad,if_info))
576 airpcap_if_close(ad);
583 * This function will create a new fake drivers' interface, to load global keys...
586 airpcap_driver_fake_if_info_new(void)
589 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
591 airpcap_if_info_t *if_info = NULL;
592 airpcap_if_info_t *fake_if_info = NULL;
594 /* Maybe for some reason no airpcap adapter is found */
595 if (g_airpcap_if_list == NULL)
599 * Retrieve the first AirPcap adapter available. If no interface is found,
600 * it is not possible to retrieve the driver's settings, so return NULL.
602 if_info = (airpcap_if_info_t *)g_list_nth_data(g_airpcap_if_list,0);
606 /* Open the 'fake' adapter */
607 ad = airpcap_if_open(if_info->name, ebuf);
610 fake_if_info = (airpcap_if_info_t *)g_malloc(sizeof (airpcap_if_info_t));
611 fake_if_info->name = g_strdup(if_info->name);
612 fake_if_info->description = g_strdup(if_info->description);
613 fake_if_info->loopback = FALSE;
614 fake_if_info->ip_addr = NULL;
615 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
616 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
617 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
618 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
619 airpcap_if_get_device_channel_ex(ad,&(fake_if_info->channelInfo));
620 airpcap_if_turn_led_on(ad, 0);
621 fake_if_info->led = TRUE;
622 fake_if_info->blinking = FALSE;
623 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
625 /* get the keys, if everything is ok, close the adapter */
626 if (airpcap_if_load_driver_keys(ad,fake_if_info))
628 airpcap_if_close(ad);
637 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
640 airpcap_if_info_print(airpcap_if_info_t* if_info)
645 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
649 g_print("\n----------------- AirPcap Interface \n");
650 g_print(" NAME: %s\n",if_info->name);
651 g_print(" DESCRIPTION: %s\n",if_info->description);
652 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
653 g_print(" channelInfo.Frequency: %u\n",if_info->channelInfo.Frequency);
654 g_print(" channelInfo.ExtChannel: %d\n",if_info->channelInfo.ExtChannel);
655 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
656 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
657 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
658 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
659 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
660 g_print(" KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
661 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
662 g_print(" LINKTYPE: %d\n",if_info->linkType);
663 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
664 g_print(" (GTK) TAG: %d\n",if_info->tag);
665 g_print("SUPPORTED CHANNELS POINTER: %p\n",if_info->pSupportedChannels);
666 g_print(" NUM SUPPORTED CHANNELS: %u\n",if_info->numSupportedChannels);
668 for(i=0; i<(if_info->numSupportedChannels); i++){
669 g_print("\n SUPPORTED CHANNEL #%u\n",i+1);
670 g_print(" CHANNEL: %u\n",if_info->pSupportedChannels[i].Channel);
671 g_print(" FREQUENCY: %u\n",if_info->pSupportedChannels[i].Frequency);
672 g_print(" FLAGS: %u\n",if_info->pSupportedChannels[i].Flags);
676 #endif /* AIRPCAP_DEBUG */
679 * Function used to load the WEP keys for a selected interface
682 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
684 if (!if_info) return FALSE;
686 if_info->keysCollectionSize = 0;
687 if_info->keysCollection = NULL;
689 if (!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
691 if (if_info->keysCollectionSize == 0)
693 if_info->keysCollection = NULL;
694 airpcap_if_close(ad);
698 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
699 if (!if_info->keysCollection)
701 if_info->keysCollectionSize = 0;
702 if_info->keysCollection = NULL;
703 airpcap_if_close(ad);
707 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
711 airpcap_if_close(ad);
716 * Function used to load the WEP keys for a selected interface
719 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
721 if_info->keysCollectionSize = 0;
722 if_info->keysCollection = NULL;
724 if (!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
726 if (if_info->keysCollectionSize == 0)
728 if_info->keysCollection = NULL;
729 airpcap_if_close(ad);
733 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
734 if (!if_info->keysCollection)
736 if_info->keysCollectionSize = 0;
737 if_info->keysCollection = NULL;
738 airpcap_if_close(ad);
742 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
746 airpcap_if_close(ad);
751 * Function used to save the WEP keys for a selected interface
754 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
756 if (!if_info || !AirpcapLoaded) return;
758 if (if_info->keysCollection != NULL)
759 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
763 * Function used to save the WEP keys for a selected interface
766 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
768 if (if_info->keysCollection != NULL)
769 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
773 * Callback used to free an instance of airpcap_if_info_t
776 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
778 airpcap_if_info_t *if_info = (airpcap_if_info_t *)data;
783 if (if_info->name != NULL)
784 g_free(if_info->name);
786 if (if_info->description != NULL)
787 g_free(if_info->description);
789 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
790 if (if_info->keysCollection != NULL)
792 g_free(if_info->keysCollection);
793 if_info->keysCollection = NULL;
796 if (if_info->ip_addr != NULL)
797 g_slist_free(if_info->ip_addr);
803 * Function used to free the airpcap interface list
806 free_airpcap_interface_list(GList *if_list)
808 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
809 g_list_free(if_list);
813 * This function will use the airpcap.dll to find all the airpcap devices.
814 * Will return null if no device is found.
817 get_airpcap_interface_list(int *err, char **err_str)
820 airpcap_if_info_t *if_info;
822 AirpcapDeviceDescription *devsList, *adListEntry;
823 char errbuf[AIRPCAP_ERRBUF_SIZE];
829 *err = AIRPCAP_NOT_LOADED;
833 if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
835 /* No interfaces, return il = NULL; */
836 *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
838 *err_str = cant_get_airpcap_if_list_error_message(errbuf);
845 adListEntry = devsList;
850 adListEntry = adListEntry->next;
855 /* No interfaces, return il= NULL */
856 g_PAirpcapFreeDeviceList(devsList);
857 *err = NO_AIRPCAP_INTERFACES_FOUND;
864 * Insert the adapters in our list
866 adListEntry = devsList;
869 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
870 if (if_info != NULL){
871 il = g_list_append(il, if_info);
874 adListEntry = adListEntry->next;
877 g_PAirpcapFreeDeviceList(devsList);
883 * Used to retrieve the interface given the name
884 * (the name is used in AirpcapOpen)
886 airpcap_if_info_t* get_airpcap_if_from_name(GList* if_list, const gchar* name)
889 airpcap_if_info_t* if_info;
891 for (curr = g_list_first(if_list); curr; curr = g_list_next(curr)) {
892 if_info = (airpcap_if_info_t *)curr->data;
893 if (if_info && (g_ascii_strcasecmp(if_info->name, name) == 0)) {
896 /* Try the name without the "\\.\" prefix. */
897 if (strlen(if_info->name) > 4 && (g_ascii_strcasecmp(if_info->name + 4, name) == 0)) {
905 * Clear keys and decryption status for the specified interface
908 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
912 if (info_if->keysCollection != NULL)
914 g_free(info_if->keysCollection);
915 info_if->keysCollection = NULL;
918 info_if->keysCollectionSize = 0;
920 info_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
921 info_if->saved = FALSE;
926 * Used to retrieve the two chars string from interface
929 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
935 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
937 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
938 * Otherwise, check if it is the "Any" adapter...
942 if (g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
943 number = g_strdup("??");
945 number = g_strdup(AIRPCAP_CHANNEL_ANY_NAME);
949 number = g_strdup_printf("%.2u",n);
956 * Used to retrieve the two chars string from interface
959 airpcap_get_if_string_number_from_description(gchar* description)
964 number = (gchar*)g_malloc(sizeof(gchar)*3);
966 pointer = g_strrstr(description,"#\0");
968 number[0] = *(pointer+1);
969 number[1] = *(pointer+2);
976 * Load the configuration for the specified interface
979 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
981 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
986 ad = airpcap_if_open(if_info->name, ebuf);
990 /* Stop blinking (if it was blinking!)*/
991 if (if_info->blinking)
993 /* Turn on the light (if it was off) */
994 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
997 /* Apply settings... */
998 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
999 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1000 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1001 airpcap_if_get_link_type(ad,&(if_info->linkType));
1002 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1003 /* get the keys, if everything is ok, close the adapter */
1004 if (airpcap_if_load_keys(ad,if_info))
1005 airpcap_if_close(ad);
1007 if_info->saved = TRUE;
1012 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1019 * Save the configuration for the specified interface
1022 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1024 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1027 if (if_info != NULL)
1029 ad = airpcap_if_open(if_info->name, ebuf);
1033 /* Stop blinking (if it was blinking!)*/
1034 if (if_info->blinking)
1036 /* Turn on the light (if it was off) */
1037 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1040 /* Apply settings... */
1041 airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
1042 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1043 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1044 airpcap_if_set_link_type(ad,if_info->linkType);
1045 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1046 airpcap_if_save_keys(ad,if_info);
1048 /* ... and save them */
1049 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
1052 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.");
1054 if_info->saved = FALSE;
1055 airpcap_if_close(ad);
1059 if_info->saved = TRUE;
1060 airpcap_if_close(ad);
1065 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1072 * Save the configuration for the specified interface
1075 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1077 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1080 if (fake_if_info != NULL)
1082 ad = airpcap_if_open(fake_if_info->name, ebuf);
1086 /* Apply decryption settings... */
1087 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1088 airpcap_if_save_driver_keys(ad,fake_if_info);
1089 airpcap_if_close(ad);
1094 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1103 * Free an instance of airpcap_if_info_t
1106 airpcap_if_info_free(airpcap_if_info_t *if_info)
1108 if (if_info != NULL)
1110 if (if_info->name != NULL)
1111 g_free(if_info->name);
1113 if (if_info->description != NULL)
1114 g_free(if_info->description);
1116 if (if_info->keysCollection != NULL)
1118 g_free(if_info->keysCollection);
1119 if_info->keysCollection = NULL;
1122 if (if_info->ip_addr != NULL)
1124 g_slist_free(if_info->ip_addr);
1125 if_info->ip_addr = NULL;
1128 if (if_info != NULL)
1136 /* DYNAMIC LIBRARY LOADER */
1138 * Used to dynamically load the airpcap library in order link it only when
1139 * it's present on the system
1141 int load_airpcap(void)
1144 gboolean base_functions = TRUE;
1145 gboolean eleven_n_functions = TRUE;
1147 if ((AirpcapLib = ws_load_library("airpcap.dll")) == NULL)
1149 /* Report the error but go on */
1150 AirpcapVersion = AIRPCAP_DLL_NOT_FOUND;
1151 return AirpcapVersion;
1155 if ((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
1156 if ((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
1157 if ((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
1158 if ((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
1159 if ((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
1160 if ((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
1161 if ((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
1162 if ((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
1163 if ((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
1164 if ((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
1165 if ((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
1166 if ((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
1167 if ((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
1168 if ((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
1169 if ((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
1170 if ((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
1171 if ((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
1172 if ((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
1173 if ((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
1174 if ((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
1175 if ((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
1176 if ((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
1177 if ((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
1178 if ((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
1179 if ((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
1180 if ((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
1181 if ((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
1182 if ((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
1183 if ((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) base_functions = FALSE;
1184 if ((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) base_functions = FALSE;
1185 if ((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) base_functions = FALSE;
1186 if ((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) base_functions = FALSE;
1188 /* TEST IF AIRPCAP SUPPORTS 11N */
1189 if ((g_PAirpcapSetDeviceChannelEx = (AirpcapSetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1190 if ((g_PAirpcapGetDeviceChannelEx = (AirpcapGetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1191 if ((g_PAirpcapGetDeviceSupportedChannels = (AirpcapGetDeviceSupportedChannelsHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceSupportedChannels")) == NULL) eleven_n_functions = FALSE;
1193 if (base_functions && eleven_n_functions){
1194 AirpcapLoaded = TRUE;
1195 AirpcapVersion = AIRPCAP_DLL_OK;
1196 } else if (base_functions){
1197 AirpcapLoaded = TRUE;
1198 AirpcapVersion = AIRPCAP_DLL_OLD;
1199 return AIRPCAP_DLL_OK;
1201 AirpcapLoaded = FALSE;
1202 AirpcapVersion = AIRPCAP_DLL_ERROR;
1205 return AirpcapVersion;
1207 return AIRPCAP_DLL_NOT_FOUND;
1212 * Append the version of AirPcap with which we were compiled to a GString.
1215 get_compiled_airpcap_version(GString *str)
1217 g_string_append(str, "with AirPcap");
1221 * Append the version of AirPcap with which we we're running to a GString.
1224 get_runtime_airpcap_version(GString *str)
1226 guint vmaj, vmin, vrev, build;
1228 /* See if the DLL has been loaded successfully. Bail if it hasn't */
1229 if (AirpcapLoaded == FALSE) {
1230 g_string_append(str, "without AirPcap");
1234 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
1235 g_string_append_printf(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
1240 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1245 * indent-tabs-mode: nil
1248 * vi: set shiftwidth=4 tabstop=8 expandtab:
1249 * :indentSize=4:tabSize=8:noTabs=true: