]> The Tcpdump Group git mirrors - libpcap/commitdiff
Have better APIs for the OID get/set request calls.
authorGuy Harris <[email protected]>
Mon, 14 Sep 2015 00:00:16 +0000 (17:00 -0700)
committerGuy Harris <[email protected]>
Mon, 14 Sep 2015 00:00:16 +0000 (17:00 -0700)
Have them take the OID, the buffer, and the length as arguments, and,
for pcap-win32.c, have the routines for those calls allocate a
PACKET_OID_DATA, fill it in as appropriate, and copy the data back.

Yeah, it's more work and another memory allocation, but it's still a
cleaner API and avoids some compiler warnings.

pcap-int.h
pcap-tc.c
pcap-win32.c
pcap.c
pcap/pcap.h
savefile.c

index b4c080653ba8b561141d437c12c7808d4edb6ac8..655c19d913a63a74230265a7de19f31a2670c40d 100644 (file)
@@ -132,8 +132,8 @@ typedef int (*setbuff_op_t)(pcap_t *, int);
 typedef int    (*setmode_op_t)(pcap_t *, int);
 typedef int    (*setmintocopy_op_t)(pcap_t *, int);
 typedef HANDLE (*getevent_op_t)(pcap_t *);
-typedef int    (*oid_get_request_op_t)(pcap_t *, pcap_oid_data_t *);
-typedef int    (*oid_set_request_op_t)(pcap_t *, pcap_oid_data_t *);
+typedef int    (*oid_get_request_op_t)(pcap_t *, bpf_u_int32, void *, size_t);
+typedef int    (*oid_set_request_op_t)(pcap_t *, bpf_u_int32, const void *, size_t);
 typedef u_int  (*sendqueue_transmit_op_t)(pcap_t *, pcap_send_queue *, int);
 typedef int    (*setuserbuffer_op_t)(pcap_t *, int);
 typedef int    (*live_dump_op_t)(pcap_t *, char *, int, int);
index 2f665f774e0e7ef12950dbafea8ab19c6d368d2f..572f900f9ee41b0e208dc1525dd143a29828b2b2 100644 (file)
--- a/pcap-tc.c
+++ b/pcap-tc.c
@@ -1228,7 +1228,7 @@ TcGetReceiveWaitHandle(pcap_t *p)
 }
 
 static int
-TcOidGetRequest(pcap_t *p, pcap_oid_data_t *data _U_)
+TcOidGetRequest(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_, size_t len _U_
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID get request cannot be performed on a TurboCap device");
@@ -1236,7 +1236,8 @@ TcOidGetRequest(pcap_t *p, pcap_oid_data_t *data _U_)
 }
 
 static int
-TcOidSetRequest(pcap_t *p, pcap_oid_data_t *data _U_)
+TcOidSetRequest(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
+    size_t len _U_)
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID set request cannot be performed on a TurboCap device");
index 2a3b558f76fb361c7122c48d9dedd48d31f9dc0b..aed5a1b87db653a615c5ab58b02b1ab219d68380 100644 (file)
@@ -258,30 +258,82 @@ pcap_getevent_win32(pcap_t *p)
 }
 
 static int
-pcap_oid_get_request_win32(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_get_request_win32(pcap_t *p, bpf_u_int32 oid, void *data, size_t len)
 {
+       PACKET_OID_DATA *oid_data_arg;
        char errbuf[PCAP_ERRBUF_SIZE+1];
 
-       if (!PacketRequest(p->adapter, FALSE, data)) {
+       /*
+        * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
+        * It should be big enough to hold "len" bytes of data; it
+        * will actually be slightly larger, as PACKET_OID_DATA has a
+        * 1-byte data array at the end, standing in for the variable-length
+        * data that's actually there.
+        */
+       oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + len);
+       if (oid_data_arg == NULL) {
+               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
+                   "Couldn't allocate argument buffer for PacketRequest");
+               return PCAP_ERROR);
+       }
+
+       /*
+        * No need to copy the data - we're doing a fetch.
+        */
+       oid_data_arg->oid = oid;
+       oid_data_arg->length = len;
+       if (!PacketRequest(p->adapter, FALSE, oid_data_arg)) {
                pcap_win32_err_to_str(GetLastError(), errbuf);
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "Error calling PacketRequest: %s", errbuf);
+               free(oid_data_arg);
                return PCAP_ERROR;
        }
