]> The Tcpdump Group git mirrors - tcpdump/blob - print-usb.c
VRRP: add a sample capture and two test cases
[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 <stdio.h>
29 #include <string.h>
30
31 #include "interface.h"
32
33
34 #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
35 #include <pcap/usb.h>
36
37 static const char tstr[] = "[|usb]";
38
39 /* returns direction: 1=inbound 2=outbound -1=invalid */
40 static int
41 get_direction(int transfer_type, int event_type)
42 {
43 int direction;
44
45 direction = -1;
46 switch(transfer_type){
47 case URB_BULK:
48 case URB_CONTROL:
49 case URB_ISOCHRONOUS:
50 switch(event_type)
51 {
52 case URB_SUBMIT:
53 direction = 2;
54 break;
55 case URB_COMPLETE:
56 case URB_ERROR:
57 direction = 1;
58 break;
59 default:
60 direction = -1;
61 }
62 break;
63 case URB_INTERRUPT:
64 switch(event_type)
65 {
66 case URB_SUBMIT:
67 direction = 1;
68 break;
69 case URB_COMPLETE:
70 case URB_ERROR:
71 direction = 2;
72 break;
73 default:
74 direction = -1;
75 }
76 break;
77 default:
78 direction = -1;
79 }
80
81 return direction;
82 }
83
84 static void
85 usb_header_print(const pcap_usb_header *uh)
86 {
87 int direction;
88
89 switch(uh->transfer_type)
90 {
91 case URB_ISOCHRONOUS:
92 printf("ISOCHRONOUS");
93 break;
94 case URB_INTERRUPT:
95 printf("INTERRUPT");
96 break;
97 case URB_CONTROL:
98 printf("CONTROL");
99 break;
100 case URB_BULK:
101 printf("BULK");
102 break;
103 default:
104 printf(" ?");
105 }
106
107 switch(uh->event_type)
108 {
109 case URB_SUBMIT:
110 printf(" SUBMIT");
111 break;
112 case URB_COMPLETE:
113 printf(" COMPLETE");
114 break;
115 case URB_ERROR:
116 printf(" ERROR");
117 break;
118 default:
119 printf(" ?");
120 }
121
122 direction = get_direction(uh->transfer_type, uh->event_type);
123 if(direction == 1)
124 printf(" from");
125 else if(direction == 2)
126 printf(" to");
127 printf(" %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f);
128 }
129
130 /*
131 * This is the top level routine of the printer for captures with a
132 * 48-byte header.
133 *
134 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
135 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
136 * is the number of bytes actually captured.
137 */
138 u_int
139 usb_linux_48_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
140 {
141 if (h->caplen < sizeof(pcap_usb_header)) {
142 printf("%s", tstr);
143 return(sizeof(pcap_usb_header));
144 }
145
146 usb_header_print((const pcap_usb_header *) p);
147
148 return(sizeof(pcap_usb_header));
149 }
150
151 #ifdef DLT_USB_LINUX_MMAPPED
152 /*
153 * This is the top level routine of the printer for captures with a
154 * 64-byte header.
155 *
156 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
157 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
158 * is the number of bytes actually captured.
159 */
160 u_int
161 usb_linux_64_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
162 {
163 if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
164 printf("%s", tstr);
165 return(sizeof(pcap_usb_header_mmapped));
166 }
167
168 usb_header_print((const pcap_usb_header *) p);
169
170 return(sizeof(pcap_usb_header_mmapped));
171 }
172 #endif /* DLT_USB_LINUX_MMAPPED */
173
174 #endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */
175