epl: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / capture_opts.c
index 9e75aa4d0b7f0dd137933e6fdba73c725ca4b99d..ef3e659850623d87bdbaaf8c8218fc0ad45d01ed 100644 (file)
 
 #include <string.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+#include <errno.h>
 
 #include <glib.h>
 
-#include <epan/packet.h>
-#include <epan/prefs.h>
-
 #include "capture_opts.h"
 #include "ringbuffer.h"
 
@@ -48,6 +43,8 @@
 #include "caputils/capture_ifinfo.h"
 #include "caputils/capture-pcap-util.h"
 
+#include "ui/filter_files.h"
+
 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
 
 
@@ -61,7 +58,7 @@ capture_opts_init(capture_options *capture_opts)
     capture_opts->default_options.descr           = NULL;
     capture_opts->default_options.cfilter         = NULL;
     capture_opts->default_options.has_snaplen     = FALSE;
-    capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE;
+    capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE_STANDARD;
     capture_opts->default_options.linktype        = -1; /* use interface default */
     capture_opts->default_options.promisc_mode    = TRUE;
     capture_opts->default_options.if_type         = IF_WIRED;
@@ -69,9 +66,12 @@ capture_opts_init(capture_options *capture_opts)
     capture_opts->default_options.extcap          = NULL;
     capture_opts->default_options.extcap_fifo     = NULL;
     capture_opts->default_options.extcap_args     = NULL;
+    capture_opts->default_options.extcap_userdata = NULL;
     capture_opts->default_options.extcap_pid      = INVALID_EXTCAP_PID;
+    capture_opts->default_options.extcap_control_in  = NULL;
+    capture_opts->default_options.extcap_control_out = NULL;
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     capture_opts->default_options.buffer_size     = DEFAULT_CAPTURE_BUFFER_SIZE;
 #endif
     capture_opts->default_options.monitor_mode    = FALSE;
@@ -100,7 +100,6 @@ capture_opts_init(capture_options *capture_opts)
 #endif
     capture_opts->real_time_mode                  = TRUE;
     capture_opts->show_info                       = TRUE;
-    capture_opts->quit_after_cap                  = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
     capture_opts->restart                         = FALSE;
     capture_opts->orig_save_file                  = NULL;
 
@@ -124,6 +123,21 @@ capture_opts_init(capture_options *capture_opts)
     capture_opts->capture_child                   = FALSE;
 }
 
+void
+capture_opts_cleanup(capture_options *capture_opts)
+{
+    if (!capture_opts)
+        return;
+
+    if (capture_opts->ifaces) {
+        g_array_free(capture_opts->ifaces, TRUE);
+        capture_opts->ifaces = NULL;
+    }
+    if (capture_opts->all_ifaces) {
+        g_array_free(capture_opts->all_ifaces, TRUE);
+        capture_opts->all_ifaces = NULL;
+    }
+}
 
 /* log content of capture_opts */
 void
