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