]>
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.135 1999-11-21 09:37:04 fenner 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>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
57 #include "interface.h"
58 #include "addrtoname.h"
60 #include "setsignal.h"
61 #include "gmt2local.h"
63 int aflag
; /* translate network and broadcast addresses */
64 int dflag
; /* print filter code */
65 int eflag
; /* print ethernet header */
66 int fflag
; /* don't translate "foreign" IP address */
67 int nflag
; /* leave addresses as numbers */
68 int Nflag
; /* remove domains from printed host names */
69 int Oflag
= 1; /* run filter code optimizer */
70 int pflag
; /* don't go promiscuous */
71 int qflag
; /* quick (shorter) output */
72 int Rflag
= 1; /* print sequence # field in AH/ESP*/
73 int Sflag
; /* print raw TCP sequence numbers */
74 int tflag
= 1; /* print packet arrival time */
75 int vflag
; /* verbose */
76 int xflag
; /* print packet in hex */
78 char *ahsecret
= NULL
; /* AH secret key */
79 char *espsecret
= NULL
; /* ESP secret key */
86 int32_t thiszone
; /* seconds offset from gmt to local time */
89 extern void bpf_dump(struct bpf_program
*, int);
92 RETSIGTYPE
cleanup(int);
93 extern __dead
void usage(void) __attribute__((volatile));
95 /* Length of saved portion of packet. */
96 int snaplen
= DEFAULT_SNAPLEN
;
103 static struct printer printers
[] = {
104 { ether_if_print
, DLT_EN10MB
},
105 { ether_if_print
, DLT_IEEE802
},
107 { lane_if_print
, DLT_LANE8023
},
110 { cip_if_print
, DLT_CIP
},
112 { sl_if_print
, DLT_SLIP
},
113 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
114 { ppp_if_print
, DLT_PPP
},
115 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
116 { fddi_if_print
, DLT_FDDI
},
117 { null_if_print
, DLT_NULL
},
118 { raw_if_print
, DLT_RAW
},
119 { atm_if_print
, DLT_ATM_RFC1483
},
121 { chdlc_if_print
, DLT_CHDLC
},
127 lookup_printer(int type
)
131 for (p
= printers
; p
->f
; ++p
)
135 error("unknown data link type 0x%x", type
);
146 main(int argc
, char **argv
)
148 register int cnt
, op
, i
;
149 bpf_u_int32 localnet
, netmask
;
150 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
151 pcap_handler printer
;
152 struct bpf_program fcode
;
153 RETSIGTYPE (*oldhandler
)(int);
154 u_char
*pcap_userdata
;
155 char ebuf
[PCAP_ERRBUF_SIZE
];
162 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
163 program_name
= cp
+ 1;
165 program_name
= argv
[0];
167 if (abort_on_misalignment(ebuf
) < 0)
172 (op
= getopt(argc
, argv
, "ac:deE:fF:i:lnNOpqr:Rs:StT:vw:xY")) != EOF
)
182 warning("crypto code not compiled in");
191 error("invalid packet count %s", optarg
);
204 warning("crypto code not compiled in");
222 #ifdef HAVE_SETLINEBUF
225 setvbuf(stdout
, NULL
, _IOLBF
, 0);
258 snaplen
= atoi(optarg
);
260 error("invalid snaplen %s", optarg
);
272 if (strcasecmp(optarg
, "vat") == 0)
274 else if (strcasecmp(optarg
, "wb") == 0)
276 else if (strcasecmp(optarg
, "rpc") == 0)
278 else if (strcasecmp(optarg
, "rtp") == 0)
280 else if (strcasecmp(optarg
, "rtcp") == 0)
281 packettype
= PT_RTCP
;
282 else if (strcasecmp(optarg
, "snmp") == 0)
283 packettype
= PT_SNMP
;
285 error("unknown packet type `%s'", optarg
);
298 /* Undocumented flag */
314 error("-a and -n options are incompatible");
317 thiszone
= gmt2local(0);
319 if (RFileName
!= NULL
) {
321 * We don't need network access, so set it back to the user id.
322 * Also, this prevents the user from reading anyone's
327 pd
= pcap_open_offline(RFileName
, ebuf
);
333 error("-f and -r options are incompatible");
335 if (device
== NULL
) {
336 device
= pcap_lookupdev(ebuf
);
340 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
343 i
= pcap_snapshot(pd
);
345 warning("snaplen raised from %d to %d", snaplen
, i
);
348 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
354 * Let user own process after socket has been opened.
359 cmdbuf
= read_infile(infile
);
361 cmdbuf
= copy_argv(&argv
[optind
]);
363 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
364 error("%s", pcap_geterr(pd
));
366 bpf_dump(&fcode
, dflag
);
369 init_addrtoname(localnet
, netmask
);
371 (void)setsignal(SIGTERM
, cleanup
);
372 (void)setsignal(SIGINT
, cleanup
);
373 /* Cooperate with nohup(1) */
374 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
375 (void)setsignal(SIGHUP
, oldhandler
);
377 if (pcap_setfilter(pd
, &fcode
) < 0)
378 error("%s", pcap_geterr(pd
));
380 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
382 error("%s", pcap_geterr(pd
));
384 pcap_userdata
= (u_char
*)p
;
386 printer
= lookup_printer(pcap_datalink(pd
));
389 if (RFileName
== NULL
) {
390 (void)fprintf(stderr
, "%s: listening on %s\n",
391 program_name
, device
);
392 (void)fflush(stderr
);
394 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
395 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
396 program_name
, pcap_geterr(pd
));
403 /* make a clean exit on interrupts */
407 struct pcap_stat stat
;
409 /* Can't print the summary if reading from a savefile */
410 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
411 (void)fflush(stdout
);
413 if (pcap_stats(pd
, &stat
) < 0)
414 (void)fprintf(stderr
, "pcap_stats: %s\n",
417 (void)fprintf(stderr
, "%d packets received by filter\n",
419 (void)fprintf(stderr
, "%d packets dropped by kernel\n",
426 /* Like default_print() but data need not be aligned */
428 default_print_unaligned(register const u_char
*cp
, register u_int length
)
431 register int nshorts
;
435 nshorts
= (u_int
) length
/ sizeof(u_short
);
437 memset(line
, ' ', 80);
441 while (nshorts
>= 0) {
443 sprintf(line
+20+i
*5, "%02x%02x ", cp
[0], cp
[1]);
451 line
[62+i
*2+1]=cp
[1];
469 * By default, print the packet out in hex.
471 * (BTW, please don't send us patches to print the packet out in ascii)
474 default_print(register const u_char
*bp
, register u_int length
)
476 register const u_short
*sp
;
478 register int nshorts
;
480 default_print_unaligned(bp
, length
);
486 extern char version
[];
487 extern char pcap_version
[];
489 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
490 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
491 (void)fprintf(stderr
,
492 "Usage: %s [-adeflnNOpqStvx] [-c count] [ -F file ]\n", program_name
);
493 (void)fprintf(stderr
,
494 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
495 (void)fprintf(stderr
,
496 "\t\t[ -T type ] [ -w file ] [ expression ]\n");