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.
38 #include <epan/crypt/airpdcap_ws.h>
39 #include <epan/strutil.h>
40 #include <wsutil/file_util.h>
41 #include <wsutil/frequency-utils.h>
43 #include <caputils/airpcap.h>
44 #include <caputils/airpcap_loader.h>
48 * Set to TRUE if the DLL was successfully loaded AND all functions
51 static gboolean AirpcapLoaded = FALSE;
55 * We load dynamically the dag library in order link it only when
56 * it's present on the system
58 static void * AirpcapLib = NULL;
60 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
61 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
62 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
63 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
64 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
65 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
66 static AirpcapReadHandler g_PAirpcapRead;
67 static AirpcapGetStatsHandler g_PAirpcapGetStats;
70 static int AirpcapVersion = 3;
72 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
73 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
74 static AirpcapOpenHandler g_PAirpcapOpen;
75 static AirpcapCloseHandler g_PAirpcapClose;
76 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
77 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
78 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
79 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
80 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
81 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
82 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
83 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
84 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
85 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
86 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
87 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
88 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
89 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
90 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
91 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
92 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
93 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
94 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
95 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
96 static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
97 static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
98 static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
100 /* Airpcap interface list */
101 GList *g_airpcap_if_list = NULL;
103 /* Airpcap current selected interface */
104 airpcap_if_info_t *airpcap_if_selected = NULL;
106 /* Airpcap current active interface */
107 airpcap_if_info_t *airpcap_if_active = NULL;
109 Dot11Channel *pSupportedChannels;
110 guint numSupportedChannels;
112 static AirpcapChannelInfo LegacyChannels[] =
130 static guint num_legacy_channels = 14;
133 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
134 * "get_airpcap_interface_list()".
137 cant_get_airpcap_if_list_error_message(const char *err_str)
139 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
143 * Airpcap wrapper, used to store the current settings for the selected adapter
146 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
148 if (!AirpcapLoaded) return FALSE;
149 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
153 * Airpcap wrapper, used to open an airpcap adapter
156 airpcap_if_open(gchar * name, gchar * err)
158 if (!AirpcapLoaded) return NULL;
159 if (name == NULL) return NULL;
160 return g_PAirpcapOpen(name,err);
164 * Airpcap wrapper, used to close an airpcap adapter
167 airpcap_if_close(PAirpcapHandle handle)
169 if (!AirpcapLoaded) return;
170 g_PAirpcapClose(handle);
174 * Retrieve the state of the Airpcap DLL
177 airpcap_get_dll_state(void)
179 return AirpcapVersion;
183 * Airpcap wrapper, used to turn on the led of an airpcap adapter
186 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
188 if (!AirpcapLoaded) return FALSE;
189 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
193 * Airpcap wrapper, used to turn off the led of an airpcap adapter
196 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
198 if (!AirpcapLoaded) return FALSE;
199 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
203 * Airpcap wrapper, used to get the channel of an airpcap adapter
206 airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
208 if (!AirpcapLoaded) return FALSE;
209 return g_PAirpcapGetDeviceChannel(ah,ch);
213 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
216 airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
218 if (!AirpcapLoaded) return FALSE;
219 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
220 *nInfo = num_legacy_channels;
221 *cInfo = (AirpcapChannelInfo*)&LegacyChannels;
224 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
225 return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
231 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
234 airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
236 AirpcapChannelInfo *chanInfo;
237 guint i=0, j=0, numInfo = 0;
241 if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
243 numSupportedChannels = 0;
246 * allocate a bigger array
251 pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
253 for (i = 0; i < numInfo; i++)
255 guint supportedChannel = G_MAXUINT;
258 * search if we have it already
260 for (j = 0; j < numSupportedChannels; j++)
262 if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
264 supportedChannel = j;
269 if (supportedChannel == G_MAXUINT)
272 * not found, create a new item
274 pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
276 switch(chanInfo[i].ExtChannel)
279 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
282 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
286 pSupportedChannels[numSupportedChannels].Flags = 0;
290 * Gather channel information
293 pSupportedChannels[numSupportedChannels].Flags |=
294 FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
295 FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
296 pSupportedChannels[numSupportedChannels].Channel =
297 ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
298 numSupportedChannels++;
303 * just update the ext channel flags
305 switch(chanInfo[i].ExtChannel)
308 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
311 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
320 if (numSupportedChannels < 1)
323 * Now sort the list by frequency
325 for (i = 0 ; i < numSupportedChannels - 1; i++)
327 for (j = i + 1; j < numSupportedChannels; j++)
329 if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
331 Dot11Channel temp = pSupportedChannels[i];
332 pSupportedChannels[i] = pSupportedChannels[j];
333 pSupportedChannels[j] = temp;
338 *pNumSupportedChannels = numSupportedChannels;
339 return pSupportedChannels;
343 * Airpcap wrapper, used to set the channel of an airpcap adapter
346 airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
348 if (!AirpcapLoaded) return FALSE;
349 return g_PAirpcapSetDeviceChannel(ah,ch);
353 * Airpcap wrapper, used to set the frequency of an airpcap adapter
356 airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
358 if (!AirpcapLoaded) return FALSE;
359 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
361 channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
366 return airpcap_if_set_device_channel(ah, channel);
368 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
369 return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
376 * Airpcap wrapper, used to get the frequency of an airpcap adapter
379 airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
381 if (!AirpcapLoaded) return FALSE;
383 pChannelInfo->Frequency = 0;
384 pChannelInfo->ExtChannel = 0;
385 pChannelInfo->Reserved[0] = 0;
386 pChannelInfo->Reserved[1] = 0;
387 pChannelInfo->Reserved[2] = 0;
389 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
393 if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
395 chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
396 if (chan_freq == 0) return FALSE;
397 pChannelInfo->Frequency = chan_freq;
400 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
401 return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
407 * Airpcap wrapper, used to get the link type of an airpcap adapter
410 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
412 if (!AirpcapLoaded) return FALSE;
413 return g_PAirpcapGetLinkType(ah,lt);
417 * Airpcap wrapper, used to set the link type of an airpcap adapter
420 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
422 if (!AirpcapLoaded) return FALSE;
423 return g_PAirpcapSetLinkType(ah,lt);
427 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
430 airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
432 if (!AirpcapLoaded) return FALSE;
433 return g_PAirpcapGetFcsPresence(ah,fcs);
437 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
440 airpcap_if_set_fcs_presence(PAirpcapHandle ah, gboolean fcs)
442 if (!AirpcapLoaded) return FALSE;
443 return g_PAirpcapSetFcsPresence(ah,fcs);
447 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
450 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
452 if (!AirpcapLoaded) return FALSE;
453 return g_PAirpcapGetDecryptionState(ah,PEnable);
457 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
460 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
462 if (!AirpcapLoaded) return FALSE;
463 return g_PAirpcapSetDecryptionState(ah,Enable);
467 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
470 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
472 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
473 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
477 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
480 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
482 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
483 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
487 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
490 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
492 if (!AirpcapLoaded) return FALSE;
493 return g_PAirpcapGetFcsValidation(ah,val);
497 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
500 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
502 if (!AirpcapLoaded) return FALSE;
503 return g_PAirpcapSetFcsValidation(ah,val);
507 * Airpcap wrapper, used to save the settings for the selected_if
510 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
512 if (!AirpcapLoaded) return FALSE;
513 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
517 * Airpcap wrapper, used to save the settings for the selected_if
520 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
522 if (!AirpcapLoaded) return FALSE;
523 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
527 * Airpcap wrapper, used to save the driver's set of keys
530 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
532 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
533 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
537 * Airpcap wrapper, used to load the driver's set of keys
540 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
542 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
543 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
547 * This function will create a new airpcap_if_info_t using a name and a description
550 airpcap_if_info_new(char *name, char *description)
553 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
555 airpcap_if_info_t *if_info = NULL;
557 /* Probably I have to switch on the leds!!! */
558 ad = airpcap_if_open(name, ebuf);
561 if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
562 if_info->name = g_strdup(name);
563 if (description == NULL){
564 if_info->description = NULL;
566 if_info->description = g_strdup(description);
569 if_info->ip_addr = NULL;
570 if_info->loopback = FALSE;
571 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
572 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
573 airpcap_if_get_link_type(ad,&(if_info->linkType));
574 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
575 if_info->pSupportedChannels = airpcap_if_get_device_supported_channels_array(ad, &(if_info->numSupportedChannels));
576 airpcap_if_turn_led_on(ad, 0);
577 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
579 if_info->blinking = FALSE;
580 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
582 /* get the keys, if everything is ok, close the adapter */
583 if (airpcap_if_load_keys(ad,if_info))
585 airpcap_if_close(ad);
592 * This function will create a new fake drivers' interface, to load global keys...
595 airpcap_driver_fake_if_info_new(void)
598 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
600 airpcap_if_info_t *if_info = NULL;
601 airpcap_if_info_t *fake_if_info = NULL;
603 /* Maybe for some reason no airpcap adapter is found */
604 if (g_airpcap_if_list == NULL)
608 * Retrieve the first AirPcap adapter available. If no interface is found,
609 * it is not possible to retrieve the driver's settings, so return NULL.
611 if_info = (airpcap_if_info_t *)g_list_nth_data(g_airpcap_if_list,0);
615 /* Open the 'fake' adapter */
616 ad = airpcap_if_open(if_info->name, ebuf);
619 fake_if_info = (airpcap_if_info_t *)g_malloc(sizeof (airpcap_if_info_t));
620 fake_if_info->name = g_strdup(if_info->name);
621 fake_if_info->description = g_strdup(if_info->description);
622 fake_if_info->loopback = FALSE;
623 fake_if_info->ip_addr = NULL;
624 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
625 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
626 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
627 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
628 airpcap_if_get_device_channel_ex(ad,&(fake_if_info->channelInfo));
629 airpcap_if_turn_led_on(ad, 0);
630 fake_if_info->led = TRUE;
631 fake_if_info->blinking = FALSE;
632 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
634 /* get the keys, if everything is ok, close the adapter */
635 if (airpcap_if_load_driver_keys(ad,fake_if_info))
637 airpcap_if_close(ad);
646 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
649 airpcap_if_info_print(airpcap_if_info_t* if_info)
654 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
658 g_print("\n----------------- AirPcap Interface \n");
659 g_print(" NAME: %s\n",if_info->name);
660 g_print(" DESCRIPTION: %s\n",if_info->description);
661 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
662 g_print(" channelInfo.Frequency: %u\n",if_info->channelInfo.Frequency);
663 g_print(" channelInfo.ExtChannel: %d\n",if_info->channelInfo.ExtChannel);
664 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
665 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
666 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
667 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
668 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
669 g_print(" KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
670 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
671 g_print(" LINKTYPE: %d\n",if_info->linkType);
672 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
673 g_print(" (GTK) TAG: %d\n",if_info->tag);
674 g_print("SUPPORTED CHANNELS POINTER: %p\n",if_info->pSupportedChannels);
675 g_print(" NUM SUPPORTED CHANNELS: %u\n",if_info->numSupportedChannels);
677 for(i=0; i<(if_info->numSupportedChannels); i++){
678 g_print("\n SUPPORTED CHANNEL #%u\n",i+1);
679 g_print(" CHANNEL: %u\n",if_info->pSupportedChannels[i].Channel);
680 g_print(" FREQUENCY: %u\n",if_info->pSupportedChannels[i].Frequency);
681 g_print(" FLAGS: %u\n",if_info->pSupportedChannels[i].Flags);
685 #endif /* AIRPCAP_DEBUG */
688 * Function used to load the WEP keys for a selected interface
691 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
693 if (!if_info) return FALSE;
695 if_info->keysCollectionSize = 0;
696 if_info->keysCollection = NULL;
698 if (!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
700 if (if_info->keysCollectionSize == 0)
702 if_info->keysCollection = NULL;
703 airpcap_if_close(ad);
707 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
708 if (!if_info->keysCollection)
710 if_info->keysCollectionSize = 0;
711 if_info->keysCollection = NULL;
712 airpcap_if_close(ad);
716 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
720 airpcap_if_close(ad);
725 * Function used to load the WEP keys for a selected interface
728 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
730 if_info->keysCollectionSize = 0;
731 if_info->keysCollection = NULL;
733 if (!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
735 if (if_info->keysCollectionSize == 0)
737 if_info->keysCollection = NULL;
738 airpcap_if_close(ad);
742 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
743 if (!if_info->keysCollection)
745 if_info->keysCollectionSize = 0;
746 if_info->keysCollection = NULL;
747 airpcap_if_close(ad);
751 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
755 airpcap_if_close(ad);
760 * Function used to save the WEP keys for a selected interface
763 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
765 if (!if_info || !AirpcapLoaded) return;
767 if (if_info->keysCollection != NULL)
768 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
772 * Function used to save the WEP keys for a selected interface
775 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
777 if (if_info->keysCollection != NULL)
778 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
782 * Callback used to free an instance of airpcap_if_info_t
785 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
787 airpcap_if_info_t *if_info = (airpcap_if_info_t *)data;
792 if (if_info->name != NULL)
793 g_free(if_info->name);
795 if (if_info->description != NULL)
796 g_free(if_info->description);
798 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
799 if (if_info->keysCollection != NULL)
801 g_free(if_info->keysCollection);
802 if_info->keysCollection = NULL;
805 if (if_info->ip_addr != NULL)
806 g_slist_free(if_info->ip_addr);
812 * Function used to free the airpcap interface list
815 free_airpcap_interface_list(GList *if_list)
817 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
818 g_list_free(if_list);
823 * This function will use the airpcap.dll to find all the airpcap devices.
824 * Will return null if no device is found.
827 get_airpcap_interface_list(int *err, char **err_str)
830 airpcap_if_info_t *if_info;
832 AirpcapDeviceDescription *devsList, *adListEntry;
833 char errbuf[AIRPCAP_ERRBUF_SIZE];
839 *err = AIRPCAP_NOT_LOADED;
843 if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
845 /* No interfaces, return il = NULL; */
846 *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
848 *err_str = cant_get_airpcap_if_list_error_message(errbuf);
855 adListEntry = devsList;
860 adListEntry = adListEntry->next;
865 /* No interfaces, return il= NULL */
866 g_PAirpcapFreeDeviceList(devsList);
867 *err = NO_AIRPCAP_INTERFACES_FOUND;
874 * Insert the adapters in our list
876 adListEntry = devsList;
879 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
880 if (if_info != NULL){
881 il = g_list_append(il, if_info);
884 adListEntry = adListEntry->next;
887 g_PAirpcapFreeDeviceList(devsList);
893 * Used to retrieve the interface given the name
894 * (the name is used in AirpcapOpen)
896 airpcap_if_info_t* get_airpcap_if_from_name(GList* if_list, const gchar* name)
899 airpcap_if_info_t* if_info;
901 for (curr = g_list_first(if_list); curr; curr = g_list_next(curr)) {
902 if_info = (airpcap_if_info_t *)curr->data;
903 if (if_info && (g_ascii_strcasecmp(if_info->name, name) == 0)) {
906 /* Try the name without the "\\.\" prefix. */
907 if (strlen(if_info->name) > 4 && (g_ascii_strcasecmp(if_info->name + 4, name) == 0)) {
915 * Clear keys and decryption status for the specified interface
918 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
922 if (info_if->keysCollection != NULL)
924 g_free(info_if->keysCollection);
925 info_if->keysCollection = NULL;
928 info_if->keysCollectionSize = 0;
930 info_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
931 info_if->saved = FALSE;
936 * Used to retrieve the two chars string from interface
939 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
945 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
947 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
948 * Otherwise, check if it is the "Any" adapter...
952 if (g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
953 number = g_strdup_printf("??");
955 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
959 number = g_strdup_printf("%.2u",n);
966 * Used to retrieve the two chars string from interface
969 airpcap_get_if_string_number_from_description(gchar* description)
974 number = (gchar*)g_malloc(sizeof(gchar)*3);
976 pointer = g_strrstr(description,"#\0");
978 number[0] = *(pointer+1);
979 number[1] = *(pointer+2);
986 * Load the configuration for the specified interface
989 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
991 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
996 ad = airpcap_if_open(if_info->name, ebuf);
1000 /* Stop blinking (if it was blinking!)*/
1001 if (if_info->blinking)
1003 /* Turn on the light (if it was off) */
1004 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1007 /* Apply settings... */
1008 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
1009 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1010 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1011 airpcap_if_get_link_type(ad,&(if_info->linkType));
1012 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1013 /* get the keys, if everything is ok, close the adapter */
1014 if (airpcap_if_load_keys(ad,if_info))
1015 airpcap_if_close(ad);
1017 if_info->saved = TRUE;
1022 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1029 * Save the configuration for the specified interface
1032 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1034 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1037 if (if_info != NULL)
1039 ad = airpcap_if_open(if_info->name, ebuf);
1043 /* Stop blinking (if it was blinking!)*/
1044 if (if_info->blinking)
1046 /* Turn on the light (if it was off) */
1047 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1050 /* Apply settings... */
1051 airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
1052 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1053 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1054 airpcap_if_set_link_type(ad,if_info->linkType);
1055 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1056 airpcap_if_save_keys(ad,if_info);
1058 /* ... and save them */
1059 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
1062 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.");
1064 if_info->saved = FALSE;
1065 airpcap_if_close(ad);
1069 if_info->saved = TRUE;
1070 airpcap_if_close(ad);
1075 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1082 * Save the configuration for the specified interface
1085 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1087 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1090 if (fake_if_info != NULL)
1092 ad = airpcap_if_open(fake_if_info->name, ebuf);
1096 /* Apply decryption settings... */
1097 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1098 airpcap_if_save_driver_keys(ad,fake_if_info);
1099 airpcap_if_close(ad);
1104 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1113 * Free an instance of airpcap_if_info_t
1116 airpcap_if_info_free(airpcap_if_info_t *if_info)
1118 if (if_info != NULL)
1120 if (if_info->name != NULL)
1121 g_free(if_info->name);
1123 if (if_info->description != NULL)
1124 g_free(if_info->description);
1126 if (if_info->keysCollection != NULL)
1128 g_free(if_info->keysCollection);
1129 if_info->keysCollection = NULL;
1132 if (if_info->ip_addr != NULL)
1134 g_slist_free(if_info->ip_addr);
1135 if_info->ip_addr = NULL;
1138 if (if_info != NULL)
1147 /* DYNAMIC LIBRARY LOADER */
1149 * Used to dynamically load the airpcap library in order link it only when
1150 * it's present on the system
1152 int load_airpcap(void)
1155 gboolean base_functions = TRUE;
1156 gboolean eleven_n_functions = TRUE;
1158 if ((AirpcapLib = ws_load_library("airpcap.dll")) == NULL)
1160 /* Report the error but go on */
1161 AirpcapVersion = AIRPCAP_DLL_NOT_FOUND;
1162 return AirpcapVersion;
1166 if ((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
1167 if ((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
1168 if ((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
1169 if ((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
1170 if ((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
1171 if ((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
1172 if ((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
1173 if ((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
1174 if ((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
1175 if ((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
1176 if ((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
1177 if ((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
1178 if ((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
1179 if ((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
1180 if ((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
1181 if ((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
1182 if ((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
1183 if ((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
1184 if ((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
1185 if ((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
1186 if ((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
1187 if ((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
1188 if ((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
1189 if ((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
1190 if ((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
1191 if ((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
1192 if ((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
1193 if ((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
1194 if ((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) base_functions = FALSE;
1195 if ((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) base_functions = FALSE;
1196 if ((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) base_functions = FALSE;
1197 if ((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) base_functions = FALSE;
1199 /* TEST IF AIRPCAP SUPPORTS 11N */
1200 if ((g_PAirpcapSetDeviceChannelEx = (AirpcapSetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1201 if ((g_PAirpcapGetDeviceChannelEx = (AirpcapGetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
1202 if ((g_PAirpcapGetDeviceSupportedChannels = (AirpcapGetDeviceSupportedChannelsHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceSupportedChannels")) == NULL) eleven_n_functions = FALSE;
1204 if (base_functions && eleven_n_functions){
1205 AirpcapLoaded = TRUE;
1206 AirpcapVersion = AIRPCAP_DLL_OK;
1207 } else if (base_functions){
1208 AirpcapLoaded = TRUE;
1209 AirpcapVersion = AIRPCAP_DLL_OLD;
1210 return AIRPCAP_DLL_OK;
1212 AirpcapLoaded = FALSE;
1213 AirpcapVersion = AIRPCAP_DLL_ERROR;
1216 return AirpcapVersion;
1218 return AIRPCAP_DLL_NOT_FOUND;
1223 * Append the version of AirPcap with which we were compiled to a GString.
1226 get_compiled_airpcap_version(GString *str)
1228 g_string_append(str, "with AirPcap");
1232 * Append the version of AirPcap with which we we're running to a GString.
1235 get_runtime_airpcap_version(GString *str)
1237 guint vmaj, vmin, vrev, build;
1239 /* See if the DLL has been loaded successfully. Bail if it hasn't */
1240 if (AirpcapLoaded == FALSE) {
1241 g_string_append(str, "without AirPcap");
1245 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
1246 g_string_append_printf(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
1249 #endif /* HAVE_AIRPCAP */
1252 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1257 * indent-tabs-mode: nil
1260 * vi: set shiftwidth=4 tabstop=8 expandtab:
1261 * :indentSize=4:tabSize=8:noTabs=true: