]>
The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
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.157 2000-12-16 22:00:50 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 { ether_if_print
, DLT_EN10MB
},
103 { token_if_print
, DLT_IEEE802
},
105 { lane_if_print
, DLT_LANE8023
},
108 { cip_if_print
, DLT_CIP
},
111 { cip_if_print
, DLT_ATM_CLIP
},
113 { sl_if_print
, DLT_SLIP
},
114 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
115 { ppp_if_print
, DLT_PPP
},
116 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
117 { fddi_if_print
, DLT_FDDI
},
118 { null_if_print
, DLT_NULL
},
120 { null_if_print
, DLT_LOOP
},
122 { raw_if_print
, DLT_RAW
},
123 { atm_if_print
, DLT_ATM_RFC1483
},
125 { chdlc_if_print
, DLT_C_HDLC
},
127 #ifdef DLT_PPP_SERIAL
128 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
134 lookup_printer(int type
)
138 for (p
= printers
; p
->f
; ++p
)
142 error("unknown data link type %d", type
);
153 main(int argc
, char **argv
)
155 register int cnt
, op
, i
;
156 bpf_u_int32 localnet
, netmask
;
157 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
158 pcap_handler printer
;
159 struct bpf_program fcode
;
160 RETSIGTYPE (*oldhandler
)(int);
161 u_char
*pcap_userdata
;
162 char ebuf
[PCAP_ERRBUF_SIZE
];
169 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
170 program_name
= cp
+ 1;
172 program_name
= argv
[0];
174 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
183 (op
= getopt(argc
, argv
, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
193 error("invalid packet count %s", optarg
);
205 #ifndef HAVE_LIBCRYPTO
206 warning("crypto code not compiled in");
224 #ifdef HAVE_SETLINEBUF
227 setvbuf(stdout
, NULL
, _IOLBF
, 0);
241 if (smiLoadModule(optarg
) == 0) {
242 error("could not load MIB module %s", optarg
);
246 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
247 program_name
, optarg
);
248 (void)fprintf(stderr
, "(no libsmi support)\n");
274 snaplen
= strtol(optarg
, &end
, 0);
275 if (optarg
== end
|| *end
!= '\0'
276 || snaplen
< 0 || snaplen
> 65535)
277 error("invalid snaplen %s", optarg
);
278 else if (snaplen
== 0)
292 if (strcasecmp(optarg
, "vat") == 0)
294 else if (strcasecmp(optarg
, "wb") == 0)
296 else if (strcasecmp(optarg
, "rpc") == 0)
298 else if (strcasecmp(optarg
, "rtp") == 0)
300 else if (strcasecmp(optarg
, "rtcp") == 0)
301 packettype
= PT_RTCP
;
302 else if (strcasecmp(optarg
, "snmp") == 0)
303 packettype
= PT_SNMP
;
304 else if (strcasecmp(optarg
, "cnfp") == 0)
305 packettype
= PT_CNFP
;
307 error("unknown packet type `%s'", optarg
);
334 /* Undocumented flag */
346 error("-a and -n options are incompatible");
349 thiszone
= gmt2local(0);
351 if (RFileName
!= NULL
) {
353 * We don't need network access, so set it back to the user id.
354 * Also, this prevents the user from reading anyone's
359 pd
= pcap_open_offline(RFileName
, ebuf
);
365 error("-f and -r options are incompatible");
367 if (device
== NULL
) {
368 device
= pcap_lookupdev(ebuf
);
372 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
375 i
= pcap_snapshot(pd
);
377 warning("snaplen raised from %d to %d", snaplen
, i
);
380 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
386 * Let user own process after socket has been opened.
391 cmdbuf
= read_infile(infile
);
393 cmdbuf
= copy_argv(&argv
[optind
]);
395 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
396 error("%s", pcap_geterr(pd
));
398 bpf_dump(&fcode
, dflag
);
401 init_addrtoname(localnet
, netmask
);
403 (void)setsignal(SIGTERM
, cleanup
);
404 (void)setsignal(SIGINT
, cleanup
);
405 /* Cooperate with nohup(1) */
406 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
407 (void)setsignal(SIGHUP
, oldhandler
);
409 if (pcap_setfilter(pd
, &fcode
) < 0)
410 error("%s", pcap_geterr(pd
));
412 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
414 error("%s", pcap_geterr(pd
));
416 pcap_userdata
= (u_char
*)p
;
418 printer
= lookup_printer(pcap_datalink(pd
));
421 if (RFileName
== NULL
) {
422 (void)fprintf(stderr
, "%s: listening on %s\n",
423 program_name
, device
);
424 (void)fflush(stderr
);
426 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
427 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
428 program_name
, pcap_geterr(pd
));
435 /* make a clean exit on interrupts */
439 struct pcap_stat stat
;
441 /* Can't print the summary if reading from a savefile */
442 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
443 (void)fflush(stdout
);
445 if (pcap_stats(pd
, &stat
) < 0)
446 (void)fprintf(stderr
, "pcap_stats: %s\n",
449 (void)fprintf(stderr
, "%d packets received by filter\n",
451 (void)fprintf(stderr
, "%d packets dropped by kernel\n",
458 /* Like default_print() but data need not be aligned */
460 default_print_unaligned(register const u_char
*cp
, register u_int length
)
463 register int nshorts
;
466 ascii_print(cp
, length
);
469 nshorts
= (u_int
) length
/ sizeof(u_short
);
471 while (--nshorts
>= 0) {
473 (void)printf("\n\t\t\t");
475 (void)printf(" %02x%02x", s
, *cp
++);
479 (void)printf("\n\t\t\t");
480 (void)printf(" %02x", *cp
);
485 * By default, print the packet out in hex.
488 default_print(register const u_char
*bp
, register u_int length
)
490 default_print_unaligned(bp
, length
);
496 extern char version
[];
497 extern char pcap_version
[];
499 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
500 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
501 (void)fprintf(stderr
,
502 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name
);
503 (void)fprintf(stderr
,
504 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
505 (void)fprintf(stderr
,
506 "\t\t[ -T type ] [ -w file ] [ expression ]\n");