Make capture filter syntax checking independent of pcap_create by using pcap_compile_nopcap

svn path=/trunk/; revision=34333
This commit is contained in:
Sake Blok 2010-10-02 14:47:41 +00:00
parent a7cc9f5358
commit 3664650ead
1 changed files with 32 additions and 69 deletions

View File

@ -245,6 +245,31 @@ capture_get_cap_settings (gchar *if_name)
return cap_settings;
}
static void
capture_filter_check_syntax_cb(GtkWidget *w _U_, gpointer user_data _U_)
{
struct bpf_program fcode;
GtkWidget *filter_cm, *filter_te;
const gchar *filter_text;
filter_cm = g_object_get_data(G_OBJECT(top_level), E_CFILTER_CM_KEY);
filter_te = GTK_COMBO(filter_cm)->entry;
filter_text = gtk_entry_get_text(GTK_ENTRY(filter_te));
if (strlen(filter_text) == 0) {
colorize_filter_te_as_empty(filter_te);
return;
}
if (pcap_compile_nopcap(128 /* use a dummy snaplength for syntax-checking */,
global_capture_opts.linktype, &fcode, filter_text, 1 /* Do optimize */,
(uint)255*256*256*256 /* use a dummy netmask for syntax-checking */) < 0) {
colorize_filter_te_as_invalid(filter_te);
} else {
colorize_filter_te_as_valid(filter_te);
}
}
/*
* Set the sensitivity of the monitor mode button, and set the
* link-layer header type list, based on the interface's capabilities.
@ -442,9 +467,13 @@ set_if_capabilities(gboolean monitor_mode_changed)
FALSE);
g_free(str);
}
if (data_link_info->dlt == cap_settings.linktype) {
if (linktype_count==0) {
/* default to first dlt of the interface */
global_capture_opts.linktype = data_link_info->dlt;
} else if (data_link_info->dlt == cap_settings.linktype) {
/* Found a matching dlt, select this */
linktype_select = linktype_count;
global_capture_opts.linktype = data_link_info->dlt;
}
linktype_count++;
} /* for (lt_entry = ... */
@ -468,8 +497,6 @@ set_if_capabilities(gboolean monitor_mode_changed)
gtk_widget_set_sensitive(linktype_lb, num_supported_link_types >= 2);
gtk_widget_set_sensitive(linktype_combo_box, num_supported_link_types >= 2);
global_capture_opts.linktype = cap_settings.linktype;
#ifdef HAVE_PCAP_CREATE
/* Set the monitor-mode checkbox to the appropriate value */
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(monitor_cb),
@ -1476,67 +1503,6 @@ capture_remote_combo_add_recent(gchar *s)
#endif
#ifdef HAVE_PCAP_CREATE
static void
capture_filter_check_syntax_cb(GtkWidget *w _U_, gpointer user_data _U_)
{
GtkWidget *if_cb;
gchar *entry_text;
gchar *if_text;
const gchar *if_name;
pcap_t *pd;
struct bpf_program fcode;
char errbuf[PCAP_ERRBUF_SIZE];
GtkWidget *filter_cm, *filter_te;
const gchar *filter_text;
filter_cm = g_object_get_data(G_OBJECT(top_level), E_CFILTER_CM_KEY);
filter_te = GTK_COMBO(filter_cm)->entry;
filter_text = gtk_entry_get_text(GTK_ENTRY(filter_te));
if (strlen(filter_text) == 0) {
colorize_filter_te_as_empty(filter_te);
return;
}
if_cb = (GtkWidget *)g_object_get_data(G_OBJECT(cap_open_w), E_CAP_IFACE_KEY);
entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(if_cb)->entry)));
if_text = g_strstrip(entry_text);
if_name = get_if_name(if_text);
if (*if_name == '\0') {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"You didn't specify an interface on which to capture packets.");
g_free(entry_text);
return;
}
if (!(pd = pcap_create(if_name, errbuf))) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errbuf);
g_free(entry_text);
return;
}
/* Activate the PD to set the proper DLT for this interface */
pcap_activate(pd);
/* change the DLT if the user selected a non-default DLT for the interface */
if (global_capture_opts.linktype != -1)
pcap_set_datalink(pd, global_capture_opts.linktype);
if (pcap_compile(pd, &fcode, filter_text, 1 /* Do optimize */,
(uint)255*256*256*256 /* use a dummy netmask for syntax-checking */) < 0) {
/* invalid filter */
colorize_filter_te_as_invalid(filter_te);
} else {
/* valid filter */
colorize_filter_te_as_valid(filter_te);
}
g_free(entry_text);
pcap_close(pd);
}
#endif
#ifdef HAVE_PCAP_CREATE
static void
capture_filter_compile_cb(GtkWidget *w _U_, gpointer user_data _U_)
@ -1999,9 +1965,7 @@ capture_prep_cb(GtkWidget *w _U_, gpointer d _U_)
g_object_set_data(G_OBJECT(top_level), E_CFILTER_CM_KEY, filter_cm);
filter_te = GTK_COMBO(filter_cm)->entry;
colorize_filter_te_as_empty(filter_te);
#ifdef HAVE_PCAP_CREATE
g_signal_connect(filter_te, "changed", G_CALLBACK(capture_filter_check_syntax_cb), NULL);
#endif
if (cfilter_list != NULL)
gtk_combo_set_popdown_strings(GTK_COMBO(filter_cm), cfilter_list);
@ -2672,9 +2636,7 @@ select_link_type_cb(GtkWidget *linktype_combo_box, gpointer data _U_)
g_assert_not_reached(); /* Programming error: somehow managed to select an "unsupported" entry */
}
global_capture_opts.linktype = dlt;
#ifdef HAVE_PCAP_CREATE
capture_filter_check_syntax_cb(linktype_combo_box,data);
#endif
capture_filter_check_syntax_cb(linktype_combo_box, data);
}
#ifdef HAVE_PCAP_REMOTE
@ -3095,6 +3057,7 @@ static void
capture_prep_interface_changed_cb(GtkWidget *entry _U_, gpointer argp _U_)
{
set_if_capabilities(FALSE);
capture_filter_check_syntax_cb(entry, argp);
}
#ifdef HAVE_PCAP_CREATE