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