]> The Tcpdump Group git mirrors - libpcap/blob - tests/threadsignaltest.c
Don't include <windows.h>.
[libpcap] / tests / threadsignaltest.c
1 /*
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.
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
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
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";
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <limits.h>
33 #include <pthread.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/types.h>
38
39 #include <pcap.h>
40
41 #include "pcap/funcattrs.h"
42
43 static char *program_name;
44
45 /* Forwards */
46 static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
47 static void PCAP_NORETURN usage(void);
48 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
49 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
50 static char *copy_argv(char **);
51
52 static pcap_t *pd;
53
54 static void
55 catch_sigusr1(int sig _U_)
56 {
57 printf("Got SIGUSR1\n");
58 }
59
60 static void *
61 capture_thread_func(void *arg)
62 {
63 char *device = arg;
64 int packet_count;
65 struct sigaction action;
66 int status;
67 sigset_t mask;
68
69 sigemptyset(&mask);
70 action.sa_handler = catch_sigusr1;
71 action.sa_mask = mask;
72 action.sa_flags = 0;
73 if (sigaction(SIGUSR1, &action, NULL) == -1)
74 error("Can't catch SIGUSR1: %s", strerror(errno));
75
76 printf("Listening on %s\n", device);
77 for (;;) {
78 packet_count = 0;
79 status = pcap_dispatch(pd, -1, countme,
80 (u_char *)&packet_count);
81 if (status < 0)
82 break;
83 if (status != 0) {
84 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
85 status, packet_count);
86 }
87 }
88 if (status == -2) {
89 /*
90 * We got interrupted, so perhaps we didn't
91 * manage to finish a line we were printing.
92 * Print an extra newline, just in case.
93 */
94 putchar('\n');
95 printf("Loop got broken\n");
96 }
97 (void)fflush(stdout);
98 if (status == -1) {
99 /*
100 * Error. Report it.
101 */
102 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
103 program_name, pcap_geterr(pd));
104 }
105 return NULL;
106 }
107
108 int
109 main(int argc, char **argv)
110 {
111 register int op;
112 register char *cp, *cmdbuf, *device;
113 int immediate = 0;
114 pcap_if_t *devlist;
115 bpf_u_int32 localnet, netmask;
116 struct bpf_program fcode;
117 char ebuf[PCAP_ERRBUF_SIZE];
118 int status;
119 pthread_t capture_thread;
120 void *retval;
121
122 device = NULL;
123 if ((cp = strrchr(argv[0], '/')) != NULL)
124 program_name = cp + 1;
125 else
126 program_name = argv[0];
127
128 opterr = 0;
129 while ((op = getopt(argc, argv, "i:")) != -1) {
130 switch (op) {
131
132 case 'i':
133 device = optarg;
134 break;
135
136 default:
137 usage();
138 /* NOTREACHED */
139 }
140 }
141
142 if (device == NULL) {
143 if (pcap_findalldevs(&devlist, ebuf) == -1)
144 error("%s", ebuf);
145 if (devlist == NULL)
146 error("no interfaces available for capture");
147 device = strdup(devlist->name);
148 pcap_freealldevs(devlist);
149 }
150 *ebuf = '\0';
151 pd = pcap_create(device, ebuf);
152 if (pd == NULL)
153 error("%s", ebuf);
154 status = pcap_set_snaplen(pd, 65535);
155 if (status != 0)
156 error("%s: pcap_set_snaplen failed: %s",
157 device, pcap_statustostr(status));
158 if (immediate) {
159 status = pcap_set_immediate_mode(pd, 1);
160 if (status != 0)
161 error("%s: pcap_set_immediate_mode failed: %s",
162 device, pcap_statustostr(status));
163 }
164 status = pcap_set_timeout(pd, 5*60*1000);
165 if (status != 0)
166 error("%s: pcap_set_timeout failed: %s",
167 device, pcap_statustostr(status));
168 status = pcap_activate(pd);
169 if (status < 0) {
170 /*
171 * pcap_activate() failed.
172 */
173 error("%s: %s\n(%s)", device,
174 pcap_statustostr(status), pcap_geterr(pd));
175 } else if (status > 0) {
176 /*
177 * pcap_activate() succeeded, but it's warning us
178 * of a problem it had.
179 */
180 warning("%s: %s\n(%s)", device,
181 pcap_statustostr(status), pcap_geterr(pd));
182 }
183 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
184 localnet = 0;
185 netmask = 0;
186 warning("%s", ebuf);
187 }
188 cmdbuf = copy_argv(&argv[optind]);
189
190 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
191 error("%s", pcap_geterr(pd));
192
193 if (pcap_setfilter(pd, &fcode) < 0)
194 error("%s", pcap_geterr(pd));
195 status = pthread_create(&capture_thread, NULL, capture_thread_func,
196 device);
197 if (status != 0)
198 error("Can't create capture thread: %s", strerror(status));
199 sleep(60);
200 pcap_breakloop(pd);
201 printf("Sending SIGUSR1\n");
202 status = pthread_kill(capture_thread, SIGUSR1);
203 if (status != 0)
204 warning("Can't interrupt capture thread: %s", strerror(status));
205 status = pthread_join(capture_thread, &retval);
206 pcap_close(pd);
207 pcap_freecode(&fcode);
208 exit(status == -1 ? 1 : 0);
209 }
210
211 static void
212 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
213 {
214 int *counterp = (int *)user;
215
216 (*counterp)++;
217 }
218
219 static void
220 usage(void)
221 {
222 (void)fprintf(stderr, "Usage: %s [ -m ] [ -i interface ] [ -t timeout] [expression]\n",
223 program_name);
224 exit(1);
225 }
226
227 /* VARARGS */
228 static void
229 error(const char *fmt, ...)
230 {
231 va_list ap;
232
233 (void)fprintf(stderr, "%s: ", program_name);
234 va_start(ap, fmt);
235 (void)vfprintf(stderr, fmt, ap);
236 va_end(ap);
237 if (*fmt) {
238 fmt += strlen(fmt);
239 if (fmt[-1] != '\n')
240 (void)fputc('\n', stderr);
241 }
242 exit(1);
243 /* NOTREACHED */
244 }
245
246 /* VARARGS */
247 static void
248 warning(const char *fmt, ...)
249 {
250 va_list ap;
251
252 (void)fprintf(stderr, "%s: WARNING: ", program_name);
253 va_start(ap, fmt);
254 (void)vfprintf(stderr, fmt, ap);
255 va_end(ap);
256 if (*fmt) {
257 fmt += strlen(fmt);
258 if (fmt[-1] != '\n')
259 (void)fputc('\n', stderr);
260 }
261 }
262
263 /*
264 * Copy arg vector into a new buffer, concatenating arguments with spaces.
265 */
266 static char *
267 copy_argv(register char **argv)
268 {
269 register char **p;
270 register u_int len = 0;
271 char *buf;
272 char *src, *dst;
273
274 p = argv;
275 if (*p == 0)
276 return 0;
277
278 while (*p)
279 len += strlen(*p++) + 1;
280
281 buf = (char *)malloc(len);
282 if (buf == NULL)
283 error("copy_argv: malloc");
284
285 p = argv;
286 dst = buf;
287 while ((src = *p++) != NULL) {
288 while ((*dst++ = *src++) != '\0')
289 ;
290 dst[-1] = ' ';
291 }
292 dst[-1] = '\0';
293
294 return buf;
295 }