]>
The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
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.
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
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
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.179 2002-07-11 09:17:25 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
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.
48 #include <sys/types.h>
51 #include <netinet/in.h>
62 #include "interface.h"
63 #include "addrtoname.h"
65 #include "setsignal.h"
66 #include "gmt2local.h"
68 int aflag
; /* translate network and broadcast addresses */
69 int dflag
; /* print filter code */
70 int eflag
; /* print ethernet header */
71 int fflag
; /* don't translate "foreign" IP address */
72 int nflag
; /* leave addresses as numbers */
73 int Nflag
; /* remove domains from printed host names */
74 int Oflag
= 1; /* run filter code optimizer */
75 int pflag
; /* don't go promiscuous */
76 int qflag
; /* quick (shorter) output */
77 int Rflag
= 1; /* print sequence # field in AH/ESP*/
78 int sflag
= 0; /* use the libsmi to translate OIDs */
79 int Sflag
; /* print raw TCP sequence numbers */
80 int tflag
= 1; /* print packet arrival time */
81 int uflag
= 0; /* Print undecoded NFS handles */
82 int vflag
; /* verbose */
83 int xflag
; /* print packet in hex */
84 int Xflag
; /* print packet in ascii as well as hex */
85 off_t Cflag
= 0; /* rotate dump files after this many bytes */
86 int Aflag
= 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
88 char *espsecret
= NULL
; /* ESP secret key */
97 int32_t thiszone
; /* seconds offset from gmt to local time */
100 static RETSIGTYPE
cleanup(int);
101 static void usage(void) __attribute__((noreturn
));
103 static void dump_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
106 RETSIGTYPE
requestinfo(int);
109 /* Length of saved portion of packet. */
110 int snaplen
= DEFAULT_SNAPLEN
;
117 static struct printer printers
[] = {
118 { arcnet_if_print
, DLT_ARCNET
},
119 { ether_if_print
, DLT_EN10MB
},
120 { token_if_print
, DLT_IEEE802
},
122 { lane_if_print
, DLT_LANE8023
},
125 { cip_if_print
, DLT_CIP
},
128 { cip_if_print
, DLT_ATM_CLIP
},
130 { sl_if_print
, DLT_SLIP
},
131 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
132 { ppp_if_print
, DLT_PPP
},
133 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
134 { fddi_if_print
, DLT_FDDI
},
135 { null_if_print
, DLT_NULL
},
137 { null_if_print
, DLT_LOOP
},
139 { raw_if_print
, DLT_RAW
},
140 { atm_if_print
, DLT_ATM_RFC1483
},
142 { chdlc_if_print
, DLT_C_HDLC
},
145 { chdlc_if_print
, DLT_HDLC
},
147 #ifdef DLT_PPP_SERIAL
148 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
151 { pppoe_if_print
, DLT_PPP_ETHER
},
154 { sll_if_print
, DLT_LINUX_SLL
},
156 #ifdef DLT_IEEE802_11
157 { ieee802_11_if_print
, DLT_IEEE802_11
},
160 { ltalk_if_print
, DLT_LTALK
},
163 { pflog_if_print
, DLT_PFLOG
},
166 { fr_if_print
, DLT_FR
},
169 { fr_if_print
, DLT_FRELAY
},
172 { sunatm_if_print
, DLT_SUNATM
},
178 lookup_printer(int type
)
182 for (p
= printers
; p
->f
; ++p
)
186 error("unknown data link type %d", type
);
203 main(int argc
, char **argv
)
205 register int cnt
, op
, i
;
206 bpf_u_int32 localnet
, netmask
;
207 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
208 pcap_handler printer
;
209 struct bpf_program fcode
;
210 RETSIGTYPE (*oldhandler
)(int);
211 struct dump_info dumpinfo
;
212 u_char
*pcap_userdata
;
213 char ebuf
[PCAP_ERRBUF_SIZE
];
220 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
221 program_name
= cp
+ 1;
223 program_name
= argv
[0];
225 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
234 (op
= getopt(argc
, argv
, "aAc:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
250 error("invalid packet count %s", optarg
);
254 Cflag
= atoi(optarg
) * 1000000;
256 error("invalid file size %s", optarg
);
268 #ifndef HAVE_LIBCRYPTO
269 warning("crypto code not compiled in");
287 #ifdef HAVE_SETLINEBUF
290 setvbuf(stdout
, NULL
, _IOLBF
, 0);
304 if (smiLoadModule(optarg
) == 0) {
305 error("could not load MIB module %s", optarg
);
309 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
310 program_name
, optarg
);
311 (void)fprintf(stderr
, "(no libsmi support)\n");
337 snaplen
= strtol(optarg
, &end
, 0);
338 if (optarg
== end
|| *end
!= '\0'
339 || snaplen
< 0 || snaplen
> 65535)
340 error("invalid snaplen %s", optarg
);
341 else if (snaplen
== 0)
355 if (strcasecmp(optarg
, "vat") == 0)
357 else if (strcasecmp(optarg
, "wb") == 0)
359 else if (strcasecmp(optarg
, "rpc") == 0)
361 else if (strcasecmp(optarg
, "rtp") == 0)
363 else if (strcasecmp(optarg
, "rtcp") == 0)
364 packettype
= PT_RTCP
;
365 else if (strcasecmp(optarg
, "snmp") == 0)
366 packettype
= PT_SNMP
;
367 else if (strcasecmp(optarg
, "cnfp") == 0)
368 packettype
= PT_CNFP
;
370 error("unknown packet type `%s'", optarg
);
397 /* Undocumented flag */
409 error("-a and -n options are incompatible");
412 thiszone
= gmt2local(0);
414 if (RFileName
!= NULL
) {
416 * We don't need network access, so set it back to the user id.
417 * Also, this prevents the user from reading anyone's
422 pd
= pcap_open_offline(RFileName
, ebuf
);
428 error("-f and -r options are incompatible");
430 if (device
== NULL
) {
431 device
= pcap_lookupdev(ebuf
);
436 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
441 i
= pcap_snapshot(pd
);
443 warning("snaplen raised from %d to %d", snaplen
, i
);
446 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
452 * Let user own process after socket has been opened.
457 cmdbuf
= read_infile(infile
);
459 cmdbuf
= copy_argv(&argv
[optind
]);
461 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
462 error("%s", pcap_geterr(pd
));
464 bpf_dump(&fcode
, dflag
);
467 init_addrtoname(localnet
, netmask
);
469 (void)setsignal(SIGTERM
, cleanup
);
470 (void)setsignal(SIGINT
, cleanup
);
471 /* Cooperate with nohup(1) */
472 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
473 (void)setsignal(SIGHUP
, oldhandler
);
475 if (pcap_setfilter(pd
, &fcode
) < 0)
476 error("%s", pcap_geterr(pd
));
478 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
480 error("%s", pcap_geterr(pd
));
482 printer
= dump_and_trunc
;
483 dumpinfo
.WFileName
= WFileName
;
486 pcap_userdata
= (u_char
*)&dumpinfo
;
489 pcap_userdata
= (u_char
*)p
;
492 printer
= lookup_printer(pcap_datalink(pd
));
495 (void)setsignal(SIGINFO
, requestinfo
);
498 if (RFileName
== NULL
) {
499 (void)fprintf(stderr
, "%s: listening on %s\n",
500 program_name
, device
);
501 (void)fflush(stderr
);
503 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
504 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
505 program_name
, pcap_geterr(pd
));
510 if (RFileName
== NULL
)
516 /* make a clean exit on interrupts */
521 /* Can't print the summary if reading from a savefile */
522 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
523 (void)fflush(stdout
);
532 info(register int verbose
)
534 struct pcap_stat stat
;
536 if (pcap_stats(pd
, &stat
) < 0) {
537 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
541 fprintf(stderr
, "%s: ", program_name
);
542 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
547 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
556 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
565 swebitoa(unsigned int n
, char *s
)
571 s
[i
++] = n
% 10 + '0';
572 } while ((n
/= 10) > 0);
579 dump_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
581 struct dump_info
*info
;
585 info
= (struct dump_info
*)user
;
588 * XXX - this won't prevent capture files from getting
589 * larger than Cflag - the last packet written to the
590 * file could put it over Cflag.
592 if (ftell((FILE *)info
->p
) > Cflag
) {
593 name
= (char *) malloc(strlen(info
->WFileName
) + 4);
595 error("dump_and_trunc: malloc");
596 strcpy(name
, info
->WFileName
);
597 swebitoa(cnt
, name
+ strlen(info
->WFileName
));
599 pcap_dump_close(info
->p
);
600 info
->p
= pcap_dump_open(info
->pd
, name
);
603 error("%s", pcap_geterr(pd
));
606 pcap_dump((u_char
*)info
->p
, h
, sp
);
609 /* Like default_print() but data need not be aligned */
611 default_print_unaligned(register const u_char
*cp
, register u_int length
)
614 register int nshorts
;
617 ascii_print(cp
, length
);
620 nshorts
= (u_int
) length
/ sizeof(u_short
);
622 while (--nshorts
>= 0) {
624 (void)printf("\n\t\t\t");
626 (void)printf(" %02x%02x", s
, *cp
++);
630 (void)printf("\n\t\t\t");
631 (void)printf(" %02x", *cp
);
636 * By default, print the packet out in hex.
639 default_print(register const u_char
*bp
, register u_int length
)
641 default_print_unaligned(bp
, length
);
645 RETSIGTYPE
requestinfo(int signo
)
657 extern char version
[];
658 extern char pcap_version
[];
660 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
661 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
662 (void)fprintf(stderr
,
663 "Usage: %s [-aAdeflnNOpqRStuvxX] [ -c count ] [ -C file_size ]\n", program_name
);
664 (void)fprintf(stderr
,
665 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
666 (void)fprintf(stderr
,
667 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ expression ]\n");