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