diff options
Diffstat (limited to 'pcap-nit.c')
-rw-r--r-- | pcap-nit.c | 92 |
1 files changed, 71 insertions, 21 deletions
@@ -20,7 +20,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include <config.h> #endif #include <sys/types.h> @@ -114,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_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "pcap_read"); return (-1); } - bp = p->buffer; + bp = (u_char *)p->buffer; } else bp = p->bp; @@ -168,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); } @@ -206,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_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); -} +} static int nit_setflags(pcap_t *p) @@ -249,8 +249,8 @@ 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_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCSNIT"); return (-1); } return (0); @@ -271,6 +271,17 @@ pcap_activate_nit(pcap_t *p) return (PCAP_ERROR_RFMON_NOTSUP); } + /* + * Turn a negative snapshot value (invalid), a snapshot value of + * 0 (unspecified), or a value bigger than the normal maximum + * value, into the maximum allowed value. + * + * If some application really *needs* a bigger snapshot + * length, we should just increase MAXIMUM_SNAPLEN. + */ + if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) + p->snapshot = MAXIMUM_SNAPLEN; + if (p->snapshot < 96) /* * NIT requires a snapshot length of at least 96. @@ -280,16 +291,23 @@ 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, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); 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, - "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno)); + /* + * 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_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "bind: %s", snit.snit_ifname); goto bad; } if (nit_setflags(p) < 0) @@ -301,9 +319,10 @@ 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); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); goto bad; } @@ -348,11 +367,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); @@ -360,8 +379,39 @@ pcap_create_interface(const char *device, char *ebuf) return (p); } -int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +/* + * 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); +} + +static int +get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) { + /* + * Nothing we can do. + * XXX - is there a way to find out whether an adapter has + * something plugged into it? + */ return (0); } + +int +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) +{ + return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound, + get_if_flags)); +} + +/* + * Libpcap version string. + */ +const char * +pcap_lib_version(void) +{ + return (PCAP_VERSION_STRING); +} |