From a6bce8883c0f9dd7fee0eb03667f57b40b1d9dab Mon Sep 17 00:00:00 2001 From: fenner Date: Tue, 3 Apr 2001 04:18:09 +0000 Subject: Virgin import of tcpdump.org libpcap v0.6.2 --- contrib/libpcap/pcap-snoop.c | 116 +++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 26 deletions(-) (limited to 'contrib/libpcap/pcap-snoop.c') diff --git a/contrib/libpcap/pcap-snoop.c b/contrib/libpcap/pcap-snoop.c index 1999c1b..65ad273 100644 --- a/contrib/libpcap/pcap-snoop.c +++ b/contrib/libpcap/pcap-snoop.c @@ -20,7 +20,11 @@ */ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.20.1.1 1999/10/07 23:46:40 mcr Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.30 2000/10/28 00:01:30 guy Exp $ (LBL)"; +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include @@ -50,7 +54,6 @@ static const char rcsid[] = #include "pcap-int.h" -#include "gnuc.h" #ifdef HAVE_OS_PROTO_H #include "os-proto.h" #endif @@ -76,7 +79,8 @@ again: case EWOULDBLOCK: return (0); /* XXX */ } - sprintf(p->errbuf, "read: %s", pcap_strerror(errno)); + snprintf(p->errbuf, sizeof(p->errbuf), + "read: %s", pcap_strerror(errno)); return (-1); } sh = (struct snoopheader *)p->buffer; @@ -104,9 +108,10 @@ pcap_stats(pcap_t *p, struct pcap_stat *ps) struct rawstats rawstats; rs = &rawstats; - bzero((char *)rs, sizeof(*rs)); + memset(rs, 0, sizeof(*rs)); if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) { - sprintf(p->errbuf, "SIOCRAWSTATS: %s", pcap_strerror(errno)); + snprintf(p->errbuf, sizeof(p->errbuf), + "SIOCRAWSTATS: %s", pcap_strerror(errno)); return (-1); } @@ -126,44 +131,41 @@ pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) struct sockaddr_raw sr; struct snoopfilter sf; u_int v; + int ll_hdrlen; + int snooplen; pcap_t *p; + struct ifreq ifr; p = (pcap_t *)malloc(sizeof(*p)); if (p == NULL) { - sprintf(ebuf, "malloc: %s", pcap_strerror(errno)); + snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", + pcap_strerror(errno)); return (NULL); } - bzero((char *)p, sizeof(*p)); + memset(p, 0, sizeof(*p)); fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP); if (fd < 0) { - sprintf(ebuf, "snoop socket: %s", pcap_strerror(errno)); + snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop socket: %s", + pcap_strerror(errno)); goto bad; } p->fd = fd; - bzero((char *)&sr, sizeof(sr)); + memset(&sr, 0, sizeof(sr)); sr.sr_family = AF_RAW; (void)strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname)); if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) { - sprintf(ebuf, "snoop bind: %s", pcap_strerror(errno)); + snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop bind: %s", + pcap_strerror(errno)); goto bad; } - bzero((char *)&sf, sizeof(sf)); + memset(&sf, 0, sizeof(sf)); if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) { - sprintf(ebuf, "SIOCADDSNOOP: %s", pcap_strerror(errno)); + snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCADDSNOOP: %s", + pcap_strerror(errno)); goto bad; } v = 64 * 1024; (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v)); - if (ioctl(fd, SIOCSNOOPLEN, &snaplen) < 0) { - sprintf(ebuf, "SIOCSNOOPLEN: %s", pcap_strerror(errno)); - goto bad; - } - p->snapshot = snaplen; - v = 1; - if (ioctl(fd, SIOCSNOOPING, &v) < 0) { - sprintf(ebuf, "SIOCSNOOPING: %s", pcap_strerror(errno)); - goto bad; - } /* * XXX hack - map device name to link layer type */ @@ -176,27 +178,88 @@ pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) strncmp("ep", device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */ strncmp("vfe", device, 3) == 0 || /* Challenge VME 100Mbit */ strncmp("fa", device, 2) == 0 || - strncmp("qaa", device, 3) == 0) { + strncmp("qaa", device, 3) == 0 || + strncmp("el", device, 2) == 0) { p->linktype = DLT_EN10MB; p->offset = RAW_HDRPAD(sizeof(struct ether_header)); + ll_hdrlen = sizeof(struct ether_header); } else if (strncmp("ipg", device, 3) == 0 || strncmp("rns", device, 3) == 0 || /* O2/200/2000 FDDI */ strncmp("xpi", device, 3) == 0) { p->linktype = DLT_FDDI; p->offset = 3; /* XXX yeah? */ + ll_hdrlen = 13; } else if (strncmp("ppp", device, 3) == 0) { p->linktype = DLT_RAW; + ll_hdrlen = 0; /* DLT_RAW meaning "no PPP header, just the IP packet"? */ } else if (strncmp("lo", device, 2) == 0) { p->linktype = DLT_NULL; + ll_hdrlen = 4; /* is this just like BSD's loopback device? */ } else { - sprintf(ebuf, "snoop: unknown physical layer type"); + snprintf(ebuf, PCAP_ERRBUF_SIZE, + "snoop: unknown physical layer type"); + goto bad; + } +#ifdef SIOCGIFMTU + /* + * XXX - IRIX appears to give you an error if you try to set the + * capture length to be greater than the MTU, so let's try to get + * the MTU first and, if that succeeds, trim the snap length + * to be no greater than the MTU. + */ + (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); + if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCGIFMTU: %s", + pcap_strerror(errno)); + goto bad; + } + /* + * OK, we got it. + * + * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an + * "ifru_metric" member of the "ifr_ifru" union in an "ifreq" + * structure, others don't. + * + * I've no idea what's going on, so, if "ifr_mtu" isn't defined, + * we define it as "ifr_metric", as using that field appears to + * work on the versions that lack "ifr_mtu" (and, on those that + * don't lack it, "ifru_metric" and "ifru_mtu" are both "int" + * members of the "ifr_ifru" union, which suggests that they + * may be interchangeable in this case). + */ +#ifndef ifr_mtu +#define ifr_mtu ifr_metric +#endif + if (snaplen > ifr.ifr_mtu) + snaplen = ifr.ifr_mtu; +#endif + + /* + * The argument to SIOCSNOOPLEN is the number of link-layer + * payload bytes to capture - it doesn't count link-layer + * header bytes. + */ + snooplen = snaplen - ll_hdrlen; + if (snooplen < 0) + snooplen = 0; + if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPLEN: %s", + pcap_strerror(errno)); + goto bad; + } + p->snapshot = snaplen; + v = 1; + if (ioctl(fd, SIOCSNOOPING, &v) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPING: %s", + pcap_strerror(errno)); goto bad; } p->bufsize = 4096; /* XXX */ p->buffer = (u_char *)malloc(p->bufsize); if (p->buffer == NULL) { - sprintf(ebuf, "malloc: %s", pcap_strerror(errno)); + snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", + pcap_strerror(errno)); goto bad; } @@ -211,6 +274,7 @@ int pcap_setfilter(pcap_t *p, struct bpf_program *fp) { - p->fcode = *fp; + if (install_bpf_program(p, fp) < 0) + return (-1); return (0); } -- cgit v1.1