]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
verify/warn icmpv6 checksum. from jinmei@kame
[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.204 2003-03-08 08:55:34 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 #ifdef DLT_ENC
203 { enc_if_print, DLT_ENC },
204 #endif
205 { NULL, 0 },
206 };
207
208 static if_printer
209 lookup_printer(int type)
210 {
211 struct printer *p;
212
213 for (p = printers; p->f; ++p)
214 if (type == p->type)
215 return p->f;
216
217 return NULL;
218 /* NOTREACHED */
219 }
220
221 static pcap_t *pd;
222
223 extern int optind;
224 extern int opterr;
225 extern char *optarg;
226
227 struct print_info {
228 if_printer printer;
229 };
230
231 struct dump_info {
232 char *WFileName;
233 pcap_t *pd;
234 pcap_dumper_t *p;
235 };
236
237 static void
238 show_dlts_and_exit(pcap_t *pd)
239 {
240 int n_dlts;
241 int *dlts = 0;
242 const char *dlt_name;
243
244 n_dlts = pcap_list_datalinks(pd, &dlts);
245 if (n_dlts < 0)
246 error("%s", pcap_geterr(pd));
247 else if (n_dlts == 0 || !dlts)
248 error("No data link types.");
249
250 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
251
252 while (--n_dlts >= 0) {
253 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
254 if (dlt_name != NULL) {
255 (void) fprintf(stderr, " %s", dlt_name);
256
257 /*
258 * OK, does tcpdump handle that type?
259 */
260 if (lookup_printer(dlts[n_dlts]) == NULL)
261 (void) fprintf(stderr, " (not supported)");
262 putchar('\n');
263 } else {
264 (void) fprintf(stderr, " DLT %d (not supported)\n",
265 dlts[n_dlts]);
266 }
267 }
268 free(dlts);
269 exit(0);
270 }
271
272 /*
273 * Set up flags that might or might not be supported depending on the
274 * version of libpcap we're using.
275 */
276 #ifdef WIN32
277 #define B_FLAG "B:"
278 #define B_FLAG_USAGE " [ -B size ]"
279 #else /* WIN32 */
280 #define B_FLAG
281 #define B_FLAG_USAGE
282 #endif /* WIN32 */
283
284 #ifdef HAVE_PCAP_FINDALLDEVS
285 #define D_FLAG "D"
286 #else
287 #define D_FLAG
288 #endif
289
290 #ifdef HAVE_PCAP_DUMP_FLUSH
291 #define U_FLAG "U"
292 #else
293 #define U_FLAG
294 #endif
295
296 int
297 main(int argc, char **argv)
298 {
299 register int cnt, op, i;
300 bpf_u_int32 localnet, netmask;
301 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
302 pcap_handler callback;
303 int type;
304 struct bpf_program fcode;
305 #ifndef WIN32
306 RETSIGTYPE (*oldhandler)(int);
307 #endif
308 struct print_info printinfo;
309 struct dump_info dumpinfo;
310 u_char *pcap_userdata;
311 char ebuf[PCAP_ERRBUF_SIZE];
312 #ifdef HAVE_PCAP_FINDALLDEVS
313 pcap_if_t *devpointer;
314 int devnum;
315 #endif
316 #ifdef WIN32
317 DWORD dwVersion;
318 DWORD dwWindowsMajorVersion;
319 u_int UserBufferSize=1000000;
320 #endif
321
322 #ifdef WIN32
323 dwVersion=GetVersion(); /* get the OS version */
324 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
325 if(wsockinit()!=0) return 1;
326 #endif /* WIN32 */
327
328 cnt = -1;
329 device = NULL;
330 infile = NULL;
331 RFileName = NULL;
332 WFileName = NULL;
333 if ((cp = strrchr(argv[0], '/')) != NULL)
334 program_name = cp + 1;
335 else
336 program_name = argv[0];
337
338 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
339 error("%s", ebuf);
340
341 #ifdef LIBSMI
342 smiInit("tcpdump");
343 #endif
344
345 opterr = 0;
346 while (
347 (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)
348 switch (op) {
349
350 case 'a':
351 ++aflag;
352 break;
353
354 case 'A':
355 ++xflag;
356 ++Xflag;
357 ++Aflag;
358 break;
359
360 #ifdef WIN32
361 case 'B':
362 UserBufferSize = atoi(optarg)*1024;
363 if (UserBufferSize < 0)
364 error("invalid packet buffer size %s", optarg);
365 break;
366 #endif /* WIN32 */
367
368 case 'c':
369 cnt = atoi(optarg);
370 if (cnt <= 0)
371 error("invalid packet count %s", optarg);
372 break;
373
374 case 'C':
375 Cflag = atoi(optarg) * 1000000;
376 if (Cflag < 0)
377 error("invalid file size %s", optarg);
378 break;
379
380 case 'd':
381 ++dflag;
382 break;
383
384 #ifdef HAVE_PCAP_FINDALLDEVS
385 case 'D':
386 if (pcap_findalldevs(&devpointer, ebuf) < 0)
387 error("%s", ebuf);
388 else {
389 for (i = 0; devpointer != 0; i++) {
390 printf("%d.%s", i+1, devpointer->name);
391 if (devpointer->description != NULL)
392 printf(" (%s)", devpointer->description);
393 printf("\n");
394 devpointer = devpointer->next;
395 }
396 }
397 return 0;
398 #endif /* HAVE_PCAP_FINDALLDEVS */
399
400 case 'L':
401 Lflag++;
402 break;
403
404 case 'e':
405 ++eflag;
406 break;
407
408 case 'E':
409 #ifndef HAVE_LIBCRYPTO
410 warning("crypto code not compiled in");
411 #endif
412 espsecret = optarg;
413 break;
414
415 case 'f':
416 ++fflag;
417 break;
418
419 case 'F':
420 infile = optarg;
421 break;
422
423 case 'i':
424 if (optarg[0] == '0' && optarg[1] == 0)
425 error("Invalid adapter index");
426
427 #ifdef HAVE_PCAP_FINDALLDEVS
428 /*
429 * If the argument is a number, treat it as
430 * an index into the list of adapters, as
431 * printed by "tcpdump -D".
432 *
433 * This should be OK on UNIX systems, as interfaces
434 * shouldn't have names that begin with digits.
435 * It can be useful on Windows, where more than
436 * one interface can have the same name.
437 */
438 if ((devnum = atoi(optarg)) != 0) {
439 if (devnum < 0)
440 error("Invalid adapter index");
441
442 if (pcap_findalldevs(&devpointer, ebuf) < 0)
443 error("%s", ebuf);
444 else {
445 for (i = 0; i < devnum-1; i++){
446 devpointer = devpointer->next;
447 if (devpointer == NULL)
448 error("Invalid adapter index");
449 }
450 }
451 device = devpointer->name;
452 break;
453 }
454 #endif /* HAVE_PCAP_FINDALLDEVS */
455 device = optarg;
456 break;
457
458 case 'l':
459 #ifdef HAVE_SETLINEBUF
460 setlinebuf(stdout);
461 #else
462 setvbuf(stdout, NULL, _IOLBF, 0);
463 #endif
464 break;
465
466 case 'n':
467 ++nflag;
468 break;
469
470 case 'N':
471 ++Nflag;
472 break;
473
474 case 'm':
475 #ifdef LIBSMI
476 if (smiLoadModule(optarg) == 0) {
477 error("could not load MIB module %s", optarg);
478 }
479 sflag = 1;
480 #else
481 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
482 program_name, optarg);
483 (void)fprintf(stderr, "(no libsmi support)\n");
484 #endif
485
486 case 'O':
487 Oflag = 0;
488 break;
489
490 case 'p':
491 ++pflag;
492 break;
493
494 case 'q':
495 ++qflag;
496 break;
497
498 case 'r':
499 RFileName = optarg;
500 break;
501
502 case 'R':
503 Rflag = 0;
504 break;
505
506 case 's': {
507 char *end;
508
509 snaplen = strtol(optarg, &end, 0);
510 if (optarg == end || *end != '\0'
511 || snaplen < 0 || snaplen > 65535)
512 error("invalid snaplen %s", optarg);
513 else if (snaplen == 0)
514 snaplen = 65535;
515 break;
516 }
517
518 case 'S':
519 ++Sflag;
520 break;
521
522 case 't':
523 --tflag;
524 break;
525
526 case 'T':
527 if (strcasecmp(optarg, "vat") == 0)
528 packettype = PT_VAT;
529 else if (strcasecmp(optarg, "wb") == 0)
530 packettype = PT_WB;
531 else if (strcasecmp(optarg, "rpc") == 0)
532 packettype = PT_RPC;
533 else if (strcasecmp(optarg, "rtp") == 0)
534 packettype = PT_RTP;
535 else if (strcasecmp(optarg, "rtcp") == 0)
536 packettype = PT_RTCP;
537 else if (strcasecmp(optarg, "snmp") == 0)
538 packettype = PT_SNMP;
539 else if (strcasecmp(optarg, "cnfp") == 0)
540 packettype = PT_CNFP;
541 else
542 error("unknown packet type `%s'", optarg);
543 break;
544
545 case 'u':
546 ++uflag;
547 break;
548
549 #ifdef HAVE_PCAP_DUMP_FLUSH
550 case 'U':
551 ++Uflag;
552 break;
553 #endif
554
555 case 'v':
556 ++vflag;
557 break;
558
559 case 'w':
560 WFileName = optarg;
561 break;
562
563 case 'x':
564 ++xflag;
565 break;
566
567 case 'X':
568 ++xflag;
569 ++Xflag;
570 break;
571
572 case 'y':
573 dlt_name = optarg;
574 dlt = pcap_datalink_name_to_val(dlt_name);
575 if (dlt < 0)
576 error("invalid data link type %s", dlt_name);
577 break;
578
579 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
580 case 'Y':
581 {
582 /* Undocumented flag */
583 #ifdef HAVE_PCAP_DEBUG
584 extern int pcap_debug;
585 pcap_debug = 1;
586 #else
587 extern int yydebug;
588 yydebug = 1;
589 #endif
590 }
591 break;
592 #endif
593 default:
594 usage();
595 /* NOTREACHED */
596 }
597
598 if (aflag && nflag)
599 error("-a and -n options are incompatible");
600
601 if (tflag > 0)
602 thiszone = gmt2local(0);
603
604 if (RFileName != NULL) {
605 /*
606 * We don't need network access, so set it back to the user id.
607 * Also, this prevents the user from reading anyone's
608 * trace file.
609 */
610 #ifndef WIN32
611 setuid(getuid());
612 #endif /* WIN32 */
613
614 pd = pcap_open_offline(RFileName, ebuf);
615 if (pd == NULL)
616 error("%s", ebuf);
617 localnet = 0;
618 netmask = 0;
619 if (fflag != 0)
620 error("-f and -r options are incompatible");
621 } else {
622 if (device == NULL) {
623 device = pcap_lookupdev(ebuf);
624 if (device == NULL)
625 error("%s", ebuf);
626 }
627 #ifdef WIN32
628 PrintCapBegins(program_name,device);
629 #endif /* WIN32 */
630 *ebuf = '\0';
631 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
632 if (pd == NULL)
633 error("%s", ebuf);
634 else if (*ebuf)
635 warning("%s", ebuf);
636 #ifdef WIN32
637 if(UserBufferSize != 1000000)
638 if(pcap_setbuff(pd, UserBufferSize)==-1){
639 error("%s", pcap_geterr(pd));
640 }
641 #endif /* WIN32 */
642 if (Lflag)
643 show_dlts_and_exit(pd);
644 if (dlt >= 0) {
645 #ifdef HAVE_PCAP_SET_DATALINK
646 if (pcap_set_datalink(pd, dlt) < 0)
647 error("%s", pcap_geterr(pd));
648 #else
649 /*
650 * We don't actually support changing the
651 * data link type, so we only let them
652 * set it to what it already is.
653 */
654 if (dlt != pcap_datalink(pd)) {
655 error("%s is not one of the DLTs supported by this device\n",
656 dlt_name);
657 }
658 #endif
659 (void)fprintf(stderr, "%s: data link type %s\n",
660 program_name, dlt_name);
661 (void)fflush(stderr);
662 }
663 i = pcap_snapshot(pd);
664 if (snaplen < i) {
665 warning("snaplen raised from %d to %d", snaplen, i);
666 snaplen = i;
667 }
668 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
669 localnet = 0;
670 netmask = 0;
671 warning("%s", ebuf);
672 }
673 /*
674 * Let user own process after socket has been opened.
675 */
676 #ifndef WIN32
677 setuid(getuid());
678 #endif /* WIN32 */
679 }
680 if (infile)
681 cmdbuf = read_infile(infile);
682 else
683 cmdbuf = copy_argv(&argv[optind]);
684
685 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
686 error("%s", pcap_geterr(pd));
687 if (dflag) {
688 bpf_dump(&fcode, dflag);
689 pcap_close(pd);
690 exit(0);
691 }
692 init_addrtoname(localnet, netmask);
693
694 (void)setsignal(SIGTERM, cleanup);
695 (void)setsignal(SIGINT, cleanup);
696 /* Cooperate with nohup(1) */
697 #ifndef WIN32
698 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
699 (void)setsignal(SIGHUP, oldhandler);
700 #endif /* WIN32 */
701
702 if (pcap_setfilter(pd, &fcode) < 0)
703 error("%s", pcap_geterr(pd));
704 if (WFileName) {
705 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
706 if (p == NULL)
707 error("%s", pcap_geterr(pd));
708 if (Cflag != 0) {
709 callback = dump_packet_and_trunc;
710 dumpinfo.WFileName = WFileName;
711 dumpinfo.pd = pd;
712 dumpinfo.p = p;
713 pcap_userdata = (u_char *)&dumpinfo;
714 } else {
715 callback = dump_packet;
716 pcap_userdata = (u_char *)p;
717 }
718 } else {
719 type = pcap_datalink(pd);
720 printinfo.printer = lookup_printer(type);
721 if (printinfo.printer == NULL) {
722 dlt_name = pcap_datalink_val_to_name(type);
723 if (dlt_name != NULL)
724 error("unsupported data link type %s", dlt_name);
725 else
726 error("unsupported data link type %d", type);
727 }
728 callback = print_packet;
729 pcap_userdata = (u_char *)&printinfo;
730 }
731 #ifdef SIGINFO
732 (void)setsignal(SIGINFO, requestinfo);
733 #endif
734 #ifndef WIN32
735 if (RFileName == NULL) {
736 if (!vflag && !WFileName) {
737 (void)fprintf(stderr,
738 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
739 program_name);
740 } else
741 (void)fprintf(stderr, "%s: ", program_name);
742 (void)fprintf(stderr, "listening on %s, capture size %u bytes\n",
743 device, snaplen);
744 (void)fflush(stderr);
745 }
746 #endif /* WIN32 */
747 if (pcap_loop(pd, cnt, callback, pcap_userdata) < 0) {
748 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
749 program_name, pcap_geterr(pd));
750 cleanup(0);
751 pcap_close(pd);
752 exit(1);
753 }
754 if (RFileName == NULL)
755 info(1);
756 pcap_close(pd);
757 exit(0);
758 }
759
760 /* make a clean exit on interrupts */
761 static RETSIGTYPE
762 cleanup(int signo)
763 {
764
765 /* Can't print the summary if reading from a savefile */
766 if (pd != NULL && pcap_file(pd) == NULL) {
767 (void)fflush(stdout);
768 putc('\n', stderr);
769 info(1);
770 }
771 if (signo)
772 exit(0);
773 }
774
775 static void
776 info(register int verbose)
777 {
778 struct pcap_stat stat;
779
780 if (pcap_stats(pd, &stat) < 0) {
781 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
782 return;
783 }
784
785 if (!verbose)
786 fprintf(stderr, "%s: ", program_name);
787
788 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
789 if (!verbose)
790 fputs(", ", stderr);
791 else
792 putc('\n', stderr);
793 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
794 infoprint = 0;
795 }
796
797 static void
798 reverse(char *s)
799 {
800 int i, j, c;
801
802 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
803 c = s[i];
804 s[i] = s[j];
805 s[j] = c;
806 }
807 }
808
809
810 static void
811 swebitoa(unsigned int n, char *s)
812 {
813 unsigned int i;
814
815 i = 0;
816 do {
817 s[i++] = n % 10 + '0';
818 } while ((n /= 10) > 0);
819
820 s[i] = '\0';
821 reverse(s);
822 }
823
824 static void
825 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
826 {
827 struct dump_info *dump_info;
828 static uint cnt = 2;
829 char *name;
830
831 ++infodelay;
832
833 dump_info = (struct dump_info *)user;
834
835 /*
836 * XXX - this won't prevent capture files from getting
837 * larger than Cflag - the last packet written to the
838 * file could put it over Cflag.
839 */
840 if (ftell((FILE *)dump_info->p) > Cflag) {
841 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
842 if (name == NULL)
843 error("dump_packet_and_trunc: malloc");
844 strcpy(name, dump_info->WFileName);
845 swebitoa(cnt, name + strlen(dump_info->WFileName));
846 cnt++;
847 pcap_dump_close(dump_info->p);
848 dump_info->p = pcap_dump_open(dump_info->pd, name);
849 free(name);
850 if (dump_info->p == NULL)
851 error("%s", pcap_geterr(pd));
852 }
853
854 pcap_dump((u_char *)dump_info->p, h, sp);
855 #ifdef HAVE_PCAP_DUMP_FLUSH
856 if (Uflag)
857 pcap_dump_flush(dump_info->p);
858 #endif
859
860 --infodelay;
861 if (infoprint)
862 info(0);
863 }
864
865 static void
866 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
867 {
868 ++infodelay;
869
870 pcap_dump(user, h, sp);
871 #ifdef HAVE_PCAP_DUMP_FLUSH
872 if (Uflag)
873 pcap_dump_flush((pcap_dumper_t *)user);
874 #endif
875
876 --infodelay;
877 if (infoprint)
878 info(0);
879 }
880
881 static void
882 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
883 {
884 struct print_info *print_info;
885 u_int hdrlen;
886
887 ++infodelay;
888 ts_print(&h->ts);
889
890 print_info = (struct print_info *)user;
891
892 /*
893 * Some printers want to check that they're not walking off the
894 * end of the packet.
895 * Rather than pass it all the way down, we set this global.
896 */
897 snapend = sp + h->caplen;
898
899 hdrlen = (*print_info->printer)(h, sp);
900 if (xflag) {
901 /*
902 * Print the raw packet data.
903 */
904 if (xflag > 1) {
905 /*
906 * Include the link-layer header.
907 */
908 default_print_unaligned(sp, h->caplen);
909 } else {
910 /*
911 * Don't include the link-layer header - and if
912 * we have nothing past the link-layer header,
913 * print nothing.
914 */
915 if (h->caplen > hdrlen)
916 default_print_unaligned(sp + hdrlen,
917 h->caplen - hdrlen);
918 }
919 }
920
921 putchar('\n');
922
923 --infodelay;
924 if (infoprint)
925 info(0);
926 }
927
928 /* Like default_print() but data need not be aligned */
929 void
930 default_print_unaligned(register const u_char *cp, register u_int length)
931 {
932 register u_int i, s;
933 register int nshorts;
934
935 if (Xflag) {
936 ascii_print(cp, length);
937 return;
938 }
939 nshorts = (u_int) length / sizeof(u_short);
940 i = 0;
941 while (--nshorts >= 0) {
942 if ((i++ % 8) == 0)
943 (void)printf("\n\t\t\t");
944 s = *cp++;
945 (void)printf(" %02x%02x", s, *cp++);
946 }
947 if (length & 1) {
948 if ((i % 8) == 0)
949 (void)printf("\n\t\t\t");
950 (void)printf(" %02x", *cp);
951 }
952 }
953
954 #ifdef WIN32
955 /*
956 * XXX - there should really be libpcap calls to get the version
957 * number as a string (the string would be generated from #defines
958 * at run time, so that it's not generated from string constants
959 * in the library, as, on many UNIX systems, those constants would
960 * be statically linked into the application executable image, and
961 * would thus reflect the version of libpcap on the system on
962 * which the application was *linked*, not the system on which it's
963 * *running*.
964 *
965 * That routine should be documented, unlike the "version[]"
966 * string, so that UNIX vendors providing their own libpcaps
967 * don't omit it (as a couple of vendors have...).
968 *
969 * Packet.dll should perhaps also export a routine to return the
970 * version number of the Packet.dll code, to supply the
971 * "Wpcap_version" information on Windows.
972 */
973 char WDversion[]="current-cvs.tcpdump.org";
974 char version[]="current-cvs.tcpdump.org";
975 char pcap_version[]="current-cvs.tcpdump.org";
976 char Wpcap_version[]="3.0 alpha";
977 #endif
978
979 /*
980 * By default, print the specified data out in hex.
981 */
982 void
983 default_print(register const u_char *bp, register u_int length)
984 {
985 default_print_unaligned(bp, length);
986 }
987
988 #ifdef SIGINFO
989 RETSIGTYPE requestinfo(int signo _U_)
990 {
991 if (infodelay)
992 ++infoprint;
993 else
994 info(0);
995 }
996 #endif
997
998 static void
999 usage(void)
1000 {
1001 extern char version[];
1002 #ifndef HAVE_PCAP_LIB_VERSION
1003 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1004 extern char pcap_version[];
1005 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1006 static char pcap_version[] = "unknown";
1007 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1008 #endif /* HAVE_PCAP_LIB_VERSION */
1009
1010 #ifdef HAVE_PCAP_LIB_VERSION
1011 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1012 (void)fprintf(stderr, "%s\n", pcap_lib_version());
1013 #else /* HAVE_PCAP_LIB_VERSION */
1014 #ifdef WIN32
1015 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1016 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1017 #else /* WIN32 */
1018 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1019 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1020 #endif /* WIN32 */
1021 #endif /* HAVE_PCAP_LIB_VERSION */
1022 (void)fprintf(stderr,
1023 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1024 (void)fprintf(stderr,
1025 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1026 (void)fprintf(stderr,
1027 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1028 (void)fprintf(stderr,
1029 "\t\t[ expression ]\n");
1030 exit(1);
1031 }