@@ -148,7 +162,7 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
         g_log(log_domain, log_level, "Extcap FIFO[%02d]     : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
         g_log(log_domain, log_level, "Extcap PID[%02d]      : %d", i, interface_opts.extcap_pid);
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
         g_log(log_domain, log_level, "Buffer size[%02d]     : %d (MB)", i, interface_opts.buffer_size);
 #endif
         g_log(log_domain, log_level, "Monitor Mode[%02d]    : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
@@ -188,7 +202,7 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
     g_log(log_domain, log_level, "Extcap[df]          : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
     g_log(log_domain, log_level, "Extcap FIFO[df]     : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     g_log(log_domain, log_level, "Buffer size[df]     : %d (MB)", capture_opts->default_options.buffer_size);
 #endif
     g_log(log_domain, log_level, "Monitor Mode[df]    : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
@@ -223,7 +237,6 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
     g_log(log_domain, log_level, "Fileformat          : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
     g_log(log_domain, log_level, "RealTimeMode        : %u", capture_opts->real_time_mode);
     g_log(log_domain, log_level, "ShowInfo            : %u", capture_opts->show_info);
-    g_log(log_domain, log_level, "QuitAfterCap        : %u", capture_opts->quit_after_cap);
 
     g_log(log_domain, log_level, "MultiFilesOn        : %u", capture_opts->multi_files_on);
     g_log(log_domain, log_level, "FileDuration    (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
@@ -258,7 +271,7 @@ set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
      * as we allow it in the preferences file, we might as well
      * allow it here).
      */
-    while (g_ascii_isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
     if (*p == '\0') {
         /*
@@ -274,7 +287,7 @@ set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
         capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
     } else if (strcmp(autostoparg,"filesize") == 0) {
         capture_opts->has_autostop_filesize = TRUE;
-        capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
+        capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
     } else if (strcmp(autostoparg,"files") == 0) {
         capture_opts->multi_files_on = TRUE;
         capture_opts->has_autostop_files = TRUE;
@@ -286,6 +299,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
@@ -309,7 +376,7 @@ get_ring_arguments(capture_options *capture_opts, const char *arg)
      * as we allow it in the preferences file, we might as well
      * allow it here).
      */
-    while (g_ascii_isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
     if (*p == '\0') {
         /*
@@ -323,10 +390,10 @@ get_ring_arguments(capture_options *capture_opts, const char *arg)
 
     if (strcmp(arg,"files") == 0) {
         capture_opts->has_ring_num_files = TRUE;
-        capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
+        capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
     } else if (strcmp(arg,"filesize") == 0) {
         capture_opts->has_autostop_filesize = TRUE;
-        capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
+        capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
     } else if (strcmp(arg,"duration") == 0) {
         capture_opts->has_file_duration = TRUE;
         capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
@@ -355,7 +422,7 @@ get_sampling_arguments(capture_options *capture_opts, const char *arg)
     p = colonp;
     *p++ = '\0';
 
-    while (g_ascii_isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
     if (*p == '\0') {
         *colonp = ':';
@@ -413,7 +480,7 @@ get_auth_arguments(capture_options *capture_opts, const char *arg)
     p = colonp;
     *p++ = '\0';
 
-    while (g_ascii_isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
 
     if (capture_opts->ifaces->len > 0) {
@@ -498,12 +565,20 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
             /* fallback to the interface name */
             interface_opts.console_display_name = g_strdup(if_info->name);
         }
+        interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+        interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
         free_interface_list(if_list);
     } else if (capture_opts->capture_child) {
         /* In Wireshark capture child mode, thus proper device name is supplied. */
         /* No need for trying to match it for friendly names. */
         interface_opts.name = g_strdup(optarg_str_p);
         interface_opts.console_display_name = g_strdup(optarg_str_p);
+        interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+        interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
     } else {
         /*
          * Retrieve the interface list so that we can search for the
@@ -541,6 +616,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                     } else {
                         interface_opts.console_display_name = g_strdup(if_info->name);
                     }
+                    interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                    interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                     matched = TRUE;
                     break;
                 }
@@ -551,6 +630,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                     /* exact match - use the friendly name for display */
                     interface_opts.name = g_strdup(if_info->name);
                     interface_opts.console_display_name = g_strdup(if_info->friendly_name);
+                    interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                    interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                     matched = TRUE;
                     break;
                 }
@@ -571,6 +654,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                         /* prefix match - use the friendly name for display */
                         interface_opts.name = g_strdup(if_info->name);
                         interface_opts.console_display_name = g_strdup(if_info->friendly_name);
+                        interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                        interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                         matched = TRUE;
                         break;
                     }
@@ -586,11 +673,19 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                  */
                 interface_opts.name = g_strdup(optarg_str_p);
                 interface_opts.console_display_name = g_strdup(optarg_str_p);
+                interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+                interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
             }
             free_interface_list(if_list);
         } else {
             interface_opts.name = g_strdup(optarg_str_p);
             interface_opts.console_display_name = g_strdup(optarg_str_p);
+            interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+            interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
         }
     }
 
@@ -604,14 +699,15 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
     interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
     interface_opts.linktype = capture_opts->default_options.linktype;
     interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
-    interface_opts.if_type = capture_opts->default_options.if_type;
 #ifdef HAVE_EXTCAP
-    interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
     interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
     interface_opts.extcap_args = NULL;
     interface_opts.extcap_pid = INVALID_EXTCAP_PID;
+    interface_opts.extcap_userdata = NULL;
+    interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
+    interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     interface_opts.buffer_size = capture_opts->default_options.buffer_size;
 #endif
     interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
@@ -671,7 +767,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
             return 1;
         }
         break;
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     case 'B':        /* Buffer size */
         if (capture_opts->ifaces->len > 0) {
             interface_options interface_opts;
@@ -690,18 +786,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;
@@ -780,7 +865,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
          * length, mirroring what tcpdump does.
          */
         if (snaplen == 0)
-            snaplen = WTAP_MAX_PACKET_SIZE;
+            snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
         if (capture_opts->ifaces->len > 0) {
             interface_options interface_opts;
 
@@ -909,14 +994,14 @@ capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
             interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
             if (interface_opts.snaplen < 1)
-                interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
+                interface_opts.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
             else if (interface_opts.snaplen < snaplen_min)
                 interface_opts.snaplen = snaplen_min;
             g_array_append_val(capture_opts->ifaces, interface_opts);
         }
     } else {
         if (capture_opts->default_options.snaplen < 1)
-            capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
+            capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
         else if (capture_opts->default_options.snaplen < snaplen_min)
             capture_opts->default_options.snaplen = snaplen_min;
     }
@@ -1047,6 +1132,9 @@ capture_opts_del_iface(capture_options *capture_opts, guint if_index)
         g_hash_table_unref(interface_opts.extcap_args);
     if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
         g_spawn_close_pid(interface_opts.extcap_pid);
+    g_free(interface_opts.extcap_userdata);
+    g_free(interface_opts.extcap_control_in);
+    g_free(interface_opts.extcap_control_out);
 #endif
 #ifdef HAVE_PCAP_REMOTE
     if (interface_opts.src_type == CAPTURE_IFREMOTE) {
@@ -1092,12 +1180,16 @@ collect_ifaces(capture_options *capture_opts)
 #ifdef HAVE_EXTCAP
             interface_opts.extcap = g_strdup(device.if_info.extcap);
             interface_opts.extcap_fifo = NULL;
+            interface_opts.extcap_userdata = NULL;
             interface_opts.extcap_args = device.external_cap_args_settings;
             interface_opts.extcap_pid = INVALID_EXTCAP_PID;
             if (interface_opts.extcap_args)
                 g_hash_table_ref(interface_opts.extcap_args);
+            interface_opts.extcap_userdata = NULL;
+            interface_opts.extcap_control_in = NULL;
+            interface_opts.extcap_control_out = NULL;
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
             interface_opts.buffer_size =  device.buffer;
 #endif
 #ifdef HAVE_PCAP_CREATE
@@ -1125,6 +1217,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 */