From Edgar Gladkich:
[obnox/wireshark/wip.git] / capture_wpcap_packet.c
index d72cec8f3cd94310545ec7a94d6d7ebb924fb992..39060e8639d5c416b667d2b3885398c06f3c62eb 100644 (file)
@@ -1,13 +1,13 @@
 /* capture_wpcap_packet.c
- * WinPcap-specific interfaces for low-level information (packet.dll).  
+ * WinPcap-specific interfaces for low-level information (packet.dll).
  * 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
@@ -34,7 +34,6 @@
 #include <glib.h>
 #include <gmodule.h>
 
-#include <wtap.h>
 #include <pcap.h>
 
 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
 
 #include <epan/value_string.h>
 
+#include <winsock2.h>    /* Needed here to force a definition of WINVER           */
+                         /* for some (all ?) Microsoft compilers newer than vc6.  */
+                         /* (If windows.h were used instead, there might be       */
+                         /*  issues re winsock.h included before winsock2.h )     */
+#include <windowsx.h>
+#include <Ntddndis.h>
 
+#include "capture_wpcap_packet.h"
 
-#ifndef HAVE_SOCKADDR_STORAGE
-/* packet32.h requires sockaddr_storage (usually defined in Platform SDK)
+/* packet32.h requires sockaddr_storage
+ * whether sockaddr_storage is defined or not depends on the Platform SDK
+ * version installed. The only one not defining it is the SDK that comes
+ * with MSVC 6.0 (WINVER 0x0400).
+ *
  * copied from RFC2553 (and slightly modified because of datatypes) ...
  * XXX - defined more than once, move this to a header file */
+#ifndef WINVER
+#error WINVER not defined ....
+#endif
+#if (WINVER <= 0x0400) && defined(_MSC_VER)
 typedef unsigned short eth_sa_family_t;
 
 /*
@@ -78,24 +91,18 @@ struct sockaddr_storage {
               /* __ss_pad1, __ss_align fields is 112 */
 };
 /* ... copied from RFC2553 */
-#endif
-
+#endif /* WINVER */
 
 #include <Packet32.h>
-#include <windows.h>
-#include <windowsx.h>
-#include <Ntddndis.h>
-
-#include "capture_wpcap_packet.h"
 
 gboolean has_wpacket = FALSE;
 
 
-/* This module will use the PacketRequest function in packet.dll (coming with WinPcap) to "directly" access 
+/* This module will use the PacketRequest function in packet.dll (coming with WinPcap) to "directly" access
  * the Win32 NDIS network driver(s) and ask for various values (status, statistics, ...).
  *
- * Unfortunately, the definitions required for this are not available through the usual windows header files, 
- * but require the Windows "Device Driver Kit" which is not available for free :-( 
+ * Unfortunately, the definitions required for this are not available through the usual windows header files,
+ * but require the Windows "Device Driver Kit" which is not available for free :-(
  *
  * Fortunately, the definitions needed to access the various NDIS values are available from various OSS projects:
  * - WinPcap in Ntddndis.h
@@ -127,55 +134,55 @@ static void      (*p_PacketCloseAdapter) (LPADAPTER);
 static int       (*p_PacketRequest) (LPADAPTER, int, void *);
 
 typedef struct {
-       const char      *name;
-       gpointer        *ptr;
-       gboolean        optional;
+    const char  *name;
+    gpointer    *ptr;
+    gboolean    optional;
 } symbol_table_t;
 
-#define SYM(x, y)      { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
+#define SYM(x, y)   { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
 
 void
 wpcap_packet_load(void)
 {
 
-       /* These are the symbols I need or want from packet.dll */
-       static const symbol_table_t     symbols[] = {
-               SYM(PacketGetVersion, FALSE),
-               SYM(PacketOpenAdapter, FALSE),
-               SYM(PacketCloseAdapter, FALSE),
-               SYM(PacketRequest, FALSE),
-               { NULL, NULL, FALSE }
-       };
-
-       GModule         *wh; /* wpcap handle */
-       const symbol_table_t    *sym;
-
-       wh = g_module_open("packet", 0);
-
-       if (!wh) {
-               return;
-       }
-
-       sym = symbols;
-       while (sym->name) {
-               if (!g_module_symbol(wh, sym->name, sym->ptr)) {
-                       if (sym->optional) {
-                               /*
-                                * We don't care if it's missing; we just
-                                * don't use it.
-                                */
-                               *sym->ptr = NULL;
-                       } else {
-                               /*
-                                * We require this symbol.
-                                */
-                               return;
-                       }
-               }
-               sym++;
-       }
-
-       has_wpacket = TRUE;
+    /* These are the symbols I need or want from packet.dll */
+    static const symbol_table_t symbols[] = {
+        SYM(PacketGetVersion, FALSE),
+        SYM(PacketOpenAdapter, FALSE),
+        SYM(PacketCloseAdapter, FALSE),
+        SYM(PacketRequest, FALSE),
+        { NULL, NULL, FALSE }
+    };
+
+    GModule     *wh; /* wpcap handle */
+    const symbol_table_t    *sym;
+
+    wh = g_module_open("packet", 0);
+
+    if (!wh) {
+        return;
+    }
+
+    sym = symbols;
+    while (sym->name) {
+        if (!g_module_symbol(wh, sym->name, sym->ptr)) {
+            if (sym->optional) {
+                /*
+                 * We don't care if it's missing; we just
+                 * don't use it.
+                 */
+                *sym->ptr = NULL;
+            } else {
+                /*
+                 * We require this symbol.
+                 */
+                return;
+            }
+        }
+        sym++;
+    }
+
+    has_wpacket = TRUE;
 }
 
 
