Make dissection of AVP: 3GPP-User-Location-Info(22) l=15 f=V-- vnd=TGPP val=303231...
[obnox/wireshark/wip.git] / capture-wpcap.c
index 5d0902bb2c60c66c197088dda8b30f08933ee1f7..5492f972ca51ca0448cfaa097e3ddb13a442f386 100644 (file)
@@ -1,12 +1,12 @@
 /* capture-wpcap.c
  * WinPcap-specific interfaces for capturing.  We load WinPcap at run
- * time, so that we only need one Ethereal binary and one Tethereal binary
+ * time, so that we only need one Wireshark binary and one TShark binary
  * for Windows, regardless of whether WinPcap is installed or not.
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 2001 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
 # include "config.h"
 #endif
 
-#ifdef HAVE_LIBPCAP
-#include <pcap.h>
-#endif
-
+#include <stdio.h>
 #include <glib.h>
 #include <gmodule.h>
 
+#include "capture_ifinfo.h"
 #include "capture-pcap-util.h"
 #include "capture-pcap-util-int.h"
 
+#include <wsutil/file_util.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
 
 
@@ -49,6 +48,11 @@ gboolean has_wpcap = FALSE;
 
 #ifdef HAVE_LIBPCAP
 
+/*
+ * XXX - should we require at least WinPcap 3.1 both for building an
+ * for using Wireshark?
+ */
+
 static char*   (*p_pcap_lookupdev) (char *);
 static void    (*p_pcap_close) (pcap_t *);
 static int     (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
@@ -57,18 +61,17 @@ static int     (*p_pcap_snapshot) (pcap_t *);
 static int     (*p_pcap_datalink) (pcap_t *);
 static int     (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
 static char*   (*p_pcap_geterr) (pcap_t *);
-static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, char *, int,
+static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, const char *, int,
+                       bpf_u_int32);
+static int     (*p_pcap_compile_nopcap) (int, int, struct bpf_program *, const char *, int,
                        bpf_u_int32);
-#ifdef WPCAP_CONSTIFIED
 static int     (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
                        char *);
 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
-#else
-static int     (*p_pcap_lookupnet) (char *, bpf_u_int32 *, bpf_u_int32 *,
-                       char *);
-static pcap_t* (*p_pcap_open_live) (char *, int, int, int, char *);
-#endif
 static int     (*p_pcap_loop) (pcap_t *, int, pcap_handler, guchar *);
+#ifdef HAVE_PCAP_OPEN_DEAD
+static pcap_t* (*p_pcap_open_dead) (int, int);
+#endif
 static void    (*p_pcap_freecode) (struct bpf_program *);
 #ifdef HAVE_PCAP_FINDALLDEVS
 static int     (*p_pcap_findalldevs) (pcap_if_t **, char *);
@@ -80,9 +83,42 @@ static int (*p_pcap_datalink_name_to_val) (const char *);
 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
 static const char *(*p_pcap_datalink_val_to_name) (int);
 #endif
+#ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
+static const char *(*p_pcap_datalink_val_to_description) (int);
+#endif
+#ifdef HAVE_PCAP_BREAKLOOP
+static void    (*p_pcap_breakloop) (pcap_t *);
+#endif
 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
+
+#ifdef HAVE_PCAP_LIST_DATALINKS
+static int     (*p_pcap_list_datalinks)(pcap_t *, int **);
+#endif
+
+#ifdef HAVE_PCAP_SET_DATALINK
+static int     (*p_pcap_set_datalink)(pcap_t *, int);
+#endif
+
+#ifdef HAVE_PCAP_FREE_DATALINKS
+static int     (*p_pcap_free_datalinks)(int *);
+#endif
+
+#ifdef HAVE_BPF_IMAGE
+static char     *(*p_bpf_image) (const struct bpf_insn *, int);
+#endif
 
 typedef struct {
        const char      *name;
@@ -107,8 +143,20 @@ load_wpcap(void)
                SYM(pcap_setfilter, FALSE),
                SYM(pcap_geterr, FALSE),
                SYM(pcap_compile, FALSE),
+                SYM(pcap_compile_nopcap, FALSE),
                SYM(pcap_lookupnet, FALSE),
+#ifdef HAVE_PCAP_REMOTE
+               SYM(pcap_open, FALSE),
+               SYM(pcap_findalldevs_ex, FALSE),
+               SYM(pcap_createsrcstr, FALSE),
+#endif
                SYM(pcap_open_live, FALSE),
+#ifdef HAVE_PCAP_OPEN_DEAD
+               SYM(pcap_open_dead, FALSE),
+#endif
+#ifdef HAVE_PCAP_SETSAMPLING
+               SYM(pcap_setsampling, TRUE),
+#endif
                SYM(pcap_loop, FALSE),
                SYM(pcap_freecode, TRUE),
 #ifdef HAVE_PCAP_FINDALLDEVS
@@ -120,17 +168,40 @@ load_wpcap(void)
 #endif
 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
                SYM(pcap_datalink_val_to_name, TRUE),
+#endif
+#ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
+               SYM(pcap_datalink_val_to_description, TRUE),
+#endif
+#ifdef HAVE_PCAP_BREAKLOOP
+               /*
+                * We don't try to work around the lack of this at
+                * run time; it's present in WinPcap 3.1, which is
+                * the version we build with and ship with.
+                */
+               SYM(pcap_breakloop, FALSE),
 #endif
                SYM(pcap_lib_version, TRUE),
                SYM(pcap_setbuff, TRUE),
                SYM(pcap_next_ex, TRUE),
+#ifdef HAVE_PCAP_LIST_DATALINKS
+               SYM(pcap_list_datalinks, FALSE),
+#endif
+#ifdef HAVE_PCAP_SET_DATALINK
+               SYM(pcap_set_datalink, FALSE),
+#endif
+#ifdef HAVE_PCAP_FREE_DATALINKS
+               SYM(pcap_free_datalinks, TRUE),
+#endif
+#ifdef HAVE_BPF_IMAGE
+                SYM(bpf_image, FALSE),
+#endif
                { NULL, NULL, FALSE }
        };
 
        GModule         *wh; /* wpcap handle */
        const symbol_table_t    *sym;
 
-       wh = g_module_open("wpcap", 0);
+       wh = ws_module_open("wpcap.dll", 0);
 
        if (!wh) {
                return;
@@ -162,7 +233,9 @@ load_wpcap(void)
 char*
 pcap_lookupdev (char *a)
 {
-       g_assert(has_wpcap);
+       if (!has_wpcap) {
+               return NULL;
+       }
        return p_pcap_lookupdev(a);
 }
 
@@ -201,6 +274,15 @@ pcap_datalink(pcap_t *a)
        return p_pcap_datalink(a);
 }
 
+#ifdef HAVE_PCAP_SET_DATALINK
+int
+pcap_set_datalink(pcap_t *p, int dlt)
+{
+       g_assert(has_wpcap);
+       return p_pcap_set_datalink(p, dlt);
+}
+#endif
+
 int
 pcap_setfilter(pcap_t *a, struct bpf_program *b)
 {
@@ -216,7 +298,7 @@ pcap_geterr(pcap_t *a)
 }
 
 int
-pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
+pcap_compile(pcap_t *a, struct bpf_program *b, const char *c, int d,
             bpf_u_int32 e)
 {
        g_assert(has_wpcap);
@@ -224,26 +306,88 @@ pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
 }
 
 int
-#ifdef WPCAP_CONSTIFIED
+pcap_compile_nopcap(int a, int b, struct bpf_program *c, const char *d, int e,
+            bpf_u_int32 f)
+{
+       g_assert(has_wpcap);
+       return p_pcap_compile_nopcap(a, b, c, d, e, f);
+}
+
+int
 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
-#else
-pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
-#endif
 {
        g_assert(has_wpcap);
        return p_pcap_lookupnet(a, b, c, d);
 }
 
 pcap_t*
-#ifdef WPCAP_CONSTIFIED
 pcap_open_live(const char *a, int b, int c, int d, char *e)
-#else
-pcap_open_live(char *a, int b, int c, int d, char *e)
+{
+    if (!has_wpcap) {
+       return NULL;
+    }
+    return p_pcap_open_live(a, b, c, d, e);
+}
+
+#ifdef HAVE_PCAP_OPEN_DEAD
+pcap_t*
+pcap_open_dead(int a, int b)
+{
+    if (!has_wpcap) {
+       return NULL;
+    }
+    return p_pcap_open_dead(a, b);
+}
 #endif
+
+#ifdef HAVE_BPF_IMAGE
+char *
+bpf_image(const struct bpf_insn *a, int b)
 {
-       g_assert(has_wpcap);
-       return p_pcap_open_live(a, b, c, d, e);
+    if (!has_wpcap) {
+       return NULL;
+    }
+    return p_bpf_image(a, b);
+}
+#endif
+
+#ifdef HAVE_PCAP_REMOTE
+pcap_t*
+pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
+{
+    if (!has_wpcap) {
+       return NULL;
+    }
+    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)
@@ -277,7 +421,7 @@ pcap_freealldevs(pcap_if_t *a)
 }
 #endif
 
