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