]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
3b744f4079f582ae4d839e9af308171e30d3f307
[tcpdump] / tcpdump.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
29 static const char copyright[] =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 static const char rcsid[] =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.215 2003-09-16 21:02:52 guy Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #ifdef WIN32
51 #include "getopt.h"
52 #include "w32_fzs.h"
53 extern int strcasecmp (const char *__s1, const char *__s2);
54 extern int SIZE_BUF;
55 #define off_t long
56 #define uint UINT
57 #endif /* WIN32 */
58
59 #include <pcap.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "interface.h"
66 #include "addrtoname.h"
67 #include "machdep.h"
68 #include "setsignal.h"
69 #include "gmt2local.h"
70 #include "pcap-missing.h"
71
72 int dflag; /* print filter code */
73 int eflag; /* print ethernet header */
74 int fflag; /* don't translate "foreign" IP address */
75 int Lflag; /* list available data link types and exit */
76 int nflag; /* leave addresses as numbers */
77 int Nflag; /* remove domains from printed host names */
78 int Oflag = 1; /* run filter code optimizer */
79 int pflag; /* don't go promiscuous */
80 int qflag; /* quick (shorter) output */
81 int Rflag = 1; /* print sequence # field in AH/ESP*/
82 int sflag = 0; /* use the libsmi to translate OIDs */
83 int Sflag; /* print raw TCP sequence numbers */
84 int tflag = 1; /* print packet arrival time */
85 int Uflag = 0; /* "unbuffered" output of dump files */
86 int uflag = 0; /* Print undecoded NFS handles */
87 int vflag; /* verbose */
88 int xflag; /* print packet in hex */
89 int Xflag; /* print packet in ascii as well as hex */
90 off_t Cflag = 0; /* rotate dump files after this many bytes */
91 int Aflag = 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
92 int dlt = -1; /* if != -1, ask libpcap for the DLT it names */
93
94 const char *dlt_name = NULL;
95
96 char *espsecret = NULL; /* ESP secret key */
97
98 int packettype;
99
100 static int infodelay;
101 static int infoprint;
102
103 char *program_name;
104
105 int32_t thiszone; /* seconds offset from gmt to local time */
106
107 /* Forwards */
108 static RETSIGTYPE cleanup(int);
109 static void usage(void) __attribute__((noreturn));
110 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
111
112 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
113 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
114 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
115
116 #ifdef SIGINFO
117 RETSIGTYPE requestinfo(int);
118 #endif
119
120 static void info(int);
121 static u_int packets_captured;
122
123 /* Length of saved portion of packet. */
124 int snaplen = DEFAULT_SNAPLEN;
125
126 typedef u_int (*if_printer)(const struct pcap_pkthdr *, const u_char *);
127
128 struct printer {
129 if_printer f;
130 int type;
131 };
132
133 static struct printer printers[] = {
134 { arcnet_if_print, DLT_ARCNET },
135 #ifdef DLT_ARCNET_LINUX
136 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
137 #endif
138 { ether_if_print, DLT_EN10MB },
139 { token_if_print, DLT_IEEE802 },
140 #ifdef DLT_LANE8023
141 { lane_if_print, DLT_LANE8023 },
142 #endif
143 #ifdef DLT_CIP
144 { cip_if_print, DLT_CIP },
145 #endif
146 #ifdef DLT_ATM_CLIP
147 { cip_if_print, DLT_ATM_CLIP },
148 #endif
149 { sl_if_print, DLT_SLIP },
150 #ifdef DLT_SLIP_BSDOS
151 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
152 #endif
153 { ppp_if_print, DLT_PPP },
154 #ifdef DLT_PPP_BSDOS
155 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
156 #endif
157 { fddi_if_print, DLT_FDDI },
158 { null_if_print, DLT_NULL },
159 #ifdef DLT_LOOP
160 { null_if_print, DLT_LOOP },
161 #endif
162 { raw_if_print, DLT_RAW },
163 { atm_if_print, DLT_ATM_RFC1483 },
164 #ifdef DLT_C_HDLC
165 { chdlc_if_print, DLT_C_HDLC },
166 #endif
167 #ifdef DLT_HDLC
168 { chdlc_if_print, DLT_HDLC },
169 #endif
170 #ifdef DLT_PPP_SERIAL
171 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
172 #endif
173 #ifdef DLT_PPP_ETHER
174 { pppoe_if_print, DLT_PPP_ETHER },
175 #endif
176 #ifdef DLT_LINUX_SLL
177 { sll_if_print, DLT_LINUX_SLL },
178 #endif
179 #ifdef DLT_IEEE802_11
180 { ieee802_11_if_print, DLT_IEEE802_11},
181 #endif
182 #ifdef DLT_LTALK
183 { ltalk_if_print, DLT_LTALK },
184 #endif
185 #ifdef DLT_PFLOG
186 { pflog_if_print, DLT_PFLOG },
187 #endif
188 #ifdef DLT_FR
189 { fr_if_print, DLT_FR },
190 #endif
191 #ifdef DLT_FRELAY
192 { fr_if_print, DLT_FRELAY },
193 #endif
194 #ifdef DLT_SUNATM
195 { sunatm_if_print, DLT_SUNATM },
196 #endif
197 #ifdef DLT_IP_OVER_FC
198 { ipfc_if_print, DLT_IP_OVER_FC },
199 #endif
200 #ifdef DLT_PRISM_HEADER
201 { prism_if_print, DLT_PRISM_HEADER },
202 #endif
203 #ifdef DLT_IEEE802_11_RADIO
204 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
205 #endif
206 #ifdef DLT_ENC
207 { enc_if_print, DLT_ENC },
208 #endif
209 { NULL, 0 },
210 };
211
212 static if_printer
213 lookup_printer(int type)
214 {
215 struct printer *p;
216
217 for (p = printers; p->f; ++p)
218 if (type == p->type)
219 return p->f;
220
221 return NULL;
222 /* NOTREACHED */
223 }
224
225 static pcap_t *pd;
226
227 extern int optind;
228 extern int opterr;
229 extern char *optarg;
230
231 struct print_info {
232 if_printer printer;
233 };
234
235 struct dump_info {
236 char *WFileName;
237 pcap_t *pd;
238 pcap_dumper_t *p;
239 };
240
241 static void
242 show_dlts_and_exit(pcap_t *pd)
243 {
244 int n_dlts;
245 int *dlts = 0;
246 const char *dlt_name;
247
248 n_dlts = pcap_list_datalinks(pd, &dlts);
249 if (n_dlts < 0)
250 error("%s", pcap_geterr(pd));
251 else if (n_dlts == 0 || !dlts)
252 error("No data link types.");
253
254 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
255
256 while (--n_dlts >= 0) {
257 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
258 if (dlt_name != NULL) {
259 (void) fprintf(stderr, " %s", dlt_name);
260
261 /*
262 * OK, does tcpdump handle that type?
263 */
264 if (lookup_printer(dlts[n_dlts]) == NULL)
265 (void) fprintf(stderr, " (not supported)");
266 putchar('\n');
267 } else {
268 (void) fprintf(stderr, " DLT %d (not supported)\n",
269 dlts[n_dlts]);
270 }
271 }
272 free(dlts);
273 exit(0);
274 }
275
276 /*
277 * Set up flags that might or might not be supported depending on the
278 * version of libpcap we're using.
279 */
280 #ifdef WIN32
281 #define B_FLAG "B:"
282 #define B_FLAG_USAGE " [ -B size ]"
283 #else /* WIN32 */
284 #define B_FLAG
285 #define B_FLAG_USAGE
286 #endif /* WIN32 */
287
288 #ifdef HAVE_PCAP_FINDALLDEVS
289 #define D_FLAG "D"
290 #else
291 #define D_FLAG
292 #endif
293
294 #ifdef HAVE_PCAP_DUMP_FLUSH
295 #define U_FLAG "U"
296 #else
297 #define U_FLAG
298 #endif
299
300 int
301 main(int argc, char **argv)
302 {
303 register int cnt, op, i;
304 bpf_u_int32 localnet, netmask;
305 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
306 pcap_handler callback;
307 int type;
308 struct bpf_program fcode;
309 #ifndef WIN32
310 RETSIGTYPE (*oldhandler)(int);
311 #endif
312 struct print_info printinfo;
313 struct dump_info dumpinfo;
314 u_char *pcap_userdata;
315 char ebuf[PCAP_ERRBUF_SIZE];
316 #ifdef HAVE_PCAP_FINDALLDEVS
317 pcap_if_t *devpointer;
318 int devnum;
319 #endif
320 #ifdef WIN32
321 u_int UserBufferSize = 1000000;
322 if(wsockinit() != 0) return 1;
323 #endif /* WIN32 */
324
325 cnt = -1;
326 device = NULL;
327 infile = NULL;
328 RFileName = NULL;
329 WFileName = NULL;
330 if ((cp = strrchr(argv[0], '/')) != NULL)
331 program_name = cp + 1;
332 else
333 program_name = argv[0];
334
335 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
336 error("%s", ebuf);
337
338 #ifdef LIBSMI
339 smiInit("tcpdump");
340 #endif
341
342 opterr = 0;
343 while (
344 (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:i:lLm:nNOpqr:Rs:StT:u" U_FLAG "vw:xXy:Y")) != -1)
345 switch (op) {
346
347 case 'a':
348 /* compatibility for old -a */
349 break;
350
351 case 'A':
352 ++xflag;
353 ++Xflag;
354 ++Aflag;
355 break;
356
357 #ifdef WIN32
358 case 'B':
359 UserBufferSize = atoi(optarg)*1024;
360 if (UserBufferSize < 0)
361 error("invalid packet buffer size %s", optarg);
362 break;
363 #endif /* WIN32 */
364
365 case 'c':
366 cnt = atoi(optarg);
367 if (cnt <= 0)
368 error("invalid packet count %s", optarg);
369 break;
370
371 case 'C':
372 Cflag = atoi(optarg) * 1000000;
373 if (Cflag < 0)
374 error("invalid file size %s", optarg);
375 break;
376
377 case 'd':
378 ++dflag;
379 break;
380
381 #ifdef HAVE_PCAP_FINDALLDEVS
382 case 'D':
383 if (pcap_findalldevs(&devpointer, ebuf) < 0)
384 error("%s", ebuf);
385 else {
386 for (i = 0; devpointer != 0; i++) {
387 printf("%d.%s", i+1, devpointer->name);
388 if (devpointer->description != NULL)
389 printf(" (%s)", devpointer->description);
390 printf("\n");
391 devpointer = devpointer->next;
392 }
393 }
394 return 0;
395 #endif /* HAVE_PCAP_FINDALLDEVS */
396
397 case 'L':
398 Lflag++;
399 break;
400
401 case 'e':
402 ++eflag;
403 break;
404
405 case 'E':
406 #ifndef HAVE_LIBCRYPTO
407 warning("crypto code not compiled in");
408 #endif
409 espsecret = optarg;
410 break;
411
412 case 'f':
413 ++fflag;
414 break;
415
416 case 'F':
417 infile = optarg;
418 break;
419
420 case 'i':
421 if (optarg[0] == '0' && optarg[1] == 0)
422 error("Invalid adapter index");
423
424 #ifdef HAVE_PCAP_FINDALLDEVS
425 /*
426 * If the argument is a number, treat it as
427 * an index into the list of adapters, as
428 * printed by "tcpdump -D".
429 *
430 * This should be OK on UNIX systems, as interfaces
431 * shouldn't have names that begin with digits.
432 * It can be useful on Windows, where more than
433 * one interface can have the same name.
434 */
435 if ((devnum = atoi(optarg)) != 0) {
436 if (devnum < 0)
437 error("Invalid adapter index");
438
439 if (pcap_findalldevs(&devpointer, ebuf) < 0)
440 error("%s", ebuf);
441 else {
442 for (i = 0; i < devnum-1; i++){
443 devpointer = devpointer->next;
444 if (devpointer == NULL)
445 error("Invalid adapter index");
446 }
447 }
448 device = devpointer->name;
449 break;
450 }
451 #endif /* HAVE_PCAP_FINDALLDEVS */
452 device = optarg;
453 break;
454
455 case 'l':
456 #ifdef HAVE_SETLINEBUF
457 setlinebuf(stdout);
458 #else
459 setvbuf(stdout, NULL, _IOLBF, 0);
460 #endif
461 break;
462
463 case 'n':
464 ++nflag;
465 break;
466
467 case 'N':
468 ++Nflag;
469 break;
470
471 case 'm':
472 #ifdef LIBSMI
473 if (smiLoadModule(optarg) == 0) {
474 error("could not load MIB module %s", optarg);
475 }
476 sflag = 1;
477 #else
478 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
479 program_name, optarg);
480 (void)fprintf(stderr, "(no libsmi support)\n");
481 #endif
482
483 case 'O':
484 Oflag = 0;
485 break;
486
487 case 'p':
488 ++pflag;
489 break;
490
491 case 'q':
492 ++qflag;
493 break;
494
495 case 'r':
496 RFileName = optarg;
497 break;
498
499 case 'R':
500 Rflag = 0;
501 break;
502
503 case 's': {
504 char *end;
505
506 snaplen = strtol(optarg, &end, 0);
507 if (optarg == end || *end != '\0'
508 || snaplen < 0 || snaplen > 65535)
509 error("invalid snaplen %s", optarg);
510 else if (snaplen == 0)
511 snaplen = 65535;
512 break;
513 }
514
515 case 'S':
516 ++Sflag;
517 break;
518
519 case 't':
520 --tflag;
521 break;
522
523 case 'T':
524 if (strcasecmp(optarg, "vat") == 0)
525 packettype = PT_VAT;
526 else if (strcasecmp(optarg, "wb") == 0)
527 packettype = PT_WB;
528 else if (strcasecmp(optarg, "rpc") == 0)
529 packettype = PT_RPC;
530 else if (strcasecmp(optarg, "rtp") == 0)
531 packettype = PT_RTP;
532 else if (strcasecmp(optarg, "rtcp") == 0)
533 packettype = PT_RTCP;
534 else if (strcasecmp(optarg, "snmp") == 0)
535 packettype = PT_SNMP;
536 else if (strcasecmp(optarg, "cnfp") == 0)
537 packettype = PT_CNFP;
538 else if (strcasecmp(optarg, "tftp") == 0)
539 packettype = PT_TFTP;
540 else if (strcasecmp(optarg, "aodv") == 0)
541 packettype = PT_AODV;
542 else
543 error("unknown packet type `%s'", optarg);
544 break;
545
546 case 'u':
547 ++uflag;
548 break;
549
550 #ifdef HAVE_PCAP_DUMP_FLUSH
551 case 'U':
552 ++Uflag;
553 break;
554 #endif
555
556 case 'v':
557 ++vflag;
558 break;
559
560 case 'w':
561 WFileName = optarg;
562 break;
563
564 case 'x':
565 ++xflag;
566 break;
567
568 case 'X':
569 ++xflag;
570 ++Xflag;
571 break;
572
573 case 'y':
574 dlt_name = optarg;
575 dlt = pcap_datalink_name_to_val(dlt_name);
576 if (dlt < 0)
577 error("invalid data link type %s", dlt_name);
578 break;
579
580 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
581 case 'Y':
582 {
583 /* Undocumented flag */
584 #ifdef HAVE_PCAP_DEBUG
585 extern int pcap_debug;
586 pcap_debug = 1;
587 #else
588 extern int yydebug;
589 yydebug = 1;
590 #endif
591 }
592 break;
593 #endif
594 default:
595 usage();
596 /* NOTREACHED */
597 }
598
599 if (tflag > 0)
600 thiszone = gmt2local(0);
601
602 if (RFileName != NULL) {
603 int dlt;
604 const char *dlt_name;
605
606 #ifndef WIN32
607 /*
608 * We don't need network access, so relinquish any set-UID
609 * or set-GID privileges we have (if any).
610 *
611 * We do *not* want set-UID privileges when opening a
612 * trace file, as that might let the user read other
613 * people's trace files (especially if we're set-UID
614 * root).
615 */
616 setuid(getuid());
617 #endif /* WIN32 */
618 pd = pcap_open_offline(RFileName, ebuf);
619 if (pd == NULL)
620 error("%s", ebuf);
621 dlt = pcap_datalink(pd);
622 dlt_name = pcap_datalink_val_to_name(dlt);
623 if (dlt_name == NULL)
624 dlt_name = "???";
625 printf("reading from file %s, link-type %u (%s)\n",
626 RFileName, dlt, dlt_name);
627 localnet = 0;
628 netmask = 0;
629 if (fflag != 0)
630 error("-f and -r options are incompatible");
631 } else {
632 if (device == NULL) {
633 device = pcap_lookupdev(ebuf);
634 if (device == NULL)
635 error("%s", ebuf);
636 }
637 #ifdef WIN32
638 if(IsTextUnicode(device,
639 wcslen((short*)device), // Device always ends with a double \0, so this way to determine its
640 // length should be always valid
641 NULL))
642 {
643 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
644 }
645 else
646 {
647 fprintf(stderr, "%s: listening on %s\n", program_name, device);
648 }
649
650 fflush(stderr);
651 #endif /* WIN32 */
652 *ebuf = '\0';
653 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
654 if (pd == NULL)
655 error("%s", ebuf);
656 else if (*ebuf)
657 warning("%s", ebuf);
658 #ifdef WIN32
659 if(UserBufferSize != 1000000)
660 if(pcap_setbuff(pd, UserBufferSize)==-1){
661 error("%s", pcap_geterr(pd));
662 }
663 #endif /* WIN32 */
664 if (Lflag)
665 show_dlts_and_exit(pd);
666 if (dlt >= 0) {
667 #ifdef HAVE_PCAP_SET_DATALINK
668 if (pcap_set_datalink(pd, dlt) < 0)
669 error("%s", pcap_geterr(pd));
670 #else
671 /*
672 * We don't actually support changing the
673 * data link type, so we only let them
674 * set it to what it already is.
675 */
676 if (dlt != pcap_datalink(pd)) {
677 error("%s is not one of the DLTs supported by this device\n",
678 dlt_name);
679 }
680 #endif
681 (void)fprintf(stderr, "%s: data link type %s\n",
682 program_name, dlt_name);
683 (void)fflush(stderr);
684 }
685 i = pcap_snapshot(pd);
686 if (snaplen < i) {
687 warning("snaplen raised from %d to %d", snaplen, i);
688 snaplen = i;
689 }
690 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
691 localnet = 0;
692 netmask = 0;
693 warning("%s", ebuf);
694 }
695 /*
696 * Let user own process after socket has been opened.
697 */
698 #ifndef WIN32
699 setuid(getuid());
700 #endif /* WIN32 */
701 }
702 if (infile)
703 cmdbuf = read_infile(infile);
704 else
705 cmdbuf = copy_argv(&argv[optind]);
706
707 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
708 error("%s", pcap_geterr(pd));
709 if (dflag) {
710 bpf_dump(&fcode, dflag);
711 pcap_close(pd);
712 exit(0);
713 }
714 init_addrtoname(localnet, netmask);
715
716 #ifndef WIN32
717 (void)setsignal(SIGPIPE, cleanup);
718 #endif /* WIN32 */
719 (void)setsignal(SIGTERM, cleanup);
720 (void)setsignal(SIGINT, cleanup);
721 /* Cooperate with nohup(1) */
722 #ifndef WIN32
723 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
724 (void)setsignal(SIGHUP, oldhandler);
725 #endif /* WIN32 */
726
727 if (pcap_setfilter(pd, &fcode) < 0)
728 error("%s", pcap_geterr(pd));
729 if (WFileName) {
730 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
731 if (p == NULL)
732 error("%s", pcap_geterr(pd));
733 if (Cflag != 0) {
734 callback = dump_packet_and_trunc;
735 dumpinfo.WFileName = WFileName;
736 dumpinfo.pd = pd;
737 dumpinfo.p = p;
738 pcap_userdata = (u_char *)&dumpinfo;
739 } else {
740 callback = dump_packet;
741 pcap_userdata = (u_char *)p;
742 }
743 } else {
744 type = pcap_datalink(pd);
745 printinfo.printer = lookup_printer(type);
746 if (printinfo.printer == NULL) {
747 dlt_name = pcap_datalink_val_to_name(type);
748 if (dlt_name != NULL)
749 error("unsupported data link type %s", dlt_name);
750 else
751 error("unsupported data link type %d", type);
752 }
753 callback = print_packet;
754 pcap_userdata = (u_char *)&printinfo;
755 }
756 #ifdef SIGINFO
757 (void)setsignal(SIGINFO, requestinfo);
758 #endif
759 #ifndef WIN32
760 if (RFileName == NULL) {
761 int dlt;
762 const char *dlt_name;
763
764 if (!vflag && !WFileName) {
765 (void)fprintf(stderr,
766 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
767 program_name);
768 } else
769 (void)fprintf(stderr, "%s: ", program_name);
770 dlt = pcap_datalink(pd);
771 dlt_name = pcap_datalink_val_to_name(dlt);
772 if (dlt_name == NULL)
773 dlt_name = "???";
774 (void)fprintf(stderr, "listening on %s, link-type %u (%s), capture size %u bytes\n",
775 device, dlt, dlt_name, snaplen);
776 (void)fflush(stderr);
777 }
778 #endif /* WIN32 */
779 if (pcap_loop(pd, cnt, callback, pcap_userdata) < 0) {
780 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
781 program_name, pcap_geterr(pd));
782 cleanup(0);
783 pcap_close(pd);
784 exit(1);
785 }
786 if (RFileName == NULL)
787 info(1);
788 pcap_close(pd);
789 exit(0);
790 }
791
792 /* make a clean exit on interrupts */
793 static RETSIGTYPE
794 cleanup(int signo)
795 {
796
797 /* Can't print the summary if reading from a savefile */
798 if (pd != NULL && pcap_file(pd) == NULL) {
799 (void)fflush(stdout);
800 putc('\n', stderr);
801 info(1);
802 }
803 if (signo)
804 exit(0);
805 }
806
807 static void
808 info(register int verbose)
809 {
810 struct pcap_stat stat;
811
812 if (pcap_stats(pd, &stat) < 0) {
813 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
814 return;
815 }
816
817 if (!verbose)
818 fprintf(stderr, "%s: ", program_name);
819
820 (void)fprintf(stderr, "%u packets captured", packets_captured);
821 if (!verbose)
822 fputs(", ", stderr);
823 else
824 putc('\n', stderr);
825 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
826 if (!verbose)
827 fputs(", ", stderr);
828 else
829 putc('\n', stderr);
830 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
831 infoprint = 0;
832 }
833
834 static void
835 reverse(char *s)
836 {
837 int i, j, c;
838
839 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
840 c = s[i];
841 s[i] = s[j];
842 s[j] = c;
843 }
844 }
845
846
847 static void
848 swebitoa(unsigned int n, char *s)
849 {
850 unsigned int i;
851
852 i = 0;
853 do {
854 s[i++] = n % 10 + '0';
855 } while ((n /= 10) > 0);
856
857 s[i] = '\0';
858 reverse(s);
859 }
860
861 static void
862 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
863 {
864 struct dump_info *dump_info;
865 static uint cnt = 2;
866 char *name;
867
868 ++packets_captured;
869
870 ++infodelay;
871
872 dump_info = (struct dump_info *)user;
873
874 /*
875 * XXX - this won't prevent capture files from getting
876 * larger than Cflag - the last packet written to the
877 * file could put it over Cflag.
878 */
879 if (ftell((FILE *)dump_info->p) > Cflag) {
880 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
881 if (name == NULL)
882 error("dump_packet_and_trunc: malloc");
883 strcpy(name, dump_info->WFileName);
884 swebitoa(cnt, name + strlen(dump_info->WFileName));
885 cnt++;
886 pcap_dump_close(dump_info->p);
887 dump_info->p = pcap_dump_open(dump_info->pd, name);
888 free(name);
889 if (dump_info->p == NULL)
890 error("%s", pcap_geterr(pd));
891 }
892
893 pcap_dump((u_char *)dump_info->p, h, sp);
894 #ifdef HAVE_PCAP_DUMP_FLUSH
895 if (Uflag)
896 pcap_dump_flush(dump_info->p);
897 #endif
898
899 --infodelay;
900 if (infoprint)
901 info(0);
902 }
903
904 static void
905 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
906 {
907 ++packets_captured;
908
909 ++infodelay;
910
911 pcap_dump(user, h, sp);
912 #ifdef HAVE_PCAP_DUMP_FLUSH
913 if (Uflag)
914 pcap_dump_flush((pcap_dumper_t *)user);
915 #endif
916
917 --infodelay;
918 if (infoprint)
919 info(0);
920 }
921
922 static void
923 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
924 {
925 struct print_info *print_info;
926 u_int hdrlen;
927
928 ++packets_captured;
929
930 ++infodelay;
931 ts_print(&h->ts);
932
933 print_info = (struct print_info *)user;
934
935 /*
936 * Some printers want to check that they're not walking off the
937 * end of the packet.
938 * Rather than pass it all the way down, we set this global.
939 */
940 snapend = sp + h->caplen;
941
942 hdrlen = (*print_info->printer)(h, sp);
943 if (xflag) {
944 /*
945 * Print the raw packet data.
946 */
947 if (xflag > 1) {
948 /*
949 * Include the link-layer header.
950 */
951 default_print_unaligned(sp, h->caplen);
952 } else {
953 /*
954 * Don't include the link-layer header - and if
955 * we have nothing past the link-layer header,
956 * print nothing.
957 */
958 if (h->caplen > hdrlen)
959 default_print_unaligned(sp + hdrlen,
960 h->caplen - hdrlen);
961 }
962 }
963
964 putchar('\n');
965
966 --infodelay;
967 if (infoprint)
968 info(0);
969 }
970
971 /* Like default_print() but data need not be aligned */
972 void
973 default_print_unaligned(register const u_char *cp, register u_int length)
974 {
975 register u_int i, s;
976 register int nshorts;
977
978 if (Xflag) {
979 ascii_print(cp, length);
980 return;
981 }
982 nshorts = (u_int) length / sizeof(u_short);
983 i = 0;
984 while (--nshorts >= 0) {
985 if ((i++ % 8) == 0)
986 (void)printf("\n\t\t\t");
987 s = *cp++;
988 (void)printf(" %02x%02x", s, *cp++);
989 }
990 if (length & 1) {
991 if ((i % 8) == 0)
992 (void)printf("\n\t\t\t");
993 (void)printf(" %02x", *cp);
994 }
995 }
996
997 #ifdef WIN32
998 /*
999 * XXX - there should really be libpcap calls to get the version
1000 * number as a string (the string would be generated from #defines
1001 * at run time, so that it's not generated from string constants
1002 * in the library, as, on many UNIX systems, those constants would
1003 * be statically linked into the application executable image, and
1004 * would thus reflect the version of libpcap on the system on
1005 * which the application was *linked*, not the system on which it's
1006 * *running*.
1007 *
1008 * That routine should be documented, unlike the "version[]"
1009 * string, so that UNIX vendors providing their own libpcaps
1010 * don't omit it (as a couple of vendors have...).
1011 *
1012 * Packet.dll should perhaps also export a routine to return the
1013 * version number of the Packet.dll code, to supply the
1014 * "Wpcap_version" information on Windows.
1015 */
1016 char WDversion[]="current-cvs.tcpdump.org";
1017 char version[]="current-cvs.tcpdump.org";
1018 char pcap_version[]="current-cvs.tcpdump.org";
1019 char Wpcap_version[]="3.0 alpha";
1020 #endif
1021
1022 /*
1023 * By default, print the specified data out in hex.
1024 */
1025 void
1026 default_print(register const u_char *bp, register u_int length)
1027 {
1028 default_print_unaligned(bp, length);
1029 }
1030
1031 #ifdef SIGINFO
1032 RETSIGTYPE requestinfo(int signo _U_)
1033 {
1034 if (infodelay)
1035 ++infoprint;
1036 else
1037 info(0);
1038 }
1039 #endif
1040
1041 static void
1042 usage(void)
1043 {
1044 extern char version[];
1045 #ifndef HAVE_PCAP_LIB_VERSION
1046 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1047 extern char pcap_version[];
1048 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1049 static char pcap_version[] = "unknown";
1050 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1051 #endif /* HAVE_PCAP_LIB_VERSION */
1052
1053 #ifdef HAVE_PCAP_LIB_VERSION
1054 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1055 (void)fprintf(stderr, "%s\n", pcap_lib_version());
1056 #else /* HAVE_PCAP_LIB_VERSION */
1057 #ifdef WIN32
1058 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1059 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1060 #else /* WIN32 */
1061 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1062 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1063 #endif /* WIN32 */
1064 #endif /* HAVE_PCAP_LIB_VERSION */
1065 (void)fprintf(stderr,
1066 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1067 (void)fprintf(stderr,
1068 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1069 (void)fprintf(stderr,
1070 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1071 (void)fprintf(stderr,
1072 "\t\t[ expression ]\n");
1073 exit(1);
1074 }