hcidump.c revision 9c09fb6319be5b971bf9e43f7955195a5d6d4141
1/*
2 *
3 *  Bluetooth packet analyzer - HCIdump
4 *
5 *  Copyright (C) 2000-2002  Maxim Krasnyansky <maxk@qualcomm.com>
6 *  Copyright (C) 2003-2004  Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 *  This program is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 *
23 *
24 *  $Id$
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31#include <stdio.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <termios.h>
37#include <string.h>
38#include <getopt.h>
39#include <pwd.h>
40
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <sys/uio.h>
45#include <sys/stat.h>
46#include <netinet/in.h>
47
48#include <bluetooth/bluetooth.h>
49#include <bluetooth/hci.h>
50#include <bluetooth/hci_lib.h>
51
52#include "parser/parser.h"
53#include "parser/sdp.h"
54
55
56#define SNAP_LEN HCI_MAX_FRAME_SIZE
57
58/* Modes */
59enum {
60	PARSE,
61	READ,
62	WRITE
63};
64
65/* Default options */
66static int  device;
67static int  snap_len = SNAP_LEN;
68static int  defpsm = 0;
69static int  mode = PARSE;
70static long flags;
71static long filter;
72static char *dump_file;
73
74struct dump_hdr {
75	uint16_t	len;
76	uint8_t		in;
77	uint8_t		pad;
78	uint32_t	ts_sec;
79	uint32_t	ts_usec;
80} __attribute__ ((packed));
81#define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
82
83static inline int read_n(int fd, char *buf, int len)
84{
85	register int t = 0, w;
86
87	while (len > 0) {
88		if ((w = read(fd, buf, len)) < 0) {
89			if( errno == EINTR || errno == EAGAIN )
90				continue;
91			return -1;
92		}
93		if (!w)
94			return 0;
95		len -= w; buf += w; t += w;
96	}
97	return t;
98}
99
100static inline int write_n(int fd, char *buf, int len)
101{
102	register int t = 0, w;
103
104	while (len > 0) {
105		if ((w = write(fd, buf, len)) < 0) {
106			if( errno == EINTR || errno == EAGAIN )
107				continue;
108			return -1;
109		}
110		if (!w)
111			return 0;
112		len -= w; buf += w; t += w;
113	}
114	return t;
115}
116
117static void process_frames(int dev, int sock, int file)
118{
119	struct cmsghdr *cmsg;
120	struct msghdr msg;
121	struct iovec  iv;
122	struct dump_hdr *dh;
123	struct frame frm;
124	char *buf, *ctrl;
125
126	if (snap_len < SNAP_LEN)
127		snap_len = SNAP_LEN;
128
129	if (!(buf = malloc(snap_len + DUMP_HDR_SIZE))) {
130		perror("Can't allocate data buffer");
131		exit(1);
132	}
133
134	dh = (void *) buf;
135	frm.data = buf + DUMP_HDR_SIZE;
136
137	if (!(ctrl = malloc(100))) {
138		perror("Can't allocate control buffer");
139		exit(1);
140	}
141
142	printf("device: hci%d snap_len: %d filter: 0x%lx\n",
143		dev, snap_len, filter);
144
145	memset(&msg, 0, sizeof(msg));
146
147	while (1) {
148		iv.iov_base = frm.data;
149		iv.iov_len  = snap_len;
150
151		msg.msg_iov = &iv;
152		msg.msg_iovlen = 1;
153		msg.msg_control = ctrl;
154		msg.msg_controllen = 100;
155
156		if ((frm.data_len = recvmsg(sock, &msg, 0)) < 0) {
157			perror("Receive failed");
158			exit(1);
159		}
160
161		/* Process control message */
162		frm.in = 0;
163		cmsg = CMSG_FIRSTHDR(&msg);
164		while (cmsg) {
165			switch (cmsg->cmsg_type) {
166			case HCI_CMSG_DIR:
167				frm.in = *((int *)CMSG_DATA(cmsg));
168				break;
169			case HCI_CMSG_TSTAMP:
170				frm.ts = *((struct timeval *)CMSG_DATA(cmsg));
171				break;
172			}
173			cmsg = CMSG_NXTHDR(&msg, cmsg);
174		}
175
176		frm.ptr = frm.data;
177		frm.len = frm.data_len;
178
179		switch (mode) {
180		case WRITE:
181			/* Save dump */
182			dh->len = htobs(frm.data_len);
183			dh->in  = frm.in;
184			dh->ts_sec  = htobl(frm.ts.tv_sec);
185			dh->ts_usec = htobl(frm.ts.tv_usec);
186			if (write_n(file, buf, frm.data_len + DUMP_HDR_SIZE) < 0) {
187				perror("Write error");
188				exit(1);
189			}
190			break;
191
192		default:
193			/* Parse and print */
194			parse(&frm);
195			break;
196		}
197	}
198}
199
200static void read_dump(int file)
201{
202	struct dump_hdr dh;
203	struct frame frm;
204	int err;
205
206	if (!(frm.data = malloc(HCI_MAX_FRAME_SIZE))) {
207		perror("Can't allocate data buffer");
208		exit(1);
209	}
210
211	while (1) {
212		if ((err = read_n(file, (void *) &dh, DUMP_HDR_SIZE)) < 0)
213			goto failed;
214		if (!err) return;
215
216		frm.data_len = btohs(dh.len);
217
218		if ((err = read_n(file, frm.data, frm.data_len)) < 0)
219			goto failed;
220		if (!err) return;
221
222		frm.ptr = frm.data;
223		frm.len = frm.data_len;
224		frm.in  = dh.in;
225		frm.ts.tv_sec  = btohl(dh.ts_sec);
226		frm.ts.tv_usec = btohl(dh.ts_usec);
227
228		parse(&frm);
229	}
230
231failed:
232	perror("Read failed");
233	exit(1);
234}
235
236static int open_file(char *file, int mode)
237{
238	int f, flags;
239
240	if (mode == WRITE)
241		flags = O_WRONLY | O_CREAT | O_APPEND;
242	else
243		flags = O_RDONLY;
244
245	if ((f = open(file, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
246		perror("Can't open output file");
247		exit(1);
248	}
249	return f;
250}
251
252static int open_socket(int dev)
253{
254	struct sockaddr_hci addr;
255	struct hci_filter flt;
256	int s, opt;
257
258	/* Create HCI socket */
259	if ((s=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
260		perror("Can't create HCI socket");
261		exit(1);
262	}
263
264	opt = 1;
265	if (setsockopt(s, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
266		perror("Can't enable data direction info");
267		exit(1);
268	}
269
270	opt = 1;
271	if (setsockopt(s, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
272		perror("Can't enable time stamp");
273		exit(1);
274	}
275
276	/* Setup filter */
277	hci_filter_clear(&flt);
278	hci_filter_all_ptypes(&flt);
279	hci_filter_all_events(&flt);
280	if (setsockopt(s, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
281		perror("Can't set HCI filter");
282		exit(1);
283	}
284
285	/* Bind socket to the HCI device */
286	addr.hci_family = AF_BLUETOOTH;
287	addr.hci_dev = dev;
288	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
289		printf("Can't attach to device hci%d. %s(%d)\n",
290					dev, strerror(errno), errno);
291		exit(1);
292	}
293	return s;
294}
295
296static struct {
297	char *name;
298	int  flag;
299} filters[] = {
300	{ "hci",	FILT_HCI	},
301	{ "sco",	FILT_SCO	},
302	{ "l2cap",	FILT_L2CAP	},
303	{ "rfcomm",	FILT_RFCOMM	},
304	{ "sdp",	FILT_SDP	},
305	{ "bnep",	FILT_BNEP	},
306	{ "cmtp",	FILT_CMTP	},
307	{ "hidp",	FILT_HIDP	},
308	{ "hcrp",	FILT_HCRP	},
309	{ "avdtp",	FILT_AVDTP	},
310	{ "capi",	FILT_CAPI	},
311	{ 0 }
312};
313
314static void parse_filter(int argc, char **argv)
315{
316	int i,n;
317
318	for (i = 0; i < argc; i++) {
319		for (n = 0; filters[n].name; n++) {
320			if (!strcmp(filters[n].name, argv[i])) {
321				filter |= filters[n].flag;
322				break;
323			}
324		}
325	}
326}
327
328static void usage(void)
329{
330	printf(
331	"Usage: hcidump [OPTION...] [filter]\n"
332	"  -i, --device=hci_dev       HCI device\n"
333	"  -s, --snap-len=len         Snap len (in bytes)\n"
334	"  -p, --psm=psm              Default PSM\n"
335	"  -w, --save-dump=file       Save dump to a file\n"
336	"  -r, --read-dump=file       Read dump from a file\n"
337	"  -t, --ts                   Display time stamps\n"
338	"  -x, --hex                  Dump data in hex\n"
339	"  -a, --ascii                Dump data in ascii\n"
340	"  -R, --raw                  Raw mode\n"
341	"  -C, --cmtp=psm             PSM for CMTP\n"
342	"  -H, --hcrp=psm             PSM for HCRP\n"
343	"  -?, --help                 Give this help list\n"
344	"      --usage                Give a short usage message\n"
345	);
346}
347
348static struct option main_options[] = {
349	{ "device",	1, 0, 'i' },
350	{ "snap-len",	1, 0, 's' },
351	{ "psm",	1, 0, 'p' },
352	{ "save-dump",	1, 0, 'w' },
353	{ "read-dump",	1, 0, 'r' },
354	{ "ts",		0, 0, 't' },
355	{ "hex",	0, 0, 'x' },
356	{ "ascii",	0, 0, 'a' },
357	{ "raw",	0, 0, 'R' },
358	{ "cmtp",	1, 0, 'C' },
359	{ "hcrp",	1, 0, 'H' },
360	{ "help",	0, 0, 'h' },
361	{ 0 }
362};
363
364int main(int argc, char *argv[])
365{
366	int opt;
367
368	printf("HCIDump - HCI packet analyzer ver %s\n", VERSION);
369
370	while ((opt=getopt_long(argc, argv, "i:s:p:w:r:txaRC:h", main_options, NULL)) != -1) {
371		switch(opt) {
372		case 'i':
373			device = atoi(optarg+3);
374			break;
375
376		case 's':
377			snap_len = atoi(optarg);
378			break;
379
380		case 'p':
381			defpsm = atoi(optarg);
382			break;
383
384		case 'w':
385			mode = WRITE;
386			dump_file = strdup(optarg);
387			break;
388
389		case 'r':
390			mode = READ;
391			dump_file = strdup(optarg);
392			break;
393
394		case 't':
395			flags |= DUMP_TSTAMP;
396			break;
397
398		case 'x':
399			flags |= DUMP_HEX;
400			break;
401
402		case 'a':
403			flags |= DUMP_ASCII;
404			break;
405
406		case 'R':
407			flags |= DUMP_RAW;
408			break;
409
410		case 'C':
411			set_proto(0, atoi(optarg), SDP_UUID_CMTP);
412			break;
413
414		case 'H':
415			set_proto(0, atoi(optarg), SDP_UUID_HARDCOPY_CONTROL_CHANNEL);
416			break;
417
418		case 'h':
419		default:
420			usage();
421			exit(0);
422		}
423	}
424
425	argc -= optind;
426	argv += optind;
427	optind = 0;
428
429	if (argc > 0)
430		parse_filter(argc, argv);
431
432	/* Default settings */
433	if (!filter)
434		filter = ~0L;
435
436	switch (mode) {
437	case PARSE:
438		init_parser(flags, filter, defpsm);
439		process_frames(device, open_socket(device), -1);
440		break;
441
442	case WRITE:
443		process_frames(device, open_socket(device), open_file(dump_file, mode));
444		break;
445
446	case READ:
447		init_parser(flags, filter, defpsm);
448		read_dump(open_file(dump_file, mode));
449		break;
450	}
451
452	return 0;
453}
454