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 <epan/crypt/wep-wpadefs.h>
46 #include <epan/crypt/airpdcap_ws.h>
47 #include <epan/strutil.h>
48 #include "capture_ui_utils.h"
50 #include "simple_dialog.h"
53 #include "airpcap_loader.h"
56 * We load dynamically the dag library in order link it only when
57 * it's present on the system
59 static HMODULE AirpcapLib = NULL;
62 * Set to TRUE if the DLL was successfully loaded AND all functions
65 static gboolean AirpcapLoaded = FALSE;
67 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
68 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
69 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
70 static AirpcapOpenHandler g_PAirpcapOpen;
71 static AirpcapCloseHandler g_PAirpcapClose;
72 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
73 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
74 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
75 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
76 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
77 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
78 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
79 static AirpcapReadHandler g_PAirpcapRead;
80 static AirpcapGetStatsHandler g_PAirpcapGetStats;
81 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
82 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
83 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
84 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
85 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
86 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
87 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
88 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
89 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
90 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
91 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
92 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
93 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
94 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
95 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
96 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
97 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
98 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
100 /* Airpcap interface list */
101 GList *airpcap_if_list = NULL;
103 /* Airpcap current selected interface */
104 airpcap_if_info_t *airpcap_if_selected = NULL;
106 /* Airpcap current active interface */
107 airpcap_if_info_t *airpcap_if_active = NULL;
109 /* WLAN preferences pointer */
110 module_t *wlan_prefs = NULL;
113 * Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
116 get_wep_key(pref_t *pref, gpointer ud _U_)
118 gchar *my_string = NULL;
119 keys_cb_data_t* user_data;
121 decryption_key_t* new_key;
123 /* Retrieve user data info */
124 user_data = (keys_cb_data_t*)ud;
126 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
128 my_string = g_strdup(*pref->varp.string);
130 /* Here we have the string describing the key... */
131 new_key = parse_key_string(my_string);
135 /* Key is added only if not null ... */
136 user_data->list = g_list_append(user_data->list,new_key);
137 user_data->number_of_keys++;
138 user_data->current_index++;
144 /* Returs TRUE if the WEP key is valid, false otherwise */
146 wep_key_is_valid(char* key)
148 GString *new_key_string;
154 new_key_string = g_string_new(key);
156 if( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
158 g_string_free(new_key_string,FALSE);
161 if((new_key_string->len % 2) != 0)
163 g_string_free(new_key_string,FALSE);
166 for(i = 0; i < new_key_string->len; i++)
168 if(!g_ascii_isxdigit(new_key_string->str[i]))
170 g_string_free(new_key_string,FALSE);
175 g_string_free(new_key_string,FALSE);
179 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
181 set_wep_key(pref_t *pref, gpointer ud _U_)
183 gchar *my_string = NULL;
184 keys_cb_data_t* user_data;
185 gint wep_key_number = 0;
187 decryption_key_t* new_key;
189 /* Retrieve user data info */
190 user_data = (keys_cb_data_t*)ud;
192 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
194 /* Ok, the pref we're gonna set is a wep_key ... but what number? */
195 sscanf(pref->name,"wep_key%d",&wep_key_number);
197 if(user_data->current_index < user_data->number_of_keys)
199 if(wep_key_number == (user_data->current_index+1))
201 /* Retrieve the nth decryption_key_t structure pointer */
202 new_key = (decryption_key_t*)g_list_nth_data(user_data->list,user_data->current_index);
204 /* Free the old key string */
205 g_free((void *)*pref->varp.string);
207 /* Create the new string describing the decryption key */
208 my_string = get_key_string(new_key);
210 /* Duplicate the string, and assign it to the variable pointer */
211 *pref->varp.string = (void *)g_strdup(my_string);
213 /* Free the previously allocated string */
217 else /* If the number of keys has been reduced somehow, we need to delete all the other keys
218 * (remember that the new ones have been probably overwritten)
221 g_free((void *)*pref->varp.string);
222 *pref->varp.string = (void *)g_strdup(""); /* Do not just free memory!!! Put an 'empty' string! */
224 user_data->current_index++;
231 * Function used to read the Decryption Keys from the preferences and store them
232 * properly into the airpcap adapter.
235 load_wlan_driver_wep_keys()
237 keys_cb_data_t* user_data;
241 /* Retrieve the wlan preferences */
242 wlan_prefs = prefs_find_module("wlan");
244 /* Allocate a structure used to keep infos between the callbacks */
245 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
247 /* Fill the structure */
248 user_data->list = NULL;
249 user_data->current_index = 0;
250 user_data->number_of_keys= 0; /* Still unknown */
252 /* Run the callback on each 802.11 preference */
253 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
255 /* Now the key list should be filled */
258 * Signal that we've changed things, and run the 802.11 dissector's
261 wlan_prefs->prefs_changed = TRUE;
263 prefs_apply(wlan_prefs);
265 write_wlan_driver_wep_keys_to_registry(user_data->list);
268 /* free the WEP key string */
269 for(i=0;i<g_list_length(user_data->list);i++)
271 g_free(g_list_nth(user_data->list,i)->data);
274 /* free the (empty) list */
275 g_list_free(user_data->list);
277 /* free the user_data structure */
280 /* airpcap_if_info_free(fake_info_if); */
286 * This function will tell the airpcap driver the key list to use
287 * This will be stored into the registry...
290 write_wlan_wep_keys_to_registry(airpcap_if_info_t* info_if, GList* key_list)
295 PAirpcapKeysCollection KeysCollection;
296 ULONG KeysCollectionSize;
298 UINT keys_in_list = 0;
299 decryption_key_t* key_item = NULL;
301 keys_in_list = g_list_length(key_list);
304 * Save the encryption keys, if we have any of them
306 KeysCollectionSize = 0;
309 * Calculate the size of the keys collection
311 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
314 * Allocate the collection
316 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
323 * Populate the key collection
325 KeysCollection->nKeys = keys_in_list;
327 for(i = 0; i < keys_in_list; i++)
329 KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
331 /* Retrieve the Item corresponding to the i-th key */
332 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
333 new_key = g_string_new(key_item->key->str);
335 KeysCollection->Keys[i].KeyLen = new_key->len / 2;
336 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
338 for(j = 0 ; j < new_key->len; j += 2)
340 s[0] = new_key->str[j];
341 s[1] = new_key->str[j+1];
343 KeyByte = (UCHAR)strtol(s, NULL, 16);
344 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
347 g_string_free(new_key,TRUE);
351 * Free the old adapter key collection!
353 if(info_if->keysCollection != NULL)
354 g_free(info_if->keysCollection);
357 * Set this collection ad the new one
359 info_if->keysCollection = KeysCollection;
360 info_if->keysCollectionSize = KeysCollectionSize;
363 * Configuration must be saved
365 info_if->saved = FALSE;
368 * Write down the changes to the registry
370 airpcap_save_selected_if_configuration(info_if);
376 * This function will tell the airpcap driver the key list to use
377 * This will be stored into the registry...
380 write_wlan_driver_wep_keys_to_registry(GList* key_list)
385 PAirpcapKeysCollection KeysCollection;
386 ULONG KeysCollectionSize;
388 UINT keys_in_list = 0;
389 decryption_key_t* key_item = NULL;
390 airpcap_if_info_t* fake_info_if = NULL;
392 /* Create the fake_info_if from the first adapter of the list */
393 fake_info_if = airpcap_driver_fake_if_info_new();
395 if(fake_info_if == NULL)
399 * XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
400 * but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
402 n = g_list_length(key_list);
403 for(k = 0; k < n; k++ )
404 if(((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
408 * Save the encryption keys, if we have any of them
410 KeysCollectionSize = 0;
413 * Calculate the size of the keys collection
415 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
418 * Allocate the collection
420 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
427 * Populate the key collection
429 KeysCollection->nKeys = keys_in_list;
432 * XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
433 * last is WEP, we have to scroll the whole list (n) but increment the array counter only
434 * when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
437 y = 0; /* Current position in the key list */
439 for(i = 0; i < n; i++)
441 /* Retrieve the Item corresponding to the i-th key */
442 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
445 * XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supportd right now!
446 * We will have to modify the AirpcapKey structure in order to
447 * support the other two types! What happens now, is that simply the
448 * not supported keys will just be discarded (they will be saved in wireshark though)
450 if(key_item->type == AIRPDCAP_KEY_TYPE_WEP)
452 KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
454 new_key = g_string_new(key_item->key->str);
456 KeysCollection->Keys[y].KeyLen = new_key->len / 2;
457 memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
459 for(j = 0 ; j < new_key->len; j += 2)
461 s[0] = new_key->str[j];
462 s[1] = new_key->str[j+1];
464 KeyByte = (UCHAR)strtol(s, NULL, 16);
465 KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
467 /* XXX - Change when WPA will be supported!!! */
469 g_string_free(new_key,TRUE);
471 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
473 /* XXX - The driver cannot deal with this kind of key yet... */
475 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
477 /* XXX - The driver cannot deal with this kind of key yet... */
482 * Free the old adapter key collection!
484 if(fake_info_if->keysCollection != NULL)
485 g_free(fake_info_if->keysCollection);
488 * Set this collection ad the new one
490 fake_info_if->keysCollection = KeysCollection;
491 fake_info_if->keysCollectionSize = KeysCollectionSize;
494 * Configuration must be saved
496 fake_info_if->saved = FALSE;
499 * Write down the changes to the registry
501 airpcap_save_driver_if_configuration(fake_info_if);
503 airpcap_if_info_free(fake_info_if);
509 * Function used to save to the preference file the Decryption Keys.
512 save_wlan_driver_wep_keys()
514 GList* key_list = NULL;
515 char* tmp_key = NULL;
516 guint keys_in_list,i;
517 keys_cb_data_t* user_data;
518 airpcap_if_info_t* fake_info_if = NULL;
520 /* Create the fake_info_if from the first adapter of the list */
521 fake_info_if = airpcap_driver_fake_if_info_new();
523 if(fake_info_if == NULL)
526 /* Retrieve the wlan preferences */
527 wlan_prefs = prefs_find_module("wlan");
529 /* Allocate a structure used to keep infos between the callbacks */
530 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
532 /* Number of keys in key list */
533 /* Number of keys in key list */
534 if(fake_info_if->keysCollectionSize != 0)
535 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
539 for(i=0; i<keys_in_list; i++)
541 /* Only if it is a WEP key... */
542 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
544 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
545 key_list = g_list_append(key_list,g_strdup(tmp_key));
550 /* Now we know the exact number of WEP keys in the list, so store it ... */
551 keys_in_list = g_list_length(key_list);
553 /* Fill the structure */
554 user_data->list = key_list;
555 user_data->current_index = 0;
556 user_data->number_of_keys= keys_in_list;
558 /* Retrieve the wlan preferences */
559 wlan_prefs = prefs_find_module("wlan");
561 /* Run the callback on each 802.11 preference */
562 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
564 /* Signal that we've changed things, and run the 802.11 dissector's
566 wlan_prefs->prefs_changed = TRUE;
568 /* Apply changes for the specified preference */
569 prefs_apply(wlan_prefs);
572 /* free the WEP key string */
573 for(i=0;i<g_list_length(user_data->list);i++)
575 g_free(g_list_nth(user_data->list,i)->data);
578 /* free the (empty) list */
579 g_list_free(user_data->list);
581 /* free the user_data structure */
584 airpcap_if_info_free(fake_info_if);
590 * Function used to save to the preference file the Decryption Keys.
593 save_wlan_wireshark_wep_keys(GList* key_ls)
595 GList* key_list = NULL;
596 char* tmp_key = NULL;
597 guint keys_in_list,i;
598 keys_cb_data_t* user_data;
599 airpcap_if_info_t* fake_info_if = NULL;
600 decryption_key_t* tmp_dk;
602 /* Retrieve the wlan preferences */
603 wlan_prefs = prefs_find_module("wlan");
605 /* Allocate a structure used to keep infos between the callbacks */
606 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
608 keys_in_list = g_list_length(key_ls);
612 /* Fill the structure */
613 user_data->list = key_list;
614 user_data->current_index = 0;
615 user_data->number_of_keys= keys_in_list;
617 /* Retrieve the wlan preferences */
618 wlan_prefs = prefs_find_module("wlan");
620 /* Run the callback on each 802.11 preference */
621 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
623 /* Signal that we've changed things, and run the 802.11 dissector's
625 wlan_prefs->prefs_changed = TRUE;
627 /* Apply changes for the specified preference */
628 prefs_apply(wlan_prefs);
631 /* free the WEP key string */
632 for(i=0;i<g_list_length(user_data->list);i++)
634 tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
635 g_string_free(tmp_dk->key,TRUE);
636 if(tmp_dk->ssid != NULL) g_byte_array_free(tmp_dk->ssid,TRUE);
639 /* free the (empty) list */
640 g_list_free(user_data->list);
642 /* free the user_data structure */
649 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
650 * "get_airpcap_interface_list()".
653 cant_get_airpcap_if_list_error_message(const char *err_str)
655 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
659 * Airpcap wrapper, used to store the current settings for the selected adapter
662 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
664 if (!AirpcapLoaded) return FALSE;
665 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
669 * Airpcap wrapper, used to open an airpcap adapter
672 airpcap_if_open(PCHAR name, PCHAR err)
674 if (!AirpcapLoaded) return NULL;
675 if (name == NULL) return NULL;
676 return g_PAirpcapOpen(name,err);
680 * Airpcap wrapper, used to close an airpcap adapter
683 airpcap_if_close(PAirpcapHandle handle)
685 if (!AirpcapLoaded) return;
686 g_PAirpcapClose(handle);
690 * Airpcap wrapper, used to turn on the led of an airpcap adapter
693 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
695 if (!AirpcapLoaded) return FALSE;
696 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
700 * Airpcap wrapper, used to turn off the led of an airpcap adapter
703 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
705 if (!AirpcapLoaded) return FALSE;
706 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
710 * Airpcap wrapper, used to get the channel of an airpcap adapter
713 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
715 if (!AirpcapLoaded) return FALSE;
716 return g_PAirpcapGetDeviceChannel(ah,ch);
720 * Airpcap wrapper, used to set the channel of an airpcap adapter
723 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
725 if (!AirpcapLoaded) return FALSE;
726 return g_PAirpcapSetDeviceChannel(ah,ch);
730 * Airpcap wrapper, used to get the link type of an airpcap adapter
733 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
735 if (!AirpcapLoaded) return FALSE;
736 return g_PAirpcapGetLinkType(ah,lt);
740 * Airpcap wrapper, used to set the link type of an airpcap adapter
743 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
745 if (!AirpcapLoaded) return FALSE;
746 return g_PAirpcapSetLinkType(ah,lt);
750 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
753 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
755 if (!AirpcapLoaded) return FALSE;
756 return g_PAirpcapGetFcsPresence(ah,fcs);
760 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
763 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
765 if (!AirpcapLoaded) return FALSE;
766 return g_PAirpcapSetFcsPresence(ah,fcs);
770 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
773 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
775 if (!AirpcapLoaded) return FALSE;
776 return g_PAirpcapGetDecryptionState(ah,PEnable);
780 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
783 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
785 if (!AirpcapLoaded) return FALSE;
786 return g_PAirpcapSetDecryptionState(ah,Enable);
790 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
793 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
795 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
796 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
800 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
803 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
805 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
806 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
810 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
813 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
815 if (!AirpcapLoaded) return FALSE;
816 return g_PAirpcapGetFcsValidation(ah,val);
820 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
823 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
825 if (!AirpcapLoaded) return FALSE;
826 return g_PAirpcapSetFcsValidation(ah,val);
830 * Airpcap wrapper, used to save the settings for the selected_if
833 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
835 if (!AirpcapLoaded) return FALSE;
836 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
840 * Airpcap wrapper, used to save the settings for the selected_if
843 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
845 if (!AirpcapLoaded) return FALSE;
846 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
850 * Airpcap wrapper, used to save the driver's set of keys
853 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
855 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
856 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
860 * Airpcap wrapper, used to load the driver's set of keys
863 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
865 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
866 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
870 * This function will create a new airpcap_if_info_t using a name and a description
873 airpcap_if_info_new(char *name, char *description)
876 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
878 airpcap_if_info_t *if_info = NULL;
880 /* Probably I have to switch on the leds!!! */
881 ad = airpcap_if_open(name, ebuf);
884 if_info = g_malloc(sizeof (airpcap_if_info_t));
885 if_info->name = g_strdup(name);
886 if (description == NULL)
887 if_info->description = NULL;
889 if_info->description = g_strdup(description);
890 if_info->ip_addr = NULL;
891 if_info->loopback = FALSE;
892 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
893 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
894 airpcap_if_get_link_type(ad,&(if_info->linkType));
895 airpcap_if_get_device_channel(ad,&(if_info->channel));
896 airpcap_if_turn_led_on(ad, 0);
897 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
899 if_info->blinking = FALSE;
900 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
902 /* get the keys, if everything is ok, close the adapter */
903 if(airpcap_if_load_keys(ad,if_info))
905 airpcap_if_close(ad);
912 * This function will create a new fake drivers' interface, to load global keys...
915 airpcap_driver_fake_if_info_new()
918 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
920 airpcap_if_info_t *if_info = NULL;
921 airpcap_if_info_t *fake_if_info = NULL;
923 /* Maybe for some reason no airpcap adapter is found */
924 if(airpcap_if_list == NULL)
928 * Retrieve the first AirPcap adapter available. If no interface is found,
929 * it is not possible to retrieve the driver's settings, so return NULL.
931 if_info = g_list_nth_data(airpcap_if_list,0);
935 /* Open the 'fake' adapter */
936 ad = airpcap_if_open(if_info->name, ebuf);
939 fake_if_info = g_malloc(sizeof (airpcap_if_info_t));
940 fake_if_info->name = g_strdup(if_info->name);
941 fake_if_info->description = g_strdup(if_info->description);
942 fake_if_info->loopback = FALSE;
943 fake_if_info->ip_addr = NULL;
944 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
945 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
946 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
947 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
948 airpcap_if_get_device_channel(ad,&(fake_if_info->channel));
949 airpcap_if_turn_led_on(ad, 0);
950 fake_if_info->led = TRUE;
951 fake_if_info->blinking = FALSE;
952 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
954 /* get the keys, if everything is ok, close the adapter */
955 if(airpcap_if_load_driver_keys(ad,fake_if_info))
957 airpcap_if_close(ad);
965 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
968 airpcap_if_info_print(airpcap_if_info_t* if_info)
972 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
976 g_print("\n----------------- AirPcap Interface \n");
977 g_print(" NAME: %s\n",if_info->name);
978 g_print(" DESCRIPTION: %s\n",if_info->description);
979 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
980 g_print(" CHANNEL: %2u\n",if_info->channel);
981 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
982 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
983 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
984 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
985 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
986 g_print("KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
987 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
988 g_print(" LINKTYPE: %d\n",if_info->linkType);
989 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
990 g_print(" (GTK) TAG: %d\n",if_info->tag);
995 * Function used to load the WEP keys for a selected interface
998 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1000 if(!if_info) return FALSE;
1002 if_info->keysCollectionSize = 0;
1003 if_info->keysCollection = NULL;
1005 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1007 if(if_info->keysCollectionSize == 0)
1009 if_info->keysCollection = NULL;
1010 airpcap_if_close(ad);
1014 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1015 if(!if_info->keysCollection)
1017 if_info->keysCollectionSize = 0;
1018 if_info->keysCollection = NULL;
1019 airpcap_if_close(ad);
1023 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1027 airpcap_if_close(ad);
1032 * Function used to load the WEP keys for a selected interface
1035 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1037 if_info->keysCollectionSize = 0;
1038 if_info->keysCollection = NULL;
1040 if(!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1042 if(if_info->keysCollectionSize == 0)
1044 if_info->keysCollection = NULL;
1045 airpcap_if_close(ad);
1049 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1050 if(!if_info->keysCollection)
1052 if_info->keysCollectionSize = 0;
1053 if_info->keysCollection = NULL;
1054 airpcap_if_close(ad);
1058 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1062 airpcap_if_close(ad);
1067 * Function used to save the WEP keys for a selected interface
1070 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1072 if(!if_info || !AirpcapLoaded) return;
1074 if(if_info->keysCollection != NULL)
1075 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1079 * Function used to save the WEP keys for a selected interface
1082 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1084 if(if_info->keysCollection != NULL)
1085 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1089 * Callback used to free an instance of airpcap_if_info_t
1092 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1094 airpcap_if_info_t *if_info = data;
1096 if (if_info->name != NULL)
1097 g_free(if_info->name);
1099 if (if_info->description != NULL)
1100 g_free(if_info->description);
1102 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1103 if(if_info->keysCollection != NULL)
1105 g_free(if_info->keysCollection);
1106 if_info->keysCollection = NULL;
1109 if(if_info->ip_addr != NULL)
1110 g_slist_free(if_info->ip_addr);
1117 * Function used to free the airpcap interface list
1120 free_airpcap_interface_list(GList *if_list)
1122 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1123 g_list_free(if_list);
1128 * This function will use the airpcap.dll to find all the airpcap devices.
1129 * Will return null if no device is found.
1132 get_airpcap_interface_list(int *err, char **err_str)
1135 airpcap_if_info_t *if_info;
1137 AirpcapDeviceDescription *devsList, *adListEntry;
1138 char errbuf[PCAP_ERRBUF_SIZE];
1143 if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
1145 /* No interfaces, return il = NULL; */
1146 *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
1147 if (err_str != NULL)
1148 *err_str = cant_get_airpcap_if_list_error_message(errbuf);
1153 * Count the adapters
1155 adListEntry = devsList;
1160 adListEntry = adListEntry->next;
1165 /* No interfaces, return il= NULL */
1166 g_PAirpcapFreeDeviceList(devsList);
1167 *err = NO_AIRPCAP_INTERFACES_FOUND;
1168 if (err_str != NULL)
1174 * Insert the adapters in our list
1176 adListEntry = devsList;
1177 for(i = 0; i < n_adapts; i++)
1179 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1180 il = g_list_append(il, if_info);
1182 adListEntry = adListEntry->next;
1185 g_PAirpcapFreeDeviceList(devsList);
1192 * Used to retrieve the name of the interface given the description
1193 * (the name is used in AirpcapOpen, the description is put in the combo box)
1195 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
1199 airpcap_if_info_t* if_info;
1204 while( ifn < g_list_length(if_list) )
1206 curr = g_list_nth(if_list, ifn);
1210 if_info = curr->data;
1213 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1215 return if_info->name;
1225 * Used to retrieve the interface given the name
1226 * (the name is used in AirpcapOpen)
1228 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
1232 airpcap_if_info_t* if_info;
1237 while( ifn < g_list_length(if_list) )
1239 curr = g_list_nth(if_list, ifn);
1243 if_info = curr->data;
1246 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
1258 * Returns the ASCII string of a key given the key bytes
1261 airpcap_get_key_string(AirpcapKey key)
1270 if(key.KeyType == AIRPDCAP_KEY_TYPE_WEP)
1274 /* Allocate the string used to store the ASCII representation of the WEP key */
1275 dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1276 /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1279 for(j = 0; j < key.KeyLen; j++)
1281 src = g_strdup_printf("%.2x\0", key.KeyData[j]);
1283 * XXX - use g_strconcat() or GStrings instead ???
1285 l = g_strlcat(dst,src,WEP_KEY_MAX_CHAR_SIZE+1);
1290 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1292 /* XXX - Add code here */
1294 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1296 /* XXX - Add code here */
1300 /* XXX - Add code here */
1307 * Clear keys and decryption status for the specified interface
1310 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1314 if(info_if->keysCollection != NULL)
1316 g_free(info_if->keysCollection);
1317 info_if->keysCollection = NULL;
1320 info_if->keysCollectionSize = 0;
1322 info_if->DecryptionOn = FALSE;
1323 info_if->saved = FALSE;
1328 * Used to retrieve the airpcap_if_info_t of the selected interface given the
1329 * description (that is the entry of the combo box).
1331 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
1335 airpcap_if_info_t* if_info;
1340 while( ifn < g_list_length(if_list) )
1342 curr = g_list_nth(if_list, ifn);
1346 if_info = curr->data;
1349 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1361 * Used to retrieve the two chars string from interface
1364 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1370 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1372 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1373 * Otherwise, check if it is the "Any" adapter...
1377 if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1378 number = g_strdup_printf("??");
1380 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1384 number = g_strdup_printf("%.2u\0",n);
1391 * Used to retrieve the two chars string from interface
1394 airpcap_get_if_string_number_from_description(gchar* description)
1399 number = (gchar*)g_malloc(sizeof(gchar)*3);
1401 pointer = g_strrstr(description,"#\0");
1403 number[0] = *(pointer+1);
1404 number[1] = *(pointer+2);
1411 * Returns the default airpcap interface of a list, NULL if list is empty
1414 airpcap_get_default_if(GList* airpcap_if_list)
1417 GList* popdown_if_list = NULL;
1421 airpcap_if_info_t* if_info = NULL;
1423 if(prefs.capture_device != NULL)
1425 s = g_strdup(get_if_name(prefs.capture_device));
1426 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
1433 * Load the configuration for the specified interface
1436 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1438 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1443 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1447 /* Stop blinking (if it was blinkig!)*/
1448 if(if_info->blinking)
1450 /* Turn on the light (if it was off) */
1451 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1454 /* Apply settings... */
1455 airpcap_if_get_device_channel(ad,&(if_info->channel));
1456 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1457 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1458 airpcap_if_get_link_type(ad,&(if_info->linkType));
1459 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1460 /* get the keys, if everything is ok, close the adapter */
1461 if(airpcap_if_load_keys(ad,if_info))
1462 airpcap_if_close(ad);
1464 if_info->saved = TRUE;
1468 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1474 * Save the configuration for the specified interface
1477 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1479 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1484 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1488 /* Stop blinking (if it was blinkig!)*/
1489 if(if_info->blinking)
1491 /* Turn on the light (if it was off) */
1492 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1495 /* Apply settings... */
1496 airpcap_if_set_device_channel(ad,if_info->channel);
1497 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1498 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1499 airpcap_if_set_link_type(ad,if_info->linkType);
1500 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1501 airpcap_if_save_keys(ad,if_info);
1503 /* ... and save them */
1504 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
1506 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.");
1507 if_info->saved = FALSE;
1508 airpcap_if_close(ad);
1512 if_info->saved = TRUE;
1513 airpcap_if_close(ad);
1517 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1523 * Save the configuration for the specified interface
1526 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1528 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1531 if(fake_if_info != NULL)
1533 ad = airpcap_if_open(fake_if_info->name, ebuf);
1537 /* Apply decryption settings... */
1538 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1539 airpcap_if_save_driver_keys(ad,fake_if_info);
1540 airpcap_if_close(ad);
1544 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1552 * DECRYPTION KEYS FUNCTIONS
1555 * This function is used for DEBUG POURPOSES ONLY!!!
1558 print_key_list(GList* key_list)
1561 decryption_key_t* tmp;
1563 if(key_list == NULL)
1565 g_print("\n\n******* KEY LIST NULL *******\n\n");
1569 n = g_list_length(key_list);
1571 g_print("\n\n********* KEY LIST **********\n\n");
1573 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1575 for(i =0; i < n; i++)
1577 g_print("[%d] :\n",i+1);
1578 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1579 g_print("KEY : %s\n",tmp->key->str);
1581 g_print("BITS: %d\n",tmp->bits);
1583 if(tmp->type == AIRPDCAP_KEY_TYPE_WEP)
1584 g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1585 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
1586 g_print("TYPE: %s\n",AIRPCAP_WPA_PWD_KEY_STRING);
1587 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
1588 g_print("TYPE: %s\n",AIRPCAP_WPA_BIN_KEY_STRING);
1590 g_print("TYPE: %s\n","???");
1592 g_print("SSID: %s\n",(tmp->ssid != NULL) ?
1593 format_text((guchar *)tmp->ssid->data, tmp->ssid->len) : "---");
1597 g_print("\n*****************************\n\n");
1601 * Retrieves a GList of decryption_key_t structures containing infos about the
1602 * keys for the given adapter... returns NULL if no keys are found.
1605 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1608 char* tmp_key = NULL;
1609 guint i,keys_in_list = 0;
1612 decryption_key_t *new_key = NULL;
1613 GList *key_list = NULL;
1615 /* Number of keys in key list */
1616 if(info_if->keysCollectionSize != 0)
1617 keys_in_list = (guint)(info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1621 for(i=0; i<keys_in_list; i++)
1623 /* Different things to do depending on the key type */
1624 if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1626 /* allocate memory for the new key item */
1627 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1629 /* fill the fields */
1631 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1632 new_key->key = g_string_new(tmp_key);
1636 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1638 /* SSID not used in WEP keys */
1639 new_key->ssid = NULL;
1641 /* TYPE (WEP in this case) */
1642 new_key->type = info_if->keysCollection->Keys[i].KeyType;
1644 /* Append the new element in the list */
1645 key_list = g_list_append(key_list,(gpointer)new_key);
1647 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1649 /* XXX - Not supported yet */
1651 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1653 /* XXX - Not supported yet */
1661 * Retrieves a GList of decryption_key_t structures containing infos about the
1662 * keys for the global AirPcap driver... returns NULL if no keys are found.
1665 get_airpcap_driver_keys()
1668 char* tmp_key = NULL;
1669 guint i,keys_in_list = 0;
1672 decryption_key_t *new_key = NULL;
1673 GList *key_list = NULL;
1676 * To read the drivers general settings we need to create and use one airpcap adapter...
1677 * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1679 airpcap_if_info_t* fake_info_if = NULL;
1681 /* Create the fake_info_if from the first adapter of the list */
1682 fake_info_if = airpcap_driver_fake_if_info_new();
1684 if(fake_info_if == NULL)
1687 /* Number of keys in key list */
1688 if(fake_info_if->keysCollectionSize != 0)
1689 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1693 for(i=0; i<keys_in_list; i++)
1695 /* Different things to do depending on the key type */
1696 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1698 /* allocate memory for the new key item */
1699 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1701 /* fill the fields */
1703 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1704 new_key->key = g_string_new(tmp_key);
1705 if(tmp_key != NULL) g_free(tmp_key);
1708 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1710 /* SSID not used in WEP keys */
1711 new_key->ssid = NULL;
1713 /* TYPE (WEP in this case) */
1714 new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1716 /* Append the new element in the list */
1717 key_list = g_list_append(key_list,(gpointer)new_key);
1719 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1721 /* XXX - Not supported yet */
1723 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1725 /* XXX - Not supported yet */
1729 airpcap_if_info_free(fake_info_if);
1735 * Returns the list of the decryption keys specified for wireshark, NULL if
1739 get_wireshark_keys()
1741 keys_cb_data_t* wep_user_data = NULL;
1745 GList* final_list = NULL;
1746 GList* wep_final_list = NULL;
1748 /* Retrieve the wlan preferences */
1749 wlan_prefs = prefs_find_module("wlan");
1751 /* Allocate a structure used to keep infos between the callbacks */
1752 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1754 /* Fill the structure */
1755 wep_user_data->list = NULL;
1756 wep_user_data->current_index = 0;
1757 wep_user_data->number_of_keys= 0; /* Still unknown */
1759 /* Run the callback on each 802.11 preference */
1760 /* XXX - Right now, only WEP keys will be loaded */
1761 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1763 /* Copy the list field in the user data structure pointer into the final_list */
1764 if(wep_user_data != NULL) wep_final_list = wep_user_data->list;
1766 /* XXX - Merge the three lists!!!!! */
1767 final_list = wep_final_list;
1769 /* free the wep_user_data structure */
1770 g_free(wep_user_data);
1776 * Merges two lists of keys and return a newly created GList. If a key is
1777 * found multiple times, it will just appear once!
1778 * list1 and list 2 pointer will have to be freed manually if needed!!!
1779 * If the total number of keys exceeeds the maximum number allowed,
1780 * exceeding keys will be discarded...
1783 merge_key_list(GList* list1, GList* list2)
1787 decryption_key_t *dk1=NULL,
1791 GList* merged_list = NULL;
1793 if( (list1 == NULL) && (list2 == NULL) )
1799 n2 = g_list_length(list2);
1803 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1804 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1806 new_dk->bits = dk2->bits;
1807 new_dk->type = dk2->type;
1808 new_dk->key = g_string_new(dk2->key->str);
1809 new_dk->ssid = byte_array_dup(dk2->ssid);
1811 /* Check the total length of the merged list */
1812 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1813 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1816 else if(list2 == NULL)
1818 n1 = g_list_length(list1);
1823 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1824 dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1826 new_dk->bits = dk1->bits;
1827 new_dk->type = dk1->type;
1828 new_dk->key = g_string_new(dk1->key->str);
1829 new_dk->ssid = byte_array_dup(dk1->ssid);
1831 /* Check the total length of the merged list */
1832 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1833 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1838 n1 = g_list_length(list1);
1839 n2 = g_list_length(list2);
1841 /* Copy the whole list1 into merged_list */
1844 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1845 dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
1847 new_dk->bits = dk1->bits;
1848 new_dk->type = dk1->type;
1849 new_dk->key = g_string_new(dk1->key->str);
1850 new_dk->ssid = byte_array_dup(dk1->ssid);
1852 /* Check the total length of the merged list */
1853 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1854 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1857 /* Look for keys that are present in list2 but aren't in list1 yet...
1858 * Add them to merged_list
1862 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1864 if(!key_is_in_list(dk2,merged_list))
1866 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1868 new_dk->bits = dk2->bits;
1869 new_dk->type = dk2->type;
1870 new_dk->key = g_string_new(dk2->key->str);
1871 new_dk->ssid = byte_array_dup(dk2->ssid);
1873 /* Check the total length of the merged list */
1874 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1875 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1884 * Use this function to free a key list.
1887 free_key_list(GList *list)
1890 decryption_key_t *curr_key;
1895 n = g_list_length(list);
1897 for(i = 0; i < n; i++)
1899 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1901 /* Free all the strings */
1902 if(curr_key->key != NULL)
1903 g_string_free(curr_key->key, TRUE);
1905 if(curr_key->ssid != NULL)
1906 g_byte_array_free(curr_key->ssid, TRUE);
1908 /* free the decryption_key_t structure*/
1921 * If the given key is contained in the list, returns TRUE.
1922 * Returns FALSE otherwise.
1925 key_is_in_list(decryption_key_t *dk,GList *list)
1928 decryption_key_t* curr_key = NULL;
1929 gboolean found = FALSE;
1931 if( (list == NULL) || (dk == NULL) )
1934 n = g_list_length(list);
1939 for(i = 0; i < n; i++)
1941 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1942 if(keys_are_equals(dk,curr_key))
1950 * Returns TRUE if keys are equals, FALSE otherwise
1953 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
1956 if((k1==NULL) || (k2==NULL))
1959 /* XXX - Remove this check when we will have the WPA/WPA2 decryption in the Driver! */
1960 /** //if( (k1->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k1->type == AIRPDCAP_KEY_TYPE_WPA_PMK) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PMK) ) **/
1961 /** // return TRUE; **/
1963 if( g_string_equal(k1->key,k2->key) &&
1964 (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
1965 k1->type == k2->type)
1967 /* Check the ssid... if the key type is WEP, the two fields should be NULL */
1968 if((k1->ssid == NULL) && (k2->ssid == NULL))
1971 /* If they are not null, they must share the same ssid */
1972 return byte_array_equal(k1->ssid,k2->ssid);
1975 /* Some field is not equal ... */
1980 * Tests if two collection of keys are equal or not, to be considered equals, they have to
1981 * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
1985 key_lists_are_equal(GList* list1, GList* list2)
1989 guint wep_n1=0,wep_n2=0;
1990 GList *wep_list1=NULL;
1991 GList *wep_list2=NULL;
1994 decryption_key_t *dk1=NULL,*dk2=NULL;
1996 n1 = g_list_length(list1);
1997 n2 = g_list_length(list2);
2000 * XXX - START : Retrieve the aublists of WEP keys!!! This is needed only 'till Driver WPA decryption
2001 * is not implemented.
2005 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2006 if(dk1->type == AIRPDCAP_KEY_TYPE_WEP)
2008 wep_list1 = g_list_append(wep_list1,(gpointer)dk1);
2014 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2015 if(dk2->type == AIRPDCAP_KEY_TYPE_WEP)
2017 wep_list2 = g_list_append(wep_list2,(gpointer)dk2);
2023 * XXX - END : Remove from START to END when the WPA/WPA2 decryption will be implemented in
2028 * Commented, because in the new AirPcap version all the keys will be saved
2029 * into the driver, and all the keys for every specific adapter will be
2030 * removed. This means that this check will always fail... and the user will
2031 * always be asked what to do... and it doesn't make much sense.
2033 /* if(n1 != n2) return FALSE; */
2034 if(wep_n1 != wep_n2) return FALSE;
2041 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2042 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2044 if(!g_string_equal(dk1->key,dk2->key)) return FALSE;
2048 dk2=(decryption_key_t*)g_list_nth_data(wep_list2,i);
2049 if(!key_is_in_list(dk2,wep_list1)) return FALSE;
2056 test_if_on(pref_t *pref, gpointer ud _U_)
2061 /* Retrieve user data info */
2062 is_on = (gboolean*)ud;
2065 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2067 number = *pref->varp.boolp;
2069 if(number) *is_on = TRUE;
2070 else *is_on = FALSE;
2078 * Returns TRUE if the Wireshark decryption is active, false otherwise
2081 wireshark_decryption_on()
2085 /* Retrieve the wlan preferences */
2086 wlan_prefs = prefs_find_module("wlan");
2088 /* Run the callback on each 802.11 preference */
2089 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2095 * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2098 airpcap_decryption_on()
2100 gboolean is_on = FALSE;
2102 airpcap_if_info_t* fake_if_info = NULL;
2104 fake_if_info = airpcap_driver_fake_if_info_new();
2106 if(fake_if_info != NULL)
2108 if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2110 else if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2114 airpcap_if_info_free(fake_if_info);
2120 * Free an instance of airpcap_if_info_t
2123 airpcap_if_info_free(airpcap_if_info_t *if_info)
2127 if (if_info->name != NULL)
2128 g_free(if_info->name);
2130 if (if_info->description != NULL)
2131 g_free(if_info->description);
2133 if(if_info->keysCollection != NULL)
2135 g_free(if_info->keysCollection);
2136 if_info->keysCollection = NULL;
2139 if(if_info->ip_addr != NULL)
2141 g_slist_free(if_info->ip_addr);
2142 if_info->ip_addr = NULL;
2154 set_on_off(pref_t *pref, gpointer ud _U_)
2159 /* Retrieve user data info */
2160 is_on = (gboolean*)ud;
2162 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2164 number = *pref->varp.boolp;
2166 g_free((void *)*pref->varp.boolp);
2168 *pref->varp.boolp = TRUE;
2170 *pref->varp.boolp = FALSE;
2178 * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2181 set_wireshark_decryption(gboolean on_off)
2187 /* Retrieve the wlan preferences */
2188 wlan_prefs = prefs_find_module("wlan");
2190 /* Run the callback on each 802.11 preference */
2191 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2194 * Signal that we've changed things, and run the 802.11 dissector's
2197 wlan_prefs->prefs_changed = TRUE;
2199 prefs_apply(wlan_prefs);
2203 * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2206 set_airpcap_decryption(gboolean on_off)
2208 /* We need to directly access the .dll functions here... */
2209 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2210 PAirpcapHandle ad,ad_driver;
2212 gboolean success = TRUE;
2216 airpcap_if_info_t* curr_if = NULL;
2217 airpcap_if_info_t* fake_if_info = NULL;
2219 fake_if_info = airpcap_driver_fake_if_info_new();
2221 if(fake_if_info == NULL)
2222 /* We apparently don't have any adapters installed.
2223 * This isn't a failure, so return TRUE
2227 /* Set the driver decryption */
2228 ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2232 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2234 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2236 airpcap_if_close(ad_driver);
2239 airpcap_if_info_free(fake_if_info);
2241 n = g_list_length(airpcap_if_list);
2243 /* Set to FALSE the decryption for all the adapters */
2244 /* Apply this change to all the adapters !!! */
2245 for(i = 0; i < n; i++)
2247 curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2249 if( curr_if != NULL )
2251 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list,curr_if->description), ebuf);
2254 curr_if->DecryptionOn = (gboolean)AIRPCAP_DECRYPTION_OFF;
2255 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2256 /* Save configuration for the curr_if */
2257 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
2261 airpcap_if_close(ad);
2270 /* DYNAMIC LIBRARY LOADER */
2272 * Used to dynamically load the airpcap library in order link it only when
2273 * it's present on the system
2275 int load_airpcap(void)
2277 BOOL base_functions = TRUE;
2278 BOOL new_functions = TRUE;
2280 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
2282 /* Report the error but go on */
2283 return AIRPCAP_DLL_NOT_FOUND;
2287 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2288 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2289 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2290 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2291 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2292 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2293 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2294 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2295 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2296 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2297 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2298 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2299 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2300 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2301 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2302 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2303 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2304 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2305 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2306 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2307 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2308 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2309 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2310 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2311 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2312 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2313 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2314 if((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2316 /* TEST IF WE CAN FIND AIRPCAP NEW DRIVER FEATURES */
2317 if((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) new_functions = FALSE;
2318 if((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) new_functions = FALSE;
2319 if((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) new_functions = FALSE;
2320 if((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) new_functions = FALSE;
2326 AirpcapLoaded = TRUE;
2327 return AIRPCAP_DLL_OK;
2331 AirpcapLoaded = TRUE;
2332 return AIRPCAP_DLL_OLD;
2337 AirpcapLoaded = FALSE;
2338 return AIRPCAP_DLL_ERROR;
2344 * Append the version of AirPcap with which we were compiled to a GString.
2347 get_compiled_airpcap_version(GString *str)
2349 g_string_append(str, "with AirPcap");
2353 * Append the version of AirPcap with which we we're running to a GString.
2356 get_runtime_airpcap_version(GString *str)
2358 guint vmaj, vmin, vrev, build;
2360 /* See if the DLL has been loaded successfully. Bail if it hasn't */
2361 if (AirpcapLoaded == FALSE) {
2362 g_string_append(str, "without AirPcap");
2366 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2367 g_string_sprintfa(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,