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