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