]> The Tcpdump Group git mirrors - libpcap/commitdiff
Make the buffer member of a pcap_t a void *.
authorGuy Harris <[email protected]>
Sun, 9 Aug 2015 23:01:40 +0000 (16:01 -0700)
committerGuy Harris <[email protected]>
Sun, 9 Aug 2015 23:01:40 +0000 (16:01 -0700)
Yes, in some sense, it's an array of bytes - on modern processors, *all*
data is ultimately an array of bytes - but different modules will use it
in different ways, not all of which will be an undifferentiated array of
bytes.

This squelches a complaint from the Clang static analyzer.

Clean up some code while we're at it.

18 files changed:
dlpisubs.c
pcap-bpf.c
pcap-bt-linux.c
pcap-bt-monitor-linux.c
pcap-can-linux.c
pcap-dlpi.c
pcap-int.h
pcap-libdlpi.c
pcap-linux.c
pcap-netfilter-linux.c
pcap-nit.c
pcap-pf.c
pcap-sita.c
pcap-snit.c
pcap-snoop.c
pcap-usb-linux.c
pcap-win32.c
sf-pcap-ng.c

index 131fa279d843534cdef0bcecb3222c903cfcd814..fd2d61c019428eb4d445459c2985b93a8e82ba11 100644 (file)
@@ -326,7 +326,7 @@ int
 pcap_alloc_databuf(pcap_t *p)
 {
        p->bufsize = PKTBUFSIZE;
-       p->buffer = (u_char *)malloc(p->bufsize + p->offset);
+       p->buffer = malloc(p->bufsize + p->offset);
        if (p->buffer == NULL) {
                strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                return (-1);
index 98140f22a03ecc8e613f54837cf4791a0133e9ba..5b863b2ba6c82f4693f7d7644e7734cd4838f642 100644 (file)
@@ -866,7 +866,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                } else
 #endif
                {
-                       cc = read(p->fd, (char *)p->buffer, p->bufsize);
+                       cc = read(p->fd, p->buffer, p->bufsize);
                }
                if (cc < 0) {
                        /* Don't choke when we get ptraced */
@@ -937,7 +937,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                            pcap_strerror(errno));
                        return (PCAP_ERROR);
                }
-               bp = p->buffer;
+               bp = (u_char *)p->buffer;
        } else
                bp = p->bp;
 
@@ -2209,7 +2209,7 @@ pcap_activate_bpf(pcap_t *p)
 #ifdef HAVE_ZEROCOPY_BPF
        if (!pb->zerocopy) {
 #endif
-       p->buffer = (u_char *)malloc(p->bufsize);
+       p->buffer = malloc(p->bufsize);
        if (p->buffer == NULL) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
                    pcap_strerror(errno));
index 56df6876766b5ed0c29ea638e3d03bfe7117f53d..ebdf1249128e1e0fcedb0f701c04d274f83cd1d5 100644 (file)
@@ -199,8 +199,7 @@ bt_activate(pcap_t* handle)
        }
 
        /* Initialize some components of the pcap structure. */
-       handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
-       handle->offset = BT_CTRL_SIZE;
+       handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
        handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
 
        handle->read_op = bt_read_linux;
@@ -305,16 +304,18 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us
        ssize_t ret;
        struct pcap_pkthdr pkth;
        pcap_bluetooth_h4_header* bthdr;
+       char *pktd;
 
-       bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
-       iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)];
+       pktd = (char *)handle->buffer + BT_CTRL_SIZE;
+       bthdr = (pcap_bluetooth_h4_header*)(void *)pktd;
+       iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header);
        iv.iov_len  = handle->snapshot;
 
        memset(&msg, 0, sizeof(msg));
        msg.msg_iov = &iv;
        msg.msg_iovlen = 1;
        msg.msg_control = handle->buffer;
-       msg.msg_controllen = handle->offset;
+       msg.msg_controllen = BT_CTRL_SIZE;
 
        /* ignore interrupt system call error */
        do {
@@ -357,9 +358,8 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us
        pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
        pkth.len = pkth.caplen;
        if (handle->fcode.bf_insns == NULL ||
-           bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
-             pkth.len, pkth.caplen)) {
-               callback(user, &pkth, &handle->buffer[handle->offset]);
+           bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
+               callback(user, &pkth, pktd);
                return 1;
        }
        return 0;       /* didn't pass filter */
