]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
Allow tcpdump to work with earlier libpcaps.
[tcpdump] / tcpdump.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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
22 #ifndef lint
23 static const char copyright[] =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997\n\
25 The Regents of the University of California. All rights reserved.\n";
26 static const char rcsid[] =
27 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.156 2000-12-09 02:58:48 fenner Exp $ (LBL)";
28 #endif
29
30 /*
31 * tcpdump - monitor tcp/ip traffic on an ethernet.
32 *
33 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
34 * Mercilessly hacked and occasionally improved since then via the
35 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44
45 #include <netinet/in.h>
46
47 #include <pcap.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <ctype.h>
54
55
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "machdep.h"
59 #include "setsignal.h"
60 #include "gmt2local.h"
61
62 int aflag; /* translate network and broadcast addresses */
63 int dflag; /* print filter code */
64 int eflag; /* print ethernet header */
65 int fflag; /* don't translate "foreign" IP address */
66 int nflag; /* leave addresses as numbers */
67 int Nflag; /* remove domains from printed host names */
68 int Oflag = 1; /* run filter code optimizer */
69 int pflag; /* don't go promiscuous */
70 int qflag; /* quick (shorter) output */
71 int Rflag = 1; /* print sequence # field in AH/ESP*/
72 int sflag = 0; /* use the libsmi to translate OIDs */
73 int Sflag; /* print raw TCP sequence numbers */
74 int tflag = 1; /* print packet arrival time */
75 int uflag = 0; /* Print undecoded NFS handles */
76 int vflag; /* verbose */
77 int xflag; /* print packet in hex */
78 int Xflag; /* print packet in ascii as well as hex */
79
80 char *espsecret = NULL; /* ESP secret key */
81
82 int packettype;
83
84
85 char *program_name;
86
87 int32_t thiszone; /* seconds offset from gmt to local time */
88
89 /* Forwards */
90 static RETSIGTYPE cleanup(int);
91 static void usage(void) __attribute__((noreturn));
92
93 /* Length of saved portion of packet. */
94 int snaplen = DEFAULT_SNAPLEN;
95
96 struct printer {
97 pcap_handler f;
98 int type;
99 };
100
101 static struct printer printers[] = {
102 { ether_if_print, DLT_EN10MB },
103 { token_if_print, DLT_IEEE802 },
104 #ifdef DLT_LANE8023
105 { lane_if_print, DLT_LANE8023 },
106 #endif
107 #ifdef DLT_CIP
108 { cip_if_print, DLT_CIP },
109 #endif
110 #ifdef DLT_ATM_CLIP
111 { cip_if_print, DLT_ATM_CLIP },
112 #endif
113 { sl_if_print, DLT_SLIP },
114 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
115 { ppp_if_print, DLT_PPP },
116 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
117 { fddi_if_print, DLT_FDDI },
118 { null_if_print, DLT_NULL },
119 { raw_if_print, DLT_RAW },
120 { atm_if_print, DLT_ATM_RFC1483 },
121 #ifdef DLT_C_HDLC
122 { chdlc_if_print, DLT_C_HDLC },
123 #endif
124 #ifdef DLT_PPP_SERIAL
125 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
126 #endif
127 { NULL, 0 },
128 };
129
130 static pcap_handler
131 lookup_printer(int type)
132 {
133 struct printer *p;
134
135 for (p = printers; p->f; ++p)
136 if (type == p->type)
137 return p->f;
138
139 error("unknown data link type %d", type);
140 /* NOTREACHED */
141 }
142
143 static pcap_t *pd;
144
145 extern int optind;
146 extern int opterr;
147 extern char *optarg;
148
149 int
150 main(int argc, char **argv)
151 {
152 register int cnt, op, i;
153 bpf_u_int32 localnet, netmask;
154 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
155 pcap_handler printer;
156 struct bpf_program fcode;
157 RETSIGTYPE (*oldhandler)(int);
158 u_char *pcap_userdata;
159 char ebuf[PCAP_ERRBUF_SIZE];
160
161 cnt = -1;
162 device = NULL;
163 infile = NULL;
164 RFileName = NULL;
165 WFileName = NULL;
166 if ((cp = strrchr(argv[0], '/')) != NULL)
167 program_name = cp + 1;
168 else
169 program_name = argv[0];
170
171 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
172 error("%s", ebuf);
173
174 #ifdef LIBSMI
175 smiInit("tcpdump");
176 #endif
177
178 opterr = 0;
179 while (
180 (op = getopt(argc, argv, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
181 switch (op) {
182
183 case 'a':
184 ++aflag;
185 break;
186
187 case 'c':
188 cnt = atoi(optarg);
189 if (cnt <= 0)
190 error("invalid packet count %s", optarg);
191 break;
192
193 case 'd':
194 ++dflag;
195 break;
196
197 case 'e':
198 ++eflag;
199 break;
200
201 case 'E':
202 #ifndef HAVE_LIBCRYPTO
203 warning("crypto code not compiled in");
204 #endif
205 espsecret = optarg;
206 break;
207
208 case 'f':
209 ++fflag;
210 break;
211
212 case 'F':
213 infile = optarg;
214 break;
215
216 case 'i':
217 device = optarg;
218 break;
219
220 case 'l':
221 #ifdef HAVE_SETLINEBUF
222 setlinebuf(stdout);
223 #else
224 setvbuf(stdout, NULL, _IOLBF, 0);
225 #endif
226 break;
227
228 case 'n':
229 ++nflag;
230 break;
231
232 case 'N':
233 ++Nflag;
234 break;
235
236 case 'm':
237 #ifdef LIBSMI
238 if (smiLoadModule(optarg) == 0) {
239 error("could not load MIB module %s", optarg);
240 }
241 sflag = 1;
242 #else
243 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
244 program_name, optarg);
245 (void)fprintf(stderr, "(no libsmi support)\n");
246 #endif
247
248 case 'O':
249 Oflag = 0;
250 break;
251
252 case 'p':
253 ++pflag;
254 break;
255
256 case 'q':
257 ++qflag;
258 break;
259
260 case 'r':
261 RFileName = optarg;
262 break;
263
264 case 'R':
265 Rflag = 0;
266 break;
267
268 case 's': {
269 char *end;
270
271 snaplen = strtol(optarg, &end, 0);
272 if (optarg == end || *end != '\0'
273 || snaplen < 0 || snaplen > 65535)
274 error("invalid snaplen %s", optarg);
275 else if (snaplen == 0)
276 snaplen = 65535;
277 break;
278 }
279
280 case 'S':
281 ++Sflag;
282 break;
283
284 case 't':
285 --tflag;
286 break;
287
288 case 'T':
289 if (strcasecmp(optarg, "vat") == 0)
290 packettype = PT_VAT;
291 else if (strcasecmp(optarg, "wb") == 0)
292 packettype = PT_WB;
293 else if (strcasecmp(optarg, "rpc") == 0)
294 packettype = PT_RPC;
295 else if (strcasecmp(optarg, "rtp") == 0)
296 packettype = PT_RTP;
297 else if (strcasecmp(optarg, "rtcp") == 0)
298 packettype = PT_RTCP;
299 else if (strcasecmp(optarg, "snmp") == 0)
300 packettype = PT_SNMP;
301 else if (strcasecmp(optarg, "cnfp") == 0)
302 packettype = PT_CNFP;
303 else
304 error("unknown packet type `%s'", optarg);
305 break;
306
307 case 'u':
308 ++uflag;
309 break;
310
311 case 'v':
312 ++vflag;
313 break;
314
315 case 'w':
316 WFileName = optarg;
317 break;
318
319 case 'x':
320 ++xflag;
321 break;
322
323 case 'X':
324 ++xflag;
325 ++Xflag;
326 break;
327
328 #ifdef YYDEBUG
329 case 'Y':
330 {
331 /* Undocumented flag */
332 extern int yydebug;
333 yydebug = 1;
334 }
335 break;
336 #endif
337 default:
338 usage();
339 /* NOTREACHED */
340 }
341
342 if (aflag && nflag)
343 error("-a and -n options are incompatible");
344
345 if (tflag > 0)
346 thiszone = gmt2local(0);
347
348 if (RFileName != NULL) {
349 /*
350 * We don't need network access, so set it back to the user id.
351 * Also, this prevents the user from reading anyone's
352 * trace file.
353 */
354 setuid(getuid());
355
356 pd = pcap_open_offline(RFileName, ebuf);
357 if (pd == NULL)
358 error("%s", ebuf);
359 localnet = 0;
360 netmask = 0;
361 if (fflag != 0)
362 error("-f and -r options are incompatible");
363 } else {
364 if (device == NULL) {
365 device = pcap_lookupdev(ebuf);
366 if (device == NULL)
367 error("%s", ebuf);
368 }
369 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
370 if (pd == NULL)
371 error("%s", ebuf);
372 i = pcap_snapshot(pd);
373 if (snaplen < i) {
374 warning("snaplen raised from %d to %d", snaplen, i);
375 snaplen = i;
376 }
377 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
378 localnet = 0;
379 netmask = 0;
380 warning("%s", ebuf);
381 }
382 /*
383 * Let user own process after socket has been opened.
384 */
385 setuid(getuid());
386 }
387 if (infile)
388 cmdbuf = read_infile(infile);
389 else
390 cmdbuf = copy_argv(&argv[optind]);
391
392 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
393 error("%s", pcap_geterr(pd));
394 if (dflag) {
395 bpf_dump(&fcode, dflag);
396 exit(0);
397 }
398 init_addrtoname(localnet, netmask);
399
400 (void)setsignal(SIGTERM, cleanup);
401 (void)setsignal(SIGINT, cleanup);
402 /* Cooperate with nohup(1) */
403 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
404 (void)setsignal(SIGHUP, oldhandler);
405
406 if (pcap_setfilter(pd, &fcode) < 0)
407 error("%s", pcap_geterr(pd));
408 if (WFileName) {
409 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
410 if (p == NULL)
411 error("%s", pcap_geterr(pd));
412 printer = pcap_dump;
413 pcap_userdata = (u_char *)p;
414 } else {
415 printer = lookup_printer(pcap_datalink(pd));
416 pcap_userdata = 0;
417 }
418 if (RFileName == NULL) {
419 (void)fprintf(stderr, "%s: listening on %s\n",
420 program_name, device);
421 (void)fflush(stderr);
422 }
423 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
424 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
425 program_name, pcap_geterr(pd));
426 exit(1);
427 }
428 pcap_close(pd);
429 exit(0);
430 }
431
432 /* make a clean exit on interrupts */
433 static RETSIGTYPE
434 cleanup(int signo)
435 {
436 struct pcap_stat stat;
437
438 /* Can't print the summary if reading from a savefile */
439 if (pd != NULL && pcap_file(pd) == NULL) {
440 (void)fflush(stdout);
441 putc('\n', stderr);
442 if (pcap_stats(pd, &stat) < 0)
443 (void)fprintf(stderr, "pcap_stats: %s\n",
444 pcap_geterr(pd));
445 else {
446 (void)fprintf(stderr, "%d packets received by filter\n",
447 stat.ps_recv);
448 (void)fprintf(stderr, "%d packets dropped by kernel\n",
449 stat.ps_drop);
450 }
451 }
452 exit(0);
453 }
454
455 /* Like default_print() but data need not be aligned */
456 void
457 default_print_unaligned(register const u_char *cp, register u_int length)
458 {
459 register u_int i, s;
460 register int nshorts;
461
462 if (Xflag) {
463 ascii_print(cp, length);
464 return;
465 }
466 nshorts = (u_int) length / sizeof(u_short);
467 i = 0;
468 while (--nshorts >= 0) {
469 if ((i++ % 8) == 0)
470 (void)printf("\n\t\t\t");
471 s = *cp++;
472 (void)printf(" %02x%02x", s, *cp++);
473 }
474 if (length & 1) {
475 if ((i % 8) == 0)
476 (void)printf("\n\t\t\t");
477 (void)printf(" %02x", *cp);
478 }
479 }
480
481 /*
482 * By default, print the packet out in hex.
483 */
484 void
485 default_print(register const u_char *bp, register u_int length)
486 {
487 default_print_unaligned(bp, length);
488 }
489
490 static void
491 usage(void)
492 {
493 extern char version[];
494 extern char pcap_version[];
495
496 (void)fprintf(stderr, "%s version %s\n", program_name, version);
497 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
498 (void)fprintf(stderr,
499 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name);
500 (void)fprintf(stderr,
501 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
502 (void)fprintf(stderr,
503 "\t\t[ -T type ] [ -w file ] [ expression ]\n");
504 exit(-1);
505 }