]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
NetBSD support for multiple data link types on an interface, from David
[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[] =
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[] =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.192 2002-12-19 09:27:57 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 #include <pcap.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "interface.h"
66 #include "addrtoname.h"
67 #include "machdep.h"
68 #include "setsignal.h"
69 #include "gmt2local.h"
70 #include "pcap-missing.h"
71
72 int aflag; /* translate network and broadcast addresses */
73 int dflag; /* print filter code */
74 int eflag; /* print ethernet header */
75 int fflag; /* don't translate "foreign" IP address */
76 int Lflag; /* list available data link types and exit */
77 int nflag; /* leave addresses as numbers */
78 int Nflag; /* remove domains from printed host names */
79 int Oflag = 1; /* run filter code optimizer */
80 int pflag; /* don't go promiscuous */
81 int qflag; /* quick (shorter) output */
82 int Rflag = 1; /* print sequence # field in AH/ESP*/
83 int sflag = 0; /* use the libsmi to translate OIDs */
84 int Sflag; /* print raw TCP sequence numbers */
85 int tflag = 1; /* print packet arrival time */
86 int uflag = 0; /* Print undecoded NFS handles */
87 int vflag; /* verbose */
88 int xflag; /* print packet in hex */
89 int Xflag; /* print packet in ascii as well as hex */
90 off_t Cflag = 0; /* rotate dump files after this many bytes */
91 int Aflag = 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
92 int dlt = -1; /* if != -1, ask libpcap for the DLT it names */
93
94 const char *dlt_name = NULL;
95
96 char *espsecret = NULL; /* ESP secret key */
97
98 int packettype;
99
100 int infodelay;
101 int infoprint;
102
103 char *program_name;
104
105 int32_t thiszone; /* seconds offset from gmt to local time */
106
107 /* Forwards */
108 static RETSIGTYPE cleanup(int);
109 static void usage(void) __attribute__((noreturn));
110 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
111
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
115 #ifdef SIGINFO
116 RETSIGTYPE requestinfo(int);
117 #endif
118
119 /* Length of saved portion of packet. */
120 int snaplen = DEFAULT_SNAPLEN;
121
122 struct printer {
123 pcap_handler f;
124 int type;
125 };
126
127 static struct printer printers[] = {
128 { arcnet_if_print, DLT_ARCNET },
129 { ether_if_print, DLT_EN10MB },
130 { token_if_print, DLT_IEEE802 },
131 #ifdef DLT_LANE8023
132 { lane_if_print, DLT_LANE8023 },
133 #endif
134 #ifdef DLT_CIP
135 { cip_if_print, DLT_CIP },
136 #endif
137 #ifdef DLT_ATM_CLIP
138 { cip_if_print, DLT_ATM_CLIP },
139 #endif
140 { sl_if_print, DLT_SLIP },
141 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
142 { ppp_if_print, DLT_PPP },
143 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
144 { fddi_if_print, DLT_FDDI },
145 { null_if_print, DLT_NULL },
146 #ifdef DLT_LOOP
147 { null_if_print, DLT_LOOP },
148 #endif
149 { raw_if_print, DLT_RAW },
150 { atm_if_print, DLT_ATM_RFC1483 },
151 #ifdef DLT_C_HDLC
152 { chdlc_if_print, DLT_C_HDLC },
153 #endif
154 #ifdef DLT_HDLC
155 { chdlc_if_print, DLT_HDLC },
156 #endif
157 #ifdef DLT_PPP_SERIAL
158 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
159 #endif
160 #ifdef DLT_PPP_ETHER
161 { pppoe_if_print, DLT_PPP_ETHER },
162 #endif
163 #ifdef DLT_LINUX_SLL
164 { sll_if_print, DLT_LINUX_SLL },
165 #endif
166 #ifdef DLT_IEEE802_11
167 { ieee802_11_if_print, DLT_IEEE802_11},
168 #endif
169 #ifdef DLT_LTALK
170 { ltalk_if_print, DLT_LTALK },
171 #endif
172 #ifdef DLT_PFLOG
173 { pflog_if_print, DLT_PFLOG },
174 #endif
175 #ifdef DLT_FR
176 { fr_if_print, DLT_FR },
177 #endif
178 #ifdef DLT_FRELAY
179 { fr_if_print, DLT_FRELAY },
180 #endif
181 #ifdef DLT_SUNATM
182 { sunatm_if_print, DLT_SUNATM },
183 #endif
184 #ifdef DLT_IP_OVER_FC
185 { ipfc_if_print, DLT_IP_OVER_FC },
186 #endif
187 #ifdef DLT_PRISM_HEADER
188 { prism_if_print, DLT_PRISM_HEADER },
189 #endif
190 #ifdef DLT_IEEE802_11_RADIO
191 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
192 #endif
193 { NULL, 0 },
194 };
195
196 static pcap_handler
197 lookup_printer(int type)
198 {
199 struct printer *p;
200
201 for (p = printers; p->f; ++p)
202 if (type == p->type)
203 return p->f;
204
205 return NULL;
206 /* NOTREACHED */
207 }
208
209 static pcap_t *pd;
210
211 extern int optind;
212 extern int opterr;
213 extern char *optarg;
214
215 struct dump_info {
216 char *WFileName;
217 pcap_t *pd;
218 pcap_dumper_t *p;
219 };
220
221 static void
222 show_dlts_and_exit(pcap_t *pd)
223 {
224 int n_dlts;
225 int *dlts = 0;
226 const char *dlt_name;
227
228 n_dlts = pcap_list_datalinks(pd, &dlts);
229 if (n_dlts < 0)
230 error("%s", pcap_geterr(pd));
231 else if (n_dlts == 0 || !dlts)
232 error("No data link types.");
233
234 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
235
236 while (--n_dlts >= 0) {
237 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
238 if (dlt_name != NULL) {
239 (void) fprintf(stderr, " %s", dlt_name);
240
241 /*
242 * OK, does tcpdump handle that type?
243 */
244 if (lookup_printer(dlts[n_dlts]) == NULL)
245 (void) fprintf(stderr, " (not supported)");
246 putchar('\n');
247 } else {
248 (void) fprintf(stderr, " DLT %d (not supported)\n",
249 dlts[n_dlts]);
250 }
251 }
252 free(dlts);
253 exit(0);
254 }
255
256 int
257 main(int argc, char **argv)
258 {
259 register int cnt, op, i;
260 bpf_u_int32 localnet, netmask;
261 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
262 int type;
263 pcap_handler printer;
264 struct bpf_program fcode;
265 #ifndef WIN32
266 RETSIGTYPE (*oldhandler)(int);
267 #endif
268 struct dump_info dumpinfo;
269 u_char *pcap_userdata;
270 char ebuf[PCAP_ERRBUF_SIZE];
271 #ifdef HAVE_PCAP_FINDALLDEVS
272 pcap_if_t *devpointer;
273 int devnum;
274 #endif
275 #ifdef WIN32
276 DWORD dwVersion;
277 DWORD dwWindowsMajorVersion;
278 u_int UserBufferSize=1000000;
279 #endif
280
281 #ifdef WIN32
282 dwVersion=GetVersion(); /* get the OS version */
283 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
284 if(wsockinit()!=0) return 1;
285 #endif /* WIN32 */
286
287 cnt = -1;
288 device = NULL;
289 infile = NULL;
290 RFileName = NULL;
291 WFileName = NULL;
292 if ((cp = strrchr(argv[0], '/')) != NULL)
293 program_name = cp + 1;
294 else
295 program_name = argv[0];
296
297 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
298 error("%s", ebuf);
299
300 #ifdef LIBSMI
301 smiInit("tcpdump");
302 #endif
303
304 opterr = 0;
305 while (
306 #ifdef WIN32
307 (op = getopt(argc, argv, "aAB:c:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
308 #else /* WIN32 */
309 #ifdef HAVE_PCAP_FINDALLDEVS
310 (op = getopt(argc, argv, "aAc:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
311 #else /* HAVE_PCAP_FINDALLDEVS */
312 (op = getopt(argc, argv, "aAc:C:deE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
313 #endif /* HAVE_PCAP_FINDALLDEVS */
314 #endif /* WIN32 */
315 switch (op) {
316
317 case 'a':
318 ++aflag;
319 break;
320
321 case 'A':
322 ++xflag;
323 ++Xflag;
324 ++Aflag;
325 break;
326
327 #ifdef WIN32
328 case 'B':
329 UserBufferSize = atoi(optarg)*1024;
330 if (UserBufferSize < 0)
331 error("invalid packet buffer size %s", optarg);
332 break;
333 #endif /* WIN32 */
334
335 case 'c':
336 cnt = atoi(optarg);
337 if (cnt <= 0)
338 error("invalid packet count %s", optarg);
339 break;
340
341 case 'C':
342 Cflag = atoi(optarg) * 1000000;
343 if (Cflag < 0)
344 error("invalid file size %s", optarg);
345 break;
346
347 case 'd':
348 ++dflag;
349 break;
350
351 #ifdef HAVE_PCAP_FINDALLDEVS
352 case 'D':
353 if (pcap_findalldevs(&devpointer, ebuf) < 0)
354 error("%s", ebuf);
355 else {
356 for (i = 0; devpointer != 0; i++) {
357 printf("%d.%s", i+1, devpointer->name);
358 if (devpointer->description != NULL)
359 printf(" (%s)", devpointer->description);
360 printf("\n");
361 devpointer = devpointer->next;
362 }
363 }
364 return 0;
365 #endif /* HAVE_PCAP_FINDALLDEVS */
366
367 case 'L':
368 Lflag++;
369 break;
370
371 case 'e':
372 ++eflag;
373 break;
374
375 case 'E':
376 #ifndef HAVE_LIBCRYPTO
377 warning("crypto code not compiled in");
378 #endif
379 espsecret = optarg;
380 break;
381
382 case 'f':
383 ++fflag;
384 break;
385
386 case 'F':
387 infile = optarg;
388 break;
389
390 case 'i':
391 if (optarg[0] == '0' && optarg[1] == 0)
392 error("Invalid adapter index");
393
394 #ifdef HAVE_PCAP_FINDALLDEVS
395 /*
396 * If the argument is a number, treat it as
397 * an index into the list of adapters, as
398 * printed by "tcpdump -D".
399 *
400 * This should be OK on UNIX systems, as interfaces
401 * shouldn't have names that begin with digits.
402 * It can be useful on Windows, where more than
403 * one interface can have the same name.
404 */
405 if ((devnum = atoi(optarg)) != 0) {
406 if (devnum < 0)
407 error("Invalid adapter index");
408
409 if (pcap_findalldevs(&devpointer, ebuf) < 0)
410 error("%s", ebuf);
411 else {
412 for (i = 0; i < devnum-1; i++){
413 devpointer = devpointer->next;
414 if (devpointer == NULL)
415 error("Invalid adapter index");
416 }
417 }
418 device = devpointer->name;
419 break;
420 }
421 #endif /* HAVE_PCAP_FINDALLDEVS */
422 device = optarg;
423 break;
424
425 case 'l':
426 #ifdef HAVE_SETLINEBUF
427 setlinebuf(stdout);
428 #else
429 setvbuf(stdout, NULL, _IOLBF, 0);
430 #endif
431 break;
432
433 case 'n':
434 ++nflag;
435 break;
436
437 case 'N':
438 ++Nflag;
439 break;
440
441 case 'm':
442 #ifdef LIBSMI
443 if (smiLoadModule(optarg) == 0) {
444 error("could not load MIB module %s", optarg);
445 }
446 sflag = 1;
447 #else
448 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
449 program_name, optarg);
450 (void)fprintf(stderr, "(no libsmi support)\n");
451 #endif
452
453 case 'O':
454 Oflag = 0;
455 break;
456
457 case 'p':
458 ++pflag;
459 break;
460
461 case 'q':
462 ++qflag;
463 break;
464
465 case 'r':
466 RFileName = optarg;
467 break;
468
469 case 'R':
470 Rflag = 0;
471 break;
472
473 case 's': {
474 char *end;
475
476 snaplen = strtol(optarg, &end, 0);
477 if (optarg == end || *end != '\0'
478 || snaplen < 0 || snaplen > 65535)
479 error("invalid snaplen %s", optarg);
480 else if (snaplen == 0)
481 snaplen = 65535;
482 break;
483 }
484
485 case 'S':
486 ++Sflag;
487 break;
488
489 case 't':
490 --tflag;
491 break;
492
493 case 'T':
494 if (strcasecmp(optarg, "vat") == 0)
495 packettype = PT_VAT;
496 else if (strcasecmp(optarg, "wb") == 0)
497 packettype = PT_WB;
498 else if (strcasecmp(optarg, "rpc") == 0)
499 packettype = PT_RPC;
500 else if (strcasecmp(optarg, "rtp") == 0)
501 packettype = PT_RTP;
502 else if (strcasecmp(optarg, "rtcp") == 0)
503 packettype = PT_RTCP;
504 else if (strcasecmp(optarg, "snmp") == 0)
505 packettype = PT_SNMP;
506 else if (strcasecmp(optarg, "cnfp") == 0)
507 packettype = PT_CNFP;
508 else
509 error("unknown packet type `%s'", optarg);
510 break;
511
512 case 'u':
513 ++uflag;
514 break;
515
516 case 'v':
517 ++vflag;
518 break;
519
520 case 'w':
521 WFileName = optarg;
522 break;
523
524 case 'x':
525 ++xflag;
526 break;
527
528 case 'X':
529 ++xflag;
530 ++Xflag;
531 break;
532
533 case 'y':
534 dlt_name = optarg;
535 dlt = pcap_datalink_name_to_val(dlt_name);
536 if (dlt < 0)
537 error("invalid data link type %s", dlt_name);
538 break;
539
540 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
541 case 'Y':
542 {
543 /* Undocumented flag */
544 #ifdef HAVE_PCAP_DEBUG
545 extern int pcap_debug;
546 pcap_debug = 1;
547 #else
548 extern int yydebug;
549 yydebug = 1;
550 #endif
551 }
552 break;
553 #endif
554 default:
555 usage();
556 /* NOTREACHED */
557 }
558
559 if (aflag && nflag)
560 error("-a and -n options are incompatible");
561
562 if (tflag > 0)
563 thiszone = gmt2local(0);
564
565 if (RFileName != NULL) {
566 /*
567 * We don't need network access, so set it back to the user id.
568 * Also, this prevents the user from reading anyone's
569 * trace file.
570 */
571 #ifndef WIN32
572 setuid(getuid());
573 #endif /* WIN32 */
574
575 pd = pcap_open_offline(RFileName, ebuf);
576 if (pd == NULL)
577 error("%s", ebuf);
578 localnet = 0;
579 netmask = 0;
580 if (fflag != 0)
581 error("-f and -r options are incompatible");
582 } else {
583 if (device == NULL) {
584 device = pcap_lookupdev(ebuf);
585 if (device == NULL)
586 error("%s", ebuf);
587 }
588 #ifdef WIN32
589 PrintCapBegins(program_name,device);
590 #endif /* WIN32 */
591 *ebuf = '\0';
592 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
593 if (pd == NULL)
594 error("%s", ebuf);
595 else if (*ebuf)
596 warning("%s", ebuf);
597 #ifdef WIN32
598 if(UserBufferSize != 1000000)
599 if(pcap_setbuff(pd, UserBufferSize)==-1){
600 error("%s", pcap_geterr(pd));
601 }
602 #endif /* WIN32 */
603 if (Lflag)
604 show_dlts_and_exit(pd);
605 if (dlt >= 0) {
606 #ifdef HAVE_PCAP_SET_DATALINK
607 if (pcap_set_datalink(pd, dlt) < 0)
608 error("%s", pcap_geterr(pd));
609 #else
610 /*
611 * We don't actually support changing the
612 * data link type, so we only let them
613 * set it to what it already is.
614 */
615 if (dlt != pcap_datalink(pd)) {
616 error("%s is not one of the DLTs supported by this device\n",
617 dlt_name);
618 }
619 #endif
620 (void)fprintf(stderr, "%s: data link type %s\n",
621 program_name, dlt_name);
622 (void)fflush(stderr);
623 }
624 i = pcap_snapshot(pd);
625 if (snaplen < i) {
626 warning("snaplen raised from %d to %d", snaplen, i);
627 snaplen = i;
628 }
629 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
630 localnet = 0;
631 netmask = 0;
632 warning("%s", ebuf);
633 }
634 /*
635 * Let user own process after socket has been opened.
636 */
637 #ifndef WIN32
638 setuid(getuid());
639 #endif /* WIN32 */
640 }
641 if (infile)
642 cmdbuf = read_infile(infile);
643 else
644 cmdbuf = copy_argv(&argv[optind]);
645
646 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
647 error("%s", pcap_geterr(pd));
648 if (dflag) {
649 bpf_dump(&fcode, dflag);
650 pcap_close(pd);
651 exit(0);
652 }
653 init_addrtoname(localnet, netmask);
654
655 (void)setsignal(SIGTERM, cleanup);
656 (void)setsignal(SIGINT, cleanup);
657 /* Cooperate with nohup(1) */
658 #ifndef WIN32
659 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
660 (void)setsignal(SIGHUP, oldhandler);
661 #endif /* WIN32 */
662
663 if (pcap_setfilter(pd, &fcode) < 0)
664 error("%s", pcap_geterr(pd));
665 if (WFileName) {
666 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
667 if (p == NULL)
668 error("%s", pcap_geterr(pd));
669 if (Cflag != 0) {
670 printer = dump_packet_and_trunc;
671 dumpinfo.WFileName = WFileName;
672 dumpinfo.pd = pd;
673 dumpinfo.p = p;
674 pcap_userdata = (u_char *)&dumpinfo;
675 } else {
676 printer = dump_packet;
677 pcap_userdata = (u_char *)p;
678 }
679 } else {
680 type = pcap_datalink(pd);
681 printer = lookup_printer(type);
682 if (printer == NULL) {
683 dlt_name = pcap_datalink_val_to_name(type);
684 if (dlt_name != NULL)
685 error("unsupported data link type %s", dlt_name);
686 else
687 error("unsupported data link type %d", type);
688 }
689 pcap_userdata = 0;
690 }
691 #ifdef SIGINFO
692 (void)setsignal(SIGINFO, requestinfo);
693 #endif
694 #ifndef WIN32
695 if (RFileName == NULL) {
696 (void)fprintf(stderr, "%s: listening on %s\n",
697 program_name, device);
698 (void)fflush(stderr);
699 }
700 #endif /* WIN32 */
701 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
702 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
703 program_name, pcap_geterr(pd));
704 cleanup(0);
705 pcap_close(pd);
706 exit(1);
707 }
708 if (RFileName == NULL)
709 info(1);
710 pcap_close(pd);
711 exit(0);
712 }
713
714 /* make a clean exit on interrupts */
715 static RETSIGTYPE
716 cleanup(int signo)
717 {
718
719 /* Can't print the summary if reading from a savefile */
720 if (pd != NULL && pcap_file(pd) == NULL) {
721 (void)fflush(stdout);
722 putc('\n', stderr);
723 info(1);
724 }
725 if (signo)
726 exit(0);
727 }
728
729 void
730 info(register int verbose)
731 {
732 struct pcap_stat stat;
733
734 if (pcap_stats(pd, &stat) < 0) {
735 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
736 return;
737 }
738
739 if (!verbose)
740 fprintf(stderr, "%s: ", program_name);
741
742 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
743 if (!verbose)
744 fputs(", ", stderr);
745 else
746 putc('\n', stderr);
747 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
748 infoprint = 0;
749 }
750
751 static void
752 reverse(char *s)
753 {
754 int i, j, c;
755
756 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
757 c = s[i];
758 s[i] = s[j];
759 s[j] = c;
760 }
761 }
762
763
764 static void
765 swebitoa(unsigned int n, char *s)
766 {
767 unsigned int i;
768
769 i = 0;
770 do {
771 s[i++] = n % 10 + '0';
772 } while ((n /= 10) > 0);
773
774 s[i] = '\0';
775 reverse(s);
776 }
777
778 static void
779 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
780 {
781 struct dump_info *dump_info;
782 static uint cnt = 2;
783 char *name;
784
785 ++infodelay;
786
787 dump_info = (struct dump_info *)user;
788
789 /*
790 * XXX - this won't prevent capture files from getting
791 * larger than Cflag - the last packet written to the
792 * file could put it over Cflag.
793 */
794 if (ftell((FILE *)dump_info->p) > Cflag) {
795 name = (char *) malloc(strlen(dump_info->WFileName) + 4);
796 if (name == NULL)
797 error("dump_packet_and_trunc: malloc");
798 strcpy(name, dump_info->WFileName);
799 swebitoa(cnt, name + strlen(dump_info->WFileName));
800 cnt++;
801 pcap_dump_close(dump_info->p);
802 dump_info->p = pcap_dump_open(dump_info->pd, name);
803 free(name);
804 if (dump_info->p == NULL)
805 error("%s", pcap_geterr(pd));
806 }
807
808 pcap_dump((u_char *)dump_info->p, h, sp);
809
810 --infodelay;
811 if (infoprint)
812 info(0);
813 }
814
815 static void
816 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
817 {
818 ++infodelay;
819
820 pcap_dump(user, h, sp);
821
822 --infodelay;
823 if (infoprint)
824 info(0);
825 }
826
827 /* Like default_print() but data need not be aligned */
828 void
829 default_print_unaligned(register const u_char *cp, register u_int length)
830 {
831 register u_int i, s;
832 register int nshorts;
833
834 if (Xflag) {
835 ascii_print(cp, length);
836 return;
837 }
838 nshorts = (u_int) length / sizeof(u_short);
839 i = 0;
840 while (--nshorts >= 0) {
841 if ((i++ % 8) == 0)
842 (void)printf("\n\t\t\t");
843 s = *cp++;
844 (void)printf(" %02x%02x", s, *cp++);
845 }
846 if (length & 1) {
847 if ((i % 8) == 0)
848 (void)printf("\n\t\t\t");
849 (void)printf(" %02x", *cp);
850 }
851 }
852
853 #ifdef WIN32
854 /*
855 * XXX - there should really be libpcap calls to get the version
856 * number as a string (the string would be generated from #defines
857 * at run time, so that it's not generated from string constants
858 * in the library, as, on many UNIX systems, those constants would
859 * be statically linked into the application executable image, and
860 * would thus reflect the version of libpcap on the system on
861 * which the application was *linked*, not the system on which it's
862 * *running*.
863 *
864 * That routine should be documented, unlike the "version[]"
865 * string, so that UNIX vendors providing their own libpcaps
866 * don't omit it (as a couple of vendors have...).
867 *
868 * Packet.dll should perhaps also export a routine to return the
869 * version number of the Packet.dll code, to supply the
870 * "Wpcap_version" information on Windows.
871 */
872 char WDversion[]="current-cvs.tcpdump.org";
873 char version[]="current-cvs.tcpdump.org";
874 char pcap_version[]="current-cvs.tcpdump.org";
875 char Wpcap_version[]="3.0 alpha";
876 #endif
877
878 /*
879 * By default, print the specified data out in hex.
880 */
881 void
882 default_print(register const u_char *bp, register u_int length)
883 {
884 default_print_unaligned(bp, length);
885 }
886
887 /*
888 * By default, print the packet out in hex; if eflag is set, print
889 * everything, otherwise print everything except for the link-layer
890 * header.
891 */
892 void
893 default_print_packet(register const u_char *bp, register u_int length,
894 u_int hdr_length)
895 {
896 if (xflag > 1) {
897 /*
898 * Include the link-layer header.
899 */
900 default_print_unaligned(bp, length);
901 } else {
902 /*
903 * Don't include the link-layer header - and if we have
904 * nothing past the link-layer header, print nothing.
905 */
906 if (length > hdr_length)
907 default_print_unaligned(bp + hdr_length,
908 length - hdr_length);
909 }
910 }
911
912 #ifdef SIGINFO
913 RETSIGTYPE requestinfo(int signo _U_)
914 {
915 if (infodelay)
916 ++infoprint;
917 else
918 info(0);
919 }
920 #endif
921
922 static void
923 usage(void)
924 {
925 extern char version[];
926 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
927 extern char pcap_version[];
928 #else
929 static char pcap_version[] = "unknown";
930 #endif
931
932 #ifdef WIN32
933 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
934 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
935 #else
936 (void)fprintf(stderr, "%s version %s\n", program_name, version);
937 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
938 #endif /* WIN32 */
939 (void)fprintf(stderr,
940 #ifdef WIN32
941 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-B size] [-c count] [ -C file_size ]\n", program_name);
942 #else /* WIN32 */
943 #ifdef HAVE_PCAP_FINDALLDEVS
944 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name);
945 #else /* HAVE_PCAP_FINDALLDEVS */
946 "Usage: %s [-aAdeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name);
947 #endif /* HAVE_PCAP_FINDALLDEVS */
948 #endif /* WIN32 */
949 (void)fprintf(stderr,
950 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
951 (void)fprintf(stderr,
952 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
953 (void)fprintf(stderr,
954 "\t\t[ expression ]\n");
955 exit(1);
956 }