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