]>
The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
785dce7aa444e0f3927ac8c899d5cddf2b675639
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright
[] =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 static const char rcsid
[] =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.174 2002-02-05 10:07:40 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <sys/types.h>
51 #include <netinet/in.h>
62 #include "interface.h"
63 #include "addrtoname.h"
65 #include "setsignal.h"
66 #include "gmt2local.h"
68 int aflag
; /* translate network and broadcast addresses */
69 int dflag
; /* print filter code */
70 int eflag
; /* print ethernet header */
71 int fflag
; /* don't translate "foreign" IP address */
72 int nflag
; /* leave addresses as numbers */
73 int Nflag
; /* remove domains from printed host names */
74 int Oflag
= 1; /* run filter code optimizer */
75 int pflag
; /* don't go promiscuous */
76 int qflag
; /* quick (shorter) output */
77 int Rflag
= 1; /* print sequence # field in AH/ESP*/
78 int sflag
= 0; /* use the libsmi to translate OIDs */
79 int Sflag
; /* print raw TCP sequence numbers */
80 int tflag
= 1; /* print packet arrival time */
81 int uflag
= 0; /* Print undecoded NFS handles */
82 int vflag
; /* verbose */
83 int xflag
; /* print packet in hex */
84 int Xflag
; /* print packet in ascii as well as hex */
85 off_t Cflag
= 0; /* rotate dump files after this many bytes */
87 char *espsecret
= NULL
; /* ESP secret key */
96 int32_t thiszone
; /* seconds offset from gmt to local time */
99 static RETSIGTYPE
cleanup(int);
100 static void usage(void) __attribute__((noreturn
));
102 static void dump_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
105 RETSIGTYPE
requestinfo(int);
108 /* Length of saved portion of packet. */
109 int snaplen
= DEFAULT_SNAPLEN
;
116 static struct printer printers
[] = {
117 { arcnet_if_print
, DLT_ARCNET
},
118 { ether_if_print
, DLT_EN10MB
},
119 { token_if_print
, DLT_IEEE802
},
121 { lane_if_print
, DLT_LANE8023
},
124 { cip_if_print
, DLT_CIP
},
127 { cip_if_print
, DLT_ATM_CLIP
},
129 { sl_if_print
, DLT_SLIP
},
130 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
131 { ppp_if_print
, DLT_PPP
},
132 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
133 { fddi_if_print
, DLT_FDDI
},
134 { null_if_print
, DLT_NULL
},
136 { null_if_print
, DLT_LOOP
},
138 { raw_if_print
, DLT_RAW
},
139 { atm_if_print
, DLT_ATM_RFC1483
},
141 { chdlc_if_print
, DLT_C_HDLC
},
144 { chdlc_if_print
, DLT_HDLC
},
146 #ifdef DLT_PPP_SERIAL
147 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
150 { pppoe_if_print
, DLT_PPP_ETHER
},
153 { sll_if_print
, DLT_LINUX_SLL
},
155 #ifdef DLT_IEEE802_11
156 { ieee802_11_if_print
, DLT_IEEE802_11
},
159 { ltalk_if_print
, DLT_LTALK
},
162 { pflog_if_print
, DLT_PFLOG
},
168 lookup_printer(int type
)
172 for (p
= printers
; p
->f
; ++p
)
176 error("unknown data link type %d", type
);
193 main(int argc
, char **argv
)
195 register int cnt
, op
, i
;
196 bpf_u_int32 localnet
, netmask
;
197 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
198 pcap_handler printer
;
199 struct bpf_program fcode
;
200 RETSIGTYPE (*oldhandler
)(int);
201 struct dump_info dumpinfo
;
202 u_char
*pcap_userdata
;
203 char ebuf
[PCAP_ERRBUF_SIZE
];
210 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
211 program_name
= cp
+ 1;
213 program_name
= argv
[0];
215 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
224 (op
= getopt(argc
, argv
, "ac:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
234 error("invalid packet count %s", optarg
);
238 Cflag
= atoi(optarg
) * 1000000;
240 error("invalid file size %s", optarg
);
252 #ifndef HAVE_LIBCRYPTO
253 warning("crypto code not compiled in");
271 #ifdef HAVE_SETLINEBUF
274 setvbuf(stdout
, NULL
, _IOLBF
, 0);
288 if (smiLoadModule(optarg
) == 0) {
289 error("could not load MIB module %s", optarg
);
293 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
294 program_name
, optarg
);
295 (void)fprintf(stderr
, "(no libsmi support)\n");
321 snaplen
= strtol(optarg
, &end
, 0);
322 if (optarg
== end
|| *end
!= '\0'
323 || snaplen
< 0 || snaplen
> 65535)
324 error("invalid snaplen %s", optarg
);
325 else if (snaplen
== 0)
339 if (strcasecmp(optarg
, "vat") == 0)
341 else if (strcasecmp(optarg
, "wb") == 0)
343 else if (strcasecmp(optarg
, "rpc") == 0)
345 else if (strcasecmp(optarg
, "rtp") == 0)
347 else if (strcasecmp(optarg
, "rtcp") == 0)
348 packettype
= PT_RTCP
;
349 else if (strcasecmp(optarg
, "snmp") == 0)
350 packettype
= PT_SNMP
;
351 else if (strcasecmp(optarg
, "cnfp") == 0)
352 packettype
= PT_CNFP
;
354 error("unknown packet type `%s'", optarg
);
381 /* Undocumented flag */
393 error("-a and -n options are incompatible");
396 thiszone
= gmt2local(0);
398 if (RFileName
!= NULL
) {
400 * We don't need network access, so set it back to the user id.
401 * Also, this prevents the user from reading anyone's
406 pd
= pcap_open_offline(RFileName
, ebuf
);
412 error("-f and -r options are incompatible");
414 if (device
== NULL
) {
415 device
= pcap_lookupdev(ebuf
);
420 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
425 i
= pcap_snapshot(pd
);
427 warning("snaplen raised from %d to %d", snaplen
, i
);
430 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
436 * Let user own process after socket has been opened.
441 cmdbuf
= read_infile(infile
);
443 cmdbuf
= copy_argv(&argv
[optind
]);
445 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
446 error("%s", pcap_geterr(pd
));
448 bpf_dump(&fcode
, dflag
);
451 init_addrtoname(localnet
, netmask
);
453 (void)setsignal(SIGTERM
, cleanup
);
454 (void)setsignal(SIGINT
, cleanup
);
455 /* Cooperate with nohup(1) */
456 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
457 (void)setsignal(SIGHUP
, oldhandler
);
459 if (pcap_setfilter(pd
, &fcode
) < 0)
460 error("%s", pcap_geterr(pd
));
462 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
464 error("%s", pcap_geterr(pd
));
466 printer
= dump_and_trunc
;
467 dumpinfo
.WFileName
= WFileName
;
470 pcap_userdata
= (u_char
*)&dumpinfo
;
473 pcap_userdata
= (u_char
*)p
;
476 printer
= lookup_printer(pcap_datalink(pd
));
479 (void)setsignal(SIGINFO
, requestinfo
);
482 if (RFileName
== NULL
) {
483 (void)fprintf(stderr
, "%s: listening on %s\n",
484 program_name
, device
);
485 (void)fflush(stderr
);
487 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
488 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
489 program_name
, pcap_geterr(pd
));
492 if (RFileName
== NULL
)
498 /* make a clean exit on interrupts */
503 /* Can't print the summary if reading from a savefile */
504 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
505 (void)fflush(stdout
);
513 info(register int verbose
)
515 struct pcap_stat stat
;
517 if (pcap_stats(pd
, &stat
) < 0) {
518 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
522 fprintf(stderr
, "%s: ", program_name
);
523 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
528 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
537 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
546 swebitoa(unsigned int n
, char *s
)
552 s
[i
++] = n
% 10 + '0';
553 } while ((n
/= 10) > 0);
560 dump_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
562 struct dump_info
*info
;
566 info
= (struct dump_info
*)user
;
569 * XXX - this won't prevent capture files from getting
570 * larger than Cflag - the last packet written to the
571 * file could put it over Cflag.
573 if (ftell((FILE *)info
->p
) > Cflag
) {
574 name
= (char *) malloc(strlen(info
->WFileName
) + 4);
576 error("dump_and_trunc: malloc");
577 strcpy(name
, info
->WFileName
);
578 swebitoa(cnt
, name
+ strlen(info
->WFileName
));
580 pcap_dump_close(info
->p
);
581 info
->p
= pcap_dump_open(info
->pd
, name
);
584 error("%s", pcap_geterr(pd
));
587 pcap_dump((u_char
*)info
->p
, h
, sp
);
590 /* Like default_print() but data need not be aligned */
592 default_print_unaligned(register const u_char
*cp
, register u_int length
)
595 register int nshorts
;
598 ascii_print(cp
, length
);
601 nshorts
= (u_int
) length
/ sizeof(u_short
);
603 while (--nshorts
>= 0) {
605 (void)printf("\n\t\t\t");
607 (void)printf(" %02x%02x", s
, *cp
++);
611 (void)printf("\n\t\t\t");
612 (void)printf(" %02x", *cp
);
617 * By default, print the packet out in hex.
620 default_print(register const u_char
*bp
, register u_int length
)
622 default_print_unaligned(bp
, length
);
626 RETSIGTYPE
requestinfo(int signo
)
638 extern char version
[];
639 extern char pcap_version
[];
641 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
642 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
643 (void)fprintf(stderr
,
644 "Usage: %s [-adeflnNOpqRStuvxX] [ -c count ] [ -C file_size ]\n", program_name
);
645 (void)fprintf(stderr
,
646 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
647 (void)fprintf(stderr
,
648 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ expression ]\n");