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.253.2.3 2005-05-12 07:23:04 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"
82 netdissect_options Gndo
;
83 netdissect_options
*gndo
= &Gndo
;
86 * Define the maximum number of files for the -C flag, and how many
87 * characters can be added to a filename for the -C flag (which
88 * should be enough to handle MAX_CFLAG - 1).
90 #define MAX_CFLAG 1000000
91 #define MAX_CFLAG_CHARS 6
93 int dflag
; /* print filter code */
94 int Lflag
; /* list available data link types and exit */
101 int32_t thiszone
; /* seconds offset from gmt to local time */
104 static RETSIGTYPE
cleanup(int);
105 static void usage(void) __attribute__((noreturn
));
106 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
108 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
109 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
110 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
111 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
112 static void droproot(const char *, const char *);
113 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
114 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
117 RETSIGTYPE
requestinfo(int);
120 #if defined(USE_WIN32_MM_TIMER)
121 #include <MMsystem.h>
122 static UINT timer_id
;
123 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
124 #elif defined(HAVE_ALARM)
125 static void verbose_stats_dump(int sig
);
128 static void info(int);
129 static u_int packets_captured
;
131 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
138 static struct printer printers
[] = {
139 { arcnet_if_print
, DLT_ARCNET
},
140 #ifdef DLT_ARCNET_LINUX
141 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
143 { ether_if_print
, DLT_EN10MB
},
144 { token_if_print
, DLT_IEEE802
},
146 { lane_if_print
, DLT_LANE8023
},
149 { cip_if_print
, DLT_CIP
},
152 { cip_if_print
, DLT_ATM_CLIP
},
154 { sl_if_print
, DLT_SLIP
},
155 #ifdef DLT_SLIP_BSDOS
156 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
158 { ppp_if_print
, DLT_PPP
},
159 #ifdef DLT_PPP_WITHDIRECTION
160 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
163 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
165 { fddi_if_print
, DLT_FDDI
},
166 { null_if_print
, DLT_NULL
},
168 { null_if_print
, DLT_LOOP
},
170 { raw_if_print
, DLT_RAW
},
171 { atm_if_print
, DLT_ATM_RFC1483
},
173 { chdlc_if_print
, DLT_C_HDLC
},
176 { chdlc_if_print
, DLT_HDLC
},
178 #ifdef DLT_PPP_SERIAL
179 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
182 { pppoe_if_print
, DLT_PPP_ETHER
},
185 { sll_if_print
, DLT_LINUX_SLL
},
187 #ifdef DLT_IEEE802_11
188 { ieee802_11_if_print
, DLT_IEEE802_11
},
191 { ltalk_if_print
, DLT_LTALK
},
194 { pflog_if_print
, DLT_PFLOG
},
197 { fr_if_print
, DLT_FR
},
200 { fr_if_print
, DLT_FRELAY
},
203 { sunatm_if_print
, DLT_SUNATM
},
205 #ifdef DLT_IP_OVER_FC
206 { ipfc_if_print
, DLT_IP_OVER_FC
},
208 #ifdef DLT_PRISM_HEADER
209 { prism_if_print
, DLT_PRISM_HEADER
},
211 #ifdef DLT_IEEE802_11_RADIO
212 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
215 { enc_if_print
, DLT_ENC
},
217 #ifdef DLT_SYMANTEC_FIREWALL
218 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
220 #ifdef DLT_APPLE_IP_OVER_IEEE1394
221 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
223 #ifdef DLT_JUNIPER_ATM1
224 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
226 #ifdef DLT_JUNIPER_ATM2
227 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
229 #ifdef DLT_JUNIPER_MFR
230 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
232 #ifdef DLT_JUNIPER_MLFR
233 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
235 #ifdef DLT_JUNIPER_MLPPP
236 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
238 #ifdef DLT_JUNIPER_PPPOE
239 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
241 #ifdef DLT_JUNIPER_PPPOE
242 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
244 #ifdef DLT_JUNIPER_PPPOE_ATM
245 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
251 lookup_printer(int type
)
255 for (p
= printers
; p
->f
; ++p
)
280 show_dlts_and_exit(pcap_t
*pd
)
284 const char *dlt_name
;
286 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
288 error("%s", pcap_geterr(pd
));
289 else if (n_dlts
== 0 || !dlts
)
290 error("No data link types.");
292 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
294 while (--n_dlts
>= 0) {
295 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
296 if (dlt_name
!= NULL
) {
297 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
298 pcap_datalink_val_to_description(dlts
[n_dlts
]));
301 * OK, does tcpdump handle that type?
303 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
304 (void) fprintf(stderr
, " (not supported)");
307 (void) fprintf(stderr
, " DLT %d (not supported)\n",
316 * Set up flags that might or might not be supported depending on the
317 * version of libpcap we're using.
321 #define B_FLAG_USAGE " [ -B size ]"
327 #ifdef HAVE_PCAP_FINDALLDEVS
328 #ifndef HAVE_PCAP_IF_T
329 #undef HAVE_PCAP_FINDALLDEVS
333 #ifdef HAVE_PCAP_FINDALLDEVS
339 #ifdef HAVE_PCAP_DUMP_FLUSH
346 /* Drop root privileges and chroot if necessary */
348 droproot(const char *username
, const char *chroot_dir
)
350 struct passwd
*pw
= NULL
;
352 if (chroot_dir
&& !username
) {
353 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
357 pw
= getpwnam(username
);
360 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
361 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
362 chroot_dir
, pcap_strerror(errno
));
366 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
367 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
368 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
370 (unsigned long)pw
->pw_uid
,
371 (unsigned long)pw
->pw_gid
,
372 pcap_strerror(errno
));
377 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
400 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
402 if (cnt
== 0 && max_chars
== 0)
403 strcpy(buffer
, orig_name
);
405 sprintf(buffer
, "%s%0*d", orig_name
, max_chars
, cnt
);
408 static int tcpdump_printf(netdissect_options
*ndo _U_
,
409 const char *fmt
, ...)
416 ret
=vfprintf(stdout
, fmt
, args
);
423 main(int argc
, char **argv
)
425 register int cnt
, op
, i
;
426 bpf_u_int32 localnet
, netmask
;
427 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
428 pcap_handler callback
;
430 struct bpf_program fcode
;
432 RETSIGTYPE (*oldhandler
)(int);
434 struct print_info printinfo
;
435 struct dump_info dumpinfo
;
436 u_char
*pcap_userdata
;
437 char ebuf
[PCAP_ERRBUF_SIZE
];
438 char *username
= NULL
;
439 char *chroot_dir
= NULL
;
440 #ifdef HAVE_PCAP_FINDALLDEVS
441 pcap_if_t
*devpointer
;
446 u_int UserBufferSize
= 1000000;
447 if(wsockinit() != 0) return 1;
453 gndo
->ndo_default_print
=ndo_default_print
;
454 gndo
->ndo_printf
=tcpdump_printf
;
455 gndo
->ndo_error
=ndo_error
;
456 gndo
->ndo_warning
=ndo_warning
;
457 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
464 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
465 program_name
= cp
+ 1;
467 program_name
= argv
[0];
469 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
478 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:i:lLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:YZ:")) != -1)
482 /* compatibility for old -a */
493 UserBufferSize
= atoi(optarg
)*1024;
494 if (UserBufferSize
< 0)
495 error("invalid packet buffer size %s", optarg
);
502 error("invalid packet count %s", optarg
);
506 Cflag
= atoi(optarg
) * 1000000;
508 error("invalid file size %s", optarg
);
515 #ifdef HAVE_PCAP_FINDALLDEVS
517 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
520 for (i
= 0; devpointer
!= 0; i
++) {
521 printf("%d.%s", i
+1, devpointer
->name
);
522 if (devpointer
->description
!= NULL
)
523 printf(" (%s)", devpointer
->description
);
525 devpointer
= devpointer
->next
;
529 #endif /* HAVE_PCAP_FINDALLDEVS */
540 #ifndef HAVE_LIBCRYPTO
541 warning("crypto code not compiled in");
543 gndo
->ndo_espsecret
= optarg
;
555 if (optarg
[0] == '0' && optarg
[1] == 0)
556 error("Invalid adapter index");
558 #ifdef HAVE_PCAP_FINDALLDEVS
560 * If the argument is a number, treat it as
561 * an index into the list of adapters, as
562 * printed by "tcpdump -D".
564 * This should be OK on UNIX systems, as interfaces
565 * shouldn't have names that begin with digits.
566 * It can be useful on Windows, where more than
567 * one interface can have the same name.
569 if ((devnum
= atoi(optarg
)) != 0) {
571 error("Invalid adapter index");
573 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
576 for (i
= 0; i
< devnum
-1; i
++){
577 devpointer
= devpointer
->next
;
578 if (devpointer
== NULL
)
579 error("Invalid adapter index");
582 device
= devpointer
->name
;
585 #endif /* HAVE_PCAP_FINDALLDEVS */
592 * _IOLBF is the same as _IOFBF in Microsoft's C
593 * libraries; the only alternative they offer
596 * XXX - this should really be checking for MSVC++,
597 * not WIN32, if, for example, MinGW has its own
598 * C library that is more UNIX-compatible.
600 setvbuf(stdout
, NULL
, _IONBF
, 0);
602 #ifdef HAVE_SETLINEBUF
605 setvbuf(stdout
, NULL
, _IOLBF
, 0);
620 if (smiLoadModule(optarg
) == 0) {
621 error("could not load MIB module %s", optarg
);
625 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
626 program_name
, optarg
);
627 (void)fprintf(stderr
, "(no libsmi support)\n");
632 /* TCP-MD5 shared secret */
633 #ifndef HAVE_LIBCRYPTO
634 warning("crypto code not compiled in");
636 tcpmd5secret
= optarg
;
662 snaplen
= strtol(optarg
, &end
, 0);
663 if (optarg
== end
|| *end
!= '\0'
664 || snaplen
< 0 || snaplen
> 65535)
665 error("invalid snaplen %s", optarg
);
666 else if (snaplen
== 0)
680 if (strcasecmp(optarg
, "vat") == 0)
682 else if (strcasecmp(optarg
, "wb") == 0)
684 else if (strcasecmp(optarg
, "rpc") == 0)
686 else if (strcasecmp(optarg
, "rtp") == 0)
688 else if (strcasecmp(optarg
, "rtcp") == 0)
689 packettype
= PT_RTCP
;
690 else if (strcasecmp(optarg
, "snmp") == 0)
691 packettype
= PT_SNMP
;
692 else if (strcasecmp(optarg
, "cnfp") == 0)
693 packettype
= PT_CNFP
;
694 else if (strcasecmp(optarg
, "tftp") == 0)
695 packettype
= PT_TFTP
;
696 else if (strcasecmp(optarg
, "aodv") == 0)
697 packettype
= PT_AODV
;
699 error("unknown packet type `%s'", optarg
);
706 #ifdef HAVE_PCAP_DUMP_FLUSH
721 Wflag
= atoi(optarg
);
723 error("invalid number of output files %s", optarg
);
724 WflagChars
= getWflagChars(Wflag
);
736 gndo
->ndo_dltname
= optarg
;
738 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
739 if (gndo
->ndo_dlt
< 0)
740 error("invalid data link type %s", gndo
->ndo_dltname
);
743 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
746 /* Undocumented flag */
747 #ifdef HAVE_PCAP_DEBUG
748 extern int pcap_debug
;
759 username
= strdup(optarg
);
774 case 0: /* Default */
775 case 4: /* Default + Date*/
776 thiszone
= gmt2local(0);
779 case 1: /* No time stamp */
780 case 2: /* Unix timeval style */
781 case 3: /* Microseconds since previous packet */
784 default: /* Not supported */
785 error("only -t, -tt, -ttt, and -tttt are supported");
790 /* if run as root, prepare for chrooting */
791 if (getuid() == 0 || geteuid() == 0) {
792 /* future extensibility for cmd-line arguments */
794 chroot_dir
= WITH_CHROOT
;
799 /* if run as root, prepare for dropping root privileges */
800 if (getuid() == 0 || geteuid() == 0) {
801 /* Run with '-Z root' to restore old behaviour */
803 username
= WITH_USER
;
807 if (RFileName
!= NULL
) {
809 const char *dlt_name
;
813 * We don't need network access, so relinquish any set-UID
814 * or set-GID privileges we have (if any).
816 * We do *not* want set-UID privileges when opening a
817 * trace file, as that might let the user read other
818 * people's trace files (especially if we're set-UID
821 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
822 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
824 pd
= pcap_open_offline(RFileName
, ebuf
);
827 dlt
= pcap_datalink(pd
);
828 dlt_name
= pcap_datalink_val_to_name(dlt
);
829 if (dlt_name
== NULL
) {
830 fprintf(stderr
, "reading from file %s, link-type %u\n",
834 "reading from file %s, link-type %s (%s)\n",
836 pcap_datalink_val_to_description(dlt
));
841 error("-f and -r options are incompatible");
843 if (device
== NULL
) {
844 device
= pcap_lookupdev(ebuf
);
849 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
850 { //a Unicode string has a \0 as second byte (so strlen() is 1)
851 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
855 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
861 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
867 * Let user own process after socket has been opened.
870 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
871 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
874 if(UserBufferSize
!= 1000000)
875 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
876 error("%s", pcap_geterr(pd
));
880 show_dlts_and_exit(pd
);
881 if (gndo
->ndo_dlt
>= 0) {
882 #ifdef HAVE_PCAP_SET_DATALINK
883 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
884 error("%s", pcap_geterr(pd
));
887 * We don't actually support changing the
888 * data link type, so we only let them
889 * set it to what it already is.
891 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
892 error("%s is not one of the DLTs supported by this device\n",
896 (void)fprintf(stderr
, "%s: data link type %s\n",
897 program_name
, gndo
->ndo_dltname
);
898 (void)fflush(stderr
);
900 i
= pcap_snapshot(pd
);
902 warning("snaplen raised from %d to %d", snaplen
, i
);
905 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
912 cmdbuf
= read_infile(infile
);
914 cmdbuf
= copy_argv(&argv
[optind
]);
916 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
917 error("%s", pcap_geterr(pd
));
919 bpf_dump(&fcode
, dflag
);
923 init_addrtoname(localnet
, netmask
);
926 (void)setsignal(SIGPIPE
, cleanup
);
928 (void)setsignal(SIGTERM
, cleanup
);
929 (void)setsignal(SIGINT
, cleanup
);
930 /* Cooperate with nohup(1) */
932 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
933 (void)setsignal(SIGHUP
, oldhandler
);
936 if (pcap_setfilter(pd
, &fcode
) < 0)
937 error("%s", pcap_geterr(pd
));
941 WFileNameAlt
= (char *)malloc(strlen(WFileName
) + MAX_CFLAG_CHARS
+ 1);
942 if (WFileNameAlt
== NULL
)
943 error("malloc of WFileNameAlt");
944 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
945 p
= pcap_dump_open(pd
, WFileNameAlt
);
947 error("%s", pcap_geterr(pd
));
949 callback
= dump_packet_and_trunc
;
950 dumpinfo
.WFileName
= WFileName
;
953 pcap_userdata
= (u_char
*)&dumpinfo
;
955 callback
= dump_packet
;
956 pcap_userdata
= (u_char
*)p
;
959 type
= pcap_datalink(pd
);
960 printinfo
.printer
= lookup_printer(type
);
961 if (printinfo
.printer
== NULL
) {
962 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
963 if (gndo
->ndo_dltname
!= NULL
)
964 error("unsupported data link type %s",
967 error("unsupported data link type %d", type
);
969 callback
= print_packet
;
970 pcap_userdata
= (u_char
*)&printinfo
;
974 * We cannot do this earlier, because we want to be able to open
975 * the file (if done) for writing before giving up permissions.
977 if (getuid() == 0 || geteuid() == 0) {
978 if (username
|| chroot_dir
)
979 droproot(username
, chroot_dir
);
983 (void)setsignal(SIGINFO
, requestinfo
);
986 if (vflag
> 0 && WFileName
) {
988 * When capturing to a file, "-v" means tcpdump should,
989 * every 10 secodns, "v"erbosely report the number of
992 #ifdef USE_WIN32_MM_TIMER
993 /* call verbose_stats_dump() each 1000 +/-100msec */
994 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
995 setvbuf(stderr
, NULL
, _IONBF
, 0);
996 #elif defined(HAVE_ALARM)
997 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1003 if (RFileName
== NULL
) {
1005 const char *dlt_name
;
1007 if (!vflag
&& !WFileName
) {
1008 (void)fprintf(stderr
,
1009 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1012 (void)fprintf(stderr
, "%s: ", program_name
);
1013 dlt
= pcap_datalink(pd
);
1014 dlt_name
= pcap_datalink_val_to_name(dlt
);
1015 if (dlt_name
== NULL
) {
1016 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1017 device
, dlt
, snaplen
);
1019 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1021 pcap_datalink_val_to_description(dlt
), snaplen
);
1023 (void)fflush(stderr
);
1026 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1027 if (WFileName
== NULL
) {
1029 * We're printing packets. Flush the printed output,
1030 * so it doesn't get intermingled with error output.
1034 * We got interrupted, so perhaps we didn't
1035 * manage to finish a line we were printing.
1036 * Print an extra newline, just in case.
1040 (void)fflush(stdout
);
1046 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1047 program_name
, pcap_geterr(pd
));
1049 if (RFileName
== NULL
) {
1051 * We're doing a live capture. Report the capture
1057 exit(status
== -1 ? 1 : 0);
1060 /* make a clean exit on interrupts */
1062 cleanup(int signo _U_
)
1064 #ifdef USE_WIN32_MM_TIMER
1066 timeKillEvent(timer_id
);
1068 #elif defined(HAVE_ALARM)
1072 #ifdef HAVE_PCAP_BREAKLOOP
1074 * We have "pcap_breakloop()"; use it, so that we do as little
1075 * as possible in the signal handler (it's probably not safe
1076 * to do anything with standard I/O streams in a signal handler -
1077 * the ANSI C standard doesn't say it is).
1082 * We don't have "pcap_breakloop()"; this isn't safe, but
1083 * it's the best we can do. Print the summary if we're
1084 * not reading from a savefile - i.e., if we're doing a
1085 * live capture - and exit.
1087 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1089 * We got interrupted, so perhaps we didn't
1090 * manage to finish a line we were printing.
1091 * Print an extra newline, just in case.
1094 (void)fflush(stdout
);
1102 info(register int verbose
)
1104 struct pcap_stat stat
;
1106 if (pcap_stats(pd
, &stat
) < 0) {
1107 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1112 fprintf(stderr
, "%s: ", program_name
);
1114 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1116 fputs(", ", stderr
);
1119 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1121 fputs(", ", stderr
);
1124 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1129 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1131 struct dump_info
*dump_info
;
1138 dump_info
= (struct dump_info
*)user
;
1141 * XXX - this won't prevent capture files from getting
1142 * larger than Cflag - the last packet written to the
1143 * file could put it over Cflag.
1145 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
1147 * Close the current file and open a new one.
1149 pcap_dump_close(dump_info
->p
);
1152 if (Cflag_count
>= Wflag
)
1155 if (Cflag_count
>= MAX_CFLAG
)
1156 error("too many output files");
1158 name
= (char *)malloc(strlen(dump_info
->WFileName
) + MAX_CFLAG_CHARS
+ 1);
1160 error("dump_packet_and_trunc: malloc");
1161 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1162 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1164 if (dump_info
->p
== NULL
)
1165 error("%s", pcap_geterr(pd
));
1168 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1169 #ifdef HAVE_PCAP_DUMP_FLUSH
1171 pcap_dump_flush(dump_info
->p
);
1180 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1186 pcap_dump(user
, h
, sp
);
1187 #ifdef HAVE_PCAP_DUMP_FLUSH
1189 pcap_dump_flush((pcap_dumper_t
*)user
);
1198 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1200 struct print_info
*print_info
;
1208 print_info
= (struct print_info
*)user
;
1211 * Some printers want to check that they're not walking off the
1212 * end of the packet.
1213 * Rather than pass it all the way down, we set this global.
1215 snapend
= sp
+ h
->caplen
;
1217 hdrlen
= (*print_info
->printer
)(h
, sp
);
1220 * Print the raw packet data.
1224 * Include the link-layer header.
1226 hex_print("\n\t", sp
, h
->caplen
);
1229 * Don't include the link-layer header - and if
1230 * we have nothing past the link-layer header,
1233 if (h
->caplen
> hdrlen
)
1234 hex_print("\n\t", sp
+ hdrlen
,
1235 h
->caplen
- hdrlen
);
1239 * Print the raw packet data.
1243 * Include the link-layer header.
1245 ascii_print("\n\t", sp
, h
->caplen
);
1248 * Don't include the link-layer header - and if
1249 * we have nothing past the link-layer header,
1252 if (h
->caplen
> hdrlen
)
1253 ascii_print("\n\t", sp
+ hdrlen
,
1254 h
->caplen
- hdrlen
);
1267 * XXX - there should really be libpcap calls to get the version
1268 * number as a string (the string would be generated from #defines
1269 * at run time, so that it's not generated from string constants
1270 * in the library, as, on many UNIX systems, those constants would
1271 * be statically linked into the application executable image, and
1272 * would thus reflect the version of libpcap on the system on
1273 * which the application was *linked*, not the system on which it's
1276 * That routine should be documented, unlike the "version[]"
1277 * string, so that UNIX vendors providing their own libpcaps
1278 * don't omit it (as a couple of vendors have...).
1280 * Packet.dll should perhaps also export a routine to return the
1281 * version number of the Packet.dll code, to supply the
1282 * "Wpcap_version" information on Windows.
1284 char WDversion
[]="current-cvs.tcpdump.org";
1285 #if !defined(HAVE_GENERATED_VERSION)
1286 char version
[]="current-cvs.tcpdump.org";
1288 char pcap_version
[]="current-cvs.tcpdump.org";
1289 char Wpcap_version
[]="3.1";
1293 * By default, print the specified data out in hex.
1296 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1298 ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1302 default_print(const u_char
*bp
, u_int length
)
1304 ndo_default_print(gndo
, bp
, length
);
1308 RETSIGTYPE
requestinfo(int signo _U_
)
1318 * Called once each second in verbose mode while dumping to file
1320 #ifdef USE_WIN32_MM_TIMER
1321 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1322 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1324 struct pcap_stat stat
;
1326 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1327 fprintf(stderr
, "Got %u\r", packets_captured
);
1329 #elif defined(HAVE_ALARM)
1330 static void verbose_stats_dump(int sig _U_
)
1332 struct pcap_stat stat
;
1334 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1335 fprintf(stderr
, "Got %u\r", packets_captured
);
1343 extern char version
[];
1344 #ifndef HAVE_PCAP_LIB_VERSION
1345 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1346 extern char pcap_version
[];
1347 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1348 static char pcap_version
[] = "unknown";
1349 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1350 #endif /* HAVE_PCAP_LIB_VERSION */
1352 #ifdef HAVE_PCAP_LIB_VERSION
1354 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1356 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1358 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1359 #else /* HAVE_PCAP_LIB_VERSION */
1361 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1362 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1364 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1365 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1367 #endif /* HAVE_PCAP_LIB_VERSION */
1368 (void)fprintf(stderr
,
1369 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1370 (void)fprintf(stderr
,
1371 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1372 (void)fprintf(stderr
,
1373 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1374 (void)fprintf(stderr
,
1375 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1376 (void)fprintf(stderr
,
1377 "\t\t[ expression ]\n");
1385 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1389 (void)fprintf(stderr
, "%s: ", program_name
);
1391 (void)vfprintf(stderr
, fmt
, ap
);
1395 if (fmt
[-1] != '\n')
1396 (void)fputc('\n', stderr
);
1404 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1408 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1410 (void)vfprintf(stderr
, fmt
, ap
);
1414 if (fmt
[-1] != '\n')
1415 (void)fputc('\n', stderr
);