tshark JSON and Elasticsearch output
[metze/wireshark/wip.git] / capture_opts.c
index 5c6042167e07139126a9d1cd3a16caa8a8c1d980..38c39b304b73018d029f188d96e3ce994a71a73a 100644 (file)
@@ -43,6 +43,8 @@
 #include "caputils/capture_ifinfo.h"
 #include "caputils/capture-pcap-util.h"
 
+#include "filter_files.h"
+
 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
 
 
@@ -281,6 +283,60 @@ set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
     return TRUE;
 }
 
+static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
+{
+    char* colonp;
+    char* val;
+    char* filter_exp = NULL;
+
+    colonp = strchr(arg, ':');
+    if (colonp) {
+        val = colonp;
+        *val = '\0';
+        val++;
+        if (strcmp(arg, "predef") == 0) {
+            GList* filterItem;
+
+            filterItem = get_filter_list_first(CFILTER_LIST);
+            while (filterItem != NULL) {
+                filter_def *filterDef;
+
+                filterDef = (filter_def*)filterItem->data;
+                if (strcmp(val, filterDef->name) == 0) {
+                    filter_exp = g_strdup(filterDef->strval);
+                    break;
+                }
+                filterItem = filterItem->next;
+            }
+        }
+    }
+
+    if (filter_exp == NULL) {
+        /* No filter expression found yet; fallback to previous implemention
+           and assume the arg contains a filter expression */
+        if (colonp) {
+            *colonp = ':';      /* restore colon */
+        }
+        filter_exp = g_strdup(arg);
+    }
+
+    if (capture_opts->ifaces->len > 0) {
+        interface_options interface_opts;
+
+        interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
+        capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
+        g_free(interface_opts.cfilter);
+        interface_opts.cfilter = filter_exp;
+        g_array_append_val(capture_opts->ifaces, interface_opts);
+        return TRUE;
+    }
+    else {
+        g_free(capture_opts->default_options.cfilter);
+        capture_opts->default_options.cfilter = filter_exp;
+        return TRUE;
+    }
+}
+
 /*
  * Given a string of the form "<ring buffer file>:<duration>", as might appear
  * as an argument to a "-b" option, parse it and set the arguments in
@@ -711,18 +767,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
         capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
         break;
     case 'f':        /* capture filter */
-        if (capture_opts->ifaces->len > 0) {
-            interface_options interface_opts;
-
-            interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
-            capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
-            g_free(interface_opts.cfilter);
-            interface_opts.cfilter = g_strdup(optarg_str_p);
-            g_array_append_val(capture_opts->ifaces, interface_opts);
-        } else {
-            g_free(capture_opts->default_options.cfilter);
-            capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
-        }
+        get_filter_arguments(capture_opts, optarg_str_p);
         break;
     case 'g':        /* enable group read access on the capture file(s) */
         capture_opts->group_read_access = TRUE;
@@ -1146,6 +1191,50 @@ collect_ifaces(capture_options *capture_opts)
     }
 }
 
+static void
+capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
+{
+    link_row* e = (link_row*)elem;
+    if (e != NULL)
+        g_free(e->name);
+    g_free(elem);
+}
+
+static void
+capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
+{
+    g_free(elem);
+}
+
+void
+capture_opts_free_interface_t(interface_t *device)
+{
+    if (device != NULL) {
+        g_free(device->name);
+        g_free(device->display_name);
+        g_free(device->friendly_name);
+        g_free(device->addresses);
+        g_free(device->cfilter);
+        g_list_foreach(device->links,
+                       capture_opts_free_interface_t_links, NULL);
+        g_list_free(device->links);
+#ifdef HAVE_PCAP_REMOTE
+        g_free(device->remote_opts.remote_host_opts.remote_host);
+        g_free(device->remote_opts.remote_host_opts.remote_port);
+        g_free(device->remote_opts.remote_host_opts.auth_username);
+        g_free(device->remote_opts.remote_host_opts.auth_password);
+#endif
+        g_free(device->if_info.name);
+        g_free(device->if_info.friendly_name);
+        g_free(device->if_info.vendor_description);
+        g_slist_foreach(device->if_info.addrs,
+                        capture_opts_free_interface_t_addrs, NULL);
+        g_slist_free(device->if_info.addrs);
+#ifdef HAVE_EXTCAP
+        g_free(device->if_info.extcap);
+#endif
+    }
+}
 
 #endif /* HAVE_LIBPCAP */