]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
So far, two people have submitted patches to "fix" the "memory leak" in
[libpcap] / pcap-usb-linux.c
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * USB sniffing API implementation for Linux platform
33 * By Paolo Abeni <paolo.abeni@email.it>
34 *
35 */
36 #ifndef lint
37 static const char rcsid[] _U_ =
38 "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.15 2007-04-01 21:43:55 guy Exp $ (LBL)";
39 #endif
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include "pcap-int.h"
46 #include "pcap-usb-linux.h"
47 #include "pcap/usb.h"
48
49 #ifdef NEED_STRERROR_H
50 #include "strerror.h"
51 #endif
52
53 #include <ctype.h>
54 #include <errno.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58 #include <string.h>
59 #include <dirent.h>
60 #include <byteswap.h>
61 #include <netinet/in.h>
62 #include <sys/ioctl.h>
63 #include <sys/mman.h>
64
65 #define USB_IFACE "usb"
66 #define USB_TEXT_DIR "/sys/kernel/debug/usbmon"
67 #define USB_BUS_DIR "/proc/bus/usb"
68 #define USB_LINE_LEN 4096
69
70
71 #define PIPE_IN 0x80
72 #define PIPE_ISOCHRONOUS 0
73 #define PIPE_INTERRUPT 1
74 #define PIPE_CONTROL 2
75 #define PIPE_BULK 3
76
77 #if __BYTE_ORDER == __LITTLE_ENDIAN
78 #define htols(s) s
79 #define htoll(l) l
80 #define htol64(ll) ll
81 #else
82 #define htols(s) bswap_16(s)
83 #define htoll(l) bswap_32(l)
84 #define htol64(ll) bswap_64(ll)
85 #endif
86
87 struct mon_bin_stats {
88 u_int32_t queued;
89 u_int32_t dropped;
90 };
91
92 struct mon_bin_get {
93 pcap_usb_header *hdr;
94 void *data;
95 size_t data_len; /* Length of data (can be zero) */
96 };
97
98 struct mon_bin_mfetch {
99 int32_t *offvec; /* Vector of events fetched */
100 int32_t nfetch; /* Number of events to fetch (out: fetched) */
101 int32_t nflush; /* Number of events to flush */
102 };
103
104 #define MON_IOC_MAGIC 0x92
105
106 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
107 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
108 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
109 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
110 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
111 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
112 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
113 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
114
115 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
116 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
117 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
118 #define MON_BIN_ERROR 0x8
119
120 /* forward declaration */
121 static int usb_stats_linux(pcap_t *, struct pcap_stat *);
122 static int usb_stats_linux_bin(pcap_t *, struct pcap_stat *);
123 static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *);
124 static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *);
125 static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
126 static int usb_inject_linux(pcap_t *, const void *, size_t);
127 static int usb_setfilter_linux(pcap_t *, struct bpf_program *);
128 static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
129 static void usb_close_linux(pcap_t *);
130 static void usb_close_linux_mmap(pcap_t *);
131
132 /* facility to add an USB device to the device list*/
133 static int
134 usb_dev_add(pcap_if_t** alldevsp, int n, char *err_str)
135 {
136 char dev_name[10];
137 char dev_descr[30];
138 snprintf(dev_name, 10, USB_IFACE"%d", n);
139 snprintf(dev_descr, 30, "USB bus number %d", n);
140
141 if (pcap_add_if(alldevsp, dev_name, 0,
142 dev_descr, err_str) < 0)
143 return -1;
144 return 0;
145 }
146
147 int
148 usb_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
149 {
150 struct dirent* data;
151 int ret = 0;
152 DIR* dir;
153
154 /* scan profs usb bus directorys */
155 dir = opendir(USB_BUS_DIR);
156 if (!dir) return 0;
157 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
158 int n;
159 char* name = data->d_name;
160 int len = strlen(name);
161
162 /* if this file name does not end with a number it's not of our interest */
163 if ((len < 1) || !isdigit(name[--len]))
164 continue;
165 while (isdigit(name[--len]));
166 if (sscanf(&name[len+1], "%d", &n) != 1)
167 continue;
168
169 ret = usb_dev_add(alldevsp, n, err_str);
170 }
171
172 closedir(dir);
173 return ret;
174 }
175
176 static
177 int usb_mmap(pcap_t* handle)
178 {
179 int len = ioctl(handle->fd, MON_IOCQ_RING_SIZE);
180 if (len < 0)
181 return 0;
182
183 handle->buffer = mmap(0, len, PROT_READ, MAP_SHARED, handle->fd, 0);
184 return handle->buffer != MAP_FAILED;
185 }
186
187 pcap_t*
188 usb_open_live(const char* bus, int snaplen, int promisc , int to_ms, char* errmsg)
189 {
190 char full_path[USB_LINE_LEN];
191 pcap_t *handle;
192
193 /* Allocate a handle for this session. */
194 handle = malloc(sizeof(*handle));
195 if (handle == NULL) {
196 snprintf(errmsg, PCAP_ERRBUF_SIZE, "malloc: %s",
197 pcap_strerror(errno));
198 return NULL;
199 }
200
201 /* Initialize some components of the pcap structure. */
202 memset(handle, 0, sizeof(*handle));
203 handle->snapshot = snaplen;
204 handle->md.timeout = to_ms;
205 handle->bufsize = snaplen;
206 handle->offset = 0;
207 handle->linktype = DLT_USB_LINUX;
208
209 /*
210 * "handle->fd" is a real file , so "select()" and "poll()"
211 * work on it.
212 */
213 handle->selectable_fd = handle->fd;
214
215 handle->inject_op = usb_inject_linux;
216 handle->setfilter_op = usb_setfilter_linux;
217 handle->setdirection_op = usb_setdirection_linux;
218 handle->set_datalink_op = NULL; /* can't change data link type */
219 handle->getnonblock_op = pcap_getnonblock_fd;
220 handle->setnonblock_op = pcap_setnonblock_fd;
221 handle->close_op = usb_close_linux;
222
223 /*get usb bus index from device name */
224 if (sscanf(bus, USB_IFACE"%d", &handle->md.ifindex) != 1)
225 {
226 snprintf(errmsg, PCAP_ERRBUF_SIZE,
227 "Can't get USB bus index from %s", bus);
228 free(handle);
229 return NULL;
230 }
231
232 /*now select the read method: try to open binary interface */
233 snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handle->md.ifindex);
234 handle->fd = open(full_path, O_RDONLY, 0);
235 if (handle->fd >= 0)
236 {
237 /* header endianess can't be fixed for memory mapped access,
238 * due to read only access to mmaped buffer, so disable it*/
239 /* binary api is available, try to use fast mmap access */
240 if (usb_mmap(handle)) {
241 handle->stats_op = usb_stats_linux_bin;
242 handle->read_op = usb_read_linux_mmap;
243 handle->close_op = usb_close_linux_mmap;
244 return handle;
245 }
246
247 /* can't mmap, use plain binary interface access */
248 handle->stats_op = usb_stats_linux_bin;
249 handle->read_op = usb_read_linux_bin;
250 }
251 else {
252 /*Binary interface not available, try open text interface */
253 snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handle->md.ifindex);
254 handle->fd = open(full_path, O_RDONLY, 0);
255 if (handle->fd < 0)
256 {
257 /* no more fallback, give it up*/
258 snprintf(errmsg, PCAP_ERRBUF_SIZE,
259 "Can't open USB bus file %s: %s", full_path, strerror(errno));
260 free(handle);
261 return NULL;
262 }
263 handle->stats_op = usb_stats_linux;
264 handle->read_op = usb_read_linux;
265 }
266
267 /* for plain binary access and text access we need to allocate the read
268 * buffer */
269 handle->buffer = malloc(handle->bufsize);
270 if (!handle->buffer) {
271 snprintf(errmsg, PCAP_ERRBUF_SIZE,
272 "malloc: %s", pcap_strerror(errno));
273 usb_close_linux(handle);
274 return NULL;
275 }
276 return handle;
277 }
278
279 static inline int
280 ascii_to_int(char c)
281 {
282 return c < 'A' ? c- '0': ((c<'a') ? c - 'A' + 10: c-'a'+10);
283 }
284
285 /*
286 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
287 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
288 * format description
289 */
290 static int
291 usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
292 {
293 /* see:
294 * /usr/src/linux/Documentation/usb/usbmon.txt
295 * for message format
296 */
297 unsigned timestamp;
298 int tag, cnt, ep_num, dev_addr, dummy, ret, urb_len, data_len;
299 char etype, pipeid1, pipeid2, status[16], urb_tag, line[USB_LINE_LEN];
300 char *string = line;
301 u_char * rawdata = handle->buffer;
302 struct pcap_pkthdr pkth;
303 pcap_usb_header* uhdr = (pcap_usb_header*)handle->buffer;
304 u_char urb_transfer=0;
305 int incoming=0;
306
307 /* ignore interrupt system call errors */
308 do {
309 ret = read(handle->fd, line, USB_LINE_LEN - 1);
310 if (handle->break_loop)
311 {
312 handle->break_loop = 0;
313 return -2;
314 }
315 } while ((ret == -1) && (errno == EINTR));
316 if (ret < 0)
317 {
318 if (errno == EAGAIN)
319 return 0; /* no data there */
320
321 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
322 "Can't read from fd %d: %s", handle->fd, strerror(errno));
323 return -1;
324 }
325
326 /* read urb header; %n argument may increment return value, but it's
327 * not mandatory, so does not count on it*/
328 string[ret] = 0;
329 ret = sscanf(string, "%x %d %c %c%c:%d:%d %s%n", &tag, &timestamp, &etype,
330 &pipeid1, &pipeid2, &dev_addr, &ep_num, status,
331 &cnt);
332 if (ret < 8)
333 {
334 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
335 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
336 string, ret);
337 return -1;
338 }
339 uhdr->id = tag;
340 uhdr->endpoint_number = ep_num;
341 uhdr->device_address = dev_addr;
342 uhdr->bus_id = handle->md.ifindex;
343 uhdr->status = 0;
344 string += cnt;
345
346 /* don't use usbmon provided timestamp, since it have low precision*/
347 if (gettimeofday(&pkth.ts, NULL) < 0)
348 {
349 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
350 "Can't get timestamp for message '%s' %d:%s",
351 string, errno, strerror(errno));
352 return -1;
353 }
354 uhdr->ts_sec = pkth.ts.tv_sec;
355 uhdr->ts_usec = pkth.ts.tv_usec;
356
357 /* parse endpoint information */
358 if (pipeid1 == 'C')
359 urb_transfer = URB_CONTROL;
360 else if (pipeid1 == 'Z')
361 urb_transfer = URB_ISOCHRONOUS;
362 else if (pipeid1 == 'I')
363 urb_transfer = URB_INTERRUPT;
364 else if (pipeid1 == 'B')
365 urb_transfer = URB_BULK;
366 if (pipeid2 == 'i') {
367 urb_transfer |= URB_TRANSFER_IN;
368 incoming = 1;
369 }
370 if (etype == 'C')
371 incoming = !incoming;
372
373 /* direction check*/
374 if (incoming)
375 {
376 if (handle->direction == PCAP_D_OUT)
377 return 0;
378 }
379 else
380 if (handle->direction == PCAP_D_IN)
381 return 0;
382 uhdr->event_type = etype;
383 uhdr->transfer_type = urb_transfer;
384 pkth.caplen = sizeof(pcap_usb_header);
385 rawdata += sizeof(pcap_usb_header);
386
387 /* check if this is a setup packet */
388 ret = sscanf(status, "%d", &dummy);
389 if (ret != 1)
390 {
391 /* this a setup packet, setup data can be filled with underscore if
392 * usbmon has not been able to read them, so we must parse this fields as
393 * strings */
394 pcap_usb_setup* shdr;
395 char str1[3], str2[3], str3[5], str4[5], str5[5];
396 ret = sscanf(string, "%s %s %s %s %s%n", str1, str2, str3, str4,
397 str5, &cnt);
398 if (ret < 5)
399 {
400 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
401 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
402 string, ret);
403 return -1;
404 }
405 string += cnt;
406
407 /* try to convert to corresponding integer */
408 shdr = &uhdr->setup;
409 shdr->bmRequestType = strtoul(str1, 0, 16);
410 shdr->bRequest = strtoul(str2, 0, 16);
411 shdr->wValue = htols(strtoul(str3, 0, 16));
412 shdr->wIndex = htols(strtoul(str4, 0, 16));
413 shdr->wLength = htols(strtoul(str5, 0, 16));
414
415 uhdr->setup_flag = 0;
416 }
417 else
418 uhdr->setup_flag = 1;
419
420 /* read urb data */
421 ret = sscanf(string, " %d%n", &urb_len, &cnt);
422 if (ret < 1)
423 {
424 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
425 "Can't parse urb length from '%s'", string);
426 return -1;
427 }
428 string += cnt;
429
430 /* urb tag is not present if urb length is 0, so we can stop here
431 * text parsing */
432 pkth.len = urb_len+pkth.caplen;
433 uhdr->urb_len = urb_len;
434 uhdr->data_flag = 1;
435 data_len = 0;
436 if (uhdr->urb_len == pkth.caplen)
437 goto got;
438
439 /* check for data presence; data is present if and only if urb tag is '=' */
440 if (sscanf(string, " %c", &urb_tag) != 1)
441 {
442 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
443 "Can't parse urb tag from '%s'", string);
444 return -1;
445 }
446
447 if (urb_tag != '=')
448 goto got;
449
450 /* skip urb tag and following space */
451 string += 3;
452
453 /* if we reach this point we got some urb data*/
454 uhdr->data_flag = 0;
455
456 /* read all urb data; if urb length is greater then the usbmon internal
457 * buffer length used by the kernel to spool the URB, we get only
458 * a partial information.
459 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
460 * length and default value is 130. */
461 while ((string[0] != 0) && (string[1] != 0) && (pkth.caplen < handle->snapshot))
462 {
463 rawdata[0] = ascii_to_int(string[0]) * 16 + ascii_to_int(string[1]);
464 rawdata++;
465 string+=2;
466 if (string[0] == ' ')
467 string++;
468 pkth.caplen++;
469 data_len++;
470 }
471
472 got:
473 uhdr->data_len = data_len;
474 handle->md.packets_read++;
475 if (pkth.caplen > handle->snapshot)
476 pkth.caplen = handle->snapshot;
477
478 callback(user, &pkth, handle->buffer);
479 return 1;
480 }
481
482 static int
483 usb_inject_linux(pcap_t *handle, const void *buf, size_t size)
484 {
485 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
486 "USB devices");
487 return (-1);
488 }
489
490 static void
491 usb_close_linux(pcap_t* handle)
492 {
493 /* handle fill be freed in pcap_close() 'common' code */
494 close(handle->fd);
495 if (handle->buffer)
496 free(handle->buffer);
497 }
498
499 static int
500 usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
501 {
502 int dummy, ret, consumed, cnt;
503 char string[USB_LINE_LEN];
504 char token[USB_LINE_LEN];
505 char * ptr = string;
506 snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handle->md.ifindex);
507
508 int fd = open(string, O_RDONLY, 0);
509 if (fd < 0)
510 {
511 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
512 "Can't open USB stats file %s: %s",
513 string, strerror(errno));
514 return -1;
515 }
516
517 /* read stats line */
518 do {
519 ret = read(fd, string, USB_LINE_LEN-1);
520 } while ((ret == -1) && (errno == EINTR));
521 close(fd);
522
523 if (ret < 0)
524 {
525 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
526 "Can't read stats from fd %d ", fd);
527 return -1;
528 }
529 string[ret] = 0;
530
531 /* extract info on dropped urbs */
532 for (consumed=0; consumed < ret; ) {
533 int ntok = sscanf(ptr, "%s%n", token, &cnt);
534 if (ntok != 2)
535 break;
536 consumed += cnt;
537 ptr += cnt;
538 if (strcmp(token, "nreaders") == 0)
539 ret = sscanf(ptr, "%d", &stats->ps_drop);
540 else
541 ret = sscanf(ptr, "%d", &dummy);
542 if (ntok != 2)
543 break;
544 consumed += cnt;
545 ptr += cnt;
546 }
547
548 stats->ps_recv = handle->md.packets_read;
549 stats->ps_ifdrop = 0;
550 return 0;
551 }
552
553 static int
554 usb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
555 {
556 return 0;
557 }
558
559 static int
560 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
561 {
562 p->direction = d;
563 return 0;
564 }
565
566
567 static int
568 usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats)
569 {
570 int ret;
571 struct mon_bin_stats st;
572 ret = ioctl(handle->fd, MON_IOCG_STATS, &st);
573 if (ret < 0)
574 {
575 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
576 "Can't read stats from fd %d:%s ", handle->fd, strerror(errno));
577 return -1;
578 }
579
580 stats->ps_recv = handle->md.packets_read + st.queued;
581 stats->ps_ifdrop = st.dropped;
582 return 0;
583 }
584
585 /*
586 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
587 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
588 */
589 static int
590 usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
591 {
592 struct mon_bin_get info;
593 int ret;
594 struct pcap_pkthdr pkth;
595 int clen = handle->snapshot - sizeof(pcap_usb_header);
596
597 /* the usb header is going to be part of 'packet' data*/
598 info.hdr = (pcap_usb_header*) handle->buffer;
599 info.data = handle->buffer + sizeof(pcap_usb_header);
600 info.data_len = clen;
601 /* ignore interrupt system call errors */
602 do {
603 ret = ioctl(handle->fd, MON_IOCX_GET, &info);
604 if (handle->break_loop)
605 {
606 handle->break_loop = 0;
607 return -2;
608 }
609 } while ((ret == -1) && (errno == EINTR));
610 if (ret < 0)
611 {
612 if (errno == EAGAIN)
613 return 0; /* no data there */
614
615 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
616 "Can't read from fd %d: %s", handle->fd, strerror(errno));
617 return -1;
618 }
619
620 /* we can get less that than really captured from kernel, depending on
621 * snaplen, so adjust header accordingly */
622 if (info.hdr->data_len < clen)
623 clen = info.hdr->data_len;
624 info.hdr->data_len = clen;
625 pkth.caplen = clen + sizeof(pcap_usb_header);
626 pkth.len = info.hdr->urb_len + sizeof(pcap_usb_header);
627 pkth.ts.tv_sec = info.hdr->ts_sec;
628 pkth.ts.tv_usec = info.hdr->ts_usec;
629
630 handle->md.packets_read++;
631 callback(user, &pkth, handle->buffer);
632 return 1;
633 }
634
635 /*
636 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
637 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
638 */
639 #define VEC_SIZE 32
640 static int
641 usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
642 {
643 struct mon_bin_mfetch fetch;
644 int32_t vec[VEC_SIZE];
645 struct pcap_pkthdr pkth;
646 pcap_usb_header* hdr;
647 int nflush = 0;
648 int packets = 0;
649
650 for (;;) {
651 int i, ret;
652 int limit = max_packets - packets;
653 if (limit < 0)
654 limit = VEC_SIZE;
655 if (limit > VEC_SIZE)
656 limit = VEC_SIZE;
657
658 /* try to fetch as many events as possible*/
659 fetch.offvec = vec;
660 fetch.nfetch = limit;
661 fetch.nflush = nflush;
662 /* ignore interrupt system call errors */
663 do {
664 ret = ioctl(handle->fd, MON_IOCX_MFETCH, &fetch);
665 if (handle->break_loop)
666 {
667 handle->break_loop = 0;
668 return -2;
669 }
670 } while ((ret == -1) && (errno == EINTR));
671 if (ret < 0)
672 {
673 if (errno == EAGAIN)
674 return 0; /* no data there */
675
676 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
677 "Can't mfetch fd %d: %s", handle->fd, strerror(errno));
678 return -1;
679 }
680
681 /* keep track of processed events, we will flush them later */
682 nflush = fetch.nfetch;
683 for (i=0; i<fetch.nfetch; ++i) {
684 /* discard filler */
685 hdr = (pcap_usb_header*) &handle->buffer[vec[i]];
686 if (hdr->event_type == '@')
687 continue;
688
689 /* get packet info from header*/
690 pkth.caplen = hdr->data_len + sizeof(pcap_usb_header);
691 pkth.len = hdr->urb_len + sizeof(pcap_usb_header);
692 pkth.ts.tv_sec = hdr->ts_sec;
693 pkth.ts.tv_usec = hdr->ts_usec;
694
695 handle->md.packets_read++;
696 callback(user, &pkth, (u_char*) hdr);
697 packets++;
698 }
699
700 /* with max_packets == -1 we stop afer the first chunk*/
701 if ((max_packets == -1) || (packets == max_packets))
702 break;
703 }
704
705 /* flush pending events*/
706 ioctl(handle->fd, MON_IOCH_MFLUSH, nflush);
707 return packets;
708 }
709
710 static void
711 usb_close_linux_mmap(pcap_t* handle)
712 {
713 /* handle fill be freed in pcap_close() 'common' code, buffer must not
714 * be freed because it's memory mapped */
715 close(handle->fd);
716 }