index b18bdcaed84f163edd697b89cbb8c5d7ba226d07..b73d08b82c5726175ca7ea35feb6dcf1488c8e7a 100644 (file)
@@ -82,13 +82,15 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch
     ssize_t ret;
     struct pcap_pkthdr pkth;
     pcap_bluetooth_linux_monitor_header *bthdr;
+    char *pktd;
     struct hci_mon_hdr hdr;
 
-    bthdr = (pcap_bluetooth_linux_monitor_header*) &handle->buffer[handle->offset];
+    pktd = (char *)handle->buffer + BT_CONTROL_SIZE;
+    bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
 
     iv[0].iov_base = &hdr;
     iv[0].iov_len = sizeof(hdr);
-    iv[1].iov_base = &handle->buffer[handle->offset + sizeof(pcap_bluetooth_linux_monitor_header)];
+    iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
     iv[1].iov_len = handle->snapshot;
 
     memset(&pkth.ts, 0, sizeof(pkth.ts));
@@ -96,7 +98,7 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch
     msg.msg_iov = iv;
     msg.msg_iovlen = 2;
     msg.msg_control = handle->buffer;
-    msg.msg_controllen = handle->offset;
+    msg.msg_controllen = BT_CONTROL_SIZE;
 
     do {
         ret = recvmsg(handle->fd, &msg, 0);
@@ -128,9 +130,8 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch
     bthdr->opcode = htons(hdr.opcode);
 
     if (handle->fcode.bf_insns == NULL ||
-        bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
-          pkth.len, pkth.caplen)) {
-        callback(user, &pkth, &handle->buffer[handle->offset]);
+        bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
+        callback(user, &pkth, pktd);
         return 1;
     }
     return 0;   /* didn't pass filter */
@@ -172,8 +173,7 @@ bt_monitor_activate(pcap_t* handle)
         return PCAP_ERROR_RFMON_NOTSUP;
     }
 
-    handle->bufsize = handle->snapshot + BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header);
-    handle->offset = BT_CONTROL_SIZE;
+    handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
 
     handle->read_op = bt_monitor_read;
index a8e1e355ef5ec6cccf9e1652fc35846225124e0c..f1d2d3b7ef5aac854d8f7c0090648a71f1555487 100644 (file)
@@ -39,6 +39,8 @@
 #include "pcap-int.h"
 #include "pcap-can-linux.h"
 
+#define CAN_CONTROL_SIZE 8
+
 #ifdef NEED_STRERROR_H
 #include "strerror.h"
 #endif
@@ -148,8 +150,7 @@ can_activate(pcap_t* handle)
        struct ifreq ifr;
 
        /* Initialize some components of the pcap structure. */
-       handle->bufsize = 24;
-       handle->offset = 8;
+       handle->bufsize = CAN_CONTROL_SIZE + 16;
        handle->linktype = DLT_CAN_SOCKETCAN;
        handle->read_op = can_read_linux;
        handle->inject_op = can_inject_linux;
@@ -221,17 +222,19 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u
 {
        struct msghdr msg;
        struct pcap_pkthdr pkth;
+       char *pktd;
        struct iovec iv;
        struct can_frame* cf;
 
-       iv.iov_base = &handle->buffer[handle->offset];
+       pktd = (char *)handle->buffer + CAN_CONTROL_SIZE;
+       iv.iov_base = pktd;
        iv.iov_len = handle->snapshot;
 
        memset(&msg, 0, sizeof(msg));
        msg.msg_iov = &iv;
        msg.msg_iovlen = 1;
        msg.msg_control = handle->buffer;
-       msg.msg_controllen = handle->offset;
+       msg.msg_controllen = CAN_CONTROL_SIZE;
 
        do
        {
@@ -251,8 +254,8 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u
        }
 
        /* adjust capture len according to frame len */
-       cf = (struct can_frame*)&handle->buffer[8];
-       pkth.caplen -= 8 - cf->can_dlc;
+       cf = (struct can_frame*)(void *)pktd;
+       pkth.caplen -= CAN_CONTROL_SIZE - cf->can_dlc;
        pkth.len = pkth.caplen;
 
        cf->can_id = htonl( cf->can_id );
@@ -264,7 +267,7 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u
                return -1;
        }
 