+
+       /*
+        * Copy back the data we fetched.
+        */
+       memcpy(data, oid_data_arg->data, len);
+       free(oid_data_arg);
        return 0;
 }
 
 static int
-pcap_oid_set_request_win32(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_set_request_win32(pcap_t *p, bpf_u_int32 oid, const void *data,
+    size_t len)
 {
+       PACKET_OID_DATA *oid_data_arg;
        char errbuf[PCAP_ERRBUF_SIZE+1];
 
+       /*
+        * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
+        * It should be big enough to hold "len" bytes of data; it
+        * will actually be slightly larger, as PACKET_OID_DATA has a
+        * 1-byte data array at the end, standing in for the variable-length
+        * data that's actually there.
+        */
+       oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + len);
+       if (oid_data_arg == NULL) {
+               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
+                   "Couldn't allocate argument buffer for PacketRequest");
+               return PCAP_ERROR);
+       }
+
+       oid_data_arg->oid = oid;
+       oid_data_arg->length = len;
+       memcpy(oid_data_arg->data, data, len);
        if (!PacketRequest(p->adapter, TRUE, data)) {
                pcap_win32_err_to_str(GetLastError(), errbuf);
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "Error calling PacketRequest: %s", errbuf);
+               free(oid_data_arg);
                return PCAP_ERROR;
        }
+
+       /*
+        * No need to copy the data - we're doing a set.
+        */
+       free(oid_data_arg);
        return 0;
 }
 
diff --git a/pcap.c b/pcap.c
index ecad19e96e4fe09f6dab5079cc07a5a5aa280f99..a97fc123838269e97d00b06b3454dde801e23ef7 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -1736,13 +1736,14 @@ pcap_getevent_dead(pcap_t *p)
 }
 
 int
-pcap_oid_get_request(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t len)
 {
-       return (p->oid_get_request_op(p, data));
+       return (p->oid_get_request_op(p, oid, data, len));
 }
 
 static int
-pcap_oid_get_request_dead(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
+    size_t len _U_)
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID get request cannot be performed on a pcap_open_dead pcap_t");
@@ -1750,13 +1751,14 @@ pcap_oid_get_request_dead(pcap_t *p, pcap_oid_data_t *data)
 }
 
 int
-pcap_oid_set_request(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t len)
 {
-       return (p->oid_set_request_op(p, data));
+       return (p->oid_set_request_op(p, oid, data, len));
 }
 
 static int
-pcap_oid_set_request_dead(pcap_t *p, pcap_oid_data_t *data)
+pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
+    size_t len _U_)
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID set request cannot be performed on a pcap_open_dead pcap_t");
index d3139550c6034cf841f97bbd43129123cb4e30b7..b4520a7b6bbf936a9f09955536616eb2b0b346ba 100644 (file)
@@ -477,17 +477,8 @@ int pcap_setmintocopy(pcap_t *p, int size);
 
 HANDLE pcap_getevent(pcap_t *p);
 
-/*
- * Same as Packet.dll's PACKET_OID_DATA.
- */
-typedef struct {
-       bpf_u_int32     oid;            /* OID code */
-       bpf_u_int32     length;         /* length of the data field */
-       u_char          data[1];        /* first byte of the data field */
-} pcap_oid_data_t;
-
-int pcap_oid_get_request(pcap_t *p, pcap_oid_data_t *data);
-int pcap_oid_set_request(pcap_t *p, pcap_oid_data_t *data);
+int pcap_oid_get_request(pcap_t *, bpf_u_int32, void *, size_t);
+int pcap_oid_set_request(pcap_t *, bpf_u_int32, const void *, size_t);
 
 pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
 
index 08ef070e681fea0b3a96065b1ec428ef6ab637af..9a04f9a1b1681a62b5fc0177bcb728a4f161b7fb 100644 (file)
@@ -151,7 +151,8 @@ sf_getevent(pcap_t *pcap)
 }
 
 static int
-sf_oid_get_request(pcap_t *p, pcap_oid_data_t *data)
+sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
+    size_t len _U_)
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID get request cannot be performed on a file");
@@ -159,7 +160,8 @@ sf_oid_get_request(pcap_t *p, pcap_oid_data_t *data)
 }
 
 static int
-sf_oid_set_request(pcap_t *p, pcap_oid_data_t *data)
+sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
+    size_t len _U_)
 {
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
            "An OID set request cannot be performed on a file");