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 <epan/packet.h>
43 #include <epan/prefs.h>
44 #include <epan/prefs-int.h>
45 #include "capture_ui_utils.h"
47 #include "simple_dialog.h"
50 #include "airpcap_loader.h"
53 * We load dinamically the dag library in order link it only when
54 * it's present on the system
56 static HMODULE AirpcapLib = NULL;
59 * Set to TRUE if the DLL was successfully loaded AND all functions
62 static gboolean AirpcapLoaded = FALSE;
64 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
65 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
66 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
67 static AirpcapOpenHandler g_PAirpcapOpen;
68 static AirpcapCloseHandler g_PAirpcapClose;
69 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
70 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
71 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
72 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
73 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
74 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
75 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
76 static AirpcapReadHandler g_PAirpcapRead;
77 static AirpcapGetStatsHandler g_PAirpcapGetStats;
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;
97 /* Airpcap interface list */
98 GList *airpcap_if_list = NULL;
100 /* Airpcap current selected interface */
101 airpcap_if_info_t *airpcap_if_selected = NULL;
103 /* Airpcap current active interface */
104 airpcap_if_info_t *airpcap_if_active = NULL;
106 /* WLAN preferences pointer */
107 module_t *wlan_prefs = NULL;
109 /* Callback used by the load_wlan_keys() routine in order to read a WEP decryption key */
111 get_wep_key(pref_t *pref, gpointer ud _U_)
113 gchar *my_string = NULL;
114 keys_cb_data_t* user_data;
116 decryption_key_t* new_key;
118 /* Retrieve user data info */
119 user_data = (keys_cb_data_t*)ud;
121 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
123 my_string = g_strdup(*pref->varp.string);
125 if( my_string != NULL)
127 /* Key is added only if not null ... */
128 if( (g_strcasecmp(my_string,"") != 0) && (wep_key_is_valid(my_string)))
130 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
132 new_key->key = g_string_new(my_string);
135 new_key->bits = new_key->key->len * 4;
137 new_key->type = AIRPCAP_KEYTYPE_WEP;
139 new_key->ssid = NULL;
141 user_data->list = g_list_append(user_data->list,new_key);
142 user_data->number_of_keys++;
143 user_data->current_index++;
150 /* Callback used by the load_wlan_keys() routine in order to read a WPA decryption key */
152 get_wpa_key(pref_t *pref, gpointer ud _U_)
157 /* Callback used by the load_wlan_keys() routine in order to read a WPA2 decryption key */
159 get_wpa2_key(pref_t *pref, gpointer ud _U_)
164 /* Returs TRUE if the WEP key is valid, false otherwise */
166 wep_key_is_valid(char* key)
168 GString *new_key_string;
174 new_key_string = g_string_new(key);
176 if( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
178 g_string_free(new_key_string,FALSE);
181 if((new_key_string->len % 2) != 0)
183 g_string_free(new_key_string,FALSE);
186 for(i = 0; i < new_key_string->len; i++)
188 if(!g_ascii_isxdigit(new_key_string->str[i]))
190 g_string_free(new_key_string,FALSE);
195 g_string_free(new_key_string,FALSE);
199 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
201 set_wep_key(pref_t *pref, gpointer ud _U_)
203 gchar *my_string = NULL;
204 keys_cb_data_t* user_data;
205 gint wep_key_number = 0;
207 /* Retrieve user data info */
208 user_data = (keys_cb_data_t*)ud;
210 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
212 /* Ok, the pref we're gonna set is a wep_key ... but what number? */
213 sscanf(pref->name,"wep_key%d",&wep_key_number);
215 if(user_data->current_index < user_data->number_of_keys)
217 if(wep_key_number == (user_data->current_index+1))
219 my_string = g_strdup((char*)g_list_nth_data(user_data->list,user_data->current_index));
221 g_free((void *)*pref->varp.string);
222 *pref->varp.string = (void *)g_strdup(my_string);
227 else /* If the number of keys has been reduced somehow, we need to delete all the other keys
228 * (remember that the new ones have been probably overwritten)
231 g_free((void *)*pref->varp.string);
232 *pref->varp.string = (void *)g_strdup(""); /* Do not just free memory!!! Put an 'empty' string! */
234 user_data->current_index++;
241 * Function used to read the Decryption Keys from the preferences and store them
242 * properly into the airpcap adapter.
245 load_wlan_wep_keys(airpcap_if_info_t* info_if)
247 keys_cb_data_t* user_data;
251 if(info_if == NULL) return FALSE;
253 /* Retrieve the wlan preferences */
254 wlan_prefs = prefs_find_module("wlan");
256 /* Allocate a structure used to keep infos between the callbacks */
257 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
259 /* Fill the structure */
260 user_data->list = NULL;
261 user_data->current_index = 0;
262 user_data->number_of_keys= 0; /* Still unknown */
264 /* Run the callback on each 802.11 preference */
265 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
267 /* Now the key list should be filled */
270 * Signal that we've changed things, and run the 802.11 dissector's
273 wlan_prefs->prefs_changed = TRUE;
275 prefs_apply(wlan_prefs);
277 write_wlan_wep_keys_to_regitry(info_if,user_data->list);
280 /* free the WEP key string */
281 for(i=0;i<g_list_length(user_data->list);i++)
283 g_free(g_list_nth(user_data->list,i)->data);
286 /* free the (empty) list */
287 g_list_free(user_data->list);
289 /* free the user_data structure */
298 * Function used to read the Decryption Keys from the preferences and store them
299 * properly into the airpcap adapter.
302 load_wlan_driver_wep_keys()
304 keys_cb_data_t* user_data;
308 /* Retrieve the wlan preferences */
309 wlan_prefs = prefs_find_module("wlan");
311 /* Allocate a structure used to keep infos between the callbacks */
312 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
314 /* Fill the structure */
315 user_data->list = NULL;
316 user_data->current_index = 0;
317 user_data->number_of_keys= 0; /* Still unknown */
319 /* Run the callback on each 802.11 preference */
320 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
322 /* Now the key list should be filled */
325 * Signal that we've changed things, and run the 802.11 dissector's
328 wlan_prefs->prefs_changed = TRUE;
330 prefs_apply(wlan_prefs);
332 write_wlan_driver_wep_keys_to_regitry(user_data->list);
335 /* free the WEP key string */
336 for(i=0;i<g_list_length(user_data->list);i++)
338 g_free(g_list_nth(user_data->list,i)->data);
341 /* free the (empty) list */
342 g_list_free(user_data->list);
344 /* free the user_data structure */
347 /* airpcap_if_info_free(fake_info_if); */
353 * This function will tell the airpcap driver the key list to use
354 * This will be stored into the registry...
357 write_wlan_wep_keys_to_regitry(airpcap_if_info_t* info_if, GList* key_list)
362 PAirpcapKeysCollection KeysCollection;
363 ULONG KeysCollectionSize;
365 UINT keys_in_list = 0;
366 decryption_key_t* key_item = NULL;
368 keys_in_list = g_list_length(key_list);
371 * Save the encryption keys, if we have any of them
373 KeysCollectionSize = 0;
376 * Calculate the size of the keys collection
378 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
381 * Allocate the collection
383 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
390 * Populate the key collection
392 KeysCollection->nKeys = keys_in_list;
394 for(i = 0; i < keys_in_list; i++)
396 KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_WEP;
398 /* Retrieve the Item corresponding to the i-th key */
399 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
400 new_key = g_string_new(key_item->key->str);
402 KeysCollection->Keys[i].KeyLen = new_key->len / 2;
403 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
405 for(j = 0 ; j < new_key->len; j += 2)
407 s[0] = new_key->str[j];
408 s[1] = new_key->str[j+1];
410 KeyByte = (UCHAR)strtol(s, NULL, 16);
411 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
414 g_string_free(new_key,TRUE);
418 * Free the old adapter key collection!
420 if(info_if->keysCollection != NULL)
421 g_free(info_if->keysCollection);
424 * Set this collection ad the new one
426 info_if->keysCollection = KeysCollection;
427 info_if->keysCollectionSize = KeysCollectionSize;
430 * Configuration must be saved
432 info_if->saved = FALSE;
435 * Write down the changes to the registry
437 airpcap_save_selected_if_configuration(info_if);
443 * This function will tell the airpcap driver the key list to use
444 * This will be stored into the registry...
447 write_wlan_driver_wep_keys_to_regitry(GList* key_list)
452 PAirpcapKeysCollection KeysCollection;
453 ULONG KeysCollectionSize;
455 UINT keys_in_list = 0;
456 decryption_key_t* key_item = NULL;
457 airpcap_if_info_t* fake_info_if = NULL;
459 /* Create the fake_info_if from the first adapter of the list */
460 fake_info_if = airpcap_driver_fake_if_info_new();
462 keys_in_list = g_list_length(key_list);
465 * Save the encryption keys, if we have any of them
467 KeysCollectionSize = 0;
470 * Calculate the size of the keys collection
472 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
475 * Allocate the collection
477 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
484 * Populate the key collection
486 KeysCollection->nKeys = keys_in_list;
488 for(i = 0; i < keys_in_list; i++)
490 KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_WEP;
492 /* Retrieve the Item corresponding to the i-th key */
493 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
494 new_key = g_string_new(key_item->key->str);
496 KeysCollection->Keys[i].KeyLen = new_key->len / 2;
497 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
499 for(j = 0 ; j < new_key->len; j += 2)
501 s[0] = new_key->str[j];
502 s[1] = new_key->str[j+1];
504 KeyByte = (UCHAR)strtol(s, NULL, 16);
505 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
508 g_string_free(new_key,TRUE);
512 * Free the old adapter key collection!
514 if(fake_info_if->keysCollection != NULL)
515 g_free(fake_info_if->keysCollection);
518 * Set this collection ad the new one
520 fake_info_if->keysCollection = KeysCollection;
521 fake_info_if->keysCollectionSize = KeysCollectionSize;
524 * Configuration must be saved
526 fake_info_if->saved = FALSE;
529 * Write down the changes to the registry
531 airpcap_save_driver_if_configuration(fake_info_if);
533 airpcap_if_info_free(fake_info_if);
539 * Function used to save to the preference file the Decryption Keys.
542 save_wlan_wep_keys(airpcap_if_info_t* info_if)
544 GList* key_list = NULL;
545 char* tmp_key = NULL;
546 guint keys_in_list,i;
547 keys_cb_data_t* user_data;
549 if(info_if == NULL) return FALSE;
551 /* Retrieve the wlan preferences */
552 wlan_prefs = prefs_find_module("wlan");
554 /* Allocate a structure used to keep infos between the callbacks */
555 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
557 /* Number of keys in key list */
558 /* Number of keys in key list */
559 if(info_if->keysCollectionSize != 0)
560 keys_in_list = (guint)(info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
564 for(i=0; i<keys_in_list; i++)
566 /* Only if it is a WEP key... */
567 if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
569 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
570 key_list = g_list_append(key_list,g_strdup(tmp_key));
575 /* Now we know the exact number of WEP keys in the list, so store it ... */
576 keys_in_list = g_list_length(key_list);
578 /* Fill the structure */
579 user_data->list = key_list;
580 user_data->current_index = 0;
581 user_data->number_of_keys= keys_in_list;
583 /* Retrieve the wlan preferences */
584 wlan_prefs = prefs_find_module("wlan");
586 /* Run the callback on each 802.11 preference */
587 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
589 /* Signal that we've changed things, and run the 802.11 dissector's
591 wlan_prefs->prefs_changed = TRUE;
593 /* Apply changes for the specified preference */
594 prefs_apply(wlan_prefs);
597 /* free the WEP key string */
598 for(i=0;i<g_list_length(user_data->list);i++)
600 g_free(g_list_nth(user_data->list,i)->data);
603 /* free the (empty) list */
604 g_list_free(user_data->list);
606 /* free the user_data structure */
613 * Function used to save to the preference file the Decryption Keys.
616 save_wlan_driver_wep_keys()
618 GList* key_list = NULL;
619 char* tmp_key = NULL;
620 guint keys_in_list,i;
621 keys_cb_data_t* user_data;
622 airpcap_if_info_t* fake_info_if = NULL;
624 /* Create the fake_info_if from the first adapter of the list */
625 fake_info_if = airpcap_driver_fake_if_info_new();
627 /* Retrieve the wlan preferences */
628 wlan_prefs = prefs_find_module("wlan");
630 /* Allocate a structure used to keep infos between the callbacks */
631 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
633 /* Number of keys in key list */
634 /* Number of keys in key list */
635 if(fake_info_if->keysCollectionSize != 0)
636 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
640 for(i=0; i<keys_in_list; i++)
642 /* Only if it is a WEP key... */
643 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
645 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
646 key_list = g_list_append(key_list,g_strdup(tmp_key));
651 /* Now we know the exact number of WEP keys in the list, so store it ... */
652 keys_in_list = g_list_length(key_list);
654 /* Fill the structure */
655 user_data->list = key_list;
656 user_data->current_index = 0;
657 user_data->number_of_keys= keys_in_list;
659 /* Retrieve the wlan preferences */
660 wlan_prefs = prefs_find_module("wlan");
662 /* Run the callback on each 802.11 preference */
663 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
665 /* Signal that we've changed things, and run the 802.11 dissector's
667 wlan_prefs->prefs_changed = TRUE;
669 /* Apply changes for the specified preference */
670 prefs_apply(wlan_prefs);
673 /* free the WEP key string */
674 for(i=0;i<g_list_length(user_data->list);i++)
676 g_free(g_list_nth(user_data->list,i)->data);
679 /* free the (empty) list */
680 g_list_free(user_data->list);
682 /* free the user_data structure */
685 airpcap_if_info_free(fake_info_if);
691 * Function used to save to the preference file the Decryption Keys.
694 save_wlan_wireshark_wep_keys(GList* key_ls)
696 GList* key_list = NULL;
697 char* tmp_key = NULL;
698 guint keys_in_list,i;
699 keys_cb_data_t* user_data;
700 airpcap_if_info_t* fake_info_if = NULL;
702 /* Retrieve the wlan preferences */
703 wlan_prefs = prefs_find_module("wlan");
705 /* Allocate a structure used to keep infos between the callbacks */
706 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
708 keys_in_list = g_list_length(key_ls);
712 /* Fill the structure */
713 user_data->list = key_list;
714 user_data->current_index = 0;
715 user_data->number_of_keys= keys_in_list;
717 /* Retrieve the wlan preferences */
718 wlan_prefs = prefs_find_module("wlan");
720 /* Run the callback on each 802.11 preference */
721 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
723 /* Signal that we've changed things, and run the 802.11 dissector's
725 wlan_prefs->prefs_changed = TRUE;
727 /* Apply changes for the specified preference */
728 prefs_apply(wlan_prefs);
731 /* free the WEP key string */
732 for(i=0;i<g_list_length(user_data->list);i++)
734 g_free(g_list_nth(user_data->list,i)->data);
737 /* free the (empty) list */
738 g_list_free(user_data->list);
740 /* free the user_data structure */
747 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
748 * "get_airpcap_interface_list()".
751 cant_get_airpcap_if_list_error_message(const char *err_str)
753 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
757 * Airpcap wrapper, used to store the current settings for the selected adapter
760 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
762 if (!AirpcapLoaded) return FALSE;
763 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
767 * Airpcap wrapper, used to open an airpcap adapter
770 airpcap_if_open(PCHAR name, PCHAR err)
772 if (!AirpcapLoaded) return NULL;
773 return g_PAirpcapOpen(name,err);
777 * Airpcap wrapper, used to close an airpcap adapter
780 airpcap_if_close(PAirpcapHandle handle)
782 if (!AirpcapLoaded) return;
783 g_PAirpcapClose(handle);
787 * Airpcap wrapper, used to turn on the led of an airpcap adapter
790 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
792 if (!AirpcapLoaded) return FALSE;
793 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
797 * Airpcap wrapper, used to turn off the led of an airpcap adapter
800 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
802 if (!AirpcapLoaded) return FALSE;
803 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
807 * Airpcap wrapper, used to get the channel of an airpcap adapter
810 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
812 if (!AirpcapLoaded) return FALSE;
813 return g_PAirpcapGetDeviceChannel(ah,ch);
817 * Airpcap wrapper, used to set the channel of an airpcap adapter
820 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
822 if (!AirpcapLoaded) return FALSE;
823 return g_PAirpcapSetDeviceChannel(ah,ch);
827 * Airpcap wrapper, used to get the link type of an airpcap adapter
830 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
832 if (!AirpcapLoaded) return FALSE;
833 return g_PAirpcapGetLinkType(ah,lt);
837 * Airpcap wrapper, used to set the link type of an airpcap adapter
840 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
842 if (!AirpcapLoaded) return FALSE;
843 return g_PAirpcapSetLinkType(ah,lt);
847 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
850 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
852 if (!AirpcapLoaded) return FALSE;
853 return g_PAirpcapGetFcsPresence(ah,fcs);
857 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
860 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
862 if (!AirpcapLoaded) return FALSE;
863 return g_PAirpcapSetFcsPresence(ah,fcs);
867 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
870 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
872 if (!AirpcapLoaded) return FALSE;
873 return g_PAirpcapGetDecryptionState(ah,PEnable);
877 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
880 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
882 if (!AirpcapLoaded) return FALSE;
883 return g_PAirpcapSetDecryptionState(ah,Enable);
887 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
890 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
892 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
893 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
897 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
900 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
902 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
903 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
907 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
910 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
912 if (!AirpcapLoaded) return FALSE;
913 return g_PAirpcapGetFcsValidation(ah,val);
917 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
920 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
922 if (!AirpcapLoaded) return FALSE;
923 return g_PAirpcapSetFcsValidation(ah,val);
927 * Airpcap wrapper, used to save the settings for the selected_if
930 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
932 if (!AirpcapLoaded) return FALSE;
933 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
937 * Airpcap wrapper, used to save the settings for the selected_if
940 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
942 if (!AirpcapLoaded) return FALSE;
943 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
947 * Airpcap wrapper, used to save the driver's set of keys
950 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
952 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
953 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
957 * Airpcap wrapper, used to load the driver's set of keys
960 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
962 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
963 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
967 * This function will create a new airpcap_if_info_t using a name and a description
970 airpcap_if_info_new(char *name, char *description)
973 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
975 airpcap_if_info_t *if_info;
977 if_info = g_malloc(sizeof (airpcap_if_info_t));
978 if_info->name = g_strdup(name);
979 if (description == NULL)
980 if_info->description = NULL;
982 if_info->description = g_strdup(description);
983 if_info->ip_addr = NULL;
984 if_info->loopback = FALSE;
986 /* Probably I have to switch on the leds!!! */
987 ad = airpcap_if_open(if_info->name, ebuf);
990 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
991 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
992 airpcap_if_get_link_type(ad,&(if_info->linkType));
993 airpcap_if_get_device_channel(ad,&(if_info->channel));
994 airpcap_if_turn_led_on(ad, 0);
995 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
997 if_info->blinking = FALSE;
998 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1000 /* get the keys, if everything is ok, close the adapter */
1001 if(airpcap_if_load_keys(ad,if_info))
1002 airpcap_if_close(ad);
1008 * This function will create a new fake drivers' interface, to load global keys...
1011 airpcap_driver_fake_if_info_new()
1014 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1016 airpcap_if_info_t *if_info = NULL;
1017 airpcap_if_info_t *fake_if_info = NULL;
1020 * Retrieve the first AirPcap adapter available. If no interface is found,
1021 * it is not possible to retrieve the driver's settings, so return NULL.
1023 if_info = g_list_nth_data(airpcap_if_list,0);
1027 fake_if_info = g_malloc(sizeof (airpcap_if_info_t));
1028 fake_if_info->name = g_strdup(if_info->name);
1029 fake_if_info->description = g_strdup(if_info->description);
1030 fake_if_info->loopback = FALSE;
1031 fake_if_info->ip_addr = NULL;
1033 /* Open the 'fake' adapter */
1034 ad = airpcap_if_open(if_info->name, ebuf);
1037 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
1038 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
1039 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
1040 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
1041 airpcap_if_get_device_channel(ad,&(fake_if_info->channel));
1042 airpcap_if_turn_led_on(ad, 0);
1043 fake_if_info->led = TRUE;
1044 fake_if_info->blinking = FALSE;
1045 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1047 /* get the keys, if everything is ok, close the adapter */
1048 if(airpcap_if_load_driver_keys(ad,fake_if_info))
1049 airpcap_if_close(ad);
1052 return fake_if_info;
1056 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
1059 airpcap_if_info_print(airpcap_if_info_t* if_info)
1063 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
1067 g_print("\n----------------- AirPcap Interface \n");
1068 g_print(" NAME: %s\n",if_info->name);
1069 g_print(" DESCRIPTION: %s\n",if_info->description);
1070 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
1071 g_print(" CHANNEL: %2u\n",if_info->channel);
1072 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
1073 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
1074 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
1075 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
1076 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
1077 g_print("KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
1078 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
1079 g_print(" LINKTYPE: %d\n",if_info->linkType);
1080 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
1081 g_print(" (GTK) TAG: %d\n",if_info->tag);
1086 * Function used to load the WEP keys for a selected interface
1089 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1091 if(!if_info) return FALSE;
1093 if_info->keysCollectionSize = 0;
1094 if_info->keysCollection = NULL;
1096 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1098 if(if_info->keysCollectionSize == 0)
1100 if_info->keysCollection = NULL;
1101 airpcap_if_close(ad);
1105 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1106 if(!if_info->keysCollection)
1108 if_info->keysCollectionSize = 0;
1109 if_info->keysCollection = NULL;
1110 airpcap_if_close(ad);
1114 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1118 airpcap_if_close(ad);
1123 * Function used to load the WEP keys for a selected interface
1126 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1128 if_info->keysCollectionSize = 0;
1129 if_info->keysCollection = NULL;
1131 if(!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1133 if(if_info->keysCollectionSize == 0)
1135 if_info->keysCollection = NULL;
1136 airpcap_if_close(ad);
1140 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1141 if(!if_info->keysCollection)
1143 if_info->keysCollectionSize = 0;
1144 if_info->keysCollection = NULL;
1145 airpcap_if_close(ad);
1149 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1153 airpcap_if_close(ad);
1158 * Function used to save the WEP keys for a selected interface
1161 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1163 if(!if_info || !AirpcapLoaded) return;
1165 if(if_info->keysCollection != NULL)
1166 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1170 * Function used to save the WEP keys for a selected interface
1173 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1175 if(if_info->keysCollection != NULL)
1176 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1180 * Callback used to free an instance of airpcap_if_info_t
1183 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1185 airpcap_if_info_t *if_info = data;
1187 if (if_info->name != NULL)
1188 g_free(if_info->name);
1190 if (if_info->description != NULL)
1191 g_free(if_info->description);
1193 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1194 if(if_info->keysCollection != NULL)
1196 g_free(if_info->keysCollection);
1197 if_info->keysCollection = NULL;
1200 if(if_info->ip_addr != NULL)
1201 g_slist_free(if_info->ip_addr);
1208 * Function used to free the airpcap interface list
1211 free_airpcap_interface_list(GList *if_list)
1213 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1214 g_list_free(if_list);
1219 * This function will use the airpcap.dll to find all the airpcap devices.
1220 * Will return null if no device is found.
1223 get_airpcap_interface_list(int *err, char *err_str)
1226 airpcap_if_info_t *if_info;
1228 AirpcapDeviceDescription *devsList, *adListEntry;
1231 *err = NO_AIRPCAP_INTERFACES_FOUND;
1233 if(!AirpcapLoaded || !g_PAirpcapGetDeviceList(&devsList, err_str))
1235 /* No interfaces, return il = NULL; */
1240 * Count the adapters
1242 adListEntry = devsList;
1247 adListEntry = adListEntry->next;
1252 /* No interfaces, return il= NULL */
1253 g_PAirpcapFreeDeviceList(devsList);
1258 * Insert the adapters in our list
1260 adListEntry = devsList;
1261 for(i = 0; i < n_adapts; i++)
1263 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1264 il = g_list_append(il, if_info);
1266 adListEntry = adListEntry->next;
1269 g_PAirpcapFreeDeviceList(devsList);
1275 * Used to retrieve the name of the interface given the description
1276 * (the name is used in AirpcapOpen, the description is put in the combo box)
1278 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
1282 airpcap_if_info_t* if_info;
1287 while( ifn < g_list_length(if_list) )
1289 curr = g_list_nth(if_list, ifn);
1293 if_info = curr->data;
1295 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1297 return if_info->name;
1306 * Used to retrieve the interface given the name
1307 * (the name is used in AirpcapOpen)
1309 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
1313 airpcap_if_info_t* if_info;
1318 while( ifn < g_list_length(if_list) )
1320 curr = g_list_nth(if_list, ifn);
1324 if_info = curr->data;
1326 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
1337 * Returns the ASCII string of a key given the key bytes
1340 airpcap_get_key_string(AirpcapKey key)
1348 if(key.KeyType == AIRPCAP_KEYTYPE_WEP)
1352 /* Allocate the string used to store the ASCII representation of the WEP key */
1353 dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1354 /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1357 for(j = 0; j < key.KeyLen; j++)
1359 src = g_strdup_printf("%.2x\0", key.KeyData[j]);
1361 * XXX - use g_strconcat() or GStrings instead ???
1363 l = g_strlcat(dst,src,WEP_KEY_MAX_CHAR_SIZE+1);
1368 else if(key.KeyType == AIRPCAP_KEYTYPE_TKIP)
1370 /* XXX - Add code here */
1372 else if(key.KeyType == AIRPCAP_KEYTYPE_CCMP)
1374 /* XXX - Add code here */
1378 /* XXX - Add code here */
1385 * Clear keys and decryption status for the specified interface
1388 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1392 if(info_if->keysCollection != NULL)
1394 g_free(info_if->keysCollection);
1395 info_if->keysCollection = NULL;
1398 info_if->keysCollectionSize = 0;
1400 info_if->DecryptionOn = FALSE;
1401 info_if->saved = FALSE;
1406 * Used to retrieve the airpcap_if_info_t of the selected interface given the
1407 * description (that is the entry of the combo box).
1409 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
1413 airpcap_if_info_t* if_info;
1418 while( ifn < g_list_length(if_list) )
1420 curr = g_list_nth(if_list, ifn);
1424 if_info = curr->data;
1426 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1437 * Used to retrieve the two chars string from interface
1440 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1446 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1448 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1449 * Otherwise, check if it is the "Any" adapter...
1453 if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1454 number = g_strdup_printf("??");
1456 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1460 number = g_strdup_printf("%.2u\0",n);
1467 * Used to retrieve the two chars string from interface
1470 airpcap_get_if_string_number_from_description(gchar* description)
1475 number = (gchar*)g_malloc(sizeof(gchar)*3);
1477 pointer = g_strrstr(description,"#\0");
1479 number[0] = *(pointer+1);
1480 number[1] = *(pointer+2);
1487 * Returns the default airpcap interface of a list, NULL if list is empty
1490 airpcap_get_default_if(GList* airpcap_if_list)
1493 GList* popdown_if_list = NULL;
1497 airpcap_if_info_t* if_info = NULL;
1499 if(prefs.capture_device != NULL)
1501 s = g_strdup(get_if_name(prefs.capture_device));
1502 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
1509 * Load the configuration for the specified interface
1512 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1514 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1519 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1523 /* Stop blinking (if it was blinkig!)*/
1524 if(if_info->blinking)
1526 /* Turn on the light (if it was off) */
1527 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1530 /* Apply settings... */
1531 airpcap_if_get_device_channel(ad,&(if_info->channel));
1532 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1533 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1534 airpcap_if_get_link_type(ad,&(if_info->linkType));
1535 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1536 /* get the keys, if everything is ok, close the adapter */
1537 if(airpcap_if_load_keys(ad,if_info))
1538 airpcap_if_close(ad);
1540 if_info->saved = TRUE;
1544 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1550 * Save the configuration for the specified interface
1553 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1555 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1560 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1564 /* Stop blinking (if it was blinkig!)*/
1565 if(if_info->blinking)
1567 /* Turn on the light (if it was off) */
1568 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1571 /* Apply settings... */
1572 airpcap_if_set_device_channel(ad,if_info->channel);
1573 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1574 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1575 airpcap_if_set_link_type(ad,if_info->linkType);
1576 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1577 airpcap_if_save_keys(ad,if_info);
1579 /* ... and save them */
1580 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
1582 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.");
1583 if_info->saved = FALSE;
1584 airpcap_if_close(ad);
1588 if_info->saved = TRUE;
1589 airpcap_if_close(ad);
1593 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1599 * Save the configuration for the specified interface
1602 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1604 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1607 if(fake_if_info != NULL)
1609 ad = airpcap_if_open(fake_if_info->name, ebuf);
1613 /* Apply decryption settings... */
1614 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1615 airpcap_if_save_driver_keys(ad,fake_if_info);
1616 airpcap_if_close(ad);
1620 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1628 * DECRYPTION KEYS FUNCTIONS
1631 * This function is used for DEBUG POURPOSES ONLY!!!
1634 print_key_list(GList* key_list)
1637 decryption_key_t* tmp;
1639 if(key_list == NULL)
1641 g_print("\n\n******* KEY LIST NULL *******\n\n");
1645 n = g_list_length(key_list);
1647 g_print("\n\n********* KEY LIST **********\n\n");
1649 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1651 for(i =0; i < n; i++)
1653 g_print("[%d] :\n",i+1);
1654 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1655 g_print("KEY : %s\n",tmp->key->str);
1657 g_print("BITS: %d\n",tmp->bits);
1659 if(tmp->type == AIRPCAP_KEYTYPE_WEP)
1660 g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1661 else if(tmp->type == AIRPCAP_KEYTYPE_TKIP)
1662 g_print("TYPE: %s\n",AIRPCAP_WPA_KEY_STRING);
1663 else if(tmp->type == AIRPCAP_KEYTYPE_CCMP)
1664 g_print("TYPE: %s\n",AIRPCAP_WPA2_KEY_STRING);
1666 g_print("TYPE: %s\n","???");
1668 g_print("SSID: %s\n",(tmp->ssid != NULL) ? tmp->ssid->str : "---");
1672 g_print("\n*****************************\n\n");
1676 * Retrieves a GList of decryption_key_t structures containing infos about the
1677 * keys for the given adapter... returns NULL if no keys are found.
1680 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1683 char* tmp_key = NULL;
1684 guint i,keys_in_list = 0;
1687 decryption_key_t *new_key = NULL;
1688 GList *key_list = NULL;
1690 /* Number of keys in key list */
1691 if(info_if->keysCollectionSize != 0)
1692 keys_in_list = (guint)(info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1696 for(i=0; i<keys_in_list; i++)
1698 /* Different things to do depending on the key type */
1699 if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
1701 /* allocate memory for the new key item */
1702 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1704 /* fill the fields */
1706 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1707 new_key->key = g_string_new(tmp_key);
1711 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1713 /* SSID not used in WEP keys */
1714 new_key->ssid = NULL;
1716 /* TYPE (WEP in this case) */
1717 new_key->type = info_if->keysCollection->Keys[i].KeyType;
1719 /* Append the new element in the list */
1720 key_list = g_list_append(key_list,(gpointer)new_key);
1722 else if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_TKIP)
1724 /* XXX - Not supported yet */
1726 else if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_CCMP)
1728 /* XXX - Not supported yet */
1736 * Retrieves a GList of decryption_key_t structures containing infos about the
1737 * keys for the global AirPcap driver... returns NULL if no keys are found.
1740 get_airpcap_driver_keys()
1743 char* tmp_key = NULL;
1744 guint i,keys_in_list = 0;
1747 decryption_key_t *new_key = NULL;
1748 GList *key_list = NULL;
1751 * To read the drivers general settings we need to create and use one airpcap adapter...
1752 * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1754 airpcap_if_info_t* fake_info_if = NULL;
1756 /* Create the fake_info_if from the first adapter of the list */
1757 fake_info_if = airpcap_driver_fake_if_info_new();
1759 if(fake_info_if == NULL)
1762 /* Number of keys in key list */
1763 if(fake_info_if->keysCollectionSize != 0)
1764 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1768 for(i=0; i<keys_in_list; i++)
1770 /* Different things to do depending on the key type */
1771 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
1773 /* allocate memory for the new key item */
1774 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1776 /* fill the fields */
1778 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1779 new_key->key = g_string_new(tmp_key);
1783 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1785 /* SSID not used in WEP keys */
1786 new_key->ssid = NULL;
1788 /* TYPE (WEP in this case) */
1789 new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1791 /* Append the new element in the list */
1792 key_list = g_list_append(key_list,(gpointer)new_key);
1794 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_TKIP)
1796 /* XXX - Not supported yet */
1798 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_CCMP)
1800 /* XXX - Not supported yet */
1804 airpcap_if_info_free(fake_info_if);
1810 * Returns the list of the decryption keys specified for wireshark, NULL if
1814 get_wireshark_keys()
1816 keys_cb_data_t* wep_user_data = NULL;
1817 keys_cb_data_t* wpa_user_data = NULL;
1818 keys_cb_data_t* wpa2_user_data= NULL;
1822 GList* final_list = NULL;
1823 GList* wep_final_list = NULL;
1824 GList* wpa_final_list = NULL;
1825 GList* wpa2_final_list = NULL;
1827 /* Retrieve the wlan preferences */
1828 wlan_prefs = prefs_find_module("wlan");
1830 /* Allocate a structure used to keep infos between the callbacks */
1831 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1833 /* Fill the structure */
1834 wep_user_data->list = NULL;
1835 wep_user_data->current_index = 0;
1836 wep_user_data->number_of_keys= 0; /* Still unknown */
1838 /* Run the callback on each 802.11 preference */
1839 /* XXX - Right now, only WEP keys will be loaded */
1840 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1841 prefs_pref_foreach(wlan_prefs, get_wpa_key, (gpointer)wpa_user_data);
1842 prefs_pref_foreach(wlan_prefs, get_wpa2_key, (gpointer)wpa2_user_data);
1844 /* Copy the list field in the user data structure pointer into the final_list */
1845 if(wep_user_data != NULL) wep_final_list = wep_user_data->list;
1846 if(wpa_user_data != NULL) wpa_final_list = wpa_user_data->list;
1847 if(wpa2_user_data != NULL) wpa2_final_list = wpa2_user_data->list;
1849 /* XXX - Merge the three lists!!!!! */
1850 final_list = wep_final_list;
1852 /* free the wep_user_data structure */
1853 g_free(wep_user_data);
1854 /* free the wpa_user_data structure */
1855 g_free(wpa_user_data);
1856 /* free the wpa2_user_data structure */
1857 g_free(wpa2_user_data);
1863 * Merges two lists of keys and return a newly created GList. If a key is
1864 * found multiple times, it will just appear once!
1865 * list1 and list 2 pointer will have to be freed manually if needed!!!
1866 * If the total number of keys exceeeds the maximum number allowed,
1867 * exceeding keys will be discarded...
1870 merge_key_list(GList* list1, GList* list2)
1874 decryption_key_t *dk1=NULL,
1878 GList* merged_list = NULL;
1880 if( (list1 == NULL) && (list2 == NULL) )
1886 n2 = g_list_length(list2);
1890 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1891 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1893 new_dk->bits = dk2->bits;
1894 new_dk->type = dk2->type;
1895 new_dk->key = g_string_new(dk2->key->str);
1896 if(dk2->ssid != NULL)
1897 new_dk->ssid = g_string_new(dk2->ssid->str);
1899 new_dk->ssid = NULL;
1901 /* Check the total length of the merged list */
1902 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1903 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1906 else if(list2 == NULL)
1908 n1 = g_list_length(list1);
1913 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1914 dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1916 new_dk->bits = dk1->bits;
1917 new_dk->type = dk1->type;
1918 new_dk->key = g_string_new(dk1->key->str);
1919 if(dk1->ssid != NULL)
1920 new_dk->ssid = g_string_new(dk1->ssid->str);
1922 new_dk->ssid = NULL;
1924 /* Check the total length of the merged list */
1925 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1926 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1931 n1 = g_list_length(list1);
1932 n2 = g_list_length(list2);
1934 /* Copy the whole list1 into merged_list */
1937 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1938 dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
1940 new_dk->bits = dk1->bits;
1941 new_dk->type = dk1->type;
1942 new_dk->key = g_string_new(dk1->key->str);
1944 if(dk1->ssid != NULL)
1945 new_dk->ssid = g_string_new(dk1->ssid->str);
1947 new_dk->ssid = NULL;
1949 /* Check the total length of the merged list */
1950 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1951 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1954 /* Look for keys that are present in list2 but aren't in list1 yet...
1955 * Add them to merged_list
1959 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1961 if(!key_is_in_list(dk2,merged_list))
1963 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1965 new_dk->bits = dk2->bits;
1966 new_dk->type = dk2->type;
1967 new_dk->key = g_string_new(dk2->key->str);
1968 if(dk2->ssid != NULL)
1969 new_dk->ssid = g_string_new(dk2->ssid->str);
1971 new_dk->ssid = NULL;
1973 /* Check the total length of the merged list */
1974 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1975 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1984 * Use this function to free a key list.
1987 free_key_list(GList *list)
1990 decryption_key_t *curr_key;
1995 n = g_list_length(list);
1997 for(i = 0; i < n; i++)
1999 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2001 /* Free all the strings */
2002 if(curr_key->key != NULL)
2003 g_string_free(curr_key->key,TRUE);
2005 if(curr_key->ssid != NULL)
2006 g_string_free(curr_key->ssid,TRUE);
2008 /* free the decryption_key_t structure*/
2021 * If the given key is contained in the list, returns TRUE.
2022 * Returns FALSE otherwise.
2025 key_is_in_list(decryption_key_t *dk,GList *list)
2028 decryption_key_t* curr_key = NULL;
2029 gboolean found = FALSE;
2031 if( (list == NULL) || (dk == NULL) )
2034 n = g_list_length(list);
2039 for(i = 0; i < n; i++)
2041 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2042 if(keys_are_equals(dk,curr_key))
2050 * Returns TRUE if keys are equals, FALSE otherwise
2053 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
2056 if((k1==NULL) || (k2==NULL))
2059 if( g_string_equal(k1->key,k2->key) &&
2060 (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
2061 k1->type == k2->type)
2063 /* Check the ssid... if the key type is WEP, the two fields should be NULL */
2064 if((k1->ssid == NULL) && (k2->ssid == NULL))
2067 /* Check if one of them is null and one is not... */
2068 if((k1->ssid == NULL) || (k2->ssid == NULL))
2071 /* If they are not null, they must share the same ssid */
2072 return g_string_equal(k1->ssid,k2->ssid);
2075 /* Some field is not equal ... */
2080 * Tests if two collection of keys are equal or not, to be considered equals, they have to
2081 * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
2085 key_lists_are_equal(GList* list1, GList* list2)
2089 decryption_key_t *dk1=NULL,*dk2=NULL;
2091 n1 = g_list_length(list1);
2092 n2 = g_list_length(list2);
2095 * Commented, because in the new AirPcap version all the keys will be saved
2096 * into the driver, and all the keys for every specific adapter will be
2097 * removed. This means that this check will always fail... and the user will
2098 * always be asked what to do... and it doesn't make much sense.
2100 if(n1 != n2) return FALSE;
2104 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2105 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2107 if(!g_string_equal(dk1->key,dk2->key)) return FALSE;
2114 test_if_on(pref_t *pref, gpointer ud _U_)
2119 /* Retrieve user data info */
2120 is_on = (gboolean*)ud;
2123 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2125 number = *pref->varp.boolp;
2127 if(number) *is_on = TRUE;
2128 else *is_on = FALSE;
2136 * Returns TRUE if the Wireshark decryption is active, false otherwise
2139 wireshark_decryption_on()
2143 /* Retrieve the wlan preferences */
2144 wlan_prefs = prefs_find_module("wlan");
2146 /* Run the callback on each 802.11 preference */
2147 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2153 * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2156 airpcap_decryption_on()
2158 gboolean is_on = FALSE;
2160 airpcap_if_info_t* fake_if_info = NULL;
2162 fake_if_info = airpcap_driver_fake_if_info_new();
2164 if(fake_if_info != NULL)
2166 if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2168 else if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2172 airpcap_if_info_free(fake_if_info);
2178 * Free an instance of airpcap_if_info_t
2181 airpcap_if_info_free(airpcap_if_info_t *if_info)
2185 if (if_info->name != NULL)
2186 g_free(if_info->name);
2188 if (if_info->description != NULL)
2189 g_free(if_info->description);
2191 if(if_info->keysCollection != NULL)
2193 g_free(if_info->keysCollection);
2194 if_info->keysCollection = NULL;
2197 if(if_info->ip_addr != NULL)
2199 g_slist_free(if_info->ip_addr);
2200 if_info->ip_addr = NULL;
2212 set_on_off(pref_t *pref, gpointer ud _U_)
2217 /* Retrieve user data info */
2218 is_on = (gboolean*)ud;
2220 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2222 number = *pref->varp.boolp;
2224 g_free((void *)*pref->varp.boolp);
2226 *pref->varp.boolp = TRUE;
2228 *pref->varp.boolp = FALSE;
2236 * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2239 set_wireshark_decryption(gboolean on_off)
2245 /* Retrieve the wlan preferences */
2246 wlan_prefs = prefs_find_module("wlan");
2248 /* Run the callback on each 802.11 preference */
2249 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2252 * Signal that we've changed things, and run the 802.11 dissector's
2255 wlan_prefs->prefs_changed = TRUE;
2257 prefs_apply(wlan_prefs);
2261 * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2264 set_airpcap_decryption(gboolean on_off)
2266 /* We need to directly access the .dll functions here... */
2267 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2268 PAirpcapHandle ad,ad_driver;
2270 gboolean success = TRUE;
2274 airpcap_if_info_t* curr_if = NULL;
2275 airpcap_if_info_t* fake_if_info = NULL;
2277 fake_if_info = airpcap_driver_fake_if_info_new();
2279 if(fake_if_info == NULL)
2280 /* We apparently don't have any adapters installed.
2281 * This isn't a failure, so return TRUE
2285 /* Set the driver decryption */
2286 ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2290 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2292 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2294 airpcap_if_close(ad_driver);
2297 airpcap_if_info_free(fake_if_info);
2299 n = g_list_length(airpcap_if_list);
2301 /* Set to FALSE the decryption for all the adapters */
2302 /* Apply this change to all the adapters !!! */
2303 for(i = 0; i < n; i++)
2305 curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2307 if( curr_if != NULL )
2309 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list,curr_if->description), ebuf);
2312 curr_if->DecryptionOn = (gboolean)AIRPCAP_DECRYPTION_OFF;
2313 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2314 /* Save configuration for the curr_if */
2315 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
2319 airpcap_if_close(ad);
2328 /* DYNAMIC LIBRARY LOADER */
2330 * Used to dynamically load the airpcap library in order link it only when
2331 * it's present on the system
2333 int load_airpcap(void)
2335 BOOL base_functions = TRUE;
2336 BOOL new_functions = TRUE;
2338 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
2340 /* Report the error but go on */
2341 return AIRPCAP_DLL_NOT_FOUND;
2345 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2346 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2347 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2348 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2349 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2350 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2351 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2352 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2353 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2354 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2355 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2356 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2357 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2358 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2359 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2360 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2361 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2362 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2363 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2364 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2365 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2366 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2367 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2368 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2369 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2370 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2371 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2372 if((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2374 /* TEST IF WE CAN FIND AIRPCAP NEW DRIVER FEATURES */
2375 if((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) new_functions = FALSE;
2376 if((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) new_functions = FALSE;
2377 if((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) new_functions = FALSE;
2378 if((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) new_functions = FALSE;
2384 AirpcapLoaded = TRUE;
2385 return AIRPCAP_DLL_OK;
2389 AirpcapLoaded = TRUE;
2390 return AIRPCAP_DLL_OLD;
2395 AirpcapLoaded = FALSE;
2396 return AIRPCAP_DLL_ERROR;
2402 * Append the version of AirPcap with which we were compiled to a GString.
2405 get_compiled_airpcap_version(GString *str)
2407 g_string_append(str, "with AirPcap");
2411 * Append the version of AirPcap with which we we're running to a GString.
2414 get_runtime_airpcap_version(GString *str)
2416 guint vmaj, vmin, vrev, build;
2418 /* See if the DLL has been loaded successfully. Bail if it hasn't */
2419 if (AirpcapLoaded == FALSE) {
2420 g_string_append(str, "without AirPcap");
2424 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2425 g_string_sprintfa(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,