Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/libpcap/dist Use http://sourceforge.net/p/libpc...



details:   https://anonhg.NetBSD.org/src/rev/1af4cda46c6b
branches:  trunk
changeset: 1008642:1af4cda46c6b
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Mar 29 19:49:26 2020 +0000

description:
Use http://sourceforge.net/p/libpcap/patches/_discuss/thread/fc8f43a0/ad35/\
attachment/rpcap3.patch to provide rpcap support.

diffstat:

 external/bsd/libpcap/dist/pcap-rpcap-unix.c |  766 ++++++++++++++++++++++++++++
 external/bsd/libpcap/dist/pcap-rpcap-unix.h |    1 +
 external/bsd/libpcap/dist/pcap.c            |   11 +-
 3 files changed, 776 insertions(+), 2 deletions(-)

diffs (truncated from 814 to 300 lines):

diff -r 33eee3d4d041 -r 1af4cda46c6b external/bsd/libpcap/dist/pcap-rpcap-unix.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/external/bsd/libpcap/dist/pcap-rpcap-unix.c       Sun Mar 29 19:49:26 2020 +0000
@@ -0,0 +1,766 @@
+/*
+ * Reimplementation of winpcap pcap-remote.c
+ * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
+ * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the Politecnico di Torino, CACE Technologies 
+ * nor the names of its contributors may be used to endorse or promote 
+ * products derived from this software without specific prior written 
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcap-int.h"
+
+#ifdef NEED_STRERROR_H
+#include "strerror.h"
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+#include <time.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <linux/types.h>
+
+#include "pcap-rpcap-unix.h"
+
+#define RPCAP_IFACE "rpcap://"
+
+/* default */
+#define RPCAP_DEFAULT_NETPORT 2002
+
+/* version */
+#define RPCAP_VERSION_EXPERIMENTAL 0
+
+/* packets */
+#define RPCAP_MSG_ERROR 1                              /*!< Message that keeps an error notification */
+#define RPCAP_MSG_OPEN_REQ 3                   /*!< Request to open a remote device */
+#define RPCAP_MSG_STARTCAP_REQ 4               /*!< Request to start a capture on a remote device */
+#define RPCAP_MSG_UPDATEFILTER_REQ 5   /*!< Send a compiled filter into the remote device */
+#define RPCAP_MSG_PACKET 7                             /*!< This is a 'data' message, which carries a network packet */
+#define RPCAP_MSG_AUTH_REQ 8                   /*!< Message that keeps the authentication parameters */
+#define RPCAP_MSG_STATS_REQ 9                  /*!< It requires to have network statistics */
+
+#define RPCAP_UPDATEFILTER_BPF 1                       /*!< This code tells us that the filter is encoded with the BPF/NPF syntax */
+
+static unsigned short
+get16(const unsigned char *buf)
+{
+       unsigned short val;
+
+       val = buf[0];
+       val = val << 8 | buf[1];
+       return val;
+}
+
+static void
+put16(unsigned char *buf, unsigned int val)
+{
+       buf[0] = val >> 8;
+       buf[1] = val >> 0;
+}
+
+static unsigned int
+get32(const unsigned char *buf)
+{
+       unsigned int val;
+
+       val = buf[0];
+       val = val << 8 | buf[1];
+       val = val << 8 | buf[2];
+       val = val << 8 | buf[3];
+       return val;
+}
+
+static void
+put32(unsigned char *buf, unsigned int val)
+{
+       buf[0] = val >> 24;
+       buf[1] = val >> 16;
+       buf[2] = val >> 8;
+       buf[3] = val >> 0;
+}
+
+static int
+rpcap_recv_pkt(pcap_t *p, int fd, char *recv_buf, unsigned int buflen)
+{
+       static char discard[1024];
+
+       size_t mlen;
+       int ret;
+       char *buf;
+       unsigned int len;
+       unsigned int pkt_len;
+
+       char hdr[8];
+       int pkt_type;
+
+/* read header loop */
+       buf = hdr;
+       len = 8;
+
+       ret = 0;
+       do {
+               buf += ret;
+               len -= ret;
+
+               do {
+                       ret = read(fd, buf, len);
+                       if (p->break_loop) {
+                               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "break-loop");
+                               p->break_loop = 0;
+                               return -2;
+                       }
+               } while (ret == -1 && errno == EINTR);
+       } while (ret > 0 && len-ret);
+
+       if (ret <= 0) {
+               if (!ret)
+                       snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection closed");
+               else
+                       snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection error (%s)", strerror(errno));
+               return -1;
+       }
+
+       if (hdr[0] != RPCAP_VERSION_EXPERIMENTAL) {
+               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: incorrect reply version (%.2x)", hdr[0]);
+               return -1;
+       }
+
+       pkt_type = (unsigned char) hdr[1];
+       pkt_len  = get32(&hdr[4]);
+
+       if (pkt_type == RPCAP_MSG_ERROR) {
+               recv_buf = p->errbuf;
+               buflen = PCAP_ERRBUF_SIZE-1;
+       }
+
+       buf = recv_buf;
+
+/* read payload loop */
+       if (pkt_len) {
+               ret = 0;
+               len = pkt_len;
+               do {
+                       buf += ret;
+                       buflen -= ret;
+                       len -= ret;
+
+                       if (!buflen) {
+                               buf = discard;
+                               buflen = sizeof(discard);
+                       }
+
+                       mlen = (len < 0x7fff) ? len : 0x7fff;
+
+                       if (mlen > buflen)
+                               mlen = buflen;
+
+                       do {
+                               ret = read(fd, buf, mlen);
+                               if (p->break_loop) {
+                                       p->break_loop = 0;
+                                       snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "break-loop");
+                                       return -2;
+                               }
+                       } while (ret == -1 && errno == EINTR);
+               } while (ret > 0 && len-ret);
+
+               if (ret <= 0) {
+                       if (!ret)
+                               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection closed");
+                       else
+                               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection error (%s)", strerror(errno));
+                       return -1;
+               }
+               buf += ret;
+       }
+
+       /* always NUL terminate errbuf, and signal error */
+       if (pkt_type == RPCAP_MSG_ERROR) {
+               *buf = '\0';
+               return -1;
+       }
+       return pkt_len;
+}
+
+static int
+rpcap_send_pkt(pcap_t *p, const char *send_buf, unsigned int len)
+{
+       char *buf;
+
+       size_t mlen;
+       int ret;
+
+/* send loop */
+       ret = 0;
+       do {
+               send_buf += ret;
+               len -= ret;
+
+               mlen = (len < 0x7fff) ? len : 0x7fff;
+
+               do {
+                       ret = write(p->fd, send_buf, mlen);
+                       if (p->break_loop) {
+                               p->break_loop = 0;
+                               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "break-loop");
+                               return -2;
+                       }
+               } while (ret == -1 && errno == EINTR);
+       } while (ret > 0 && len-ret);
+
+       if (ret <= 0) {
+               if (!ret)
+                       snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection closed");
+               else
+                       snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: connection error (%s)", strerror(errno));
+               return -1;
+       }
+       return 0;
+}
+
+static int
+rpcap_send_request(pcap_t *p, char type, char *buf, unsigned int payload_len)
+{
+       buf[0] = RPCAP_VERSION_EXPERIMENTAL;
+       buf[1] = type;
+       buf[2] = buf[3] = 0;
+       put32(&buf[4], payload_len);
+
+       return rpcap_send_pkt(p, buf, 8+payload_len);
+}
+
+static int
+rpcap_send_request_auth(pcap_t *p, const char *username, const char *password)
+{
+       int res;
+
+       if (username || password) {
+               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "rpcap: auth not supported (yet!)");
+               return -1;
+
+       } else {
+               static const char login_null_pkt[16] = { 
+                       RPCAP_VERSION_EXPERIMENTAL,
+                       RPCAP_MSG_AUTH_REQ, 
+                       0, 0, 
+                       0, 0, 0, 8,
+
+                       0, 0, 0, 0, 0, 0, 0, 0
+               };
+
+               if (rpcap_send_pkt(p, login_null_pkt, sizeof(login_null_pkt)))
+                       return -1;
+       }
+
+       return rpcap_recv_pkt(p, p->fd, NULL, 0);
+}
+
+static int
+rpcap_send_request_open(pcap_t *p, const char *interface)
+{
+       const size_t interface_len = strlen(interface);
+
+       char buf_open[8+255] = { 



Home | Main Index | Thread Index | Old Index