]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
ef3edc9ee134949b75a5cadd319f7c7e44d58907
[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.203 2003-02-11 08:04:18 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 aflag; /* translate network and broadcast addresses */
73 int dflag; /* print filter code */
74 int eflag; /* print ethernet header */
75 int fflag; /* don't translate "foreign" IP address */
76 int Lflag; /* list available data link types and exit */
77 int nflag; /* leave addresses as numbers */
78 int Nflag; /* remove domains from printed host names */
79 int Oflag = 1; /* run filter code optimizer */
80 int pflag; /* don't go promiscuous */
81 int qflag; /* quick (shorter) output */
82 int Rflag = 1; /* print sequence # field in AH/ESP*/
83 int sflag = 0; /* use the libsmi to translate OIDs */
84 int Sflag; /* print raw TCP sequence numbers */
85 int tflag = 1; /* print packet arrival time */
86 int Uflag = 0; /* "unbuffered" output of dump files */
87 int uflag = 0; /* Print undecoded NFS handles */
88 int vflag; /* verbose */
89 int xflag; /* print packet in hex */
90 int Xflag; /* print packet in ascii as well as hex */
91 off_t Cflag = 0; /* rotate dump files after this many bytes */
92 int Aflag = 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
93 int dlt = -1; /* if != -1, ask libpcap for the DLT it names */
94
95 const char *dlt_name = NULL;
96
97 char *espsecret = NULL; /* ESP secret key */
98
99 int packettype;
100
101 static int infodelay;
102 static int infoprint;
103
104 char *program_name;
105
106 int32_t thiszone; /* seconds offset from gmt to local time */
107
108 /* Forwards */
109 static RETSIGTYPE cleanup(int);
110 static void usage(void) __attribute__((noreturn));
111 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
112
113 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
114 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
115 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
116
117 #ifdef SIGINFO
118 RETSIGTYPE requestinfo(int);
119 #endif
120
121 static void info(int);
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 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
151 { ppp_if_print, DLT_PPP },
152 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
153 { fddi_if_print, DLT_FDDI },
154 { null_if_print, DLT_NULL },
155 #ifdef DLT_LOOP
156 { null_if_print, DLT_LOOP },
157 #endif
158 { raw_if_print, DLT_RAW },
159 { atm_if_print, DLT_ATM_RFC1483 },
160 #ifdef DLT_C_HDLC
161 { chdlc_if_print, DLT_C_HDLC },
162 #endif
163 #ifdef DLT_HDLC
164 { chdlc_if_print, DLT_HDLC },
165 #endif
166 #ifdef DLT_PPP_SERIAL
167 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
168 #endif
169 #ifdef DLT_PPP_ETHER
170 { pppoe_if_print, DLT_PPP_ETHER },
171 #endif
172 #ifdef DLT_LINUX_SLL
173 { sll_if_print, DLT_LINUX_SLL },
174 #endif
175 #ifdef DLT_IEEE802_11
176 { ieee802_11_if_print, DLT_IEEE802_11},
177 #endif
178 #ifdef DLT_LTALK
179 { ltalk_if_print, DLT_LTALK },
180 #endif
181 #ifdef DLT_PFLOG
182 { pflog_if_print, DLT_PFLOG },
183 #endif
184 #ifdef DLT_FR
185 { fr_if_print, DLT_FR },
186 #endif
187 #ifdef DLT_FRELAY
188 { fr_if_print, DLT_FRELAY },
189 #endif
190 #ifdef DLT_SUNATM
191 { sunatm_if_print, DLT_SUNATM },
192 #endif
193 #ifdef DLT_IP_OVER_FC
194 { ipfc_if_print, DLT_IP_OVER_FC },
195 #endif
196 #ifdef DLT_PRISM_HEADER
197 { prism_if_print, DLT_PRISM_HEADER },
198 #endif
199 #ifdef DLT_IEEE802_11_RADIO
200 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
201 #endif
202 { NULL, 0 },
203 };
204
205 static if_printer
206 lookup_printer(int type)
207 {
208 struct printer *p;
209
210 for (p = printers; p->f; ++p)
211 if (type == p->type)
212 return p->f;
213
214 return NULL;
215 /* NOTREACHED */
216 }
217
218 static pcap_t *pd;
219
220 extern int optind;
221 extern int opterr;
222 extern char *optarg;
223
224 struct print_info {
225 if_printer printer;
226 };
227
228 struct dump_info {
229 char *WFileName;
230 pcap_t *pd;
231 pcap_dumper_t *p;
232 };
233
234 static void
235 show_dlts_and_exit(pcap_t *pd)
236 {
237 int n_dlts;
238 int *dlts = 0;
239 const char *dlt_name;
240
241 n_dlts = pcap_list_datalinks(pd, &dlts);
242 if (n_dlts < 0)
243 error("%s", pcap_geterr(pd));
244 else if (n_dlts == 0 || !dlts)
245 error("No data link types.");
246
247 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
248
249 while (--n_dlts >= 0) {
250 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
251 if (dlt_name != NULL) {
252 (void) fprintf(stderr, " %s", dlt_name);
253
254 /*
255 * OK, does tcpdump handle that type?
256 */
257 if (lookup_printer(dlts[n_dlts]) == NULL)
258 (void) fprintf(stderr, " (not supported)");
259 putchar('\n');
260 } else {
261 (void) fprintf(stderr, " DLT %d (not supported)\n",
262 dlts[n_dlts]);
263 }
264 }
265 free(dlts);
266 exit(0);
267 }
268
269 /*
270 * Set up flags that might or might not be supported depending on the
271 * version of libpcap we're using.
272 */
273 #ifdef WIN32
274 #define B_FLAG "B:"
275 #define B_FLAG_USAGE " [ -B size ]"
276 #else /* WIN32 */
277 #define B_FLAG
278 #define B_FLAG_USAGE
279 #endif /* WIN32 */
280
281 #ifdef HAVE_PCAP_FINDALLDEVS
282 #define D_FLAG "D"
283 #else
284 #define D_FLAG
285 #endif
286
287 #ifdef HAVE_PCAP_DUMP_FLUSH
288 #define U_FLAG "U"
289 #else
290 #define U_FLAG
291 #endif
292
293 int
294 main(int argc, char **argv)
295 {
296 register int cnt, op, i;
297 bpf_u_int32 localnet, netmask;
298 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
299 pcap_handler callback;
300 int type;
301 struct bpf_program fcode;
302 #ifndef WIN32
303 RETSIGTYPE (*oldhandler)(int);
304 #endif
305 struct print_info printinfo;
306 struct dump_info dumpinfo;
307 u_char *pcap_userdata;
308 char ebuf[PCAP_ERRBUF_SIZE];
309 #ifdef HAVE_PCAP_FINDALLDEVS
310 pcap_if_t *devpointer;
311 int devnum;
312 #endif
313 #ifdef WIN32
314 DWORD dwVersion;
315 DWORD dwWindowsMajorVersion;
316 u_int UserBufferSize=1000000;
317 #endif
318
319 #ifdef WIN32
320 dwVersion=GetVersion(); /* get the OS version */
321 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
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 ++aflag;
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
539 error("unknown packet type `%s'", optarg);
540 break;
541
542 case 'u':
543 ++uflag;
544 break;
545
546 #ifdef HAVE_PCAP_DUMP_FLUSH
547 case 'U':
548 ++Uflag;
549 break;
550 #endif
551
552 case 'v':
553 ++vflag;
554 break;
555
556 case 'w':
557 WFileName = optarg;
558 break;
559
560 case 'x':
561 ++xflag;
562 break;
563
564 case 'X':
565 ++xflag;
566 ++Xflag;
567 break;
568
569 case 'y':
570 dlt_name = optarg;
571 dlt = pcap_datalink_name_to_val(dlt_name);
572 if (dlt < 0)
573 error("invalid data link type %s", dlt_name);
574 break;
575
576 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
577 case 'Y':
578 {
579 /* Undocumented flag */
580 #ifdef HAVE_PCAP_DEBUG
581 extern int pcap_debug;
582 pcap_debug = 1;
583 #else
584 extern int yydebug;
585 yydebug = 1;
586 #endif
587 }
588 break;
589 #endif
590 default:
591 usage();
592 /* NOTREACHED */
593 }
594
595 if (aflag && nflag)
596 error("-a and -n options are incompatible");
597
598 if (tflag > 0)
599 thiszone = gmt2local(0);
600
601 if (RFileName != NULL) {
602 /*
603 * We don't need network access, so set it back to the user id.
604 * Also, this prevents the user from reading anyone's
605 * trace file.
606 */
607 #ifndef WIN32
608 setuid(getuid());
609 #endif /* WIN32 */
610
611 pd = pcap_open_offline(RFileName, ebuf);
612 if (pd == NULL)
613 error("%s", ebuf);
614 localnet = 0;
615 netmask = 0;
616 if (fflag != 0)
617 error("-f and -r options are incompatible");
618 } else {
619 if (device == NULL) {
620 device = pcap_lookupdev(ebuf);
621 if (device == NULL)
622 error("%s", ebuf);
623 }
624 #ifdef WIN32
625 PrintCapBegins(program_name,device);
626 #endif /* WIN32 */
627 *ebuf = '\0';
628 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
629 if (pd == NULL)
630 error("%s", ebuf);
631 else if (*ebuf)
632 warning("%s", ebuf);
633 #ifdef WIN32
634 if(UserBufferSize != 1000000)
635 if(pcap_setbuff(pd, UserBufferSize)==-1){
636 error("%s", pcap_geterr(pd));
637 }
638 #endif /* WIN32 */
639 if (Lflag)
640 show_dlts_and_exit(pd);
641 if (dlt >= 0) {
642 #ifdef HAVE_PCAP_SET_DATALINK
643 if (pcap_set_datalink(pd, dlt) < 0)
644 error("%s", pcap_geterr(pd));
645 #else
646 /*
647 * We don't actually support changing the
648 * data link type, so we only let them
649 * set it to what it already is.
650 */
651 if (dlt != pcap_datalink(pd)) {
652 error("%s is not one of the DLTs supported by this device\n",
653 dlt_name);
654 }
655 #endif
656 (void)fprintf(stderr, "%s: data link type %s\n",
657 program_name, dlt_name);
658 (void)fflush(stderr);
659 }
660 i = pcap_snapshot(pd);
661 if (snaplen < i) {
662 warning("snaplen raised from %d to %d", snaplen, i);
663 snaplen = i;
664 }
665 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
666 localnet = 0;
667 netmask = 0;
668 warning("%s", ebuf);
669 }
670 /*
671 * Let user own process after socket has been opened.
672 */
673 #ifndef WIN32
674 setuid(getuid());
675 #endif /* WIN32 */
676 }
677 if (infile)
678 cmdbuf = read_infile(infile);
679 else
680 cmdbuf = copy_argv(&argv[optind]);
681
682 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
683 error("%s", pcap_geterr(pd));
684 if (dflag) {
685 bpf_dump(&fcode, dflag);
686 pcap_close(pd);
687 exit(0);
688 }
689 init_addrtoname(localnet, netmask);
690
691 (void)setsignal(SIGTERM, cleanup);
692 (void)setsignal(SIGINT, cleanup);
693 /* Cooperate with nohup(1) */
694 #ifndef WIN32
695 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
696 (void)setsignal(SIGHUP, oldhandler);
697 #endif /* WIN32 */
698
699 if (pcap_setfilter(pd, &fcode) < 0)
700 error("%s", pcap_geterr(pd));
701 if (WFileName) {
702 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
703 if (p == NULL)
704 error("%s", pcap_geterr(pd));
705 if (Cflag != 0) {
706 callback = dump_packet_and_trunc;
707 dumpinfo.WFileName = WFileName;
708 dumpinfo.pd = pd;
709 dumpinfo.p = p;
710 pcap_userdata = (u_char *)&dumpinfo;
711 } else {
712 callback = dump_packet;
713 pcap_userdata = (u_char *)p;
714 }
715 } else {
716 type = pcap_datalink(pd);
717 printinfo.printer = lookup_printer(type);
718 if (printinfo.printer == NULL) {
719 dlt_name = pcap_datalink_val_to_name(type);
720 if (dlt_name != NULL)
721 error("unsupported data link type %s", dlt_name);
722 else
723 error("unsupported data link type %d", type);
724 }
725 callback = print_packet;
726 pcap_userdata = (u_char *)&printinfo;
727 }
728 #ifdef SIGINFO
729 (void)setsignal(SIGINFO, requestinfo);
730 #endif
731 #ifndef WIN32
732 if (RFileName == NULL) {
733 if (!vflag && !WFileName) {
734 (void)fprintf(stderr,
735 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
736 program_name);
737 } else
738 (void)fprintf(stderr, "%s: ", program_name);
739 (void)fprintf(stderr, "listening on %s, capture size %u bytes\n",
740 device, snaplen);
741 (void)fflush(stderr);
742 }
743 #endif /* WIN32 */
744 if (pcap_loop(pd, cnt, callback, pcap_userdata) < 0) {
745 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
746 program_name, pcap_geterr(pd));
747 cleanup(0);
748 pcap_close(pd);
749 exit(1);
750 }
751 if (RFileName == NULL)
752 info(1);
753 pcap_close(pd);
754 exit(0);
755 }
756
757 /* make a clean exit on interrupts */
758 static RETSIGTYPE
759 cleanup(int signo)
760 {
761
762 /* Can't print the summary if reading from a savefile */
763 if (pd != NULL && pcap_file(pd) == NULL) {
764 (void)fflush(stdout);
765 putc('\n', stderr);
766 info(1);
767 }
768 if (signo)
769 exit(0);
770 }
771
772 static void
773 info(register int verbose)
774 {
775 struct pcap_stat stat;
776
777 if (pcap_stats(pd, &stat) < 0) {
778 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
779 return;
780 }
781
782 if (!verbose)
783 fprintf(stderr, "%s: ", program_name);
784
785 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
786 if (!verbose)
787 fputs(", ", stderr);
788 else
789 putc('\n', stderr);
790 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
791 infoprint = 0;
792 }
793
794 static void
795 reverse(char *s)
796 {
797 int i, j, c;
798
799 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
800 c = s[i];
801 s[i] = s[j];
802 s[j] = c;
803 }
804 }
805
806
807 static void
808 swebitoa(unsigned int n, char *s)
809 {
810 unsigned int i;
811
812 i = 0;
813 do {
814 s[i++] = n % 10 + '0';
815 } while ((n /= 10) > 0);
816
817 s[i] = '\0';
818 reverse(s);
819 }
820
821 static void
822 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
823 {
824 struct dump_info *dump_info;
825 static uint cnt = 2;
826 char *name;
827
828 ++infodelay;
829
830 dump_info = (struct dump_info *)user;
831
832 /*
833 * XXX - this won't prevent capture files from getting
834 * larger than Cflag - the last packet written to the
835 * file could put it over Cflag.
836 */
837 if (ftell((FILE *)dump_info->p) > Cflag) {
838 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
839 if (name == NULL)
840 error("dump_packet_and_trunc: malloc");
841 strcpy(name, dump_info->WFileName);
842 swebitoa(cnt, name + strlen(dump_info->WFileName));
843 cnt++;
844 pcap_dump_close(dump_info->p);
845 dump_info->p = pcap_dump_open(dump_info->pd, name);
846 free(name);
847 if (dump_info->p == NULL)
848 error("%s", pcap_geterr(pd));
849 }
850
851 pcap_dump((u_char *)dump_info->p, h, sp);
852 #ifdef HAVE_PCAP_DUMP_FLUSH
853 if (Uflag)
854 pcap_dump_flush(dump_info->p);
855 #endif
856
857 --infodelay;
858 if (infoprint)
859 info(0);
860 }
861
862 static void
863 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
864 {
865 ++infodelay;
866
867 pcap_dump(user, h, sp);
868 #ifdef HAVE_PCAP_DUMP_FLUSH
869 if (Uflag)
870 pcap_dump_flush((pcap_dumper_t *)user);
871 #endif
872
873 --infodelay;
874 if (infoprint)
875 info(0);
876 }
877
878 static void
879 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
880 {
881 struct print_info *print_info;
882 u_int hdrlen;
883
884 ++infodelay;
885 ts_print(&h->ts);
886
887 print_info = (struct print_info *)user;
888
889 /*
890 * Some printers want to check that they're not walking off the
891 * end of the packet.
892 * Rather than pass it all the way down, we set this global.
893 */
894 snapend = sp + h->caplen;
895
896 hdrlen = (*print_info->printer)(h, sp);
897 if (xflag) {
898 /*
899 * Print the raw packet data.
900 */
901 if (xflag > 1) {
902 /*
903 * Include the link-layer header.
904 */
905 default_print_unaligned(sp, h->caplen);
906 } else {
907 /*
908 * Don't include the link-layer header - and if
909 * we have nothing past the link-layer header,
910 * print nothing.
911 */
912 if (h->caplen > hdrlen)
913 default_print_unaligned(sp + hdrlen,
914 h->caplen - hdrlen);
915 }
916 }
917
918 putchar('\n');
919
920 --infodelay;
921 if (infoprint)
922 info(0);
923 }
924
925 /* Like default_print() but data need not be aligned */
926 void
927 default_print_unaligned(register const u_char *cp, register u_int length)
928 {
929 register u_int i, s;
930 register int nshorts;
931
932 if (Xflag) {
933 ascii_print(cp, length);
934 return;
935 }
936 nshorts = (u_int) length / sizeof(u_short);
937 i = 0;
938 while (--nshorts >= 0) {
939 if ((i++ % 8) == 0)
940 (void)printf("\n\t\t\t");
941 s = *cp++;
942 (void)printf(" %02x%02x", s, *cp++);
943 }
944 if (length & 1) {
945 if ((i % 8) == 0)
946 (void)printf("\n\t\t\t");
947 (void)printf(" %02x", *cp);
948 }
949 }
950
951 #ifdef WIN32
952 /*
953 * XXX - there should really be libpcap calls to get the version
954 * number as a string (the string would be generated from #defines
955 * at run time, so that it's not generated from string constants
956 * in the library, as, on many UNIX systems, those constants would
957 * be statically linked into the application executable image, and
958 * would thus reflect the version of libpcap on the system on
959 * which the application was *linked*, not the system on which it's
960 * *running*.
961 *
962 * That routine should be documented, unlike the "version[]"
963 * string, so that UNIX vendors providing their own libpcaps
964 * don't omit it (as a couple of vendors have...).
965 *
966 * Packet.dll should perhaps also export a routine to return the
967 * version number of the Packet.dll code, to supply the
968 * "Wpcap_version" information on Windows.
969 */
970 char WDversion[]="current-cvs.tcpdump.org";
971 char version[]="current-cvs.tcpdump.org";
972 char pcap_version[]="current-cvs.tcpdump.org";
973 char Wpcap_version[]="3.0 alpha";
974 #endif
975
976 /*
977 * By default, print the specified data out in hex.
978 */
979 void
980 default_print(register const u_char *bp, register u_int length)
981 {
982 default_print_unaligned(bp, length);
983 }
984
985 #ifdef SIGINFO
986 RETSIGTYPE requestinfo(int signo _U_)
987 {
988 if (infodelay)
989 ++infoprint;
990 else
991 info(0);
992 }
993 #endif
994
995 static void
996 usage(void)
997 {
998 extern char version[];
999 #ifndef HAVE_PCAP_LIB_VERSION
1000 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1001 extern char pcap_version[];
1002 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1003 static char pcap_version[] = "unknown";
1004 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1005 #endif /* HAVE_PCAP_LIB_VERSION */
1006
1007 #ifdef HAVE_PCAP_LIB_VERSION
1008 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1009 (void)fprintf(stderr, "%s\n", pcap_lib_version());
1010 #else /* HAVE_PCAP_LIB_VERSION */
1011 #ifdef WIN32
1012 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1013 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1014 #else /* WIN32 */
1015 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1016 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1017 #endif /* WIN32 */
1018 #endif /* HAVE_PCAP_LIB_VERSION */
1019 (void)fprintf(stderr,
1020 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1021 (void)fprintf(stderr,
1022 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1023 (void)fprintf(stderr,
1024 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1025 (void)fprintf(stderr,
1026 "\t\t[ expression ]\n");
1027 exit(1);
1028 }