]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
14dca7408719f412c355be806e472009316f9349
[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.212 2003-08-01 01:01:40 fenner 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
547 error("unknown packet type `%s'", optarg);
548 break;
549
550 case 'u':
551 ++uflag;
552 break;
553
554 #ifdef HAVE_PCAP_DUMP_FLUSH
555 case 'U':
556 ++Uflag;
557 break;
558 #endif
559
560 case 'v':
561 ++vflag;
562 break;
563
564 case 'w':
565 WFileName = optarg;
566 break;
567
568 case 'x':
569 ++xflag;
570 break;
571
572 case 'X':
573 ++xflag;
574 ++Xflag;
575 break;
576
577 case 'y':
578 dlt_name = optarg;
579 dlt = pcap_datalink_name_to_val(dlt_name);
580 if (dlt < 0)
581 error("invalid data link type %s", dlt_name);
582 break;
583
584 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
585 case 'Y':
586 {
587 /* Undocumented flag */
588 #ifdef HAVE_PCAP_DEBUG
589 extern int pcap_debug;
590 pcap_debug = 1;
591 #else
592 extern int yydebug;
593 yydebug = 1;
594 #endif
595 }
596 break;
597 #endif
598 default:
599 usage();
600 /* NOTREACHED */
601 }
602
603 if (tflag > 0)
604 thiszone = gmt2local(0);
605
606 if (RFileName != NULL) {
607 int dlt;
608 const char *dlt_name;
609
610 #ifndef WIN32
611 /*
612 * We don't need network access, so relinquish any set-UID
613 * or set-GID privileges we have (if any).
614 *
615 * We do *not* want set-UID privileges when opening a
616 * trace file, as that might let the user read other
617 * people's trace files (especially if we're set-UID
618 * root).
619 */
620 setuid(getuid());
621 #endif /* WIN32 */
622 pd = pcap_open_offline(RFileName, ebuf);
623 if (pd == NULL)
624 error("%s", ebuf);
625 dlt = pcap_datalink(pd);
626 dlt_name = pcap_datalink_val_to_name(dlt);
627 if (dlt_name == NULL)
628 dlt_name = "???";
629 printf("reading from file %s, link-type %u (%s)\n",
630 RFileName, dlt, dlt_name);
631 localnet = 0;
632 netmask = 0;
633 if (fflag != 0)
634 error("-f and -r options are incompatible");
635 } else {
636 if (device == NULL) {
637 device = pcap_lookupdev(ebuf);
638 if (device == NULL)
639 error("%s", ebuf);
640 }
641 #ifdef WIN32
642 PrintCapBegins(program_name,device);
643 #endif /* WIN32 */
644 *ebuf = '\0';
645 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
646 if (pd == NULL)
647 error("%s", ebuf);
648 else if (*ebuf)
649 warning("%s", ebuf);
650 #ifdef WIN32
651 if(UserBufferSize != 1000000)
652 if(pcap_setbuff(pd, UserBufferSize)==-1){
653 error("%s", pcap_geterr(pd));
654 }
655 #endif /* WIN32 */
656 if (Lflag)
657 show_dlts_and_exit(pd);
658 if (dlt >= 0) {
659 #ifdef HAVE_PCAP_SET_DATALINK
660 if (pcap_set_datalink(pd, dlt) < 0)
661 error("%s", pcap_geterr(pd));
662 #else
663 /*
664 * We don't actually support changing the
665 * data link type, so we only let them
666 * set it to what it already is.
667 */
668 if (dlt != pcap_datalink(pd)) {
669 error("%s is not one of the DLTs supported by this device\n",
670 dlt_name);
671 }
672 #endif
673 (void)fprintf(stderr, "%s: data link type %s\n",
674 program_name, dlt_name);
675 (void)fflush(stderr);
676 }
677 i = pcap_snapshot(pd);
678 if (snaplen < i) {
679 warning("snaplen raised from %d to %d", snaplen, i);
680 snaplen = i;
681 }
682 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
683 localnet = 0;
684 netmask = 0;
685 warning("%s", ebuf);
686 }
687 /*
688 * Let user own process after socket has been opened.
689 */
690 #ifndef WIN32
691 setuid(getuid());
692 #endif /* WIN32 */
693 }
694 if (infile)
695 cmdbuf = read_infile(infile);
696 else
697 cmdbuf = copy_argv(&argv[optind]);
698
699 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
700 error("%s", pcap_geterr(pd));
701 if (dflag) {
702 bpf_dump(&fcode, dflag);
703 pcap_close(pd);
704 exit(0);
705 }
706 init_addrtoname(localnet, netmask);
707
708 #ifndef WIN32
709 (void)setsignal(SIGPIPE, cleanup);
710 #endif /* WIN32 */
711 (void)setsignal(SIGTERM, cleanup);
712 (void)setsignal(SIGINT, cleanup);
713 /* Cooperate with nohup(1) */
714 #ifndef WIN32
715 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
716 (void)setsignal(SIGHUP, oldhandler);
717 #endif /* WIN32 */
718
719 if (pcap_setfilter(pd, &fcode) < 0)
720 error("%s", pcap_geterr(pd));
721 if (WFileName) {
722 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
723 if (p == NULL)
724 error("%s", pcap_geterr(pd));
725 if (Cflag != 0) {
726 callback = dump_packet_and_trunc;
727 dumpinfo.WFileName = WFileName;
728 dumpinfo.pd = pd;
729 dumpinfo.p = p;
730 pcap_userdata = (u_char *)&dumpinfo;
731 } else {
732 callback = dump_packet;
733 pcap_userdata = (u_char *)p;
734 }
735 } else {
736 type = pcap_datalink(pd);
737 printinfo.printer = lookup_printer(type);
738 if (printinfo.printer == NULL) {
739 dlt_name = pcap_datalink_val_to_name(type);
740 if (dlt_name != NULL)
741 error("unsupported data link type %s", dlt_name);
742 else
743 error("unsupported data link type %d", type);
744 }
745 callback = print_packet;
746 pcap_userdata = (u_char *)&printinfo;
747 }
748 #ifdef SIGINFO
749 (void)setsignal(SIGINFO, requestinfo);
750 #endif
751 #ifndef WIN32
752 if (RFileName == NULL) {
753 int dlt;
754 const char *dlt_name;
755
756 if (!vflag && !WFileName) {
757 (void)fprintf(stderr,
758 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
759 program_name);
760 } else
761 (void)fprintf(stderr, "%s: ", program_name);
762 dlt = pcap_datalink(pd);
763 dlt_name = pcap_datalink_val_to_name(dlt);
764 if (dlt_name == NULL)
765 dlt_name = "???";
766 (void)fprintf(stderr, "listening on %s, link-type %u (%s), capture size %u bytes\n",
767 device, dlt, dlt_name, snaplen);
768 (void)fflush(stderr);
769 }
770 #endif /* WIN32 */
771 if (pcap_loop(pd, cnt, callback, pcap_userdata) < 0) {
772 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
773 program_name, pcap_geterr(pd));
774 cleanup(0);
775 pcap_close(pd);
776 exit(1);
777 }
778 if (RFileName == NULL)
779 info(1);
780 pcap_close(pd);
781 exit(0);
782 }
783
784 /* make a clean exit on interrupts */
785 static RETSIGTYPE
786 cleanup(int signo)
787 {
788
789 /* Can't print the summary if reading from a savefile */
790 if (pd != NULL && pcap_file(pd) == NULL) {
791 (void)fflush(stdout);
792 putc('\n', stderr);
793 info(1);
794 }
795 if (signo)
796 exit(0);
797 }
798
799 static void
800 info(register int verbose)
801 {
802 struct pcap_stat stat;
803
804 if (pcap_stats(pd, &stat) < 0) {
805 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
806 return;
807 }
808
809 if (!verbose)
810 fprintf(stderr, "%s: ", program_name);
811
812 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
813 if (!verbose)
814 fputs(", ", stderr);
815 else
816 putc('\n', stderr);
817 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
818 infoprint = 0;
819 }
820
821 static void
822 reverse(char *s)
823 {
824 int i, j, c;
825
826 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
827 c = s[i];
828 s[i] = s[j];
829 s[j] = c;
830 }
831 }
832
833
834 static void
835 swebitoa(unsigned int n, char *s)
836 {
837 unsigned int i;
838
839 i = 0;
840 do {
841 s[i++] = n % 10 + '0';
842 } while ((n /= 10) > 0);
843
844 s[i] = '\0';
845 reverse(s);
846 }
847
848 static void
849 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
850 {
851 struct dump_info *dump_info;
852 static uint cnt = 2;
853 char *name;
854
855 ++infodelay;
856
857 dump_info = (struct dump_info *)user;
858
859 /*
860 * XXX - this won't prevent capture files from getting
861 * larger than Cflag - the last packet written to the
862 * file could put it over Cflag.
863 */
864 if (ftell((FILE *)dump_info->p) > Cflag) {
865 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
866 if (name == NULL)
867 error("dump_packet_and_trunc: malloc");
868 strcpy(name, dump_info->WFileName);
869 swebitoa(cnt, name + strlen(dump_info->WFileName));
870 cnt++;
871 pcap_dump_close(dump_info->p);
872 dump_info->p = pcap_dump_open(dump_info->pd, name);
873 free(name);
874 if (dump_info->p == NULL)
875 error("%s", pcap_geterr(pd));
876 }
877
878 pcap_dump((u_char *)dump_info->p, h, sp);
879 #ifdef HAVE_PCAP_DUMP_FLUSH
880 if (Uflag)
881 pcap_dump_flush(dump_info->p);
882 #endif
883
884 --infodelay;
885 if (infoprint)
886 info(0);
887 }
888
889 static void
890 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
891 {
892 ++infodelay;
893
894 pcap_dump(user, h, sp);
895 #ifdef HAVE_PCAP_DUMP_FLUSH
896 if (Uflag)
897 pcap_dump_flush((pcap_dumper_t *)user);
898 #endif
899
900 --infodelay;
901 if (infoprint)
902 info(0);
903 }
904
905 static void
906 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
907 {
908 struct print_info *print_info;
909 u_int hdrlen;
910
911 ++infodelay;
912 ts_print(&h->ts);
913
914 print_info = (struct print_info *)user;
915
916 /*
917 * Some printers want to check that they're not walking off the
918 * end of the packet.
919 * Rather than pass it all the way down, we set this global.
920 */
921 snapend = sp + h->caplen;
922
923 hdrlen = (*print_info->printer)(h, sp);
924 if (xflag) {
925 /*
926 * Print the raw packet data.
927 */
928 if (xflag > 1) {
929 /*
930 * Include the link-layer header.
931 */
932 default_print_unaligned(sp, h->caplen);
933 } else {
934 /*
935 * Don't include the link-layer header - and if
936 * we have nothing past the link-layer header,
937 * print nothing.
938 */
939 if (h->caplen > hdrlen)
940 default_print_unaligned(sp + hdrlen,
941 h->caplen - hdrlen);
942 }
943 }
944
945 putchar('\n');
946
947 --infodelay;
948 if (infoprint)
949 info(0);
950 }
951
952 /* Like default_print() but data need not be aligned */
953 void
954 default_print_unaligned(register const u_char *cp, register u_int length)
955 {
956 register u_int i, s;
957 register int nshorts;
958
959 if (Xflag) {
960 ascii_print(cp, length);
961 return;
962 }
963 nshorts = (u_int) length / sizeof(u_short);
964 i = 0;
965 while (--nshorts >= 0) {
966 if ((i++ % 8) == 0)
967 (void)printf("\n\t\t\t");
968 s = *cp++;
969 (void)printf(" %02x%02x", s, *cp++);
970 }
971 if (length & 1) {
972 if ((i % 8) == 0)
973 (void)printf("\n\t\t\t");
974 (void)printf(" %02x", *cp);
975 }
976 }
977
978 #ifdef WIN32
979 /*
980 * XXX - there should really be libpcap calls to get the version
981 * number as a string (the string would be generated from #defines
982 * at run time, so that it's not generated from string constants
983 * in the library, as, on many UNIX systems, those constants would
984 * be statically linked into the application executable image, and
985 * would thus reflect the version of libpcap on the system on
986 * which the application was *linked*, not the system on which it's
987 * *running*.
988 *
989 * That routine should be documented, unlike the "version[]"
990 * string, so that UNIX vendors providing their own libpcaps
991 * don't omit it (as a couple of vendors have...).
992 *
993 * Packet.dll should perhaps also export a routine to return the
994 * version number of the Packet.dll code, to supply the
995 * "Wpcap_version" information on Windows.
996 */
997 char WDversion[]="current-cvs.tcpdump.org";
998 char version[]="current-cvs.tcpdump.org";
999 char pcap_version[]="current-cvs.tcpdump.org";
1000 char Wpcap_version[]="3.0 alpha";
1001 #endif
1002
1003 /*
1004 * By default, print the specified data out in hex.
1005 */
1006 void
1007 default_print(register const u_char *bp, register u_int length)
1008 {
1009 default_print_unaligned(bp, length);
1010 }
1011
1012 #ifdef SIGINFO
1013 RETSIGTYPE requestinfo(int signo _U_)
1014 {
1015 if (infodelay)
1016 ++infoprint;
1017 else
1018 info(0);
1019 }
1020 #endif
1021
1022 static void
1023 usage(void)
1024 {
1025 extern char version[];
1026 #ifndef HAVE_PCAP_LIB_VERSION
1027 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1028 extern char pcap_version[];
1029 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1030 static char pcap_version[] = "unknown";
1031 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1032 #endif /* HAVE_PCAP_LIB_VERSION */
1033
1034 #ifdef HAVE_PCAP_LIB_VERSION
1035 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1036 (void)fprintf(stderr, "%s\n", pcap_lib_version());
1037 #else /* HAVE_PCAP_LIB_VERSION */
1038 #ifdef WIN32
1039 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1040 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1041 #else /* WIN32 */
1042 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1043 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1044 #endif /* WIN32 */
1045 #endif /* HAVE_PCAP_LIB_VERSION */
1046 (void)fprintf(stderr,
1047 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1048 (void)fprintf(stderr,
1049 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1050 (void)fprintf(stderr,
1051 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1052 (void)fprintf(stderr,
1053 "\t\t[ expression ]\n");
1054 exit(1);
1055 }