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