X-Git-Url: https://git.tcpdump.org/libpcap/blobdiff_plain/eb6599de5e0bb1a18792c369bb19aea689deb524..cc3ca65d6519faf3a0e4609b5150757c14af36e9:/pcap-nit.c diff --git a/pcap-nit.c b/pcap-nit.c index 5610b293..1b626e21 100644 --- a/pcap-nit.c +++ b/pcap-nit.c @@ -18,10 +18,6 @@ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.62 2008-04-14 20:40:58 guy Exp $ (LBL)"; -#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -81,7 +77,7 @@ struct pcap_nit { static int pcap_stats_nit(pcap_t *p, struct pcap_stat *ps) { - struct pcap_nit *pn = p->private; + struct pcap_nit *pn = p->priv; /* * "ps_recv" counts packets handed to the filter, not packets @@ -106,7 +102,7 @@ pcap_stats_nit(pcap_t *p, struct pcap_stat *ps) static int pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { - struct pcap_nit *pn = p->private; + struct pcap_nit *pn = p->priv; register int cc, n; register u_char *bp, *cp, *ep; register struct nit_hdr *nh; @@ -118,11 +114,11 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) if (cc < 0) { if (errno == EWOULDBLOCK) return (0); - snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", + pcap_snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", pcap_strerror(errno)); return (-1); } - bp = p->buffer; + bp = (u_char *)p->buffer; } else bp = p->bp; @@ -172,7 +168,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) continue; default: - snprintf(p->errbuf, sizeof(p->errbuf), + pcap_snprintf(p->errbuf, sizeof(p->errbuf), "bad nit state %d", nh->nh_state); return (-1); } @@ -189,7 +185,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) h.len = nh->nh_wirelen; h.caplen = caplen; (*callback)(user, &h, cp); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->cc = ep - bp; p->bp = bp; return (n); @@ -210,12 +206,12 @@ pcap_inject_nit(pcap_t *p, const void *buf, size_t size) strncpy(sa.sa_data, device, sizeof(sa.sa_data)); ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa)); if (ret == -1) { - snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", pcap_strerror(errno)); return (-1); } return (ret); -} +} static int nit_setflags(pcap_t *p) @@ -253,7 +249,7 @@ nit_setflags(pcap_t *p) nioc.nioc_flags |= NF_PROMISC; if (ioctl(p->fd, SIOCSNIT, &nioc) < 0) { - snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s", + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s", pcap_strerror(errno)); return (-1); } @@ -284,15 +280,22 @@ pcap_activate_nit(pcap_t *p) memset(p, 0, sizeof(*p)); p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW); if (fd < 0) { - snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "socket: %s", pcap_strerror(errno)); goto bad; } snit.snit_family = AF_NIT; - (void)strncpy(snit.snit_ifname, p->opt.source, NITIFSIZ); + (void)strncpy(snit.snit_ifname, p->opt.device, NITIFSIZ); if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) { - snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + /* + * XXX - there's probably a particular bind error that + * means "there's no such device" and a particular bind + * error that means "that device doesn't support NIT"; + * they might be the same error, if they both end up + * meaning "NIT doesn't know about that device". + */ + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno)); goto bad; } @@ -305,7 +308,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; @@ -352,11 +355,11 @@ pcap_activate_nit(pcap_t *p) } pcap_t * -pcap_create_interface(const char *device, char *ebuf) +pcap_create_interface(const char *device _U_, char *ebuf) { pcap_t *p; - p = pcap_create_common(device, ebuf, sizeof (struct pcap_nit)); + p = pcap_create_common(ebuf, sizeof (struct pcap_nit)); if (p == NULL) return (NULL); @@ -364,8 +367,18 @@ pcap_create_interface(const char *device, char *ebuf) return (p); } +/* + * XXX - there's probably a particular bind error that means "that device + * doesn't support NIT"; if so, we should try a bind and use that. + */ +static int +can_be_bound(const char *name _U_) +{ + return (1); +} + int pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) { - return (0); + return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); }