Witness: add pidl output
[wireshark-wip.git] / airpcap_loader.c
blob14e527dd87ca8fd551cb8cf07a976fbc31ee89c5
1 /* airpcap_loader.c
3 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #ifdef HAVE_AIRPCAP
31 #ifdef HAVE_LIBPCAP
32 #include <glib.h>
33 #include <gmodule.h>
36 #include <wtap.h>
37 #include <pcap.h>
38 #endif
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42 #include <epan/prefs-int.h>
43 #include <epan/uat-int.h>
44 #include <epan/dissectors/packet-ieee80211.h>
45 #include <epan/crypt/wep-wpadefs.h>
46 #include <epan/crypt/airpdcap_ws.h>
47 #include <epan/strutil.h>
48 #include <epan/frequency-utils.h>
49 #include "capture_ui_utils.h"
50 #include <wsutil/file_util.h>
52 #include "ui/simple_dialog.h"
54 #include <airpcap.h>
55 #include "airpcap_loader.h"
59 * Set to TRUE if the DLL was successfully loaded AND all functions
60 * are present.
62 static gboolean AirpcapLoaded = FALSE;
64 #ifdef _WIN32
66 * We load dynamically the dag library in order link it only when
67 * it's present on the system
69 static void * AirpcapLib = NULL;
71 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
72 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
73 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
74 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
75 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
76 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
77 static AirpcapReadHandler g_PAirpcapRead;
78 static AirpcapGetStatsHandler g_PAirpcapGetStats;
79 #endif
81 static int AirpcapVersion = 3;
83 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
84 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
85 static AirpcapOpenHandler g_PAirpcapOpen;
86 static AirpcapCloseHandler g_PAirpcapClose;
87 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
88 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
89 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
90 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
91 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
92 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
93 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
94 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
95 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
96 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
97 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
98 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
99 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
100 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
101 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
102 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
103 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
104 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
105 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
106 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
107 static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
108 static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
109 static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
111 /* Airpcap interface list */
112 GList *airpcap_if_list = NULL;
114 /* Airpcap current selected interface */
115 airpcap_if_info_t *airpcap_if_selected = NULL;
117 /* Airpcap current active interface */
118 airpcap_if_info_t *airpcap_if_active = NULL;
120 /* WLAN preferences pointer */
121 module_t *wlan_prefs = NULL;
123 Dot11Channel *pSupportedChannels;
124 guint numSupportedChannels;
126 static AirpcapChannelInfo LegacyChannels[] =
128 {2412, 0, {0,0,0}},
129 {2417, 0, {0,0,0}},
130 {2422, 0, {0,0,0}},
131 {2427, 0, {0,0,0}},
132 {2432, 0, {0,0,0}},
133 {2437, 0, {0,0,0}},
134 {2442, 0, {0,0,0}},
135 {2447, 0, {0,0,0}},
136 {2452, 0, {0,0,0}},
137 {2457, 0, {0,0,0}},
138 {2462, 0, {0,0,0}},
139 {2467, 0, {0,0,0}},
140 {2472, 0, {0,0,0}},
141 {2484, 0, {0,0,0}},
144 static guint num_legacy_channels = 14;
147 * Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
149 static guint
150 get_wep_key(pref_t *pref, gpointer ud)
152 gchar *key_string = NULL;
153 guint8 key_type = AIRPDCAP_KEY_TYPE_WEP;
154 keys_cb_data_t* user_data;
155 uat_t *uat;
156 guint i;
157 const char* err = NULL;
158 uat_wep_key_record_t* wep_keys;
159 decryption_key_t* new_key;
161 /* Retrieve user data info */
162 user_data = (keys_cb_data_t*)ud;
164 if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
166 uat = pref->varp.uat;
167 /* This is just a sanity check. UAT should be loaded */
168 if (!uat->loaded)
170 uat_load(uat, &err);
171 if (err != NULL)
172 return 1;
175 for (i = 0, wep_keys = (uat_wep_key_record_t*)*uat->user_ptr; i < *uat->nrows_p; i++, wep_keys++)
177 /* strip out key type if present */
178 if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WEP ":", 4) == 0) {
179 key_type = AIRPDCAP_KEY_TYPE_WEP;
180 key_string = (gchar*)wep_keys->string+4;
182 else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PWD ":", 8) == 0) {
183 key_string = (gchar*)wep_keys->string+8;
184 key_type = AIRPDCAP_KEY_TYPE_WPA_PWD;
186 else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PSK ":", 8) == 0) {
187 key_string = (gchar*)wep_keys->string+8;
188 key_type = AIRPDCAP_KEY_TYPE_WPA_PSK;
190 else {
191 key_type = wep_keys->key;
192 key_string = (gchar*)wep_keys->string;
195 /* Here we have the string describing the key... */
196 new_key = parse_key_string(key_string, key_type);
198 if (new_key != NULL)
200 /* Key is added only if not null ... */
201 user_data->list = g_list_append(user_data->list,new_key);
202 user_data->number_of_keys++;
203 user_data->current_index++;
207 return 0;
210 /* Returs TRUE if the WEP key is valid, false otherwise */
211 gboolean
212 wep_key_is_valid(char* key)
214 GString *new_key_string;
215 guint i=0;
217 if (key == NULL)
218 return FALSE;
220 new_key_string = g_string_new(key);
222 if ( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
224 g_string_free(new_key_string,FALSE);
225 return FALSE;
227 if ((new_key_string->len % 2) != 0)
229 g_string_free(new_key_string,FALSE);
230 return FALSE;
232 for(i = 0; i < new_key_string->len; i++)
234 if (!g_ascii_isxdigit(new_key_string->str[i]))
236 g_string_free(new_key_string,FALSE);
237 return FALSE;
241 g_string_free(new_key_string,FALSE);
242 return TRUE;
245 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
246 static guint
247 set_wep_key(pref_t *pref, gpointer ud _U_)
249 keys_cb_data_t* user_data;
250 uat_t *uat;
251 gint i;
252 const char* err = NULL;
253 uat_wep_key_record_t uat_key;
255 decryption_key_t* new_key;
257 /* Retrieve user data info */
258 user_data = (keys_cb_data_t*)ud;
260 if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
262 uat = pref->varp.uat;
263 if (!uat->loaded)
265 /* UAT will only be loaded if previous keys exist, so it may need
266 to be loaded now */
267 uat_load(uat, &err);
268 if (err != NULL)
269 return 1;
271 uat->loaded = 1;
273 /* Free the old records */
274 uat_clear(uat);
276 for (i = 0; i < user_data->number_of_keys; i++)
278 new_key = (decryption_key_t*)g_list_nth_data(user_data->list,i);
280 uat_key.string = get_key_string(new_key);
281 uat_key.key = new_key->type;
282 uat_add_record(uat, &uat_key, TRUE);
285 uat_save(uat, &err);
286 if (err != NULL)
287 return 1;
290 return 0;
294 * Function used to read the Decryption Keys from the preferences and store them
295 * properly into the airpcap adapter.
297 gboolean
298 load_wlan_driver_wep_keys(void)
300 keys_cb_data_t* user_data;
301 guint i;
303 /* Retrieve the wlan preferences */
304 wlan_prefs = prefs_find_module("wlan");
306 /* Allocate a structure used to keep infos between the callbacks */
307 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
309 /* Fill the structure */
310 user_data->list = NULL;
311 user_data->current_index = 0;
312 user_data->number_of_keys= 0; /* Still unknown */
314 /* Run the callback on each 802.11 preference */
315 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
317 /* Now the key list should be filled */
320 * Signal that we've changed things, and run the 802.11 dissector's
321 * callback
323 wlan_prefs->prefs_changed = TRUE;
325 prefs_apply(wlan_prefs);
327 write_wlan_driver_wep_keys_to_registry(user_data->list);
329 /* FREE MEMORY */
330 /* free the WEP key string */
331 for(i=0;i<g_list_length(user_data->list);i++)
333 g_free(g_list_nth(user_data->list,i)->data);
336 /* free the (empty) list */
337 g_list_free(user_data->list);
339 /* free the user_data structure */
340 g_free(user_data);
342 /* airpcap_if_info_free(fake_info_if); */
344 return TRUE;
348 * This function will tell the airpcap driver the key list to use
349 * This will be stored into the registry...
351 gboolean
352 write_wlan_wep_keys_to_registry(airpcap_if_info_t* info_if, GList* key_list)
354 guint i,j;
355 GString *new_key;
356 gchar s[3];
357 PAirpcapKeysCollection KeysCollection;
358 guint KeysCollectionSize;
359 guint8 KeyByte;
360 guint keys_in_list = 0;
361 decryption_key_t* key_item = NULL;
363 keys_in_list = g_list_length(key_list);
366 * Calculate the size of the keys collection
368 KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
371 * Allocate the collection
373 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
374 if (!KeysCollection)
376 return FALSE;
380 * Populate the key collection
382 KeysCollection->nKeys = keys_in_list;
384 for(i = 0; i < keys_in_list; i++)
386 KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
388 /* Retrieve the Item corresponding to the i-th key */
389 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
390 new_key = g_string_new(key_item->key->str);
392 KeysCollection->Keys[i].KeyLen = (guint) new_key->len / 2;
393 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
395 for(j = 0 ; j < new_key->len; j += 2)
397 s[0] = new_key->str[j];
398 s[1] = new_key->str[j+1];
399 s[2] = '\0';
400 KeyByte = (guint8)strtol(s, NULL, 16);
401 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
404 g_string_free(new_key,TRUE);
408 * Free the old adapter key collection!
410 if (info_if->keysCollection != NULL)
411 g_free(info_if->keysCollection);
414 * Set this collection ad the new one
416 info_if->keysCollection = KeysCollection;
417 info_if->keysCollectionSize = KeysCollectionSize;
420 * Configuration must be saved
422 info_if->saved = FALSE;
425 * Write down the changes to the registry
427 airpcap_save_selected_if_configuration(info_if);
429 return TRUE;
433 * This function will tell the airpcap driver the key list to use
434 * This will be stored into the registry...
436 gboolean
437 write_wlan_driver_wep_keys_to_registry(GList* key_list)
439 guint i,j,k,n,y;
440 GString *new_key;
441 gchar s[3];
442 PAirpcapKeysCollection KeysCollection;
443 guint KeysCollectionSize;
444 guint8 KeyByte;
445 guint keys_in_list = 0;
446 decryption_key_t* key_item = NULL;
447 airpcap_if_info_t* fake_info_if = NULL;
449 /* Create the fake_info_if from the first adapter of the list */
450 fake_info_if = airpcap_driver_fake_if_info_new();
452 if (fake_info_if == NULL)
453 return FALSE;
456 * XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
457 * but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
459 n = g_list_length(key_list);
460 for(k = 0; k < n; k++ )
461 if (((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
462 keys_in_list++;
465 * Calculate the size of the keys collection
467 KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
470 * Allocate the collection
472 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
473 if (!KeysCollection)
475 return FALSE;
479 * Populate the key collection
481 KeysCollection->nKeys = keys_in_list;
484 * XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
485 * last is WEP, we have to scroll the whole list (n) but increment the array counter only
486 * when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
487 * this
489 y = 0; /* Current position in the key list */
491 for(i = 0; i < n; i++)
493 /* Retrieve the Item corresponding to the i-th key */
494 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
497 * XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supported right now!
498 * We will have to modify the AirpcapKey structure in order to
499 * support the other two types! What happens now, is that simply the
500 * not supported keys will just be discarded (they will be saved in Wireshark though)
502 if (key_item->type == AIRPDCAP_KEY_TYPE_WEP)
504 KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
506 new_key = g_string_new(key_item->key->str);
508 KeysCollection->Keys[y].KeyLen = (guint) new_key->len / 2;
509 memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
511 for(j = 0 ; j < new_key->len; j += 2)
513 s[0] = new_key->str[j];
514 s[1] = new_key->str[j+1];
515 s[2] = '\0';
516 KeyByte = (guint8)strtol(s, NULL, 16);
517 KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
519 /* XXX - Change when WPA will be supported!!! */
520 y++;
521 g_string_free(new_key,TRUE);
523 else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
525 /* XXX - The driver cannot deal with this kind of key yet... */
527 else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
529 /* XXX - The driver cannot deal with this kind of key yet... */
534 * Free the old adapter key collection!
536 if (fake_info_if->keysCollection != NULL)
537 g_free(fake_info_if->keysCollection);
540 * Set this collection ad the new one
542 fake_info_if->keysCollection = KeysCollection;
543 fake_info_if->keysCollectionSize = KeysCollectionSize;
546 * Configuration must be saved
548 fake_info_if->saved = FALSE;
551 * Write down the changes to the registry
553 airpcap_save_driver_if_configuration(fake_info_if);
555 airpcap_if_info_free(fake_info_if);
557 return TRUE;
561 * Function used to save to the preference file the Decryption Keys.
564 save_wlan_driver_wep_keys(void)
566 GList* key_list = NULL;
567 char* tmp_key = NULL;
568 guint keys_in_list,i;
569 keys_cb_data_t* user_data;
570 airpcap_if_info_t* fake_info_if = NULL;
572 /* Create the fake_info_if from the first adapter of the list */
573 fake_info_if = airpcap_driver_fake_if_info_new();
575 if (fake_info_if == NULL)
576 return 0;
578 /* Retrieve the wlan preferences */
579 wlan_prefs = prefs_find_module("wlan");
581 /* Allocate a structure used to keep infos between the callbacks */
582 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
584 /* Number of keys in key list */
585 if (fake_info_if->keysCollectionSize != 0)
586 keys_in_list = AirpcapKeysCollectionSizeToKeyCount(fake_info_if->keysCollectionSize);
587 else
588 keys_in_list = 0;
590 for(i=0; i<keys_in_list; i++)
592 /* Only if it is a WEP key... */
593 if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
595 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
596 key_list = g_list_append(key_list,g_strdup(tmp_key));
597 g_free(tmp_key);
601 /* Now we know the exact number of WEP keys in the list, so store it ... */
602 keys_in_list = g_list_length(key_list);
604 /* Fill the structure */
605 user_data->list = key_list;
606 user_data->current_index = 0;
607 user_data->number_of_keys= keys_in_list;
609 /* Retrieve the wlan preferences */
610 wlan_prefs = prefs_find_module("wlan");
612 /* Run the callback on each 802.11 preference */
613 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
615 /* Signal that we've changed things, and run the 802.11 dissector's
616 * callback */
617 wlan_prefs->prefs_changed = TRUE;
619 /* Apply changes for the specified preference */
620 prefs_apply(wlan_prefs);
622 /* FREE MEMORY */
623 /* free the WEP key string */
624 for(i=0;i<g_list_length(user_data->list);i++)
626 g_free(g_list_nth(user_data->list,i)->data);
629 /* free the (empty) list */
630 g_list_free(user_data->list);
632 /* free the user_data structure */
633 g_free(user_data);
635 airpcap_if_info_free(fake_info_if);
637 return keys_in_list;
641 * Function used to save to the preference file the Decryption Keys.
644 save_wlan_wireshark_wep_keys(GList* key_ls)
646 GList* key_list = NULL;
647 guint keys_in_list,i;
648 keys_cb_data_t* user_data;
649 decryption_key_t* tmp_dk;
651 /* Retrieve the wlan preferences */
652 wlan_prefs = prefs_find_module("wlan");
654 /* Allocate a structure used to keep infos between the callbacks */
655 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
657 keys_in_list = g_list_length(key_ls);
659 key_list = key_ls;
661 /* Fill the structure */
662 user_data->list = key_list;
663 user_data->current_index = 0;
664 user_data->number_of_keys= keys_in_list;
666 /* Retrieve the wlan preferences */
667 wlan_prefs = prefs_find_module("wlan");
669 /* Run the callback on each 802.11 preference */
670 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
672 /* Signal that we've changed things, and run the 802.11 dissector's
673 * callback */
674 wlan_prefs->prefs_changed = TRUE;
676 /* Apply changes for the specified preference */
677 prefs_apply(wlan_prefs);
679 /* FREE MEMORY */
680 /* free the WEP key string */
681 for(i=0;i<g_list_length(user_data->list);i++)
683 tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
684 g_string_free(tmp_dk->key,TRUE);
685 if (tmp_dk->ssid != NULL) g_byte_array_free(tmp_dk->ssid,TRUE);
688 /* free the (empty) list */
689 g_list_free(user_data->list);
691 /* free the user_data structure */
692 g_free(user_data);
694 return keys_in_list;
698 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
699 * "get_airpcap_interface_list()".
701 static gchar *
702 cant_get_airpcap_if_list_error_message(const char *err_str)
704 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
708 * Airpcap wrapper, used to store the current settings for the selected adapter
710 gboolean
711 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
713 if (!AirpcapLoaded) return FALSE;
714 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
718 * Airpcap wrapper, used to open an airpcap adapter
720 PAirpcapHandle
721 airpcap_if_open(gchar * name, gchar * err)
723 if (!AirpcapLoaded) return NULL;
724 if (name == NULL) return NULL;
725 return g_PAirpcapOpen(name,err);
729 * Airpcap wrapper, used to close an airpcap adapter
731 void
732 airpcap_if_close(PAirpcapHandle handle)
734 if (!AirpcapLoaded) return;
735 g_PAirpcapClose(handle);
739 * Retrieve the state of the Airpcap DLL
742 airpcap_get_dll_state(void)
744 return AirpcapVersion;
748 * Airpcap wrapper, used to turn on the led of an airpcap adapter
750 gboolean
751 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
753 if (!AirpcapLoaded) return FALSE;
754 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
758 * Airpcap wrapper, used to turn off the led of an airpcap adapter
760 gboolean
761 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
763 if (!AirpcapLoaded) return FALSE;
764 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
768 * Airpcap wrapper, used to get the channel of an airpcap adapter
770 gboolean
771 airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
773 if (!AirpcapLoaded) return FALSE;
774 return g_PAirpcapGetDeviceChannel(ah,ch);
778 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
780 gboolean
781 airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
783 if (!AirpcapLoaded) return FALSE;
784 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
785 *nInfo = num_legacy_channels;
786 *cInfo = (AirpcapChannelInfo*)&LegacyChannels;
788 return TRUE;
789 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
790 return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
792 return FALSE;
796 * Airpcap wrapper, used to get the supported channels of an airpcap adapter
798 Dot11Channel*
799 airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
801 AirpcapChannelInfo *chanInfo;
802 guint i=0, j=0, numInfo = 0;
804 if (!AirpcapLoaded)
805 return NULL;
806 if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
807 return NULL;
808 numSupportedChannels = 0;
811 * allocate a bigger array
813 if (numInfo == 0)
814 return NULL;
816 pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
818 for (i = 0; i < numInfo; i++)
820 guint supportedChannel = G_MAXUINT;
823 * search if we have it already
825 for (j = 0; j < numSupportedChannels; j++)
827 if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
829 supportedChannel = j;
830 break;
834 if (supportedChannel == G_MAXUINT)
837 * not found, create a new item
839 pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
841 switch(chanInfo[i].ExtChannel)
843 case -1:
844 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
845 break;
846 case +1:
847 pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
848 break;
849 case 0:
850 default:
851 pSupportedChannels[numSupportedChannels].Flags = 0;
855 * Gather channel information
858 pSupportedChannels[numSupportedChannels].Flags |=
859 FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
860 FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
861 pSupportedChannels[numSupportedChannels].Channel =
862 ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
863 numSupportedChannels++;
865 else
868 * just update the ext channel flags
870 switch(chanInfo[i].ExtChannel)
872 case -1:
873 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
874 break;
875 case +1:
876 pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
877 break;
878 case 0:
879 default:
880 break;
885 if (numSupportedChannels < 1)
886 return NULL;
888 * Now sort the list by frequency
890 for (i = 0 ; i < numSupportedChannels - 1; i++)
892 for (j = i + 1; j < numSupportedChannels; j++)
894 if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
896 Dot11Channel temp = pSupportedChannels[i];
897 pSupportedChannels[i] = pSupportedChannels[j];
898 pSupportedChannels[j] = temp;
903 *pNumSupportedChannels = numSupportedChannels;
904 return pSupportedChannels;
908 * Airpcap wrapper, used to set the channel of an airpcap adapter
910 gboolean
911 airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
913 if (!AirpcapLoaded) return FALSE;
914 return g_PAirpcapSetDeviceChannel(ah,ch);
918 * Airpcap wrapper, used to set the frequency of an airpcap adapter
920 gboolean
921 airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
923 if (!AirpcapLoaded) return FALSE;
924 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
925 gint channel = 0;
926 channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
928 if (channel < 0){
929 return FALSE;
930 } else {
931 return airpcap_if_set_device_channel(ah, channel);
933 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
934 return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
937 return FALSE;
941 * Airpcap wrapper, used to get the frequency of an airpcap adapter
943 gboolean
944 airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
946 if (!AirpcapLoaded) return FALSE;
948 pChannelInfo->Frequency = 0;
949 pChannelInfo->ExtChannel = 0;
950 pChannelInfo->Reserved[0] = 0;
951 pChannelInfo->Reserved[1] = 0;
952 pChannelInfo->Reserved[2] = 0;
954 if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
955 guint channel = 0;
956 guint chan_freq = 0;
958 if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
960 chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
961 if (chan_freq == 0) return FALSE;
962 pChannelInfo->Frequency = chan_freq;
964 return TRUE;
965 } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
966 return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
968 return FALSE;
972 * Airpcap wrapper, used to get the link type of an airpcap adapter
974 gboolean
975 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
977 if (!AirpcapLoaded) return FALSE;
978 return g_PAirpcapGetLinkType(ah,lt);
982 * Airpcap wrapper, used to set the link type of an airpcap adapter
984 gboolean
985 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
987 if (!AirpcapLoaded) return FALSE;
988 return g_PAirpcapSetLinkType(ah,lt);
992 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
994 gboolean
995 airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
997 if (!AirpcapLoaded) return FALSE;
998 return g_PAirpcapGetFcsPresence(ah,fcs);
1002 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
1004 gboolean
1005 airpcap_if_set_fcs_presence(PAirpcapHandle ah, gboolean fcs)
1007 if (!AirpcapLoaded) return FALSE;
1008 return g_PAirpcapSetFcsPresence(ah,fcs);
1012 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
1014 gboolean
1015 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
1017 if (!AirpcapLoaded) return FALSE;
1018 return g_PAirpcapGetDecryptionState(ah,PEnable);
1022 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
1024 gboolean
1025 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
1027 if (!AirpcapLoaded) return FALSE;
1028 return g_PAirpcapSetDecryptionState(ah,Enable);
1032 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
1034 gboolean
1035 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
1037 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
1038 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
1042 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
1044 gboolean
1045 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
1047 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
1048 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
1052 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
1054 gboolean
1055 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
1057 if (!AirpcapLoaded) return FALSE;
1058 return g_PAirpcapGetFcsValidation(ah,val);
1062 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
1064 gboolean
1065 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
1067 if (!AirpcapLoaded) return FALSE;
1068 return g_PAirpcapSetFcsValidation(ah,val);
1072 * Airpcap wrapper, used to save the settings for the selected_if
1074 gboolean
1075 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
1077 if (!AirpcapLoaded) return FALSE;
1078 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
1082 * Airpcap wrapper, used to save the settings for the selected_if
1084 gboolean
1085 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
1087 if (!AirpcapLoaded) return FALSE;
1088 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
1092 * Airpcap wrapper, used to save the driver's set of keys
1094 gboolean
1095 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
1097 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
1098 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
1102 * Airpcap wrapper, used to load the driver's set of keys
1104 gboolean
1105 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
1107 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
1108 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
1112 * This function will create a new airpcap_if_info_t using a name and a description
1114 airpcap_if_info_t *
1115 airpcap_if_info_new(char *name, char *description)
1117 PAirpcapHandle ad;
1118 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1120 airpcap_if_info_t *if_info = NULL;
1122 /* Probably I have to switch on the leds!!! */
1123 ad = airpcap_if_open(name, ebuf);
1124 if (ad)
1126 if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
1127 if_info->name = g_strdup(name);
1128 if (description == NULL){
1129 if_info->description = NULL;
1130 }else{
1131 if_info->description = g_strdup(description);
1134 if_info->ip_addr = NULL;
1135 if_info->loopback = FALSE;
1136 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1137 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1138 airpcap_if_get_link_type(ad,&(if_info->linkType));
1139 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
1140 if_info->pSupportedChannels = airpcap_if_get_device_supported_channels_array(ad, &(if_info->numSupportedChannels));
1141 airpcap_if_turn_led_on(ad, 0);
1142 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1143 if_info->led = TRUE;
1144 if_info->blinking = FALSE;
1145 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1147 /* get the keys, if everything is ok, close the adapter */
1148 if (airpcap_if_load_keys(ad,if_info))
1150 airpcap_if_close(ad);
1153 return if_info;
1157 * This function will create a new fake drivers' interface, to load global keys...
1159 airpcap_if_info_t*
1160 airpcap_driver_fake_if_info_new(void)
1162 PAirpcapHandle ad;
1163 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1165 airpcap_if_info_t *if_info = NULL;
1166 airpcap_if_info_t *fake_if_info = NULL;
1168 /* Maybe for some reason no airpcap adapter is found */
1169 if (airpcap_if_list == NULL)
1170 return NULL;
1173 * Retrieve the first AirPcap adapter available. If no interface is found,
1174 * it is not possible to retrieve the driver's settings, so return NULL.
1176 if_info = (airpcap_if_info_t *)g_list_nth_data(airpcap_if_list,0);
1177 if (if_info == NULL)
1178 return NULL;
1180 /* Open the 'fake' adapter */
1181 ad = airpcap_if_open(if_info->name, ebuf);
1182 if (ad)
1184 fake_if_info = (airpcap_if_info_t *)g_malloc(sizeof (airpcap_if_info_t));
1185 fake_if_info->name = g_strdup(if_info->name);
1186 fake_if_info->description = g_strdup(if_info->description);
1187 fake_if_info->loopback = FALSE;
1188 fake_if_info->ip_addr = NULL;
1189 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
1190 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
1191 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
1192 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
1193 airpcap_if_get_device_channel_ex(ad,&(fake_if_info->channelInfo));
1194 airpcap_if_turn_led_on(ad, 0);
1195 fake_if_info->led = TRUE;
1196 fake_if_info->blinking = FALSE;
1197 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1199 /* get the keys, if everything is ok, close the adapter */
1200 if (airpcap_if_load_driver_keys(ad,fake_if_info))
1202 airpcap_if_close(ad);
1206 return fake_if_info;
1210 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
1212 void
1213 airpcap_if_info_print(airpcap_if_info_t* if_info)
1215 guint i;
1216 if (if_info == NULL)
1218 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
1219 return;
1222 g_print("\n----------------- AirPcap Interface \n");
1223 g_print(" NAME: %s\n",if_info->name);
1224 g_print(" DESCRIPTION: %s\n",if_info->description);
1225 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
1226 g_print(" channelInfo.Frequency: %u\n",if_info->channelInfo.Frequency);
1227 g_print(" channelInfo.ExtChannel: %d\n",if_info->channelInfo.ExtChannel);
1228 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
1229 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
1230 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
1231 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
1232 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
1233 g_print(" KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
1234 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
1235 g_print(" LINKTYPE: %d\n",if_info->linkType);
1236 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
1237 g_print(" (GTK) TAG: %d\n",if_info->tag);
1238 g_print("SUPPORTED CHANNELS POINTER: %p\n",if_info->pSupportedChannels);
1239 g_print(" NUM SUPPORTED CHANNELS: %u\n",if_info->numSupportedChannels);
1241 for(i=0; i<(if_info->numSupportedChannels); i++){
1242 g_print("\n SUPPORTED CHANNEL #%u\n",i+1);
1243 g_print(" CHANNEL: %u\n",if_info->pSupportedChannels[i].Channel);
1244 g_print(" FREQUENCY: %u\n",if_info->pSupportedChannels[i].Frequency);
1245 g_print(" FLAGS: %u\n",if_info->pSupportedChannels[i].Flags);
1247 g_print("\n\n");
1251 * Function used to load the WEP keys for a selected interface
1253 gboolean
1254 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1256 if (!if_info) return FALSE;
1258 if_info->keysCollectionSize = 0;
1259 if_info->keysCollection = NULL;
1261 if (!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1263 if (if_info->keysCollectionSize == 0)
1265 if_info->keysCollection = NULL;
1266 airpcap_if_close(ad);
1267 return FALSE;
1270 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1271 if (!if_info->keysCollection)
1273 if_info->keysCollectionSize = 0;
1274 if_info->keysCollection = NULL;
1275 airpcap_if_close(ad);
1276 return FALSE;
1279 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1280 return TRUE;
1283 airpcap_if_close(ad);
1284 return FALSE;
1288 * Function used to load the WEP keys for a selected interface
1290 gboolean
1291 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1293 if_info->keysCollectionSize = 0;
1294 if_info->keysCollection = NULL;
1296 if (!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1298 if (if_info->keysCollectionSize == 0)
1300 if_info->keysCollection = NULL;
1301 airpcap_if_close(ad);
1302 return FALSE;
1305 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1306 if (!if_info->keysCollection)
1308 if_info->keysCollectionSize = 0;
1309 if_info->keysCollection = NULL;
1310 airpcap_if_close(ad);
1311 return FALSE;
1314 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1315 return TRUE;
1318 airpcap_if_close(ad);
1319 return FALSE;
1323 * Function used to save the WEP keys for a selected interface
1325 void
1326 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1328 if (!if_info || !AirpcapLoaded) return;
1330 if (if_info->keysCollection != NULL)
1331 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1335 * Function used to save the WEP keys for a selected interface
1337 void
1338 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1340 if (if_info->keysCollection != NULL)
1341 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1345 * Callback used to free an instance of airpcap_if_info_t
1347 static void
1348 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1350 airpcap_if_info_t *if_info = (airpcap_if_info_t *)data;
1352 if (NULL == if_info)
1353 return;
1355 if (if_info->name != NULL)
1356 g_free(if_info->name);
1358 if (if_info->description != NULL)
1359 g_free(if_info->description);
1361 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1362 if (if_info->keysCollection != NULL)
1364 g_free(if_info->keysCollection);
1365 if_info->keysCollection = NULL;
1368 if (if_info->ip_addr != NULL)
1369 g_slist_free(if_info->ip_addr);
1371 g_free(if_info);
1375 * Function used to free the airpcap interface list
1377 void
1378 free_airpcap_interface_list(GList *if_list)
1380 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1381 g_list_free(if_list);
1382 if_list = NULL;
1386 * This function will use the airpcap.dll to find all the airpcap devices.
1387 * Will return null if no device is found.
1389 GList*
1390 get_airpcap_interface_list(int *err, char **err_str)
1392 GList *il = NULL;
1393 airpcap_if_info_t *if_info;
1394 int n_adapts;
1395 AirpcapDeviceDescription *devsList, *adListEntry;
1396 char errbuf[AIRPCAP_ERRBUF_SIZE];
1398 *err = 0;
1400 if (!AirpcapLoaded)
1402 *err = AIRPCAP_NOT_LOADED;
1403 return il;
1406 if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
1408 /* No interfaces, return il = NULL; */
1409 *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
1410 if (err_str != NULL)
1411 *err_str = cant_get_airpcap_if_list_error_message(errbuf);
1412 return il;
1416 * Count the adapters
1418 adListEntry = devsList;
1419 n_adapts = 0;
1420 while(adListEntry)
1422 n_adapts++;
1423 adListEntry = adListEntry->next;
1426 if (n_adapts == 0)
1428 /* No interfaces, return il= NULL */
1429 g_PAirpcapFreeDeviceList(devsList);
1430 *err = NO_AIRPCAP_INTERFACES_FOUND;
1431 if (err_str != NULL)
1432 *err_str = NULL;
1433 return il;
1437 * Insert the adapters in our list
1439 adListEntry = devsList;
1440 while(adListEntry)
1442 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1443 if (if_info != NULL){
1444 il = g_list_append(il, if_info);
1447 adListEntry = adListEntry->next;
1450 g_PAirpcapFreeDeviceList(devsList);
1452 return il;
1456 * Used to retrieve the interface given the name
1457 * (the name is used in AirpcapOpen)
1459 airpcap_if_info_t* get_airpcap_if_from_name(GList* if_list, const gchar* name)
1461 GList* curr;
1462 airpcap_if_info_t* if_info;
1464 for (curr = g_list_first(if_list); curr; curr = g_list_next(curr)) {
1465 if_info = (airpcap_if_info_t *)curr->data;
1466 if (if_info && (g_ascii_strcasecmp(if_info->name, name) == 0)) {
1467 return (if_info);
1470 return (NULL);
1474 * Returns the ASCII string of a key given the key bytes
1476 gchar*
1477 airpcap_get_key_string(AirpcapKey key)
1479 unsigned int j = 0;
1480 gchar *dst,*src;
1482 dst = NULL;
1483 src = NULL;
1485 if (key.KeyType == AIRPDCAP_KEY_TYPE_WEP)
1487 if (key.KeyLen != 0)
1489 /* Allocate the string used to store the ASCII representation of the WEP key */
1490 dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1491 /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1492 dst[0]='\0';
1494 for(j = 0; j < key.KeyLen; j++)
1496 src = g_strdup_printf("%.2x", key.KeyData[j]);
1498 * XXX - use g_strconcat() or GStrings instead ???
1500 g_strlcat(dst, src, WEP_KEY_MAX_CHAR_SIZE+1);
1502 g_free(src);
1505 else if (key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1507 /* XXX - Add code here */
1509 else if (key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1511 /* XXX - Add code here */
1513 else
1515 /* XXX - Add code here */
1518 return dst;
1522 * Clear keys and decryption status for the specified interface
1524 void
1525 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1527 if (info_if != NULL)
1529 if (info_if->keysCollection != NULL)
1531 g_free(info_if->keysCollection);
1532 info_if->keysCollection = NULL;
1535 info_if->keysCollectionSize = 0;
1537 info_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
1538 info_if->saved = FALSE;
1543 * Used to retrieve the two chars string from interface
1545 gchar*
1546 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1548 gchar* number;
1549 guint n;
1550 int a;
1552 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1554 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1555 * Otherwise, check if it is the "Any" adapter...
1557 if (a == 0)
1559 if (g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1560 number = g_strdup_printf("??");
1561 else
1562 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1564 else
1566 number = g_strdup_printf("%.2u",n);
1569 return number;
1573 * Used to retrieve the two chars string from interface
1575 gchar*
1576 airpcap_get_if_string_number_from_description(gchar* description)
1578 gchar* number;
1579 gchar* pointer;
1581 number = (gchar*)g_malloc(sizeof(gchar)*3);
1583 pointer = g_strrstr(description,"#\0");
1585 number[0] = *(pointer+1);
1586 number[1] = *(pointer+2);
1587 number[2] = '\0';
1589 return number;
1593 * Returns the default airpcap interface of a list, NULL if list is empty
1595 airpcap_if_info_t*
1596 airpcap_get_default_if(GList* airpcap_if_list_p)
1598 airpcap_if_info_t* if_info = NULL;
1600 if ((prefs.capture_device != NULL) && (*prefs.capture_device != '\0'))
1602 if_info = get_airpcap_if_from_name(airpcap_if_list_p,
1603 get_if_name(prefs.capture_device));
1605 return if_info;
1609 * Load the configuration for the specified interface
1611 void
1612 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1614 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1615 PAirpcapHandle ad;
1617 if (if_info != NULL)
1619 ad = airpcap_if_open(if_info->name, ebuf);
1621 if (ad)
1623 /* Stop blinking (if it was blinking!)*/
1624 if (if_info->blinking)
1626 /* Turn on the light (if it was off) */
1627 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1630 /* Apply settings... */
1631 airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
1632 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1633 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1634 airpcap_if_get_link_type(ad,&(if_info->linkType));
1635 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1636 /* get the keys, if everything is ok, close the adapter */
1637 if (airpcap_if_load_keys(ad,if_info))
1638 airpcap_if_close(ad);
1640 if_info->saved = TRUE;
1642 else
1644 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1650 * Save the configuration for the specified interface
1652 void
1653 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1655 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1656 PAirpcapHandle ad;
1658 if (if_info != NULL)
1660 ad = airpcap_if_open(if_info->name, ebuf);
1662 if (ad)
1664 /* Stop blinking (if it was blinking!)*/
1665 if (if_info->blinking)
1667 /* Turn on the light (if it was off) */
1668 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1671 /* Apply settings... */
1672 airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
1673 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1674 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1675 airpcap_if_set_link_type(ad,if_info->linkType);
1676 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1677 airpcap_if_save_keys(ad,if_info);
1679 /* ... and save them */
1680 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
1682 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.");
1683 if_info->saved = FALSE;
1684 airpcap_if_close(ad);
1685 return;
1688 if_info->saved = TRUE;
1689 airpcap_if_close(ad);
1691 else
1693 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1699 * Save the configuration for the specified interface
1701 void
1702 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1704 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1705 PAirpcapHandle ad;
1707 if (fake_if_info != NULL)
1709 ad = airpcap_if_open(fake_if_info->name, ebuf);
1711 if (ad)
1713 /* Apply decryption settings... */
1714 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1715 airpcap_if_save_driver_keys(ad,fake_if_info);
1716 airpcap_if_close(ad);
1718 else
1720 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1724 return;
1728 * DECRYPTION KEYS FUNCTIONS
1731 * This function is used for DEBUG POURPOSES ONLY!!!
1733 void
1734 print_key_list(GList* key_list)
1736 gint n,i;
1737 decryption_key_t* tmp;
1739 if (key_list == NULL)
1741 g_print("\n\n******* KEY LIST NULL *******\n\n");
1742 return;
1745 n = g_list_length(key_list);
1747 g_print("\n\n********* KEY LIST **********\n\n");
1749 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1751 for(i =0; i < n; i++)
1753 g_print("[%d] :\n",i+1);
1754 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1755 g_print("KEY : %s\n",tmp->key->str);
1757 g_print("BITS: %d\n",tmp->bits);
1759 if (tmp->type == AIRPDCAP_KEY_TYPE_WEP)
1760 g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1761 else if (tmp->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
1762 g_print("TYPE: %s\n",AIRPCAP_WPA_PWD_KEY_STRING);
1763 else if (tmp->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
1764 g_print("TYPE: %s\n",AIRPCAP_WPA_BIN_KEY_STRING);
1765 else
1766 g_print("TYPE: %s\n","???");
1768 g_print("SSID: %s\n",(tmp->ssid != NULL) ?
1769 format_text((guchar *)tmp->ssid->data, tmp->ssid->len) : "---");
1770 g_print("\n");
1773 g_print("\n*****************************\n\n");
1777 * Retrieves a GList of decryption_key_t structures containing infos about the
1778 * keys for the given adapter... returns NULL if no keys are found.
1780 GList *
1781 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1783 /* tmp vars */
1784 char* tmp_key = NULL;
1785 guint i,keys_in_list = 0;
1787 /* real vars*/
1788 decryption_key_t *new_key = NULL;
1789 GList *key_list = NULL;
1791 /* Number of keys in key list */
1792 if (info_if->keysCollectionSize != 0)
1793 keys_in_list = AirpcapKeysCollectionSizeToKeyCount(info_if->keysCollectionSize);
1794 else
1795 keys_in_list = 0;
1797 for(i=0; i<keys_in_list; i++)
1799 /* Different things to do depending on the key type */
1800 if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1802 /* allocate memory for the new key item */
1803 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1805 /* fill the fields */
1806 /* KEY */
1807 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1808 new_key->key = g_string_new(tmp_key);
1809 g_free(tmp_key);
1811 /* BITS */
1812 new_key->bits = (guint) new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an hexadecimal number) */
1814 /* SSID not used in WEP keys */
1815 new_key->ssid = NULL;
1817 /* TYPE (WEP in this case) */
1818 new_key->type = info_if->keysCollection->Keys[i].KeyType;
1820 /* Append the new element in the list */
1821 key_list = g_list_append(key_list,(gpointer)new_key);
1823 else if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1825 /* XXX - Not supported yet */
1827 else if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1829 /* XXX - Not supported yet */
1833 return key_list;
1837 * Retrieves a GList of decryption_key_t structures containing infos about the
1838 * keys for the global AirPcap driver... returns NULL if no keys are found.
1840 GList *
1841 get_airpcap_driver_keys(void)
1843 /* tmp vars */
1844 char *tmp_key = NULL;
1845 guint i,keys_in_list = 0;
1847 /* real vars*/
1848 decryption_key_t *new_key = NULL;
1849 GList *key_list = NULL;
1852 * To read the drivers general settings we need to create and use one airpcap adapter...
1853 * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1855 airpcap_if_info_t* fake_info_if = NULL;
1857 /* Create the fake_info_if from the first adapter of the list */
1858 fake_info_if = airpcap_driver_fake_if_info_new();
1860 if (fake_info_if == NULL)
1861 return NULL;
1863 /* Number of keys in key list */
1864 if (fake_info_if->keysCollectionSize != 0)
1865 keys_in_list = AirpcapKeysCollectionSizeToKeyCount(fake_info_if->keysCollectionSize);
1866 else
1867 keys_in_list = 0;
1869 for(i=0; i<keys_in_list; i++)
1871 /* Different things to do depending on the key type */
1872 if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1874 /* allocate memory for the new key item */
1875 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1877 /* fill the fields */
1878 /* KEY */
1879 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1880 new_key->key = g_string_new(tmp_key);
1881 if (tmp_key != NULL) g_free(tmp_key);
1883 /* BITS */
1884 new_key->bits = (guint) new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an hexadecimal number) */
1886 /* SSID not used in WEP keys */
1887 new_key->ssid = NULL;
1889 /* TYPE (WEP in this case) */
1890 new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1892 /* Append the new element in the list */
1893 key_list = g_list_append(key_list,(gpointer)new_key);
1895 else if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1897 /* XXX - Not supported yet */
1899 else if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1901 /* XXX - Not supported yet */
1905 airpcap_if_info_free(fake_info_if);
1907 return key_list;
1911 * Returns the list of the decryption keys specified for wireshark, NULL if
1912 * no key is found
1914 GList *
1915 get_wireshark_keys(void)
1917 keys_cb_data_t *wep_user_data = NULL;
1919 GList *final_list = NULL;
1920 GList *wep_final_list = NULL;
1922 /* Retrieve the wlan preferences */
1923 wlan_prefs = prefs_find_module("wlan");
1925 /* Allocate a structure used to keep infos between the callbacks */
1926 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1928 /* Fill the structure */
1929 wep_user_data->list = NULL;
1930 wep_user_data->current_index = 0;
1931 wep_user_data->number_of_keys= 0; /* Still unknown */
1933 /* Run the callback on each 802.11 preference */
1934 /* XXX - Right now, only WEP keys will be loaded */
1935 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1937 /* Copy the list field in the user data structure pointer into the final_list */
1938 wep_final_list = wep_user_data->list;
1940 /* XXX - Merge the three lists!!!!! */
1941 final_list = wep_final_list;
1943 /* free the wep_user_data structure */
1944 g_free(wep_user_data);
1946 return final_list;
1950 * Merges two lists of keys and return a newly created GList. If a key is
1951 * found multiple times, it will just appear once!
1952 * list1 and list 2 pointer will have to be freed manually if needed!!!
1953 * If the total number of keys exceeeds the maximum number allowed,
1954 * exceeding keys will be discarded...
1956 GList *
1957 merge_key_list(GList* list1, GList* list2)
1959 guint n1=0,n2=0;
1960 guint i;
1961 decryption_key_t *dk1=NULL,
1962 *dk2=NULL,
1963 *new_dk=NULL;
1965 GList* merged_list = NULL;
1967 if ( (list1 == NULL) && (list2 == NULL) )
1968 return NULL;
1970 if (list1 == NULL)
1972 n2 = g_list_length(list2);
1974 for(i=0;i<n2;i++)
1976 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1977 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1979 new_dk->bits = dk2->bits;
1980 new_dk->type = dk2->type;
1981 new_dk->key = g_string_new(dk2->key->str);
1982 new_dk->ssid = byte_array_dup(dk2->ssid);
1984 /* Check the total length of the merged list */
1985 if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1986 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1989 else if (list2 == NULL)
1991 n1 = g_list_length(list1);
1993 for(i=0;i<n1;i++)
1995 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1996 dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1998 new_dk->bits = dk1->bits;
1999 new_dk->type = dk1->type;
2000 new_dk->key = g_string_new(dk1->key->str);
2001 new_dk->ssid = byte_array_dup(dk1->ssid);
2003 /* Check the total length of the merged list */
2004 if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2005 merged_list = g_list_append(merged_list,(gpointer)new_dk);
2008 else
2010 n1 = g_list_length(list1);
2011 n2 = g_list_length(list2);
2013 /* Copy the whole list1 into merged_list */
2014 for(i=0;i<n1;i++)
2016 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2017 dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
2019 new_dk->bits = dk1->bits;
2020 new_dk->type = dk1->type;
2021 new_dk->key = g_string_new(dk1->key->str);
2022 new_dk->ssid = byte_array_dup(dk1->ssid);
2024 /* Check the total length of the merged list */
2025 if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2026 merged_list = g_list_append(merged_list,(gpointer)new_dk);
2029 /* Look for keys that are present in list2 but aren't in list1 yet...
2030 * Add them to merged_list
2032 for(i=0;i<n2;i++)
2034 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
2036 if (!key_is_in_list(dk2,merged_list))
2038 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2040 new_dk->bits = dk2->bits;
2041 new_dk->type = dk2->type;
2042 new_dk->key = g_string_new(dk2->key->str);
2043 new_dk->ssid = byte_array_dup(dk2->ssid);
2045 /* Check the total length of the merged list */
2046 if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2047 merged_list = g_list_append(merged_list,(gpointer)new_dk);
2052 return merged_list;
2056 * Use this function to free a key list.
2058 void
2059 free_key_list(GList *list)
2061 guint i,n;
2062 decryption_key_t *curr_key;
2064 if (list == NULL)
2065 return;
2067 n = g_list_length(list);
2069 for(i = 0; i < n; i++)
2071 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2073 /* Free all the strings */
2074 if (curr_key->key != NULL)
2075 g_string_free(curr_key->key, TRUE);
2077 if (curr_key->ssid != NULL)
2078 g_byte_array_free(curr_key->ssid, TRUE);
2080 /* free the decryption_key_t structure*/
2081 g_free(curr_key);
2082 curr_key = NULL;
2085 /* Free the list */
2086 g_list_free(list);
2088 return;
2093 * If the given key is contained in the list, returns TRUE.
2094 * Returns FALSE otherwise.
2096 gboolean
2097 key_is_in_list(decryption_key_t *dk,GList *list)
2099 guint i,n;
2100 decryption_key_t *curr_key = NULL;
2101 gboolean found = FALSE;
2103 if ( (list == NULL) || (dk == NULL) )
2104 return FALSE;
2106 n = g_list_length(list);
2108 if (n < 1)
2109 return FALSE;
2111 for(i = 0; i < n; i++)
2113 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2114 if (keys_are_equals(dk,curr_key))
2115 found = TRUE;
2118 return found;
2122 * Returns TRUE if keys are equals, FALSE otherwise
2124 gboolean
2125 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
2128 if ((k1==NULL) || (k2==NULL))
2129 return FALSE;
2131 /* XXX - Remove this check when we will have the WPA/WPA2 decryption in the Driver! */
2132 /** 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) ) **/
2133 /** return TRUE; **/
2135 if (g_string_equal(k1->key,k2->key) &&
2136 (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
2137 (k1->type == k2->type))
2139 /* Check the ssid... if the key type is WEP, the two fields should be NULL */
2140 if ((k1->ssid == NULL) && (k2->ssid == NULL))
2141 return TRUE;
2143 /* If they are not null, they must share the same ssid */
2144 return byte_array_equal(k1->ssid,k2->ssid);
2147 /* Some field is not equal ... */
2148 return FALSE;
2152 * Tests if two collection of keys are equal or not, to be considered equals, they have to
2153 * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
2154 * return TRUE)
2156 gboolean
2157 key_lists_are_equal(GList* list1, GList* list2)
2159 guint n1 = 0,n2=0;
2160 /* XXX - Remove */
2161 guint wep_n1 = 0,wep_n2=0;
2162 GList *wep_list1 = NULL;
2163 GList *wep_list2 = NULL;
2164 /* XXX - END*/
2165 guint i/*,j*/;
2166 decryption_key_t *dk1=NULL,*dk2=NULL;
2168 n1 = g_list_length(list1);
2169 n2 = g_list_length(list2);
2172 * XXX - START : Retrieve the aublists of WEP keys!!! This is needed only 'till Driver WPA decryption
2173 * is implemented.
2175 for(i=0;i<n1;i++)
2177 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2178 if (dk1->type == AIRPDCAP_KEY_TYPE_WEP)
2180 wep_list1 = g_list_append(wep_list1,(gpointer)dk1);
2181 wep_n1++;
2184 for(i=0;i<n2;i++)
2186 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2187 if (dk2->type == AIRPDCAP_KEY_TYPE_WEP)
2189 wep_list2 = g_list_append(wep_list2,(gpointer)dk2);
2190 wep_n2++;
2195 * XXX - END : Remove from START to END when the WPA/WPA2 decryption will be implemented in
2196 * the Driver
2200 * Commented, because in the new AirPcap version all the keys will be saved
2201 * into the driver, and all the keys for every specific adapter will be
2202 * removed. This means that this check will always fail... and the user will
2203 * always be asked what to do... and it doesn't make much sense.
2205 /* if (n1 != n2) return FALSE; */
2206 if (wep_n1 != wep_n2) return FALSE;
2208 n2 = wep_n2;
2210 /*for(i=0;i<n1;i++)
2212 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2213 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2215 if (!g_string_equal(dk1->key,dk2->key)) return FALSE;
2217 for(i=0;i<n2;i++)
2219 dk2=(decryption_key_t*)g_list_nth_data(wep_list2,i);
2220 if (!key_is_in_list(dk2,wep_list1)) return FALSE;
2223 return TRUE;
2226 static guint
2227 test_if_on(pref_t *pref, gpointer ud)
2229 gboolean *is_on;
2230 gboolean number;
2232 /* Retrieve user data info */
2233 is_on = (gboolean*)ud;
2236 if (g_ascii_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2238 number = *pref->varp.boolp;
2240 if (number) *is_on = TRUE;
2241 else *is_on = FALSE;
2243 return 1;
2245 return 0;
2249 * Returns TRUE if the Wireshark decryption is active, false otherwise
2250 * XXX - Should we just add a routine to packet-ieee80211.c to grab this directly?
2252 gboolean
2253 wireshark_decryption_on(void)
2255 gboolean is_on;
2257 /* Retrieve the wlan preferences */
2258 wlan_prefs = prefs_find_module("wlan");
2260 /* Run the callback on each 802.11 preference */
2261 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2263 return is_on;
2267 * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2269 gboolean
2270 airpcap_decryption_on(void)
2272 gboolean is_on = FALSE;
2274 airpcap_if_info_t* fake_if_info = NULL;
2276 fake_if_info = airpcap_driver_fake_if_info_new();
2278 if (fake_if_info != NULL)
2280 if (fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2281 is_on = TRUE;
2282 else if (fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2283 is_on = FALSE;
2286 airpcap_if_info_free(fake_if_info);
2288 return is_on;
2292 * Free an instance of airpcap_if_info_t
2294 void
2295 airpcap_if_info_free(airpcap_if_info_t *if_info)
2297 if (if_info != NULL)
2299 if (if_info->name != NULL)
2300 g_free(if_info->name);
2302 if (if_info->description != NULL)
2303 g_free(if_info->description);
2305 if (if_info->keysCollection != NULL)
2307 g_free(if_info->keysCollection);
2308 if_info->keysCollection = NULL;
2311 if (if_info->ip_addr != NULL)
2313 g_slist_free(if_info->ip_addr);
2314 if_info->ip_addr = NULL;
2317 if (if_info != NULL)
2319 g_free(if_info);
2320 if_info = NULL;
2325 static guint
2326 set_on_off(pref_t *pref, gpointer ud)
2328 gboolean *is_on;
2330 /* Retrieve user data info */
2331 is_on = (gboolean*)ud;
2333 if (g_ascii_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2336 if (*is_on)
2337 *pref->varp.boolp = TRUE;
2338 else
2339 *pref->varp.boolp = FALSE;
2341 return 1;
2343 return 0;
2347 * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2349 void
2350 set_wireshark_decryption(gboolean on_off)
2352 gboolean is_on;
2354 is_on = on_off;
2356 /* Retrieve the wlan preferences */
2357 wlan_prefs = prefs_find_module("wlan");
2359 /* Run the callback on each 802.11 preference */
2360 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2363 * Signal that we've changed things, and run the 802.11 dissector's
2364 * callback
2366 wlan_prefs->prefs_changed = TRUE;
2368 prefs_apply(wlan_prefs);
2372 * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2374 gboolean
2375 set_airpcap_decryption(gboolean on_off)
2377 /* We need to directly access the .dll functions here... */
2378 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2379 PAirpcapHandle ad,ad_driver;
2381 gboolean success = TRUE;
2383 gint n = 0;
2384 gint i = 0;
2385 airpcap_if_info_t* curr_if = NULL;
2386 airpcap_if_info_t* fake_if_info = NULL;
2388 fake_if_info = airpcap_driver_fake_if_info_new();
2390 if (fake_if_info == NULL)
2391 /* We apparently don't have any adapters installed.
2392 * This isn't a failure, so return TRUE
2394 return TRUE;
2396 /* Set the driver decryption */
2397 ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2398 if (ad_driver)
2400 if (on_off)
2401 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2402 else
2403 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2405 airpcap_if_close(ad_driver);
2408 airpcap_if_info_free(fake_if_info);
2410 n = g_list_length(airpcap_if_list);
2412 /* Set to FALSE the decryption for all the adapters */
2413 /* Apply this change to all the adapters !!! */
2414 for(i = 0; i < n; i++)
2416 curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2418 if (curr_if != NULL)
2420 ad = airpcap_if_open(curr_if->name, ebuf);
2421 if (ad)
2423 curr_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
2424 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2425 /* Save configuration for the curr_if */
2426 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
2428 success = FALSE;
2430 airpcap_if_close(ad);
2435 return success;
2439 /* DYNAMIC LIBRARY LOADER */
2441 * Used to dynamically load the airpcap library in order link it only when
2442 * it's present on the system
2444 int load_airpcap(void)
2446 #ifdef _WIN32
2447 gboolean base_functions = TRUE;
2448 gboolean eleven_n_functions = TRUE;
2450 if ((AirpcapLib = ws_load_library("airpcap.dll")) == NULL)
2452 /* Report the error but go on */
2453 AirpcapVersion = AIRPCAP_DLL_NOT_FOUND;
2454 return AirpcapVersion;
2456 else
2458 if ((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2459 if ((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2460 if ((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2461 if ((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2462 if ((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2463 if ((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2464 if ((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2465 if ((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2466 if ((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2467 if ((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2468 if ((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2469 if ((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2470 if ((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2471 if ((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2472 if ((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2473 if ((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2474 if ((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2475 if ((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2476 if ((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2477 if ((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2478 if ((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2479 if ((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2480 if ((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2481 if ((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2482 if ((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2483 if ((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2484 if ((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2485 if ((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2486 if ((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) base_functions = FALSE;
2487 if ((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) base_functions = FALSE;
2488 if ((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) base_functions = FALSE;
2489 if ((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) base_functions = FALSE;
2491 /* TEST IF AIRPCAP SUPPORTS 11N */
2492 if ((g_PAirpcapSetDeviceChannelEx = (AirpcapSetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
2493 if ((g_PAirpcapGetDeviceChannelEx = (AirpcapGetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
2494 if ((g_PAirpcapGetDeviceSupportedChannels = (AirpcapGetDeviceSupportedChannelsHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceSupportedChannels")) == NULL) eleven_n_functions = FALSE;
2496 if (base_functions && eleven_n_functions){
2497 AirpcapLoaded = TRUE;
2498 AirpcapVersion = AIRPCAP_DLL_OK;
2499 } else if (base_functions){
2500 AirpcapLoaded = TRUE;
2501 AirpcapVersion = AIRPCAP_DLL_OLD;
2502 return AIRPCAP_DLL_OK;
2503 }else{
2504 AirpcapLoaded = FALSE;
2505 AirpcapVersion = AIRPCAP_DLL_ERROR;
2508 return AirpcapVersion;
2509 #else /* _WIN32 */
2510 return AIRPCAP_DLL_NOT_FOUND;
2511 #endif /* _WIN32 */
2515 * Append the version of AirPcap with which we were compiled to a GString.
2517 void
2518 get_compiled_airpcap_version(GString *str)
2520 g_string_append(str, "with AirPcap");
2524 * Append the version of AirPcap with which we we're running to a GString.
2526 void
2527 get_runtime_airpcap_version(GString *str)
2529 guint vmaj, vmin, vrev, build;
2531 /* See if the DLL has been loaded successfully. Bail if it hasn't */
2532 if (AirpcapLoaded == FALSE) {
2533 g_string_append(str, "without AirPcap");
2534 return;
2537 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2538 g_string_append_printf(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
2539 vrev, build);
2541 #endif /* HAVE_AIRPCAP */
2544 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2546 * Local variables:
2547 * c-basic-offset: 4
2548 * tab-width: 8
2549 * indent-tabs-mode: nil
2550 * End:
2552 * vi: set shiftwidth=4 tabstop=8 expandtab:
2553 * :indentSize=4:tabSize=8:noTabs=true: