print-usb.c revision 214456
1274246Sae/*
2274246Sae * Copyright 2009 Bert Vermeulen <bert@biot.com>
3274246Sae *
4274246Sae * Redistribution and use in source and binary forms, with or without
5274246Sae * modification, are permitted provided that: (1) source code distributions
6274246Sae * retain the above copyright notice and this paragraph in its entirety, (2)
7274246Sae * distributions including binary code include the above copyright notice and
8274246Sae * this paragraph in its entirety in the documentation or other materials
9274246Sae * provided with the distribution, and (3) all advertising materials mentioning
10274246Sae * features or use of this software display the following acknowledgement:
11274246Sae * ``This product includes software developed by Paolo Abeni.''
12274246Sae * The name of author may not be used to endorse or promote products derived
13274246Sae * from this software without specific prior written permission.
14274246Sae * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15274246Sae * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16274246Sae * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17274246Sae *
18274246Sae * Support for USB packets
19274246Sae *
20274246Sae */
21274246Sae
22274246Sae#ifdef HAVE_CONFIG_H
23274246Sae#include "config.h"
24274246Sae#endif
25274246Sae
26274246Sae#include <tcpdump-stdinc.h>
27274246Sae
28274246Sae#include <pcap.h>
29274246Sae#include <stdio.h>
30274246Sae#include <string.h>
31274246Sae
32274246Sae#include "interface.h"
33274246Sae
34274246Sae
35274246Sae#if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
36274246Sae#include <pcap/usb.h>
37274246Sae
38274246Sae/* returns direction: 1=inbound 2=outbound -1=invalid */
39274246Saestatic int
40274246Saeget_direction(int transfer_type, int event_type)
41274246Sae{
42274246Sae	int direction;
43274246Sae
44274246Sae	direction = -1;
45274246Sae	switch(transfer_type){
46274246Sae	case URB_BULK:
47274246Sae	case URB_CONTROL:
48274246Sae	case URB_ISOCHRONOUS:
49274246Sae		switch(event_type)
50274246Sae		{
51274246Sae		case URB_SUBMIT:
52274246Sae			direction = 2;
53274246Sae			break;
54274246Sae		case URB_COMPLETE:
55274246Sae		case URB_ERROR:
56274246Sae			direction = 1;
57274246Sae			break;
58274246Sae		default:
59274246Sae			direction = -1;
60274246Sae		}
61274246Sae		break;
62274246Sae	case URB_INTERRUPT:
63274246Sae		switch(event_type)
64274246Sae		{
65274246Sae		case URB_SUBMIT:
66274246Sae			direction = 1;
67274246Sae			break;
68274246Sae		case URB_COMPLETE:
69274246Sae		case URB_ERROR:
70274246Sae			direction = 2;
71274246Sae			break;
72274246Sae		default:
73274246Sae			direction = -1;
74274246Sae		}
75274246Sae		break;
76274246Sae	 default:
77274246Sae		direction = -1;
78274246Sae	}
79274246Sae
80274246Sae	return direction;
81274246Sae}
82274246Sae
83274246Saestatic void
84274246Saeusb_header_print(const pcap_usb_header *uh)
85{
86	int direction;
87
88	switch(uh->transfer_type)
89	{
90		case URB_ISOCHRONOUS:
91			printf("ISOCHRONOUS");
92			break;
93		case URB_INTERRUPT:
94			printf("INTERRUPT");
95			break;
96		case URB_CONTROL:
97			printf("CONTROL");
98			break;
99		case URB_BULK:
100			printf("BULK");
101			break;
102		default:
103			printf(" ?");
104	}
105
106	switch(uh->event_type)
107	{
108		case URB_SUBMIT:
109			printf(" SUBMIT");
110			break;
111		case URB_COMPLETE:
112			printf(" COMPLETE");
113			break;
114		case URB_ERROR:
115			printf(" ERROR");
116			break;
117		default:
118			printf(" ?");
119	}
120
121	direction = get_direction(uh->transfer_type, uh->event_type);
122	if(direction == 1)
123		printf(" from");
124	else if(direction == 2)
125		printf(" to");
126	printf(" %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f);
127}
128
129/*
130 * This is the top level routine of the printer for captures with a
131 * 48-byte header.
132 *
133 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
134 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
135 * is the number of bytes actually captured.
136 */
137u_int
138usb_linux_48_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
139{
140	if (h->caplen < sizeof(pcap_usb_header)) {
141		printf("[|usb]");
142		return(sizeof(pcap_usb_header));
143	}
144
145	usb_header_print((const pcap_usb_header *) p);
146
147	return(sizeof(pcap_usb_header));
148}
149
150#ifdef DLT_USB_LINUX_MMAPPED
151/*
152 * This is the top level routine of the printer for captures with a
153 * 64-byte header.
154 *
155 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
156 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
157 * is the number of bytes actually captured.
158 */
159u_int
160usb_linux_64_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
161{
162	if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
163		printf("[|usb]");
164		return(sizeof(pcap_usb_header_mmapped));
165	}
166
167	usb_header_print((const pcap_usb_header *) p);
168
169	return(sizeof(pcap_usb_header_mmapped));
170}
171#endif /* DLT_USB_LINUX_MMAPPED */
172
173#endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */
174
175