]> The Tcpdump Group git mirrors - tcpdump/blob - print-usb.c
Use EXTRACT_nBITS even when just testing against zero.
[tcpdump] / print-usb.c
1 /*
2 * Copyright 2009 Bert Vermeulen <bert@biot.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code distributions
6 * retain the above copyright notice and this paragraph in its entirety, (2)
7 * distributions including binary code include the above copyright notice and
8 * this paragraph in its entirety in the documentation or other materials
9 * provided with the distribution, and (3) all advertising materials mentioning
10 * features or use of this software display the following acknowledgement:
11 * ``This product includes software developed by Paolo Abeni.''
12 * The name of author may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * Support for USB packets
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <pcap.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "interface.h"
33
34
35 #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
36 #include <pcap/usb.h>
37
38 static const char tstr[] = "[|usb]";
39
40 /* returns direction: 1=inbound 2=outbound -1=invalid */
41 static int
42 get_direction(int transfer_type, int event_type)
43 {
44 int direction;
45
46 direction = -1;
47 switch(transfer_type){
48 case URB_BULK:
49 case URB_CONTROL:
50 case URB_ISOCHRONOUS:
51 switch(event_type)
52 {
53 case URB_SUBMIT:
54 direction = 2;
55 break;
56 case URB_COMPLETE:
57 case URB_ERROR:
58 direction = 1;
59 break;
60 default:
61 direction = -1;
62 }
63 break;
64 case URB_INTERRUPT:
65 switch(event_type)
66 {
67 case URB_SUBMIT:
68 direction = 1;
69 break;
70 case URB_COMPLETE:
71 case URB_ERROR:
72 direction = 2;
73 break;
74 default:
75 direction = -1;
76 }
77 break;
78 default:
79 direction = -1;
80 }
81
82 return direction;
83 }
84
85 static void
86 usb_header_print(const pcap_usb_header *uh)
87 {
88 int direction;
89
90 switch(uh->transfer_type)
91 {
92 case URB_ISOCHRONOUS:
93 printf("ISOCHRONOUS");
94 break;
95 case URB_INTERRUPT:
96 printf("INTERRUPT");
97 break;
98 case URB_CONTROL:
99 printf("CONTROL");
100 break;
101 case URB_BULK:
102 printf("BULK");
103 break;
104 default:
105 printf(" ?");
106 }
107
108 switch(uh->event_type)
109 {
110 case URB_SUBMIT:
111 printf(" SUBMIT");
112 break;
113 case URB_COMPLETE:
114 printf(" COMPLETE");
115 break;
116 case URB_ERROR:
117 printf(" ERROR");
118 break;
119 default:
120 printf(" ?");
121 }
122
123 direction = get_direction(uh->transfer_type, uh->event_type);
124 if(direction == 1)
125 printf(" from");
126 else if(direction == 2)
127 printf(" to");
128 printf(" %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f);
129 }
130
131 /*
132 * This is the top level routine of the printer for captures with a
133 * 48-byte header.
134 *
135 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
136 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
137 * is the number of bytes actually captured.
138 */
139 u_int
140 usb_linux_48_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
141 {
142 if (h->caplen < sizeof(pcap_usb_header)) {
143 printf("%s", tstr);
144 return(sizeof(pcap_usb_header));
145 }
146
147 usb_header_print((const pcap_usb_header *) p);
148
149 return(sizeof(pcap_usb_header));
150 }
151
152 #ifdef DLT_USB_LINUX_MMAPPED
153 /*
154 * This is the top level routine of the printer for captures with a
155 * 64-byte header.
156 *
157 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
158 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
159 * is the number of bytes actually captured.
160 */
161 u_int
162 usb_linux_64_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
163 {
164 if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
165 printf("%s", tstr);
166 return(sizeof(pcap_usb_header_mmapped));
167 }
168
169 usb_header_print((const pcap_usb_header *) p);
170
171 return(sizeof(pcap_usb_header_mmapped));
172 }
173 #endif /* DLT_USB_LINUX_MMAPPED */
174
175 #endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */
176