@@ -201,7 +208,7 @@ wpcap_packet_open(char *if_name)
 {
     LPADAPTER   adapter;
 
-       g_assert(has_wpacket);
+    g_assert(has_wpacket);
     adapter = p_PacketOpenAdapter(if_name);
 
     return adapter;
@@ -213,13 +220,13 @@ void
 wpcap_packet_close(void *adapter)
 {
 
-       g_assert(has_wpacket);
+    g_assert(has_wpacket);
     p_PacketCloseAdapter(adapter);
 }
 
 
 /* do a packet request call */
-int 
+int
 wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned int *length)
 {
     BOOLEAN    Status;
@@ -227,16 +234,17 @@ wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned in
     PPACKET_OID_DATA  OidData;
 
 
-       g_assert(has_wpacket);
+    g_assert(has_wpacket);
 
     if(p_PacketRequest == NULL) {
         g_warning("packet_request not available\n");
         return 0;
     }
 
+    /* get a buffer suitable for PacketRequest() */
     OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,IoCtlBufferLength);
     if (OidData == NULL) {
-        g_warning("packet_link_status failed\n");
+        g_warning("GlobalAllocPtr failed for %u\n", IoCtlBufferLength);
         return 0;
     }
 
@@ -247,9 +255,15 @@ wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned in
     Status = p_PacketRequest(adapter, set, OidData);
 
     if(Status) {
-        g_assert(OidData->Length <= *length);
-        memcpy(value, OidData->Data, OidData->Length);
-        *length = OidData->Length;
+        if(OidData->Length <= *length) {
+            /* copy value from driver */
+            memcpy(value, OidData->Data, OidData->Length);
+            *length = OidData->Length;
+        } else {
+            /* the driver returned a value that is longer than expected (and longer than the given buffer) */
+            g_warning("returned oid too long, Oid: 0x%x OidLen:%u MaxLen:%u", Oid, OidData->Length, *length);
+            Status = FALSE;
+        }
     }
 
     GlobalFreePtr (OidData);
@@ -301,7 +315,20 @@ wpcap_packet_request_ulong(void *adapter, ULONG Oid, ULONG *value)
 void
 wpcap_packet_load(void)
 {
-       return;
+    return;
 }
 
 #endif /* HAVE_LIBPCAP */
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * ex: set shiftwidth=4 tabstop=8 expandtab
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */