5 * Giorgio Tino <giorgio.tino@cacetech.com>
6 * Copyright (c) CACE Technologies, LLC 2006
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 2000 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include "capture_ui_utils.h"
43 #include <epan/prefs.h>
45 #include "simple_dialog.h"
48 #include "airpcap_loader.h"
51 * We load dinamically the dag library in order link it only when
52 * it's present on the system
54 static HMODULE AirpcapLib;
56 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
57 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
58 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
59 static AirpcapOpenHandler g_PAirpcapOpen;
60 static AirpcapCloseHandler g_PAirpcapClose;
61 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
62 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
63 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
64 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
65 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
66 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
67 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
68 static AirpcapReadHandler g_PAirpcapRead;
69 static AirpcapGetStatsHandler g_PAirpcapGetStats;
70 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
71 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
72 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
73 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
74 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
75 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
76 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
77 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
78 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
79 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
80 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
81 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
82 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
83 /* HAVE_AIRPCAP_API */
85 /* Airpcap interface list */
86 GList *airpcap_if_list = NULL;
88 /* Airpcap current selected interface */
89 airpcap_if_info_t *airpcap_if_selected = NULL;
91 /* Airpcap current active interface */
92 airpcap_if_info_t *airpcap_if_active = NULL;
95 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
96 * "get_airpcap_interface_list()".
99 cant_get_airpcap_if_list_error_message(const char *err_str)
101 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
105 * Airpcap wrapper, used to store the current settings for the selected adapter
108 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
110 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
114 * Airpcap wrapper, used to open an airpcap adapter
117 airpcap_if_open(PCHAR name, PCHAR err)
119 return g_PAirpcapOpen(name,err);
123 * Airpcap wrapper, used to close an airpcap adapter
126 airpcap_if_close(PAirpcapHandle handle)
128 g_PAirpcapClose(handle);
133 * Airpcap wrapper, used to turn on the led of an airpcap adapter
136 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
138 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
142 * Airpcap wrapper, used to turn off the led of an airpcap adapter
145 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
147 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
151 * Airpcap wrapper, used to get the channel of an airpcap adapter
154 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
156 return g_PAirpcapGetDeviceChannel(ah,ch);
160 * Airpcap wrapper, used to set the channel of an airpcap adapter
163 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
165 return g_PAirpcapSetDeviceChannel(ah,ch);
169 * Airpcap wrapper, used to get the link type of an airpcap adapter
172 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
174 return g_PAirpcapGetLinkType(ah,lt);
178 * Airpcap wrapper, used to set the link type of an airpcap adapter
181 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
183 return g_PAirpcapSetLinkType(ah,lt);
187 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
190 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
192 return g_PAirpcapGetFcsPresence(ah,fcs);
196 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
199 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
201 return g_PAirpcapSetFcsPresence(ah,fcs);
205 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
208 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
210 return g_PAirpcapGetDecryptionState(ah,PEnable);
214 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
217 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
219 return g_PAirpcapSetDecryptionState(ah,Enable);
223 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
226 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
228 return g_PAirpcapGetFcsValidation(ah,val);
232 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
235 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
237 return g_PAirpcapSetFcsValidation(ah,val);
241 * Airpcap wrapper, used to save the settings for the selected_if
244 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
246 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
250 * Airpcap wrapper, used to save the settings for the selected_if
253 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
255 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
259 * This function will create a new airpcap_if_info_t using a name and a description
262 airpcap_if_info_new(char *name, char *description)
267 airpcap_if_info_t *if_info;
269 if_info = g_malloc(sizeof (airpcap_if_info_t));
270 if_info->name = g_strdup(name);
271 if (description == NULL)
272 if_info->description = NULL;
274 if_info->description = g_strdup(description);
275 if_info->ip_addr = NULL;
276 if_info->loopback = FALSE;
278 /* Probably I have to switch on the leds!!! */
279 ad = airpcap_if_open(if_info->name, ebuf);
282 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
283 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
284 airpcap_if_get_link_type(ad,&(if_info->linkType));
285 airpcap_if_get_device_channel(ad,&(if_info->channel));
286 airpcap_if_turn_led_on(ad, 0);
287 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
289 if_info->blinking = FALSE;
290 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
292 /* get the keys, if everything is ok, close the adapter */
293 if(airpcap_if_load_keys(ad,if_info))
294 airpcap_if_close(ad);
300 * Function used to load the WEP keys for a selected interface
303 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
305 if_info->keysCollectionSize = 0;
306 if_info->keysCollection = NULL;
308 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
310 if(if_info->keysCollectionSize == 0)
312 if_info->keysCollection = NULL;
313 airpcap_if_close(ad);
317 if_info->keysCollection = (PAirpcapKeysCollection)malloc(if_info->keysCollectionSize);
318 if(!if_info->keysCollection)
320 if_info->keysCollectionSize = 0;
321 if_info->keysCollection = NULL;
322 airpcap_if_close(ad);
326 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
329 airpcap_if_close(ad);
334 * Function used to save the WEP keys for a selected interface
337 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
339 if(if_info->keysCollection != NULL)
340 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
344 * Callback used to free an instance of airpcap_if_info_t
347 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
349 airpcap_if_info_t *if_info = data;
351 if (if_info->name != NULL)
352 g_free(if_info->name);
354 if (if_info->description != NULL)
355 g_free(if_info->description);
357 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
358 if(if_info->keysCollection != NULL)
359 g_free(if_info->keysCollection);
361 if(if_info->ip_addr != NULL)
362 g_slist_free(if_info->ip_addr);
369 * Function used to free the airpcap interface list
372 free_airpcap_interface_list(GList *if_list)
374 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
375 g_list_free(if_list);
380 * This function will use the airpcap.dll to find all the airpcap devices.
381 * Will return null if no device is found.
384 get_airpcap_interface_list(int *err, char *err_str)
387 airpcap_if_info_t *if_info;
389 AirpcapDeviceDescription *devsList, *adListEntry;
391 if(!g_PAirpcapGetDeviceList(&devsList, err_str))
393 /* No interfaces, return il = NULL; */
394 *err = NO_AIRPCAP_INTERFACES_FOUND;
401 adListEntry = devsList;
406 adListEntry = adListEntry->next;
411 /* No interfaces, return il= NULL */
412 g_PAirpcapFreeDeviceList(devsList);
413 *err = NO_AIRPCAP_INTERFACES_FOUND;
418 * Insert the adapters in our list
420 adListEntry = devsList;
421 for(i = 0; i < n_adapts; i++)
423 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
424 il = g_list_append(il, if_info);
426 adListEntry = adListEntry->next;
429 g_PAirpcapFreeDeviceList(devsList);
435 * Used to retrieve the name of the interface given the description
436 * (the name is used in AirpcapOpen, the description is put in the combo box)
438 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
442 airpcap_if_info_t* if_info;
447 while( ifn < g_list_length(if_list) )
449 curr = g_list_nth(if_list, ifn);
453 if_info = curr->data;
455 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
457 return if_info->name;
466 * Used to retrieve the interface given the name
467 * (the name is used in AirpcapOpen)
469 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
473 airpcap_if_info_t* if_info;
478 while( ifn < g_list_length(if_list) )
480 curr = g_list_nth(if_list, ifn);
484 if_info = curr->data;
486 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
497 * Returns the ASCII string of a key given the key bites
500 airpcap_get_key_string(AirpcapKey key)
508 if(key.KeyType == AIRPCAP_KEYTYPE_WEP)
510 s = g_strdup_printf("");
511 for(j = 0; j < key.KeyLen != 0; j++)
513 s1 = g_strdup_printf("%.2x", key.KeyData[j]);
514 g_strlcat(s,s1,WEP_KEY_MAX_SIZE);
521 * Used to retrieve the airpcap_if_info_t of the selected interface given the
522 * description (that is the entry of the combo box).
524 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
528 airpcap_if_info_t* if_info;
533 while( ifn < g_list_length(if_list) )
535 curr = g_list_nth(if_list, ifn);
539 if_info = curr->data;
541 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
552 * Used to retrieve the two chars string from interface
555 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
561 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
563 number = g_strdup_printf("%.2u\0",n);
569 * Used to retrieve the two chars string from interface
572 airpcap_get_if_string_number_from_description(gchar* description)
577 number = (gchar*)g_malloc(sizeof(gchar)*3);
579 pointer = g_strrstr(description,"#\0");
581 number[0] = *(pointer+1);
582 number[1] = *(pointer+2);
589 * Returns the default airpcap interface of a list, NULL if list is empty
592 airpcap_get_default_if(GList* airpcap_if_list)
595 GList* popdown_if_list = NULL;
599 airpcap_if_info_t* if_info = NULL;
601 if(prefs.capture_device != NULL)
603 s = g_strdup(get_if_name(prefs.capture_device));
604 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
611 * Load the configuration for the specified interface
614 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
616 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
621 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
625 /* Stop blinking (if it was blinkig!)*/
626 if(if_info->blinking)
628 /* Turn on the light (if it was off) */
629 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
632 /* Apply settings... */
633 airpcap_if_get_device_channel(ad,&(if_info->channel));
634 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
635 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
636 airpcap_if_get_link_type(ad,&(if_info->linkType));
637 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
638 /* get the keys, if everything is ok, close the adapter */
639 if(airpcap_if_load_keys(ad,if_info))
640 airpcap_if_close(ad);
642 if_info->saved = TRUE;
646 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
652 * Save the configuration for the specified interface
655 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
657 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
662 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
666 /* Stop blinking (if it was blinkig!)*/
667 if(if_info->blinking)
669 /* Turn on the light (if it was off) */
670 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
673 /* Apply settings... */
674 airpcap_if_set_device_channel(ad,if_info->channel);
675 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
676 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
677 airpcap_if_set_link_type(ad,if_info->linkType);
678 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
679 airpcap_if_save_keys(ad,if_info);
681 /* ... and save them */
682 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
684 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.");
685 if_info->saved = FALSE;
686 airpcap_if_close(ad);
690 if_info->saved = TRUE;
691 airpcap_if_close(ad);
695 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
701 * Used to dinamically load the airpcap library in order link it only when
702 * it's present on the system
704 BOOL load_airpcap(void)
706 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
708 /* Report the error but go on */
713 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) return FALSE;
714 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) return FALSE;
715 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) return FALSE;
716 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) return FALSE;
717 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) return FALSE;
718 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) return FALSE;
719 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) return FALSE;
720 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) return FALSE;
721 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) return FALSE;
722 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) return FALSE;
723 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) return FALSE;
724 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) return FALSE;
725 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) return FALSE;
726 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) return FALSE;
727 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) return FALSE;
728 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) return FALSE;
729 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) return FALSE;
730 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) return FALSE;
731 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) return FALSE;
732 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) return FALSE;
733 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) return FALSE;
734 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) return FALSE;
735 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) return FALSE;
736 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) return FALSE;
737 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) return FALSE;
738 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) return FALSE;
739 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) return FALSE;