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.
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
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.
25 static const char copyright
[] _U_
=
26 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27 The Regents of the University of California. All rights reserved.\n";
44 #include <sys/types.h>
48 #include "pcap/funcattrs.h"
51 #include "portability.h"
54 static char *program_name
;
57 static void PCAP_NORETURN
usage(void);
58 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
59 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static char *copy_argv(char **);
66 stop_capture(DWORD ctrltype _U_
)
73 stop_capture(int signum _U_
)
80 parse_interface_number(const char *device
)
87 * Search for a colon, terminating any scheme at the beginning
90 p
= strchr(device
, ':');
93 * We found it. Is it followed by "//"?
96 if (strncmp(p
, "//", 2) == 0) {
98 * Yes. Search for the next /, at the end of the
99 * authority part of the URL.
101 p
+= 2; /* skip the // */
105 * OK, past the / is the path.
111 devnum
= strtol(device
, &end
, 10);
112 if (device
!= end
&& *end
== '\0') {
114 * It's all-numeric, but is it a valid number?
118 * No, it's not an ordinal.
120 error("Invalid adapter index");
125 * It's not all-numeric; return -1, so our caller
133 find_interface_by_number(long devnum
)
135 pcap_if_t
*dev
, *devlist
;
137 char ebuf
[PCAP_ERRBUF_SIZE
];
141 status
= pcap_findalldevs(&devlist
, ebuf
);
145 * Look for the devnum-th entry in the list of devices (1-based).
147 for (i
= 0, dev
= devlist
; i
< devnum
-1 && dev
!= NULL
;
148 i
++, dev
= dev
->next
)
151 error("Invalid adapter index");
152 device
= strdup(dev
->name
);
153 pcap_freealldevs(devlist
);
158 open_interface(const char *device
, int snaplen_set
, int snaplen
, char *ebuf
)
164 pc
= pcap_create(device
, ebuf
);
167 * If this failed with "No such device", that means
168 * the interface doesn't exist; return NULL, so that
169 * the caller can see whether the device name is
170 * actually an interface index.
172 if (strstr(ebuf
, "No such device") != NULL
)
177 status
= pcap_set_snaplen(pc
, snaplen
);
179 error("%s: pcap_set_snaplen failed: %s",
180 device
, pcap_statustostr(status
));
182 status
= pcap_set_timeout(pc
, 100);
184 error("%s: pcap_set_timeout failed: %s",
185 device
, pcap_statustostr(status
));
186 status
= pcap_activate(pc
);
189 * pcap_activate() failed.
191 cp
= pcap_geterr(pc
);
192 if (status
== PCAP_ERROR
)
194 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
) {
196 * Return an error for our caller to handle.
198 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s\n(%s)",
199 device
, pcap_statustostr(status
), cp
);
200 } else if (status
== PCAP_ERROR_PERM_DENIED
&& *cp
!= '\0')
201 error("%s: %s\n(%s)", device
,
202 pcap_statustostr(status
), cp
);
204 error("%s: %s", device
,
205 pcap_statustostr(status
));
208 } else if (status
> 0) {
210 * pcap_activate() succeeded, but it's warning us
211 * of a problem it had.
213 cp
= pcap_geterr(pc
);
214 if (status
== PCAP_WARNING
)
216 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
218 warning("%s: %s\n(%s)", device
,
219 pcap_statustostr(status
), cp
);
221 warning("%s: %s", device
,
222 pcap_statustostr(status
));
227 #define COMMAND_OPTIONS "DLi:s:w:y:"
230 main(int argc
, char **argv
)
233 char *cp
, *cmdbuf
= NULL
, *device
, *end
, *savefile
= NULL
;
238 int show_interfaces
= 0;
239 int show_dlt_types
= 0;
242 bpf_u_int32 localnet
, netmask
;
243 struct bpf_program fcode
;
244 char ebuf
[PCAP_ERRBUF_SIZE
];
246 struct sigaction action
;
249 const char *dlt_name
= NULL
;
254 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
255 program_name
= cp
+ 1;
257 program_name
= argv
[0];
260 while ((op
= getopt(argc
, argv
, COMMAND_OPTIONS
)) != -1) {
276 snaplen
= (int)strtol(optarg
, &end
, 0);
277 if (optarg
== end
|| *end
!= '\0' || snaplen
< 0)
278 error("invalid snaplen %s (must be >= 0)",
297 if (show_interfaces
) {
301 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
303 for (i
= 0, dev
= devlist
; dev
!= NULL
; i
++, dev
= dev
->next
) {
304 printf("%d.%s", i
+1, dev
->name
);
305 if (dev
->description
!= NULL
)
306 printf(" (%s)", dev
->description
);
309 pcap_freealldevs(devlist
);
313 if (device
== NULL
) {
314 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
317 error("no interfaces available for capture");
318 device
= strdup(devlist
->name
);
319 pcap_freealldevs(devlist
);
321 if (show_dlt_types
) {
322 pd
= pcap_create(device
, ebuf
);
325 status
= pcap_activate(pd
);
328 * pcap_activate() failed.
330 error("%s: %s\n(%s)", device
,
331 pcap_statustostr(status
), pcap_geterr(pd
));
333 ndlts
= pcap_list_datalinks(pd
, &dlts
);
336 * pcap_list_datalinks() failed.
338 error("%s: %s\n(%s)", device
,
339 pcap_statustostr(status
), pcap_geterr(pd
));
341 for (int i
= 0; i
< ndlts
; i
++) {
342 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
343 if (dlt_name
== NULL
)
344 printf("DLT %d", dlts
[i
]);
346 printf("%s", dlt_name
);
349 pcap_free_datalinks(dlts
);
354 if (savefile
== NULL
)
355 error("no savefile specified");
359 pd
= open_interface(device
, snaplen_set
, snaplen
, ebuf
);
362 * That failed because the interface couldn't be found.
364 * If we can get a list of interfaces, and the interface name
365 * is purely numeric, try to use it as a 1-based index
366 * in the list of interfaces.
368 devnum
= parse_interface_number(device
);
371 * It's not a number; just report
372 * the open error and fail.
378 * OK, it's a number; try to find the
379 * interface with that index, and try
382 * find_interface_by_number() exits if it
385 device
= find_interface_by_number(devnum
);
386 pd
= open_interface(device
, snaplen_set
, snaplen
, ebuf
);
391 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
397 if (dlt_name
!= NULL
) {
398 dlt
= pcap_datalink_name_to_val(dlt_name
);
399 if (dlt
== PCAP_ERROR
)
400 error("%s isn't a valid DLT name", dlt_name
);
401 if (pcap_set_datalink(pd
, dlt
) == PCAP_ERROR
)
402 error("%s: %s", device
, pcap_geterr(pd
));
406 * Don't set a filter unless we were given one on the
407 * command line; if capturing doesn't work, or doesn't
408 * use the snapshot length, without a filter, that's
412 cmdbuf
= copy_argv(&argv
[optind
]);
414 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
415 error("%s", pcap_geterr(pd
));
417 if (pcap_setfilter(pd
, &fcode
) < 0)
418 error("%s", pcap_geterr(pd
));
421 pdd
= pcap_dump_open(pd
, savefile
);
423 error("%s", pcap_geterr(pd
));
426 SetConsoleCtrlHandler(stop_capture
, TRUE
);
428 action
.sa_handler
= stop_capture
;
429 sigemptyset(&action
.sa_mask
);
431 if (sigaction(SIGINT
, &action
, NULL
) == -1)
432 error("Can't catch SIGINT: %s\n", strerror(errno
));
435 printf("Listening on %s, link-type ", device
);
436 dlt
= pcap_datalink(pd
);
437 dlt_name
= pcap_datalink_val_to_name(dlt
);
438 if (dlt_name
== NULL
)
439 printf("DLT %d", dlt
);
441 printf("%s", dlt_name
);
444 status
= pcap_dispatch(pd
, -1, pcap_dump
, (u_char
*)pdd
);
448 printf("%d packets seen\n", status
);
451 printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
452 ps
.ps_recv
, ps
.ps_drop
, ps
.ps_ifdrop
);
457 * We got interrupted, so perhaps we didn't
458 * manage to finish a line we were printing.
459 * Print an extra newline, just in case.
462 printf("Broken out of loop from SIGINT handler\n");
464 (void)fflush(stdout
);
469 (void)fprintf(stderr
, "%s: pcap_dispatch: %s\n",
470 program_name
, pcap_geterr(pd
));
473 if (cmdbuf
!= NULL
) {
474 pcap_freecode(&fcode
);
477 exit(status
== -1 ? 1 : 0);
483 (void)fprintf(stderr
, "Usage: %s -D -L [ -i interface ] [ -s snaplen ] [ -w file ] [ -y dlt ] [expression]\n",
490 error(const char *fmt
, ...)
494 (void)fprintf(stderr
, "%s: ", program_name
);
496 (void)vfprintf(stderr
, fmt
, ap
);
501 (void)fputc('\n', stderr
);
509 warning(const char *fmt
, ...)
513 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
515 (void)vfprintf(stderr
, fmt
, ap
);
520 (void)fputc('\n', stderr
);
525 * Copy arg vector into a new buffer, concatenating arguments with spaces.
528 copy_argv(register char **argv
)
531 register size_t len
= 0;
540 len
+= strlen(*p
++) + 1;
542 buf
= (char *)malloc(len
);
544 error("copy_argv: malloc");
548 while ((src
= *p
++) != NULL
) {
549 while ((*dst
++ = *src
++) != '\0')