Default enable DSP.
[obnox/wireshark/wip.git] / capture_wpcap_packet.c
index a16535e11ee711a096feaccd6f645ccaacc49042..c2668724dd0212e3b34fe2cd35e4449c8f0a74e7 100644 (file)
@@ -1,13 +1,13 @@
 /* capture_wpcap_packet.c
  * 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
 #include <glib.h>
 #include <gmodule.h>
 
+#include <pcap.h>
+
 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
 #include "tools/lemon/cppmagic.h"
 
 #include <epan/value_string.h>
 
-#include <Packet32.h>
-#include <windows.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"
 
+/* 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;
+
+/*
+ * Desired design of maximum size and alignment
+ */
+#define ETH_SS_MAXSIZE    128  /* Implementation specific max size */
+#define ETH_SS_ALIGNSIZE  (sizeof (gint64 /*int64_t*/))
+                         /* Implementation specific desired alignment */
+/*
+ * Definitions used for sockaddr_storage structure paddings design.
+ */
+#define ETH_SS_PAD1SIZE   (ETH_SS_ALIGNSIZE - sizeof (eth_sa_family_t))
+#define ETH_SS_PAD2SIZE   (ETH_SS_MAXSIZE - (sizeof (eth_sa_family_t) + \
+                              ETH_SS_PAD1SIZE + ETH_SS_ALIGNSIZE))
+
+struct sockaddr_storage {
+    eth_sa_family_t  __ss_family;     /* address family */
+    /* Following fields are implementation specific */
+    char      __ss_pad1[ETH_SS_PAD1SIZE];
+              /* 6 byte pad, this is to make implementation */
+              /* specific pad up to alignment field that */
+              /* follows explicit in the data structure */
+    gint64 /*int64_t*/   __ss_align;     /* field to force desired structure */
+               /* storage alignment */
+    char      __ss_pad2[ETH_SS_PAD2SIZE];
+              /* 112 byte pad to achieve desired size, */
+              /* _SS_MAXSIZE value minus size of ss_family */
+              /* __ss_pad1, __ss_align fields is 112 */
+};
+/* ... copied from RFC2553 */
+#endif /* WINVER */
+
+#include <Packet32.h>
+
 gboolean has_wpacket = FALSE;
 
 
@@ -79,6 +128,7 @@ gboolean has_wpacket = FALSE;
 /******************************************************************************************************************************/
 /* stuff to load WinPcap's packet.dll and the functions required from it */
 
+static PCHAR     (*p_PacketGetVersion) (void);
 static LPADAPTER (*p_PacketOpenAdapter) (char *adaptername);
 static void      (*p_PacketCloseAdapter) (LPADAPTER);
 static int       (*p_PacketRequest) (LPADAPTER, int, void *);
@@ -97,9 +147,10 @@ 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, TRUE),
+               SYM(PacketRequest, FALSE),
                { NULL, NULL, FALSE }
        };
 
@@ -131,7 +182,6 @@ wpcap_packet_load(void)
                sym++;
        }
 
-
        has_wpacket = TRUE;
 }
 
@@ -141,12 +191,24 @@ wpcap_packet_load(void)
 /* functions to access the NDIS driver values */
 
 
+/* get dll version */
+char *
+wpcap_packet_get_version(void)
+{
+    if(!has_wpacket) {
+        return NULL;
+    }
+    return p_PacketGetVersion();
+}
+
+
 /* open the interface */
-LPADAPTER
+void *
 wpcap_packet_open(char *if_name)
 {
-    LPADAPTER adapter;
+    LPADAPTER   adapter;
 
+       g_assert(has_wpacket);
     adapter = p_PacketOpenAdapter(if_name);
 
     return adapter;
@@ -155,16 +217,17 @@ wpcap_packet_open(char *if_name)
 
 /* close the interface */
 void
-wpcap_packet_close(LPADAPTER adapter)
+wpcap_packet_close(void *adapter)
 {
 
+       g_assert(has_wpacket);
     p_PacketCloseAdapter(adapter);
 }
 
 
 /* do a packet request call */
 int 
-wpcap_packet_request(LPADAPTER a, ULONG Oid, int set, char *value, unsigned int *length)
+wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned int *length)
 {
     BOOLEAN    Status;
     ULONG      IoCtlBufferLength=(sizeof(PACKET_OID_DATA) + (*length) - 1);
@@ -178,21 +241,29 @@ wpcap_packet_request(LPADAPTER a, ULONG Oid, int set, char *value, unsigned int
         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;
     }
 
     OidData->Oid = Oid;
     OidData->Length = *length;
+    memcpy(OidData->Data, value, *length);
 
-    Status = p_PacketRequest(a, set, OidData);
+    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);
@@ -207,15 +278,14 @@ wpcap_packet_request(LPADAPTER a, ULONG Oid, int set, char *value, unsigned int
 
 /* get an UINT value using the packet request call */
 int
-wpcap_packet_request_uint(LPADAPTER a, ULONG Oid, UINT *value)
+wpcap_packet_request_uint(void *adapter, ULONG Oid, UINT *value)
 {
     BOOLEAN     Status;
     int         length = sizeof(UINT);
 
 
-    Status = wpcap_packet_request(a, Oid, FALSE /* !set */, (char *) value, &length);
-    if(Status) {
-        g_assert(length == sizeof(UINT));
+    Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
+    if(Status && length == sizeof(UINT)) {
         return 1;
     } else {
         return 0;
@@ -225,15 +295,14 @@ wpcap_packet_request_uint(LPADAPTER a, ULONG Oid, UINT *value)
 
 /* get an ULONG value using the NDIS packet request call */
 int
-wpcap_packet_request_ulong(LPADAPTER a, ULONG Oid, ULONG *value)
+wpcap_packet_request_ulong(void *adapter, ULONG Oid, ULONG *value)
 {
     BOOLEAN     Status;
     int         length = sizeof(ULONG);
 
 
-    Status = wpcap_packet_request(a, Oid, FALSE /* !set */, (char *) value, &length);
-    if(Status) {
-        g_assert(length == sizeof(ULONG));
+    Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
+    if(Status && length == sizeof(ULONG)) {
         return 1;
     } else {
         return 0;
@@ -250,3 +319,16 @@ wpcap_packet_load(void)
 }
 
 #endif /* HAVE_LIBPCAP */
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set shiftwidth=4 tabstop=4 noexpandtab
+ * :indentSize=4:tabSize=4:noTabs=false:
+ */