-#if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
+#if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
 /*
  * Table of DLT_ types, names, and descriptions, for use if the version
  * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
@@ -370,10 +514,13 @@ static struct dlt_choice dlt_choices[] = {
 #endif
 #ifdef DLT_HDLC
        DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
+#endif
+#ifdef DLT_PPI
+       DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
 #endif
        DLT_CHOICE_SENTINEL
 };
-#endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
+#endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION */
 
 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
 int
@@ -390,7 +537,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;
                }
@@ -399,6 +546,34 @@ pcap_datalink_name_to_val(const char *name)
 }
 #endif
 
+#ifdef HAVE_PCAP_LIST_DATALINKS
+int
+pcap_list_datalinks(pcap_t *p, int **ddlt)
+{
+       g_assert(has_wpcap);
+       return p_pcap_list_datalinks(p, ddlt);
+}
+#endif
+
+#ifdef HAVE_PCAP_FREE_DATALINKS
+void
+pcap_free_datalinks(int *ddlt)
+{
+       g_assert(has_wpcap);
+
+       /*
+        * If we don't have pcap_free_datalinks() in WinPcap,
+        * we don't free the memory - we can't use free(), as
+        * we might not have been built with the same version
+        * of the C runtime library as WinPcap was, and, if we're
+        * not, free() isn't guaranteed to work on something
+        * allocated by WinPcap.
+        */
+       if (p_pcap_free_datalinks != NULL)
+               p_pcap_free_datalinks(ddlt);
+}
+#endif
+
 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
 const char *
 pcap_datalink_val_to_name(int dlt)
