]>
The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
2a2c106cb43daa652a1b72e970b6749b17904ba5
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.163 2001-06-18 08:52:54 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>
56 #include "interface.h"
57 #include "addrtoname.h"
59 #include "setsignal.h"
60 #include "gmt2local.h"
62 int aflag
; /* translate network and broadcast addresses */
63 int dflag
; /* print filter code */
64 int eflag
; /* print ethernet header */
65 int fflag
; /* don't translate "foreign" IP address */
66 int nflag
; /* leave addresses as numbers */
67 int Nflag
; /* remove domains from printed host names */
68 int Oflag
= 1; /* run filter code optimizer */
69 int pflag
; /* don't go promiscuous */
70 int qflag
; /* quick (shorter) output */
71 int Rflag
= 1; /* print sequence # field in AH/ESP*/
72 int sflag
= 0; /* use the libsmi to translate OIDs */
73 int Sflag
; /* print raw TCP sequence numbers */
74 int tflag
= 1; /* print packet arrival time */
75 int uflag
= 0; /* Print undecoded NFS handles */
76 int vflag
; /* verbose */
77 int xflag
; /* print packet in hex */
78 int Xflag
; /* print packet in ascii as well as hex */
80 char *espsecret
= NULL
; /* ESP secret key */
87 int32_t thiszone
; /* seconds offset from gmt to local time */
90 static RETSIGTYPE
cleanup(int);
91 static void usage(void) __attribute__((noreturn
));
93 /* Length of saved portion of packet. */
94 int snaplen
= DEFAULT_SNAPLEN
;
101 static struct printer printers
[] = {
102 { arcnet_if_print
, DLT_ARCNET
},
103 { ether_if_print
, DLT_EN10MB
},
104 { token_if_print
, DLT_IEEE802
},
106 { lane_if_print
, DLT_LANE8023
},
109 { cip_if_print
, DLT_CIP
},
112 { cip_if_print
, DLT_ATM_CLIP
},
114 { sl_if_print
, DLT_SLIP
},
115 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
116 { ppp_if_print
, DLT_PPP
},
117 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
118 { fddi_if_print
, DLT_FDDI
},
119 { null_if_print
, DLT_NULL
},
121 { null_if_print
, DLT_LOOP
},
123 { raw_if_print
, DLT_RAW
},
124 { atm_if_print
, DLT_ATM_RFC1483
},
126 { chdlc_if_print
, DLT_C_HDLC
},
129 { chdlc_if_print
, DLT_HDLC
},
131 #ifdef DLT_PPP_SERIAL
132 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
135 { sll_if_print
, DLT_LINUX_SLL
},
137 #ifdef DLT_IEEE802_11
138 { ieee802_11_if_print
, DLT_IEEE802_11
},
141 { ltalk_if_print
, DLT_LTALK
},
147 lookup_printer(int type
)
151 for (p
= printers
; p
->f
; ++p
)
155 error("unknown data link type %d", type
);
166 main(int argc
, char **argv
)
168 register int cnt
, op
, i
;
169 bpf_u_int32 localnet
, netmask
;
170 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
171 pcap_handler printer
;
172 struct bpf_program fcode
;
173 RETSIGTYPE (*oldhandler
)(int);
174 u_char
*pcap_userdata
;
175 char ebuf
[PCAP_ERRBUF_SIZE
];
182 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
183 program_name
= cp
+ 1;
185 program_name
= argv
[0];
187 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
196 (op
= getopt(argc
, argv
, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
206 error("invalid packet count %s", optarg
);
218 #ifndef HAVE_LIBCRYPTO
219 warning("crypto code not compiled in");
237 #ifdef HAVE_SETLINEBUF
240 setvbuf(stdout
, NULL
, _IOLBF
, 0);
254 if (smiLoadModule(optarg
) == 0) {
255 error("could not load MIB module %s", optarg
);
259 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
260 program_name
, optarg
);
261 (void)fprintf(stderr
, "(no libsmi support)\n");
287 snaplen
= strtol(optarg
, &end
, 0);
288 if (optarg
== end
|| *end
!= '\0'
289 || snaplen
< 0 || snaplen
> 65535)
290 error("invalid snaplen %s", optarg
);
291 else if (snaplen
== 0)
305 if (strcasecmp(optarg
, "vat") == 0)
307 else if (strcasecmp(optarg
, "wb") == 0)
309 else if (strcasecmp(optarg
, "rpc") == 0)
311 else if (strcasecmp(optarg
, "rtp") == 0)
313 else if (strcasecmp(optarg
, "rtcp") == 0)
314 packettype
= PT_RTCP
;
315 else if (strcasecmp(optarg
, "snmp") == 0)
316 packettype
= PT_SNMP
;
317 else if (strcasecmp(optarg
, "cnfp") == 0)
318 packettype
= PT_CNFP
;
320 error("unknown packet type `%s'", optarg
);
347 /* Undocumented flag */
359 error("-a and -n options are incompatible");
362 thiszone
= gmt2local(0);
364 if (RFileName
!= NULL
) {
366 * We don't need network access, so set it back to the user id.
367 * Also, this prevents the user from reading anyone's
372 pd
= pcap_open_offline(RFileName
, ebuf
);
378 error("-f and -r options are incompatible");
380 if (device
== NULL
) {
381 device
= pcap_lookupdev(ebuf
);
386 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
391 i
= pcap_snapshot(pd
);
393 warning("snaplen raised from %d to %d", snaplen
, i
);
396 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
402 * Let user own process after socket has been opened.
407 cmdbuf
= read_infile(infile
);
409 cmdbuf
= copy_argv(&argv
[optind
]);
411 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
412 error("%s", pcap_geterr(pd
));
414 bpf_dump(&fcode
, dflag
);
417 init_addrtoname(localnet
, netmask
);
419 (void)setsignal(SIGTERM
, cleanup
);
420 (void)setsignal(SIGINT
, cleanup
);
421 /* Cooperate with nohup(1) */
422 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
423 (void)setsignal(SIGHUP
, oldhandler
);
425 if (pcap_setfilter(pd
, &fcode
) < 0)
426 error("%s", pcap_geterr(pd
));
428 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
430 error("%s", pcap_geterr(pd
));
432 pcap_userdata
= (u_char
*)p
;
434 printer
= lookup_printer(pcap_datalink(pd
));
437 if (RFileName
== NULL
) {
438 (void)fprintf(stderr
, "%s: listening on %s\n",
439 program_name
, device
);
440 (void)fflush(stderr
);
442 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
443 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
444 program_name
, pcap_geterr(pd
));
451 /* make a clean exit on interrupts */
455 struct pcap_stat stat
;
457 /* Can't print the summary if reading from a savefile */
458 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
459 (void)fflush(stdout
);
461 if (pcap_stats(pd
, &stat
) < 0)
462 (void)fprintf(stderr
, "pcap_stats: %s\n",
465 (void)fprintf(stderr
, "%d packets received by filter\n",
467 (void)fprintf(stderr
, "%d packets dropped by kernel\n",
474 /* Like default_print() but data need not be aligned */
476 default_print_unaligned(register const u_char
*cp
, register u_int length
)
479 register int nshorts
;
482 ascii_print(cp
, length
);
485 nshorts
= (u_int
) length
/ sizeof(u_short
);
487 while (--nshorts
>= 0) {
489 (void)printf("\n\t\t\t");
491 (void)printf(" %02x%02x", s
, *cp
++);
495 (void)printf("\n\t\t\t");
496 (void)printf(" %02x", *cp
);
501 * By default, print the packet out in hex.
504 default_print(register const u_char
*bp
, register u_int length
)
506 default_print_unaligned(bp
, length
);
512 extern char version
[];
513 extern char pcap_version
[];
515 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
516 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
517 (void)fprintf(stderr
,
518 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name
);
519 (void)fprintf(stderr
,
520 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
521 (void)fprintf(stderr
,
522 "\t\t[ -T type ] [ -w file ] [ expression ]\n");