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