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