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