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