tcpdump.c (44165) | tcpdump.c (56648) |
---|---|
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 --- 13 unchanged lines hidden (view full) --- 22#ifndef lint 23static const char copyright[] = 24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997\n\ 25The Regents of the University of California. All rights reserved.\n"; 26static const char rcsid[] = 27 "@(#) $Header: tcpdump.c,v 1.129 97/06/13 13:10:11 leres Exp $ (LBL)"; 28#endif 29 | 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 --- 13 unchanged lines hidden (view full) --- 22#ifndef lint 23static const char copyright[] = 24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997\n\ 25The Regents of the University of California. All rights reserved.\n"; 26static const char rcsid[] = 27 "@(#) $Header: tcpdump.c,v 1.129 97/06/13 13:10:11 leres Exp $ (LBL)"; 28#endif 29 |
30/* $FreeBSD: head/contrib/tcpdump/tcpdump.c 56648 2000-01-26 18:10:21Z archie $ */ 31 |
|
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 --- 23 unchanged lines hidden (view full) --- 61int Nflag; /* remove domains from printed host names */ 62int Oflag = 1; /* run filter code optimizer */ 63int pflag; /* don't go promiscuous */ 64int qflag; /* quick (shorter) output */ 65int Sflag; /* print raw TCP sequence numbers */ 66int tflag = 1; /* print packet arrival time */ 67int vflag; /* verbose */ 68int xflag; /* print packet in hex */ | 32/* 33 * tcpdump - monitor tcp/ip traffic on an ethernet. 34 * 35 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory. 36 * Mercilessly hacked and occasionally improved since then via the 37 * combined efforts of Van, Steve McCanne and Craig Leres of LBL. 38 */ 39 --- 23 unchanged lines hidden (view full) --- 63int Nflag; /* remove domains from printed host names */ 64int Oflag = 1; /* run filter code optimizer */ 65int pflag; /* don't go promiscuous */ 66int qflag; /* quick (shorter) output */ 67int Sflag; /* print raw TCP sequence numbers */ 68int tflag = 1; /* print packet arrival time */ 69int vflag; /* verbose */ 70int xflag; /* print packet in hex */ |
71int Xflag; /* print packet in emacs-hexl style */ |
|
69 70int packettype; 71 72 73char *program_name; 74 75int32_t thiszone; /* seconds offset from gmt to local time */ 76 --- 67 unchanged lines hidden (view full) --- 144 else 145 program_name = argv[0]; 146 147 if (abort_on_misalignment(ebuf) < 0) 148 error("%s", ebuf); 149 150 opterr = 0; 151 while ( | 72 73int packettype; 74 75 76char *program_name; 77 78int32_t thiszone; /* seconds offset from gmt to local time */ 79 --- 67 unchanged lines hidden (view full) --- 147 else 148 program_name = argv[0]; 149 150 if (abort_on_misalignment(ebuf) < 0) 151 error("%s", ebuf); 152 153 opterr = 0; 154 while ( |
152 (op = getopt(argc, argv, "ac:defF:i:lnNOpqr:s:StT:vw:xY")) != EOF) | 155 (op = getopt(argc, argv, "ac:defF:i:lnNOpqr:s:StT:vw:xXY")) != EOF) |
153 switch (op) { 154 155 case 'a': 156 ++aflag; 157 break; 158 159 case 'c': 160 cnt = atoi(optarg); --- 97 unchanged lines hidden (view full) --- 258 yydebug = 1; 259 } 260 break; 261#endif 262 case 'x': 263 ++xflag; 264 break; 265 | 156 switch (op) { 157 158 case 'a': 159 ++aflag; 160 break; 161 162 case 'c': 163 cnt = atoi(optarg); --- 97 unchanged lines hidden (view full) --- 261 yydebug = 1; 262 } 263 break; 264#endif 265 case 'x': 266 ++xflag; 267 break; 268 |
269 case 'X': 270 ++Xflag; 271 if (xflag == 0) ++xflag; 272 break; 273 |
|
266 default: 267 usage(); 268 /* NOTREACHED */ 269 } 270 271 if (aflag && nflag) 272 error("-a and -n options are incompatible"); 273 --- 102 unchanged lines hidden (view full) --- 376 stat.ps_recv); 377 (void)fprintf(stderr, "%d packets dropped by kernel\n", 378 stat.ps_drop); 379 } 380 } 381 exit(0); 382} 383 | 274 default: 275 usage(); 276 /* NOTREACHED */ 277 } 278 279 if (aflag && nflag) 280 error("-a and -n options are incompatible"); 281 --- 102 unchanged lines hidden (view full) --- 384 stat.ps_recv); 385 (void)fprintf(stderr, "%d packets dropped by kernel\n", 386 stat.ps_drop); 387 } 388 } 389 exit(0); 390} 391 |
392/* dump the buffer in `emacs-hexl' style */ 393void 394default_print_hexl(const u_char *cp, unsigned int length, unsigned int offset) 395{ 396 unsigned int i, j, jm; 397 int c; 398 char ln[128]; 399 400 printf("\n"); 401 for (i = 0; i < length; i += 0x10) { 402 snprintf(ln, 403 sizeof(ln), 404 " %04x: ", (unsigned int)(i + offset)); 405 jm = length - i; 406 jm = jm > 16 ? 16 : jm; 407 408 for (j = 0; j < jm; j++) { 409 if ((j % 2) == 1) 410 snprintf(ln + strlen(ln), 411 sizeof(ln) - strlen(ln), 412 "%02x ", (unsigned int)cp[i+j]); 413 else 414 snprintf(ln + strlen(ln), 415 sizeof(ln) - strlen(ln), 416 "%02x", (unsigned int)cp[i+j]); 417 } 418 for (; j < 16; j++) { 419 if ((j % 2) == 1) 420 snprintf(ln + strlen(ln), 421 sizeof(ln) - strlen(ln), 422 " "); 423 else 424 snprintf(ln + strlen(ln), 425 sizeof(ln) - strlen(ln), 426 " "); 427 } 428 429 snprintf(ln + strlen(ln), sizeof(ln) - strlen(ln), " "); 430 for (j = 0; j < jm; j++) { 431 c = cp[i+j]; 432 c = isprint(c) ? c : '.'; 433 snprintf(ln + strlen(ln), 434 sizeof(ln) - strlen(ln), 435 "%c", c); 436 } 437 printf("%s\n", ln); 438 } 439} 440 |
|
384/* Like default_print() but data need not be aligned */ 385void 386default_print_unaligned(register const u_char *cp, register u_int length) 387{ 388 register u_int i, s; 389 register int nshorts; 390 | 441/* Like default_print() but data need not be aligned */ 442void 443default_print_unaligned(register const u_char *cp, register u_int length) 444{ 445 register u_int i, s; 446 register int nshorts; 447 |
391 nshorts = (u_int) length / sizeof(u_short); 392 i = 0; 393 while (--nshorts >= 0) { 394 if ((i++ % 8) == 0) 395 (void)printf("\n\t\t\t"); 396 s = *cp++; 397 (void)printf(" %02x%02x", s, *cp++); | 448 if (Xflag) { 449 /* dump the buffer in `emacs-hexl' style */ 450 default_print_hexl(cp, length, 0); 451 } else { 452 /* dump the buffer in old tcpdump style */ 453 nshorts = (u_int) length / sizeof(u_short); 454 i = 0; 455 while (--nshorts >= 0) { 456 if ((i++ % 8) == 0) 457 (void)printf("\n\t\t\t"); 458 s = *cp++; 459 (void)printf(" %02x%02x", s, *cp++); 460 } 461 if (length & 1) { 462 if ((i % 8) == 0) 463 (void)printf("\n\t\t\t"); 464 (void)printf(" %02x", *cp); 465 } |
398 } | 466 } |
399 if (length & 1) { 400 if ((i % 8) == 0) 401 (void)printf("\n\t\t\t"); 402 (void)printf(" %02x", *cp); 403 } | |
404} 405 406/* 407 * By default, print the packet out in hex. 408 * 409 * (BTW, please don't send us patches to print the packet out in ascii) 410 */ 411void 412default_print(register const u_char *bp, register u_int length) 413{ 414 register const u_short *sp; 415 register u_int i; 416 register int nshorts; 417 | 467} 468 469/* 470 * By default, print the packet out in hex. 471 * 472 * (BTW, please don't send us patches to print the packet out in ascii) 473 */ 474void 475default_print(register const u_char *bp, register u_int length) 476{ 477 register const u_short *sp; 478 register u_int i; 479 register int nshorts; 480 |
418 if ((long)bp & 1) { 419 default_print_unaligned(bp, length); 420 return; | 481 if (Xflag) { 482 /* dump the buffer in `emacs-hexl' style */ 483 default_print_hexl(bp, length, 0); 484 } else { 485 /* dump the buffer in old tcpdump style */ 486 if ((long)bp & 1) { 487 default_print_unaligned(bp, length); 488 return; 489 } 490 sp = (u_short *)bp; 491 nshorts = (u_int) length / sizeof(u_short); 492 i = 0; 493 while (--nshorts >= 0) { 494 if ((i++ % 8) == 0) 495 (void)printf("\n\t\t\t"); 496 (void)printf(" %04x", ntohs(*sp++)); 497 } 498 if (length & 1) { 499 if ((i % 8) == 0) 500 (void)printf("\n\t\t\t"); 501 (void)printf(" %02x", *(u_char *)sp); 502 } |
421 } | 503 } |
422 sp = (u_short *)bp; 423 nshorts = (u_int) length / sizeof(u_short); 424 i = 0; 425 while (--nshorts >= 0) { 426 if ((i++ % 8) == 0) 427 (void)printf("\n\t\t\t"); 428 (void)printf(" %04x", ntohs(*sp++)); 429 } 430 if (length & 1) { 431 if ((i % 8) == 0) 432 (void)printf("\n\t\t\t"); 433 (void)printf(" %02x", *(u_char *)sp); 434 } | |
435} 436 437__dead void 438usage(void) 439{ 440 extern char version[]; 441 extern char pcap_version[]; 442 443 (void)fprintf(stderr, "%s version %s\n", program_name, version); 444 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 445 (void)fprintf(stderr, | 504} 505 506__dead void 507usage(void) 508{ 509 extern char version[]; 510 extern char pcap_version[]; 511 512 (void)fprintf(stderr, "%s version %s\n", program_name, version); 513 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 514 (void)fprintf(stderr, |
446"Usage: %s [-adeflnNOpqStvx] [-c count] [ -F file ]\n", program_name); | 515"Usage: %s [-adeflnNOpqStvxX] [-c count] [ -F file ]\n", program_name); |
447 (void)fprintf(stderr, 448"\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n"); 449 (void)fprintf(stderr, 450"\t\t[ -T type ] [ -w file ] [ expression ]\n"); 451 exit(-1); 452} | 516 (void)fprintf(stderr, 517"\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n"); 518 (void)fprintf(stderr, 519"\t\t[ -T type ] [ -w file ] [ expression ]\n"); 520 exit(-1); 521} |