1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
/*
* Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "varattrs.h"
#ifndef lint
static const char copyright[] _U_ =
"@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#ifdef _WIN32
#include "getopt.h"
#else
#include <unistd.h>
#endif
#include <errno.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include <sys/types.h>
#include <pcap.h>
#include "pcap/funcattrs.h"
#ifdef _WIN32
#include "portability.h"
#endif
static char *program_name;
/* Forwards */
static void PCAP_NORETURN usage(void);
static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
static char *copy_argv(char **);
static pcap_t *pd;
#ifdef _WIN32
static BOOL WINAPI
stop_capture(DWORD ctrltype _U_)
{
pcap_breakloop(pd);
return TRUE;
}
#else
static void
stop_capture(int signum _U_)
{
pcap_breakloop(pd);
}
#endif
static long
parse_interface_number(const char *device)
{
const char *p;
long devnum;
char *end;
/*
* Search for a colon, terminating any scheme at the beginning
* of the device.
*/
p = strchr(device, ':');
if (p != NULL) {
/*
* We found it. Is it followed by "//"?
*/
p++; /* skip the : */
if (strncmp(p, "//", 2) == 0) {
/*
* Yes. Search for the next /, at the end of the
* authority part of the URL.
*/
p += 2; /* skip the // */
p = strchr(p, '/');
if (p != NULL) {
/*
* OK, past the / is the path.
*/
device = p + 1;
}
}
}
devnum = strtol(device, &end, 10);
if (device != end && *end == '\0') {
/*
* It's all-numeric, but is it a valid number?
*/
if (devnum <= 0) {
/*
* No, it's not an ordinal.
*/
error("Invalid adapter index");
}
return (devnum);
} else {
/*
* It's not all-numeric; return -1, so our caller
* knows that.
*/
return (-1);
}
}
static char *
find_interface_by_number(long devnum)
{
pcap_if_t *dev, *devlist;
long i;
char ebuf[PCAP_ERRBUF_SIZE];
char *device;
int status;
status = pcap_findalldevs(&devlist, ebuf);
if (status < 0)
error("%s", ebuf);
/*
* Look for the devnum-th entry in the list of devices (1-based).
*/
for (i = 0, dev = devlist; i < devnum-1 && dev != NULL;
i++, dev = dev->next)
;
if (dev == NULL)
error("Invalid adapter index");
device = strdup(dev->name);
pcap_freealldevs(devlist);
return (device);
}
static pcap_t *
open_interface(const char *device, int snaplen_set, int snaplen, char *ebuf)
{
pcap_t *pc;
int status;
char *cp;
pc = pcap_create(device, ebuf);
if (pc == NULL) {
/*
* If this failed with "No such device", that means
* the interface doesn't exist; return NULL, so that
* the caller can see whether the device name is
* actually an interface index.
*/
if (strstr(ebuf, "No such device") != NULL)
return (NULL);
error("%s", ebuf);
}
if (snaplen_set) {
status = pcap_set_snaplen(pc, snaplen);
if (status != 0)
error("%s: pcap_set_snaplen failed: %s",
device, pcap_statustostr(status));
}
status = pcap_set_timeout(pc, 100);
if (status != 0)
error("%s: pcap_set_timeout failed: %s",
device, pcap_statustostr(status));
status = pcap_activate(pc);
if (status < 0) {
/*
* pcap_activate() failed.
*/
cp = pcap_geterr(pc);
if (status == PCAP_ERROR)
error("%s", cp);
else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
/*
* Return an error for our caller to handle.
*/
snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
device, pcap_statustostr(status), cp);
} else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
error("%s: %s\n(%s)", device,
pcap_statustostr(status), cp);
else
error("%s: %s", device,
pcap_statustostr(status));
pcap_close(pc);
return (NULL);
} else if (status > 0) {
/*
* pcap_activate() succeeded, but it's warning us
* of a problem it had.
*/
cp = pcap_geterr(pc);
if (status == PCAP_WARNING)
warning("%s", cp);
else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
*cp != '\0')
warning("%s: %s\n(%s)", device,
pcap_statustostr(status), cp);
else
warning("%s: %s", device,
pcap_statustostr(status));
}
return (pc);
}
#define COMMAND_OPTIONS "DLi:s:w:y:"
int
main(int argc, char **argv)
{
int op;
char *cp, *cmdbuf = NULL, *device, *end, *savefile = NULL;
int snaplen = 0;
int snaplen_set = 0;
pcap_if_t *devlist;
long devnum;
int show_interfaces = 0;
int show_dlt_types = 0;
int ndlts;
int *dlts;
bpf_u_int32 localnet, netmask;
struct bpf_program fcode;
char ebuf[PCAP_ERRBUF_SIZE];
#ifndef _WIN32
struct sigaction action;
#endif
int dlt;
const char *dlt_name = NULL;
int status;
pcap_dumper_t *pdd;
device = NULL;
if ((cp = strrchr(argv[0], '/')) != NULL)
program_name = cp + 1;
else
program_name = argv[0];
opterr = 0;
while ((op = getopt(argc, argv, COMMAND_OPTIONS)) != -1) {
switch (op) {
case 'D':
show_interfaces = 1;
break;
case 'L':
show_dlt_types = 1;
break;
case 'i':
device = optarg;
break;
case 's':
snaplen = (int)strtol(optarg, &end, 0);
if (optarg == end || *end != '\0' || snaplen < 0)
error("invalid snaplen %s (must be >= 0)",
optarg);
snaplen_set = 1;
break;
case 'w':
savefile = optarg;
break;
case 'y':
dlt_name = optarg;
break;
default:
usage();
/* NOTREACHED */
}
}
if (show_interfaces) {
pcap_if_t *dev;
int i;
if (pcap_findalldevs(&devlist, ebuf) < 0)
error("%s", ebuf);
for (i = 0, dev = devlist; dev != NULL; i++, dev = dev->next) {
printf("%d.%s", i+1, dev->name);
if (dev->description != NULL)
printf(" (%s)", dev->description);
printf("\n");
}
pcap_freealldevs(devlist);
return (0);
}
if (device == NULL) {
if (pcap_findalldevs(&devlist, ebuf) == -1)
error("%s", ebuf);
if (devlist == NULL)
error("no interfaces available for capture");
device = strdup(devlist->name);
pcap_freealldevs(devlist);
}
if (show_dlt_types) {
pd = pcap_create(device, ebuf);
if (pd == NULL)
error("%s", ebuf);
status = pcap_activate(pd);
if (status < 0) {
/*
* pcap_activate() failed.
*/
error("%s: %s\n(%s)", device,
pcap_statustostr(status), pcap_geterr(pd));
}
ndlts = pcap_list_datalinks(pd, &dlts);
if (ndlts < 0) {
/*
* pcap_list_datalinks() failed.
*/
error("%s: %s\n(%s)", device,
pcap_statustostr(status), pcap_geterr(pd));
}
for (int i = 0; i < ndlts; i++) {
dlt_name = pcap_datalink_val_to_name(dlts[i]);
if (dlt_name == NULL)
printf("DLT %d", dlts[i]);
else
printf("%s", dlt_name);
printf("\n");
}
pcap_free_datalinks(dlts);
pcap_close(pd);
return 0;
}
if (savefile == NULL)
error("no savefile specified");
*ebuf = '\0';
pd = open_interface(device, snaplen_set, snaplen, ebuf);
if (pd == NULL) {
/*
* That failed because the interface couldn't be found.
*
* If we can get a list of interfaces, and the interface name
* is purely numeric, try to use it as a 1-based index
* in the list of interfaces.
*/
devnum = parse_interface_number(device);
if (devnum == -1) {
/*
* It's not a number; just report
* the open error and fail.
*/
error("%s", ebuf);
}
/*
* OK, it's a number; try to find the
* interface with that index, and try
* to open it.
*
* find_interface_by_number() exits if it
* couldn't be found.
*/
device = find_interface_by_number(devnum);
pd = open_interface(device, snaplen_set, snaplen, ebuf);
if (pd == NULL)
error("%s", ebuf);
}
if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
localnet = 0;
netmask = 0;
warning("%s", ebuf);
}
if (dlt_name != NULL) {
dlt = pcap_datalink_name_to_val(dlt_name);
if (dlt == PCAP_ERROR)
error("%s isn't a valid DLT name", dlt_name);
if (pcap_set_datalink(pd, dlt) == PCAP_ERROR)
error("%s: %s", device, pcap_geterr(pd));
}
/*
* Don't set a filter unless we were given one on the
* command line; if capturing doesn't work, or doesn't
* use the snapshot length, without a filter, that's
* a bug.
*/
if (optind < argc) {
cmdbuf = copy_argv(&argv[optind]);
if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
error("%s", pcap_geterr(pd));
if (pcap_setfilter(pd, &fcode) < 0)
error("%s", pcap_geterr(pd));
}
pdd = pcap_dump_open(pd, savefile);
if (pdd == NULL)
error("%s", pcap_geterr(pd));
#ifdef _WIN32
SetConsoleCtrlHandler(stop_capture, TRUE);
#else
action.sa_handler = stop_capture;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGINT, &action, NULL) == -1)
error("Can't catch SIGINT: %s\n", strerror(errno));
#endif
printf("Listening on %s, link-type ", device);
dlt = pcap_datalink(pd);
dlt_name = pcap_datalink_val_to_name(dlt);
if (dlt_name == NULL)
printf("DLT %d", dlt);
else
printf("%s", dlt_name);
printf("\n");
for (;;) {
status = pcap_dispatch(pd, -1, pcap_dump, (u_char *)pdd);
if (status < 0)
break;
if (status != 0) {
printf("%d packets seen\n", status);
struct pcap_stat ps;
pcap_stats(pd, &ps);
printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
}
}
if (status == -2) {
/*
* We got interrupted, so perhaps we didn't
* manage to finish a line we were printing.
* Print an extra newline, just in case.
*/
putchar('\n');
printf("Broken out of loop from SIGINT handler\n");
}
(void)fflush(stdout);
if (status == -1) {
/*
* Error. Report it.
*/
(void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
program_name, pcap_geterr(pd));
}
pcap_close(pd);
if (cmdbuf != NULL) {
pcap_freecode(&fcode);
free(cmdbuf);
}
exit(status == -1 ? 1 : 0);
}
static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s -D -L [ -i interface ] [ -s snaplen ] [ -w file ] [ -y dlt ] [expression]\n",
program_name);
exit(1);
}
/* VARARGS */
static void
error(const char *fmt, ...)
{
va_list ap;
(void)fprintf(stderr, "%s: ", program_name);
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
if (*fmt) {
fmt += strlen(fmt);
if (fmt[-1] != '\n')
(void)fputc('\n', stderr);
}
exit(1);
/* NOTREACHED */
}
/* VARARGS */
static void
warning(const char *fmt, ...)
{
va_list ap;
(void)fprintf(stderr, "%s: WARNING: ", program_name);
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
if (*fmt) {
fmt += strlen(fmt);
if (fmt[-1] != '\n')
(void)fputc('\n', stderr);
}
}
/*
* Copy arg vector into a new buffer, concatenating arguments with spaces.
*/
static char *
copy_argv(register char **argv)
{
register char **p;
register size_t len = 0;
char *buf;
char *src, *dst;
p = argv;
if (*p == 0)
return 0;
while (*p)
len += strlen(*p++) + 1;
buf = (char *)malloc(len);
if (buf == NULL)
error("copy_argv: malloc");
p = argv;
dst = buf;
while ((src = *p++) != NULL) {
while ((*dst++ = *src++) != '\0')
;
dst[-1] = ' ';
}
dst[-1] = '\0';
return buf;
}
|