]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/nonblocktest.c
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 * Tests for pcap_set_nonblock / pcap_get_nonblock:
27 * - set/get are symmetric
28 * - get returns the same before/after activate
29 * - pcap_breakloop works after setting nonblock on and then off
31 * Really this is meant to
32 * be run manually under strace, to check for extra
33 * calls to eventfd or close.
41 #include <sys/types.h>
46 static char *program_name
= "nonblocktest";
48 static void PCAP_NORETURN
usage(void);
49 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
50 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
54 error(const char *fmt
, ...)
58 (void)fprintf(stderr
, "%s: ", program_name
);
60 (void)vfprintf(stderr
, fmt
, ap
);
65 (void)fputc('\n', stderr
);
73 warning(const char *fmt
, ...)
77 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
79 (void)vfprintf(stderr
, fmt
, ap
);
84 (void)fputc('\n', stderr
);
91 (void)fprintf(stderr
, "Usage: %s [ -i interface ]\n",
97 breakme(u_char
*user _U_
, const struct pcap_pkthdr
*h _U_
, const u_char
*sp _U_
)
99 warning("using pcap_breakloop()");
104 main(int argc
, char **argv
)
106 int status
, op
, i
, ret
;
109 char ebuf
[PCAP_ERRBUF_SIZE
];
112 while ((op
= getopt(argc
, argv
, "i:sptnq")) != -1) {
124 if (device
== NULL
) {
125 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
128 error("no interfaces available for capture");
129 device
= strdup(devlist
->name
);
130 warning("listening on %s", device
);
131 pcap_freealldevs(devlist
);
134 pd
= pcap_create(device
, ebuf
);
139 /* set nonblock before activate */
140 if (pcap_setnonblock(pd
, 1, ebuf
) < 0)
141 error("pcap_setnonblock failed: %s", ebuf
);
142 /* getnonblock just returns "not activated yet" */
143 ret
= pcap_getnonblock(pd
, ebuf
);
144 if (ret
!= PCAP_ERROR_NOT_ACTIVATED
)
145 error("pcap_getnonblock unexpectedly succeeded");
146 if ((status
= pcap_activate(pd
)) < 0)
147 error("pcap_activate failed");
148 ret
= pcap_getnonblock(pd
, ebuf
);
150 error( "pcap_getnonblock did not return nonblocking" );
152 /* Set nonblock multiple times, ensure with strace that it's a noop */
153 for (i
=0; i
<10; i
++) {
154 if (pcap_setnonblock(pd
, 1, ebuf
) < 0)
155 error("pcap_setnonblock failed: %s", ebuf
);
156 ret
= pcap_getnonblock(pd
, ebuf
);
158 error( "pcap_getnonblock did not return nonblocking" );
160 /* Set block multiple times, ensure with strace that it's a noop */
161 for (i
=0; i
<10; i
++) {
162 if (pcap_setnonblock(pd
, 0, ebuf
) < 0)
163 error("pcap_setnonblock failed: %s", ebuf
);
164 ret
= pcap_getnonblock(pd
, ebuf
);
166 error( "pcap_getnonblock did not return blocking" );
169 /* Now pcap_loop forever, with a callback that
170 * uses pcap_breakloop to get out of forever */
171 status
= pcap_loop(pd
, -1, breakme
, NULL
);
172 if (status
!= PCAP_ERROR_BREAK
) {
174 error("pcap_breakloop didn't cause a break");
175 error("pcap_loop failed: %s", pcap_geterr(pd
));
178 /* Now test that pcap_setnonblock fails if we can't open the
180 if (pcap_setnonblock(pd
, 1, ebuf
) < 0)
181 error("pcap_setnonblock failed: %s", ebuf
);
183 ret
= open("/dev/null", O_RDONLY
);
187 ret
= pcap_setnonblock(pd
, 0, ebuf
);
189 error("pcap_setnonblock succeeded even though file table is full");
191 warning("pcap_setnonblock failed as expected: %s", ebuf
);