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