change all file offsets from long to gint64 so we can - theoretically - handle files...
[obnox/wireshark/wip.git] / airpcap_loader.c
1 /* airpcap_loader.c
2  *
3  * $Id$
4  *
5  * Giorgio Tino <giorgio.tino@cacetech.com>
6  * Copyright (c) CACE Technologies, LLC 2006
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2000 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #ifdef _WIN32
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_LIBPCAP
34 #include <glib.h>
35 #include <gmodule.h>
36
37
38 #include <wtap.h>
39 #include <pcap.h>
40 #endif
41
42 #include <epan/packet.h>
43 #include <epan/prefs.h>
44 #include <epan/prefs-int.h>
45 #include "capture_ui_utils.h"
46
47 #include "simple_dialog.h"
48
49 #include <airpcap.h>
50 #include "airpcap_loader.h"
51
52 /*
53  * We load dinamically the dag library in order link it only when
54  * it's present on the system
55  */
56 static HMODULE AirpcapLib = NULL;
57
58 /*
59  * Set to TRUE if the DLL was successfully loaded AND all functions
60  * are present.
61  */
62 static gboolean AirpcapLoaded = FALSE;
63
64 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
65 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
66 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
67 static AirpcapOpenHandler g_PAirpcapOpen;
68 static AirpcapCloseHandler g_PAirpcapClose;
69 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
70 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
71 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
72 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
73 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
74 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
75 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
76 static AirpcapReadHandler g_PAirpcapRead;
77 static AirpcapGetStatsHandler g_PAirpcapGetStats;
78 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
79 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
80 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
81 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
82 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
83 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
84 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
85 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
86 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
87 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
88 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
89 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
90 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
91 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
92 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
93 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
94 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
95 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
96
97 /* Airpcap interface list */
98 GList *airpcap_if_list = NULL;
99
100 /* Airpcap current selected interface */
101 airpcap_if_info_t *airpcap_if_selected = NULL;
102
103 /* Airpcap current active interface */
104 airpcap_if_info_t *airpcap_if_active = NULL;
105
106 /* WLAN preferences pointer */
107 module_t *wlan_prefs = NULL;
108
109 /* Callback used by the load_wlan_keys() routine in order to read a WEP decryption key */
110 static guint
111 get_wep_key(pref_t *pref, gpointer ud _U_)
112 {
113 gchar *my_string = NULL;
114 keys_cb_data_t* user_data;
115
116 decryption_key_t* new_key;
117
118 /* Retrieve user data info */
119 user_data = (keys_cb_data_t*)ud;
120
121 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
122     {
123     my_string = g_strdup(*pref->varp.string);
124
125     if( my_string != NULL)
126         {
127         /* Key is added only if not null ... */
128         if( (g_strcasecmp(my_string,"") != 0) && (wep_key_is_valid(my_string)))
129             {
130             new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
131
132             new_key->key = g_string_new(my_string);
133             g_free(my_string);
134
135             new_key->bits = new_key->key->len * 4;
136
137             new_key->type = AIRPCAP_KEYTYPE_WEP;
138
139             new_key->ssid = NULL;
140
141             user_data->list = g_list_append(user_data->list,new_key);
142             user_data->number_of_keys++;
143             user_data->current_index++;
144             }
145         }
146     }
147 return 0;
148 }
149
150 /* Callback used by the load_wlan_keys() routine in order to read a WPA decryption key */
151 static guint
152 get_wpa_key(pref_t *pref, gpointer ud _U_)
153 {
154 return 1;
155 }
156
157 /* Callback used by the load_wlan_keys() routine in order to read a WPA2 decryption key */
158 static guint
159 get_wpa2_key(pref_t *pref, gpointer ud _U_)
160 {
161 return 1;
162 }
163
164 /* Returs TRUE if the WEP key is valid, false otherwise */
165 gboolean
166 wep_key_is_valid(char* key)
167 {
168 GString *new_key_string;
169 guint i=0;
170
171 if(key == NULL)
172         return FALSE;
173
174 new_key_string = g_string_new(key);
175
176 if( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
177         {
178         g_string_free(new_key_string,FALSE);
179         return FALSE;
180         }
181 if((new_key_string->len % 2) != 0)
182         {
183         g_string_free(new_key_string,FALSE);
184         return FALSE;
185         }
186 for(i = 0; i < new_key_string->len; i++)
187         {
188         if(!g_ascii_isxdigit(new_key_string->str[i]))
189                 {
190                 g_string_free(new_key_string,FALSE);
191                 return FALSE;
192                 }
193         }
194
195 g_string_free(new_key_string,FALSE);
196 return TRUE;
197 }
198
199 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
200 static guint
201 set_wep_key(pref_t *pref, gpointer ud _U_)
202 {
203 gchar *my_string = NULL;
204 keys_cb_data_t* user_data;
205 gint wep_key_number = 0;
206
207 /* Retrieve user data info */
208 user_data = (keys_cb_data_t*)ud;
209
210 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
211     {
212     /* Ok, the pref we're gonna set is a wep_key ... but what number? */
213     sscanf(pref->name,"wep_key%d",&wep_key_number);
214
215     if(user_data->current_index < user_data->number_of_keys)
216         {
217         if(wep_key_number == (user_data->current_index+1))
218             {
219             my_string = g_strdup((char*)g_list_nth_data(user_data->list,user_data->current_index));
220
221                         g_free((void *)*pref->varp.string);
222                         *pref->varp.string = (void *)g_strdup(my_string);
223
224             g_free(my_string);
225             }
226         }
227     else /* If the number of keys has been reduced somehow, we need to delete all the other keys
228           * (remember that the new ones have been probably overwritten)
229           */
230         {
231         g_free((void *)*pref->varp.string);
232         *pref->varp.string = (void *)g_strdup("");  /* Do not just free memory!!! Put an 'empty' string! */
233         }
234     user_data->current_index++;
235     }
236
237 return 0;
238 }
239
240 /*
241  * Function used to read the Decryption Keys from the preferences and store them
242  * properly into the airpcap adapter.
243  */
244 BOOL
245 load_wlan_wep_keys(airpcap_if_info_t* info_if)
246 {
247 keys_cb_data_t* user_data;
248 guint i;
249 gchar *tmp = NULL;
250
251 if(info_if == NULL) return FALSE;
252
253 /* Retrieve the wlan preferences */
254 wlan_prefs = prefs_find_module("wlan");
255
256 /* Allocate a structure used to keep infos  between the callbacks */
257 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
258
259 /* Fill the structure */
260 user_data->list = NULL;
261 user_data->current_index = 0;
262 user_data->number_of_keys= 0; /* Still unknown */
263
264 /* Run the callback on each 802.11 preference */
265 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
266
267 /* Now the key list should be filled */
268
269 /*
270  * Signal that we've changed things, and run the 802.11 dissector's
271  * callback
272  */
273 wlan_prefs->prefs_changed = TRUE;
274
275 prefs_apply(wlan_prefs);
276
277 write_wlan_wep_keys_to_regitry(info_if,user_data->list);
278
279 /* FREE MEMORY */
280 /* free the WEP key string */
281 for(i=0;i<g_list_length(user_data->list);i++)
282     {
283     g_free(g_list_nth(user_data->list,i)->data);
284     }
285
286 /* free the (empty) list */
287 g_list_free(user_data->list);
288
289 /* free the user_data structure */
290 g_free(user_data);
291
292 return TRUE;
293 }
294
295
296
297 /*
298  * Function used to read the Decryption Keys from the preferences and store them
299  * properly into the airpcap adapter.
300  */
301 BOOL
302 load_wlan_driver_wep_keys()
303 {
304 keys_cb_data_t* user_data;
305 guint i;
306 gchar *tmp = NULL;
307
308 /* Retrieve the wlan preferences */
309 wlan_prefs = prefs_find_module("wlan");
310
311 /* Allocate a structure used to keep infos  between the callbacks */
312 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
313
314 /* Fill the structure */
315 user_data->list = NULL;
316 user_data->current_index = 0;
317 user_data->number_of_keys= 0; /* Still unknown */
318
319 /* Run the callback on each 802.11 preference */
320 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
321
322 /* Now the key list should be filled */
323
324 /*
325  * Signal that we've changed things, and run the 802.11 dissector's
326  * callback
327  */
328 wlan_prefs->prefs_changed = TRUE;
329
330 prefs_apply(wlan_prefs);
331
332 write_wlan_driver_wep_keys_to_regitry(user_data->list);
333
334 /* FREE MEMORY */
335 /* free the WEP key string */
336 for(i=0;i<g_list_length(user_data->list);i++)
337     {
338     g_free(g_list_nth(user_data->list,i)->data);
339     }
340
341 /* free the (empty) list */
342 g_list_free(user_data->list);
343
344 /* free the user_data structure */
345 g_free(user_data);
346
347 /* airpcap_if_info_free(fake_info_if); */
348
349 return TRUE;
350 }
351
352 /*
353  * This function will tell the airpcap driver the key list to use
354  * This will be stored into the registry...
355  */
356 BOOL
357 write_wlan_wep_keys_to_regitry(airpcap_if_info_t* info_if, GList* key_list)
358 {
359 UINT i,j;
360 GString *new_key;
361 gchar s[3];
362 PAirpcapKeysCollection KeysCollection;
363 ULONG KeysCollectionSize;
364 UCHAR KeyByte;
365 UINT keys_in_list = 0;
366 decryption_key_t* key_item = NULL;
367
368 keys_in_list = g_list_length(key_list);
369
370 /*
371  * Save the encryption keys, if we have any of them
372  */
373 KeysCollectionSize = 0;
374
375 /*
376  * Calculate the size of the keys collection
377  */
378 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
379
380 /*
381  * Allocate the collection
382  */
383 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
384 if(!KeysCollection)
385 {
386         return FALSE;
387 }
388
389 /*
390  * Populate the key collection
391  */
392 KeysCollection->nKeys = keys_in_list;
393
394 for(i = 0; i < keys_in_list; i++)
395 {
396     KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_WEP;
397
398         /* Retrieve the Item corresponding to the i-th key */
399         key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
400         new_key = g_string_new(key_item->key->str);
401
402         KeysCollection->Keys[i].KeyLen = new_key->len / 2;
403         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
404
405         for(j = 0 ; j < new_key->len; j += 2)
406         {
407                 s[0] = new_key->str[j];
408                 s[1] = new_key->str[j+1];
409                 s[2] = '\0';
410                 KeyByte = (UCHAR)strtol(s, NULL, 16);
411                 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
412         }
413
414         g_string_free(new_key,TRUE);
415
416 }
417 /*
418  * Free the old adapter key collection!
419  */
420 if(info_if->keysCollection != NULL)
421         g_free(info_if->keysCollection);
422
423 /*
424  * Set this collection ad the new one
425  */
426 info_if->keysCollection = KeysCollection;
427 info_if->keysCollectionSize = KeysCollectionSize;
428
429 /*
430  * Configuration must be saved
431  */
432 info_if->saved = FALSE;
433
434 /*
435  * Write down the changes to the registry
436  */
437 airpcap_save_selected_if_configuration(info_if);
438
439 return TRUE;
440 }
441
442 /*
443  * This function will tell the airpcap driver the key list to use
444  * This will be stored into the registry...
445  */
446 BOOL
447 write_wlan_driver_wep_keys_to_regitry(GList* key_list)
448 {
449 UINT i,j;
450 GString *new_key;
451 gchar s[3];
452 PAirpcapKeysCollection KeysCollection;
453 ULONG KeysCollectionSize;
454 UCHAR KeyByte;
455 UINT keys_in_list = 0;
456 decryption_key_t* key_item = NULL;
457 airpcap_if_info_t* fake_info_if = NULL;
458
459 /* Create the fake_info_if from the first adapter of the list */
460 fake_info_if = airpcap_driver_fake_if_info_new();
461
462 keys_in_list = g_list_length(key_list);
463
464 /*
465  * Save the encryption keys, if we have any of them
466  */
467 KeysCollectionSize = 0;
468
469 /*
470  * Calculate the size of the keys collection
471  */
472 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
473
474 /*
475  * Allocate the collection
476  */
477 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
478 if(!KeysCollection)
479 {
480         return FALSE;
481 }
482
483 /*
484  * Populate the key collection
485  */
486 KeysCollection->nKeys = keys_in_list;
487
488 for(i = 0; i < keys_in_list; i++)
489 {
490     KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_WEP;
491
492         /* Retrieve the Item corresponding to the i-th key */
493         key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
494         new_key = g_string_new(key_item->key->str);
495
496         KeysCollection->Keys[i].KeyLen = new_key->len / 2;
497         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
498
499         for(j = 0 ; j < new_key->len; j += 2)
500         {
501                 s[0] = new_key->str[j];
502                 s[1] = new_key->str[j+1];
503                 s[2] = '\0';
504                 KeyByte = (UCHAR)strtol(s, NULL, 16);
505                 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
506         }
507
508         g_string_free(new_key,TRUE);
509 }
510
511 /*
512  * Free the old adapter key collection!
513  */
514 if(fake_info_if->keysCollection != NULL)
515         g_free(fake_info_if->keysCollection);
516
517 /*
518  * Set this collection ad the new one
519  */
520 fake_info_if->keysCollection = KeysCollection;
521 fake_info_if->keysCollectionSize = KeysCollectionSize;
522
523 /*
524  * Configuration must be saved
525  */
526 fake_info_if->saved = FALSE;
527
528 /*
529  * Write down the changes to the registry
530  */
531 airpcap_save_driver_if_configuration(fake_info_if);
532
533 airpcap_if_info_free(fake_info_if);
534
535 return TRUE;
536 }
537
538 /*
539  *  Function used to save to the preference file the Decryption Keys.
540  */
541 gboolean
542 save_wlan_wep_keys(airpcap_if_info_t* info_if)
543 {
544 GList* key_list = NULL;
545 char* tmp_key = NULL;
546 guint keys_in_list,i;
547 keys_cb_data_t* user_data;
548
549 if(info_if == NULL) return FALSE;
550
551 /* Retrieve the wlan preferences */
552 wlan_prefs = prefs_find_module("wlan");
553
554 /* Allocate a structure used to keep infos  between the callbacks */
555 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
556
557 /* Number of keys in key list */
558 /* Number of keys in key list */
559 if(info_if->keysCollectionSize != 0)
560     keys_in_list = (guint)(info_if->keysCollectionSize -  sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
561 else
562     keys_in_list = 0;
563
564 for(i=0; i<keys_in_list; i++)
565 {
566 /* Only if it is a WEP key... */
567 if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
568     {
569     tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
570     key_list = g_list_append(key_list,g_strdup(tmp_key));
571     g_free(tmp_key);
572     }
573 }
574
575 /* Now we know the exact number of WEP keys in the list, so store it ... */
576 keys_in_list = g_list_length(key_list);
577
578 /* Fill the structure */
579 user_data->list = key_list;
580 user_data->current_index = 0;
581 user_data->number_of_keys= keys_in_list;
582
583 /* Retrieve the wlan preferences */
584 wlan_prefs = prefs_find_module("wlan");
585
586 /* Run the callback on each 802.11 preference */
587 prefs_pref_foreach(wlan_prefs, set_wep_key,  (gpointer)user_data);
588
589 /* Signal that we've changed things, and run the 802.11 dissector's
590  * callback */
591 wlan_prefs->prefs_changed = TRUE;
592
593 /* Apply changes for the specified preference */
594 prefs_apply(wlan_prefs);
595
596 /* FREE MEMORY */
597 /* free the WEP key string */
598 for(i=0;i<g_list_length(user_data->list);i++)
599     {
600     g_free(g_list_nth(user_data->list,i)->data);
601     }
602
603 /* free the (empty) list */
604 g_list_free(user_data->list);
605
606 /* free the user_data structure */
607 g_free(user_data);
608
609 return TRUE;
610 }
611
612 /*
613  *  Function used to save to the preference file the Decryption Keys.
614  */
615 int
616 save_wlan_driver_wep_keys()
617 {
618 GList* key_list = NULL;
619 char* tmp_key = NULL;
620 guint keys_in_list,i;
621 keys_cb_data_t* user_data;
622 airpcap_if_info_t* fake_info_if = NULL;
623
624 /* Create the fake_info_if from the first adapter of the list */
625 fake_info_if = airpcap_driver_fake_if_info_new();
626
627 /* Retrieve the wlan preferences */
628 wlan_prefs = prefs_find_module("wlan");
629
630 /* Allocate a structure used to keep infos  between the callbacks */
631 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
632
633 /* Number of keys in key list */
634 /* Number of keys in key list */
635 if(fake_info_if->keysCollectionSize != 0)
636     keys_in_list = (guint)(fake_info_if->keysCollectionSize -  sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
637 else
638     keys_in_list = 0;
639
640 for(i=0; i<keys_in_list; i++)
641 {
642 /* Only if it is a WEP key... */
643 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
644     {
645     tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
646     key_list = g_list_append(key_list,g_strdup(tmp_key));
647     g_free(tmp_key);
648     }
649 }
650
651 /* Now we know the exact number of WEP keys in the list, so store it ... */
652 keys_in_list = g_list_length(key_list);
653
654 /* Fill the structure */
655 user_data->list = key_list;
656 user_data->current_index = 0;
657 user_data->number_of_keys= keys_in_list;
658
659 /* Retrieve the wlan preferences */
660 wlan_prefs = prefs_find_module("wlan");
661
662 /* Run the callback on each 802.11 preference */
663 prefs_pref_foreach(wlan_prefs, set_wep_key,  (gpointer)user_data);
664
665 /* Signal that we've changed things, and run the 802.11 dissector's
666  * callback */
667 wlan_prefs->prefs_changed = TRUE;
668
669 /* Apply changes for the specified preference */
670 prefs_apply(wlan_prefs);
671
672 /* FREE MEMORY */
673 /* free the WEP key string */
674 for(i=0;i<g_list_length(user_data->list);i++)
675     {
676     g_free(g_list_nth(user_data->list,i)->data);
677     }
678
679 /* free the (empty) list */
680 g_list_free(user_data->list);
681
682 /* free the user_data structure */
683 g_free(user_data);
684
685 airpcap_if_info_free(fake_info_if);
686
687 return keys_in_list;
688 }
689
690 /*
691  *  Function used to save to the preference file the Decryption Keys.
692  */
693 int
694 save_wlan_wireshark_wep_keys(GList* key_ls)
695 {
696 GList* key_list = NULL;
697 char* tmp_key = NULL;
698 guint keys_in_list,i;
699 keys_cb_data_t* user_data;
700 airpcap_if_info_t* fake_info_if = NULL;
701
702 /* Retrieve the wlan preferences */
703 wlan_prefs = prefs_find_module("wlan");
704
705 /* Allocate a structure used to keep infos  between the callbacks */
706 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
707
708 keys_in_list = g_list_length(key_ls);
709
710 key_list = key_ls;
711
712 /* Fill the structure */
713 user_data->list = key_list;
714 user_data->current_index = 0;
715 user_data->number_of_keys= keys_in_list;
716
717 /* Retrieve the wlan preferences */
718 wlan_prefs = prefs_find_module("wlan");
719
720 /* Run the callback on each 802.11 preference */
721 prefs_pref_foreach(wlan_prefs, set_wep_key,  (gpointer)user_data);
722
723 /* Signal that we've changed things, and run the 802.11 dissector's
724  * callback */
725 wlan_prefs->prefs_changed = TRUE;
726
727 /* Apply changes for the specified preference */
728 prefs_apply(wlan_prefs);
729
730 /* FREE MEMORY */
731 /* free the WEP key string */
732 for(i=0;i<g_list_length(user_data->list);i++)
733     {
734     g_free(g_list_nth(user_data->list,i)->data);
735     }
736
737 /* free the (empty) list */
738 g_list_free(user_data->list);
739
740 /* free the user_data structure */
741 g_free(user_data);
742
743 return keys_in_list;
744 }
745
746 /*
747  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
748  * "get_airpcap_interface_list()".
749  */
750 gchar *
751 cant_get_airpcap_if_list_error_message(const char *err_str)
752 {
753         return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
754 }
755
756 /*
757  * Airpcap wrapper, used to store the current settings for the selected adapter
758  */
759 BOOL
760 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
761 {
762         if (!AirpcapLoaded) return FALSE;
763         return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
764 }
765
766 /*
767  * Airpcap wrapper, used to open an airpcap adapter
768  */
769 PAirpcapHandle
770 airpcap_if_open(PCHAR name, PCHAR err)
771 {
772         if (!AirpcapLoaded) return NULL;
773         return g_PAirpcapOpen(name,err);
774 }
775
776 /*
777  * Airpcap wrapper, used to close an airpcap adapter
778  */
779 VOID
780 airpcap_if_close(PAirpcapHandle handle)
781 {
782         if (!AirpcapLoaded) return;
783         g_PAirpcapClose(handle);
784 }
785
786 /*
787  * Airpcap wrapper, used to turn on the led of an airpcap adapter
788  */
789 BOOL
790 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
791 {
792         if (!AirpcapLoaded) return FALSE;
793         return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
794 }
795
796 /*
797  * Airpcap wrapper, used to turn off the led of an airpcap adapter
798  */
799 BOOL
800 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
801 {
802         if (!AirpcapLoaded) return FALSE;
803         return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
804 }
805
806 /*
807  * Airpcap wrapper, used to get the channel of an airpcap adapter
808  */
809 BOOL
810 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
811 {
812         if (!AirpcapLoaded) return FALSE;
813         return g_PAirpcapGetDeviceChannel(ah,ch);
814 }
815
816 /*
817  * Airpcap wrapper, used to set the channel of an airpcap adapter
818  */
819 BOOL
820 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
821 {
822         if (!AirpcapLoaded) return FALSE;
823         return g_PAirpcapSetDeviceChannel(ah,ch);
824 }
825
826 /*
827  * Airpcap wrapper, used to get the link type of an airpcap adapter
828  */
829 BOOL
830 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
831 {
832         if (!AirpcapLoaded) return FALSE;
833         return g_PAirpcapGetLinkType(ah,lt);
834 }
835
836 /*
837  * Airpcap wrapper, used to set the link type of an airpcap adapter
838  */
839 BOOL
840 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
841 {
842         if (!AirpcapLoaded) return FALSE;
843         return g_PAirpcapSetLinkType(ah,lt);
844 }
845
846 /*
847  * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
848  */
849 BOOL
850 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
851 {
852         if (!AirpcapLoaded) return FALSE;
853         return g_PAirpcapGetFcsPresence(ah,fcs);
854 }
855
856 /*
857  * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
858  */
859 BOOL
860 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
861 {
862         if (!AirpcapLoaded) return FALSE;
863         return g_PAirpcapSetFcsPresence(ah,fcs);
864 }
865
866 /*
867  * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
868  */
869 BOOL
870 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
871 {
872         if (!AirpcapLoaded) return FALSE;
873         return g_PAirpcapGetDecryptionState(ah,PEnable);
874 }
875
876 /*
877  * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
878  */
879 BOOL
880 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
881 {
882         if (!AirpcapLoaded) return FALSE;
883         return g_PAirpcapSetDecryptionState(ah,Enable);
884 }
885
886 /*
887  * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
888  */
889 BOOL
890 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
891 {
892         if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
893         return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
894 }
895
896 /*
897  * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
898  */
899 BOOL
900 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
901 {
902         if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
903         return g_PAirpcapSetDriverDecryptionState(ah,Enable);
904 }
905
906 /*
907  * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
908  */
909 BOOL
910 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
911 {
912         if (!AirpcapLoaded) return FALSE;
913         return g_PAirpcapGetFcsValidation(ah,val);
914 }
915
916 /*
917  * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
918  */
919 BOOL
920 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
921 {
922         if (!AirpcapLoaded) return FALSE;
923         return g_PAirpcapSetFcsValidation(ah,val);
924 }
925
926 /*
927  * Airpcap wrapper, used to save the settings for the selected_if
928  */
929 BOOL
930 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
931 {
932         if (!AirpcapLoaded) return FALSE;
933         return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
934 }
935
936 /*
937  * Airpcap wrapper, used to save the settings for the selected_if
938  */
939 BOOL
940 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
941 {
942         if (!AirpcapLoaded) return FALSE;
943         return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
944 }
945
946 /*
947  * Airpcap wrapper, used to save the driver's set of keys
948  */
949 BOOL
950 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
951 {
952         if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
953         return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
954 }
955
956 /*
957  * Airpcap wrapper, used to load the driver's set of keys
958  */
959 BOOL
960 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
961 {
962         if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
963         return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
964 }
965
966 /*
967  * This function will create a new airpcap_if_info_t using a name and a description
968  */
969 airpcap_if_info_t *
970 airpcap_if_info_new(char *name, char *description)
971 {
972 PAirpcapHandle ad;
973 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
974
975         airpcap_if_info_t *if_info;
976
977         if_info = g_malloc(sizeof (airpcap_if_info_t));
978         if_info->name = g_strdup(name);
979         if (description == NULL)
980                 if_info->description = NULL;
981         else
982                 if_info->description = g_strdup(description);
983         if_info->ip_addr = NULL;
984         if_info->loopback = FALSE;
985
986         /* Probably I have to switch on the leds!!! */
987         ad = airpcap_if_open(if_info->name, ebuf);
988         if(ad)
989                 {
990                 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
991                 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
992                 airpcap_if_get_link_type(ad,&(if_info->linkType));
993                 airpcap_if_get_device_channel(ad,&(if_info->channel));
994                 airpcap_if_turn_led_on(ad, 0);
995                 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
996                 if_info->led = TRUE;
997                 if_info->blinking = FALSE;
998                 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
999
1000                 /* get the keys, if everything is ok, close the adapter */
1001                 if(airpcap_if_load_keys(ad,if_info))
1002                         airpcap_if_close(ad);
1003                 }
1004         return if_info;
1005 }
1006
1007 /*
1008  * This function will create a new fake drivers' interface, to load global keys...
1009  */
1010 airpcap_if_info_t*
1011 airpcap_driver_fake_if_info_new()
1012 {
1013         PAirpcapHandle ad;
1014         gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1015
1016         airpcap_if_info_t *if_info = NULL;
1017         airpcap_if_info_t *fake_if_info = NULL;
1018
1019         /*
1020          * Retrieve the first AirPcap adapter available. If no interface is found,
1021          * it is not possible to retrieve the driver's settings, so return NULL.
1022          */
1023         if_info = g_list_nth_data(airpcap_if_list,0);
1024         if(if_info == NULL)
1025                 return NULL;
1026
1027         fake_if_info = g_malloc(sizeof (airpcap_if_info_t));
1028         fake_if_info->name = g_strdup(if_info->name);
1029         fake_if_info->description = g_strdup(if_info->description);
1030         fake_if_info->loopback = FALSE;
1031         fake_if_info->ip_addr = NULL;
1032
1033         /* Open the 'fake' adapter */
1034         ad = airpcap_if_open(if_info->name, ebuf);
1035         if(ad)
1036                 {
1037                 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
1038                 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
1039                 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
1040                 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
1041                 airpcap_if_get_device_channel(ad,&(fake_if_info->channel));
1042                 airpcap_if_turn_led_on(ad, 0);
1043                 fake_if_info->led = TRUE;
1044                 fake_if_info->blinking = FALSE;
1045                 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1046
1047                 /* get the keys, if everything is ok, close the adapter */
1048                 if(airpcap_if_load_driver_keys(ad,fake_if_info))
1049                         airpcap_if_close(ad);
1050                 }
1051
1052         return fake_if_info;
1053 }
1054
1055 /*
1056  * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
1057  */
1058 void
1059 airpcap_if_info_print(airpcap_if_info_t* if_info)
1060 {
1061 if(if_info == NULL)
1062         {
1063         g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
1064         return;
1065         }
1066
1067 g_print("\n----------------- AirPcap Interface \n");
1068 g_print("              NAME: %s\n",if_info->name);
1069 g_print("       DESCRIPTION: %s\n",if_info->description);
1070 g_print("          BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
1071 g_print("           CHANNEL: %2u\n",if_info->channel);
1072 g_print("     CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
1073 g_print("        DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
1074 g_print("           IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
1075 g_print("        FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
1076 g_print("    KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
1077 g_print("KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
1078 g_print("               LED: %s\n",if_info->led ? "ON" : "OFF");
1079 g_print("          LINKTYPE: %d\n",if_info->linkType);
1080 g_print("          LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
1081 g_print("         (GTK) TAG: %d\n",if_info->tag);
1082 g_print("\n\n");
1083 }
1084
1085 /*
1086  * Function used to load the WEP keys for a selected interface
1087  */
1088 BOOL
1089 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1090 {
1091 if(!if_info) return FALSE;
1092
1093 if_info->keysCollectionSize = 0;
1094 if_info->keysCollection = NULL;
1095
1096 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1097         {
1098         if(if_info->keysCollectionSize == 0)
1099                 {
1100                 if_info->keysCollection = NULL;
1101                 airpcap_if_close(ad);
1102                 return FALSE;
1103                 }
1104
1105         if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1106         if(!if_info->keysCollection)
1107                 {
1108                 if_info->keysCollectionSize = 0;
1109                 if_info->keysCollection = NULL;
1110                 airpcap_if_close(ad);
1111                 return FALSE;
1112                 }
1113
1114         airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1115         return TRUE;
1116         }
1117
1118 airpcap_if_close(ad);
1119 return FALSE;
1120 }
1121
1122 /*
1123  * Function used to load the WEP keys for a selected interface
1124  */
1125 BOOL
1126 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1127 {
1128 if_info->keysCollectionSize = 0;
1129 if_info->keysCollection = NULL;
1130
1131 if(!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1132         {
1133         if(if_info->keysCollectionSize == 0)
1134                 {
1135                 if_info->keysCollection = NULL;
1136                 airpcap_if_close(ad);
1137                 return FALSE;
1138                 }
1139
1140         if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1141         if(!if_info->keysCollection)
1142                 {
1143                 if_info->keysCollectionSize = 0;
1144                 if_info->keysCollection = NULL;
1145                 airpcap_if_close(ad);
1146                 return FALSE;
1147                 }
1148
1149         airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1150         return TRUE;
1151         }
1152
1153 airpcap_if_close(ad);
1154 return FALSE;
1155 }
1156
1157 /*
1158  * Function used to save the WEP keys for a selected interface
1159  */
1160 void
1161 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1162 {
1163         if(!if_info || !AirpcapLoaded) return;
1164
1165         if(if_info->keysCollection != NULL)
1166                 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1167 }
1168
1169 /*
1170  * Function used to save the WEP keys for a selected interface
1171  */
1172 void
1173 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1174 {
1175         if(if_info->keysCollection != NULL)
1176                 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1177 }
1178
1179 /*
1180  * Callback used to free an instance of airpcap_if_info_t
1181  */
1182 static void
1183 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1184 {
1185         airpcap_if_info_t *if_info = data;
1186
1187         if (if_info->name != NULL)
1188                 g_free(if_info->name);
1189
1190         if (if_info->description != NULL)
1191                 g_free(if_info->description);
1192
1193         /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1194         if(if_info->keysCollection != NULL)
1195                 {
1196                 g_free(if_info->keysCollection);
1197                 if_info->keysCollection = NULL;
1198                 }
1199
1200         if(if_info->ip_addr != NULL)
1201                 g_slist_free(if_info->ip_addr);
1202
1203         if(if_info != NULL)
1204                 g_free(if_info);
1205 }
1206
1207 /*
1208  * Function used to free the airpcap interface list
1209  */
1210 void
1211 free_airpcap_interface_list(GList *if_list)
1212 {
1213         g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1214         g_list_free(if_list);
1215         if_list = NULL;
1216 }
1217
1218 /*
1219  * This function will use the airpcap.dll to find all the airpcap devices.
1220  * Will return null if no device is found.
1221  */
1222 GList*
1223 get_airpcap_interface_list(int *err, char *err_str)
1224 {
1225         GList  *il = NULL;
1226         airpcap_if_info_t *if_info;
1227         int i, n_adapts;
1228     AirpcapDeviceDescription *devsList, *adListEntry;
1229
1230         if (err)
1231                 *err = NO_AIRPCAP_INTERFACES_FOUND;
1232
1233         if(!AirpcapLoaded || !g_PAirpcapGetDeviceList(&devsList, err_str))
1234         {
1235                 /* No interfaces, return il = NULL; */
1236                 return il;
1237         }
1238
1239         /*
1240          * Count the adapters
1241          */
1242         adListEntry = devsList;
1243         n_adapts = 0;
1244         while(adListEntry)
1245         {
1246                 n_adapts++;
1247                 adListEntry = adListEntry->next;
1248         }
1249
1250         if(n_adapts == 0)
1251         {
1252                 /* No interfaces, return il= NULL */
1253                 g_PAirpcapFreeDeviceList(devsList);
1254                 return il;
1255         }
1256
1257         /*
1258          * Insert the adapters in our list
1259          */
1260         adListEntry = devsList;
1261         for(i = 0; i < n_adapts; i++)
1262         {
1263                 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1264         il = g_list_append(il, if_info);
1265
1266                 adListEntry = adListEntry->next;
1267         }
1268
1269         g_PAirpcapFreeDeviceList(devsList);
1270
1271         return il;
1272 }
1273
1274 /*
1275  * Used to retrieve the name of the interface given the description
1276  * (the name is used in AirpcapOpen, the description is put in the combo box)
1277  */
1278 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
1279 {
1280 unsigned int ifn;
1281 GList* curr;
1282 airpcap_if_info_t* if_info;
1283
1284 ifn = 0;
1285 if(if_list != NULL)
1286         {
1287         while( ifn < g_list_length(if_list) )
1288                 {
1289                 curr = g_list_nth(if_list, ifn);
1290
1291                 if_info = NULL;
1292                 if(curr != NULL)
1293                         if_info = curr->data;
1294                 if(if_info != NULL)
1295                         if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1296                                 {
1297                                 return if_info->name;
1298                                 }
1299                 ifn++;
1300                 }
1301         }
1302 return NULL;
1303 }
1304
1305 /*
1306  * Used to retrieve the interface given the name
1307  * (the name is used in AirpcapOpen)
1308  */
1309 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
1310 {
1311 unsigned int ifn;
1312 GList* curr;
1313 airpcap_if_info_t* if_info;
1314
1315 ifn = 0;
1316 if(if_list != NULL)
1317         {
1318         while( ifn < g_list_length(if_list) )
1319                 {
1320                 curr = g_list_nth(if_list, ifn);
1321
1322                 if_info = NULL;
1323                 if(curr != NULL)
1324                         if_info = curr->data;
1325                 if(if_info != NULL)
1326                         if ( g_ascii_strcasecmp(if_info->name,name) == 0)
1327                                 {
1328                                 return if_info;
1329                                 }
1330                 ifn++;
1331                 }
1332         }
1333 return NULL;
1334 }
1335
1336 /*
1337  * Returns the ASCII string of a key given the key bytes
1338  */
1339 gchar*
1340 airpcap_get_key_string(AirpcapKey key)
1341 {
1342 unsigned int j = 0;
1343 unsigned int l = 0;
1344 gchar *dst,*src;
1345
1346 src = NULL;
1347
1348 if(key.KeyType == AIRPCAP_KEYTYPE_WEP)
1349         {
1350         if(key.KeyLen != 0)
1351             {
1352         /* Allocate the string used to store the ASCII representation of the WEP key */
1353         dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1354         /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1355         dst[0]='\0';
1356
1357             for(j = 0; j < key.KeyLen; j++)
1358                     {
1359                     src = g_strdup_printf("%.2x\0", key.KeyData[j]);
1360                         /*
1361                          * XXX - use g_strconcat() or GStrings instead ???
1362                          */
1363                 l = g_strlcat(dst,src,WEP_KEY_MAX_CHAR_SIZE+1);
1364                 }
1365         g_free(src);
1366         }
1367         }
1368 else if(key.KeyType == AIRPCAP_KEYTYPE_TKIP)
1369     {
1370     /* XXX - Add code here */
1371     }
1372 else if(key.KeyType == AIRPCAP_KEYTYPE_CCMP)
1373     {
1374     /* XXX - Add code here */
1375     }
1376 else
1377     {
1378     /* XXX - Add code here */
1379     }
1380
1381 return dst;
1382 }
1383
1384 /*
1385  * Clear keys and decryption status for the specified interface
1386  */
1387 void
1388 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1389 {
1390 if(info_if != NULL)
1391         {
1392         if(info_if->keysCollection != NULL)
1393                 {
1394                 g_free(info_if->keysCollection);
1395                 info_if->keysCollection = NULL;
1396                 }
1397
1398         info_if->keysCollectionSize = 0;
1399
1400         info_if->DecryptionOn = FALSE;
1401         info_if->saved = FALSE;
1402         }
1403 }
1404
1405 /*
1406  * Used to retrieve the airpcap_if_info_t of the selected interface given the
1407  * description (that is the entry of the combo box).
1408  */
1409 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
1410 {
1411 unsigned int ifn;
1412 GList* curr;
1413 airpcap_if_info_t* if_info;
1414
1415 ifn = 0;
1416 if(if_list != NULL)
1417         {
1418         while( ifn < g_list_length(if_list) )
1419                 {
1420                 curr = g_list_nth(if_list, ifn);
1421
1422                 if_info = NULL;
1423                 if(curr != NULL)
1424                         if_info = curr->data;
1425                 if(if_info != NULL)
1426                         if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1427                                 {
1428                                 return if_info;
1429                                 }
1430                 ifn++;
1431                 }
1432         }
1433 return NULL;
1434 }
1435
1436 /*
1437  * Used to retrieve the two chars string from interface
1438  */
1439 gchar*
1440 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1441 {
1442         gchar* number;
1443         guint n;
1444         int a;
1445
1446         a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1447
1448     /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1449      * Otherwise, check if it is the "Any" adapter...
1450      */
1451      if(a == 0)
1452           {
1453           if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1454                number = g_strdup_printf("??");
1455           else
1456                number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1457           }
1458      else
1459           {
1460           number = g_strdup_printf("%.2u\0",n);
1461           }
1462
1463         return number;
1464 }
1465
1466 /*
1467  * Used to retrieve the two chars string from interface
1468  */
1469 gchar*
1470 airpcap_get_if_string_number_from_description(gchar* description)
1471 {
1472         gchar* number;
1473         gchar* pointer;
1474
1475         number = (gchar*)g_malloc(sizeof(gchar)*3);
1476
1477         pointer = g_strrstr(description,"#\0");
1478
1479         number[0] = *(pointer+1);
1480         number[1] = *(pointer+2);
1481         number[2] = '\0';
1482
1483         return number;
1484 }
1485
1486 /*
1487  * Returns the default airpcap interface of a list, NULL if list is empty
1488  */
1489 airpcap_if_info_t*
1490 airpcap_get_default_if(GList* airpcap_if_list)
1491 {
1492 int ifn = 0;
1493 GList* popdown_if_list = NULL;
1494 GList* curr = NULL;
1495
1496         gchar* s;
1497         airpcap_if_info_t* if_info = NULL;
1498
1499     if(prefs.capture_device != NULL)
1500     {
1501         s = g_strdup(get_if_name(prefs.capture_device));
1502         if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
1503         g_free(s);
1504     }
1505         return if_info;
1506 }
1507
1508 /*
1509  * Load the configuration for the specified interface
1510  */
1511 void
1512 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1513 {
1514 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1515 PAirpcapHandle ad;
1516
1517 if(if_info != NULL)
1518         {
1519         ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1520
1521         if(ad)
1522                 {
1523                 /* Stop blinking (if it was blinkig!)*/
1524                 if(if_info->blinking)
1525                         {
1526                         /* Turn on the light (if it was off) */
1527                         if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1528                         }
1529
1530                 /* Apply settings... */
1531                 airpcap_if_get_device_channel(ad,&(if_info->channel));
1532                 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1533                 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1534                 airpcap_if_get_link_type(ad,&(if_info->linkType));
1535                 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1536                 /* get the keys, if everything is ok, close the adapter */
1537                 if(airpcap_if_load_keys(ad,if_info))
1538                         airpcap_if_close(ad);
1539
1540                 if_info->saved = TRUE;
1541                 }
1542         else
1543                 {
1544                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1545                 }
1546         }
1547 }
1548
1549 /*
1550  * Save the configuration for the specified interface
1551  */
1552 void
1553 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1554 {
1555 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1556 PAirpcapHandle ad;
1557
1558 if(if_info != NULL)
1559         {
1560         ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1561
1562         if(ad)
1563                 {
1564                 /* Stop blinking (if it was blinkig!)*/
1565                 if(if_info->blinking)
1566                         {
1567                         /* Turn on the light (if it was off) */
1568                         if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1569                         }
1570
1571                 /* Apply settings... */
1572                 airpcap_if_set_device_channel(ad,if_info->channel);
1573                 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1574                 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1575                 airpcap_if_set_link_type(ad,if_info->linkType);
1576                 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1577                 airpcap_if_save_keys(ad,if_info);
1578
1579                 /* ... and save them */
1580                 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
1581                         {
1582                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save Wireless configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
1583                         if_info->saved = FALSE;
1584                         airpcap_if_close(ad);
1585                         return;
1586                         }
1587
1588                 if_info->saved = TRUE;
1589                 airpcap_if_close(ad);
1590                 }
1591         else
1592                 {
1593                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1594                 }
1595         }
1596 }
1597
1598 /*
1599  * Save the configuration for the specified interface
1600  */
1601 void
1602 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1603 {
1604 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1605 PAirpcapHandle ad;
1606
1607 if(fake_if_info != NULL)
1608         {
1609         ad = airpcap_if_open(fake_if_info->name, ebuf);
1610
1611         if(ad)
1612                 {
1613                 /* Apply decryption settings... */
1614                 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1615                 airpcap_if_save_driver_keys(ad,fake_if_info);
1616                 airpcap_if_close(ad);
1617                 }
1618         else
1619                 {
1620                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1621                 }
1622         }
1623
1624 return;
1625 }
1626
1627 /*
1628  * DECRYPTION KEYS FUNCTIONS
1629  */
1630 /*
1631  * This function is used for DEBUG POURPOSES ONLY!!!
1632  */
1633 void
1634 print_key_list(GList* key_list)
1635 {
1636 gint n,i;
1637 decryption_key_t* tmp;
1638
1639 if(key_list == NULL)
1640 {
1641 g_print("\n\n******* KEY LIST NULL *******\n\n");
1642 return;
1643 }
1644
1645 n = g_list_length(key_list);
1646
1647 g_print("\n\n********* KEY LIST **********\n\n");
1648
1649 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1650
1651 for(i =0; i < n; i++)
1652 {
1653 g_print("[%d] :\n",i+1);
1654 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1655 g_print("KEY : %s\n",tmp->key->str);
1656
1657 g_print("BITS: %d\n",tmp->bits);
1658
1659 if(tmp->type == AIRPCAP_KEYTYPE_WEP)
1660     g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1661 else if(tmp->type == AIRPCAP_KEYTYPE_TKIP)
1662     g_print("TYPE: %s\n",AIRPCAP_WPA_KEY_STRING);
1663 else if(tmp->type == AIRPCAP_KEYTYPE_CCMP)
1664     g_print("TYPE: %s\n",AIRPCAP_WPA2_KEY_STRING);
1665 else
1666     g_print("TYPE: %s\n","???");
1667
1668 g_print("SSID: %s\n",(tmp->ssid != NULL) ? tmp->ssid->str : "---");
1669 g_print("\n");
1670 }
1671
1672 g_print("\n*****************************\n\n");
1673 }
1674
1675 /*
1676  * Retrieves a GList of decryption_key_t structures containing infos about the
1677  * keys for the given adapter... returns NULL if no keys are found.
1678  */
1679 GList*
1680 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1681 {
1682 /* tmp vars */
1683 char* tmp_key = NULL;
1684 guint i,keys_in_list = 0;
1685
1686 /* real vars*/
1687 decryption_key_t *new_key  = NULL;
1688 GList            *key_list = NULL;
1689
1690 /* Number of keys in key list */
1691 if(info_if->keysCollectionSize != 0)
1692     keys_in_list = (guint)(info_if->keysCollectionSize -  sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1693 else
1694     keys_in_list = 0;
1695
1696 for(i=0; i<keys_in_list; i++)
1697 {
1698 /* Different things to do depending on the key type  */
1699 if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
1700     {
1701     /* allocate memory for the new key item */
1702     new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1703
1704     /* fill the fields */
1705     /* KEY */
1706     tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1707     new_key->key = g_string_new(tmp_key);
1708     g_free(tmp_key);
1709
1710     /* BITS */
1711     new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1712
1713     /* SSID not used in WEP keys */
1714     new_key->ssid = NULL;
1715
1716     /* TYPE (WEP in this case) */
1717     new_key->type = info_if->keysCollection->Keys[i].KeyType;
1718
1719     /* Append the new element in the list */
1720     key_list = g_list_append(key_list,(gpointer)new_key);
1721     }
1722 else if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_TKIP)
1723     {
1724     /* XXX - Not supported yet */
1725     }
1726 else if(info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_CCMP)
1727     {
1728     /* XXX - Not supported yet */
1729     }
1730 }
1731
1732 return key_list;
1733 }
1734
1735 /*
1736  * Retrieves a GList of decryption_key_t structures containing infos about the
1737  * keys for the global AirPcap driver... returns NULL if no keys are found.
1738  */
1739 GList*
1740 get_airpcap_driver_keys()
1741 {
1742 /* tmp vars */
1743 char* tmp_key = NULL;
1744 guint i,keys_in_list = 0;
1745
1746 /* real vars*/
1747 decryption_key_t *new_key  = NULL;
1748 GList            *key_list = NULL;
1749
1750 /*
1751  * To read the drivers general settings we need to create and use one airpcap adapter...
1752  * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1753  */
1754 airpcap_if_info_t* fake_info_if = NULL;
1755
1756 /* Create the fake_info_if from the first adapter of the list */
1757 fake_info_if = airpcap_driver_fake_if_info_new();
1758
1759 if(fake_info_if == NULL)
1760         return NULL;
1761
1762 /* Number of keys in key list */
1763 if(fake_info_if->keysCollectionSize != 0)
1764     keys_in_list = (guint)(fake_info_if->keysCollectionSize -  sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1765 else
1766     keys_in_list = 0;
1767
1768 for(i=0; i<keys_in_list; i++)
1769 {
1770 /* Different things to do depending on the key type  */
1771 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_WEP)
1772     {
1773     /* allocate memory for the new key item */
1774     new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1775
1776     /* fill the fields */
1777     /* KEY */
1778     tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1779     new_key->key = g_string_new(tmp_key);
1780     g_free(tmp_key);
1781
1782     /* BITS */
1783     new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1784
1785     /* SSID not used in WEP keys */
1786     new_key->ssid = NULL;
1787
1788     /* TYPE (WEP in this case) */
1789     new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1790
1791     /* Append the new element in the list */
1792     key_list = g_list_append(key_list,(gpointer)new_key);
1793     }
1794 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_TKIP)
1795     {
1796     /* XXX - Not supported yet */
1797     }
1798 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPCAP_KEYTYPE_CCMP)
1799     {
1800     /* XXX - Not supported yet */
1801     }
1802 }
1803
1804 airpcap_if_info_free(fake_info_if);
1805
1806 return key_list;
1807 }
1808
1809 /*
1810  * Returns the list of the decryption keys specified for wireshark, NULL if
1811  * no key is found
1812  */
1813 GList*
1814 get_wireshark_keys()
1815 {
1816 keys_cb_data_t* wep_user_data = NULL;
1817 keys_cb_data_t* wpa_user_data = NULL;
1818 keys_cb_data_t* wpa2_user_data= NULL;
1819
1820 gchar *tmp = NULL;
1821
1822 GList* final_list = NULL;
1823 GList* wep_final_list = NULL;
1824 GList* wpa_final_list = NULL;
1825 GList* wpa2_final_list = NULL;
1826
1827 /* Retrieve the wlan preferences */
1828 wlan_prefs = prefs_find_module("wlan");
1829
1830 /* Allocate a structure used to keep infos  between the callbacks */
1831 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1832
1833 /* Fill the structure */
1834 wep_user_data->list = NULL;
1835 wep_user_data->current_index = 0;
1836 wep_user_data->number_of_keys= 0; /* Still unknown */
1837
1838 /* Run the callback on each 802.11 preference */
1839 /* XXX - Right now, only WEP keys will be loaded */
1840 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1841 prefs_pref_foreach(wlan_prefs, get_wpa_key, (gpointer)wpa_user_data);
1842 prefs_pref_foreach(wlan_prefs, get_wpa2_key, (gpointer)wpa2_user_data);
1843
1844 /* Copy the list field in the user data structure pointer into the final_list */
1845 if(wep_user_data != NULL)  wep_final_list  = wep_user_data->list;
1846 if(wpa_user_data != NULL)  wpa_final_list  = wpa_user_data->list;
1847 if(wpa2_user_data != NULL) wpa2_final_list = wpa2_user_data->list;
1848
1849 /* XXX - Merge the three lists!!!!! */
1850 final_list = wep_final_list;
1851
1852 /* free the wep_user_data structure */
1853 g_free(wep_user_data);
1854 /* free the wpa_user_data structure */
1855 g_free(wpa_user_data);
1856 /* free the wpa2_user_data structure */
1857 g_free(wpa2_user_data);
1858
1859 return final_list;
1860 }
1861
1862 /*
1863  * Merges two lists of keys and return a newly created GList. If a key is
1864  * found multiple times, it will just appear once!
1865  * list1 and list 2 pointer will have to be freed manually if needed!!!
1866  * If the total number of keys exceeeds the maximum number allowed,
1867  * exceeding keys will be discarded...
1868  */
1869 GList*
1870 merge_key_list(GList* list1, GList* list2)
1871 {
1872 guint n1=0,n2=0;
1873 guint i;
1874 decryption_key_t *dk1=NULL,
1875                  *dk2=NULL,
1876                  *new_dk=NULL;
1877
1878 GList* merged_list = NULL;
1879
1880 if( (list1 == NULL) && (list2 == NULL) )
1881     return NULL;
1882
1883 if(list1 == NULL)
1884     {
1885     n1 = 0;
1886     n2 = g_list_length(list2);
1887
1888     for(i=0;i<n2;i++)
1889         {
1890         new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1891         dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1892
1893         new_dk->bits = dk2->bits;
1894         new_dk->type = dk2->type;
1895         new_dk->key  = g_string_new(dk2->key->str);
1896         if(dk2->ssid != NULL)
1897             new_dk->ssid = g_string_new(dk2->ssid->str);
1898         else
1899             new_dk->ssid = NULL;
1900
1901                 /* Check the total length of the merged list */
1902                 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1903                         merged_list = g_list_append(merged_list,(gpointer)new_dk);
1904         }
1905     }
1906 else if(list2 == NULL)
1907     {
1908     n1 = g_list_length(list1);
1909     n2 = 0;
1910
1911     for(i=0;i<n1;i++)
1912         {
1913         new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1914         dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1915
1916         new_dk->bits = dk1->bits;
1917         new_dk->type = dk1->type;
1918         new_dk->key  = g_string_new(dk1->key->str);
1919         if(dk1->ssid != NULL)
1920             new_dk->ssid = g_string_new(dk1->ssid->str);
1921         else
1922             new_dk->ssid = NULL;
1923
1924                 /* Check the total length of the merged list */
1925                 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1926                         merged_list = g_list_append(merged_list,(gpointer)new_dk);
1927         }
1928     }
1929 else
1930     {
1931     n1 = g_list_length(list1);
1932     n2 = g_list_length(list2);
1933
1934     /* Copy the whole list1 into merged_list */
1935     for(i=0;i<n1;i++)
1936     {
1937     new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1938     dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
1939
1940     new_dk->bits = dk1->bits;
1941     new_dk->type = dk1->type;
1942     new_dk->key  = g_string_new(dk1->key->str);
1943
1944     if(dk1->ssid != NULL)
1945         new_dk->ssid = g_string_new(dk1->ssid->str);
1946     else
1947         new_dk->ssid = NULL;
1948
1949         /* Check the total length of the merged list */
1950         if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1951                 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1952     }
1953
1954     /* Look for keys that are present in list2 but aren't in list1 yet...
1955      * Add them to merged_list
1956      */
1957     for(i=0;i<n2;i++)
1958         {
1959         dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1960
1961         if(!key_is_in_list(dk2,merged_list))
1962             {
1963             new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1964
1965             new_dk->bits = dk2->bits;
1966             new_dk->type = dk2->type;
1967             new_dk->key  = g_string_new(dk2->key->str);
1968             if(dk2->ssid != NULL)
1969                 new_dk->ssid = g_string_new(dk2->ssid->str);
1970             else
1971                 new_dk->ssid = NULL;
1972
1973                         /* Check the total length of the merged list */
1974                         if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1975                                 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1976             }
1977         }
1978     }
1979
1980 return merged_list;
1981 }
1982
1983 /*
1984  * Use this function to free a key list.
1985  */
1986 void
1987 free_key_list(GList *list)
1988 {
1989 guint i,n;
1990 decryption_key_t *curr_key;
1991
1992 if(list == NULL)
1993     return;
1994
1995 n = g_list_length(list);
1996
1997 for(i = 0; i < n; i++)
1998 {
1999 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2000
2001 /* Free all the strings */
2002 if(curr_key->key != NULL)
2003     g_string_free(curr_key->key,TRUE);
2004
2005 if(curr_key->ssid != NULL)
2006 g_string_free(curr_key->ssid,TRUE);
2007
2008 /* free the decryption_key_t structure*/
2009 g_free(curr_key);
2010 curr_key = NULL;
2011 }
2012
2013 /* Free the list */
2014 g_list_free(list);
2015
2016 return;
2017 }
2018
2019
2020 /*
2021  * If the given key is contained in the list, returns TRUE.
2022  * Returns FALSE otherwise.
2023  */
2024 gboolean
2025 key_is_in_list(decryption_key_t *dk,GList *list)
2026 {
2027 guint i,n;
2028 decryption_key_t* curr_key = NULL;
2029 gboolean found = FALSE;
2030
2031 if( (list == NULL) || (dk == NULL) )
2032     return FALSE;
2033
2034 n = g_list_length(list);
2035
2036 if(n < 1)
2037     return FALSE;
2038
2039 for(i = 0; i < n; i++)
2040 {
2041 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2042 if(keys_are_equals(dk,curr_key))
2043     found = TRUE;
2044 }
2045
2046 return found;
2047 }
2048
2049 /*
2050  * Returns TRUE if keys are equals, FALSE otherwise
2051  */
2052 gboolean
2053 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
2054 {
2055
2056 if((k1==NULL) || (k2==NULL))
2057     return FALSE;
2058
2059 if( g_string_equal(k1->key,k2->key) &&
2060     (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
2061     k1->type == k2->type)
2062     {
2063     /* Check the ssid... if the key type is WEP, the two fields should be NULL */
2064     if((k1->ssid == NULL) && (k2->ssid == NULL))
2065         return TRUE;
2066
2067     /* Check if one of them is null and one is not... */
2068     if((k1->ssid == NULL) || (k2->ssid == NULL))
2069         return FALSE;
2070
2071     /* If they are not null, they must share the same ssid */
2072     return g_string_equal(k1->ssid,k2->ssid);
2073     }
2074
2075 /* Some field is not equal ... */
2076 return FALSE;
2077 }
2078
2079 /*
2080  * Tests if two collection of keys are equal or not, to be considered equals, they have to
2081  * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
2082  * return TRUE)
2083  */
2084 gboolean
2085 key_lists_are_equal(GList* list1, GList* list2)
2086 {
2087 guint n1=0,n2=0;
2088 guint i;
2089 decryption_key_t *dk1=NULL,*dk2=NULL;
2090
2091 n1 = g_list_length(list1);
2092 n2 = g_list_length(list2);
2093
2094 /*
2095  * Commented, because in the new AirPcap version all the keys will be saved
2096  * into the driver, and all the keys for every specific adapter will be
2097  * removed. This means that this check will always fail... and the user will
2098  * always be asked what to do... and it doesn't make much sense.
2099  */
2100 if(n1 != n2) return FALSE;
2101
2102 for(i=0;i<n1;i++)
2103 {
2104 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2105 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2106
2107 if(!g_string_equal(dk1->key,dk2->key)) return FALSE;
2108 }
2109
2110 return TRUE;
2111 }
2112
2113 static guint
2114 test_if_on(pref_t *pref, gpointer ud _U_)
2115 {
2116 gboolean *is_on;
2117 gboolean number;
2118
2119 /* Retrieve user data info */
2120 is_on = (gboolean*)ud;
2121
2122
2123 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2124     {
2125     number = *pref->varp.boolp;
2126
2127     if(number) *is_on = TRUE;
2128     else *is_on = FALSE;
2129
2130     return 1;
2131     }
2132 return 0;
2133 }
2134
2135 /*
2136  * Returns TRUE if the Wireshark decryption is active, false otherwise
2137  */
2138 gboolean
2139 wireshark_decryption_on()
2140 {
2141 gboolean is_on;
2142
2143 /* Retrieve the wlan preferences */
2144 wlan_prefs = prefs_find_module("wlan");
2145
2146 /* Run the callback on each 802.11 preference */
2147 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2148
2149 return is_on;
2150 }
2151
2152 /*
2153  * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2154  */
2155 gboolean
2156 airpcap_decryption_on()
2157 {
2158 gboolean is_on = FALSE;
2159
2160 airpcap_if_info_t* fake_if_info = NULL;
2161
2162 fake_if_info = airpcap_driver_fake_if_info_new();
2163
2164 if(fake_if_info != NULL)
2165     {
2166         if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2167                 is_on = TRUE;
2168         else if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2169                 is_on = FALSE;
2170     }
2171
2172 airpcap_if_info_free(fake_if_info);
2173
2174 return is_on;
2175 }
2176
2177 /*
2178  * Free an instance of airpcap_if_info_t
2179  */
2180 void
2181 airpcap_if_info_free(airpcap_if_info_t *if_info)
2182 {
2183 if(if_info != NULL)
2184         {
2185         if (if_info->name != NULL)
2186                 g_free(if_info->name);
2187
2188         if (if_info->description != NULL)
2189                 g_free(if_info->description);
2190
2191         if(if_info->keysCollection != NULL)
2192                 {
2193                 g_free(if_info->keysCollection);
2194                 if_info->keysCollection = NULL;
2195                 }
2196
2197         if(if_info->ip_addr != NULL)
2198                 {
2199                 g_slist_free(if_info->ip_addr);
2200                 if_info->ip_addr = NULL;
2201                 }
2202
2203         if(if_info != NULL)
2204                 {
2205                 g_free(if_info);
2206                 if_info = NULL;
2207                 }
2208         }
2209 }
2210
2211 static guint
2212 set_on_off(pref_t *pref, gpointer ud _U_)
2213 {
2214 gboolean *is_on;
2215 gboolean number;
2216
2217 /* Retrieve user data info */
2218 is_on = (gboolean*)ud;
2219
2220 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2221     {
2222     number = *pref->varp.boolp;
2223
2224     g_free((void *)*pref->varp.boolp);
2225     if(*is_on)
2226         *pref->varp.boolp = TRUE;
2227     else
2228         *pref->varp.boolp = FALSE;
2229
2230     return 1;
2231     }
2232 return 0;
2233 }
2234
2235 /*
2236  * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2237  */
2238 void
2239 set_wireshark_decryption(gboolean on_off)
2240 {
2241 gboolean is_on;
2242
2243 is_on = on_off;
2244
2245 /* Retrieve the wlan preferences */
2246 wlan_prefs = prefs_find_module("wlan");
2247
2248 /* Run the callback on each 802.11 preference */
2249 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2250
2251 /*
2252  * Signal that we've changed things, and run the 802.11 dissector's
2253  * callback
2254  */
2255 wlan_prefs->prefs_changed = TRUE;
2256
2257 prefs_apply(wlan_prefs);
2258 }
2259
2260 /*
2261  * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2262  */
2263 gboolean
2264 set_airpcap_decryption(gboolean on_off)
2265 {
2266         /* We need to directly access the .dll functions here... */
2267         gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2268         PAirpcapHandle ad,ad_driver;
2269
2270         gboolean success = TRUE;
2271
2272         gint n = 0;
2273         gint i = 0;
2274         airpcap_if_info_t* curr_if = NULL;
2275         airpcap_if_info_t* fake_if_info = NULL;
2276
2277         fake_if_info = airpcap_driver_fake_if_info_new();
2278
2279         if(fake_if_info == NULL)
2280                 /* We apparently don't have any adapters installed.
2281                  * This isn't a failure, so return TRUE
2282                  */
2283                 return TRUE;
2284
2285         /* Set the driver decryption */
2286         ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2287         if(ad_driver)
2288                 {
2289                 if(on_off)
2290                         airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2291                 else
2292                         airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2293
2294                 airpcap_if_close(ad_driver);
2295                 }
2296
2297         airpcap_if_info_free(fake_if_info);
2298
2299         n = g_list_length(airpcap_if_list);
2300
2301         /* Set to FALSE the decryption for all the adapters */
2302         /* Apply this change to all the adapters !!! */
2303         for(i = 0; i < n; i++)
2304             {
2305             curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2306
2307             if( curr_if != NULL )
2308                 {
2309                 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list,curr_if->description), ebuf);
2310                         if(ad)
2311                     {
2312                     curr_if->DecryptionOn = (gboolean)AIRPCAP_DECRYPTION_OFF;
2313                         airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2314                         /* Save configuration for the curr_if */
2315                         if(!airpcap_if_store_cur_config_as_adapter_default(ad))
2316                                 {
2317                                 success = FALSE;
2318                                 }
2319                         airpcap_if_close(ad);
2320                     }
2321                 }
2322             }
2323
2324         return success;
2325 }
2326
2327
2328 /* DYNAMIC LIBRARY LOADER */
2329 /*
2330  *  Used to dynamically load the airpcap library in order link it only when
2331  *  it's present on the system
2332  */
2333 int load_airpcap(void)
2334 {
2335 BOOL base_functions = TRUE;
2336 BOOL new_functions = TRUE;
2337
2338  if((AirpcapLib =  LoadLibrary(TEXT("airpcap.dll"))) == NULL)
2339  {
2340   /* Report the error but go on */
2341   return AIRPCAP_DLL_NOT_FOUND;
2342  }
2343  else
2344  {
2345   if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2346   if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2347   if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2348   if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2349   if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2350   if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2351   if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2352   if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2353   if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2354   if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2355   if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2356   if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2357   if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2358   if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2359   if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2360   if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2361   if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2362   if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2363   if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2364   if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2365   if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2366   if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2367   if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2368   if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2369   if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2370   if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2371   if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2372   if((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2373
2374   /* TEST IF WE CAN FIND AIRPCAP NEW DRIVER FEATURES */
2375   if((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) new_functions = FALSE;
2376   if((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) new_functions = FALSE;
2377   if((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) new_functions = FALSE;
2378   if((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) new_functions = FALSE;
2379
2380   if(base_functions)
2381   {
2382           if(new_functions)
2383           {
2384           AirpcapLoaded = TRUE;
2385           return AIRPCAP_DLL_OK;
2386           }
2387           else
2388           {
2389           AirpcapLoaded = TRUE;
2390           return AIRPCAP_DLL_OLD;
2391           }
2392   }
2393   else
2394   {
2395           AirpcapLoaded = FALSE;
2396           return AIRPCAP_DLL_ERROR;
2397   }
2398  }
2399 }
2400
2401 /*
2402  * Append the version of AirPcap with which we were compiled to a GString.
2403  */
2404 void
2405 get_compiled_airpcap_version(GString *str)
2406 {
2407         g_string_append(str, "with AirPcap");
2408 }
2409
2410 /*
2411  * Append the version of AirPcap with which we we're running to a GString.
2412  */
2413 void
2414 get_runtime_airpcap_version(GString *str)
2415 {
2416         guint vmaj, vmin, vrev, build;
2417
2418         /* See if the DLL has been loaded successfully.  Bail if it hasn't */
2419         if (AirpcapLoaded == FALSE) {
2420                 g_string_append(str, "without AirPcap");
2421                 return;
2422         }
2423
2424         g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2425         g_string_sprintfa(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
2426                 vrev, build);
2427 }
2428
2429 #endif /* _WIN32 */