]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
Have the configure script arrange that the Makefile define _U_
[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[] _U_ =
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[] _U_ =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.218 2003-11-16 09:36:44 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 int status;
321 #ifdef WIN32
322 u_int UserBufferSize = 1000000;
323 if(wsockinit() != 0) return 1;
324 #endif /* WIN32 */
325
326 cnt = -1;
327 device = NULL;
328 infile = NULL;
329 RFileName = NULL;
330 WFileName = NULL;
331 if ((cp = strrchr(argv[0], '/')) != NULL)
332 program_name = cp + 1;
333 else
334 program_name = argv[0];
335
336 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
337 error("%s", ebuf);
338
339 #ifdef LIBSMI
340 smiInit("tcpdump");
341 #endif
342
343 opterr = 0;
344 while (
345 (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)
346 switch (op) {
347
348 case 'a':
349 /* compatibility for old -a */
350 break;
351
352 case 'A':
353 ++xflag;
354 ++Xflag;
355 ++Aflag;
356 break;
357
358 #ifdef WIN32
359 case 'B':
360 UserBufferSize = atoi(optarg)*1024;
361 if (UserBufferSize < 0)
362 error("invalid packet buffer size %s", optarg);
363 break;
364 #endif /* WIN32 */
365
366 case 'c':
367 cnt = atoi(optarg);
368 if (cnt <= 0)
369 error("invalid packet count %s", optarg);
370 break;
371
372 case 'C':
373 Cflag = atoi(optarg) * 1000000;
374 if (Cflag < 0)
375 error("invalid file size %s", optarg);
376 break;
377
378 case 'd':
379 ++dflag;
380 break;
381
382 #ifdef HAVE_PCAP_FINDALLDEVS
383 case 'D':
384 if (pcap_findalldevs(&devpointer, ebuf) < 0)
385 error("%s", ebuf);
386 else {
387 for (i = 0; devpointer != 0; i++) {
388 printf("%d.%s", i+1, devpointer->name);
389 if (devpointer->description != NULL)
390 printf(" (%s)", devpointer->description);
391 printf("\n");
392 devpointer = devpointer->next;
393 }
394 }
395 return 0;
396 #endif /* HAVE_PCAP_FINDALLDEVS */
397
398 case 'L':
399 Lflag++;
400 break;
401
402 case 'e':
403 ++eflag;
404 break;
405
406 case 'E':
407 #ifndef HAVE_LIBCRYPTO
408 warning("crypto code not compiled in");
409 #endif
410 espsecret = optarg;
411 break;
412
413 case 'f':
414 ++fflag;
415 break;
416
417 case 'F':
418 infile = optarg;
419 break;
420
421 case 'i':
422 if (optarg[0] == '0' && optarg[1] == 0)
423 error("Invalid adapter index");
424
425 #ifdef HAVE_PCAP_FINDALLDEVS
426 /*
427 * If the argument is a number, treat it as
428 * an index into the list of adapters, as
429 * printed by "tcpdump -D".
430 *
431 * This should be OK on UNIX systems, as interfaces
432 * shouldn't have names that begin with digits.
433 * It can be useful on Windows, where more than
434 * one interface can have the same name.
435 */
436 if ((devnum = atoi(optarg)) != 0) {
437 if (devnum < 0)
438 error("Invalid adapter index");
439
440 if (pcap_findalldevs(&devpointer, ebuf) < 0)
441 error("%s", ebuf);
442 else {
443 for (i = 0; i < devnum-1; i++){
444 devpointer = devpointer->next;
445 if (devpointer == NULL)
446 error("Invalid adapter index");
447 }
448 }
449 device = devpointer->name;
450 break;
451 }
452 #endif /* HAVE_PCAP_FINDALLDEVS */
453 device = optarg;
454 break;
455
456 case 'l':
457 #ifdef HAVE_SETLINEBUF
458 setlinebuf(stdout);
459 #else
460 setvbuf(stdout, NULL, _IOLBF, 0);
461 #endif
462 break;
463
464 case 'n':
465 ++nflag;
466 break;
467
468 case 'N':
469 ++Nflag;
470 break;
471
472 case 'm':
473 #ifdef LIBSMI
474 if (smiLoadModule(optarg) == 0) {
475 error("could not load MIB module %s", optarg);
476 }
477 sflag = 1;
478 #else
479 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
480 program_name, optarg);
481 (void)fprintf(stderr, "(no libsmi support)\n");
482 #endif
483
484 case 'O':
485 Oflag = 0;
486 break;
487
488 case 'p':
489 ++pflag;
490 break;
491
492 case 'q':
493 ++qflag;
494 break;
495
496 case 'r':
497 RFileName = optarg;
498 break;
499
500 case 'R':
501 Rflag = 0;
502 break;
503
504 case 's': {
505 char *end;
506
507 snaplen = strtol(optarg, &end, 0);
508 if (optarg == end || *end != '\0'
509 || snaplen < 0 || snaplen > 65535)
510 error("invalid snaplen %s", optarg);
511 else if (snaplen == 0)
512 snaplen = 65535;
513 break;
514 }
515
516 case 'S':
517 ++Sflag;
518 break;
519
520 case 't':
521 --tflag;
522 break;
523
524 case 'T':
525 if (strcasecmp(optarg, "vat") == 0)
526 packettype = PT_VAT;
527 else if (strcasecmp(optarg, "wb") == 0)
528 packettype = PT_WB;
529 else if (strcasecmp(optarg, "rpc") == 0)
530 packettype = PT_RPC;
531 else if (strcasecmp(optarg, "rtp") == 0)
532 packettype = PT_RTP;
533 else if (strcasecmp(optarg, "rtcp") == 0)
534 packettype = PT_RTCP;
535 else if (strcasecmp(optarg, "snmp") == 0)
536 packettype = PT_SNMP;
537 else if (strcasecmp(optarg, "cnfp") == 0)
538 packettype = PT_CNFP;
539 else if (strcasecmp(optarg, "tftp") == 0)
540 packettype = PT_TFTP;
541 else if (strcasecmp(optarg, "aodv") == 0)
542 packettype = PT_AODV;
543 else
544 error("unknown packet type `%s'", optarg);
545 break;
546
547 case 'u':
548 ++uflag;
549 break;
550
551 #ifdef HAVE_PCAP_DUMP_FLUSH
552 case 'U':
553 ++Uflag;
554 break;
555 #endif
556
557 case 'v':
558 ++vflag;
559 break;
560
561 case 'w':
562 WFileName = optarg;
563 break;
564
565 case 'x':
566 ++xflag;
567 break;
568
569 case 'X':
570 ++xflag;
571 ++Xflag;
572 break;
573
574 case 'y':
575 dlt_name = optarg;
576 dlt = pcap_datalink_name_to_val(dlt_name);
577 if (dlt < 0)
578 error("invalid data link type %s", dlt_name);
579 break;
580
581 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
582 case 'Y':
583 {
584 /* Undocumented flag */
585 #ifdef HAVE_PCAP_DEBUG
586 extern int pcap_debug;
587 pcap_debug = 1;
588 #else
589 extern int yydebug;
590 yydebug = 1;
591 #endif
592 }
593 break;
594 #endif
595 default:
596 usage();
597 /* NOTREACHED */
598 }
599
600 if (tflag > 0)
601 thiszone = gmt2local(0);
602
603 if (RFileName != NULL) {
604 int dlt;
605 const char *dlt_name;
606
607 #ifndef WIN32
608 /*
609 * We don't need network access, so relinquish any set-UID
610 * or set-GID privileges we have (if any).
611 *
612 * We do *not* want set-UID privileges when opening a
613 * trace file, as that might let the user read other
614 * people's trace files (especially if we're set-UID
615 * root).
616 */
617 setuid(getuid());
618 #endif /* WIN32 */
619 pd = pcap_open_offline(RFileName, ebuf);
620 if (pd == NULL)
621 error("%s", ebuf);
622 dlt = pcap_datalink(pd);
623 dlt_name = pcap_datalink_val_to_name(dlt);
624 if (dlt_name == NULL)
625 dlt_name = "???";
626 printf("reading from file %s, link-type %u (%s)\n",
627 RFileName, dlt, dlt_name);
628 localnet = 0;
629 netmask = 0;
630 if (fflag != 0)
631 error("-f and -r options are incompatible");
632 } else {
633 if (device == NULL) {
634 device = pcap_lookupdev(ebuf);
635 if (device == NULL)
636 error("%s", ebuf);
637 }
638 #ifdef WIN32
639 if(IsTextUnicode(device,
640 wcslen((short*)device), // Device always ends with a double \0, so this way to determine its
641 // length should be always valid
642 NULL))
643 {
644 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
645 }
646 else
647 {
648 fprintf(stderr, "%s: listening on %s\n", program_name, device);
649 }
650
651 fflush(stderr);
652 #endif /* WIN32 */
653 *ebuf = '\0';
654 pd = pcap_open_live(device, snaplen, !pflag, 10000, ebuf);
655 if (pd == NULL)
656 error("%s", ebuf);
657 else if (*ebuf)
658 warning("%s", ebuf);
659 #ifdef WIN32
660 if(UserBufferSize != 1000000)
661 if(pcap_setbuff(pd, UserBufferSize)==-1){
662 error("%s", pcap_geterr(pd));
663 }
664 #endif /* WIN32 */
665 if (Lflag)
666 show_dlts_and_exit(pd);
667 if (dlt >= 0) {
668 #ifdef HAVE_PCAP_SET_DATALINK
669 if (pcap_set_datalink(pd, dlt) < 0)
670 error("%s", pcap_geterr(pd));
671 #else
672 /*
673 * We don't actually support changing the
674 * data link type, so we only let them
675 * set it to what it already is.
676 */
677 if (dlt != pcap_datalink(pd)) {
678 error("%s is not one of the DLTs supported by this device\n",
679 dlt_name);
680 }
681 #endif
682 (void)fprintf(stderr, "%s: data link type %s\n",
683 program_name, dlt_name);
684 (void)fflush(stderr);
685 }
686 i = pcap_snapshot(pd);
687 if (snaplen < i) {
688 warning("snaplen raised from %d to %d", snaplen, i);
689 snaplen = i;
690 }
691 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
692 localnet = 0;
693 netmask = 0;
694 warning("%s", ebuf);
695 }
696 /*
697 * Let user own process after socket has been opened.
698 */
699 #ifndef WIN32
700 setuid(getuid());
701 #endif /* WIN32 */
702 }
703 if (infile)
704 cmdbuf = read_infile(infile);
705 else
706 cmdbuf = copy_argv(&argv[optind]);
707
708 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
709 error("%s", pcap_geterr(pd));
710 if (dflag) {
711 bpf_dump(&fcode, dflag);
712 pcap_close(pd);
713 exit(0);
714 }
715 init_addrtoname(localnet, netmask);
716
717 #ifndef WIN32
718 (void)setsignal(SIGPIPE, cleanup);
719 #endif /* WIN32 */
720 (void)setsignal(SIGTERM, cleanup);
721 (void)setsignal(SIGINT, cleanup);
722 /* Cooperate with nohup(1) */
723 #ifndef WIN32
724 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
725 (void)setsignal(SIGHUP, oldhandler);
726 #endif /* WIN32 */
727
728 if (pcap_setfilter(pd, &fcode) < 0)
729 error("%s", pcap_geterr(pd));
730 if (WFileName) {
731 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
732 if (p == NULL)
733 error("%s", pcap_geterr(pd));
734 if (Cflag != 0) {
735 callback = dump_packet_and_trunc;
736 dumpinfo.WFileName = WFileName;
737 dumpinfo.pd = pd;
738 dumpinfo.p = p;
739 pcap_userdata = (u_char *)&dumpinfo;
740 } else {
741 callback = dump_packet;
742 pcap_userdata = (u_char *)p;
743 }
744 } else {
745 type = pcap_datalink(pd);
746 printinfo.printer = lookup_printer(type);
747 if (printinfo.printer == NULL) {
748 dlt_name = pcap_datalink_val_to_name(type);
749 if (dlt_name != NULL)
750 error("unsupported data link type %s", dlt_name);
751 else
752 error("unsupported data link type %d", type);
753 }
754 callback = print_packet;
755 pcap_userdata = (u_char *)&printinfo;
756 }
757 #ifdef SIGINFO
758 (void)setsignal(SIGINFO, requestinfo);
759 #endif
760 #ifndef WIN32
761 if (RFileName == NULL) {
762 int dlt;
763 const char *dlt_name;
764
765 if (!vflag && !WFileName) {
766 (void)fprintf(stderr,
767 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
768 program_name);
769 } else
770 (void)fprintf(stderr, "%s: ", program_name);
771 dlt = pcap_datalink(pd);
772 dlt_name = pcap_datalink_val_to_name(dlt);
773 if (dlt_name == NULL)
774 dlt_name = "???";
775 (void)fprintf(stderr, "listening on %s, link-type %u (%s), capture size %u bytes\n",
776 device, dlt, dlt_name, snaplen);
777 (void)fflush(stderr);
778 }
779 #endif /* WIN32 */
780 status = pcap_loop(pd, cnt, callback, pcap_userdata);
781 if (WFileName == NULL) {
782 /*
783 * We're printing packets. Flush the printed output,
784 * so it doesn't get intermingled with error output.
785 */
786 if (status == -2) {
787 /*
788 * We got interrupted, so perhaps we didn't
789 * manage to finish a line we were printing.
790 * Print an extra newline, just in case.
791 */
792 putchar('\n');
793 }
794 (void)fflush(stdout);
795 }
796 if (status == -1) {
797 /*
798 * Error. Report it.
799 */
800 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
801 program_name, pcap_geterr(pd));
802 }
803 if (RFileName == NULL) {
804 /*
805 * We're doing a live capture. Report the capture
806 * statistics.
807 */
808 info(1);
809 }
810 pcap_close(pd);
811 exit(status == -1 ? 1 : 0);
812 }
813
814 /* make a clean exit on interrupts */
815 static RETSIGTYPE
816 cleanup(int signo _U_)
817 {
818 #ifdef HAVE_PCAP_BREAKLOOP
819 /*
820 * We have "pcap_breakloop()"; use it, so that we do as little
821 * as possible in the signal handler (it's probably not safe
822 * to do anything with standard I/O streams in a signal handler -
823 * the ANSI C standard doesn't say it is).
824 */
825 pcap_breakloop(pd);
826 #else
827 /*
828 * We don't have "pcap_breakloop()"; this isn't safe, but
829 * it's the best we can do. Print the summary if we're
830 * not reading from a savefile - i.e., if we're doing a
831 * live capture - and exit.
832 */
833 if (pd != NULL && pcap_file(pd) == NULL) {
834 /*
835 * We got interrupted, so perhaps we didn't
836 * manage to finish a line we were printing.
837 * Print an extra newline, just in case.
838 */
839 putchar('\n');
840 (void)fflush(stdout);
841 info(1);
842 }
843 exit(0);
844 #endif
845 }
846
847 static void
848 info(register int verbose)
849 {
850 struct pcap_stat stat;
851
852 if (pcap_stats(pd, &stat) < 0) {
853 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
854 return;
855 }
856
857 if (!verbose)
858 fprintf(stderr, "%s: ", program_name);
859
860 (void)fprintf(stderr, "%u packets captured", packets_captured);
861 if (!verbose)
862 fputs(", ", stderr);
863 else
864 putc('\n', stderr);
865 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
866 if (!verbose)
867 fputs(", ", stderr);
868 else
869 putc('\n', stderr);
870 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
871 infoprint = 0;
872 }
873
874 static void
875 reverse(char *s)
876 {
877 int i, j, c;
878
879 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
880 c = s[i];
881 s[i] = s[j];
882 s[j] = c;
883 }
884 }
885
886
887 static void
888 swebitoa(unsigned int n, char *s)
889 {
890 unsigned int i;
891
892 i = 0;
893 do {
894 s[i++] = n % 10 + '0';
895 } while ((n /= 10) > 0);
896
897 s[i] = '\0';
898 reverse(s);
899 }
900
901 static void
902 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
903 {
904 struct dump_info *dump_info;
905 static uint cnt = 2;
906 char *name;
907
908 ++packets_captured;
909
910 ++infodelay;
911
912 dump_info = (struct dump_info *)user;
913
914 /*
915 * XXX - this won't prevent capture files from getting
916 * larger than Cflag - the last packet written to the
917 * file could put it over Cflag.
918 */
919 if (ftell((FILE *)dump_info->p) > Cflag) {
920 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
921 if (name == NULL)
922 error("dump_packet_and_trunc: malloc");
923 strcpy(name, dump_info->WFileName);
924 swebitoa(cnt, name + strlen(dump_info->WFileName));
925 cnt++;
926 pcap_dump_close(dump_info->p);
927 dump_info->p = pcap_dump_open(dump_info->pd, name);
928 free(name);
929 if (dump_info->p == NULL)
930 error("%s", pcap_geterr(pd));
931 }
932
933 pcap_dump((u_char *)dump_info->p, h, sp);
934 #ifdef HAVE_PCAP_DUMP_FLUSH
935 if (Uflag)
936 pcap_dump_flush(dump_info->p);
937 #endif
938
939 --infodelay;
940 if (infoprint)
941 info(0);
942 }
943
944 static void
945 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
946 {
947 ++packets_captured;
948
949 ++infodelay;
950
951 pcap_dump(user, h, sp);
952 #ifdef HAVE_PCAP_DUMP_FLUSH
953 if (Uflag)
954 pcap_dump_flush((pcap_dumper_t *)user);
955 #endif
956
957 --infodelay;
958 if (infoprint)
959 info(0);
960 }
961
962 static void
963 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
964 {
965 struct print_info *print_info;
966 u_int hdrlen;
967
968 ++packets_captured;
969
970 ++infodelay;
971 ts_print(&h->ts);
972
973 print_info = (struct print_info *)user;
974
975 /*
976 * Some printers want to check that they're not walking off the
977 * end of the packet.
978 * Rather than pass it all the way down, we set this global.
979 */
980 snapend = sp + h->caplen;
981
982 hdrlen = (*print_info->printer)(h, sp);
983 if (xflag) {
984 /*
985 * Print the raw packet data.
986 */
987 if (xflag > 1) {
988 /*
989 * Include the link-layer header.
990 */
991 default_print_unaligned(sp, h->caplen);
992 } else {
993 /*
994 * Don't include the link-layer header - and if
995 * we have nothing past the link-layer header,
996 * print nothing.
997 */
998 if (h->caplen > hdrlen)
999 default_print_unaligned(sp + hdrlen,
1000 h->caplen - hdrlen);
1001 }
1002 }
1003
1004 putchar('\n');
1005
1006 --infodelay;
1007 if (infoprint)
1008 info(0);
1009 }
1010
1011 /* Like default_print() but data need not be aligned */
1012 void
1013 default_print_unaligned(register const u_char *cp, register u_int length)
1014 {
1015 register u_int i, s;
1016 register int nshorts;
1017
1018 if (Xflag) {
1019 ascii_print(cp, length);
1020 return;
1021 }
1022 nshorts = (u_int) length / sizeof(u_short);
1023 i = 0;
1024 while (--nshorts >= 0) {
1025 if ((i++ % 8) == 0)
1026 (void)printf("\n\t\t\t");
1027 s = *cp++;
1028 (void)printf(" %02x%02x", s, *cp++);
1029 }
1030 if (length & 1) {
1031 if ((i % 8) == 0)
1032 (void)printf("\n\t\t\t");
1033 (void)printf(" %02x", *cp);
1034 }
1035 }
1036
1037 #ifdef WIN32
1038 /*
1039 * XXX - there should really be libpcap calls to get the version
1040 * number as a string (the string would be generated from #defines
1041 * at run time, so that it's not generated from string constants
1042 * in the library, as, on many UNIX systems, those constants would
1043 * be statically linked into the application executable image, and
1044 * would thus reflect the version of libpcap on the system on
1045 * which the application was *linked*, not the system on which it's
1046 * *running*.
1047 *
1048 * That routine should be documented, unlike the "version[]"
1049 * string, so that UNIX vendors providing their own libpcaps
1050 * don't omit it (as a couple of vendors have...).
1051 *
1052 * Packet.dll should perhaps also export a routine to return the
1053 * version number of the Packet.dll code, to supply the
1054 * "Wpcap_version" information on Windows.
1055 */
1056 char WDversion[]="current-cvs.tcpdump.org";
1057 char version[]="current-cvs.tcpdump.org";
1058 char pcap_version[]="current-cvs.tcpdump.org";
1059 char Wpcap_version[]="3.0 alpha";
1060 #endif
1061
1062 /*
1063 * By default, print the specified data out in hex.
1064 */
1065 void
1066 default_print(register const u_char *bp, register u_int length)
1067 {
1068 default_print_unaligned(bp, length);
1069 }
1070
1071 #ifdef SIGINFO
1072 RETSIGTYPE requestinfo(int signo _U_)
1073 {
1074 if (infodelay)
1075 ++infoprint;
1076 else
1077 info(0);
1078 }
1079 #endif
1080
1081 static void
1082 usage(void)
1083 {
1084 extern char version[];
1085 #ifndef HAVE_PCAP_LIB_VERSION
1086 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1087 extern char pcap_version[];
1088 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1089 static char pcap_version[] = "unknown";
1090 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1091 #endif /* HAVE_PCAP_LIB_VERSION */
1092
1093 #ifdef HAVE_PCAP_LIB_VERSION
1094 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1095 (void)fprintf(stderr, "%s\n", pcap_lib_version());
1096 #else /* HAVE_PCAP_LIB_VERSION */
1097 #ifdef WIN32
1098 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1099 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1100 #else /* WIN32 */
1101 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1102 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1103 #endif /* WIN32 */
1104 #endif /* HAVE_PCAP_LIB_VERSION */
1105 (void)fprintf(stderr,
1106 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1107 (void)fprintf(stderr,
1108 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1109 (void)fprintf(stderr,
1110 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1111 (void)fprintf(stderr,
1112 "\t\t[ expression ]\n");
1113 exit(1);
1114 }