Centralize the code to set the active_dlt value for a device.
authorGuy Harris <guy@alum.mit.edu>
Sun, 22 Mar 2015 23:58:42 +0000 (16:58 -0700)
committerGuy Harris <guy@alum.mit.edu>
Mon, 23 Mar 2015 00:00:42 +0000 (00:00 +0000)
We were doing it similarly, but not the same, in several places; make a
common routine for it.

Have that code check to make sure the DLT_ value in question is actually
supported by the device; if not, pick the first supported DLT_ value, if
any; this keeps it from, for example, picking a bad DLT_ value if your
defaults were set based on monitor mode being on but monitor mode
actually being off, or vice versa.

Change-Id: I1722bfeaf60429bc5c6f665fdea3d466052b13bd
Reviewed-on: https://code.wireshark.org/review/7795
Reviewed-by: Guy Harris <guy@alum.mit.edu>
ui/capture_ui_utils.c
ui/capture_ui_utils.h
ui/gtk/capture_dlg.c
ui/iface_lists.c
ui/qt/capture_interfaces_dialog.cpp

index f94afb6b3ad8c63f22d3c02958b436cecfb9931c..b5d0e3e0e1c5e1064789593f1a181c3dfeb8d5ad 100644 (file)
@@ -33,6 +33,7 @@
 #include "epan/ex-opt.h"
 #include "caputils/capture_ifinfo.h"
 #include "ui/capture_ui_utils.h"
+#include "ui/capture_globals.h"
 #include "wiretap/wtap.h"
 #include "epan/to_str.h"
 
@@ -490,6 +491,56 @@ get_iface_description_for_interface(capture_options *capture_opts, guint i)
   }
 }
 
+/*
+ * Set the active DLT for a device appropriately.
+ */
+void
+set_active_dlt(interface_t *device, int global_default_dlt)
+{
+  GList    *list;
+  gboolean  found_active_dlt;
+  link_row *link;
+
+  /*
+   * If there's a preference for the link-layer header type for
+   * this interface, use it.  If not, use the all-interface
+   * default; if that's not set on the command line, that will
+   * be -1, meaning "use per-interface defaults", otherwise
+   * we'll fail if it's not one of the types the interface
+   * supports.
+   */
+  if ((device->active_dlt = capture_dev_user_linktype_find(device->name)) == -1) {
+    device->active_dlt = global_default_dlt;
+  }
+
+  /*
+   * Is that one of the supported link-layer header types?
+   * If not, set it to -1, so we'll fall back on the first supported
+   * link-layer header type.
+   */
+  found_active_dlt = FALSE;
+  for (list = device->links; list != NULL; list = g_list_next(list)) {
+    link = (link_row *)(list->data);
+    if (link->dlt != -1 && link->dlt == device->active_dlt) {
+      found_active_dlt = TRUE;
+      break;
+    }
+  }
+  if (!found_active_dlt) {
+    device->active_dlt = -1;
+  }
+  if (device->active_dlt == -1) {
+    /* Fall back on the first supported DLT, if we have one. */
+    for (list = device->links; list != NULL; list = g_list_next(list)) {
+      link = (link_row *)(list->data);
+      if (link->dlt != -1) {
+        device->active_dlt = link->dlt;
+        break;
+      }
+    }
+  }
+}
+
 #endif /* HAVE_LIBPCAP */
 
 /*
index 6b5361f83e3b41d3bb958db6b7efd34460bf564b..ee411e05fda673fe08f0016831750f85f4a97c29 100644 (file)
@@ -145,6 +145,13 @@ const char *get_if_name(const char *if_text);
  */
 const char *get_iface_description_for_interface(capture_options *capture_opts, guint i);
 
+/** Set the active DLT for a device appropriately.
+ *
+ * @param device the device on which to set the active DLT
+ * @param global_default_dlt the global default DLT
+ */
+extern void set_active_dlt(interface_t *device, int global_default_dlt);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 350660d86f46c0c63327907cb2af6deba1ec9f88..9b919427e1e0b78f8a99682347d56ddc48b2a9ee 100644 (file)
@@ -5656,10 +5656,8 @@ create_and_fill_model(GtkTreeView *view)
       } else {
         temp = g_strdup_printf("<b>%s</b>\n<span size='small'>%s</span>", device.display_name, device.addresses);
       }
+      set_active_dlt(&device, global_capture_opts.default_options.linktype);
       linkname = NULL;
-      if(capture_dev_user_linktype_find(device.name) != -1) {
-        device.active_dlt = capture_dev_user_linktype_find(device.name);
-      }
       for (list = device.links; list != NULL; list = g_list_next(list)) {
         linkr = (link_row*)(list->data);
         if (linkr->dlt == device.active_dlt) {
index 1310c30a3fbe484501adb995d1e19bf54487cfb7..1dfb9b88c3f44413577645e596dd3fdc02fa1094 100644 (file)
@@ -243,29 +243,11 @@ scan_local_interfaces(void (*update_cb)(void))
             device.monitor_mode_enabled = monitor_mode;
             device.monitor_mode_supported = caps->can_set_rfmon;
 #endif
-            /*
-             * If there's a preference for the link-layer header type for
-             * this interface, use it.  If not, use the all-interface
-             * default; if that's not set on the command line, that will
-             * be -1, meaning "use per-interface defaults", otherwise
-             * we'll fail if it's not one of the types the interface
-             * supports.
-             */
-            if ((device.active_dlt = capture_dev_user_linktype_find(if_info->name)) == -1) {
-                device.active_dlt = global_capture_opts.default_options.linktype;
-            }
-
             /*
              * Process the list of link-layer header types.
-             * If the active link-layer header type wasn't set from a
-             * preference or a global option (meaning it's -1), default
-             * to the first link-layer header type in the list.
              */
             for (lt_entry = caps->data_link_types; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
                 data_link_info = (data_link_info_t *)lt_entry->data;
-                if (linktype_count == 0 && device.active_dlt == -1) {
-                    device.active_dlt = data_link_info->dlt;
-                }
                 link = (link_row *)g_malloc(sizeof(link_row));
                 if (data_link_info->description != NULL) {
                     link->dlt = data_link_info->dlt;
@@ -277,6 +259,11 @@ scan_local_interfaces(void (*update_cb)(void))
                 device.links = g_list_append(device.links, link);
                 linktype_count++;
             }
+
+            /*
+             * Set the active DLT for the device appropriately.
+             */
+            set_active_dlt(&device, global_capture_opts.default_options.linktype);
         } else {
 #if defined(HAVE_PCAP_CREATE)
             device.monitor_mode_enabled = FALSE;
index f1c853942916120fdd9c2805301627be3a89038e..2e164770f018a77b0fe6684f6c9691f44691bb97 100644 (file)
@@ -487,10 +487,9 @@ void CaptureInterfacesDialog::updateInterfaces()
                 ti->setToolTip(col_interface_, tr("no addresses"));
             }
 
+            set_active_dlt(device, global_capture_opts.default_options.linktype);
+
             QString linkname = "unknown";
-            if(capture_dev_user_linktype_find(device->name) != -1) {
-              device->active_dlt = capture_dev_user_linktype_find(device->name);
-            }
             for (list = device->links; list != NULL; list = g_list_next(list)) {
                 linkr = (link_row*)(list->data);
                 if (linkr->dlt == device->active_dlt) {