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