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;
84 /* Airpcap interface list */
85 GList *airpcap_if_list = NULL;
87 /* Airpcap current selected interface */
88 airpcap_if_info_t *airpcap_if_selected = NULL;
90 /* Airpcap current active interface */
91 airpcap_if_info_t *airpcap_if_active = NULL;
94 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
95 * "get_airpcap_interface_list()".
98 cant_get_airpcap_if_list_error_message(const char *err_str)
100 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
104 * Airpcap wrapper, used to store the current settings for the selected adapter
107 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
109 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
113 * Airpcap wrapper, used to open an airpcap adapter
116 airpcap_if_open(PCHAR name, PCHAR err)
118 return g_PAirpcapOpen(name,err);
122 * Airpcap wrapper, used to close an airpcap adapter
125 airpcap_if_close(PAirpcapHandle handle)
127 g_PAirpcapClose(handle);
132 * Airpcap wrapper, used to turn on the led of an airpcap adapter
135 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
137 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
141 * Airpcap wrapper, used to turn off the led of an airpcap adapter
144 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
146 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
150 * Airpcap wrapper, used to get the channel of an airpcap adapter
153 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
155 return g_PAirpcapGetDeviceChannel(ah,ch);
159 * Airpcap wrapper, used to set the channel of an airpcap adapter
162 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
164 return g_PAirpcapSetDeviceChannel(ah,ch);
168 * Airpcap wrapper, used to get the link type of an airpcap adapter
171 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
173 return g_PAirpcapGetLinkType(ah,lt);
177 * Airpcap wrapper, used to set the link type of an airpcap adapter
180 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
182 return g_PAirpcapSetLinkType(ah,lt);
186 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
189 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
191 return g_PAirpcapGetFcsPresence(ah,fcs);
195 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
198 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
200 return g_PAirpcapSetFcsPresence(ah,fcs);
204 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
207 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
209 return g_PAirpcapGetDecryptionState(ah,PEnable);
213 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
216 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
218 return g_PAirpcapSetDecryptionState(ah,Enable);
222 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
225 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
227 return g_PAirpcapGetFcsValidation(ah,val);
231 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
234 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
236 return g_PAirpcapSetFcsValidation(ah,val);
240 * Airpcap wrapper, used to save the settings for the selected_if
243 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
245 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
249 * Airpcap wrapper, used to save the settings for the selected_if
252 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
254 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
258 * This function will create a new airpcap_if_info_t using a name and a description
261 airpcap_if_info_new(char *name, char *description)
266 airpcap_if_info_t *if_info;
268 if_info = g_malloc(sizeof (airpcap_if_info_t));
269 if_info->name = g_strdup(name);
270 if (description == NULL)
271 if_info->description = NULL;
273 if_info->description = g_strdup(description);
274 if_info->ip_addr = NULL;
275 if_info->loopback = FALSE;
277 /* Probably I have to switch on the leds!!! */
278 ad = airpcap_if_open(if_info->name, ebuf);
281 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
282 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
283 airpcap_if_get_link_type(ad,&(if_info->linkType));
284 airpcap_if_get_device_channel(ad,&(if_info->channel));
285 airpcap_if_turn_led_on(ad, 0);
286 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
288 if_info->blinking = FALSE;
289 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
291 /* get the keys, if everything is ok, close the adapter */
292 if(airpcap_if_load_keys(ad,if_info))
293 airpcap_if_close(ad);
299 * Function used to load the WEP keys for a selected interface
302 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
304 if_info->keysCollectionSize = 0;
305 if_info->keysCollection = NULL;
307 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
309 if(if_info->keysCollectionSize == 0)
311 if_info->keysCollection = NULL;
312 airpcap_if_close(ad);
316 if_info->keysCollection = (PAirpcapKeysCollection)malloc(if_info->keysCollectionSize);
317 if(!if_info->keysCollection)
319 if_info->keysCollectionSize = 0;
320 if_info->keysCollection = NULL;
321 airpcap_if_close(ad);
325 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
328 airpcap_if_close(ad);
333 * Function used to save the WEP keys for a selected interface
336 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
338 if(if_info->keysCollection != NULL)
339 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
343 * Callback used to free an instance of airpcap_if_info_t
346 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
348 airpcap_if_info_t *if_info = data;
350 if (if_info->name != NULL)
351 g_free(if_info->name);
353 if (if_info->description != NULL)
354 g_free(if_info->description);
356 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
357 if(if_info->keysCollection != NULL)
358 g_free(if_info->keysCollection);
360 if(if_info->ip_addr != NULL)
361 g_slist_free(if_info->ip_addr);
368 * Function used to free the airpcap interface list
371 free_airpcap_interface_list(GList *if_list)
373 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
374 g_list_free(if_list);
379 * This function will use the airpcap.dll to find all the airpcap devices.
380 * Will return null if no device is found.
383 get_airpcap_interface_list(int *err, char *err_str)
386 airpcap_if_info_t *if_info;
388 AirpcapDeviceDescription *devsList, *adListEntry;
390 if(!g_PAirpcapGetDeviceList(&devsList, err_str))
392 /* No interfaces, return il = NULL; */
393 *err = NO_AIRPCAP_INTERFACES_FOUND;
400 adListEntry = devsList;
405 adListEntry = adListEntry->next;
410 /* No interfaces, return il= NULL */
411 g_PAirpcapFreeDeviceList(devsList);
412 *err = NO_AIRPCAP_INTERFACES_FOUND;
417 * Insert the adapters in our list
419 adListEntry = devsList;
420 for(i = 0; i < n_adapts; i++)
422 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
423 il = g_list_append(il, if_info);
425 adListEntry = adListEntry->next;
428 g_PAirpcapFreeDeviceList(devsList);
434 * Used to retrieve the name of the interface given the description
435 * (the name is used in AirpcapOpen, the description is put in the combo box)
437 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
441 airpcap_if_info_t* if_info;
446 while( ifn < g_list_length(if_list) )
448 curr = g_list_nth(if_list, ifn);
452 if_info = curr->data;
454 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
456 return if_info->name;
465 * Used to retrieve the interface given the name
466 * (the name is used in AirpcapOpen)
468 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
472 airpcap_if_info_t* if_info;
477 while( ifn < g_list_length(if_list) )
479 curr = g_list_nth(if_list, ifn);
483 if_info = curr->data;
485 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
496 * Returns the ASCII string of a key given the key bites
499 airpcap_get_key_string(AirpcapKey key)
507 if(key.KeyType == AIRPCAP_KEYTYPE_WEP)
509 s = g_strdup_printf("");
510 for(j = 0; j < key.KeyLen != 0; j++)
512 s1 = g_strdup_printf("%.2x", key.KeyData[j]);
513 g_strlcat(s,s1,WEP_KEY_MAX_SIZE);
520 * Used to retrieve the airpcap_if_info_t of the selected interface given the
521 * description (that is the entry of the combo box).
523 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
527 airpcap_if_info_t* if_info;
532 while( ifn < g_list_length(if_list) )
534 curr = g_list_nth(if_list, ifn);
538 if_info = curr->data;
540 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
551 * Used to retrieve the two chars string from interface
554 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
560 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
562 number = g_strdup_printf("%.2u\0",n);
568 * Used to retrieve the two chars string from interface
571 airpcap_get_if_string_number_from_description(gchar* description)
576 number = (gchar*)g_malloc(sizeof(gchar)*3);
578 pointer = g_strrstr(description,"#\0");
580 number[0] = *(pointer+1);
581 number[1] = *(pointer+2);
588 * Returns the default airpcap interface of a list, NULL if list is empty
591 airpcap_get_default_if(GList* airpcap_if_list)
594 GList* popdown_if_list = NULL;
598 airpcap_if_info_t* if_info = NULL;
600 if(prefs.capture_device != NULL)
602 s = g_strdup(get_if_name(prefs.capture_device));
603 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
610 * Load the configuration for the specified interface
613 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
615 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
620 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
624 /* Stop blinking (if it was blinkig!)*/
625 if(if_info->blinking)
627 /* Turn on the light (if it was off) */
628 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
631 /* Apply settings... */
632 airpcap_if_get_device_channel(ad,&(if_info->channel));
633 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
634 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
635 airpcap_if_get_link_type(ad,&(if_info->linkType));
636 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
637 /* get the keys, if everything is ok, close the adapter */
638 if(airpcap_if_load_keys(ad,if_info))
639 airpcap_if_close(ad);
641 if_info->saved = TRUE;
645 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
651 * Save the configuration for the specified interface
654 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
656 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
661 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
665 /* Stop blinking (if it was blinkig!)*/
666 if(if_info->blinking)
668 /* Turn on the light (if it was off) */
669 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
672 /* Apply settings... */
673 airpcap_if_set_device_channel(ad,if_info->channel);
674 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
675 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
676 airpcap_if_set_link_type(ad,if_info->linkType);
677 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
678 airpcap_if_save_keys(ad,if_info);
680 /* ... and save them */
681 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
683 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.");
684 if_info->saved = FALSE;
685 airpcap_if_close(ad);
689 if_info->saved = TRUE;
690 airpcap_if_close(ad);
694 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
700 * Used to dinamically load the airpcap library in order link it only when
701 * it's present on the system
703 BOOL load_airpcap(void)
705 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
707 /* Report the error but go on */
712 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) return FALSE;
713 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) return FALSE;
714 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) return FALSE;
715 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) return FALSE;
716 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) return FALSE;
717 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) return FALSE;
718 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) return FALSE;
719 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) return FALSE;
720 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) return FALSE;
721 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) return FALSE;
722 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) return FALSE;
723 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) return FALSE;
724 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) return FALSE;
725 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) return FALSE;
726 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) return FALSE;
727 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) return FALSE;
728 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) return FALSE;
729 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) return FALSE;
730 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) return FALSE;
731 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) return FALSE;
732 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) return FALSE;
733 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) return FALSE;
734 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) return FALSE;
735 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) return FALSE;
736 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) return FALSE;
737 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) return FALSE;
738 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) return FALSE;