If we have pcap_open, call it instead of pcap_open_live, otherwise we might
[obnox/wireshark/wip.git] / capture-wpcap.c
index dcbb1adef6779e05b4c4f94b7eabcd81d96caac4..a70116826d4203e48d011a052f4e37ae1781cbb5 100644 (file)
 # include "config.h"
 #endif
 
+#include <stdio.h>
+#include <glib.h>
+#include <gmodule.h>
+
 #ifdef HAVE_LIBPCAP
 #include <pcap.h>
 #endif
 
-#include <glib.h>
-#include <gmodule.h>
-
 #include "capture-pcap-util.h"
 #include "capture-pcap-util-int.h"
 
 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
 #include "tools/lemon/cppmagic.h"
 
-
 #define MAX_WIN_IF_NAME_LEN 511
 
 
@@ -91,6 +91,17 @@ static void    (*p_pcap_breakloop) (pcap_t *);
 static const char *(*p_pcap_lib_version) (void);
 static int     (*p_pcap_setbuff) (pcap_t *, int dim);
 static int     (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
+#ifdef HAVE_PCAP_REMOTE
+static pcap_t* (*p_pcap_open) (const char *, int, int, int,
+                               struct pcap_rmtauth *, char *);
+static int     (*p_pcap_findalldevs_ex) (char *, struct pcap_rmtauth *,
+                                         pcap_if_t **, char *);
+static int     (*p_pcap_createsrcstr) (char *, int, const char *, const char *,
+                                       const char *, char *);
+#endif
+#ifdef HAVE_PCAP_SETSAMPLING
+static struct pcap_samp* (*p_pcap_setsampling)(pcap_t *);
+#endif
 
 typedef struct {
        const char      *name;
@@ -116,7 +127,16 @@ load_wpcap(void)
                SYM(pcap_geterr, FALSE),
                SYM(pcap_compile, FALSE),
                SYM(pcap_lookupnet, FALSE),
+#ifdef HAVE_PCAP_REMOTE
+               SYM(pcap_open, FALSE),
+               SYM(pcap_findalldevs_ex, FALSE),
+               SYM(pcap_createsrcstr, FALSE),
+#else
                SYM(pcap_open_live, FALSE),
+#endif
+#ifdef HAVE_PCAP_SETSAMPLING
+               SYM(pcap_setsampling, TRUE),
+#endif
                SYM(pcap_loop, FALSE),
                SYM(pcap_freecode, TRUE),
 #ifdef HAVE_PCAP_FINDALLDEVS
@@ -261,6 +281,42 @@ pcap_open_live(char *a, int b, int c, int d, char *e)
        return p_pcap_open_live(a, b, c, d, e);
 }
 
+#ifdef HAVE_PCAP_REMOTE
+pcap_t*
+pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
+{
+    g_assert(has_wpcap);
+    return p_pcap_open(a, b, c, d, e, f);
+}
+
+int
+pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
+{
+    g_assert(has_wpcap);
+    return p_pcap_findalldevs_ex(a, b, c, d);
+}
+
+int
+pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
+                  char *f)
+{
+    g_assert(has_wpcap);
+    return p_pcap_createsrcstr(a, b, c, d, e, f);
+}
+#endif
+
+#ifdef HAVE_PCAP_SETSAMPLING
+struct pcap_samp *
+pcap_setsampling(pcap_t *a)
+{
+    g_assert(has_wpcap);
+    if (p_pcap_setsampling != NULL) {
+        return p_pcap_setsampling(a);
+    }
+    return NULL;
+}
+#endif
+
 int
 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
 {
@@ -406,7 +462,7 @@ pcap_datalink_name_to_val(const char *name)
                 * We don't have it in WinPcap; do it ourselves.
                 */
                for (i = 0; dlt_choices[i].name != NULL; i++) {
-                       if (strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
+                       if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
                            name) == 0)
                                return dlt_choices[i].dlt;
                }
@@ -460,19 +516,66 @@ int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
        return p_pcap_next_ex(a, b, c);
 }
 
+#ifdef HAVE_PCAP_REMOTE
+GList *
+get_remote_interface_list(const char *hostname, const char *port,
+                          int auth_type, const char *username,
+                          const char *passwd, int *err, char **err_str)
+{
+    struct pcap_rmtauth auth;
+    char source[PCAP_BUF_SIZE];
+    char errbuf[PCAP_ERRBUF_SIZE];
+    GList *result;
+
+    if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
+                          NULL, errbuf) == -1) {
+        *err = CANT_GET_INTERFACE_LIST;
+        if (err_str != NULL)
+            *err_str = cant_get_if_list_error_message(errbuf);
+        return NULL;
+    }
+
+    auth.type = auth_type;
+    auth.username = g_strdup(username);
+    auth.password = g_strdup(passwd);
+
+    result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
+    g_free(auth.username);
+    g_free(auth.password);
+
+    return result;
+}
+#endif
+
 /*
  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
  * fall back on "pcap_lookupdev()".
  */
 GList *
-get_interface_list(int *err, char *err_str)
+get_interface_list(int *err, char **err_str)
 {
+#ifdef HAVE_PCAP_REMOTE
+       char source[PCAP_BUF_SIZE];
+#else
        GList  *il = NULL;
        wchar_t *names;
        char *win95names;
        char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
        char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
        int i, j;
+#endif
+       char errbuf[PCAP_ERRBUF_SIZE];
+
+#ifdef HAVE_PCAP_REMOTE
+    if (p_pcap_createsrcstr(source, PCAP_SRC_IFLOCAL, NULL, NULL,
+                            NULL, errbuf) == -1) {
+        *err = CANT_GET_INTERFACE_LIST;
+        if (err_str != NULL)
+            *err_str = cant_get_if_list_error_message(errbuf);
+        return NULL;
+    }
+    return get_interface_list_findalldevs_ex(source, NULL, err, err_str);
+#else
 
 #ifdef HAVE_PCAP_FINDALLDEVS
        if (p_pcap_findalldevs != NULL)
@@ -522,7 +625,7 @@ get_interface_list(int *err, char *err_str)
         * description of the Nth adapter.
         */
 
-       names = (wchar_t *)pcap_lookupdev(err_str);
+       names = (wchar_t *)pcap_lookupdev(errbuf);
        i = 0;
 
        if (names) {
@@ -609,9 +712,12 @@ get_interface_list(int *err, char *err_str)
                 * No interfaces found.
                 */
                *err = NO_INTERFACES_FOUND;
+               if (err_str != NULL)
+                       *err_str = NULL;
        }
 
        return il;
+#endif  /* HAVE_PCAP_REMOTE */
 }
 
 /*
@@ -662,9 +768,9 @@ get_runtime_pcap_version(GString *str)
        gchar *blankp;
 
        if (has_wpcap) {
-               g_string_sprintfa(str, "with ");
+               g_string_append_printf(str, "with ");
                if (p_pcap_lib_version != NULL)
-                       g_string_sprintfa(str, p_pcap_lib_version());
+                       g_string_append_printf(str, p_pcap_lib_version());
                else {
                        /*
                         * An alternative method of obtaining the version
@@ -696,7 +802,7 @@ get_runtime_pcap_version(GString *str)
                                        g_module_close(handle);
                                }
                        }
-                       g_string_sprintfa(str, "WinPcap (%s)", packetVer);
+                       g_string_append_printf(str, "WinPcap (%s)", packetVer);
                }
        } else
                g_string_append(str, "without WinPcap");