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