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