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
[] _U_
=
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
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.262 2005-10-20 07:43:52 hannes 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 <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
74 #include "netdissect.h"
75 #include "interface.h"
76 #include "addrtoname.h"
78 #include "setsignal.h"
79 #include "gmt2local.h"
80 #include "pcap-missing.h"
86 netdissect_options Gndo
;
87 netdissect_options
*gndo
= &Gndo
;
89 int dflag
; /* print filter code */
90 int Lflag
; /* list available data link types and exit */
97 int32_t thiszone
; /* seconds offset from gmt to local time */
100 static RETSIGTYPE
cleanup(int);
101 static void usage(void) __attribute__((noreturn
));
102 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
104 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
105 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
106 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
107 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
108 static void droproot(const char *, const char *);
109 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
110 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
113 RETSIGTYPE
requestinfo(int);
116 #if defined(USE_WIN32_MM_TIMER)
117 #include <MMsystem.h>
118 static UINT timer_id
;
119 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
120 #elif defined(HAVE_ALARM)
121 static void verbose_stats_dump(int sig
);
124 static void info(int);
125 static u_int packets_captured
;
127 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
134 static struct printer printers
[] = {
135 { arcnet_if_print
, DLT_ARCNET
},
136 #ifdef DLT_ARCNET_LINUX
137 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
139 { ether_if_print
, DLT_EN10MB
},
140 { token_if_print
, DLT_IEEE802
},
142 { lane_if_print
, DLT_LANE8023
},
145 { cip_if_print
, DLT_CIP
},
148 { cip_if_print
, DLT_ATM_CLIP
},
150 { sl_if_print
, DLT_SLIP
},
151 #ifdef DLT_SLIP_BSDOS
152 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
154 { ppp_if_print
, DLT_PPP
},
155 #ifdef DLT_PPP_WITHDIRECTION
156 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
159 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
161 { fddi_if_print
, DLT_FDDI
},
162 { null_if_print
, DLT_NULL
},
164 { null_if_print
, DLT_LOOP
},
166 { raw_if_print
, DLT_RAW
},
167 { atm_if_print
, DLT_ATM_RFC1483
},
169 { chdlc_if_print
, DLT_C_HDLC
},
172 { chdlc_if_print
, DLT_HDLC
},
174 #ifdef DLT_PPP_SERIAL
175 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
178 { pppoe_if_print
, DLT_PPP_ETHER
},
181 { sll_if_print
, DLT_LINUX_SLL
},
183 #ifdef DLT_IEEE802_11
184 { ieee802_11_if_print
, DLT_IEEE802_11
},
187 { ltalk_if_print
, DLT_LTALK
},
190 { pflog_if_print
, DLT_PFLOG
},
193 { fr_if_print
, DLT_FR
},
196 { fr_if_print
, DLT_FRELAY
},
199 { sunatm_if_print
, DLT_SUNATM
},
201 #ifdef DLT_IP_OVER_FC
202 { ipfc_if_print
, DLT_IP_OVER_FC
},
204 #ifdef DLT_PRISM_HEADER
205 { prism_if_print
, DLT_PRISM_HEADER
},
207 #ifdef DLT_IEEE802_11_RADIO
208 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
211 { enc_if_print
, DLT_ENC
},
213 #ifdef DLT_SYMANTEC_FIREWALL
214 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
216 #ifdef DLT_APPLE_IP_OVER_IEEE1394
217 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
219 #ifdef DLT_JUNIPER_ATM1
220 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
222 #ifdef DLT_JUNIPER_ATM2
223 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
225 #ifdef DLT_JUNIPER_MFR
226 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
228 #ifdef DLT_JUNIPER_MLFR
229 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
231 #ifdef DLT_JUNIPER_MLPPP
232 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
234 #ifdef DLT_JUNIPER_PPPOE
235 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
237 #ifdef DLT_JUNIPER_PPPOE_ATM
238 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
240 #ifdef DLT_JUNIPER_GGSN
241 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
243 #ifdef DLT_JUNIPER_ES
244 { juniper_es_print
, DLT_JUNIPER_ES
},
246 #ifdef DLT_JUNIPER_MONITOR
247 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
249 #ifdef DLT_JUNIPER_SERVICES
250 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
252 #ifdef DLT_JUNIPER_ETHER
253 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
255 #ifdef DLT_JUNIPER_PPP
256 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
258 #ifdef DLT_JUNIPER_FRELAY
259 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
261 #ifdef DLT_JUNIPER_CHDLC
262 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
268 lookup_printer(int type
)
272 for (p
= printers
; p
->f
; ++p
)
297 show_dlts_and_exit(pcap_t
*pd
)
301 const char *dlt_name
;
303 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
305 error("%s", pcap_geterr(pd
));
306 else if (n_dlts
== 0 || !dlts
)
307 error("No data link types.");
309 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
311 while (--n_dlts
>= 0) {
312 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
313 if (dlt_name
!= NULL
) {
314 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
315 pcap_datalink_val_to_description(dlts
[n_dlts
]));
318 * OK, does tcpdump handle that type?
320 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
321 (void) fprintf(stderr
, " (not supported)");
324 (void) fprintf(stderr
, " DLT %d (not supported)\n",
333 * Set up flags that might or might not be supported depending on the
334 * version of libpcap we're using.
338 #define B_FLAG_USAGE " [ -B size ]"
344 #ifdef HAVE_PCAP_FINDALLDEVS
345 #ifndef HAVE_PCAP_IF_T
346 #undef HAVE_PCAP_FINDALLDEVS
350 #ifdef HAVE_PCAP_FINDALLDEVS
356 #ifdef HAVE_PCAP_DUMP_FLUSH
363 /* Drop root privileges and chroot if necessary */
365 droproot(const char *username
, const char *chroot_dir
)
367 struct passwd
*pw
= NULL
;
369 if (chroot_dir
&& !username
) {
370 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
374 pw
= getpwnam(username
);
377 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
378 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
379 chroot_dir
, pcap_strerror(errno
));
383 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
384 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
385 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
387 (unsigned long)pw
->pw_uid
,
388 (unsigned long)pw
->pw_gid
,
389 pcap_strerror(errno
));
394 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
417 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
419 char *filename
= malloc(NAME_MAX
+ 1);
421 /* Process with strftime if Gflag is set. */
425 /* Convert Gflag_time to a usable format */
426 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
427 error("MakeTimedFilename: localtime");
430 /* There's no good way to detect an error in strftime since a return
431 * value of 0 isn't necessarily failure.
433 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
435 strncpy(filename
, orig_name
, NAME_MAX
);
438 if (cnt
== 0 && max_chars
== 0)
439 strncpy(buffer
, filename
, NAME_MAX
+ 1);
441 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
442 /* Report an error if the filename is too large */
443 error("too many output files or filename is too long (> %d)", NAME_MAX
);
446 static int tcpdump_printf(netdissect_options
*ndo _U_
,
447 const char *fmt
, ...)
454 ret
=vfprintf(stdout
, fmt
, args
);
461 main(int argc
, char **argv
)
463 register int cnt
, op
, i
;
464 bpf_u_int32 localnet
, netmask
;
465 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
466 pcap_handler callback
;
468 struct bpf_program fcode
;
470 RETSIGTYPE (*oldhandler
)(int);
472 struct print_info printinfo
;
473 struct dump_info dumpinfo
;
474 u_char
*pcap_userdata
;
475 char ebuf
[PCAP_ERRBUF_SIZE
];
476 char *username
= NULL
;
477 char *chroot_dir
= NULL
;
478 #ifdef HAVE_PCAP_FINDALLDEVS
479 pcap_if_t
*devpointer
;
484 u_int UserBufferSize
= 1000000;
485 if(wsockinit() != 0) return 1;
491 gndo
->ndo_default_print
=ndo_default_print
;
492 gndo
->ndo_printf
=tcpdump_printf
;
493 gndo
->ndo_error
=ndo_error
;
494 gndo
->ndo_warning
=ndo_warning
;
495 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
502 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
503 program_name
= cp
+ 1;
505 program_name
= argv
[0];
507 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
516 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:i:lLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:YZ:")) != -1)
520 /* compatibility for old -a */
529 UserBufferSize
= atoi(optarg
)*1024;
530 if (UserBufferSize
< 0)
531 error("invalid packet buffer size %s", optarg
);
538 error("invalid packet count %s", optarg
);
542 Cflag
= atoi(optarg
) * 1000000;
544 error("invalid file size %s", optarg
);
551 #ifdef HAVE_PCAP_FINDALLDEVS
553 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
556 for (i
= 0; devpointer
!= 0; i
++) {
557 printf("%d.%s", i
+1, devpointer
->name
);
558 if (devpointer
->description
!= NULL
)
559 printf(" (%s)", devpointer
->description
);
561 devpointer
= devpointer
->next
;
565 #endif /* HAVE_PCAP_FINDALLDEVS */
576 #ifndef HAVE_LIBCRYPTO
577 warning("crypto code not compiled in");
579 gndo
->ndo_espsecret
= optarg
;
591 Gflag
= atoi(optarg
);
592 /* We will create one file initially. */
594 /* Grab the current time for rotation use. */
597 if (gettimeofday(&t
, NULL
) != 0) {
598 error("main: gettimeofday");
600 Gflag_time
= t
.tv_sec
;
603 error("invalid number of seconds %s", optarg
);
607 if (optarg
[0] == '0' && optarg
[1] == 0)
608 error("Invalid adapter index");
610 #ifdef HAVE_PCAP_FINDALLDEVS
612 * If the argument is a number, treat it as
613 * an index into the list of adapters, as
614 * printed by "tcpdump -D".
616 * This should be OK on UNIX systems, as interfaces
617 * shouldn't have names that begin with digits.
618 * It can be useful on Windows, where more than
619 * one interface can have the same name.
621 if ((devnum
= atoi(optarg
)) != 0) {
623 error("Invalid adapter index");
625 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
628 for (i
= 0; i
< devnum
-1; i
++){
629 devpointer
= devpointer
->next
;
630 if (devpointer
== NULL
)
631 error("Invalid adapter index");
634 device
= devpointer
->name
;
637 #endif /* HAVE_PCAP_FINDALLDEVS */
644 * _IOLBF is the same as _IOFBF in Microsoft's C
645 * libraries; the only alternative they offer
648 * XXX - this should really be checking for MSVC++,
649 * not WIN32, if, for example, MinGW has its own
650 * C library that is more UNIX-compatible.
652 setvbuf(stdout
, NULL
, _IONBF
, 0);
654 #ifdef HAVE_SETLINEBUF
657 setvbuf(stdout
, NULL
, _IOLBF
, 0);
672 if (smiLoadModule(optarg
) == 0) {
673 error("could not load MIB module %s", optarg
);
677 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
678 program_name
, optarg
);
679 (void)fprintf(stderr
, "(no libsmi support)\n");
684 /* TCP-MD5 shared secret */
685 #ifndef HAVE_LIBCRYPTO
686 warning("crypto code not compiled in");
688 tcpmd5secret
= optarg
;
701 ++suppress_default_print
;
715 snaplen
= strtol(optarg
, &end
, 0);
716 if (optarg
== end
|| *end
!= '\0'
717 || snaplen
< 0 || snaplen
> 65535)
718 error("invalid snaplen %s", optarg
);
719 else if (snaplen
== 0)
733 if (strcasecmp(optarg
, "vat") == 0)
735 else if (strcasecmp(optarg
, "wb") == 0)
737 else if (strcasecmp(optarg
, "rpc") == 0)
739 else if (strcasecmp(optarg
, "rtp") == 0)
741 else if (strcasecmp(optarg
, "rtcp") == 0)
742 packettype
= PT_RTCP
;
743 else if (strcasecmp(optarg
, "snmp") == 0)
744 packettype
= PT_SNMP
;
745 else if (strcasecmp(optarg
, "cnfp") == 0)
746 packettype
= PT_CNFP
;
747 else if (strcasecmp(optarg
, "tftp") == 0)
748 packettype
= PT_TFTP
;
749 else if (strcasecmp(optarg
, "aodv") == 0)
750 packettype
= PT_AODV
;
752 error("unknown packet type `%s'", optarg
);
759 #ifdef HAVE_PCAP_DUMP_FLUSH
774 Wflag
= atoi(optarg
);
776 error("invalid number of output files %s", optarg
);
777 WflagChars
= getWflagChars(Wflag
);
782 ++suppress_default_print
;
787 ++suppress_default_print
;
791 gndo
->ndo_dltname
= optarg
;
793 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
794 if (gndo
->ndo_dlt
< 0)
795 error("invalid data link type %s", gndo
->ndo_dltname
);
798 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
801 /* Undocumented flag */
802 #ifdef HAVE_PCAP_DEBUG
803 extern int pcap_debug
;
814 username
= strdup(optarg
);
829 case 0: /* Default */
830 case 4: /* Default + Date*/
831 thiszone
= gmt2local(0);
834 case 1: /* No time stamp */
835 case 2: /* Unix timeval style */
836 case 3: /* Microseconds since previous packet */
839 default: /* Not supported */
840 error("only -t, -tt, -ttt, and -tttt are supported");
845 /* if run as root, prepare for chrooting */
846 if (getuid() == 0 || geteuid() == 0) {
847 /* future extensibility for cmd-line arguments */
849 chroot_dir
= WITH_CHROOT
;
854 /* if run as root, prepare for dropping root privileges */
855 if (getuid() == 0 || geteuid() == 0) {
856 /* Run with '-Z root' to restore old behaviour */
858 username
= WITH_USER
;
862 if (RFileName
!= NULL
) {
864 const char *dlt_name
;
868 * We don't need network access, so relinquish any set-UID
869 * or set-GID privileges we have (if any).
871 * We do *not* want set-UID privileges when opening a
872 * trace file, as that might let the user read other
873 * people's trace files (especially if we're set-UID
876 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
877 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
879 pd
= pcap_open_offline(RFileName
, ebuf
);
882 dlt
= pcap_datalink(pd
);
883 dlt_name
= pcap_datalink_val_to_name(dlt
);
884 if (dlt_name
== NULL
) {
885 fprintf(stderr
, "reading from file %s, link-type %u\n",
889 "reading from file %s, link-type %s (%s)\n",
891 pcap_datalink_val_to_description(dlt
));
896 error("-f and -r options are incompatible");
898 if (device
== NULL
) {
899 device
= pcap_lookupdev(ebuf
);
904 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
905 { //a Unicode string has a \0 as second byte (so strlen() is 1)
906 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
910 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
916 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
922 * Let user own process after socket has been opened.
925 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
926 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
929 if(UserBufferSize
!= 1000000)
930 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
931 error("%s", pcap_geterr(pd
));
935 show_dlts_and_exit(pd
);
936 if (gndo
->ndo_dlt
>= 0) {
937 #ifdef HAVE_PCAP_SET_DATALINK
938 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
939 error("%s", pcap_geterr(pd
));
942 * We don't actually support changing the
943 * data link type, so we only let them
944 * set it to what it already is.
946 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
947 error("%s is not one of the DLTs supported by this device\n",
951 (void)fprintf(stderr
, "%s: data link type %s\n",
952 program_name
, gndo
->ndo_dltname
);
953 (void)fflush(stderr
);
955 i
= pcap_snapshot(pd
);
957 warning("snaplen raised from %d to %d", snaplen
, i
);
960 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
967 cmdbuf
= read_infile(infile
);
969 cmdbuf
= copy_argv(&argv
[optind
]);
971 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
972 error("%s", pcap_geterr(pd
));
974 bpf_dump(&fcode
, dflag
);
978 init_addrtoname(localnet
, netmask
);
981 (void)setsignal(SIGPIPE
, cleanup
);
983 (void)setsignal(SIGTERM
, cleanup
);
984 (void)setsignal(SIGINT
, cleanup
);
985 /* Cooperate with nohup(1) */
987 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
988 (void)setsignal(SIGHUP
, oldhandler
);
991 if (pcap_setfilter(pd
, &fcode
) < 0)
992 error("%s", pcap_geterr(pd
));
995 /* Do not exceed the default NAME_MAX for files. */
996 WFileNameAlt
= (char *)malloc(NAME_MAX
+ 1);
998 if (WFileNameAlt
== NULL
)
999 error("malloc of WFileNameAlt");
1001 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1003 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
1005 MakeFilename(WFileNameAlt
, WFileName
, 0, 0);
1007 p
= pcap_dump_open(pd
, WFileNameAlt
);
1009 error("%s", pcap_geterr(pd
));
1010 if (Cflag
!= 0 || Gflag
!= 0) {
1011 callback
= dump_packet_and_trunc
;
1012 dumpinfo
.WFileName
= WFileName
;
1015 pcap_userdata
= (u_char
*)&dumpinfo
;
1017 callback
= dump_packet
;
1018 pcap_userdata
= (u_char
*)p
;
1021 type
= pcap_datalink(pd
);
1022 printinfo
.printer
= lookup_printer(type
);
1023 if (printinfo
.printer
== NULL
) {
1024 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1025 if (gndo
->ndo_dltname
!= NULL
)
1026 error("unsupported data link type %s",
1029 error("unsupported data link type %d", type
);
1031 callback
= print_packet
;
1032 pcap_userdata
= (u_char
*)&printinfo
;
1036 * We cannot do this earlier, because we want to be able to open
1037 * the file (if done) for writing before giving up permissions.
1039 if (getuid() == 0 || geteuid() == 0) {
1040 if (username
|| chroot_dir
)
1041 droproot(username
, chroot_dir
);
1045 (void)setsignal(SIGINFO
, requestinfo
);
1048 if (vflag
> 0 && WFileName
) {
1050 * When capturing to a file, "-v" means tcpdump should,
1051 * every 10 secodns, "v"erbosely report the number of
1054 #ifdef USE_WIN32_MM_TIMER
1055 /* call verbose_stats_dump() each 1000 +/-100msec */
1056 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1057 setvbuf(stderr
, NULL
, _IONBF
, 0);
1058 #elif defined(HAVE_ALARM)
1059 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1065 if (RFileName
== NULL
) {
1067 const char *dlt_name
;
1069 if (!vflag
&& !WFileName
) {
1070 (void)fprintf(stderr
,
1071 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1074 (void)fprintf(stderr
, "%s: ", program_name
);
1075 dlt
= pcap_datalink(pd
);
1076 dlt_name
= pcap_datalink_val_to_name(dlt
);
1077 if (dlt_name
== NULL
) {
1078 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1079 device
, dlt
, snaplen
);
1081 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1083 pcap_datalink_val_to_description(dlt
), snaplen
);
1085 (void)fflush(stderr
);
1088 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1089 if (WFileName
== NULL
) {
1091 * We're printing packets. Flush the printed output,
1092 * so it doesn't get intermingled with error output.
1096 * We got interrupted, so perhaps we didn't
1097 * manage to finish a line we were printing.
1098 * Print an extra newline, just in case.
1102 (void)fflush(stdout
);
1108 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1109 program_name
, pcap_geterr(pd
));
1111 if (RFileName
== NULL
) {
1113 * We're doing a live capture. Report the capture
1119 exit(status
== -1 ? 1 : 0);
1122 /* make a clean exit on interrupts */
1124 cleanup(int signo _U_
)
1126 #ifdef USE_WIN32_MM_TIMER
1128 timeKillEvent(timer_id
);
1130 #elif defined(HAVE_ALARM)
1134 #ifdef HAVE_PCAP_BREAKLOOP
1136 * We have "pcap_breakloop()"; use it, so that we do as little
1137 * as possible in the signal handler (it's probably not safe
1138 * to do anything with standard I/O streams in a signal handler -
1139 * the ANSI C standard doesn't say it is).
1144 * We don't have "pcap_breakloop()"; this isn't safe, but
1145 * it's the best we can do. Print the summary if we're
1146 * not reading from a savefile - i.e., if we're doing a
1147 * live capture - and exit.
1149 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1151 * We got interrupted, so perhaps we didn't
1152 * manage to finish a line we were printing.
1153 * Print an extra newline, just in case.
1156 (void)fflush(stdout
);
1164 info(register int verbose
)
1166 struct pcap_stat stat
;
1168 if (pcap_stats(pd
, &stat
) < 0) {
1169 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1174 fprintf(stderr
, "%s: ", program_name
);
1176 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1178 fputs(", ", stderr
);
1181 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1183 fputs(", ", stderr
);
1186 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1191 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1193 struct dump_info
*dump_info
;
1200 dump_info
= (struct dump_info
*)user
;
1203 * XXX - this won't force the file to rotate on the specified time
1204 * boundary, but it will rotate on the first packet received after the
1205 * specified Gflag number of seconds. Note: if a Gflag time boundary
1206 * and a Cflag size boundary coincide, the time rotation will occur
1207 * first thereby cancelling the Cflag boundary (since the file should
1211 /* Check if it is time to rotate */
1214 /* Get the current time */
1215 if (gettimeofday(&t
, NULL
) != 0) {
1216 error("dump_and_trunc_packet: gettimeofday");
1220 /* If the time is greater than the specified window, rotate */
1221 if (t
.tv_sec
- Gflag_time
>= Gflag
) {
1222 /* Update the Gflag_time */
1223 Gflag_time
= t
.tv_sec
;
1224 /* Update Gflag_count */
1227 * Close the current file and open a new one.
1229 pcap_dump_close(dump_info
->p
);
1231 /* Check to see if we've exceeded the Wflag (when not using Cflag). */
1232 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1233 (void)fprintf(stderr
, "Maximum file limit reached: %d\n", Wflag
);
1234 (void)fflush(stderr
);
1238 /* Allocate space for max filename + \0. */
1239 name
= (char *)malloc(NAME_MAX
+ 1);
1241 error("dump_packet_and_trunc: malloc");
1242 /* This is always the first file in the Cflag rotation: e.g. 0
1243 * We also don't need numbering if Cflag is not set.
1246 MakeFilename(name
, dump_info
->WFileName
, 0, WflagChars
);
1248 MakeFilename(name
, dump_info
->WFileName
, 0, 0);
1250 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1252 if (dump_info
->p
== NULL
)
1253 error("%s", pcap_geterr(pd
));
1259 * XXX - this won't prevent capture files from getting
1260 * larger than Cflag - the last packet written to the
1261 * file could put it over Cflag.
1263 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1265 * Close the current file and open a new one.
1267 pcap_dump_close(dump_info
->p
);
1270 if (Cflag_count
>= Wflag
)
1273 name
= (char *)malloc(NAME_MAX
+ 1);
1275 error("dump_packet_and_trunc: malloc");
1276 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1277 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1279 if (dump_info
->p
== NULL
)
1280 error("%s", pcap_geterr(pd
));
1283 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1284 #ifdef HAVE_PCAP_DUMP_FLUSH
1286 pcap_dump_flush(dump_info
->p
);
1295 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1301 pcap_dump(user
, h
, sp
);
1302 #ifdef HAVE_PCAP_DUMP_FLUSH
1304 pcap_dump_flush((pcap_dumper_t
*)user
);
1313 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1315 struct print_info
*print_info
;
1323 print_info
= (struct print_info
*)user
;
1326 * Some printers want to check that they're not walking off the
1327 * end of the packet.
1328 * Rather than pass it all the way down, we set this global.
1330 snapend
= sp
+ h
->caplen
;
1332 hdrlen
= (*print_info
->printer
)(h
, sp
);
1335 * Print the raw packet data in hex and ASCII.
1339 * Include the link-layer header.
1341 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1344 * Don't include the link-layer header - and if
1345 * we have nothing past the link-layer header,
1348 if (h
->caplen
> hdrlen
)
1349 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1350 h
->caplen
- hdrlen
);
1354 * Print the raw packet data in hex.
1358 * Include the link-layer header.
1360 hex_print("\n\t", sp
, h
->caplen
);
1363 * Don't include the link-layer header - and if
1364 * we have nothing past the link-layer header,
1367 if (h
->caplen
> hdrlen
)
1368 hex_print("\n\t", sp
+ hdrlen
,
1369 h
->caplen
- hdrlen
);
1373 * Print the raw packet data in ASCII.
1377 * Include the link-layer header.
1379 ascii_print(sp
, h
->caplen
);
1382 * Don't include the link-layer header - and if
1383 * we have nothing past the link-layer header,
1386 if (h
->caplen
> hdrlen
)
1387 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1400 * XXX - there should really be libpcap calls to get the version
1401 * number as a string (the string would be generated from #defines
1402 * at run time, so that it's not generated from string constants
1403 * in the library, as, on many UNIX systems, those constants would
1404 * be statically linked into the application executable image, and
1405 * would thus reflect the version of libpcap on the system on
1406 * which the application was *linked*, not the system on which it's
1409 * That routine should be documented, unlike the "version[]"
1410 * string, so that UNIX vendors providing their own libpcaps
1411 * don't omit it (as a couple of vendors have...).
1413 * Packet.dll should perhaps also export a routine to return the
1414 * version number of the Packet.dll code, to supply the
1415 * "Wpcap_version" information on Windows.
1417 char WDversion
[]="current-cvs.tcpdump.org";
1418 #if !defined(HAVE_GENERATED_VERSION)
1419 char version
[]="current-cvs.tcpdump.org";
1421 char pcap_version
[]="current-cvs.tcpdump.org";
1422 char Wpcap_version
[]="3.1";
1426 * By default, print the specified data out in hex and ASCII.
1429 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1431 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1435 default_print(const u_char
*bp
, u_int length
)
1437 ndo_default_print(gndo
, bp
, length
);
1441 RETSIGTYPE
requestinfo(int signo _U_
)
1451 * Called once each second in verbose mode while dumping to file
1453 #ifdef USE_WIN32_MM_TIMER
1454 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1455 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1457 struct pcap_stat stat
;
1459 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1460 fprintf(stderr
, "Got %u\r", packets_captured
);
1462 #elif defined(HAVE_ALARM)
1463 static void verbose_stats_dump(int sig _U_
)
1465 struct pcap_stat stat
;
1467 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1468 fprintf(stderr
, "Got %u\r", packets_captured
);
1476 extern char version
[];
1477 #ifndef HAVE_PCAP_LIB_VERSION
1478 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1479 extern char pcap_version
[];
1480 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1481 static char pcap_version
[] = "unknown";
1482 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1483 #endif /* HAVE_PCAP_LIB_VERSION */
1485 #ifdef HAVE_PCAP_LIB_VERSION
1487 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1489 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1491 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1492 #else /* HAVE_PCAP_LIB_VERSION */
1494 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1495 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1497 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1498 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1500 #endif /* HAVE_PCAP_LIB_VERSION */
1501 (void)fprintf(stderr
,
1502 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1503 (void)fprintf(stderr
,
1504 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1505 (void)fprintf(stderr
,
1506 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1507 (void)fprintf(stderr
,
1508 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1509 (void)fprintf(stderr
,
1510 "\t\t[ expression ]\n");
1518 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1522 (void)fprintf(stderr
, "%s: ", program_name
);
1524 (void)vfprintf(stderr
, fmt
, ap
);
1528 if (fmt
[-1] != '\n')
1529 (void)fputc('\n', stderr
);
1537 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1541 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1543 (void)vfprintf(stderr
, fmt
, ap
);
1547 if (fmt
[-1] != '\n')
1548 (void)fputc('\n', stderr
);