Spelling fixes.
[obnox/wireshark/wip.git] / gtk / airpcap_gui_utils.c
1 /* airpcap_gui_utils.c
2  *
3  * $Id$
4  *
5  * Giorgio Tino <giorgio.tino@cacetech.com>
6  * Copyright (c) CACE Technologies, LLC 2006
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2000 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_AIRPCAP
32
33 #include <gtk/gtk.h>
34 #include <glib.h>
35
36 #include <string.h>
37
38 #include <epan/filesystem.h>
39 #include <epan/strutil.h>
40 #include <epan/crypt/airpdcap_ws.h>
41
42 #include "gtk/main.h"
43 #include "dlg_utils.h"
44 #include "gui_utils.h"
45 #include "simple_dialog.h"
46 #include "dfilter_expr_dlg.h"
47 #include "compat_macros.h"
48 #include "gtkglobals.h"
49 #include "help_dlg.h"
50
51 #include <airpcap.h>
52 #include "airpcap_loader.h"
53 #include "airpcap_gui_utils.h"
54
55 #include "keys.h"
56
57 /*
58  * Used to retrieve a string containing a list of all the channels
59  * on which at least one adapter is capturing. This is true
60  * if the adapter passed as parameter is "Any" ... if not,
61  * this function returns the only channel number string.
62  */
63 gchar*
64 airpcap_get_all_channels_list(airpcap_if_info_t* if_info)
65 {
66     gchar *channels;
67     gchar *tmp;
68     guint n,i;
69     GList *current_item;
70     airpcap_if_info_t* current_adapter;
71
72     /* Allocate the string used to store the ASCII representation of the WEP key */
73     channels = (gchar*)g_malloc(sizeof(gchar)*128);
74     /* Make sure that the first char is '\0' in order to make g_strlcat() work */
75     channels[0]='\0';
76
77     if(airpcap_if_is_any(if_info))
78     {
79         n = g_list_length(airpcap_if_list);
80
81         for(i = 0; i < n; i++)
82         {
83             current_item = g_list_nth(airpcap_if_list,i);
84             current_adapter = (airpcap_if_info_t*)current_item->data;
85             if(current_adapter != if_info)
86             {
87                 tmp = g_strdup_printf("%d",current_adapter->channel);
88                 g_strlcat(channels,tmp,128);
89                 g_free(tmp);
90
91                 if(i<(n-1)) g_strlcat(channels,",",128);
92             }
93         }
94     }
95
96     return channels;
97 }
98
99 /*
100  * Set up the airpcap toolbar for the new capture interface
101  */
102 void
103 airpcap_set_toolbar_start_capture(airpcap_if_info_t* if_info)
104 {
105     GtkWidget *airpcap_toolbar_label;
106     GtkWidget *airpcap_toolbar_channel;
107     GtkWidget *airpcap_toolbar_channel_lb;
108     GtkWidget *airpcap_toolbar_button;
109     GtkWidget *airpcap_toolbar_fcs;
110     GtkWidget *airpcap_toolbar_fcs_lb;
111     GtkWidget *airpcap_toolbar_decryption;
112     GtkWidget *airpcap_toolbar_decryption_lb;
113     GtkWidget *airpcap_toolbar_keys_button;
114
115     gchar *if_label_text;
116
117     airpcap_toolbar_label    = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_INTERFACE_KEY);
118     airpcap_toolbar_channel  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_CHANNEL_KEY);
119     airpcap_toolbar_channel_lb  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
120     airpcap_toolbar_fcs  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
121     airpcap_toolbar_fcs_lb  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
122     airpcap_toolbar_button   = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_ADVANCED_KEY);
123     airpcap_toolbar_decryption = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_KEY);
124     airpcap_toolbar_decryption_lb = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_LABEL_KEY);
125     airpcap_toolbar_keys_button = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_KEY_MANAGEMENT_KEY);
126
127     /* The current interface is an airpcap interface */
128     if(if_info != NULL)
129     {
130         gtk_widget_set_sensitive(airpcap_tb,TRUE);
131         gtk_widget_set_sensitive(airpcap_toolbar_label,TRUE);
132         gtk_widget_set_sensitive(airpcap_toolbar_channel,TRUE);
133         gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,TRUE);
134         gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
135         gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
136         gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
137         gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
138         gtk_widget_set_sensitive(airpcap_toolbar_decryption,FALSE);
139         gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,FALSE);
140         gtk_widget_set_sensitive(airpcap_toolbar_keys_button,FALSE);
141         airpcap_update_channel_combo(GTK_WIDGET(airpcap_toolbar_channel),if_info);
142
143         /*decription check box*/
144         gtk_signal_handler_block_by_func (GTK_OBJECT(airpcap_toolbar_decryption),GTK_SIGNAL_FUNC(airpcap_toolbar_encryption_cb), airpcap_tb);
145         if(if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
146             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),TRUE);
147         else
148             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),FALSE);
149         gtk_signal_handler_unblock_by_func (GTK_OBJECT(airpcap_toolbar_decryption),GTK_SIGNAL_FUNC(airpcap_toolbar_encryption_cb), airpcap_tb);
150
151         if_label_text = g_strdup_printf("Current Wireless Interface: #%s", airpcap_get_if_string_number(if_info));
152         gtk_label_set_text(GTK_LABEL(airpcap_toolbar_label),if_label_text);
153         g_free(if_label_text);
154     }
155     else /* Current interface is NOT an AirPcap one... */
156     {
157         gtk_widget_set_sensitive(airpcap_tb,FALSE);
158         gtk_widget_set_sensitive(airpcap_toolbar_label,FALSE);
159         gtk_widget_set_sensitive(airpcap_toolbar_channel,FALSE);
160         gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,FALSE);
161         gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
162         gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
163         gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
164         gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
165         gtk_widget_set_sensitive(airpcap_toolbar_decryption,FALSE);
166         gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,FALSE);
167         gtk_widget_set_sensitive(airpcap_toolbar_keys_button,FALSE);
168         airpcap_set_toolbar_no_if(airpcap_tb);
169     }
170 }
171
172 /*
173  * Set up the airpcap toolbar for the new capture interface
174  */
175 void
176 airpcap_set_toolbar_stop_capture(airpcap_if_info_t* if_info)
177 {
178     GtkWidget *airpcap_toolbar_crc_filter_combo;
179     GtkWidget *airpcap_toolbar_label;
180     GtkWidget *airpcap_toolbar_channel;
181     GtkWidget *airpcap_toolbar_channel_lb;
182     GtkWidget *airpcap_toolbar_button;
183     GtkWidget *airpcap_toolbar_fcs;
184     GtkWidget *airpcap_toolbar_fcs_lb;
185     GtkWidget *airpcap_toolbar_decryption;
186     GtkWidget *airpcap_toolbar_decryption_lb;
187     GtkWidget *airpcap_toolbar_keys_button;
188
189     gchar *if_label_text;
190
191     airpcap_toolbar_crc_filter_combo = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
192     airpcap_toolbar_label    = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_INTERFACE_KEY);
193     airpcap_toolbar_channel  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_CHANNEL_KEY);
194     airpcap_toolbar_channel_lb  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
195     airpcap_toolbar_fcs  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
196     airpcap_toolbar_fcs_lb  = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
197     airpcap_toolbar_button   = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_ADVANCED_KEY);
198     airpcap_toolbar_decryption = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_KEY);
199     airpcap_toolbar_decryption_lb = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_LABEL_KEY);
200     airpcap_toolbar_keys_button = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_KEY_MANAGEMENT_KEY);
201
202     /* The current interface is an airpcap interface */
203     if(if_info != NULL)
204     {
205         gtk_widget_set_sensitive(airpcap_tb,TRUE);
206         gtk_widget_set_sensitive(airpcap_toolbar_label,TRUE);
207         gtk_widget_set_sensitive(airpcap_toolbar_channel,TRUE);
208         gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,TRUE);
209         gtk_widget_set_sensitive(airpcap_toolbar_fcs,TRUE);
210         gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,TRUE);
211         gtk_widget_set_sensitive(airpcap_toolbar_button,TRUE);
212         gtk_widget_set_sensitive(airpcap_toolbar_crc_filter_combo,TRUE);
213         gtk_widget_set_sensitive(airpcap_toolbar_decryption,TRUE);
214         gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,TRUE);
215         gtk_widget_set_sensitive(airpcap_toolbar_keys_button,TRUE);
216         airpcap_validation_type_combo_set_by_type(GTK_WIDGET(airpcap_toolbar_crc_filter_combo),if_info->CrcValidationOn);
217         airpcap_update_channel_combo(GTK_WIDGET(airpcap_toolbar_channel),if_info);
218
219         /*decription check box*/
220         gtk_signal_handler_block_by_func (GTK_OBJECT(airpcap_toolbar_decryption),GTK_SIGNAL_FUNC(airpcap_toolbar_encryption_cb), airpcap_tb);
221         if(if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
222             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),TRUE);
223         else
224             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),FALSE);
225         gtk_signal_handler_unblock_by_func (GTK_OBJECT(airpcap_toolbar_decryption),GTK_SIGNAL_FUNC(airpcap_toolbar_encryption_cb), airpcap_tb);
226
227
228         if_label_text = g_strdup_printf("Current Wireless Interface: #%s", airpcap_get_if_string_number(if_info));
229         gtk_label_set_text(GTK_LABEL(airpcap_toolbar_label),if_label_text);
230         g_free(if_label_text);
231     }
232     else
233     {
234         gtk_widget_set_sensitive(airpcap_tb,TRUE);
235         gtk_widget_set_sensitive(airpcap_toolbar_label,FALSE);
236         gtk_widget_set_sensitive(airpcap_toolbar_channel,FALSE);
237         gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,FALSE);
238         gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
239         gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
240         gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
241         gtk_widget_set_sensitive(airpcap_toolbar_crc_filter_combo,FALSE);
242         gtk_widget_set_sensitive(airpcap_toolbar_decryption,TRUE);
243         gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,TRUE);
244         gtk_widget_set_sensitive(airpcap_toolbar_keys_button,TRUE);
245         airpcap_set_toolbar_no_if(airpcap_tb);
246     }
247 }
248
249 /*
250  * Add a key (string) to the given list
251  */
252 void
253 airpcap_add_key_to_list(GtkWidget *keylist, gchar* type, gchar* key, gchar* ssid)
254 {
255     gchar*       new_row[3];
256
257     new_row[0] = g_strdup(type);
258     new_row[1] = g_strdup(key);
259     new_row[2] = g_strdup(ssid);
260
261     gtk_clist_append(GTK_CLIST(keylist),new_row);
262
263     g_free(new_row[0]);
264     g_free(new_row[1]);
265     g_free(new_row[2]);
266 }
267
268 /*
269  * Modify a key given a list and a row
270  */
271 void
272 airpcap_modify_key_in_list(GtkWidget *keylist, gint row, gchar* type, gchar* key, gchar* ssid)
273 {
274     gchar*       new_row[3];
275
276     new_row[0] = g_strdup(type);
277     new_row[1] = g_strdup(key);
278     new_row[2] = g_strdup(ssid);
279
280     gtk_clist_set_text(GTK_CLIST(keylist),row,0,new_row[0]);
281     gtk_clist_set_text(GTK_CLIST(keylist),row,1,new_row[1]);
282     gtk_clist_set_text(GTK_CLIST(keylist),row,2,new_row[2]);
283
284     g_free(new_row[0]);
285     g_free(new_row[1]);
286     g_free(new_row[2]);
287 }
288
289 /*
290  * Fill the list with the keys. BEWARE! At this point, Wireshark and Drivers
291  * keys should be EQUALS! But is better to load keys from Wireshark, because
292  * the driver is not always present, and maybe that cannot support some keys
293  * (i.e. the WPA problem)
294  */
295 void
296 airpcap_fill_key_list(GtkWidget *keylist)
297 {
298     gchar*               s = NULL;
299     gchar*               s2 = NULL;
300     unsigned int i,n;
301     gchar*       new_row[3];
302     airpcap_if_info_t* fake_if_info;
303     GList*               wireshark_key_list=NULL;
304     decryption_key_t* curr_key = NULL;
305
306     n = 0;
307
308     fake_if_info = airpcap_driver_fake_if_info_new();
309
310     /* We can retrieve the driver's key list (i.e. we have the right .dll)*/
311     wireshark_key_list = get_wireshark_keys();
312     n = g_list_length(wireshark_key_list);
313
314     for(i = 0; i < n; i++)
315     {
316         curr_key = (decryption_key_t*)g_list_nth_data(wireshark_key_list,i);
317
318         if(curr_key->type == AIRPDCAP_KEY_TYPE_WEP)
319         {
320             s = g_strdup(curr_key->key->str);
321
322             new_row[0] = g_strdup(AIRPCAP_WEP_KEY_STRING);
323             new_row[1] = g_strdup(s);
324             new_row[2] = g_strdup("");
325
326             gtk_clist_append(GTK_CLIST(keylist),new_row);
327
328             g_free(new_row[0]);
329             g_free(new_row[1]);
330             g_free(new_row[2]);
331
332             g_free(s);
333         }
334         else if(curr_key->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
335         {
336             s = g_strdup(curr_key->key->str);
337             if(curr_key->ssid != NULL)
338                 s2= g_strdup(format_uri(curr_key->ssid, ":"));
339             else
340                 s2 = NULL;
341
342             new_row[0] = g_strdup(AIRPCAP_WPA_PWD_KEY_STRING);
343             new_row[1] = g_strdup(s);
344
345             if(curr_key->ssid != NULL)
346                 new_row[2] = g_strdup(s2);
347             else
348                 new_row[2] = g_strdup("");
349
350             gtk_clist_append(GTK_CLIST(keylist),new_row);
351
352             g_free(new_row[0]);
353             g_free(new_row[1]);
354             g_free(new_row[2]);
355
356             g_free(s);
357             if(s2 != NULL)
358                 g_free(s2);
359         }
360         else if(curr_key->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
361         {
362             s = g_strdup(curr_key->key->str);
363
364             new_row[0] = g_strdup(AIRPCAP_WPA_BIN_KEY_STRING);
365             new_row[1] = g_strdup(s);
366             new_row[2] = g_strdup("");
367
368             gtk_clist_append(GTK_CLIST(keylist),new_row);
369
370             g_free(new_row[0]);
371             g_free(new_row[1]);
372             g_free(new_row[2]);
373
374             g_free(s);
375         }
376     }
377
378     airpcap_if_info_free(fake_if_info);
379     return;
380 }
381
382 /*
383  * Function used to retrieve the AirpcapValidationType given the string name.
384  */
385 AirpcapValidationType
386 airpcap_get_validation_type(const gchar* name)
387 {
388     if(!(g_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_ALL,name)))
389     {
390         return AIRPCAP_VT_ACCEPT_EVERYTHING;
391     }
392     else if(!(g_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_CORRECT,name)))
393     {
394         return AIRPCAP_VT_ACCEPT_CORRECT_FRAMES;
395     }
396     else if(!(g_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_CORRUPT,name)))
397     {
398         return AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES;
399     }
400     else
401     {
402         return AIRPCAP_VT_UNKNOWN;
403     }
404 }
405
406 /*
407  * Function used to retrieve the string name given an AirpcapValidationType,
408  * or NULL in case of error
409  */
410 gchar*
411 airpcap_get_validation_name(AirpcapValidationType vt)
412 {
413     if(vt == AIRPCAP_VT_ACCEPT_EVERYTHING)
414     {
415         return AIRPCAP_VALIDATION_TYPE_NAME_ALL;
416     }
417     else if(vt == AIRPCAP_VT_ACCEPT_CORRECT_FRAMES)
418     {
419         return AIRPCAP_VALIDATION_TYPE_NAME_CORRECT;
420     }
421     else if(vt == AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES)
422     {
423         return AIRPCAP_VALIDATION_TYPE_NAME_CORRUPT;
424     }
425     else if(vt == AIRPCAP_VT_UNKNOWN)
426     {
427         return AIRPCAP_VALIDATION_TYPE_NAME_UNKNOWN;
428     }
429     return NULL;
430 }
431
432 /*
433  * Returns the AirpcapLinkType corresponding to the given string name.
434  */
435 AirpcapLinkType
436 airpcap_get_link_type(const gchar* name)
437 {
438     if(!(g_strcasecmp(AIRPCAP_LINK_TYPE_NAME_802_11_ONLY,name)))
439     {
440         return AIRPCAP_LT_802_11;
441     }
442     else if(!(g_strcasecmp(AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO,name)))
443     {
444         return AIRPCAP_LT_802_11_PLUS_RADIO;
445     }
446     else
447     {
448         return AIRPCAP_LT_UNKNOWN;
449     }
450 }
451
452 /*
453  * Returns the string name corresponding to the given AirpcapLinkType, or
454  * NULL in case of error.
455  */
456 gchar*
457 airpcap_get_link_name(AirpcapLinkType lt)
458 {
459     if(lt == AIRPCAP_LT_802_11)
460     {
461         return AIRPCAP_LINK_TYPE_NAME_802_11_ONLY;
462     }
463     else if(lt == AIRPCAP_LT_802_11_PLUS_RADIO)
464     {
465         return AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO;
466     }
467     else if(lt == AIRPCAP_LT_UNKNOWN)
468     {
469         return AIRPCAP_LINK_TYPE_NAME_UNKNOWN;
470     }
471     return NULL;
472 }
473
474 /*
475  * Sets the entry of the link type combo using the AirpcapLinkType.
476  */
477 void
478 airpcap_link_type_combo_set_by_type(GtkWidget* c, AirpcapLinkType type)
479 {
480     gchar* s;
481
482     s = airpcap_get_link_name(type);
483     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(c)->entry),s);
484 }
485
486 /*
487  * Retrieves the name in link type the combo entry.
488  */
489 AirpcapLinkType
490 airpcap_link_type_combo_get_type(GtkWidget* c)
491 {
492     const gchar* s;
493
494     s = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(c)->entry));
495
496     return airpcap_get_link_type(s);
497 }
498
499 /*
500  * Sets the entry of the validation combo using the AirpcapValidationType.
501  */
502 void
503 airpcap_validation_type_combo_set_by_type(GtkWidget* c, AirpcapValidationType type)
504 {
505     const gchar* s;
506
507     s = airpcap_get_validation_name(type);
508     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(c)->entry),s);
509 }
510
511 /*
512  * Retrieves the name in the validation combo entry.
513  */
514 AirpcapValidationType
515 airpcap_validation_type_combo_get_type(GtkWidget* c)
516 {
517     const gchar* s;
518
519     s = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(c)->entry));
520
521     return airpcap_get_validation_type(s);
522 }
523
524 /*
525  * Retrieve the UINT corresponding to the given string (channel only, handle with care!)
526  */
527 UINT
528 airpcap_get_channel_number(const gchar* s)
529 {
530     int ch_num;
531
532     sscanf(s,"%d",&ch_num);
533
534     /* XXX - check for ch_num btween 1-14, and return -1 otherwise??? */
535
536     return ch_num;
537 }
538
539 /*
540  * Returns the string corresponding to the given UINT (1-14, for channel only)
541  */
542 gchar*
543 airpcap_get_channel_name(UINT n)
544 {
545     return g_strdup_printf("%d",n);
546 }
547
548 /*
549  * Set the combo box entry string given an UINT channel number
550  */
551 void
552 airpcap_channel_combo_set_by_number(GtkWidget* w,UINT channel)
553 {
554     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(w)->entry),airpcap_get_channel_name(channel));
555 }
556
557 /*
558  * Returns '1' if this is the "Any" adapter, '0' otherwise
559  */
560 int
561 airpcap_if_is_any(airpcap_if_info_t* if_info)
562 {
563     if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)==0)
564         return 1;
565     else
566         return 0;
567 }
568
569 /*
570  * Update channel combo box. If the airpcap interface is "Any", the combo box will be disabled.
571  */
572 void
573 airpcap_update_channel_combo(GtkWidget* w, airpcap_if_info_t* if_info)
574 {
575 gchar* channels_list;
576
577     if(airpcap_if_is_any(if_info))
578     {
579         channels_list = airpcap_get_all_channels_list(if_info);
580         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(w)->entry),channels_list);
581         g_free(channels_list);
582         gtk_widget_set_sensitive(GTK_WIDGET(w),FALSE);
583     }
584     else
585     {
586         airpcap_channel_combo_set_by_number(w,if_info->channel);
587         gtk_widget_set_sensitive(GTK_WIDGET(w),TRUE);
588     }
589 }
590
591 /*
592  * Takes the keys from the GtkList widget, and add them to the interface list
593  */
594 void
595 airpcap_add_keys_from_list(GtkWidget *key_ls, airpcap_if_info_t *if_info)
596 {
597     GString             *new_key;
598
599     gchar               *text_entered = NULL;
600
601     /* airpcap stuff */
602     UINT i, j;
603     gchar s[3];
604     PAirpcapKeysCollection KeysCollection;
605     ULONG KeysCollectionSize;
606     UCHAR KeyByte;
607
608     UINT keys_in_list = 0;
609
610     gchar *row_type,
611           *row_key,
612           *row_ssid;
613
614     keys_in_list = GTK_CLIST(key_ls)->rows;
615
616     /*
617      * Save the encryption keys, if we have any of them
618      */
619     KeysCollectionSize = 0;
620
621     /*
622      * Calculate the size of the keys collection
623      */
624     KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
625
626     /*
627      * Allocate the collection
628      */
629     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
630     if(!KeysCollection)
631     {
632         /* Simple dialog ERROR */
633         simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,"%s","Failed mamory allocation for KeysCollection!");
634         return;
635     }
636
637     /*
638      * Populate the key collection
639      */
640     KeysCollection->nKeys = keys_in_list;
641
642     for(i = 0; i < keys_in_list; i++)
643     {
644         /* Retrieve the row infos */
645         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&row_type);
646         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&row_key);
647         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&row_ssid);
648
649         if(g_strcasecmp(row_type,AIRPCAP_WEP_KEY_STRING) == 0)
650         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
651         else if(g_strcasecmp(row_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
652         KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_TKIP;
653         else if(g_strcasecmp(row_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
654         KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_CCMP;
655
656         /* Retrieve the Item corresponding to the i-th key */
657         new_key = g_string_new(row_key);
658
659         KeysCollection->Keys[i].KeyLen = new_key->len / 2;
660         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
661
662         for(j = 0 ; j < new_key->len; j += 2)
663         {
664             s[0] = new_key->str[j];
665             s[1] = new_key->str[j+1];
666             s[2] = '\0';
667             KeyByte = (UCHAR)strtol(s, NULL, 16);
668             KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
669         }
670     }
671
672     /*
673      * Free the old adapter key collection!
674      */
675     if(airpcap_if_selected->keysCollection != NULL)
676             g_free(airpcap_if_selected->keysCollection);
677
678     /*
679      * Set this collection ad the new one
680      */
681     airpcap_if_selected->keysCollection = KeysCollection;
682     airpcap_if_selected->keysCollectionSize = KeysCollectionSize;
683
684     return;
685 }
686
687 /*
688  * Takes the keys from the GtkList widget, and add them to the interface list
689  */
690 void
691 airpcap_add_keys_to_driver_from_list(GtkWidget *key_ls,airpcap_if_info_t *fake_if_info)
692 {
693     GString             *new_key;
694
695     gchar               *text_entered = NULL;
696
697     /* airpcap stuff */
698     UINT i, j;
699     gchar s[3];
700     PAirpcapKeysCollection KeysCollection;
701     ULONG KeysCollectionSize;
702     UCHAR KeyByte;
703
704     UINT keys_in_list = 0;
705
706     gchar *row_type,
707           *row_key,
708           *row_ssid;
709
710     if(fake_if_info == NULL)
711             return;
712
713     keys_in_list = GTK_CLIST(key_ls)->rows;
714
715     /*
716      * Save the encryption keys, if we have any of them
717      */
718     KeysCollectionSize = 0;
719
720     /*
721      * Calculate the size of the keys collection
722      */
723     KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
724
725     /*
726      * Allocate the collection
727      */
728     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
729     if(!KeysCollection)
730     {
731         /* Simple dialog ERROR */
732         simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,"%s","Failed mamory allocation for KeysCollection!");
733         return;
734     }
735
736     /*
737      * Populate the key collection
738      */
739     KeysCollection->nKeys = keys_in_list;
740
741     for(i = 0; i < keys_in_list; i++)
742     {
743         /* Retrieve the row infos */
744         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&row_type);
745         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&row_key);
746         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&row_ssid);
747
748         if(g_strcasecmp(row_type,AIRPCAP_WEP_KEY_STRING) == 0)
749         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
750         else if(g_strcasecmp(row_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
751         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WPA_PWD;
752         else if(g_strcasecmp(row_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
753         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WPA_PMK;
754
755         /* Retrieve the Item corresponding to the i-th key */
756         new_key = g_string_new(row_key);
757
758         KeysCollection->Keys[i].KeyLen = new_key->len / 2;
759         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
760
761         /* Key must be saved in adifferent way, depending on its type... */
762         if(KeysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
763         {
764             for(j = 0 ; j < new_key->len; j += 2)
765             {
766                     s[0] = new_key->str[j];
767                     s[1] = new_key->str[j+1];
768                     s[2] = '\0';
769                     KeyByte = (UCHAR)strtol(s, NULL, 16);
770                     KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
771             }
772         }
773         /* XXX - Save the keys that are not WEP!!! */
774     }
775
776     /*
777      * Free the old adapter key collection!
778      */
779     if(fake_if_info->keysCollection != NULL)
780             g_free(fake_if_info->keysCollection);
781
782     /*
783      * Set this collection ad the new one
784      */
785     fake_if_info->keysCollection = KeysCollection;
786     fake_if_info->keysCollectionSize = KeysCollectionSize;
787     return;
788 }
789
790 /*
791  * This function will take the current keys (widget list), specified for the
792  * current adapter, and save them as default for ALL the others.
793  */
794 void
795 airpcap_read_and_save_decryption_keys_from_clist(GtkWidget* key_ls, airpcap_if_info_t* info_if, GList* if_list)
796 {
797     gint if_n = 0;
798     gint i = 0;
799     gint r = 0;
800     gint n = 0;
801     airpcap_if_info_t* curr_if = NULL;
802     airpcap_if_info_t* fake_info_if = NULL;
803     GList* key_list=NULL;
804
805     char* tmp_type = NULL;
806     char* tmp_key = NULL;
807     char* tmp_ssid = NULL;
808
809     decryption_key_t* tmp_dk=NULL;
810
811     /*
812      * Save the keys for Wireshark...
813      */
814
815     /* Create a list of keys from the list widget... */
816     n = GTK_CLIST(key_ls)->rows;
817
818     for(i = 0; i < n; i++)
819     {
820         /* XXX - Create a decryption_key_t struct, and pass a list of those structs!!! */
821         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&tmp_type);
822         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&tmp_key);
823         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&tmp_ssid);
824
825         if(g_strcasecmp(tmp_type,AIRPCAP_WEP_KEY_STRING) == 0)
826         {
827             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
828             tmp_dk->key = g_string_new(tmp_key);
829             tmp_dk->ssid = NULL;
830             tmp_dk->type = AIRPDCAP_KEY_TYPE_WEP;
831             tmp_dk->bits = tmp_dk->key->len * 4;
832             key_list = g_list_append(key_list,tmp_dk);
833         }
834         else if(g_strcasecmp(tmp_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
835         {
836             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
837             tmp_dk->key = g_string_new(tmp_key);
838             tmp_dk->ssid = g_byte_array_new();
839             uri_str_to_bytes(tmp_ssid, tmp_dk->ssid);
840             tmp_dk->type = AIRPDCAP_KEY_TYPE_WPA_PWD;
841             tmp_dk->bits = 256;
842             key_list = g_list_append(key_list,tmp_dk);
843         }
844         else if(g_strcasecmp(tmp_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
845         {
846             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
847             tmp_dk->key = g_string_new(tmp_key);
848             tmp_dk->ssid = NULL; /* No SSID in this case */
849             tmp_dk->type = AIRPDCAP_KEY_TYPE_WPA_PMK;
850             tmp_dk->bits = 256;
851             key_list = g_list_append(key_list,tmp_dk);
852         }
853     }
854
855     r = save_wlan_wireshark_wep_keys(key_list);
856     /* The key_list has been freed!!! */
857
858     /*
859      * Save the key list for driver.
860      */
861     if( (if_list == NULL) || (info_if == NULL) ) return;
862
863     fake_info_if = airpcap_driver_fake_if_info_new();
864
865     airpcap_add_keys_to_driver_from_list(key_ls,fake_info_if);
866     airpcap_save_driver_if_configuration(fake_info_if);
867     airpcap_if_info_free(fake_info_if);
868
869     if_n = g_list_length(if_list);
870
871     /* For all the adapters in the list, empty the key list */
872     for(i = 0; i < if_n; i++)
873     {
874         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
875
876         if(curr_if != NULL)
877         {
878             /* XXX - Set an empty collection */
879             airpcap_if_clear_decryption_settings(curr_if);
880
881             /* Save to registry */
882             airpcap_save_selected_if_configuration(curr_if);
883         }
884     }
885 }
886
887 /*
888  * This function will load from the preferences file ALL the
889  * keys (WEP, WPA and WPA_BIN) and will set them as default for
890  * each adapter. To do this, it will save the keys in the registry...
891  * A check will be performed, to make sure that keys found in
892  * registry and keys found in Wireshark preferences are the same. If not,
893  * the user will be asked to choose if use all keys (merge them),
894  * or use Wireshark preferences ones. In the last case, registry keys will
895  * be overwritten for all the connected AirPcap adapters.
896  * In the first case, adapters will use their own keys, but those
897  * keys will not be accessible via Wireshark...
898  */
899 gboolean
900 airpcap_check_decryption_keys(GList* if_list)
901 {
902     gint if_n = 0;
903     gint i = 0;
904     gint n_adapters_keys = 0;
905     gint n_driver_keys = 0;
906     gint n_wireshark_keys = 0;
907     airpcap_if_info_t* curr_if = NULL;
908
909     GList* wireshark_key_list;
910     GList* driver_key_list;
911     GList* curr_adapter_key_list;
912
913     gboolean equals = TRUE;
914     gboolean adapters_keys_equals=TRUE;
915
916     /*
917      * If no AirPcap interface is found, return TRUE, so Wireshark
918      * will use HIS OWN keys.
919      */
920     if(if_list == NULL)
921         return TRUE;
922
923     if_n = g_list_length(if_list);
924
925     /* Get Wireshark preferences keys */
926     wireshark_key_list = get_wireshark_keys();
927     n_wireshark_keys = g_list_length(wireshark_key_list);
928
929     /* Retrieve AirPcap driver's keys */
930     driver_key_list = get_airpcap_driver_keys();
931     n_driver_keys = g_list_length(driver_key_list);
932
933     equals &= key_lists_are_equal(wireshark_key_list,driver_key_list);
934
935     for(i = 0; i < if_n; i++)
936     {
937         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
938         curr_adapter_key_list = get_airpcap_device_keys(curr_if);
939         n_adapters_keys += g_list_length(curr_adapter_key_list);
940         adapters_keys_equals &= key_lists_are_equal(wireshark_key_list,curr_adapter_key_list);
941     }
942
943     if(n_adapters_keys != 0) /* If for some reason at least one specific key has been found */
944         equals &= adapters_keys_equals; /* */
945
946     if(n_driver_keys == 0) /* No keys set in any of the AirPcap adapters... */
947         return TRUE; /* Use Wireshark keys and set them ad default for airpcap devices */
948
949     return equals;
950 }
951
952 /*
953  * This function will load from the preferences file ALL the
954  * keys (WEP, WPA_PWD and WPA_BIN) and will set them as default for
955  * each adapter. To do this, it will save the keys in the registry...
956  * A check will be performed, to make sure that keys found in
957  * registry and keys found in Wireshark preferences are the same. If not,
958  * the user will be asked to choose if use all keys (merge them),
959  * or use Wireshark preferences ones. In the last case, registry keys will
960  * be overwritten for all the connected AirPcap adapters.
961  * In the first case, adapters will use their own keys, but those
962  * keys will not be accessible via Wireshark...
963  */
964 void
965 airpcap_load_decryption_keys(GList* if_list)
966 {
967     gint if_n = 0;
968     gint i = 0;
969     airpcap_if_info_t* curr_if = NULL;
970
971     if(if_list == NULL) return;
972
973     if_n = g_list_length(if_list);
974
975     for(i = 0; i < if_n; i++)
976     {
977         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
978         load_wlan_driver_wep_keys();
979     }
980 }
981
982 /*
983  * This function will set the gibven GList of decryption_key_t structures
984  * as the defoult for both Wireshark and the AirPcap adapters...
985  */
986 void
987 airpcap_save_decryption_keys(GList* key_list, GList* adapters_list)
988 {
989     gint if_n = 0;
990     gint key_n = 0;
991     gint i = 0;
992     airpcap_if_info_t* curr_if = NULL;
993     GList* empty_key_list = NULL;
994
995     if( (key_list == NULL) || (adapters_list == NULL)) return;
996
997     if_n = g_list_length(adapters_list);
998     key_n = g_list_length(key_list);
999
1000     /* Set the driver's global list of keys. */
1001     write_wlan_driver_wep_keys_to_registry(key_list);
1002
1003     /* Empty the key list for each interface */
1004     for(i = 0; i < if_n; i++)
1005     {
1006         curr_if = (airpcap_if_info_t*)g_list_nth_data(adapters_list,i);
1007         write_wlan_wep_keys_to_registry(curr_if,empty_key_list);
1008     }
1009
1010     /*
1011      * This will set the keys of the current adapter as Wireshark default...
1012      * Now all the adapters have the same keys, so curr_if is ok as any other...
1013      */
1014     save_wlan_wireshark_wep_keys(key_list);
1015 }
1016
1017 /*
1018  * This function is used to enable/disable the toolbar widgets
1019  * depending on the type of interface selected... Not the whole
1020  * toolbar must be grayed/enabled ... Only some widgets...
1021  */
1022 void
1023 airpcap_enable_toolbar_widgets(GtkWidget* w, gboolean en)
1024 {
1025     GtkWidget   *toolbar_tb,
1026                 *if_description_lb,
1027                 *channel_cb,
1028                 *channel_lb,
1029                 *fcs_cb,
1030                 *fcs_lb,
1031                 *advanced_bt;
1032
1033     if(w == NULL)
1034         return;
1035
1036     toolbar_tb = w;
1037
1038     if_description_lb   = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_INTERFACE_KEY);
1039     channel_lb                  = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
1040     channel_cb                  = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_CHANNEL_KEY);
1041     fcs_lb                              = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
1042     fcs_cb                              = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
1043     advanced_bt                 = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_ADVANCED_KEY);
1044
1045
1046     if(if_description_lb != NULL)       gtk_widget_set_sensitive(if_description_lb,en);
1047     if(channel_lb != NULL)                      gtk_widget_set_sensitive(channel_lb,en);
1048     if(channel_cb != NULL)                      gtk_widget_set_sensitive(channel_cb,en);
1049     if(fcs_lb != NULL)                          gtk_widget_set_sensitive(fcs_lb,en);
1050     if(fcs_cb != NULL)                          gtk_widget_set_sensitive(fcs_cb,en);
1051     if(advanced_bt != NULL)                     gtk_widget_set_sensitive(advanced_bt,en);
1052
1053     return;
1054 }
1055
1056 /*
1057  * This function sets up the correct airpcap toolbar that must
1058  * be displayed when no airpcap if is found on the system...
1059  */
1060 void
1061 airpcap_set_toolbar_no_if(GtkWidget* w)
1062 {
1063     GtkWidget   *toolbar_tb,
1064                 *if_description_lb,
1065                 *channel_cb,
1066                 *channel_lb,
1067                 *fcs_cb,
1068                 *fcs_lb,
1069                 *advanced_bt;
1070
1071     if(w == NULL)
1072         return;
1073
1074     toolbar_tb = w;
1075
1076     if_description_lb   = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_INTERFACE_KEY);
1077     channel_lb                  = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
1078     channel_cb                  = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_CHANNEL_KEY);
1079     fcs_lb                              = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
1080     fcs_cb                              = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
1081     advanced_bt                 = OBJECT_GET_DATA(toolbar_tb,AIRPCAP_TOOLBAR_ADVANCED_KEY);
1082
1083     if(fcs_cb != NULL)                          gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(fcs_cb)->entry),"");
1084     if(channel_cb != NULL)                      gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(channel_cb)->entry),"");
1085     if(if_description_lb != NULL)       gtk_label_set_text(GTK_LABEL(if_description_lb),"Current Wireless Interface: None");
1086
1087     /*if(if_description_lb != NULL)     gtk_widget_set_sensitive(if_description_lb,FALSE);
1088     if(channel_lb != NULL)                      gtk_widget_set_sensitive(channel_lb,FALSE);
1089     if(channel_cb != NULL)                      gtk_widget_set_sensitive(channel_cb,FALSE);
1090     if(fcs_lb != NULL)                          gtk_widget_set_sensitive(fcs_lb,FALSE);
1091     if(fcs_cb != NULL)                          gtk_widget_set_sensitive(fcs_cb,FALSE);
1092     if(advanced_bt != NULL)                     gtk_widget_set_sensitive(advanced_bt,FALSE);*/
1093 }
1094
1095 #endif /* HAVE_AIRPCAP */