diff options
Diffstat (limited to 'contrib/libpcap/pcap-usb-linux.c')
-rw-r--r-- | contrib/libpcap/pcap-usb-linux.c | 78 |
1 files changed, 50 insertions, 28 deletions
diff --git a/contrib/libpcap/pcap-usb-linux.c b/contrib/libpcap/pcap-usb-linux.c index 26f618970604..bdabf005ccae 100644 --- a/contrib/libpcap/pcap-usb-linux.c +++ b/contrib/libpcap/pcap-usb-linux.c @@ -32,10 +32,6 @@ * Modifications: Kris Katterjohn <katterjohn@gmail.com> * */ -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.33 2008-12-23 21:38:50 guy Exp $ (LBL)"; -#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -121,6 +117,16 @@ struct mon_bin_mfetch { #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */ #define MON_BIN_ERROR 0x8 +/* + * Private data for capturing on Linux USB. + */ +struct pcap_usb_linux { + u_char *mmapbuf; /* memory-mapped region pointer */ + size_t mmapbuflen; /* size of region */ + int bus_index; + u_int packets_read; +}; + /* forward declaration */ static int usb_activate(pcap_t *); static int usb_stats_linux(pcap_t *, struct pcap_stat *); @@ -204,16 +210,19 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str) static int usb_mmap(pcap_t* handle) { + struct pcap_usb_linux *handlep = handle->priv; int len = ioctl(handle->fd, MON_IOCQ_RING_SIZE); if (len < 0) return 0; - handle->md.mmapbuflen = len; - handle->md.mmapbuf = mmap(0, handle->md.mmapbuflen, PROT_READ, + handlep->mmapbuflen = len; + handlep->mmapbuf = mmap(0, handlep->mmapbuflen, PROT_READ, MAP_SHARED, handle->fd, 0); - return handle->md.mmapbuf != MAP_FAILED; + return handlep->mmapbuf != MAP_FAILED; } +#ifdef HAVE_LINUX_USBDEVICE_FS_H + #define CTRL_TIMEOUT (5*1000) /* milliseconds */ #define USB_DIR_IN 0x80 @@ -282,6 +291,7 @@ probe_devices(int bus) } closedir(dir); } +#endif /* HAVE_LINUX_USBDEVICE_FS_H */ pcap_t * usb_create(const char *device, char *ebuf, int *is_ours) @@ -318,7 +328,7 @@ usb_create(const char *device, char *ebuf, int *is_ours) /* OK, it's probably ours. */ *is_ours = 1; - p = pcap_create_common(device, ebuf); + p = pcap_create_common(device, ebuf, sizeof (struct pcap_usb_linux)); if (p == NULL) return (NULL); @@ -329,6 +339,7 @@ usb_create(const char *device, char *ebuf, int *is_ours) static int usb_activate(pcap_t* handle) { + struct pcap_usb_linux *handlep = handle->priv; char full_path[USB_LINE_LEN]; /* Initialize some components of the pcap structure. */ @@ -344,7 +355,7 @@ usb_activate(pcap_t* handle) handle->setnonblock_op = pcap_setnonblock_fd; /*get usb bus index from device name */ - if (sscanf(handle->opt.source, USB_IFACE"%d", &handle->md.ifindex) != 1) + if (sscanf(handle->opt.source, USB_IFACE"%d", &handlep->bus_index) != 1) { snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get USB bus index from %s", handle->opt.source); @@ -352,7 +363,7 @@ usb_activate(pcap_t* handle) } /*now select the read method: try to open binary interface */ - snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handle->md.ifindex); + snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handlep->bus_index); handle->fd = open(full_path, O_RDONLY, 0); if (handle->fd >= 0) { @@ -370,7 +381,9 @@ usb_activate(pcap_t* handle) handle->stats_op = usb_stats_linux_bin; handle->read_op = usb_read_linux_mmap; handle->cleanup_op = usb_cleanup_linux_mmap; - probe_devices(handle->md.ifindex); +#ifdef HAVE_LINUX_USBDEVICE_FS_H + probe_devices(handlep->bus_index); +#endif /* * "handle->fd" is a real file, so "select()" and @@ -383,11 +396,13 @@ usb_activate(pcap_t* handle) /* can't mmap, use plain binary interface access */ handle->stats_op = usb_stats_linux_bin; handle->read_op = usb_read_linux_bin; - probe_devices(handle->md.ifindex); +#ifdef HAVE_LINUX_USBDEVICE_FS_H + probe_devices(handlep->bus_index); +#endif } else { /*Binary interface not available, try open text interface */ - snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handle->md.ifindex); + snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handlep->bus_index); handle->fd = open(full_path, O_RDONLY, 0); if (handle->fd < 0) { @@ -397,7 +412,7 @@ usb_activate(pcap_t* handle) * Not found at the new location; try * the old location. */ - snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%dt", handle->md.ifindex); + snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%dt", handlep->bus_index); handle->fd = open(full_path, O_RDONLY, 0); } if (handle->fd < 0) { @@ -456,6 +471,7 @@ usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u * /usr/src/linux/Documentation/usb/usbmon.txt * for message format */ + struct pcap_usb_linux *handlep = handle->priv; unsigned timestamp; int tag, cnt, ep_num, dev_addr, dummy, ret, urb_len, data_len; char etype, pipeid1, pipeid2, status[16], urb_tag, line[USB_LINE_LEN]; @@ -500,7 +516,7 @@ usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u } uhdr->id = tag; uhdr->device_address = dev_addr; - uhdr->bus_id = handle->md.ifindex; + uhdr->bus_id = handlep->bus_index; uhdr->status = 0; string += cnt; @@ -639,7 +655,7 @@ got: if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, handle->buffer, pkth.len, pkth.caplen)) { - handle->md.packets_read++; + handlep->packets_read++; callback(user, &pkth, handle->buffer); return 1; } @@ -657,13 +673,14 @@ usb_inject_linux(pcap_t *handle, const void *buf, size_t size) static int usb_stats_linux(pcap_t *handle, struct pcap_stat *stats) { + struct pcap_usb_linux *handlep = handle->priv; int dummy, ret, consumed, cnt; char string[USB_LINE_LEN]; char token[USB_LINE_LEN]; char * ptr = string; int fd; - snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handle->md.ifindex); + snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handlep->bus_index); fd = open(string, O_RDONLY, 0); if (fd < 0) { @@ -673,7 +690,7 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats) * Not found at the new location; try the old * location. */ - snprintf(string, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%ds", handle->md.ifindex); + snprintf(string, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%ds", handlep->bus_index); fd = open(string, O_RDONLY, 0); } if (fd < 0) { @@ -724,7 +741,7 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats) ptr += cnt; } - stats->ps_recv = handle->md.packets_read; + stats->ps_recv = handlep->packets_read; stats->ps_ifdrop = 0; return 0; } @@ -740,6 +757,7 @@ usb_setdirection_linux(pcap_t *p, pcap_direction_t d) static int usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats) { + struct pcap_usb_linux *handlep = handle->priv; int ret; struct mon_bin_stats st; ret = ioctl(handle->fd, MON_IOCG_STATS, &st); @@ -750,7 +768,7 @@ usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats) return -1; } - stats->ps_recv = handle->md.packets_read + st.queued; + stats->ps_recv = handlep->packets_read + st.queued; stats->ps_drop = st.dropped; stats->ps_ifdrop = 0; return 0; @@ -763,6 +781,7 @@ usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats) static int usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { + struct pcap_usb_linux *handlep = handle->priv; struct mon_bin_get info; int ret; struct pcap_pkthdr pkth; @@ -805,7 +824,7 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, handle->buffer, pkth.len, pkth.caplen)) { - handle->md.packets_read++; + handlep->packets_read++; callback(user, &pkth, handle->buffer); return 1; } @@ -821,6 +840,7 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha static int usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { + struct pcap_usb_linux *handlep = handle->priv; struct mon_bin_mfetch fetch; int32_t vec[VEC_SIZE]; struct pcap_pkthdr pkth; @@ -866,7 +886,7 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch nflush = fetch.nfetch; for (i=0; i<fetch.nfetch; ++i) { /* discard filler */ - hdr = (pcap_usb_header*) &handle->md.mmapbuf[vec[i]]; + hdr = (pcap_usb_header*) &handlep->mmapbuf[vec[i]]; if (hdr->event_type == '@') continue; @@ -885,14 +905,14 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, (u_char*) hdr, pkth.len, pkth.caplen)) { - handle->md.packets_read++; + handlep->packets_read++; callback(user, &pkth, (u_char*) hdr); packets++; } } - /* with max_packets <= 0 we stop afer the first chunk*/ - if ((max_packets <= 0) || (packets == max_packets)) + /* with max_packets specifying "unlimited" we stop afer the first chunk*/ + if (PACKET_COUNT_IS_UNLIMITED(max_packets) || (packets == max_packets)) break; } @@ -904,10 +924,12 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch static void usb_cleanup_linux_mmap(pcap_t* handle) { + struct pcap_usb_linux *handlep = handle->priv; + /* if we have a memory-mapped buffer, unmap it */ - if (handle->md.mmapbuf != NULL) { - munmap(handle->md.mmapbuf, handle->md.mmapbuflen); - handle->md.mmapbuf = NULL; + if (handlep->mmapbuf != NULL) { + munmap(handlep->mmapbuf, handlep->mmapbuflen); + handlep->mmapbuf = NULL; } pcap_cleanup_live_common(handle); } |