-       callback(user, &pkth, &handle->buffer[8]);
+       callback(user, &pkth, pktd);
 
        return 1;
 }
index c0071352674bd3fec072a92c7c0b99f54e641e38..254ca43bac8cdde8f91860c4467c6c05fe349d5d 100644 (file)
@@ -230,7 +230,7 @@ pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                        }
                        cc = data.len;
                } while (cc == 0);
-               bp = p->buffer + p->offset;
+               bp = (u_char *)p->buffer + p->offset;
        } else
                bp = p->bp;
 
index 2f71e1152676aa8c949b3aff06dea27a2ad6697e..bae3ee95d77efc3065a4a812bf84afe51609640c 100644 (file)
@@ -162,7 +162,7 @@ struct pcap {
         * Read buffer.
         */
        int bufsize;
-       u_char *buffer;
+       void *buffer;
        u_char *bp;
        int cc;
 
index 333e532b765a99fbe2413b6307763c8977cd1c51..899b07bf960dccf35aed84d24b6ba221ed3be2f8 100644 (file)
@@ -337,7 +337,7 @@ pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
                }
 
                msglen = p->bufsize;
-               bufp = p->buffer + p->offset;
+               bufp = (u_char *)p->buffer + p->offset;
 
                retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
                    &msglen, -1, NULL);
index 6ef9e75632c105ba4c88980069d25dff3923ffb5..1f975370c01014385755117b1a2c1bd0a4e7376c 100644 (file)
@@ -1715,7 +1715,7 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
         * if we're using a memory-mapped buffer, we won't even
         * get notified of "network down" events.
         */
-       bp = handle->buffer + handle->offset;
+       bp = (u_char *)handle->buffer + handle->offset;
 
 #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI)
        msg.msg_name            = &from;
index 3ee6faa3b7b8af0c28d0884ec6266465c4f2b70d..4dda9a7932e948504e7a7c3dd097ace1fe8a3ec0 100644 (file)
@@ -105,7 +105,7 @@ netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_c
                return -1;
        }
 
-       buf = handle->buffer;
+       buf = (unsigned char *)handle->buffer;
        while (len >= NLMSG_SPACE(0)) {
                const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf;
                u_int32_t msg_len;
index a8355f9d6cf921ef9bb342a292e13f4f542e70d7..36ed7758268e8a6a0d8fa881f19b70ae03cefd3f 100644 (file)
@@ -118,7 +118,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                                pcap_strerror(errno));
                        return (-1);
                }
-               bp = p->buffer;
+               bp = (u_char *)p->buffer;
        } else
                bp = p->bp;
 
@@ -301,7 +301,7 @@ pcap_activate_nit(pcap_t *p)
        p->linktype = DLT_EN10MB;
 
        p->bufsize = BUFSPACE;
-       p->buffer = (u_char *)malloc(p->bufsize);
+       p->buffer = malloc(p->bufsize);
        if (p->buffer == NULL) {
                strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
index e03b2ed1f0f32e50a2730f4b33394c46f03dee3e..d9e9ec7604db25b42e76c6d3af737017746c83be 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -131,7 +131,7 @@ pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
                                pcap_strerror(errno));
                        return (-1);
                }
-               bp = pc->buffer + pc->offset;
+               bp = (u_char *)pc->buffer + pc->offset;
        } else
                bp = pc->bp;
        /*
@@ -476,7 +476,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
        }
 
        p->bufsize = BUFSPACE;
-       p->buffer = (u_char*)malloc(p->bufsize + p->offset);
+       p->buffer = malloc(p->bufsize + p->offset);
        if (p->buffer == NULL) {
                strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
index bc0428c0270567dd8d5d459ac0ba5854fda1ef26..ffffc92aa9f620e3a680adeba42e245f72a0c2e3 100644 (file)
@@ -951,7 +951,7 @@ static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback,
        pcap_header.caplen              = ntohl(*(uint32_t *)&packet_header[8]);                                /* caplen */
        pcap_header.len                 = ntohl(*(uint32_t *)&packet_header[12]);                               /* len */
 
