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