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.
23 static const char copyright
[] _U_
=
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California. All rights reserved.\n";
37 #define THREAD_HANDLE HANDLE
38 #define THREAD_FUNC_RETURN_TYPE int
46 #define THREAD_HANDLE pthread_t
47 #define THREAD_FUNC_RETURN_TYPE void *
50 #include <sys/types.h>
54 #include "pcap/funcattrs.h"
57 #include "portability.h"
60 static char *program_name
;
63 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
64 static void PCAP_NORETURN
usage(void);
65 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
66 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
67 static char *copy_argv(char **);
73 * Generate a string for a Win32-specific error (i.e. an error generated when
74 * calling a Win32 API).
75 * For errors occurred during standard C calls, we still use pcap_strerror()
77 #define ERRBUF_SIZE 1024
79 win32_strerror(DWORD error
)
81 static char errbuf
[ERRBUF_SIZE
+1];
84 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
88 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
89 * message. Get rid of it.
91 errlen
= strlen(errbuf
);
93 errbuf
[errlen
- 1] = '\0';
94 errbuf
[errlen
- 2] = '\0';
101 catch_sigusr1(int sig _U_
)
103 printf("Got SIGUSR1\n");
113 unsigned secs_remaining
;
117 secs_remaining
= secs
;
118 while (secs_remaining
!= 0)
119 secs_remaining
= sleep(secs_remaining
);
123 static THREAD_FUNC_RETURN_TYPE
124 capture_thread_func(void *arg
)
130 struct sigaction action
;
136 action
.sa_handler
= catch_sigusr1
;
137 action
.sa_mask
= mask
;
139 if (sigaction(SIGUSR1
, &action
, NULL
) == -1)
140 error("Can't catch SIGUSR1: %s", strerror(errno
));
143 printf("Listening on %s\n", device
);
146 status
= pcap_dispatch(pd
, -1, countme
,
147 (u_char
*)&packet_count
);
151 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
152 status
, packet_count
);
157 * We got interrupted, so perhaps we didn't
158 * manage to finish a line we were printing.
159 * Print an extra newline, just in case.
162 printf("Loop got broken\n");
164 (void)fflush(stdout
);
169 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
170 program_name
, pcap_geterr(pd
));
176 main(int argc
, char **argv
)
179 register char *cp
, *cmdbuf
, *device
;
182 bpf_u_int32 localnet
, netmask
;
183 struct bpf_program fcode
;
184 char ebuf
[PCAP_ERRBUF_SIZE
];
186 THREAD_HANDLE capture_thread
;
190 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
191 program_name
= cp
+ 1;
193 program_name
= argv
[0];
196 while ((op
= getopt(argc
, argv
, "i:")) != -1) {
209 if (device
== NULL
) {
210 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
213 error("no interfaces available for capture");
214 device
= strdup(devlist
->name
);
215 pcap_freealldevs(devlist
);
218 pd
= pcap_create(device
, ebuf
);
221 status
= pcap_set_snaplen(pd
, 65535);
223 error("%s: pcap_set_snaplen failed: %s",
224 device
, pcap_statustostr(status
));
226 status
= pcap_set_immediate_mode(pd
, 1);
228 error("%s: pcap_set_immediate_mode failed: %s",
229 device
, pcap_statustostr(status
));
231 status
= pcap_set_timeout(pd
, 5*60*1000);
233 error("%s: pcap_set_timeout failed: %s",
234 device
, pcap_statustostr(status
));
235 status
= pcap_activate(pd
);
238 * pcap_activate() failed.
240 error("%s: %s\n(%s)", device
,
241 pcap_statustostr(status
), pcap_geterr(pd
));
242 } else if (status
> 0) {
244 * pcap_activate() succeeded, but it's warning us
245 * of a problem it had.
247 warning("%s: %s\n(%s)", device
,
248 pcap_statustostr(status
), pcap_geterr(pd
));
250 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
255 cmdbuf
= copy_argv(&argv
[optind
]);
257 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
258 error("%s", pcap_geterr(pd
));
260 if (pcap_setfilter(pd
, &fcode
) < 0)
261 error("%s", pcap_geterr(pd
));
264 capture_thread
= CreateThread(NULL
, 0, capture_thread_func
, device
,
266 if (capture_thread
== NULL
)
267 error("Can't create capture thread: %s",
268 win32_strerror(GetLastError()));
270 status
= pthread_create(&capture_thread
, NULL
, capture_thread_func
,
273 error("Can't create capture thread: %s", strerror(status
));
278 printf("Setting event\n");
279 if (!SetEvent(pcap_getevent(pd
)))
280 error("Can't set event for pcap_t: %s",
281 win32_strerror(GetLastError()));
282 if (WaitForSingleObject(capture_thread
, INFINITE
) == WAIT_FAILED
)
283 error("Wait for thread termination failed: %s",
284 win32_strerror(GetLastError()));
285 CloseHandle(capture_thread
);
287 printf("Sending SIGUSR1\n");
288 status
= pthread_kill(capture_thread
, SIGUSR1
);
290 warning("Can't interrupt capture thread: %s", strerror(status
));
291 status
= pthread_join(capture_thread
, &retval
);
293 error("Wait for thread termination failed: %s",
298 pcap_freecode(&fcode
);
299 exit(status
== -1 ? 1 : 0);
303 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
305 int *counterp
= (int *)user
;
313 (void)fprintf(stderr
, "Usage: %s [ -m ] [ -i interface ] [ -t timeout] [expression]\n",
320 error(const char *fmt
, ...)
324 (void)fprintf(stderr
, "%s: ", program_name
);
326 (void)vfprintf(stderr
, fmt
, ap
);
331 (void)fputc('\n', stderr
);
339 warning(const char *fmt
, ...)
343 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
345 (void)vfprintf(stderr
, fmt
, ap
);
350 (void)fputc('\n', stderr
);
355 * Copy arg vector into a new buffer, concatenating arguments with spaces.
358 copy_argv(register char **argv
)
361 register u_int len
= 0;
370 len
+= strlen(*p
++) + 1;
372 buf
= (char *)malloc(len
);
374 error("copy_argv: malloc");
378 while ((src
= *p
++) != NULL
) {
379 while ((*dst
++ = *src
++) != '\0')