diff options
-rw-r--r-- | gtk/Makefile.common | 1 | ||||
-rw-r--r-- | gtk/capture_dlg.c | 33 | ||||
-rw-r--r-- | gtk/cfilter_combo_utils.c | 107 | ||||
-rw-r--r-- | gtk/cfilter_combo_utils.h | 36 | ||||
-rw-r--r-- | gtk/recent.c | 10 |
5 files changed, 177 insertions, 10 deletions
diff --git a/gtk/Makefile.common b/gtk/Makefile.common index 6bc2a1d8cc..53b2215873 100644 --- a/gtk/Makefile.common +++ b/gtk/Makefile.common @@ -34,6 +34,7 @@ ETHEREAL_GTK_SRC = \ capture_if_dlg.c \ capture_info_dlg.c \ capture_prefs.c \ + cfilter_combo_utils.c \ color_dlg.c \ color_utils.c \ colors.c \ diff --git a/gtk/capture_dlg.c b/gtk/capture_dlg.c index 3f5150a0f9..94a4d4dc53 100644 --- a/gtk/capture_dlg.c +++ b/gtk/capture_dlg.c @@ -51,6 +51,7 @@ #include "file_dlg.h" #include "help_dlg.h" #include "gtkglobals.h" +#include "cfilter_combo_utils.h" #ifdef _WIN32 #include "capture-wpcap.h" @@ -488,7 +489,7 @@ capture_prep(void) *linktype_hb, *linktype_lb, *linktype_om, *snap_hb, *snap_cb, *snap_sb, *snap_lb, *promisc_cb, - *filter_hb, *filter_bt, *filter_te, + *filter_hb, *filter_bt, *filter_te, *filter_cm, *file_fr, *file_vb, *file_hb, *file_bt, *file_lb, *file_te, @@ -516,7 +517,7 @@ capture_prep(void) GtkTooltips *tooltips; GtkAdjustment *snap_adj, *ringbuffer_nbf_adj, *stop_packets_adj, *stop_filesize_adj, *stop_duration_adj, *stop_files_adj, *ring_filesize_adj, *file_duration_adj; - GList *if_list, *combo_list; + GList *if_list, *combo_list, *filter_list; int err; int row; char err_str[PCAP_ERRBUF_SIZE]; @@ -725,15 +726,24 @@ capture_prep(void) NULL); gtk_box_pack_start(GTK_BOX(filter_hb), filter_bt, FALSE, FALSE, 3); - filter_te = gtk_entry_new(); - if (cfile.cfilter) gtk_entry_set_text(GTK_ENTRY(filter_te), cfile.cfilter); - OBJECT_SET_DATA(filter_bt, E_FILT_TE_PTR_KEY, filter_te); + /* Create the capture filter combo */ + filter_cm = gtk_combo_new(); + + filter_list = OBJECT_GET_DATA(top_level, E_CFILTER_FL_KEY); + gtk_combo_disable_activate(GTK_COMBO(filter_cm)); + gtk_combo_set_case_sensitive(GTK_COMBO(filter_cm), TRUE); + OBJECT_SET_DATA(top_level, E_CFILTER_FL_KEY, filter_list); + OBJECT_SET_DATA(top_level, E_CFILTER_CM_KEY, filter_cm); + filter_te = GTK_COMBO(filter_cm)->entry; + + gtk_combo_set_popdown_strings(GTK_COMBO(filter_cm), filter_list); + gtk_entry_set_text(GTK_ENTRY(filter_te), ""); /* Default capture filter is empty */ gtk_tooltips_set_tip(tooltips, filter_te, "Enter a capture filter to reduce the amount of packets to be captured. " "See \"Capture Filters\" in the online help for further information how to use it.", NULL); - gtk_box_pack_start(GTK_BOX(filter_hb), filter_te, TRUE, TRUE, 3); - + WIDGET_SET_SIZE(filter_cm, 400, -1); + gtk_box_pack_start(GTK_BOX(filter_hb), filter_cm, FALSE, FALSE, 3); main_hb = gtk_hbox_new(FALSE, 5); gtk_container_border_width(GTK_CONTAINER(main_hb), 0); gtk_container_add(GTK_CONTAINER(main_vb), main_hb); @@ -1208,7 +1218,7 @@ capture_prep_file_cb(GtkWidget *file_bt, GtkWidget *file_te) static void capture_prep_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w) { - GtkWidget *if_cb, *snap_cb, *snap_sb, *promisc_cb, *filter_te, + GtkWidget *if_cb, *snap_cb, *snap_sb, *promisc_cb, *filter_te, *filter_cm, *file_te, *multi_files_on_cb, *ringbuffer_nbf_sb, *ringbuffer_nbf_cb, *linktype_om, *sync_cb, *auto_scroll_cb, *hide_info_cb, *stop_packets_cb, *stop_packets_sb, @@ -1239,7 +1249,8 @@ capture_prep_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w) { buffer_size_sb = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_BUFFER_SIZE_SB_KEY); #endif promisc_cb = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_PROMISC_KEY); - filter_te = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_FILT_KEY); + filter_cm = OBJECT_GET_DATA(top_level, E_CFILTER_CM_KEY); + filter_te = GTK_COMBO(filter_cm)->entry; file_te = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_FILE_TE_KEY); multi_files_on_cb = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_MULTI_FILES_ON_CB_KEY); ringbuffer_nbf_cb = (GtkWidget *) OBJECT_GET_DATA(parent_w, E_CAP_RING_NBF_CB_KEY); @@ -1282,6 +1293,10 @@ capture_prep_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w) { cfile.iface = g_strdup(if_name); g_free(entry_text); + /* Add this capture filter to the recent capture filter list if it passes a syntax check */ + if(check_capture_filter_syntax(cfile.iface, (gchar *) gtk_entry_get_text(GTK_ENTRY(filter_te)))) + cfilter_combo_add_recent((gchar *) gtk_entry_get_text(GTK_ENTRY(filter_te))); + capture_opts.linktype = GPOINTER_TO_INT(OBJECT_GET_DATA(linktype_om, E_CAP_OM_LT_VALUE_KEY)); diff --git a/gtk/cfilter_combo_utils.c b/gtk/cfilter_combo_utils.c new file mode 100644 index 0000000000..fe10194ad2 --- /dev/null +++ b/gtk/cfilter_combo_utils.c @@ -0,0 +1,107 @@ +/* cfilter_combo_utils.c
+ * Capture filter combo box routines
+ *
+ * $Id: cfilter_combo_utils.c 12115 2004-09-27 22:55:15Z guy $
+ *
+ * Ethereal - Network traffic analyzer
+ * By Gerald Combs <gerald@ethereal.com>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <gtk/gtk.h>
+#include "compat_macros.h"
+#include "main.h"
+#include "gtkglobals.h"
+#include "cfilter_combo_utils.h"
+#include "recent.h"
+#ifdef HAVE_LIBPCAP
+#include <pcap.h>
+#endif
+
+/* XXX: use a preference for this setting! */
+static guint cfilter_combo_max_recent = 20;
+
+gboolean
+cfilter_combo_add(gchar *s) {
+ GList *li;
+ GList *fl = OBJECT_GET_DATA(top_level, E_CFILTER_FL_KEY);
+
+ li = g_list_first(fl);
+ while (li) {
+ /* If the filter is already in the list, remove the old one and
+ * append the new one at the latest position (at g_list_append() below) */
+ if (li->data && strcmp(s, li->data) == 0) {
+ fl = g_list_remove(fl, li->data);
+ break;
+ }
+ li = li->next;
+ }
+ fl = g_list_append(fl, s);
+ OBJECT_SET_DATA(top_level, E_CFILTER_FL_KEY, fl);
+ return TRUE;
+}
+
+
+/* write all non empty capture filters (until maximum count)
+ * of the combo box GList to the user's recent file */
+void
+ cfilter_combo_recent_write_all(FILE *rf) {
+ GList *filter_list = OBJECT_GET_DATA(top_level, E_CFILTER_FL_KEY);
+ GList *li;
+ guint max_count = 0;
+
+ /* write all non empty display filter strings to the recent file (until max count) */
+ li = g_list_first(filter_list);
+ while ( li && (max_count++ <= cfilter_combo_max_recent) ) {
+ if (strlen(li->data)) {
+ fprintf (rf, RECENT_KEY_CAPTURE_FILTER ": %s\n", (char *)li->data);
+ }
+ li = li->next;
+ }
+}
+
+/* add a capture filter coming from the user's recent file to the cfilter combo box */
+gboolean
+ cfilter_combo_add_recent(gchar *s) {
+ gchar *dup;
+
+ dup = g_strdup(s);
+ if (!cfilter_combo_add(dup)) {
+ g_free(dup);
+ return FALSE;
+ }
+ return TRUE;
+}
+
+gboolean check_capture_filter_syntax(gchar *interface_name, gchar *filter_str){
+ struct bpf_program fcode;
+ gchar open_err_str[PCAP_ERRBUF_SIZE];
+ pcap_t *pch;
+ int status=0;
+
+ open_err_str[0] = '\0';
+ pch = pcap_open_live(interface_name, WTAP_MAX_PACKET_SIZE, 0, 250, open_err_str);
+ status = pcap_compile(pch, &fcode, filter_str, 1, 0);
+ pcap_close(pch);
+
+ if (status < 0){
+ return FALSE;
+ }
+ else{
+ return TRUE;
+ }
+}
diff --git a/gtk/cfilter_combo_utils.h b/gtk/cfilter_combo_utils.h new file mode 100644 index 0000000000..1a4560cb96 --- /dev/null +++ b/gtk/cfilter_combo_utils.h @@ -0,0 +1,36 @@ +/* cfilter_combo_utils.h
+ * Capture filter combo box routines
+ *
+ * $Id: cfilter_combo_utils.h 12115 2004-09-27 22:55:15Z guy $
+ *
+ * Ethereal - Network traffic analyzer
+ * By Gerald Combs <gerald@ethereal.com>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+extern void cfilter_combo_recent_write_all(FILE *rf);
+extern gboolean cfilter_combo_add_recent(gchar *s);
+/** Check the syntax of a capture filter string. This is done by calling pcap_open_live().
+ *
+ * @param interface_name The interface name to be opened by pcap_open_live().
+ * @param filter_str The filter string to be verified.
+ */
+extern gboolean check_capture_filter_syntax(gchar *interface_name, gchar *filter_str);
+
+#define E_CFILTER_CM_KEY "capture_filter_combo"
+#define E_CFILTER_FL_KEY "capture_filter_list"
+#define RECENT_KEY_CAPTURE_FILTER "recent.capture_filter"
\ No newline at end of file diff --git a/gtk/recent.c b/gtk/recent.c index de198e8a6f..544b4c449f 100644 --- a/gtk/recent.c +++ b/gtk/recent.c @@ -40,7 +40,7 @@ #include "prefs-int.h" #include "ui_util.h" #include "dlg_utils.h" - +#include "cfilter_combo_utils.h" #define RECENT_KEY_MAIN_TOOLBAR_SHOW "gui.toolbar_main_show" #define RECENT_KEY_FILTER_TOOLBAR_SHOW "gui.filter_toolbar_show" @@ -123,6 +123,12 @@ write_recent(char **rf_path_return) menu_recent_file_write_all(rf); fputs("\n" + "######## Recent capture filters (latest last) ########\n" + "\n", rf); + + cfilter_combo_recent_write_all(rf); + + fputs("\n" "######## Recent display filters (latest last) ########\n" "\n", rf); @@ -247,6 +253,8 @@ read_set_recent_pair(gchar *key, gchar *value) add_menu_recent_capture_file(value); } else if (strcmp(key, RECENT_KEY_DISPLAY_FILTER) == 0) { dfilter_combo_add_recent(value); + } else if (strcmp(key, RECENT_KEY_CAPTURE_FILTER) == 0) { + cfilter_combo_add_recent(value); } else if (strcmp(key, RECENT_KEY_MAIN_TOOLBAR_SHOW) == 0) { if (strcasecmp(value, "true") == 0) { recent.main_toolbar_show = TRUE; |