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