Update to V9.0.0 (2009-12)
[obnox/wireshark/wip.git] / capture_ui_utils.c
index 53740237b27d86a1b5824c5af976117ee5b662c3..551b78947cd0d6859540c08afdb9ff5de918a0c0 100644 (file)
@@ -3,8 +3,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
@@ -28,7 +28,8 @@
 
 #ifdef HAVE_LIBPCAP
 
-#include <pcap.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <glib.h>
 
@@ -40,7 +41,7 @@
  * Find user-specified capture device description that matches interface
  * name, if any.
  */
-static char *
+char *
 capture_dev_user_descr_find(const gchar *if_name)
 {
        char    *p;
@@ -96,11 +97,48 @@ capture_dev_user_descr_find(const gchar *if_name)
                return NULL;
 }
 
+gint
+capture_dev_user_linktype_find(const gchar *if_name)
+{
+       gchar *p, *next;
+       long linktype;
+
+       if (prefs.capture_devices_linktypes == NULL) {
+               /* There are no link-layer header types */
+               return -1;
+       }
+
+       if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
+               /* There are, but there isn't one for this interface. */
+               return -1;
+       }
+
+       p += strlen(if_name) + 1;
+       linktype = strtol(p, &next, 10);
+       if (next == p || *next != ')' || linktype < 0) {
+               /* Syntax error */
+               return -1;
+       }
+       if (linktype > G_MAXINT) {
+               /* Value doesn't fit in a gint */
+               return -1;
+       }
+
+       return (gint)linktype;
+}
+
 /*
  * Return as descriptive a name for an interface as we can get.
  * If the user has specified a comment, use that.  Otherwise,
  * if get_interface_list() supplies a description, use that,
  * otherwise use the interface name.
+ *
+ * The result must be g_free()'d when you're done with it.
+ *
+ * Note: given that this calls get_interface_list(), which attempts to
+ * open all adapters it finds in order to check whether they can be
+ * captured on, this is an expensive routine to call, so don't call it
+ * frequently.
  */
 char *
 get_interface_descriptive_name(const char *if_name)
@@ -110,7 +148,6 @@ get_interface_descriptive_name(const char *if_name)
   GList *if_entry;
   if_info_t *if_info;
   int err;
-  char err_buf[PCAP_ERRBUF_SIZE];
 
   /* Do we have a user-supplied description? */
   descr = capture_dev_user_descr_find(if_name);
@@ -121,8 +158,8 @@ get_interface_descriptive_name(const char *if_name)
     /* No, we don't have a user-supplied description; did we get
        one from the OS or libpcap? */
     descr = NULL;
-    if_list = get_interface_list(&err, err_buf);
-    if (if_list != NULL) {
+    if_list = get_interface_list(&err, NULL);
+    if (if_list != NULL && if_name != NULL) {
       if_entry = if_list;
       do {
         if_info = if_entry->data;
@@ -220,9 +257,7 @@ build_capture_combo_list(GList *if_list, gboolean do_hide)
 
       /* Is this interface hidden and, if so, should we include it
          anyway? */
-      if (prefs.capture_devices_hide == NULL ||
-         strstr(prefs.capture_devices_hide, if_info->name) == NULL ||
-         !do_hide) {
+      if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
        /* It's not hidden, or it is but we should include it in the list. */
 
        /* Do we have a user-supplied description? */
@@ -269,10 +304,10 @@ free_capture_combo_list(GList *combo_list)
  * Given text that contains an interface name possibly prefixed by an
  * interface description, extract the interface name.
  */
-char *
-get_if_name(char *if_text)
+const char *
+get_if_name(const char *if_text)
 {
-  char *if_name;
+  const char *if_name;
 
 #ifdef _WIN32
   /*
@@ -313,11 +348,13 @@ get_if_name(char *if_text)
         * it'll be followed by a blank if it separates the description
         * and the interface name.  (We don't wire in "rpcap", in case we
         * support other protocols in the same syntax.)
+        * Unfortunately, another colon can be used in "rpcap://host:port/"
+        * before port. Check if colon is followed by digit.
         */
-       if (strncmp(if_name, "://", 3) != 0) {
+       if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
          /*
-          * OK, we've found a colon not followed by "//".  Skip blanks
-          * following it.
+          * OK, we've found a colon followed neither by "//" nor by digit.  
+          * Skip blanks following it.
           */
          if_name++;
          while (*if_name == ' ')
@@ -346,4 +383,17 @@ get_if_name(char *if_text)
   return if_name;
 }
 
+/*  Return capture_opts->iface_descr (after setting it if it is not set)
+ *  This is necessary because capture_opts.c can't set iface_descr (at least
+ *  not without adding significant dependencies there).
+ */
+const char *
+get_iface_description(capture_options *capture_opts)
+{
+       if (!capture_opts->iface_descr && capture_opts->iface)
+               capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
+
+       return(capture_opts->iface_descr);
+
+}
 #endif /* HAVE_LIBPCAP */