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