157e22627SCy Schubert /*
257e22627SCy Schubert * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
357e22627SCy Schubert * The Regents of the University of California. All rights reserved.
457e22627SCy Schubert *
557e22627SCy Schubert * Redistribution and use in source and binary forms, with or without
657e22627SCy Schubert * modification, are permitted provided that: (1) source code distributions
757e22627SCy Schubert * retain the above copyright notice and this paragraph in its entirety, (2)
857e22627SCy Schubert * distributions including binary code include the above copyright notice and
957e22627SCy Schubert * this paragraph in its entirety in the documentation or other materials
1057e22627SCy Schubert * provided with the distribution, and (3) all advertising materials mentioning
1157e22627SCy Schubert * features or use of this software display the following acknowledgement:
1257e22627SCy Schubert * ``This product includes software developed by the University of California,
1357e22627SCy Schubert * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1457e22627SCy Schubert * the University nor the names of its contributors may be used to endorse
1557e22627SCy Schubert * or promote products derived from this software without specific prior
1657e22627SCy Schubert * written permission.
1757e22627SCy Schubert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1857e22627SCy Schubert * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1957e22627SCy Schubert * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2057e22627SCy Schubert */
2157e22627SCy Schubert
2257e22627SCy Schubert #include "varattrs.h"
2357e22627SCy Schubert
2457e22627SCy Schubert #ifndef lint
2557e22627SCy Schubert static const char copyright[] _U_ =
2657e22627SCy Schubert "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
2757e22627SCy Schubert The Regents of the University of California. All rights reserved.\n";
2857e22627SCy Schubert #endif
2957e22627SCy Schubert
3057e22627SCy Schubert #include <stdio.h>
3157e22627SCy Schubert #include <stdlib.h>
3257e22627SCy Schubert #include <string.h>
3357e22627SCy Schubert #include <stdarg.h>
3457e22627SCy Schubert #include <limits.h>
3557e22627SCy Schubert #ifdef _WIN32
3657e22627SCy Schubert #include <winsock2.h>
3757e22627SCy Schubert #include <windows.h>
3857e22627SCy Schubert
3957e22627SCy Schubert #define THREAD_HANDLE HANDLE
4057e22627SCy Schubert #define THREAD_FUNC_ARG_TYPE LPVOID
4157e22627SCy Schubert #define THREAD_FUNC_RETURN_TYPE DWORD __stdcall
4257e22627SCy Schubert
4357e22627SCy Schubert #include "getopt.h"
4457e22627SCy Schubert #else
4557e22627SCy Schubert #include <pthread.h>
4657e22627SCy Schubert #include <signal.h>
4757e22627SCy Schubert #include <unistd.h>
4857e22627SCy Schubert
4957e22627SCy Schubert #define THREAD_HANDLE pthread_t
5057e22627SCy Schubert #define THREAD_FUNC_ARG_TYPE void *
5157e22627SCy Schubert #define THREAD_FUNC_RETURN_TYPE void *
5257e22627SCy Schubert #endif
5357e22627SCy Schubert #include <errno.h>
5457e22627SCy Schubert #include <sys/types.h>
5557e22627SCy Schubert
5657e22627SCy Schubert #include <pcap.h>
5757e22627SCy Schubert
5857e22627SCy Schubert #include "pcap/funcattrs.h"
5957e22627SCy Schubert
6057e22627SCy Schubert #ifdef _WIN32
6157e22627SCy Schubert #include "portability.h"
6257e22627SCy Schubert #endif
6357e22627SCy Schubert
6457e22627SCy Schubert static char *program_name;
6557e22627SCy Schubert
6657e22627SCy Schubert /* Forwards */
6757e22627SCy Schubert static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
6857e22627SCy Schubert static void PCAP_NORETURN usage(void);
6957e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
7057e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
7157e22627SCy Schubert static char *copy_argv(char **);
7257e22627SCy Schubert
7357e22627SCy Schubert static pcap_t *pd;
7457e22627SCy Schubert
7557e22627SCy Schubert #ifdef _WIN32
7657e22627SCy Schubert /*
7757e22627SCy Schubert * Generate a string for a Win32-specific error (i.e. an error generated when
7857e22627SCy Schubert * calling a Win32 API).
7957e22627SCy Schubert * For errors occurred during standard C calls, we still use pcap_strerror()
8057e22627SCy Schubert */
8157e22627SCy Schubert #define ERRBUF_SIZE 1024
8257e22627SCy Schubert static const char *
win32_strerror(DWORD error)8357e22627SCy Schubert win32_strerror(DWORD error)
8457e22627SCy Schubert {
8557e22627SCy Schubert static char errbuf[ERRBUF_SIZE+1];
8657e22627SCy Schubert size_t errlen;
8757e22627SCy Schubert
8857e22627SCy Schubert FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
8957e22627SCy Schubert ERRBUF_SIZE, NULL);
9057e22627SCy Schubert
9157e22627SCy Schubert /*
9257e22627SCy Schubert * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
9357e22627SCy Schubert * message. Get rid of it.
9457e22627SCy Schubert */
9557e22627SCy Schubert errlen = strlen(errbuf);
9657e22627SCy Schubert if (errlen >= 2) {
9757e22627SCy Schubert errbuf[errlen - 1] = '\0';
9857e22627SCy Schubert errbuf[errlen - 2] = '\0';
9957e22627SCy Schubert errlen -= 2;
10057e22627SCy Schubert }
10157e22627SCy Schubert return errbuf;
10257e22627SCy Schubert }
10357e22627SCy Schubert #else
10457e22627SCy Schubert static void
catch_sigusr1(int sig _U_)10557e22627SCy Schubert catch_sigusr1(int sig _U_)
10657e22627SCy Schubert {
10757e22627SCy Schubert printf("Got SIGUSR1\n");
10857e22627SCy Schubert }
10957e22627SCy Schubert #endif
11057e22627SCy Schubert
11157e22627SCy Schubert static void
sleep_secs(int secs)11257e22627SCy Schubert sleep_secs(int secs)
11357e22627SCy Schubert {
11457e22627SCy Schubert #ifdef _WIN32
11557e22627SCy Schubert Sleep(secs*1000);
11657e22627SCy Schubert #else
11757e22627SCy Schubert unsigned secs_remaining;
11857e22627SCy Schubert
11957e22627SCy Schubert if (secs <= 0)
12057e22627SCy Schubert return;
12157e22627SCy Schubert secs_remaining = secs;
12257e22627SCy Schubert while (secs_remaining != 0)
12357e22627SCy Schubert secs_remaining = sleep(secs_remaining);
12457e22627SCy Schubert #endif
12557e22627SCy Schubert }
12657e22627SCy Schubert
12757e22627SCy Schubert static THREAD_FUNC_RETURN_TYPE
capture_thread_func(THREAD_FUNC_ARG_TYPE arg)12857e22627SCy Schubert capture_thread_func(THREAD_FUNC_ARG_TYPE arg)
12957e22627SCy Schubert {
13057e22627SCy Schubert char *device = arg;
13157e22627SCy Schubert int packet_count;
13257e22627SCy Schubert int status;
13357e22627SCy Schubert #ifndef _WIN32
13457e22627SCy Schubert struct sigaction action;
13557e22627SCy Schubert sigset_t mask;
13657e22627SCy Schubert #endif
13757e22627SCy Schubert
13857e22627SCy Schubert #ifndef _WIN32
13957e22627SCy Schubert sigemptyset(&mask);
14057e22627SCy Schubert action.sa_handler = catch_sigusr1;
14157e22627SCy Schubert action.sa_mask = mask;
14257e22627SCy Schubert action.sa_flags = 0;
14357e22627SCy Schubert if (sigaction(SIGUSR1, &action, NULL) == -1)
14457e22627SCy Schubert error("Can't catch SIGUSR1: %s", strerror(errno));
14557e22627SCy Schubert #endif
14657e22627SCy Schubert
14757e22627SCy Schubert printf("Listening on %s\n", device);
14857e22627SCy Schubert for (;;) {
14957e22627SCy Schubert packet_count = 0;
15057e22627SCy Schubert status = pcap_dispatch(pd, -1, countme,
15157e22627SCy Schubert (u_char *)&packet_count);
15257e22627SCy Schubert if (status < 0)
15357e22627SCy Schubert break;
15457e22627SCy Schubert if (status != 0) {
15557e22627SCy Schubert printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
15657e22627SCy Schubert status, packet_count);
15757e22627SCy Schubert } else
15857e22627SCy Schubert printf("No packets seen by pcap_dispatch\n");
15957e22627SCy Schubert }
160*6f9cba8fSJoseph Mingrone if (status == PCAP_ERROR_BREAK) {
16157e22627SCy Schubert /*
16257e22627SCy Schubert * We got interrupted, so perhaps we didn't
16357e22627SCy Schubert * manage to finish a line we were printing.
16457e22627SCy Schubert * Print an extra newline, just in case.
16557e22627SCy Schubert */
16657e22627SCy Schubert putchar('\n');
16757e22627SCy Schubert printf("Loop got broken\n");
16857e22627SCy Schubert }
16957e22627SCy Schubert (void)fflush(stdout);
170*6f9cba8fSJoseph Mingrone if (status == PCAP_ERROR) {
17157e22627SCy Schubert /*
17257e22627SCy Schubert * Error. Report it.
17357e22627SCy Schubert */
174*6f9cba8fSJoseph Mingrone (void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
17557e22627SCy Schubert program_name, pcap_geterr(pd));
17657e22627SCy Schubert }
17757e22627SCy Schubert return 0;
17857e22627SCy Schubert }
17957e22627SCy Schubert
18057e22627SCy Schubert int
main(int argc,char ** argv)18157e22627SCy Schubert main(int argc, char **argv)
18257e22627SCy Schubert {
18357e22627SCy Schubert register int op;
18457e22627SCy Schubert register char *cp, *cmdbuf, *device;
185*6f9cba8fSJoseph Mingrone int do_wakeup = 1;
18657e22627SCy Schubert pcap_if_t *devlist;
18757e22627SCy Schubert bpf_u_int32 localnet, netmask;
18857e22627SCy Schubert struct bpf_program fcode;
18957e22627SCy Schubert char ebuf[PCAP_ERRBUF_SIZE];
19057e22627SCy Schubert int status;
19157e22627SCy Schubert THREAD_HANDLE capture_thread;
19257e22627SCy Schubert #ifndef _WIN32
19357e22627SCy Schubert void *retval;
19457e22627SCy Schubert #endif
19557e22627SCy Schubert
19657e22627SCy Schubert device = NULL;
19757e22627SCy Schubert if ((cp = strrchr(argv[0], '/')) != NULL)
19857e22627SCy Schubert program_name = cp + 1;
19957e22627SCy Schubert else
20057e22627SCy Schubert program_name = argv[0];
20157e22627SCy Schubert
20257e22627SCy Schubert opterr = 0;
203*6f9cba8fSJoseph Mingrone while ((op = getopt(argc, argv, "i:n")) != -1) {
20457e22627SCy Schubert switch (op) {
20557e22627SCy Schubert
20657e22627SCy Schubert case 'i':
20757e22627SCy Schubert device = optarg;
20857e22627SCy Schubert break;
20957e22627SCy Schubert
210*6f9cba8fSJoseph Mingrone case 'n':
211*6f9cba8fSJoseph Mingrone do_wakeup = 0;
212*6f9cba8fSJoseph Mingrone break;
213*6f9cba8fSJoseph Mingrone
21457e22627SCy Schubert default:
21557e22627SCy Schubert usage();
21657e22627SCy Schubert /* NOTREACHED */
21757e22627SCy Schubert }
21857e22627SCy Schubert }
21957e22627SCy Schubert
22057e22627SCy Schubert if (device == NULL) {
22157e22627SCy Schubert if (pcap_findalldevs(&devlist, ebuf) == -1)
22257e22627SCy Schubert error("%s", ebuf);
22357e22627SCy Schubert if (devlist == NULL)
22457e22627SCy Schubert error("no interfaces available for capture");
22557e22627SCy Schubert device = strdup(devlist->name);
22657e22627SCy Schubert pcap_freealldevs(devlist);
22757e22627SCy Schubert }
22857e22627SCy Schubert *ebuf = '\0';
22957e22627SCy Schubert pd = pcap_create(device, ebuf);
23057e22627SCy Schubert if (pd == NULL)
23157e22627SCy Schubert error("%s", ebuf);
23257e22627SCy Schubert status = pcap_set_snaplen(pd, 65535);
23357e22627SCy Schubert if (status != 0)
23457e22627SCy Schubert error("%s: pcap_set_snaplen failed: %s",
23557e22627SCy Schubert device, pcap_statustostr(status));
23657e22627SCy Schubert status = pcap_set_timeout(pd, 5*60*1000);
23757e22627SCy Schubert if (status != 0)
23857e22627SCy Schubert error("%s: pcap_set_timeout failed: %s",
23957e22627SCy Schubert device, pcap_statustostr(status));
24057e22627SCy Schubert status = pcap_activate(pd);
24157e22627SCy Schubert if (status < 0) {
24257e22627SCy Schubert /*
24357e22627SCy Schubert * pcap_activate() failed.
24457e22627SCy Schubert */
24557e22627SCy Schubert error("%s: %s\n(%s)", device,
24657e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd));
24757e22627SCy Schubert } else if (status > 0) {
24857e22627SCy Schubert /*
24957e22627SCy Schubert * pcap_activate() succeeded, but it's warning us
25057e22627SCy Schubert * of a problem it had.
25157e22627SCy Schubert */
25257e22627SCy Schubert warning("%s: %s\n(%s)", device,
25357e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd));
25457e22627SCy Schubert }
25557e22627SCy Schubert if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
25657e22627SCy Schubert localnet = 0;
25757e22627SCy Schubert netmask = 0;
25857e22627SCy Schubert warning("%s", ebuf);
25957e22627SCy Schubert }
26057e22627SCy Schubert cmdbuf = copy_argv(&argv[optind]);
26157e22627SCy Schubert
26257e22627SCy Schubert if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
26357e22627SCy Schubert error("%s", pcap_geterr(pd));
26457e22627SCy Schubert
26557e22627SCy Schubert if (pcap_setfilter(pd, &fcode) < 0)
26657e22627SCy Schubert error("%s", pcap_geterr(pd));
26757e22627SCy Schubert
26857e22627SCy Schubert #ifdef _WIN32
26957e22627SCy Schubert capture_thread = CreateThread(NULL, 0, capture_thread_func, device,
27057e22627SCy Schubert 0, NULL);
27157e22627SCy Schubert if (capture_thread == NULL)
27257e22627SCy Schubert error("Can't create capture thread: %s",
27357e22627SCy Schubert win32_strerror(GetLastError()));
27457e22627SCy Schubert #else
27557e22627SCy Schubert status = pthread_create(&capture_thread, NULL, capture_thread_func,
27657e22627SCy Schubert device);
27757e22627SCy Schubert if (status != 0)
27857e22627SCy Schubert error("Can't create capture thread: %s", strerror(status));
27957e22627SCy Schubert #endif
28057e22627SCy Schubert sleep_secs(60);
281*6f9cba8fSJoseph Mingrone printf("Doing pcap_breakloop()\n");
28257e22627SCy Schubert pcap_breakloop(pd);
283*6f9cba8fSJoseph Mingrone if (do_wakeup) {
284*6f9cba8fSJoseph Mingrone /*
285*6f9cba8fSJoseph Mingrone * Force a wakeup in the capture thread.
286*6f9cba8fSJoseph Mingrone *
287*6f9cba8fSJoseph Mingrone * On some platforms, with some devices,, pcap_breakloop()
288*6f9cba8fSJoseph Mingrone * can't do that itself. On Windows, poke the device's
289*6f9cba8fSJoseph Mingrone * event handle; on UN*X, send a SIGUSR1 to the thread.
290*6f9cba8fSJoseph Mingrone */
29157e22627SCy Schubert #ifdef _WIN32
29257e22627SCy Schubert printf("Setting event\n");
29357e22627SCy Schubert if (!SetEvent(pcap_getevent(pd)))
29457e22627SCy Schubert error("Can't set event for pcap_t: %s",
29557e22627SCy Schubert win32_strerror(GetLastError()));
296*6f9cba8fSJoseph Mingrone #else
297*6f9cba8fSJoseph Mingrone printf("Sending SIGUSR1\n");
298*6f9cba8fSJoseph Mingrone status = pthread_kill(capture_thread, SIGUSR1);
299*6f9cba8fSJoseph Mingrone if (status != 0)
300*6f9cba8fSJoseph Mingrone warning("Can't interrupt capture thread: %s",
301*6f9cba8fSJoseph Mingrone strerror(status));
302*6f9cba8fSJoseph Mingrone #endif
303*6f9cba8fSJoseph Mingrone }
304*6f9cba8fSJoseph Mingrone
305*6f9cba8fSJoseph Mingrone /*
306*6f9cba8fSJoseph Mingrone * Now wait for the capture thread to terminate.
307*6f9cba8fSJoseph Mingrone */
308*6f9cba8fSJoseph Mingrone #ifdef _WIN32
30957e22627SCy Schubert if (WaitForSingleObject(capture_thread, INFINITE) == WAIT_FAILED)
31057e22627SCy Schubert error("Wait for thread termination failed: %s",
31157e22627SCy Schubert win32_strerror(GetLastError()));
31257e22627SCy Schubert CloseHandle(capture_thread);
31357e22627SCy Schubert #else
31457e22627SCy Schubert status = pthread_join(capture_thread, &retval);
31557e22627SCy Schubert if (status != 0)
31657e22627SCy Schubert error("Wait for thread termination failed: %s",
31757e22627SCy Schubert strerror(status));
31857e22627SCy Schubert #endif
31957e22627SCy Schubert
32057e22627SCy Schubert pcap_close(pd);
32157e22627SCy Schubert pcap_freecode(&fcode);
32257e22627SCy Schubert exit(status == -1 ? 1 : 0);
32357e22627SCy Schubert }
32457e22627SCy Schubert
32557e22627SCy Schubert static void
countme(u_char * user,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)32657e22627SCy Schubert countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
32757e22627SCy Schubert {
32857e22627SCy Schubert int *counterp = (int *)user;
32957e22627SCy Schubert
33057e22627SCy Schubert (*counterp)++;
33157e22627SCy Schubert }
33257e22627SCy Schubert
33357e22627SCy Schubert static void
usage(void)33457e22627SCy Schubert usage(void)
33557e22627SCy Schubert {
336*6f9cba8fSJoseph Mingrone (void)fprintf(stderr, "Usage: %s [ -n ] [ -i interface ] [ expression ]\n",
33757e22627SCy Schubert program_name);
33857e22627SCy Schubert exit(1);
33957e22627SCy Schubert }
34057e22627SCy Schubert
34157e22627SCy Schubert /* VARARGS */
34257e22627SCy Schubert static void
error(const char * fmt,...)34357e22627SCy Schubert error(const char *fmt, ...)
34457e22627SCy Schubert {
34557e22627SCy Schubert va_list ap;
34657e22627SCy Schubert
34757e22627SCy Schubert (void)fprintf(stderr, "%s: ", program_name);
34857e22627SCy Schubert va_start(ap, fmt);
34957e22627SCy Schubert (void)vfprintf(stderr, fmt, ap);
35057e22627SCy Schubert va_end(ap);
35157e22627SCy Schubert if (*fmt) {
35257e22627SCy Schubert fmt += strlen(fmt);
35357e22627SCy Schubert if (fmt[-1] != '\n')
35457e22627SCy Schubert (void)fputc('\n', stderr);
35557e22627SCy Schubert }
35657e22627SCy Schubert exit(1);
35757e22627SCy Schubert /* NOTREACHED */
35857e22627SCy Schubert }
35957e22627SCy Schubert
36057e22627SCy Schubert /* VARARGS */
36157e22627SCy Schubert static void
warning(const char * fmt,...)36257e22627SCy Schubert warning(const char *fmt, ...)
36357e22627SCy Schubert {
36457e22627SCy Schubert va_list ap;
36557e22627SCy Schubert
36657e22627SCy Schubert (void)fprintf(stderr, "%s: WARNING: ", program_name);
36757e22627SCy Schubert va_start(ap, fmt);
36857e22627SCy Schubert (void)vfprintf(stderr, fmt, ap);
36957e22627SCy Schubert va_end(ap);
37057e22627SCy Schubert if (*fmt) {
37157e22627SCy Schubert fmt += strlen(fmt);
37257e22627SCy Schubert if (fmt[-1] != '\n')
37357e22627SCy Schubert (void)fputc('\n', stderr);
37457e22627SCy Schubert }
37557e22627SCy Schubert }
37657e22627SCy Schubert
37757e22627SCy Schubert /*
37857e22627SCy Schubert * Copy arg vector into a new buffer, concatenating arguments with spaces.
37957e22627SCy Schubert */
38057e22627SCy Schubert static char *
copy_argv(register char ** argv)38157e22627SCy Schubert copy_argv(register char **argv)
38257e22627SCy Schubert {
38357e22627SCy Schubert register char **p;
384*6f9cba8fSJoseph Mingrone register size_t len = 0;
38557e22627SCy Schubert char *buf;
38657e22627SCy Schubert char *src, *dst;
38757e22627SCy Schubert
38857e22627SCy Schubert p = argv;
38957e22627SCy Schubert if (*p == 0)
39057e22627SCy Schubert return 0;
39157e22627SCy Schubert
39257e22627SCy Schubert while (*p)
39357e22627SCy Schubert len += strlen(*p++) + 1;
39457e22627SCy Schubert
39557e22627SCy Schubert buf = (char *)malloc(len);
39657e22627SCy Schubert if (buf == NULL)
39757e22627SCy Schubert error("copy_argv: malloc");
39857e22627SCy Schubert
39957e22627SCy Schubert p = argv;
40057e22627SCy Schubert dst = buf;
40157e22627SCy Schubert while ((src = *p++) != NULL) {
40257e22627SCy Schubert while ((*dst++ = *src++) != '\0')
40357e22627SCy Schubert ;
40457e22627SCy Schubert dst[-1] = ' ';
40557e22627SCy Schubert }
40657e22627SCy Schubert dst[-1] = '\0';
40757e22627SCy Schubert
40857e22627SCy Schubert return buf;
40957e22627SCy Schubert }
410