]>
The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
be9e031b2a314586a796859725d7e0039def243c
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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.
23 static const char copyright
[] =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997\n\
25 The Regents of the University of California. All rights reserved.\n";
26 static const char rcsid
[] =
27 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.151 2000-09-17 04:13:13 guy Exp $ (LBL)";
31 * tcpdump - monitor tcp/ip traffic on an ethernet.
33 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
34 * Mercilessly hacked and occasionally improved since then via the
35 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
42 #include <sys/types.h>
45 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
58 #include "interface.h"
59 #include "addrtoname.h"
61 #include "setsignal.h"
62 #include "gmt2local.h"
64 int aflag
; /* translate network and broadcast addresses */
65 int dflag
; /* print filter code */
66 int eflag
; /* print ethernet header */
67 int fflag
; /* don't translate "foreign" IP address */
68 int nflag
; /* leave addresses as numbers */
69 int Nflag
; /* remove domains from printed host names */
70 int Oflag
= 1; /* run filter code optimizer */
71 int pflag
; /* don't go promiscuous */
72 int qflag
; /* quick (shorter) output */
73 int Rflag
= 1; /* print sequence # field in AH/ESP*/
74 int sflag
= 0; /* use the libsmi to translate OIDs */
75 int Sflag
; /* print raw TCP sequence numbers */
76 int tflag
= 1; /* print packet arrival time */
77 int uflag
= 0; /* Print undecoded NFS handles */
78 int vflag
; /* verbose */
79 int xflag
; /* print packet in hex */
80 int Xflag
; /* print packet in ascii as well as hex */
82 char *espsecret
= NULL
; /* ESP secret key */
89 int32_t thiszone
; /* seconds offset from gmt to local time */
92 static RETSIGTYPE
cleanup(int);
93 static void usage(void) __attribute__((noreturn
));
95 /* Length of saved portion of packet. */
96 int snaplen
= DEFAULT_SNAPLEN
;
104 * This table checks some DLT_* codes as well as checking PCAP_ENCAP_*
105 * codes, so that those DLT_* codes that aren't the same on all platforms
106 * (and thus don't have values identical to the values of the corresponding
107 * PCAP_ENCAP_* codes) are, at least, handled on the platform that wrote
110 * (This may result in two identical entries in the table, if there's
111 * a DLT_* code defined to have the same value as the corresponding
112 * PCAP_ENCAP_* code. Those duplicate entries are harmless.)
114 static struct printer printers
[] = {
116 * PCAP_ENCAP_* codes that correspond to DLT_* codes whose values
117 * are the same on all platforms (the PCAP_ENCAP_* code values
118 * are the same as the DLT_* code values).
120 { null_if_print
, PCAP_ENCAP_NULL
},
121 { ether_if_print
, PCAP_ENCAP_ETHERNET
},
122 { token_if_print
, PCAP_ENCAP_TOKEN_RING
},
123 { sl_if_print
, PCAP_ENCAP_SLIP
},
124 { ppp_if_print
, PCAP_ENCAP_PPP
},
125 { fddi_if_print
, PCAP_ENCAP_FDDI
},
128 * DLT_* codes that aren't the same on all platforms.
130 #ifdef DLT_ATM_RFC1483
131 { atm_if_print
, DLT_ATM_RFC1483
},
134 { raw_if_print
, DLT_RAW
},
136 #ifdef DLT_SLIP_BSDOS
137 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
140 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
143 { cip_if_print
, DLT_CIP
},
146 { cip_if_print
, DLT_ATM_CLIP
},
149 { lane_if_print
, DLT_LANE8023
},
153 * PCAP_ENCAP_* codes corresponding to DLT_* codes that aren't
154 * necessarily the same on all platforms, and PCAP_ENCAP_* codes
155 * for which there aren't DLT_* codes.
157 { atm_if_print
, PCAP_ENCAP_ATM_RFC1483
},
158 { raw_if_print
, PCAP_ENCAP_RAW
},
159 { sl_bsdos_if_print
, PCAP_ENCAP_SLIP_BSDOS
},
160 { ppp_bsdos_if_print
, PCAP_ENCAP_PPP_BSDOS
},
161 { chdlc_if_print
, PCAP_ENCAP_C_HDLC
},
162 { cip_if_print
, PCAP_ENCAP_ATM_CLIP
},
167 lookup_printer(int type
)
171 for (p
= printers
; p
->f
; ++p
)
175 error("unknown data link type %d", type
);
186 main(int argc
, char **argv
)
188 register int cnt
, op
, i
;
189 bpf_u_int32 localnet
, netmask
;
190 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
191 pcap_handler printer
;
192 struct bpf_program fcode
;
193 RETSIGTYPE (*oldhandler
)(int);
194 u_char
*pcap_userdata
;
195 char ebuf
[PCAP_ERRBUF_SIZE
];
202 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
203 program_name
= cp
+ 1;
205 program_name
= argv
[0];
207 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
216 (op
= getopt(argc
, argv
, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
226 error("invalid packet count %s", optarg
);
238 #ifndef HAVE_LIBCRYPTO
239 warning("crypto code not compiled in");
257 #ifdef HAVE_SETLINEBUF
260 setvbuf(stdout
, NULL
, _IOLBF
, 0);
274 if (smiLoadModule(optarg
) == 0) {
275 error("could not load MIB module %s", optarg
);
279 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
280 program_name
, optarg
);
281 (void)fprintf(stderr
, "(no libsmi support)\n");
307 snaplen
= strtol(optarg
, &end
, 0);
308 if (optarg
== end
|| *end
!= '\0'
309 || snaplen
< 0 || snaplen
> 65535)
310 error("invalid snaplen %s", optarg
);
311 else if (snaplen
== 0)
325 if (strcasecmp(optarg
, "vat") == 0)
327 else if (strcasecmp(optarg
, "wb") == 0)
329 else if (strcasecmp(optarg
, "rpc") == 0)
331 else if (strcasecmp(optarg
, "rtp") == 0)
333 else if (strcasecmp(optarg
, "rtcp") == 0)
334 packettype
= PT_RTCP
;
335 else if (strcasecmp(optarg
, "snmp") == 0)
336 packettype
= PT_SNMP
;
337 else if (strcasecmp(optarg
, "cnfp") == 0)
338 packettype
= PT_CNFP
;
340 error("unknown packet type `%s'", optarg
);
367 /* Undocumented flag */
379 error("-a and -n options are incompatible");
382 thiszone
= gmt2local(0);
384 if (RFileName
!= NULL
) {
386 * We don't need network access, so set it back to the user id.
387 * Also, this prevents the user from reading anyone's
392 pd
= pcap_open_offline(RFileName
, ebuf
);
398 error("-f and -r options are incompatible");
400 if (device
== NULL
) {
401 device
= pcap_lookupdev(ebuf
);
405 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
408 i
= pcap_snapshot(pd
);
410 warning("snaplen raised from %d to %d", snaplen
, i
);
413 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
419 * Let user own process after socket has been opened.
424 cmdbuf
= read_infile(infile
);
426 cmdbuf
= copy_argv(&argv
[optind
]);
428 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
429 error("%s", pcap_geterr(pd
));
431 bpf_dump(&fcode
, dflag
);
434 init_addrtoname(localnet
, netmask
);
436 (void)setsignal(SIGTERM
, cleanup
);
437 (void)setsignal(SIGINT
, cleanup
);
438 /* Cooperate with nohup(1) */
439 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
440 (void)setsignal(SIGHUP
, oldhandler
);
442 if (pcap_setfilter(pd
, &fcode
) < 0)
443 error("%s", pcap_geterr(pd
));
445 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
447 error("%s", pcap_geterr(pd
));
449 pcap_userdata
= (u_char
*)p
;
451 printer
= lookup_printer(pcap_datalink(pd
));
454 if (RFileName
== NULL
) {
455 (void)fprintf(stderr
, "%s: listening on %s\n",
456 program_name
, device
);
457 (void)fflush(stderr
);
459 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
460 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
461 program_name
, pcap_geterr(pd
));
468 /* make a clean exit on interrupts */
472 struct pcap_stat stat
;
474 /* Can't print the summary if reading from a savefile */
475 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
476 (void)fflush(stdout
);
478 if (pcap_stats(pd
, &stat
) < 0)
479 (void)fprintf(stderr
, "pcap_stats: %s\n",
482 (void)fprintf(stderr
, "%d packets received by filter\n",
484 (void)fprintf(stderr
, "%d packets dropped by kernel\n",
491 /* Like default_print() but data need not be aligned */
493 default_print_unaligned(register const u_char
*cp
, register u_int length
)
496 register int nshorts
;
499 ascii_print(cp
, length
);
502 nshorts
= (u_int
) length
/ sizeof(u_short
);
504 while (--nshorts
>= 0) {
506 (void)printf("\n\t\t\t");
508 (void)printf(" %02x%02x", s
, *cp
++);
512 (void)printf("\n\t\t\t");
513 (void)printf(" %02x", *cp
);
518 * By default, print the packet out in hex.
521 default_print(register const u_char
*bp
, register u_int length
)
523 default_print_unaligned(bp
, length
);
529 extern char version
[];
530 extern char pcap_version
[];
532 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
533 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
534 (void)fprintf(stderr
,
535 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name
);
536 (void)fprintf(stderr
,
537 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
538 (void)fprintf(stderr
,
539 "\t\t[ -T type ] [ -w file ] [ expression ]\n");