-       handle->bp = handle->buffer + handle->offset;                                                                   /* start off the receive pointer at the right spot */
+       handle->bp = (u_char *)handle->buffer + handle->offset;                                                                 /* start off the receive pointer at the right spot */
        if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0;  /* then try to read in the rest of the data */
 
        callback(user, &pcap_header, handle->bp);                                                                               /* call the user supplied callback function */
index 0ce7860309d84bd69f4425224d8f2809804f9b87..c6d8a93ff100419a4c299e3d8b9f25052deadfc7 100644 (file)
@@ -134,7 +134,7 @@ pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                                pcap_strerror(errno));
                        return (-1);
                }
-               bp = p->buffer;
+               bp = (u_char *)p->buffer;
        } else
                bp = p->bp;
 
@@ -378,7 +378,7 @@ pcap_activate_snit(pcap_t *p)
        p->linktype = DLT_EN10MB;
 
        p->bufsize = BUFSPACE;
-       p->buffer = (u_char *)malloc(p->bufsize);
+       p->buffer = malloc(p->bufsize);
        if (p->buffer == NULL) {
                strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
index f622f31faad073bf80ee82f24772ac0f86854e4a..10b93aa66dc291abbc6fa30a5fdc596bf524e333 100644 (file)
@@ -371,7 +371,7 @@ pcap_activate_snoop(pcap_t *p)
        }
 
        p->bufsize = 4096;                              /* XXX */
-       p->buffer = (u_char *)malloc(p->bufsize);
+       p->buffer = malloc(p->bufsize);
        if (p->buffer == NULL) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
                    pcap_strerror(errno));
index 957273a87d62bf4d0d9a66e5074464bb445bab5a..aa49eacfbec1b55cdd05518604e07ff880394485 100644 (file)
@@ -789,7 +789,7 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha
 
        /* the usb header is going to be part of 'packet' data*/
        info.hdr = (pcap_usb_header*) handle->buffer;
-       info.data = handle->buffer + sizeof(pcap_usb_header);
+       info.data = (u_char *)handle->buffer + sizeof(pcap_usb_header);
        info.data_len = clen;
 
        /* ignore interrupt system call errors */
index f449f796598871509b0386f3eac7b800b8bd57d3..9b38f3c386e4b4756669edae2d5710db635ffd98 100644 (file)
@@ -663,7 +663,7 @@ pcap_activate_win32(pcap_t *p)
                        goto bad;
                }
 
-               p->buffer = (u_char *)malloc(p->bufsize);
+               p->buffer = malloc(p->bufsize);
                if (p->buffer == NULL)
                {
                        snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
index 45dca11dd47427c4dfd5c6e1344d0517eeea5d82..6579f30a0032ee76ffe7d295d70a40046001282f 100644 (file)
@@ -260,6 +260,8 @@ read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
 {
        int status;
        struct block_header bhdr;
+       u_char *bdata;
+       size_t data_remaining;
 
        status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
        if (status <= 0)
@@ -316,16 +318,16 @@ read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
         * of the block.
         */
        memcpy(p->buffer, &bhdr, sizeof(bhdr));
-       if (read_bytes(fp, p->buffer + sizeof(bhdr),
-           bhdr.total_length - sizeof(bhdr), 1, errbuf) == -1)
+       bdata = (u_char *)p->buffer + sizeof(bhdr);
+       data_remaining = bhdr.total_length - sizeof(bhdr);
+       if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
                return (-1);
 
        /*
         * Initialize the cursor.
         */
-       cursor->data = p->buffer + sizeof(bhdr);
-       cursor->data_remaining = bhdr.total_length - sizeof(bhdr) -
-           sizeof(struct block_trailer);
+       cursor->data = bdata;
+       cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
        cursor->block_type = bhdr.block_type;
        return (1);
 }
@@ -803,12 +805,12 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
         * of the SHB.
         */
        bhdrp = (struct block_header *)p->buffer;
-       shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header));
+       shbp = (struct section_header_block *)((u_char *)p->buffer + sizeof(struct block_header));
        bhdrp->block_type = magic;
        bhdrp->total_length = total_length;
        shbp->byte_order_magic = byte_order_magic;
        if (read_bytes(fp,
-           p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
+           (u_char *)p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
            total_length - (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
            1, errbuf) == -1)
                goto fail;