@@ -422,6 +597,36 @@ pcap_datalink_val_to_name(int dlt)
 }
 #endif
 
+#ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
+const char *
+pcap_datalink_val_to_description(int dlt)
+{
+       int i;
+
+       g_assert(has_wpcap);
+
+       if (p_pcap_datalink_val_to_description != NULL)
+               return p_pcap_datalink_val_to_description(dlt);
+       else {
+               /*
+                * We don't have it in WinPcap; do it ourselves.
+                */
+               for (i = 0; dlt_choices[i].name != NULL; i++) {
+                       if (dlt_choices[i].dlt == dlt)
+                               return (dlt_choices[i].description);
+               }
+               return NULL;
+       }
+}
+#endif
+
+#ifdef HAVE_PCAP_BREAKLOOP
+void pcap_breakloop(pcap_t *a)
+{
+       p_pcap_breakloop(a);
+}
+#endif
+
 /* setbuff is win32 specific! */
 int pcap_setbuff(pcap_t *a, int b)
 {
@@ -437,12 +642,43 @@ 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)
 {
        GList  *il = NULL;
        wchar_t *names;
@@ -450,6 +686,7 @@ get_interface_list(int *err, char *err_str)
        char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
        char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
        int i, j;
+       char errbuf[PCAP_ERRBUF_SIZE];
 
 #ifdef HAVE_PCAP_FINDALLDEVS
        if (p_pcap_findalldevs != NULL)
@@ -499,7 +736,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) {
@@ -586,6 +823,8 @@ get_interface_list(int *err, char *err_str)
                 * No interfaces found.
                 */
                *err = NO_INTERFACES_FOUND;
+               if (err_str != NULL)
+                       *err_str = NULL;
        }
 
        return il;
@@ -634,14 +873,14 @@ get_runtime_pcap_version(GString *str)
         * not and, if we have it and we have "pcap_lib_version()",
         * what version we have.
         */
-       GModule *handle;                /* handle returned by dlopen */
+       GModule *handle;                /* handle returned by ws_module_open */
        static gchar *packetVer;
        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
@@ -658,7 +897,7 @@ get_runtime_pcap_version(GString *str)
                         */
                        if (packetVer == NULL) {
                                packetVer = "version unknown";
-                               handle = g_module_open("Packet.dll", 0);
+                               handle = ws_module_open("packet.dll", 0);
                                if (handle != NULL) {
                                        if (g_module_symbol(handle,
                                            "PacketLibraryVersion",
@@ -673,11 +912,10 @@ 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");
-       g_string_append(str, " ");
 }
 
 #else /* HAVE_LIBPCAP */