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