2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * pcap-usb-linux-common.h - common code for everything that needs to
22 * deal with Linux USB captures, whether live or in a capture file;
23 * the later means that this is *not* Linux-only.
29 * Return the sum of the two u_int arguments if that sum fits in a u_int,
30 * and return UINT_MAX otherwise.
33 u_int_sum(u_int a
, u_int b
)
35 return (((b
) <= UINT_MAX
- (a
)) ? (a
) + (b
) : UINT_MAX
);
39 * Is this a completion event for an isochronous transfer?
42 is_isochronous_transfer_completion(const pcap_usb_header_mmapped
*hdr
)
44 return (hdr
->transfer_type
== URB_ISOCHRONOUS
&&
45 hdr
->event_type
== URB_COMPLETE
&&
46 (hdr
->endpoint_number
& URB_TRANSFER_IN
));
50 * Total length of the pseudo-header, including the isochronous
53 static inline uint32_t
54 iso_pseudo_header_len(const pcap_usb_header_mmapped
*usb_hdr
)
56 return (sizeof(pcap_usb_header_mmapped
) +
57 usb_hdr
->ndesc
* sizeof (usb_isodesc
));
61 * Calculate the packet length for a "this is complete" incoming
62 * isochronous transfer event.
64 * Calculating that from hdr->urb_len is not correct, because the
65 * data is not contiguous, and the isochroous descriptors show how
69 incoming_isochronous_transfer_completed_len(struct pcap_pkthdr
*phdr
,
72 const pcap_usb_header_mmapped
*hdr
;
74 const usb_isodesc
*descs
;
75 u_int pre_truncation_data_len
;
78 * All callers of this routine must ensure that pkth->caplen is
79 * >= sizeof (pcap_usb_header_mmapped).
81 bytes_left
= phdr
->caplen
;
82 bytes_left
-= sizeof (pcap_usb_header_mmapped
);
84 hdr
= (const pcap_usb_header_mmapped
*) bp
;
85 descs
= (const usb_isodesc
*) (bp
+ sizeof(pcap_usb_header_mmapped
));
88 * Find the end of the last chunk of data in the buffer
89 * referred to by the isochronous descriptors; that indicates
90 * how far into the buffer the data would have gone.
92 * Make sure we don't run past the end of the captured data
93 * while processing the isochronous descriptors.
95 pre_truncation_data_len
= 0;
96 for (uint32_t desc
= 0;
97 desc
< hdr
->ndesc
&& bytes_left
>= sizeof (usb_isodesc
);
98 desc
++, bytes_left
-= sizeof (usb_isodesc
)) {
101 if (descs
[desc
].len
!= 0) {
103 * Compute the end offset of the data
104 * for this descriptor, i.e. the offset
105 * of the byte after the data. Clamp
106 * the sum at UINT_MAX, so that it fits
109 desc_end
= u_int_sum(descs
[desc
].offset
,
111 if (desc_end
> pre_truncation_data_len
)
112 pre_truncation_data_len
= desc_end
;
117 * Return the sum of the total header length (memory-mapped
118 * header and ISO descriptors) and the data length, clamped
121 * We've made sure that the number of descriptors is
122 * <= USB_MAXDESC, so we know that the total size,
123 * in bytes, of the descriptors fits in a 32-bit
126 return (u_int_sum(iso_pseudo_header_len(hdr
), pre_truncation_data_len
));