]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
Merge pull request #46 from fxlb/master
[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
1015 error("unknown packet type `%s'", optarg);
1016 break;
1017
1018 case 'u':
1019 ++uflag;
1020 break;
1021
1022 #ifdef HAVE_PCAP_DUMP_FLUSH
1023 case 'U':
1024 ++Uflag;
1025 break;
1026 #endif
1027
1028 case 'v':
1029 ++vflag;
1030 break;
1031
1032 case 'V':
1033 VFileName = optarg;
1034 break;
1035
1036 case 'w':
1037 WFileName = optarg;
1038 break;
1039
1040 case 'W':
1041 Wflag = atoi(optarg);
1042 if (Wflag < 0)
1043 error("invalid number of output files %s", optarg);
1044 WflagChars = getWflagChars(Wflag);
1045 break;
1046
1047 case 'x':
1048 ++xflag;
1049 ++suppress_default_print;
1050 break;
1051
1052 case 'X':
1053 ++Xflag;
1054 ++suppress_default_print;
1055 break;
1056
1057 case 'y':
1058 gndo->ndo_dltname = optarg;
1059 gndo->ndo_dlt =
1060 pcap_datalink_name_to_val(gndo->ndo_dltname);
1061 if (gndo->ndo_dlt < 0)
1062 error("invalid data link type %s", gndo->ndo_dltname);
1063 break;
1064
1065 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1066 case 'Y':
1067 {
1068 /* Undocumented flag */
1069 #ifdef HAVE_PCAP_DEBUG
1070 extern int pcap_debug;
1071 pcap_debug = 1;
1072 #else
1073 extern int yydebug;
1074 yydebug = 1;
1075 #endif
1076 }
1077 break;
1078 #endif
1079 case 'z':
1080 if (optarg) {
1081 zflag = strdup(optarg);
1082 } else {
1083 usage();
1084 /* NOTREACHED */
1085 }
1086 break;
1087
1088 case 'Z':
1089 if (optarg) {
1090 username = strdup(optarg);
1091 }
1092 else {
1093 usage();
1094 /* NOTREACHED */
1095 }
1096 break;
1097
1098 default:
1099 usage();
1100 /* NOTREACHED */
1101 }
1102
1103 switch (tflag) {
1104
1105 case 0: /* Default */
1106 case 4: /* Default + Date*/
1107 thiszone = gmt2local(0);
1108 break;
1109
1110 case 1: /* No time stamp */
1111 case 2: /* Unix timeval style */
1112 case 3: /* Microseconds since previous packet */
1113 case 5: /* Microseconds since first packet */
1114 break;
1115
1116 default: /* Not supported */
1117 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1118 break;
1119 }
1120
1121 if (fflag != 0 && (VFileName != NULL || RFileName != NULL))
1122 error("-f can not be used with -V or -r");
1123
1124 if (VFileName != NULL && RFileName != NULL)
1125 error("-V and -r are mutually exclusive.");
1126
1127 #ifdef WITH_CHROOT
1128 /* if run as root, prepare for chrooting */
1129 if (getuid() == 0 || geteuid() == 0) {
1130 /* future extensibility for cmd-line arguments */
1131 if (!chroot_dir)
1132 chroot_dir = WITH_CHROOT;
1133 }
1134 #endif
1135
1136 #ifdef WITH_USER
1137 /* if run as root, prepare for dropping root privileges */
1138 if (getuid() == 0 || geteuid() == 0) {
1139 /* Run with '-Z root' to restore old behaviour */
1140 if (!username)
1141 username = WITH_USER;
1142 }
1143 #endif
1144
1145 if (RFileName != NULL || VFileName != NULL) {
1146 /*
1147 * If RFileName is non-null, it's the pathname of a
1148 * savefile to read. If VFileName is non-null, it's
1149 * the pathname of a file containing a list of pathnames
1150 * (one per line) of savefiles to read.
1151 *
1152 * In either case, we're reading a savefile, not doing
1153 * a live capture.
1154 */
1155 #ifndef WIN32
1156 /*
1157 * We don't need network access, so relinquish any set-UID
1158 * or set-GID privileges we have (if any).
1159 *
1160 * We do *not* want set-UID privileges when opening a
1161 * trace file, as that might let the user read other
1162 * people's trace files (especially if we're set-UID
1163 * root).
1164 */
1165 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1166 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1167 #endif /* WIN32 */
1168 if (VFileName != NULL) {
1169 if (VFileName[0] == '-' && VFileName[1] == '\0')
1170 VFile = stdin;
1171 else
1172 VFile = fopen(VFileName, "r");
1173
1174 if (VFile == NULL)
1175 error("Unable to open file: %s\n", strerror(errno));
1176
1177 ret = get_next_file(VFile, VFileLine);
1178 if (!ret)
1179 error("Nothing in %s\n", VFileName);
1180 RFileName = VFileLine;
1181 }
1182
1183 pd = pcap_open_offline(RFileName, ebuf);
1184 if (pd == NULL)
1185 error("%s", ebuf);
1186 dlt = pcap_datalink(pd);
1187 dlt_name = pcap_datalink_val_to_name(dlt);
1188 if (dlt_name == NULL) {
1189 fprintf(stderr, "reading from file %s, link-type %u\n",
1190 RFileName, dlt);
1191 } else {
1192 fprintf(stderr,
1193 "reading from file %s, link-type %s (%s)\n",
1194 RFileName, dlt_name,
1195 pcap_datalink_val_to_description(dlt));
1196 }
1197 localnet = 0;
1198 netmask = 0;
1199 } else {
1200 /*
1201 * We're doing a live capture.
1202 */
1203 if (device == NULL) {
1204 device = pcap_lookupdev(ebuf);
1205 if (device == NULL)
1206 error("%s", ebuf);
1207 }
1208 #ifdef WIN32
1209 /*
1210 * Print a message to the standard error on Windows.
1211 * XXX - why do it here, with a different message?
1212 */
1213 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
1214 { //a Unicode string has a \0 as second byte (so strlen() is 1)
1215 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
1216 }
1217 else
1218 {
1219 fprintf(stderr, "%s: listening on %s\n", program_name, device);
1220 }
1221
1222 fflush(stderr);
1223 #endif /* WIN32 */
1224 #ifdef HAVE_PCAP_CREATE
1225 pd = pcap_create(device, ebuf);
1226 if (pd == NULL)
1227 error("%s", ebuf);
1228 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1229 if (Jflag)
1230 show_tstamp_types_and_exit(device, pd);
1231 #endif
1232 /*
1233 * Is this an interface that supports monitor mode?
1234 */
1235 if (pcap_can_set_rfmon(pd) == 1)
1236 supports_monitor_mode = 1;
1237 else
1238 supports_monitor_mode = 0;
1239 status = pcap_set_snaplen(pd, snaplen);
1240 if (status != 0)
1241 error("%s: Can't set snapshot length: %s",
1242 device, pcap_statustostr(status));
1243 status = pcap_set_promisc(pd, !pflag);
1244 if (status != 0)
1245 error("%s: Can't set promiscuous mode: %s",
1246 device, pcap_statustostr(status));
1247 if (Iflag) {
1248 status = pcap_set_rfmon(pd, 1);
1249 if (status != 0)
1250 error("%s: Can't set monitor mode: %s",
1251 device, pcap_statustostr(status));
1252 }
1253 status = pcap_set_timeout(pd, 1000);
1254 if (status != 0)
1255 error("%s: pcap_set_timeout failed: %s",
1256 device, pcap_statustostr(status));
1257 if (Bflag != 0) {
1258 status = pcap_set_buffer_size(pd, Bflag);
1259 if (status != 0)
1260 error("%s: Can't set buffer size: %s",
1261 device, pcap_statustostr(status));
1262 }
1263 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1264 if (jflag != -1) {
1265 status = pcap_set_tstamp_type(pd, jflag);
1266 if (status < 0)
1267 error("%s: Can't set time stamp type: %s",
1268 device, pcap_statustostr(status));
1269 }
1270 #endif
1271 status = pcap_activate(pd);
1272 if (status < 0) {
1273 /*
1274 * pcap_activate() failed.
1275 */
1276 cp = pcap_geterr(pd);
1277 if (status == PCAP_ERROR)
1278 error("%s", cp);
1279 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||
1280 status == PCAP_ERROR_PERM_DENIED) &&
1281 *cp != '\0')
1282 error("%s: %s\n(%s)", device,
1283 pcap_statustostr(status), cp);
1284 else
1285 error("%s: %s", device,
1286 pcap_statustostr(status));
1287 } else if (status > 0) {
1288 /*
1289 * pcap_activate() succeeded, but it's warning us
1290 * of a problem it had.
1291 */
1292 cp = pcap_geterr(pd);
1293 if (status == PCAP_WARNING)
1294 warning("%s", cp);
1295 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1296 *cp != '\0')
1297 warning("%s: %s\n(%s)", device,
1298 pcap_statustostr(status), cp);
1299 else
1300 warning("%s: %s", device,
1301 pcap_statustostr(status));
1302 }
1303 #else
1304 *ebuf = '\0';
1305 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
1306 if (pd == NULL)
1307 error("%s", ebuf);
1308 else if (*ebuf)
1309 warning("%s", ebuf);
1310 #endif /* HAVE_PCAP_CREATE */
1311 /*
1312 * Let user own process after socket has been opened.
1313 */
1314 #ifndef WIN32
1315 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1316 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1317 #endif /* WIN32 */
1318 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1319 if(Bflag != 0)
1320 if(pcap_setbuff(pd, Bflag)==-1){
1321 error("%s", pcap_geterr(pd));
1322 }
1323 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1324 if (Lflag)
1325 show_dlts_and_exit(device, pd);
1326 if (gndo->ndo_dlt >= 0) {
1327 #ifdef HAVE_PCAP_SET_DATALINK
1328 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
1329 error("%s", pcap_geterr(pd));
1330 #else
1331 /*
1332 * We don't actually support changing the
1333 * data link type, so we only let them
1334 * set it to what it already is.
1335 */
1336 if (gndo->ndo_dlt != pcap_datalink(pd)) {
1337 error("%s is not one of the DLTs supported by this device\n",
1338 gndo->ndo_dltname);
1339 }
1340 #endif
1341 (void)fprintf(stderr, "%s: data link type %s\n",
1342 program_name, gndo->ndo_dltname);
1343 (void)fflush(stderr);
1344 }
1345 i = pcap_snapshot(pd);
1346 if (snaplen < i) {
1347 warning("snaplen raised from %d to %d", snaplen, i);
1348 snaplen = i;
1349 }
1350 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1351 localnet = 0;
1352 netmask = 0;
1353 warning("%s", ebuf);
1354 }
1355 }
1356 if (infile)
1357 cmdbuf = read_infile(infile);
1358 else
1359 cmdbuf = copy_argv(&argv[optind]);
1360
1361 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1362 error("%s", pcap_geterr(pd));
1363 if (dflag) {
1364 bpf_dump(&fcode, dflag);
1365 pcap_close(pd);
1366 free(cmdbuf);
1367 exit(0);
1368 }
1369 init_addrtoname(localnet, netmask);
1370 init_checksum();
1371
1372 #ifndef WIN32
1373 (void)setsignal(SIGPIPE, cleanup);
1374 (void)setsignal(SIGTERM, cleanup);
1375 (void)setsignal(SIGINT, cleanup);
1376 #endif /* WIN32 */
1377 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1378 (void)setsignal(SIGCHLD, child_cleanup);
1379 #endif
1380 /* Cooperate with nohup(1) */
1381 #ifndef WIN32
1382 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1383 (void)setsignal(SIGHUP, oldhandler);
1384 #endif /* WIN32 */
1385
1386 #ifndef WIN32
1387 /*
1388 * If a user name was specified with "-Z", attempt to switch to
1389 * that user's UID. This would probably be used with sudo,
1390 * to allow tcpdump to be run in a special restricted
1391 * account (if you just want to allow users to open capture
1392 * devices, and can't just give users that permission,
1393 * you'd make tcpdump set-UID or set-GID).
1394 *
1395 * Tcpdump doesn't necessarily write only to one savefile;
1396 * the general only way to allow a -Z instance to write to
1397 * savefiles as the user under whose UID it's run, rather
1398 * than as the user specified with -Z, would thus be to switch
1399 * to the original user ID before opening a capture file and
1400 * then switch back to the -Z user ID after opening the savefile.
1401 * Switching to the -Z user ID only after opening the first
1402 * savefile doesn't handle the general case.
1403 */
1404
1405 #ifdef HAVE_CAP_NG_H
1406 /* We are running as root and we will be writing to savefile */
1407 if ((getuid() == 0 || geteuid() == 0) && WFileName) {
1408 if (username) {
1409 /* Drop all capabilities from effective set */
1410 capng_clear(CAPNG_EFFECTIVE);
1411 /* Add capabilities we will need*/
1412 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETUID);
1413 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETGID);
1414 capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_DAC_OVERRIDE);
1415
1416 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETUID);
1417 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETGID);
1418 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1419
1420 capng_apply(CAPNG_SELECT_BOTH);
1421 }
1422 }
1423 #endif /* HAVE_CAP_NG_H */
1424
1425 if (getuid() == 0 || geteuid() == 0) {
1426 if (username || chroot_dir)
1427 droproot(username, chroot_dir);
1428
1429 }
1430 #endif /* WIN32 */
1431
1432 if (pcap_setfilter(pd, &fcode) < 0)
1433 error("%s", pcap_geterr(pd));
1434 if (WFileName) {
1435 pcap_dumper_t *p;
1436 /* Do not exceed the default PATH_MAX for files. */
1437 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1438
1439 if (dumpinfo.CurrentFileName == NULL)
1440 error("malloc of dumpinfo.CurrentFileName");
1441
1442 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1443 if (Cflag != 0)
1444 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1445 else
1446 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1447
1448 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1449 #ifdef HAVE_CAP_NG_H
1450 /* Give up capabilities, clear Effective set */
1451 capng_clear(CAPNG_EFFECTIVE);
1452 #endif
1453 if (p == NULL)
1454 error("%s", pcap_geterr(pd));
1455 if (Cflag != 0 || Gflag != 0) {
1456 callback = dump_packet_and_trunc;
1457 dumpinfo.WFileName = WFileName;
1458 dumpinfo.pd = pd;
1459 dumpinfo.p = p;
1460 pcap_userdata = (u_char *)&dumpinfo;
1461 } else {
1462 callback = dump_packet;
1463 pcap_userdata = (u_char *)p;
1464 }
1465 #ifdef HAVE_PCAP_DUMP_FLUSH
1466 if (Uflag)
1467 pcap_dump_flush(p);
1468 #endif
1469 } else {
1470 type = pcap_datalink(pd);
1471 printinfo = get_print_info(type);
1472 callback = print_packet;
1473 pcap_userdata = (u_char *)&printinfo;
1474 }
1475
1476 #ifdef SIGNAL_REQ_INFO
1477 /*
1478 * We can't get statistics when reading from a file rather
1479 * than capturing from a device.
1480 */
1481 if (RFileName == NULL)
1482 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
1483 #endif
1484
1485 if (vflag > 0 && WFileName) {
1486 /*
1487 * When capturing to a file, "-v" means tcpdump should,
1488 * every 10 secodns, "v"erbosely report the number of
1489 * packets captured.
1490 */
1491 #ifdef USE_WIN32_MM_TIMER
1492 /* call verbose_stats_dump() each 1000 +/-100msec */
1493 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1494 setvbuf(stderr, NULL, _IONBF, 0);
1495 #elif defined(HAVE_ALARM)
1496 (void)setsignal(SIGALRM, verbose_stats_dump);
1497 alarm(1);
1498 #endif
1499 }
1500
1501 #ifndef WIN32
1502 if (RFileName == NULL) {
1503 /*
1504 * Live capture (if -V was specified, we set RFileName
1505 * to a file from the -V file). Print a message to
1506 * the standard error on UN*X.
1507 */
1508 if (!vflag && !WFileName) {
1509 (void)fprintf(stderr,
1510 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1511 program_name);
1512 } else
1513 (void)fprintf(stderr, "%s: ", program_name);
1514 dlt = pcap_datalink(pd);
1515 dlt_name = pcap_datalink_val_to_name(dlt);
1516 if (dlt_name == NULL) {
1517 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1518 device, dlt, snaplen);
1519 } else {
1520 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1521 device, dlt_name,
1522 pcap_datalink_val_to_description(dlt), snaplen);
1523 }
1524 (void)fflush(stderr);
1525 }
1526 #endif /* WIN32 */
1527 do {
1528 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1529 if (WFileName == NULL) {
1530 /*
1531 * We're printing packets. Flush the printed output,
1532 * so it doesn't get intermingled with error output.
1533 */
1534 if (status == -2) {
1535 /*
1536 * We got interrupted, so perhaps we didn't
1537 * manage to finish a line we were printing.
1538 * Print an extra newline, just in case.
1539 */
1540 putchar('\n');
1541 }
1542 (void)fflush(stdout);
1543 }
1544 if (status == -1) {
1545 /*
1546 * Error. Report it.
1547 */
1548 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1549 program_name, pcap_geterr(pd));
1550 }
1551 if (RFileName == NULL) {
1552 /*
1553 * We're doing a live capture. Report the capture
1554 * statistics.
1555 */
1556 info(1);
1557 }
1558 pcap_close(pd);
1559 if (VFileName != NULL) {
1560 ret = get_next_file(VFile, VFileLine);
1561 if (ret) {
1562 RFileName = VFileLine;
1563 pd = pcap_open_offline(RFileName, ebuf);
1564 if (pd == NULL)
1565 error("%s", ebuf);
1566 new_dlt = pcap_datalink(pd);
1567 if (WFileName && new_dlt != dlt)
1568 error("%s: new dlt does not match original", RFileName);
1569 printinfo = get_print_info(new_dlt);
1570 dlt_name = pcap_datalink_val_to_name(new_dlt);
1571 if (dlt_name == NULL) {
1572 fprintf(stderr, "reading from file %s, link-type %u\n",
1573 RFileName, new_dlt);
1574 } else {
1575 fprintf(stderr,
1576 "reading from file %s, link-type %s (%s)\n",
1577 RFileName, dlt_name,
1578 pcap_datalink_val_to_description(new_dlt));
1579 }
1580 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1581 error("%s", pcap_geterr(pd));
1582 if (pcap_setfilter(pd, &fcode) < 0)
1583 error("%s", pcap_geterr(pd));
1584 }
1585 }
1586 }
1587 while (ret != NULL);
1588
1589 free(cmdbuf);
1590 exit(status == -1 ? 1 : 0);
1591 }
1592
1593 /* make a clean exit on interrupts */
1594 static RETSIGTYPE
1595 cleanup(int signo _U_)
1596 {
1597 #ifdef USE_WIN32_MM_TIMER
1598 if (timer_id)
1599 timeKillEvent(timer_id);
1600 timer_id = 0;
1601 #elif defined(HAVE_ALARM)
1602 alarm(0);
1603 #endif
1604
1605 #ifdef HAVE_PCAP_BREAKLOOP
1606 /*
1607 * We have "pcap_breakloop()"; use it, so that we do as little
1608 * as possible in the signal handler (it's probably not safe
1609 * to do anything with standard I/O streams in a signal handler -
1610 * the ANSI C standard doesn't say it is).
1611 */
1612 pcap_breakloop(pd);
1613 #else
1614 /*
1615 * We don't have "pcap_breakloop()"; this isn't safe, but
1616 * it's the best we can do. Print the summary if we're
1617 * not reading from a savefile - i.e., if we're doing a
1618 * live capture - and exit.
1619 */
1620 if (pd != NULL && pcap_file(pd) == NULL) {
1621 /*
1622 * We got interrupted, so perhaps we didn't
1623 * manage to finish a line we were printing.
1624 * Print an extra newline, just in case.
1625 */
1626 putchar('\n');
1627 (void)fflush(stdout);
1628 info(1);
1629 }
1630 exit(0);
1631 #endif
1632 }
1633
1634 /*
1635 On windows, we do not use a fork, so we do not care less about
1636 waiting a child processes to die
1637 */
1638 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1639 static RETSIGTYPE
1640 child_cleanup(int signo _U_)
1641 {
1642 wait(NULL);
1643 }
1644 #endif /* HAVE_FORK && HAVE_VFORK */
1645
1646 static void
1647 info(register int verbose)
1648 {
1649 struct pcap_stat stat;
1650
1651 /*
1652 * Older versions of libpcap didn't set ps_ifdrop on some
1653 * platforms; initialize it to 0 to handle that.
1654 */
1655 stat.ps_ifdrop = 0;
1656 if (pcap_stats(pd, &stat) < 0) {
1657 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1658 infoprint = 0;
1659 return;
1660 }
1661
1662 if (!verbose)
1663 fprintf(stderr, "%s: ", program_name);
1664
1665 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
1666 PLURAL_SUFFIX(packets_captured));
1667 if (!verbose)
1668 fputs(", ", stderr);
1669 else
1670 putc('\n', stderr);
1671 (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv,
1672 PLURAL_SUFFIX(stat.ps_recv));
1673 if (!verbose)
1674 fputs(", ", stderr);
1675 else
1676 putc('\n', stderr);
1677 (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop,
1678 PLURAL_SUFFIX(stat.ps_drop));
1679 if (stat.ps_ifdrop != 0) {
1680 if (!verbose)
1681 fputs(", ", stderr);
1682 else
1683 putc('\n', stderr);
1684 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
1685 stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop));
1686 } else
1687 putc('\n', stderr);
1688 infoprint = 0;
1689 }
1690
1691 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1692 static void
1693 compress_savefile(const char *filename)
1694 {
1695 # ifdef HAVE_FORK
1696 if (fork())
1697 # else
1698 if (vfork())
1699 # endif
1700 return;
1701 /*
1702 * Set to lowest priority so that this doesn't disturb the capture
1703 */
1704 #ifdef NZERO
1705 setpriority(PRIO_PROCESS, 0, NZERO - 1);
1706 #else
1707 setpriority(PRIO_PROCESS, 0, 19);
1708 #endif
1709 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
1710 fprintf(stderr,
1711 "compress_savefile:execlp(%s, %s): %s\n",
1712 zflag,
1713 filename,
1714 strerror(errno));
1715 # ifdef HAVE_FORK
1716 exit(1);
1717 # else
1718 _exit(1);
1719 # endif
1720 }
1721 #else /* HAVE_FORK && HAVE_VFORK */
1722 static void
1723 compress_savefile(const char *filename)
1724 {
1725 fprintf(stderr,
1726 "compress_savefile failed. Functionality not implemented under your system\n");
1727 }
1728 #endif /* HAVE_FORK && HAVE_VFORK */
1729
1730 static void
1731 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1732 {
1733 struct dump_info *dump_info;
1734
1735 ++packets_captured;
1736
1737 ++infodelay;
1738
1739 dump_info = (struct dump_info *)user;
1740
1741 /*
1742 * XXX - this won't force the file to rotate on the specified time
1743 * boundary, but it will rotate on the first packet received after the
1744 * specified Gflag number of seconds. Note: if a Gflag time boundary
1745 * and a Cflag size boundary coincide, the time rotation will occur
1746 * first thereby cancelling the Cflag boundary (since the file should
1747 * be 0).
1748 */
1749 if (Gflag != 0) {
1750 /* Check if it is time to rotate */
1751 time_t t;
1752
1753 /* Get the current time */
1754 if ((t = time(NULL)) == (time_t)-1) {
1755 error("dump_and_trunc_packet: can't get current_time: %s",
1756 pcap_strerror(errno));
1757 }
1758
1759
1760 /* If the time is greater than the specified window, rotate */
1761 if (t - Gflag_time >= Gflag) {
1762 /* Update the Gflag_time */
1763 Gflag_time = t;
1764 /* Update Gflag_count */
1765 Gflag_count++;
1766 /*
1767 * Close the current file and open a new one.
1768 */
1769 pcap_dump_close(dump_info->p);
1770
1771 /*
1772 * Compress the file we just closed, if the user asked for it
1773 */
1774 if (zflag != NULL)
1775 compress_savefile(dump_info->CurrentFileName);
1776
1777 /*
1778 * Check to see if we've exceeded the Wflag (when
1779 * not using Cflag).
1780 */
1781 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
1782 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
1783 Wflag);
1784 exit(0);
1785 /* NOTREACHED */
1786 }
1787 if (dump_info->CurrentFileName != NULL)
1788 free(dump_info->CurrentFileName);
1789 /* Allocate space for max filename + \0. */
1790 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
1791 if (dump_info->CurrentFileName == NULL)
1792 error("dump_packet_and_trunc: malloc");
1793 /*
1794 * This is always the first file in the Cflag
1795 * rotation: e.g. 0
1796 * We also don't need numbering if Cflag is not set.
1797 */
1798 if (Cflag != 0)
1799 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
1800 WflagChars);
1801 else
1802 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
1803
1804 #ifdef HAVE_CAP_NG_H
1805 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1806 capng_apply(CAPNG_EFFECTIVE);
1807 #endif /* HAVE_CAP_NG_H */
1808 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1809 #ifdef HAVE_CAP_NG_H
1810 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
1811 capng_apply(CAPNG_EFFECTIVE);
1812 #endif /* HAVE_CAP_NG_H */
1813 if (dump_info->p == NULL)
1814 error("%s", pcap_geterr(pd));
1815 }
1816 }
1817
1818 /*
1819 * XXX - this won't prevent capture files from getting
1820 * larger than Cflag - the last packet written to the
1821 * file could put it over Cflag.
1822 */
1823 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) {
1824 /*
1825 * Close the current file and open a new one.
1826 */
1827 pcap_dump_close(dump_info->p);
1828
1829 /*
1830 * Compress the file we just closed, if the user asked for it
1831 */
1832 if (zflag != NULL)
1833 compress_savefile(dump_info->CurrentFileName);
1834
1835 Cflag_count++;
1836 if (Wflag > 0) {
1837 if (Cflag_count >= Wflag)
1838 Cflag_count = 0;
1839 }
1840 if (dump_info->CurrentFileName != NULL)
1841 free(dump_info->CurrentFileName);
1842 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
1843 if (dump_info->CurrentFileName == NULL)
1844 error("dump_packet_and_trunc: malloc");
1845 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
1846 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1847 if (dump_info->p == NULL)
1848 error("%s", pcap_geterr(pd));
1849 }
1850
1851 pcap_dump((u_char *)dump_info->p, h, sp);
1852 #ifdef HAVE_PCAP_DUMP_FLUSH
1853 if (Uflag)
1854 pcap_dump_flush(dump_info->p);
1855 #endif
1856
1857 --infodelay;
1858 if (infoprint)
1859 info(0);
1860 }
1861
1862 static void
1863 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1864 {
1865 ++packets_captured;
1866
1867 ++infodelay;
1868
1869 pcap_dump(user, h, sp);
1870 #ifdef HAVE_PCAP_DUMP_FLUSH
1871 if (Uflag)
1872 pcap_dump_flush((pcap_dumper_t *)user);
1873 #endif
1874
1875 --infodelay;
1876 if (infoprint)
1877 info(0);
1878 }
1879
1880 static void
1881 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1882 {
1883 struct print_info *print_info;
1884 u_int hdrlen;
1885
1886 ++packets_captured;
1887
1888 ++infodelay;
1889 ts_print(&h->ts);
1890
1891 print_info = (struct print_info *)user;
1892
1893 /*
1894 * Some printers want to check that they're not walking off the
1895 * end of the packet.
1896 * Rather than pass it all the way down, we set this global.
1897 */
1898 snapend = sp + h->caplen;
1899
1900 if(print_info->ndo_type) {
1901 hdrlen = (*print_info->p.ndo_printer)(print_info->ndo, h, sp);
1902 } else {
1903 hdrlen = (*print_info->p.printer)(h, sp);
1904 }
1905
1906 if (Xflag) {
1907 /*
1908 * Print the raw packet data in hex and ASCII.
1909 */
1910 if (Xflag > 1) {
1911 /*
1912 * Include the link-layer header.
1913 */
1914 hex_and_ascii_print("\n\t", sp, h->caplen);
1915 } else {
1916 /*
1917 * Don't include the link-layer header - and if
1918 * we have nothing past the link-layer header,
1919 * print nothing.
1920 */
1921 if (h->caplen > hdrlen)
1922 hex_and_ascii_print("\n\t", sp + hdrlen,
1923 h->caplen - hdrlen);
1924 }
1925 } else if (xflag) {
1926 /*
1927 * Print the raw packet data in hex.
1928 */
1929 if (xflag > 1) {
1930 /*
1931 * Include the link-layer header.
1932 */
1933 hex_print("\n\t", sp, h->caplen);
1934 } else {
1935 /*
1936 * Don't include the link-layer header - and if
1937 * we have nothing past the link-layer header,
1938 * print nothing.
1939 */
1940 if (h->caplen > hdrlen)
1941 hex_print("\n\t", sp + hdrlen,
1942 h->caplen - hdrlen);
1943 }
1944 } else if (Aflag) {
1945 /*
1946 * Print the raw packet data in ASCII.
1947 */
1948 if (Aflag > 1) {
1949 /*
1950 * Include the link-layer header.
1951 */
1952 ascii_print(sp, h->caplen);
1953 } else {
1954 /*
1955 * Don't include the link-layer header - and if
1956 * we have nothing past the link-layer header,
1957 * print nothing.
1958 */
1959 if (h->caplen > hdrlen)
1960 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1961 }
1962 }
1963
1964 putchar('\n');
1965
1966 --infodelay;
1967 if (infoprint)
1968 info(0);
1969 }
1970
1971 #ifdef WIN32
1972 /*
1973 * XXX - there should really be libpcap calls to get the version
1974 * number as a string (the string would be generated from #defines
1975 * at run time, so that it's not generated from string constants
1976 * in the library, as, on many UNIX systems, those constants would
1977 * be statically linked into the application executable image, and
1978 * would thus reflect the version of libpcap on the system on
1979 * which the application was *linked*, not the system on which it's
1980 * *running*.
1981 *
1982 * That routine should be documented, unlike the "version[]"
1983 * string, so that UNIX vendors providing their own libpcaps
1984 * don't omit it (as a couple of vendors have...).
1985 *
1986 * Packet.dll should perhaps also export a routine to return the
1987 * version number of the Packet.dll code, to supply the
1988 * "Wpcap_version" information on Windows.
1989 */
1990 char WDversion[]="current-cvs.tcpdump.org";
1991 #if !defined(HAVE_GENERATED_VERSION)
1992 char version[]="current-cvs.tcpdump.org";
1993 #endif
1994 char pcap_version[]="current-cvs.tcpdump.org";
1995 char Wpcap_version[]="3.1";
1996 #endif
1997
1998 /*
1999 * By default, print the specified data out in hex and ASCII.
2000 */
2001 static void
2002 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
2003 {
2004 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
2005 }
2006
2007 void
2008 default_print(const u_char *bp, u_int length)
2009 {
2010 ndo_default_print(gndo, bp, length);
2011 }
2012
2013 #ifdef SIGNAL_REQ_INFO
2014 RETSIGTYPE requestinfo(int signo _U_)
2015 {
2016 if (infodelay)
2017 ++infoprint;
2018 else
2019 info(0);
2020 }
2021 #endif
2022
2023 /*
2024 * Called once each second in verbose mode while dumping to file
2025 */
2026 #ifdef USE_WIN32_MM_TIMER
2027 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2028 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2029 {
2030 struct pcap_stat stat;
2031
2032 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2033 fprintf(stderr, "Got %u\r", packets_captured);
2034 }
2035 #elif defined(HAVE_ALARM)
2036 static void verbose_stats_dump(int sig _U_)
2037 {
2038 struct pcap_stat stat;
2039
2040 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2041 fprintf(stderr, "Got %u\r", packets_captured);
2042 alarm(1);
2043 }
2044 #endif
2045
2046 static void
2047 usage(void)
2048 {
2049 extern char version[];
2050 #ifndef HAVE_PCAP_LIB_VERSION
2051 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2052 extern char pcap_version[];
2053 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2054 static char pcap_version[] = "unknown";
2055 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2056 #endif /* HAVE_PCAP_LIB_VERSION */
2057
2058 #ifdef HAVE_PCAP_LIB_VERSION
2059 #ifdef WIN32
2060 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2061 #else /* WIN32 */
2062 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2063 #endif /* WIN32 */
2064 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2065 #else /* HAVE_PCAP_LIB_VERSION */
2066 #ifdef WIN32
2067 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2068 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2069 #else /* WIN32 */
2070 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2071 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2072 #endif /* WIN32 */
2073 #endif /* HAVE_PCAP_LIB_VERSION */
2074 (void)fprintf(stderr,
2075 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2076 (void)fprintf(stderr,
2077 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2078 (void)fprintf(stderr,
2079 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ]\n");
2080 (void)fprintf(stderr,
2081 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2082 (void)fprintf(stderr,
2083 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2084 (void)fprintf(stderr,
2085 "\t\t[ -Z user ] [ expression ]\n");
2086 exit(1);
2087 }
2088
2089
2090
2091 /* VARARGS */
2092 static void
2093 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
2094 {
2095 va_list ap;
2096
2097 (void)fprintf(stderr, "%s: ", program_name);
2098 va_start(ap, fmt);
2099 (void)vfprintf(stderr, fmt, ap);
2100 va_end(ap);
2101 if (*fmt) {
2102 fmt += strlen(fmt);
2103 if (fmt[-1] != '\n')
2104 (void)fputc('\n', stderr);
2105 }
2106 exit(1);
2107 /* NOTREACHED */
2108 }
2109
2110 /* VARARGS */
2111 static void
2112 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
2113 {
2114 va_list ap;
2115
2116 (void)fprintf(stderr, "%s: WARNING: ", program_name);
2117 va_start(ap, fmt);
2118 (void)vfprintf(stderr, fmt, ap);
2119 va_end(ap);
2120 if (*fmt) {
2121 fmt += strlen(fmt);
2122 if (fmt[-1] != '\n')
2123 (void)fputc('\n', stderr);
2124 }
2125 }