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