]>
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.130 1999-10-17 21:37:17 mcr 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.
38 #include <sys/types.h>
41 #include <netinet/in.h>
50 #include "interface.h"
51 #include "addrtoname.h"
53 #include "setsignal.h"
54 #include "gmt2local.h"
56 int aflag
; /* translate network and broadcast addresses */
57 int dflag
; /* print filter code */
58 int eflag
; /* print ethernet header */
59 int fflag
; /* don't translate "foreign" IP address */
60 int nflag
; /* leave addresses as numbers */
61 int Nflag
; /* remove domains from printed host names */
62 int Oflag
= 1; /* run filter code optimizer */
63 int pflag
; /* don't go promiscuous */
64 int qflag
; /* quick (shorter) output */
65 int Sflag
; /* print raw TCP sequence numbers */
66 int tflag
= 1; /* print packet arrival time */
67 int vflag
; /* verbose */
68 int xflag
; /* print packet in hex */
75 int32_t thiszone
; /* seconds offset from gmt to local time */
78 extern void bpf_dump(struct bpf_program
*, int);
81 RETSIGTYPE
cleanup(int);
82 extern __dead
void usage(void) __attribute__((volatile));
84 /* Length of saved portion of packet. */
85 int snaplen
= DEFAULT_SNAPLEN
;
92 static struct printer printers
[] = {
93 { ether_if_print
, DLT_EN10MB
},
94 { ether_if_print
, DLT_IEEE802
},
95 { sl_if_print
, DLT_SLIP
},
96 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
97 { ppp_if_print
, DLT_PPP
},
98 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
99 { fddi_if_print
, DLT_FDDI
},
100 { null_if_print
, DLT_NULL
},
101 { raw_if_print
, DLT_RAW
},
102 { atm_if_print
, DLT_ATM_RFC1483
},
107 lookup_printer(int type
)
111 for (p
= printers
; p
->f
; ++p
)
115 error("unknown data link type 0x%x", type
);
126 main(int argc
, char **argv
)
128 register int cnt
, op
, i
;
129 bpf_u_int32 localnet
, netmask
;
130 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
131 pcap_handler printer
;
132 struct bpf_program fcode
;
133 RETSIGTYPE (*oldhandler
)(int);
134 u_char
*pcap_userdata
;
135 char ebuf
[PCAP_ERRBUF_SIZE
];
142 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
143 program_name
= cp
+ 1;
145 program_name
= argv
[0];
147 if (abort_on_misalignment(ebuf
) < 0)
152 (op
= getopt(argc
, argv
, "ac:defF:i:lnNOpqr:s:StT:vw:xY")) != EOF
)
162 error("invalid packet count %s", optarg
);
186 #ifdef HAVE_SETLINEBUF
189 setvbuf(stdout
, NULL
, _IOLBF
, 0);
218 snaplen
= atoi(optarg
);
220 error("invalid snaplen %s", optarg
);
232 if (strcasecmp(optarg
, "vat") == 0)
234 else if (strcasecmp(optarg
, "wb") == 0)
236 else if (strcasecmp(optarg
, "rpc") == 0)
238 else if (strcasecmp(optarg
, "rtp") == 0)
240 else if (strcasecmp(optarg
, "rtcp") == 0)
241 packettype
= PT_RTCP
;
242 else if (strcasecmp(optarg
, "snmp") == 0)
243 packettype
= PT_SNMP
;
245 error("unknown packet type `%s'", optarg
);
258 /* Undocumented flag */
274 error("-a and -n options are incompatible");
277 thiszone
= gmt2local(0);
279 if (RFileName
!= NULL
) {
281 * We don't need network access, so set it back to the user id.
282 * Also, this prevents the user from reading anyone's
287 pd
= pcap_open_offline(RFileName
, ebuf
);
293 error("-f and -r options are incompatible");
295 if (device
== NULL
) {
296 device
= pcap_lookupdev(ebuf
);
300 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
303 i
= pcap_snapshot(pd
);
305 warning("snaplen raised from %d to %d", snaplen
, i
);
308 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
314 * Let user own process after socket has been opened.
319 cmdbuf
= read_infile(infile
);
321 cmdbuf
= copy_argv(&argv
[optind
]);
323 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
324 error("%s", pcap_geterr(pd
));
326 bpf_dump(&fcode
, dflag
);
329 init_addrtoname(localnet
, netmask
);
331 (void)setsignal(SIGTERM
, cleanup
);
332 (void)setsignal(SIGINT
, cleanup
);
333 /* Cooperate with nohup(1) */
334 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
335 (void)setsignal(SIGHUP
, oldhandler
);
337 if (pcap_setfilter(pd
, &fcode
) < 0)
338 error("%s", pcap_geterr(pd
));
340 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
342 error("%s", pcap_geterr(pd
));
344 pcap_userdata
= (u_char
*)p
;
346 printer
= lookup_printer(pcap_datalink(pd
));
349 if (RFileName
== NULL
) {
350 (void)fprintf(stderr
, "%s: listening on %s\n",
351 program_name
, device
);
352 (void)fflush(stderr
);
354 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
355 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
356 program_name
, pcap_geterr(pd
));
363 /* make a clean exit on interrupts */
367 struct pcap_stat stat
;
369 /* Can't print the summary if reading from a savefile */
370 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
371 (void)fflush(stdout
);
373 if (pcap_stats(pd
, &stat
) < 0)
374 (void)fprintf(stderr
, "pcap_stats: %s\n",
377 (void)fprintf(stderr
, "%d packets received by filter\n",
379 (void)fprintf(stderr
, "%d packets dropped by kernel\n",
386 /* Like default_print() but data need not be aligned */
388 default_print_unaligned(register const u_char
*cp
, register u_int length
)
391 register int nshorts
;
393 nshorts
= (u_int
) length
/ sizeof(u_short
);
395 while (--nshorts
>= 0) {
397 (void)printf("\n\t\t\t");
399 (void)printf(" %02x%02x", s
, *cp
++);
403 (void)printf("\n\t\t\t");
404 (void)printf(" %02x", *cp
);
409 * By default, print the packet out in hex.
411 * (BTW, please don't send us patches to print the packet out in ascii)
414 default_print(register const u_char
*bp
, register u_int length
)
416 register const u_short
*sp
;
418 register int nshorts
;
421 default_print_unaligned(bp
, length
);
425 nshorts
= (u_int
) length
/ sizeof(u_short
);
427 while (--nshorts
>= 0) {
429 (void)printf("\n\t\t\t");
430 (void)printf(" %04x", ntohs(*sp
++));
434 (void)printf("\n\t\t\t");
435 (void)printf(" %02x", *(u_char
*)sp
);
442 extern char version
[];
443 extern char pcap_version
[];
445 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
446 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
447 (void)fprintf(stderr
,
448 "Usage: %s [-adeflnNOpqStvx] [-c count] [ -F file ]\n", program_name
);
449 (void)fprintf(stderr
,
450 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
451 (void)fprintf(stderr
,
452 "\t\t[ -T type ] [ -w file ] [ expression ]\n");