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