]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
2a4e17c8f0e24096af2c9ee03e955e34be202e1d
[tcpdump] / tcpdump.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
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.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
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.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #ifdef WIN32
51 #include "getopt.h"
52 #include "w32_fzs.h"
53 extern int strcasecmp (const char *__s1, const char *__s2);
54 extern int SIZE_BUF;
55 #define off_t long
56 #define uint UINT
57 #endif /* WIN32 */
58
59 #ifdef HAVE_SMI_H
60 #include <smi.h>
61 #endif
62
63 #include <pcap.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <limits.h>
69 #ifndef WIN32
70 #include <sys/wait.h>
71 #include <sys/resource.h>
72 #include <pwd.h>
73 #include <grp.h>
74 #include <errno.h>
75 #endif /* WIN32 */
76
77 /* capabilities convinience library */
78 #ifdef HAVE_CAP_NG_H
79 #include <cap-ng.h>
80 #endif /* HAVE_CAP_NG_H */
81
82 #include "netdissect.h"
83 #include "interface.h"
84 #include "addrtoname.h"
85 #include "machdep.h"
86 #include "setsignal.h"
87 #include "gmt2local.h"
88 #include "pcap-missing.h"
89
90 #ifndef PATH_MAX
91 #define PATH_MAX 1024
92 #endif
93
94 #ifdef SIGINFO
95 #define SIGNAL_REQ_INFO SIGINFO
96 #elif SIGUSR1
97 #define SIGNAL_REQ_INFO SIGUSR1
98 #endif
99
100 netdissect_options Gndo;
101 netdissect_options *gndo = &Gndo;
102
103 static int dflag; /* print filter code */
104 static int Lflag; /* list available data link types and exit */
105 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
106 static int Jflag; /* list available time stamp types */
107 #endif
108 #ifdef HAVE_PCAP_SETDIRECTION
109 int Pflag = -1; /* Restrict captured packet by sent/receive direction */
110 #endif
111 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
112
113 static int infodelay;
114 static int infoprint;
115
116 char *program_name;
117
118 int32_t thiszone; /* seconds offset from gmt to local time */
119
120 /* Forwards */
121 static RETSIGTYPE cleanup(int);
122 static RETSIGTYPE child_cleanup(int);
123 static void usage(void) __attribute__((noreturn));
124 static void show_dlts_and_exit(const char *device, pcap_t *pd) __attribute__((noreturn));
125
126 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
127 static void ndo_default_print(netdissect_options *, const u_char *, u_int);
128 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
129 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
130 static void droproot(const char *, const char *);
131 static void ndo_error(netdissect_options *ndo, const char *fmt, ...)
132 __attribute__ ((noreturn, format (printf, 2, 3)));
133 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...);
134
135 #ifdef SIGNAL_REQ_INFO
136 RETSIGTYPE requestinfo(int);
137 #endif
138
139 #if defined(USE_WIN32_MM_TIMER)
140 #include <MMsystem.h>
141 static UINT timer_id;
142 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
143 #elif defined(HAVE_ALARM)
144 static void verbose_stats_dump(int sig);
145 #endif
146
147 static void info(int);
148 static u_int packets_captured;
149
150 struct printer {
151 if_printer f;
152 int type;
153 };
154
155
156 struct ndo_printer {
157 if_ndo_printer f;
158 int type;
159 };
160
161
162 static struct printer printers[] = {
163 { arcnet_if_print, DLT_ARCNET },
164 #ifdef DLT_ARCNET_LINUX
165 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
166 #endif
167 { token_if_print, DLT_IEEE802 },
168 #ifdef DLT_LANE8023
169 { lane_if_print, DLT_LANE8023 },
170 #endif
171 #ifdef DLT_CIP
172 { cip_if_print, DLT_CIP },
173 #endif
174 #ifdef DLT_ATM_CLIP
175 { cip_if_print, DLT_ATM_CLIP },
176 #endif
177 { sl_if_print, DLT_SLIP },
178 #ifdef DLT_SLIP_BSDOS
179 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
180 #endif
181 { ppp_if_print, DLT_PPP },
182 #ifdef DLT_PPP_WITHDIRECTION
183 { ppp_if_print, DLT_PPP_WITHDIRECTION },
184 #endif
185 #ifdef DLT_PPP_BSDOS
186 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
187 #endif
188 { fddi_if_print, DLT_FDDI },
189 { null_if_print, DLT_NULL },
190 #ifdef DLT_LOOP
191 { null_if_print, DLT_LOOP },
192 #endif
193 { raw_if_print, DLT_RAW },
194 { atm_if_print, DLT_ATM_RFC1483 },
195 #ifdef DLT_C_HDLC
196 { chdlc_if_print, DLT_C_HDLC },
197 #endif
198 #ifdef DLT_HDLC
199 { chdlc_if_print, DLT_HDLC },
200 #endif
201 #ifdef DLT_PPP_SERIAL
202 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
203 #endif
204 #ifdef DLT_PPP_ETHER
205 { pppoe_if_print, DLT_PPP_ETHER },
206 #endif
207 #ifdef DLT_LINUX_SLL
208 { sll_if_print, DLT_LINUX_SLL },
209 #endif
210 #ifdef DLT_IEEE802_11
211 { ieee802_11_if_print, DLT_IEEE802_11},
212 #endif
213 #ifdef DLT_LTALK
214 { ltalk_if_print, DLT_LTALK },
215 #endif
216 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
217 { pflog_if_print, DLT_PFLOG },
218 #endif
219 #ifdef DLT_FR
220 { fr_if_print, DLT_FR },
221 #endif
222 #ifdef DLT_FRELAY
223 { fr_if_print, DLT_FRELAY },
224 #endif
225 #ifdef DLT_SUNATM
226 { sunatm_if_print, DLT_SUNATM },
227 #endif
228 #ifdef DLT_IP_OVER_FC
229 { ipfc_if_print, DLT_IP_OVER_FC },
230 #endif
231 #ifdef DLT_PRISM_HEADER
232 { prism_if_print, DLT_PRISM_HEADER },
233 #endif
234 #ifdef DLT_IEEE802_11_RADIO
235 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
236 #endif
237 #ifdef DLT_ENC
238 { enc_if_print, DLT_ENC },
239 #endif
240 #ifdef DLT_SYMANTEC_FIREWALL
241 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
242 #endif
243 #ifdef DLT_APPLE_IP_OVER_IEEE1394
244 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
245 #endif
246 #ifdef DLT_IEEE802_11_RADIO_AVS
247 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
248 #endif
249 #ifdef DLT_JUNIPER_ATM1
250 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
251 #endif
252 #ifdef DLT_JUNIPER_ATM2
253 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
254 #endif
255 #ifdef DLT_JUNIPER_MFR
256 { juniper_mfr_print, DLT_JUNIPER_MFR },
257 #endif
258 #ifdef DLT_JUNIPER_MLFR
259 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
260 #endif
261 #ifdef DLT_JUNIPER_MLPPP
262 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
263 #endif
264 #ifdef DLT_JUNIPER_PPPOE
265 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
266 #endif
267 #ifdef DLT_JUNIPER_PPPOE_ATM
268 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
269 #endif
270 #ifdef DLT_JUNIPER_GGSN
271 { juniper_ggsn_print, DLT_JUNIPER_GGSN },
272 #endif
273 #ifdef DLT_JUNIPER_ES
274 { juniper_es_print, DLT_JUNIPER_ES },
275 #endif
276 #ifdef DLT_JUNIPER_MONITOR
277 { juniper_monitor_print, DLT_JUNIPER_MONITOR },
278 #endif
279 #ifdef DLT_JUNIPER_SERVICES
280 { juniper_services_print, DLT_JUNIPER_SERVICES },
281 #endif
282 #ifdef DLT_JUNIPER_ETHER
283 { juniper_ether_print, DLT_JUNIPER_ETHER },
284 #endif
285 #ifdef DLT_JUNIPER_PPP
286 { juniper_ppp_print, DLT_JUNIPER_PPP },
287 #endif
288 #ifdef DLT_JUNIPER_FRELAY
289 { juniper_frelay_print, DLT_JUNIPER_FRELAY },
290 #endif
291 #ifdef DLT_JUNIPER_CHDLC
292 { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
293 #endif
294 #ifdef DLT_MFR
295 { mfr_if_print, DLT_MFR },
296 #endif
297 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
298 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
299 #endif
300 #ifdef HAVE_PCAP_USB_H
301 #ifdef DLT_USB_LINUX
302 { usb_linux_48_byte_print, DLT_USB_LINUX},
303 #endif /* DLT_USB_LINUX */
304 #ifdef DLT_USB_LINUX_MMAPPED
305 { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED},
306 #endif /* DLT_USB_LINUX_MMAPPED */
307 #endif /* HAVE_PCAP_USB_H */
308 #ifdef DLT_IPV4
309 { raw_if_print, DLT_IPV4 },
310 #endif
311 #ifdef DLT_IPV6
312 { raw_if_print, DLT_IPV6 },
313 #endif
314 { NULL, 0 },
315 };
316
317 static struct ndo_printer ndo_printers[] = {
318 { ether_if_print, DLT_EN10MB },
319 #ifdef DLT_IPNET
320 { ipnet_if_print, DLT_IPNET },
321 #endif
322 #ifdef DLT_IEEE802_15_4
323 { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
324 #endif
325 #ifdef DLT_IEEE802_15_4_NOFCS
326 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
327 #endif
328 #ifdef DLT_PPI
329 { ppi_if_print, DLT_PPI },
330 #endif
331 #ifdef DLT_NETANALYZER
332 { netanalyzer_if_print, DLT_NETANALYZER },
333 #endif
334 #ifdef DLT_NETANALYZER_TRANSPARENT
335 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
336 #endif
337 #ifdef DLT_NFLOG
338 { nflog_if_print, DLT_NFLOG},
339 #endif
340 { NULL, 0 },
341 };
342
343 if_printer
344 lookup_printer(int type)
345 {
346 struct printer *p;
347
348 for (p = printers; p->f; ++p)
349 if (type == p->type)
350 return p->f;
351
352 return NULL;
353 /* NOTREACHED */
354 }
355
356 if_ndo_printer
357 lookup_ndo_printer(int type)
358 {
359 struct ndo_printer *p;
360
361 for (p = ndo_printers; p->f; ++p)
362 if (type == p->type)
363 return p->f;
364
365 return NULL;
366 /* NOTREACHED */
367 }
368
369 static pcap_t *pd;
370
371 static int supports_monitor_mode;
372
373 extern int optind;
374 extern int opterr;
375 extern char *optarg;
376
377 struct print_info {
378 netdissect_options *ndo;
379 union {
380 if_printer printer;
381 if_ndo_printer ndo_printer;
382 } p;
383 int ndo_type;
384 };
385
386 struct dump_info {
387 char *WFileName;
388 char *CurrentFileName;
389 pcap_t *pd;
390 pcap_dumper_t *p;
391 };
392
393 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
394 static void
395 show_tstamp_types_and_exit(const char *device, pcap_t *pd)
396 {
397 int n_tstamp_types;
398 int *tstamp_types = 0;
399 const char *tstamp_type_name;
400 int i;
401
402 n_tstamp_types = pcap_list_tstamp_types(pd, &tstamp_types);
403 if (n_tstamp_types < 0)
404 error("%s", pcap_geterr(pd));
405
406 if (n_tstamp_types == 0) {
407 fprintf(stderr, "Time stamp type cannot be set for %s\n",
408 device);
409 exit(0);
410 }
411 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
412 device);
413 for (i = 0; i < n_tstamp_types; i++) {
414 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
415 if (tstamp_type_name != NULL) {
416 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
417 pcap_tstamp_type_val_to_description(tstamp_types[i]));
418 } else {
419 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
420 }
421 }
422 pcap_free_tstamp_types(tstamp_types);
423 exit(0);
424 }
425 #endif
426
427 static void
428 show_dlts_and_exit(const char *device, pcap_t *pd)
429 {
430 int n_dlts;
431 int *dlts = 0;
432 const char *dlt_name;
433
434 n_dlts = pcap_list_datalinks(pd, &dlts);
435 if (n_dlts < 0)
436 error("%s", pcap_geterr(pd));
437 else if (n_dlts == 0 || !dlts)
438 error("No data link types.");
439
440 /*
441 * If the interface is known to support monitor mode, indicate
442 * whether these are the data link types available when not in
443 * monitor mode, if -I wasn't specified, or when in monitor mode,
444 * when -I was specified (the link-layer types available in
445 * monitor mode might be different from the ones available when
446 * not in monitor mode).
447 */
448 if (supports_monitor_mode)
449 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
450 device,
451 Iflag ? "when in monitor mode" : "when not in monitor mode");
452 else
453 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
454 device);
455
456 while (--n_dlts >= 0) {
457 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
458 if (dlt_name != NULL) {
459 (void) fprintf(stderr, " %s (%s)", dlt_name,
460 pcap_datalink_val_to_description(dlts[n_dlts]));
461
462 /*
463 * OK, does tcpdump handle that type?
464 */
465 if (lookup_printer(dlts[n_dlts]) == NULL
466 && lookup_ndo_printer(dlts[n_dlts]) == NULL)
467 (void) fprintf(stderr, " (printing not supported)");
468 fprintf(stderr, "\n");
469 } else {
470 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
471 dlts[n_dlts]);
472 }
473 }
474 #ifdef HAVE_PCAP_FREE_DATALINKS
475 pcap_free_datalinks(dlts);
476 #endif
477 exit(0);
478 }
479
480 /*
481 * Set up flags that might or might not be supported depending on the
482 * version of libpcap we're using.
483 */
484 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
485 #define B_FLAG "B:"
486 #define B_FLAG_USAGE " [ -B size ]"
487 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
488 #define B_FLAG
489 #define B_FLAG_USAGE
490 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
491
492 #ifdef HAVE_PCAP_CREATE
493 #define I_FLAG "I"
494 #else /* HAVE_PCAP_CREATE */
495 #define I_FLAG
496 #endif /* HAVE_PCAP_CREATE */
497
498 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
499 #define j_FLAG "j:"
500 #define j_FLAG_USAGE " [ -j tstamptype ]"
501 #define J_FLAG "J"
502 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
503 #define j_FLAG
504 #define j_FLAG_USAGE
505 #define J_FLAG
506 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
507
508 #ifdef HAVE_PCAP_FINDALLDEVS
509 #ifndef HAVE_PCAP_IF_T
510 #undef HAVE_PCAP_FINDALLDEVS
511 #endif
512 #endif
513
514 #ifdef HAVE_PCAP_FINDALLDEVS
515 #define D_FLAG "D"
516 #else
517 #define D_FLAG
518 #endif
519
520 #ifdef HAVE_PCAP_DUMP_FLUSH
521 #define U_FLAG "U"
522 #else
523 #define U_FLAG
524 #endif
525
526 #ifdef HAVE_PCAP_SETDIRECTION
527 #define P_FLAG "P:"
528 #else
529 #define P_FLAG
530 #endif
531
532 #ifndef WIN32
533 /* Drop root privileges and chroot if necessary */
534 static void
535 droproot(const char *username, const char *chroot_dir)
536 {
537 struct passwd *pw = NULL;
538
539 if (chroot_dir && !username) {
540 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
541 exit(1);
542 }
543
544 pw = getpwnam(username);
545 if (pw) {
546 if (chroot_dir) {
547 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
548 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
549 chroot_dir, pcap_strerror(errno));
550 exit(1);
551 }
552 }
553 #ifdef HAVE_CAP_NG_H
554 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
555 if (ret < 0) {
556 printf("error : ret %d\n", ret);
557 }
558 /* We don't need CAP_SETUID and CAP_SETGID */
559 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETUID);
560 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETUID);
561 capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_SETUID);
562 capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_SETUID);
563 capng_apply(CAPNG_SELECT_BOTH);
564
565 #else
566 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
567 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
568 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
569 username,
570 (unsigned long)pw->pw_uid,
571 (unsigned long)pw->pw_gid,
572 pcap_strerror(errno));
573 exit(1);
574 }
575 #endif /* HAVE_CAP_NG_H */
576 }
577 else {
578 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
579 username);
580 exit(1);
581 }
582 }
583 #endif /* WIN32 */
584
585 static int
586 getWflagChars(int x)
587 {
588 int c = 0;
589
590 x -= 1;
591 while (x > 0) {
592 c += 1;
593 x /= 10;
594 }
595
596 return c;
597 }
598
599
600 static void
601 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
602 {
603 char *filename = malloc(PATH_MAX + 1);
604 if (filename == NULL)
605 error("Makefilename: malloc");
606
607 /* Process with strftime if Gflag is set. */
608 if (Gflag != 0) {
609 struct tm *local_tm;
610
611 /* Convert Gflag_time to a usable format */
612 if ((local_tm = localtime(&Gflag_time)) == NULL) {
613 error("MakeTimedFilename: localtime");
614 }
615
616 /* There's no good way to detect an error in strftime since a return
617 * value of 0 isn't necessarily failure.
618 */
619 strftime(filename, PATH_MAX, orig_name, local_tm);
620 } else {
621 strncpy(filename, orig_name, PATH_MAX);
622 }
623
624 if (cnt == 0 && max_chars == 0)
625 strncpy(buffer, filename, PATH_MAX + 1);
626 else
627 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
628 /* Report an error if the filename is too large */
629 error("too many output files or filename is too long (> %d)", PATH_MAX);
630 free(filename);
631 }
632
633 static int tcpdump_printf(netdissect_options *ndo _U_,
634 const char *fmt, ...)
635 {
636
637 va_list args;
638 int ret;
639
640 va_start(args, fmt);
641 ret=vfprintf(stdout, fmt, args);
642 va_end(args);
643
644 return ret;
645 }
646
647 static struct print_info
648 get_print_info(int type)
649 {
650 struct print_info printinfo;
651
652 printinfo.ndo_type = 1;
653 printinfo.ndo = gndo;
654 printinfo.p.ndo_printer = lookup_ndo_printer(type);
655 if (printinfo.p.ndo_printer == NULL) {
656 printinfo.p.printer = lookup_printer(type);
657 printinfo.ndo_type = 0;
658 if (printinfo.p.printer == NULL) {
659 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
660 if (gndo->ndo_dltname != NULL)
661 error("packet printing is not supported for link type %s: use -w",
662 gndo->ndo_dltname);
663 else
664 error("packet printing is not supported for link type %d: use -w", type);
665 }
666 }
667 return (printinfo);
668 }
669
670 static char *
671 get_next_file(FILE *VFile, char *ptr)
672 {
673 char *ret;
674
675 ret = fgets(ptr, PATH_MAX, VFile);
676 if (!ret)
677 return NULL;
678
679 if (ptr[strlen(ptr) - 1] == '\n')
680 ptr[strlen(ptr) - 1] = '\0';
681
682 return ret;
683 }
684
685 int
686 main(int argc, char **argv)
687 {
688 register int cnt, op, i;
689 bpf_u_int32 localnet, netmask;
690 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
691 pcap_handler callback;
692 int type;
693 int dlt;
694 int new_dlt;
695 const char *dlt_name;
696 struct bpf_program fcode;
697 #ifndef WIN32
698 RETSIGTYPE (*oldhandler)(int);
699 #endif
700 struct print_info printinfo;
701 struct dump_info dumpinfo;
702 u_char *pcap_userdata;
703 char ebuf[PCAP_ERRBUF_SIZE];
704 char VFileLine[PATH_MAX + 1];
705 char *username = NULL;
706 char *chroot_dir = NULL;
707 char *ret = NULL;
708 char *end;
709 #ifdef HAVE_PCAP_FINDALLDEVS
710 pcap_if_t *devpointer;
711 int devnum;
712 #endif
713 int status;
714 FILE *VFile;
715 #ifdef WIN32
716 if(wsockinit() != 0) return 1;
717 #endif /* WIN32 */
718
719 jflag=-1; /* not set */
720 gndo->ndo_Oflag=1;
721 gndo->ndo_Rflag=1;
722 gndo->ndo_dlt=-1;
723 gndo->ndo_default_print=ndo_default_print;
724 gndo->ndo_printf=tcpdump_printf;
725 gndo->ndo_error=ndo_error;
726 gndo->ndo_warning=ndo_warning;
727 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
728
729 cnt = -1;
730 device = NULL;
731 infile = NULL;
732 RFileName = NULL;
733 VFileName = NULL;
734 VFile = NULL;
735 WFileName = NULL;
736 dlt = -1;
737 if ((cp = strrchr(argv[0], '/')) != NULL)
738 program_name = cp + 1;
739 else
740 program_name = argv[0];
741
742 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
743 error("%s", ebuf);
744
745 #ifdef LIBSMI
746 smiInit("tcpdump");
747 #endif
748
749 while (
750 (op = getopt(argc, argv, "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOp" P_FLAG "qr:Rs:StT:u" U_FLAG "V:vw:W:xXy:Yz:Z:")) != -1)
751 switch (op) {
752
753 case 'a':
754 /* compatibility for old -a */
755 break;
756
757 case 'A':
758 ++Aflag;
759 break;
760
761 case 'b':
762 ++bflag;
763 break;
764
765 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
766 case 'B':
767 Bflag = atoi(optarg)*1024;
768 if (Bflag <= 0)
769 error("invalid packet buffer size %s", optarg);
770 break;
771 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
772
773 case 'c':
774 cnt = atoi(optarg);
775 if (cnt <= 0)
776 error("invalid packet count %s", optarg);
777 break;
778
779 case 'C':
780 Cflag = atoi(optarg) * 1000000;
781 if (Cflag < 0)
782 error("invalid file size %s", optarg);
783 break;
784
785 case 'd':
786 ++dflag;
787 break;
788
789 #ifdef HAVE_PCAP_FINDALLDEVS
790 case 'D':
791 if (pcap_findalldevs(&devpointer, ebuf) < 0)
792 error("%s", ebuf);
793 else {
794 for (i = 0; devpointer != 0; i++) {
795 printf("%d.%s", i+1, devpointer->name);
796 if (devpointer->description != NULL)
797 printf(" (%s)", devpointer->description);
798 printf("\n");
799 devpointer = devpointer->next;
800 }
801 }
802 return 0;
803 #endif /* HAVE_PCAP_FINDALLDEVS */
804
805 case 'L':
806 Lflag++;
807 break;
808
809 case 'e':
810 ++eflag;
811 break;
812
813 case 'E':
814 #ifndef HAVE_LIBCRYPTO
815 warning("crypto code not compiled in");
816 #endif
817 gndo->ndo_espsecret = optarg;
818 break;
819
820 case 'f':
821 ++fflag;
822 break;
823
824 case 'F':
825 infile = optarg;
826 break;
827
828 case 'G':
829 Gflag = atoi(optarg);
830 if (Gflag < 0)
831 error("invalid number of seconds %s", optarg);
832
833 /* We will create one file initially. */
834 Gflag_count = 0;
835
836 /* Grab the current time for rotation use. */
837 if ((Gflag_time = time(NULL)) == (time_t)-1) {
838 error("main: can't get current time: %s",
839 pcap_strerror(errno));
840 }
841 break;
842
843 case 'h':
844 usage();
845 break;
846
847 case 'H':
848 ++Hflag;
849 break;
850
851 case 'i':
852 if (optarg[0] == '0' && optarg[1] == 0)
853 error("Invalid adapter index");
854
855 #ifdef HAVE_PCAP_FINDALLDEVS
856 /*
857 * If the argument is a number, treat it as
858 * an index into the list of adapters, as
859 * printed by "tcpdump -D".
860 *
861 * This should be OK on UNIX systems, as interfaces
862 * shouldn't have names that begin with digits.
863 * It can be useful on Windows, where more than
864 * one interface can have the same name.
865 */
866 devnum = strtol(optarg, &end, 10);
867 if (optarg != end && *end == '\0') {
868 if (devnum < 0)
869 error("Invalid adapter index");
870
871 if (pcap_findalldevs(&devpointer, ebuf) < 0)
872 error("%s", ebuf);
873 else {
874 /*
875 * Look for the devnum-th entry
876 * in the list of devices
877 * (1-based).
878 */
879 for (i = 0;
880 i < devnum-1 && devpointer != NULL;
881 i++, devpointer = devpointer->next)
882 ;
883 if (devpointer == NULL)
884 error("Invalid adapter index");
885 }
886 device = devpointer->name;
887 break;
888 }
889 #endif /* HAVE_PCAP_FINDALLDEVS */
890 device = optarg;
891 break;
892
893 #ifdef HAVE_PCAP_CREATE
894 case 'I':
895 ++Iflag;
896 break;
897 #endif /* HAVE_PCAP_CREATE */
898
899 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
900 case 'j':
901 jflag = pcap_tstamp_type_name_to_val(optarg);
902 if (jflag < 0)
903 error("invalid time stamp type %s", optarg);
904 break;
905
906 case 'J':
907 Jflag++;
908 break;
909 #endif
910
911 case 'l':
912 #ifdef WIN32
913 /*
914 * _IOLBF is the same as _IOFBF in Microsoft's C
915 * libraries; the only alternative they offer
916 * is _IONBF.
917 *
918 * XXX - this should really be checking for MSVC++,
919 * not WIN32, if, for example, MinGW has its own
920 * C library that is more UNIX-compatible.
921 */
922 setvbuf(stdout, NULL, _IONBF, 0);
923 #else /* WIN32 */
924 #ifdef HAVE_SETLINEBUF
925 setlinebuf(stdout);
926 #else
927 setvbuf(stdout, NULL, _IOLBF, 0);
928 #endif
929 #endif /* WIN32 */
930 break;
931
932 case 'K':
933 ++Kflag;
934 break;
935
936 case 'm':
937 #ifdef LIBSMI
938 if (smiLoadModule(optarg) == 0) {
939 error("could not load MIB module %s", optarg);
940 }
941 sflag = 1;
942 #else
943 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
944 program_name, optarg);
945 (void)fprintf(stderr, "(no libsmi support)\n");
946 #endif
947 break;
948
949 case 'M':
950 /* TCP-MD5 shared secret */
951 #ifndef HAVE_LIBCRYPTO
952 warning("crypto code not compiled in");
953 #endif
954 sigsecret = optarg;
955 break;
956
957 case 'n':
958 ++nflag;
959 break;
960
961 case 'N':
962 ++Nflag;
963 break;
964
965 case 'O':
966 Oflag = 0;
967 break;
968
969 case 'p':
970 ++pflag;
971 break;
972 #ifdef HAVE_PCAP_SETDIRECTION
973 case 'P':
974 if (strcasecmp(optarg, "in") == 0)
975 Pflag = PCAP_D_IN;
976 else if (strcasecmp(optarg, "out") == 0)
977 Pflag = PCAP_D_OUT;
978 else if (strcasecmp(optarg, "inout") == 0)
979 Pflag = PCAP_D_INOUT;
980 else
981 error("unknown capture direction `%s'", optarg);
982 break;
983 #endif /* HAVE_PCAP_SETDIRECTION */
984 case 'q':
985 ++qflag;
986 ++suppress_default_print;
987 break;
988
989 case 'r':
990 RFileName = optarg;
991 break;
992
993 case 'R':
994 Rflag = 0;
995 break;
996
997 case 's':
998 snaplen = strtol(optarg, &end, 0);
999 if (optarg == end || *end != '\0'
1000 || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
1001 error("invalid snaplen %s", optarg);
1002 else if (snaplen == 0)
1003 snaplen = MAXIMUM_SNAPLEN;
1004 break;
1005
1006 case 'S':
1007 ++Sflag;
1008 break;
1009
1010 case 't':
1011 ++tflag;
1012 break;
1013
1014 case 'T':
1015 if (strcasecmp(optarg, "vat") == 0)
1016 packettype = PT_VAT;
1017 else if (strcasecmp(optarg, "wb") == 0)
1018 packettype = PT_WB;
1019 else if (strcasecmp(optarg, "rpc") == 0)
1020 packettype = PT_RPC;
1021 else if (strcasecmp(optarg, "rtp") == 0)
1022 packettype = PT_RTP;
1023 else if (strcasecmp(optarg, "rtcp") == 0)
1024 packettype = PT_RTCP;
1025 else if (strcasecmp(optarg, "snmp") == 0)
1026 packettype = PT_SNMP;
1027 else if (strcasecmp(optarg, "cnfp") == 0)
1028 packettype = PT_CNFP;
1029 else if (strcasecmp(optarg, "tftp") == 0)
1030 packettype = PT_TFTP;
1031 else if (strcasecmp(optarg, "aodv") == 0)
1032 packettype = PT_AODV;
1033 else if (strcasecmp(optarg, "carp") == 0)
1034 packettype = PT_CARP;
1035 else if (strcasecmp(optarg, "radius") == 0)
1036 packettype = PT_RADIUS;
1037 else if (strcasecmp(optarg, "zmtp1") == 0)
1038 packettype = PT_ZMTP1;
1039 else if (strcasecmp(optarg, "vxlan") == 0)
1040 packettype = PT_VXLAN;
1041 else if (strcasecmp(optarg, "pgm") == 0)
1042 packettype = PT_PGM;
1043 else if (strcasecmp(optarg, "pgm_zmtp1") == 0)
1044 packettype = PT_PGM_ZMTP1;
1045 else if (strcasecmp(optarg, "lmp") == 0)
1046 packettype = PT_LMP;
1047 else
1048 error("unknown packet type `%s'", optarg);
1049 break;
1050
1051 case 'u':
1052 ++uflag;
1053 break;
1054
1055 #ifdef HAVE_PCAP_DUMP_FLUSH
1056 case 'U':
1057 ++Uflag;
1058 break;
1059 #endif
1060
1061 case 'v':
1062 ++vflag;
1063 break;
1064
1065 case 'V':
1066 VFileName = optarg;
1067 break;
1068
1069 case 'w':
1070 WFileName = optarg;
1071 break;
1072
1073 case 'W':
1074 Wflag = atoi(optarg);
1075 if (Wflag < 0)
1076 error("invalid number of output files %s", optarg);
1077 WflagChars = getWflagChars(Wflag);
1078 break;
1079
1080 case 'x':
1081 ++xflag;
1082 ++suppress_default_print;
1083 break;
1084
1085 case 'X':
1086 ++Xflag;
1087 ++suppress_default_print;
1088 break;
1089
1090 case 'y':
1091 gndo->ndo_dltname = optarg;
1092 gndo->ndo_dlt =
1093 pcap_datalink_name_to_val(gndo->ndo_dltname);
1094 if (gndo->ndo_dlt < 0)
1095 error("invalid data link type %s", gndo->ndo_dltname);
1096 break;
1097
1098 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1099 case 'Y':
1100 {
1101 /* Undocumented flag */
1102 #ifdef HAVE_PCAP_DEBUG
1103 extern int pcap_debug;
1104 pcap_debug = 1;
1105 #else
1106 extern int yydebug;
1107 yydebug = 1;
1108 #endif
1109 }
1110 break;
1111 #endif
1112 case 'z':
1113 if (optarg) {
1114 zflag = strdup(optarg);
1115 } else {
1116 usage();
1117 /* NOTREACHED */
1118 }
1119 break;
1120
1121 case 'Z':
1122 if (optarg) {
1123 username = strdup(optarg);
1124 }
1125 else {
1126 usage();
1127 /* NOTREACHED */
1128 }
1129 break;
1130
1131 default:
1132 usage();
1133 /* NOTREACHED */
1134 }
1135
1136 switch (tflag) {
1137
1138 case 0: /* Default */
1139 case 4: /* Default + Date*/
1140 thiszone = gmt2local(0);
1141 break;
1142
1143 case 1: /* No time stamp */
1144 case 2: /* Unix timeval style */
1145 case 3: /* Microseconds since previous packet */
1146 case 5: /* Microseconds since first packet */
1147 break;
1148
1149 default: /* Not supported */
1150 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1151 break;
1152 }
1153
1154 if (fflag != 0 && (VFileName != NULL || RFileName != NULL))
1155 error("-f can not be used with -V or -r");
1156
1157 if (VFileName != NULL && RFileName != NULL)
1158 error("-V and -r are mutually exclusive.");
1159
1160 #ifdef WITH_CHROOT
1161 /* if run as root, prepare for chrooting */
1162 if (getuid() == 0 || geteuid() == 0) {
1163 /* future extensibility for cmd-line arguments */
1164 if (!chroot_dir)
1165 chroot_dir = WITH_CHROOT;
1166 }
1167 #endif
1168
1169 #ifdef WITH_USER
1170 /* if run as root, prepare for dropping root privileges */
1171 if (getuid() == 0 || geteuid() == 0) {
1172 /* Run with '-Z root' to restore old behaviour */
1173 if (!username)
1174 username = WITH_USER;
1175 }
1176 #endif
1177
1178 if (RFileName != NULL || VFileName != NULL) {
1179 /*
1180 * If RFileName is non-null, it's the pathname of a
1181 * savefile to read. If VFileName is non-null, it's
1182 * the pathname of a file containing a list of pathnames
1183 * (one per line) of savefiles to read.
1184 *
1185 * In either case, we're reading a savefile, not doing
1186 * a live capture.
1187 */
1188 #ifndef WIN32
1189 /*
1190 * We don't need network access, so relinquish any set-UID
1191 * or set-GID privileges we have (if any).
1192 *
1193 * We do *not* want set-UID privileges when opening a
1194 * trace file, as that might let the user read other
1195 * people's trace files (especially if we're set-UID
1196 * root).
1197 */
1198 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1199 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1200 #endif /* WIN32 */
1201 if (VFileName != NULL) {
1202 if (VFileName[0] == '-' && VFileName[1] == '\0')
1203 VFile = stdin;
1204 else
1205 VFile = fopen(VFileName, "r");
1206
1207 if (VFile == NULL)
1208 error("Unable to open file: %s\n", strerror(errno));
1209
1210 ret = get_next_file(VFile, VFileLine);
1211 if (!ret)
1212 error("Nothing in %s\n", VFileName);
1213 RFileName = VFileLine;
1214 }
1215
1216 pd = pcap_open_offline(RFileName, ebuf);
1217 if (pd == NULL)
1218 error("%s", ebuf);
1219 dlt = pcap_datalink(pd);
1220 dlt_name = pcap_datalink_val_to_name(dlt);
1221 if (dlt_name == NULL) {
1222 fprintf(stderr, "reading from file %s, link-type %u\n",
1223 RFileName, dlt);
1224 } else {
1225 fprintf(stderr,
1226 "reading from file %s, link-type %s (%s)\n",
1227 RFileName, dlt_name,
1228 pcap_datalink_val_to_description(dlt));
1229 }
1230 localnet = 0;
1231 netmask = 0;
1232 } else {
1233 /*
1234 * We're doing a live capture.
1235 */
1236 if (device == NULL) {
1237 device = pcap_lookupdev(ebuf);
1238 if (device == NULL)
1239 error("%s", ebuf);
1240 }
1241 #ifdef WIN32
1242 /*
1243 * Print a message to the standard error on Windows.
1244 * XXX - why do it here, with a different message?
1245 */
1246 if(strlen(device) == 1) /* we assume that an ASCII string is always longer than 1 char */
1247 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1248 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
1249 }
1250 else
1251 {
1252 fprintf(stderr, "%s: listening on %s\n", program_name, device);
1253 }
1254
1255 fflush(stderr);
1256 #endif /* WIN32 */
1257 #ifdef HAVE_PCAP_CREATE
1258 pd = pcap_create(device, ebuf);
1259 if (pd == NULL)
1260 error("%s", ebuf);
1261 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1262 if (Jflag)
1263 show_tstamp_types_and_exit(device, pd);
1264 #endif
1265 /*
1266 * Is this an interface that supports monitor mode?
1267 */
1268 if (pcap_can_set_rfmon(pd) == 1)
1269 supports_monitor_mode = 1;
1270 else
1271 supports_monitor_mode = 0;
1272 status = pcap_set_snaplen(pd, snaplen);
1273 if (status != 0)
1274 error("%s: Can't set snapshot length: %s",
1275 device, pcap_statustostr(status));
1276 status = pcap_set_promisc(pd, !pflag);
1277 if (status != 0)
1278 error("%s: Can't set promiscuous mode: %s",
1279 device, pcap_statustostr(status));
1280 if (Iflag) {
1281 status = pcap_set_rfmon(pd, 1);
1282 if (status != 0)
1283 error("%s: Can't set monitor mode: %s",
1284 device, pcap_statustostr(status));
1285 }
1286 status = pcap_set_timeout(pd, 1000);
1287 if (status != 0)
1288 error("%s: pcap_set_timeout failed: %s",
1289 device, pcap_statustostr(status));
1290 if (Bflag != 0) {
1291 status = pcap_set_buffer_size(pd, Bflag);
1292 if (status != 0)
1293 error("%s: Can't set buffer size: %s",
1294 device, pcap_statustostr(status));
1295 }
1296 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1297 if (jflag != -1) {
1298 status = pcap_set_tstamp_type(pd, jflag);
1299 if (status < 0)
1300 error("%s: Can't set time stamp type: %s",
1301 device, pcap_statustostr(status));
1302 }
1303 #endif
1304 status = pcap_activate(pd);
1305 if (status < 0) {
1306 /*
1307 * pcap_activate() failed.
1308 */
1309 cp = pcap_geterr(pd);
1310 if (status == PCAP_ERROR)
1311 error("%s", cp);
1312 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||
1313 status == PCAP_ERROR_PERM_DENIED) &&
1314 *cp != '\0')
1315 error("%s: %s\n(%s)", device,
1316 pcap_statustostr(status), cp);
1317 else
1318 error("%s: %s", device,
1319 pcap_statustostr(status));
1320 } else if (status > 0) {
1321 /*
1322 * pcap_activate() succeeded, but it's warning us
1323 * of a problem it had.
1324 */
1325 cp = pcap_geterr(pd);
1326 if (status == PCAP_WARNING)
1327 warning("%s", cp);
1328 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1329 *cp != '\0')
1330 warning("%s: %s\n(%s)", device,
1331 pcap_statustostr(status), cp);
1332 else
1333 warning("%s: %s", device,
1334 pcap_statustostr(status));
1335 }
1336 #ifdef HAVE_PCAP_SETDIRECTION
1337 if (Pflag != -1) {
1338 status = pcap_setdirection(pd, Pflag);
1339 if (status != 0)
1340 error("%s: pcap_setdirection() failed: %s",
1341 device, pcap_geterr(pd));
1342 }
1343 #endif
1344 #else
1345 *ebuf = '\0';
1346 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
1347 if (pd == NULL)
1348 error("%s", ebuf);
1349 else if (*ebuf)
1350 warning("%s", ebuf);
1351 #endif /* HAVE_PCAP_CREATE */
1352 /*
1353 * Let user own process after socket has been opened.
1354 */
1355 #ifndef WIN32
1356 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1357 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1358 #endif /* WIN32 */
1359 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1360 if(Bflag != 0)
1361 if(pcap_setbuff(pd, Bflag)==-1){
1362 error("%s", pcap_geterr(pd));
1363 }
1364 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1365 if (Lflag)
1366 show_dlts_and_exit(device, pd);
1367 if (gndo->ndo_dlt >= 0) {
1368 #ifdef HAVE_PCAP_SET_DATALINK
1369 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
1370 error("%s", pcap_geterr(pd));
1371 #else
1372 /*
1373 * We don't actually support changing the
1374 * data link type, so we only let them
1375 * set it to what it already is.
1376 */
1377 if (gndo->ndo_dlt != pcap_datalink(pd)) {
1378 error("%s is not one of the DLTs supported by this device\n",
1379 gndo->ndo_dltname);
1380 }
1381 #endif
1382 (void)fprintf(stderr, "%s: data link type %s\n",
1383 program_name, gndo->ndo_dltname);
1384 (void)fflush(stderr);
1385 }
1386 i = pcap_snapshot(pd);
1387 if (snaplen < i) {
1388 warning("snaplen raised from %d to %d", snaplen, i);
1389 snaplen = i;
1390 }
1391 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1392 localnet = 0;
1393 netmask = 0;
1394 warning("%s", ebuf);
1395 }
1396 }
1397 if (infile)
1398 cmdbuf = read_infile(infile);
1399 else
1400 cmdbuf = copy_argv(&argv[optind]);
1401
1402 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1403 error("%s", pcap_geterr(pd));
1404 if (dflag) {
1405 bpf_dump(&fcode, dflag);
1406 pcap_close(pd);
1407 free(cmdbuf);
1408 exit(0);
1409 }
1410 init_addrtoname(localnet, netmask);
1411 init_checksum();
1412
1413 #ifndef WIN32
1414 (void)setsignal(SIGPIPE, cleanup);
1415 (void)setsignal(SIGTERM, cleanup);
1416 (void)setsignal(SIGINT, cleanup);
1417 #endif /* WIN32 */
1418 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1419 (void)setsignal(SIGCHLD, child_cleanup);
1420 #endif
1421 /* Cooperate with nohup(1) */
1422 #ifndef WIN32
1423 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1424 (void)setsignal(SIGHUP, oldhandler);
1425 #endif /* WIN32 */
1426
1427 #ifndef WIN32
1428 /*
1429 * If a user name was specified with "-Z", attempt to switch to
1430 * that user's UID. This would probably be used with sudo,
1431 * to allow tcpdump to be run in a special restricted
1432 * account (if you just want to allow users to open capture
1433 * devices, and can't just give users that permission,
1434 * you'd make tcpdump set-UID or set-GID).
1435 *
1436 * Tcpdump doesn't necessarily write only to one savefile;
1437 * the general only way to allow a -Z instance to write to
1438 * savefiles as the user under whose UID it's run, rather
1439 * than as the user specified with -Z, would thus be to switch
1440 * to the original user ID before opening a capture file and
1441 * then switch back to the -Z user ID after opening the savefile.
1442 * Switching to the -Z user ID only after opening the first
1443 * savefile doesn't handle the general case.
1444 */
1445
1446 #ifdef HAVE_CAP_NG_H
1447 /* We are running as root and we will be writing to savefile */
1448 if ((getuid() == 0 || geteuid() == 0) && WFileName) {
1449 if (username) {
1450 /* Drop all capabilities from effective set */
1451 capng_clear(CAPNG_EFFECTIVE);
1452 /* Add capabilities we will need*/
1453 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETUID);
1454 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETGID);
1455 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_DAC_OVERRIDE);
1456
1457 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETUID);
1458 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETGID);
1459 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1460
1461 capng_apply(CAPNG_SELECT_BOTH);
1462 }
1463 }
1464 #endif /* HAVE_CAP_NG_H */
1465
1466 if (getuid() == 0 || geteuid() == 0) {
1467 if (username || chroot_dir)
1468 droproot(username, chroot_dir);
1469
1470 }
1471 #endif /* WIN32 */
1472
1473 if (pcap_setfilter(pd, &fcode) < 0)
1474 error("%s", pcap_geterr(pd));
1475 if (WFileName) {
1476 pcap_dumper_t *p;
1477 /* Do not exceed the default PATH_MAX for files. */
1478 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1479
1480 if (dumpinfo.CurrentFileName == NULL)
1481 error("malloc of dumpinfo.CurrentFileName");
1482
1483 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1484 if (Cflag != 0)
1485 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1486 else
1487 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1488
1489 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1490 #ifdef HAVE_CAP_NG_H
1491 /* Give up capabilities, clear Effective set */
1492 capng_clear(CAPNG_EFFECTIVE);
1493 #endif
1494 if (p == NULL)
1495 error("%s", pcap_geterr(pd));
1496 if (Cflag != 0 || Gflag != 0) {
1497 callback = dump_packet_and_trunc;
1498 dumpinfo.WFileName = WFileName;
1499 dumpinfo.pd = pd;
1500 dumpinfo.p = p;
1501 pcap_userdata = (u_char *)&dumpinfo;
1502 } else {
1503 callback = dump_packet;
1504 pcap_userdata = (u_char *)p;
1505 }
1506 #ifdef HAVE_PCAP_DUMP_FLUSH
1507 if (Uflag)
1508 pcap_dump_flush(p);
1509 #endif
1510 } else {
1511 type = pcap_datalink(pd);
1512 printinfo = get_print_info(type);
1513 callback = print_packet;
1514 pcap_userdata = (u_char *)&printinfo;
1515 }
1516
1517 #ifdef SIGNAL_REQ_INFO
1518 /*
1519 * We can't get statistics when reading from a file rather
1520 * than capturing from a device.
1521 */
1522 if (RFileName == NULL)
1523 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
1524 #endif
1525
1526 if (vflag > 0 && WFileName) {
1527 /*
1528 * When capturing to a file, "-v" means tcpdump should,
1529 * every 10 secodns, "v"erbosely report the number of
1530 * packets captured.
1531 */
1532 #ifdef USE_WIN32_MM_TIMER
1533 /* call verbose_stats_dump() each 1000 +/-100msec */
1534 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1535 setvbuf(stderr, NULL, _IONBF, 0);
1536 #elif defined(HAVE_ALARM)
1537 (void)setsignal(SIGALRM, verbose_stats_dump);
1538 alarm(1);
1539 #endif
1540 }
1541
1542 #ifndef WIN32
1543 if (RFileName == NULL) {
1544 /*
1545 * Live capture (if -V was specified, we set RFileName
1546 * to a file from the -V file). Print a message to
1547 * the standard error on UN*X.
1548 */
1549 if (!vflag && !WFileName) {
1550 (void)fprintf(stderr,
1551 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1552 program_name);
1553 } else
1554 (void)fprintf(stderr, "%s: ", program_name);
1555 dlt = pcap_datalink(pd);
1556 dlt_name = pcap_datalink_val_to_name(dlt);
1557 if (dlt_name == NULL) {
1558 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1559 device, dlt, snaplen);
1560 } else {
1561 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1562 device, dlt_name,
1563 pcap_datalink_val_to_description(dlt), snaplen);
1564 }
1565 (void)fflush(stderr);
1566 }
1567 #endif /* WIN32 */
1568 do {
1569 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1570 if (WFileName == NULL) {
1571 /*
1572 * We're printing packets. Flush the printed output,
1573 * so it doesn't get intermingled with error output.
1574 */
1575 if (status == -2) {
1576 /*
1577 * We got interrupted, so perhaps we didn't
1578 * manage to finish a line we were printing.
1579 * Print an extra newline, just in case.
1580 */
1581 putchar('\n');
1582 }
1583 (void)fflush(stdout);
1584 }
1585 if (status == -2) {
1586 /*
1587 * We got interrupted. If we are reading multiple
1588 * files (via -V) set these so that we stop.
1589 */
1590 VFileName = NULL;
1591 ret = NULL;
1592 }
1593 if (status == -1) {
1594 /*
1595 * Error. Report it.
1596 */
1597 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1598 program_name, pcap_geterr(pd));
1599 }
1600 if (RFileName == NULL) {
1601 /*
1602 * We're doing a live capture. Report the capture
1603 * statistics.
1604 */
1605 info(1);
1606 }
1607 pcap_close(pd);
1608 if (VFileName != NULL) {
1609 ret = get_next_file(VFile, VFileLine);
1610 if (ret) {
1611 RFileName = VFileLine;
1612 pd = pcap_open_offline(RFileName, ebuf);
1613 if (pd == NULL)
1614 error("%s", ebuf);
1615 new_dlt = pcap_datalink(pd);
1616 if (WFileName && new_dlt != dlt)
1617 error("%s: new dlt does not match original", RFileName);
1618 printinfo = get_print_info(new_dlt);
1619 dlt_name = pcap_datalink_val_to_name(new_dlt);
1620 if (dlt_name == NULL) {
1621 fprintf(stderr, "reading from file %s, link-type %u\n",
1622 RFileName, new_dlt);
1623 } else {
1624 fprintf(stderr,
1625 "reading from file %s, link-type %s (%s)\n",
1626 RFileName, dlt_name,
1627 pcap_datalink_val_to_description(new_dlt));
1628 }
1629 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1630 error("%s", pcap_geterr(pd));
1631 if (pcap_setfilter(pd, &fcode) < 0)
1632 error("%s", pcap_geterr(pd));
1633 }
1634 }
1635 }
1636 while (ret != NULL);
1637
1638 free(cmdbuf);
1639 exit(status == -1 ? 1 : 0);
1640 }
1641
1642 /* make a clean exit on interrupts */
1643 static RETSIGTYPE
1644 cleanup(int signo _U_)
1645 {
1646 #ifdef USE_WIN32_MM_TIMER
1647 if (timer_id)
1648 timeKillEvent(timer_id);
1649 timer_id = 0;
1650 #elif defined(HAVE_ALARM)
1651 alarm(0);
1652 #endif
1653
1654 #ifdef HAVE_PCAP_BREAKLOOP
1655 /*
1656 * We have "pcap_breakloop()"; use it, so that we do as little
1657 * as possible in the signal handler (it's probably not safe
1658 * to do anything with standard I/O streams in a signal handler -
1659 * the ANSI C standard doesn't say it is).
1660 */
1661 pcap_breakloop(pd);
1662 #else
1663 /*
1664 * We don't have "pcap_breakloop()"; this isn't safe, but
1665 * it's the best we can do. Print the summary if we're
1666 * not reading from a savefile - i.e., if we're doing a
1667 * live capture - and exit.
1668 */
1669 if (pd != NULL && pcap_file(pd) == NULL) {
1670 /*
1671 * We got interrupted, so perhaps we didn't
1672 * manage to finish a line we were printing.
1673 * Print an extra newline, just in case.
1674 */
1675 putchar('\n');
1676 (void)fflush(stdout);
1677 info(1);
1678 }
1679 exit(0);
1680 #endif
1681 }
1682
1683 /*
1684 On windows, we do not use a fork, so we do not care less about
1685 waiting a child processes to die
1686 */
1687 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1688 static RETSIGTYPE
1689 child_cleanup(int signo _U_)
1690 {
1691 wait(NULL);
1692 }
1693 #endif /* HAVE_FORK && HAVE_VFORK */
1694
1695 static void
1696 info(register int verbose)
1697 {
1698 struct pcap_stat stat;
1699
1700 /*
1701 * Older versions of libpcap didn't set ps_ifdrop on some
1702 * platforms; initialize it to 0 to handle that.
1703 */
1704 stat.ps_ifdrop = 0;
1705 if (pcap_stats(pd, &stat) < 0) {
1706 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1707 infoprint = 0;
1708 return;
1709 }
1710
1711 if (!verbose)
1712 fprintf(stderr, "%s: ", program_name);
1713
1714 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
1715 PLURAL_SUFFIX(packets_captured));
1716 if (!verbose)
1717 fputs(", ", stderr);
1718 else
1719 putc('\n', stderr);
1720 (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv,
1721 PLURAL_SUFFIX(stat.ps_recv));
1722 if (!verbose)
1723 fputs(", ", stderr);
1724 else
1725 putc('\n', stderr);
1726 (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop,
1727 PLURAL_SUFFIX(stat.ps_drop));
1728 if (stat.ps_ifdrop != 0) {
1729 if (!verbose)
1730 fputs(", ", stderr);
1731 else
1732 putc('\n', stderr);
1733 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
1734 stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop));
1735 } else
1736 putc('\n', stderr);
1737 infoprint = 0;
1738 }
1739
1740 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1741 static void
1742 compress_savefile(const char *filename)
1743 {
1744 # ifdef HAVE_FORK
1745 if (fork())
1746 # else
1747 if (vfork())
1748 # endif
1749 return;
1750 /*
1751 * Set to lowest priority so that this doesn't disturb the capture
1752 */
1753 #ifdef NZERO
1754 setpriority(PRIO_PROCESS, 0, NZERO - 1);
1755 #else
1756 setpriority(PRIO_PROCESS, 0, 19);
1757 #endif
1758 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
1759 fprintf(stderr,
1760 "compress_savefile:execlp(%s, %s): %s\n",
1761 zflag,
1762 filename,
1763 strerror(errno));
1764 # ifdef HAVE_FORK
1765 exit(1);
1766 # else
1767 _exit(1);
1768 # endif
1769 }
1770 #else /* HAVE_FORK && HAVE_VFORK */
1771 static void
1772 compress_savefile(const char *filename)
1773 {
1774 fprintf(stderr,
1775 "compress_savefile failed. Functionality not implemented under your system\n");
1776 }
1777 #endif /* HAVE_FORK && HAVE_VFORK */
1778
1779 static void
1780 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1781 {
1782 struct dump_info *dump_info;
1783
1784 ++packets_captured;
1785
1786 ++infodelay;
1787
1788 dump_info = (struct dump_info *)user;
1789
1790 /*
1791 * XXX - this won't force the file to rotate on the specified time
1792 * boundary, but it will rotate on the first packet received after the
1793 * specified Gflag number of seconds. Note: if a Gflag time boundary
1794 * and a Cflag size boundary coincide, the time rotation will occur
1795 * first thereby cancelling the Cflag boundary (since the file should
1796 * be 0).
1797 */
1798 if (Gflag != 0) {
1799 /* Check if it is time to rotate */
1800 time_t t;
1801
1802 /* Get the current time */
1803 if ((t = time(NULL)) == (time_t)-1) {
1804 error("dump_and_trunc_packet: can't get current_time: %s",
1805 pcap_strerror(errno));
1806 }
1807
1808
1809 /* If the time is greater than the specified window, rotate */
1810 if (t - Gflag_time >= Gflag) {
1811 /* Update the Gflag_time */
1812 Gflag_time = t;
1813 /* Update Gflag_count */
1814 Gflag_count++;
1815 /*
1816 * Close the current file and open a new one.
1817 */
1818 pcap_dump_close(dump_info->p);
1819
1820 /*
1821 * Compress the file we just closed, if the user asked for it
1822 */
1823 if (zflag != NULL)
1824 compress_savefile(dump_info->CurrentFileName);
1825
1826 /*
1827 * Check to see if we've exceeded the Wflag (when
1828 * not using Cflag).
1829 */
1830 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
1831 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
1832 Wflag);
1833 exit(0);
1834 /* NOTREACHED */
1835 }
1836 if (dump_info->CurrentFileName != NULL)
1837 free(dump_info->CurrentFileName);
1838 /* Allocate space for max filename + \0. */
1839 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
1840 if (dump_info->CurrentFileName == NULL)
1841 error("dump_packet_and_trunc: malloc");
1842 /*
1843 * This is always the first file in the Cflag
1844 * rotation: e.g. 0
1845 * We also don't need numbering if Cflag is not set.
1846 */
1847 if (Cflag != 0)
1848 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
1849 WflagChars);
1850 else
1851 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
1852
1853 #ifdef HAVE_CAP_NG_H
1854 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1855 capng_apply(CAPNG_EFFECTIVE);
1856 #endif /* HAVE_CAP_NG_H */
1857 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1858 #ifdef HAVE_CAP_NG_H
1859 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1860 capng_apply(CAPNG_EFFECTIVE);
1861 #endif /* HAVE_CAP_NG_H */
1862 if (dump_info->p == NULL)
1863 error("%s", pcap_geterr(pd));
1864 }
1865 }
1866
1867 /*
1868 * XXX - this won't prevent capture files from getting
1869 * larger than Cflag - the last packet written to the
1870 * file could put it over Cflag.
1871 */
1872 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) {
1873 /*
1874 * Close the current file and open a new one.
1875 */
1876 pcap_dump_close(dump_info->p);
1877
1878 /*
1879 * Compress the file we just closed, if the user asked for it
1880 */
1881 if (zflag != NULL)
1882 compress_savefile(dump_info->CurrentFileName);
1883
1884 Cflag_count++;
1885 if (Wflag > 0) {
1886 if (Cflag_count >= Wflag)
1887 Cflag_count = 0;
1888 }
1889 if (dump_info->CurrentFileName != NULL)
1890 free(dump_info->CurrentFileName);
1891 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
1892 if (dump_info->CurrentFileName == NULL)
1893 error("dump_packet_and_trunc: malloc");
1894 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
1895 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1896 if (dump_info->p == NULL)
1897 error("%s", pcap_geterr(pd));
1898 }
1899
1900 pcap_dump((u_char *)dump_info->p, h, sp);
1901 #ifdef HAVE_PCAP_DUMP_FLUSH
1902 if (Uflag)
1903 pcap_dump_flush(dump_info->p);
1904 #endif
1905
1906 --infodelay;
1907 if (infoprint)
1908 info(0);
1909 }
1910
1911 static void
1912 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1913 {
1914 ++packets_captured;
1915
1916 ++infodelay;
1917
1918 pcap_dump(user, h, sp);
1919 #ifdef HAVE_PCAP_DUMP_FLUSH
1920 if (Uflag)
1921 pcap_dump_flush((pcap_dumper_t *)user);
1922 #endif
1923
1924 --infodelay;
1925 if (infoprint)
1926 info(0);
1927 }
1928
1929 static void
1930 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1931 {
1932 struct print_info *print_info;
1933 u_int hdrlen;
1934
1935 ++packets_captured;
1936
1937 ++infodelay;
1938 ts_print(&h->ts);
1939
1940 print_info = (struct print_info *)user;
1941
1942 /*
1943 * Some printers want to check that they're not walking off the
1944 * end of the packet.
1945 * Rather than pass it all the way down, we set this global.
1946 */
1947 snapend = sp + h->caplen;
1948
1949 if(print_info->ndo_type) {
1950 hdrlen = (*print_info->p.ndo_printer)(print_info->ndo, h, sp);
1951 } else {
1952 hdrlen = (*print_info->p.printer)(h, sp);
1953 }
1954
1955 if (Xflag) {
1956 /*
1957 * Print the raw packet data in hex and ASCII.
1958 */
1959 if (Xflag > 1) {
1960 /*
1961 * Include the link-layer header.
1962 */
1963 hex_and_ascii_print("\n\t", sp, h->caplen);
1964 } else {
1965 /*
1966 * Don't include the link-layer header - and if
1967 * we have nothing past the link-layer header,
1968 * print nothing.
1969 */
1970 if (h->caplen > hdrlen)
1971 hex_and_ascii_print("\n\t", sp + hdrlen,
1972 h->caplen - hdrlen);
1973 }
1974 } else if (xflag) {
1975 /*
1976 * Print the raw packet data in hex.
1977 */
1978 if (xflag > 1) {
1979 /*
1980 * Include the link-layer header.
1981 */
1982 hex_print("\n\t", sp, h->caplen);
1983 } else {
1984 /*
1985 * Don't include the link-layer header - and if
1986 * we have nothing past the link-layer header,
1987 * print nothing.
1988 */
1989 if (h->caplen > hdrlen)
1990 hex_print("\n\t", sp + hdrlen,
1991 h->caplen - hdrlen);
1992 }
1993 } else if (Aflag) {
1994 /*
1995 * Print the raw packet data in ASCII.
1996 */
1997 if (Aflag > 1) {
1998 /*
1999 * Include the link-layer header.
2000 */
2001 ascii_print(sp, h->caplen);
2002 } else {
2003 /*
2004 * Don't include the link-layer header - and if
2005 * we have nothing past the link-layer header,
2006 * print nothing.
2007 */
2008 if (h->caplen > hdrlen)
2009 ascii_print(sp + hdrlen, h->caplen - hdrlen);
2010 }
2011 }
2012
2013 putchar('\n');
2014
2015 --infodelay;
2016 if (infoprint)
2017 info(0);
2018 }
2019
2020 #ifdef WIN32
2021 /*
2022 * XXX - there should really be libpcap calls to get the version
2023 * number as a string (the string would be generated from #defines
2024 * at run time, so that it's not generated from string constants
2025 * in the library, as, on many UNIX systems, those constants would
2026 * be statically linked into the application executable image, and
2027 * would thus reflect the version of libpcap on the system on
2028 * which the application was *linked*, not the system on which it's
2029 * *running*.
2030 *
2031 * That routine should be documented, unlike the "version[]"
2032 * string, so that UNIX vendors providing their own libpcaps
2033 * don't omit it (as a couple of vendors have...).
2034 *
2035 * Packet.dll should perhaps also export a routine to return the
2036 * version number of the Packet.dll code, to supply the
2037 * "Wpcap_version" information on Windows.
2038 */
2039 char WDversion[]="current-cvs.tcpdump.org";
2040 #if !defined(HAVE_GENERATED_VERSION)
2041 char version[]="current-cvs.tcpdump.org";
2042 #endif
2043 char pcap_version[]="current-cvs.tcpdump.org";
2044 char Wpcap_version[]="3.1";
2045 #endif
2046
2047 /*
2048 * By default, print the specified data out in hex and ASCII.
2049 */
2050 static void
2051 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
2052 {
2053 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
2054 }
2055
2056 void
2057 default_print(const u_char *bp, u_int length)
2058 {
2059 ndo_default_print(gndo, bp, length);
2060 }
2061
2062 #ifdef SIGNAL_REQ_INFO
2063 RETSIGTYPE requestinfo(int signo _U_)
2064 {
2065 if (infodelay)
2066 ++infoprint;
2067 else
2068 info(0);
2069 }
2070 #endif
2071
2072 /*
2073 * Called once each second in verbose mode while dumping to file
2074 */
2075 #ifdef USE_WIN32_MM_TIMER
2076 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2077 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2078 {
2079 struct pcap_stat stat;
2080
2081 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2082 fprintf(stderr, "Got %u\r", packets_captured);
2083 }
2084 #elif defined(HAVE_ALARM)
2085 static void verbose_stats_dump(int sig _U_)
2086 {
2087 struct pcap_stat stat;
2088
2089 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2090 fprintf(stderr, "Got %u\r", packets_captured);
2091 alarm(1);
2092 }
2093 #endif
2094
2095 static void
2096 usage(void)
2097 {
2098 extern char version[];
2099 #ifndef HAVE_PCAP_LIB_VERSION
2100 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2101 extern char pcap_version[];
2102 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2103 static char pcap_version[] = "unknown";
2104 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2105 #endif /* HAVE_PCAP_LIB_VERSION */
2106
2107 #ifdef HAVE_PCAP_LIB_VERSION
2108 #ifdef WIN32
2109 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2110 #else /* WIN32 */
2111 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2112 #endif /* WIN32 */
2113 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2114 #else /* HAVE_PCAP_LIB_VERSION */
2115 #ifdef WIN32
2116 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2117 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2118 #else /* WIN32 */
2119 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2120 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2121 #endif /* WIN32 */
2122 #endif /* HAVE_PCAP_LIB_VERSION */
2123 (void)fprintf(stderr,
2124 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2125 (void)fprintf(stderr,
2126 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2127 (void)fprintf(stderr,
2128 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ]\n");
2129 #ifdef HAVE_PCAP_SETDIRECTION
2130 (void)fprintf(stderr,
2131 "\t\t[ -P in|out|inout ]\n");
2132 #endif
2133 (void)fprintf(stderr,
2134 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2135 (void)fprintf(stderr,
2136 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2137 (void)fprintf(stderr,
2138 "\t\t[ -Z user ] [ expression ]\n");
2139 exit(1);
2140 }
2141
2142
2143
2144 /* VARARGS */
2145 static void
2146 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
2147 {
2148 va_list ap;
2149
2150 (void)fprintf(stderr, "%s: ", program_name);
2151 va_start(ap, fmt);
2152 (void)vfprintf(stderr, fmt, ap);
2153 va_end(ap);
2154 if (*fmt) {
2155 fmt += strlen(fmt);
2156 if (fmt[-1] != '\n')
2157 (void)fputc('\n', stderr);
2158 }
2159 exit(1);
2160 /* NOTREACHED */
2161 }
2162
2163 /* VARARGS */
2164 static void
2165 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
2166 {
2167 va_list ap;
2168
2169 (void)fprintf(stderr, "%s: WARNING: ", program_name);
2170 va_start(ap, fmt);
2171 (void)vfprintf(stderr, fmt, ap);
2172 va_end(ap);
2173 if (*fmt) {
2174 fmt += strlen(fmt);
2175 if (fmt[-1] != '\n')
2176 (void)fputc('\n', stderr);
2177 }
2178 }