xref: /freebsd/contrib/unbound/util/winsock_event.h (revision 3005e0a300309ec169f8e42181b39865e910da6c)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * util/winsock_event.h - unbound event handling for winsock on windows
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2008, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains interface functions with the WinSock2 API on Windows.
40b7579f77SDag-Erling Smørgrav  * It uses the winsock WSAWaitForMultipleEvents interface on a number of
41b7579f77SDag-Erling Smørgrav  * sockets.
42b7579f77SDag-Erling Smørgrav  *
43b7579f77SDag-Erling Smørgrav  * Note that windows can only wait for max 64 events at one time.
44b7579f77SDag-Erling Smørgrav  *
45b7579f77SDag-Erling Smørgrav  * Also, file descriptors cannot be waited for.
46b7579f77SDag-Erling Smørgrav  *
47b7579f77SDag-Erling Smørgrav  * Named pipes are not easily available (and are not usable in select() ).
48b7579f77SDag-Erling Smørgrav  * For interprocess communication, it is possible to wait for a hEvent to
49b7579f77SDag-Erling Smørgrav  * be signaled by another thread.
50b7579f77SDag-Erling Smørgrav  *
51b7579f77SDag-Erling Smørgrav  * When a socket becomes readable, then it will not be flagged as
52b7579f77SDag-Erling Smørgrav  * readable again until you have gotten WOULDBLOCK from a recv routine.
53b7579f77SDag-Erling Smørgrav  * That means the event handler must store the readability (edge notify)
54b7579f77SDag-Erling Smørgrav  * and process the incoming data until it blocks.
55b7579f77SDag-Erling Smørgrav  * The function performing recv then has to inform the event handler that
56b7579f77SDag-Erling Smørgrav  * the socket has blocked, and the event handler can mark it as such.
57b7579f77SDag-Erling Smørgrav  * Thus, this file transforms the edge notify from windows to a level notify
58b7579f77SDag-Erling Smørgrav  * that is compatible with UNIX.
59b7579f77SDag-Erling Smørgrav  * The WSAEventSelect page says that it does do level notify, as long
60b7579f77SDag-Erling Smørgrav  * as you call a recv/write/accept at least once when it is signalled.
61b7579f77SDag-Erling Smørgrav  * This last bit is not true, even though documented in server2008 api docs
62b7579f77SDag-Erling Smørgrav  * from microsoft, it does not happen at all. Instead you have to test for
63b7579f77SDag-Erling Smørgrav  * WSAEWOULDBLOCK on a tcp stream, and only then retest the socket.
64b7579f77SDag-Erling Smørgrav  * And before that remember the previous result as still valid.
65b7579f77SDag-Erling Smørgrav  *
66b7579f77SDag-Erling Smørgrav  * To stay 'fair', instead of emptying a socket completely, the event handler
67b7579f77SDag-Erling Smørgrav  * can test the other (marked as blocking) sockets for new events.
68b7579f77SDag-Erling Smørgrav  *
69b7579f77SDag-Erling Smørgrav  * Additionally, TCP accept sockets get special event support.
70b7579f77SDag-Erling Smørgrav  *
71b7579f77SDag-Erling Smørgrav  * Socket numbers are not starting small, they can be any number (say 33060).
72b7579f77SDag-Erling Smørgrav  * Therefore, bitmaps are not used, but arrays.
73b7579f77SDag-Erling Smørgrav  *
74b7579f77SDag-Erling Smørgrav  * on winsock, you must use recv() and send() for TCP reads and writes,
75b7579f77SDag-Erling Smørgrav  * not read() and write(), those work only on files.
76b7579f77SDag-Erling Smørgrav  *
77b7579f77SDag-Erling Smørgrav  * Also fseek and fseeko do not work if a FILE is not fopen-ed in binary mode.
78b7579f77SDag-Erling Smørgrav  *
79b7579f77SDag-Erling Smørgrav  * When under a high load windows gives out lots of errors, from recvfrom
80b7579f77SDag-Erling Smørgrav  * on udp sockets for example (WSAECONNRESET). Even though the udp socket
81b7579f77SDag-Erling Smørgrav  * has no connection per se.
82b7579f77SDag-Erling Smørgrav  */
83b7579f77SDag-Erling Smørgrav 
84b7579f77SDag-Erling Smørgrav #ifndef UTIL_WINSOCK_EVENT_H
85b7579f77SDag-Erling Smørgrav #define UTIL_WINSOCK_EVENT_H
86b7579f77SDag-Erling Smørgrav 
87b7579f77SDag-Erling Smørgrav #ifdef USE_WINSOCK
88b7579f77SDag-Erling Smørgrav 
89b7579f77SDag-Erling Smørgrav #ifndef HAVE_EVENT_BASE_FREE
90b7579f77SDag-Erling Smørgrav #define HAVE_EVENT_BASE_FREE
91b7579f77SDag-Erling Smørgrav #endif
92b7579f77SDag-Erling Smørgrav 
93ff825849SDag-Erling Smørgrav /* redefine the calls to different names so that there is no name
94ff825849SDag-Erling Smørgrav  * collision with other code that uses libevent names. (that uses libunbound)*/
95ff825849SDag-Erling Smørgrav #define event_init winsockevent_init
96ff825849SDag-Erling Smørgrav #define event_get_version winsockevent_get_version
97ff825849SDag-Erling Smørgrav #define event_get_method winsockevent_get_method
98ff825849SDag-Erling Smørgrav #define event_base_dispatch winsockevent_base_dispatch
99ff825849SDag-Erling Smørgrav #define event_base_loopexit winsockevent_base_loopexit
100ff825849SDag-Erling Smørgrav #define event_base_free winsockevent_base_free
101ff825849SDag-Erling Smørgrav #define event_set winsockevent_set
102ff825849SDag-Erling Smørgrav #define event_base_set winsockevent_base_set
103ff825849SDag-Erling Smørgrav #define event_add winsockevent_add
104ff825849SDag-Erling Smørgrav #define event_del winsockevent_del
105ff825849SDag-Erling Smørgrav #define signal_add winsocksignal_add
106ff825849SDag-Erling Smørgrav #define signal_del winsocksignal_del
107ff825849SDag-Erling Smørgrav 
108b7579f77SDag-Erling Smørgrav /** event timeout */
109b7579f77SDag-Erling Smørgrav #define EV_TIMEOUT      0x01
110b7579f77SDag-Erling Smørgrav /** event fd readable */
111b7579f77SDag-Erling Smørgrav #define EV_READ         0x02
112b7579f77SDag-Erling Smørgrav /** event fd writable */
113b7579f77SDag-Erling Smørgrav #define EV_WRITE        0x04
114b7579f77SDag-Erling Smørgrav /** event signal */
115b7579f77SDag-Erling Smørgrav #define EV_SIGNAL       0x08
116b7579f77SDag-Erling Smørgrav /** event must persist */
117b7579f77SDag-Erling Smørgrav #define EV_PERSIST      0x10
118b7579f77SDag-Erling Smørgrav 
119b7579f77SDag-Erling Smørgrav /* needs our redblack tree */
120b7579f77SDag-Erling Smørgrav #include "rbtree.h"
121b7579f77SDag-Erling Smørgrav 
122b7579f77SDag-Erling Smørgrav /** max number of signals to support */
123b7579f77SDag-Erling Smørgrav #define MAX_SIG 32
124b7579f77SDag-Erling Smørgrav 
125b7579f77SDag-Erling Smørgrav /** The number of items that the winsock event handler can service.
126b7579f77SDag-Erling Smørgrav  * Windows cannot handle more anyway */
127b7579f77SDag-Erling Smørgrav #define WSK_MAX_ITEMS 64
128b7579f77SDag-Erling Smørgrav 
129b7579f77SDag-Erling Smørgrav /**
130b7579f77SDag-Erling Smørgrav  * event base for winsock event handler
131b7579f77SDag-Erling Smørgrav  */
132b7579f77SDag-Erling Smørgrav struct event_base
133b7579f77SDag-Erling Smørgrav {
134b7579f77SDag-Erling Smørgrav 	/** sorted by timeout (absolute), ptr */
135*3005e0a3SDag-Erling Smørgrav 	rbtree_type* times;
136b7579f77SDag-Erling Smørgrav 	/** array (first part in use) of handles to work on */
137b7579f77SDag-Erling Smørgrav 	struct event** items;
138b7579f77SDag-Erling Smørgrav 	/** number of items in use in array */
139b7579f77SDag-Erling Smørgrav 	int max;
140b7579f77SDag-Erling Smørgrav 	/** capacity of array, size of array in items */
141b7579f77SDag-Erling Smørgrav 	int cap;
142b7579f77SDag-Erling Smørgrav 	/** array of 0 - maxsig of ptr to event for it */
143b7579f77SDag-Erling Smørgrav         struct event** signals;
144b7579f77SDag-Erling Smørgrav 	/** if we need to exit */
145b7579f77SDag-Erling Smørgrav 	int need_to_exit;
146b7579f77SDag-Erling Smørgrav 	/** where to store time in seconds */
14717d15b25SDag-Erling Smørgrav 	time_t* time_secs;
148b7579f77SDag-Erling Smørgrav 	/** where to store time in microseconds */
149b7579f77SDag-Erling Smørgrav 	struct timeval* time_tv;
150b7579f77SDag-Erling Smørgrav 	/**
151b7579f77SDag-Erling Smørgrav 	 * TCP streams have sticky events to them, these are not
152b7579f77SDag-Erling Smørgrav 	 * reported by the windows event system anymore, we have to
153b7579f77SDag-Erling Smørgrav 	 * keep reporting those events as present until wouldblock() is
154b7579f77SDag-Erling Smørgrav 	 * signalled by the handler back to use.
155b7579f77SDag-Erling Smørgrav 	 */
156b7579f77SDag-Erling Smørgrav 	int tcp_stickies;
157b7579f77SDag-Erling Smørgrav 	/**
158b7579f77SDag-Erling Smørgrav 	 * should next cycle process reinvigorated stickies,
159b7579f77SDag-Erling Smørgrav 	 * these are stickies that have been stored, but due to a new
160b7579f77SDag-Erling Smørgrav 	 * event_add a sudden interest in the event has incepted.
161b7579f77SDag-Erling Smørgrav 	 */
162b7579f77SDag-Erling Smørgrav 	int tcp_reinvigorated;
163b7579f77SDag-Erling Smørgrav 	/** The list of events that is currently being processed. */
164b7579f77SDag-Erling Smørgrav 	WSAEVENT waitfor[WSK_MAX_ITEMS];
165b7579f77SDag-Erling Smørgrav };
166b7579f77SDag-Erling Smørgrav 
167b7579f77SDag-Erling Smørgrav /**
168b7579f77SDag-Erling Smørgrav  * Event structure. Has some of the event elements.
169b7579f77SDag-Erling Smørgrav  */
170b7579f77SDag-Erling Smørgrav struct event {
171b7579f77SDag-Erling Smørgrav         /** node in timeout rbtree */
172*3005e0a3SDag-Erling Smørgrav         rbnode_type node;
173b7579f77SDag-Erling Smørgrav         /** is event already added */
174b7579f77SDag-Erling Smørgrav         int added;
175b7579f77SDag-Erling Smørgrav 
176b7579f77SDag-Erling Smørgrav         /** event base it belongs to */
177b7579f77SDag-Erling Smørgrav         struct event_base *ev_base;
178b7579f77SDag-Erling Smørgrav         /** fd to poll or -1 for timeouts. signal number for sigs. */
179b7579f77SDag-Erling Smørgrav         int ev_fd;
180b7579f77SDag-Erling Smørgrav         /** what events this event is interested in, see EV_.. above. */
181b7579f77SDag-Erling Smørgrav         short ev_events;
182b7579f77SDag-Erling Smørgrav         /** timeout value */
183b7579f77SDag-Erling Smørgrav         struct timeval ev_timeout;
184b7579f77SDag-Erling Smørgrav 
185b7579f77SDag-Erling Smørgrav         /** callback to call: fd, eventbits, userarg */
186b7579f77SDag-Erling Smørgrav         void (*ev_callback)(int, short, void *);
187b7579f77SDag-Erling Smørgrav         /** callback user arg */
188b7579f77SDag-Erling Smørgrav         void *ev_arg;
189b7579f77SDag-Erling Smørgrav 
190b7579f77SDag-Erling Smørgrav 	/* ----- nonpublic part, for winsock_event only ----- */
191b7579f77SDag-Erling Smørgrav 	/** index of this event in the items array (if added) */
192b7579f77SDag-Erling Smørgrav 	int idx;
193b7579f77SDag-Erling Smørgrav 	/** the event handle to wait for new events to become ready */
194b7579f77SDag-Erling Smørgrav 	WSAEVENT hEvent;
195b7579f77SDag-Erling Smørgrav 	/** true if this filedes is a TCP socket and needs special attention */
196b7579f77SDag-Erling Smørgrav 	int is_tcp;
197b7579f77SDag-Erling Smørgrav 	/** remembered EV_ values */
198b7579f77SDag-Erling Smørgrav 	short old_events;
199b7579f77SDag-Erling Smørgrav 	/** should remembered EV_ values be used for TCP streams.
200b7579f77SDag-Erling Smørgrav 	 * Reset after WOULDBLOCK is signaled using the function. */
201b7579f77SDag-Erling Smørgrav 	int stick_events;
202b7579f77SDag-Erling Smørgrav 
203b7579f77SDag-Erling Smørgrav 	/** true if this event is a signaling WSAEvent by the user.
20405ab2901SDag-Erling Smørgrav 	 * User created and user closed WSAEvent. Only signaled/unsignaled,
205b7579f77SDag-Erling Smørgrav 	 * no read/write/distinctions needed. */
206b7579f77SDag-Erling Smørgrav 	int is_signal;
207b7579f77SDag-Erling Smørgrav 	/** used during callbacks to see which events were just checked */
208b7579f77SDag-Erling Smørgrav 	int just_checked;
209b7579f77SDag-Erling Smørgrav };
210b7579f77SDag-Erling Smørgrav 
211b7579f77SDag-Erling Smørgrav /** create event base */
21217d15b25SDag-Erling Smørgrav void *event_init(time_t* time_secs, struct timeval* time_tv);
213b7579f77SDag-Erling Smørgrav /** get version */
214b7579f77SDag-Erling Smørgrav const char *event_get_version(void);
215b7579f77SDag-Erling Smørgrav /** get polling method (select,epoll) */
216b7579f77SDag-Erling Smørgrav const char *event_get_method(void);
217b7579f77SDag-Erling Smørgrav /** run select in a loop */
218b7579f77SDag-Erling Smørgrav int event_base_dispatch(struct event_base *);
219b7579f77SDag-Erling Smørgrav /** exit that loop */
220b7579f77SDag-Erling Smørgrav int event_base_loopexit(struct event_base *, struct timeval *);
221b7579f77SDag-Erling Smørgrav /** free event base. Free events yourself */
222b7579f77SDag-Erling Smørgrav void event_base_free(struct event_base *);
223b7579f77SDag-Erling Smørgrav /** set content of event */
224b7579f77SDag-Erling Smørgrav void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
225b7579f77SDag-Erling Smørgrav 
226b7579f77SDag-Erling Smørgrav /** add event to a base. You *must* call this for every event. */
227b7579f77SDag-Erling Smørgrav int event_base_set(struct event_base *, struct event *);
228b7579f77SDag-Erling Smørgrav /** add event to make it active. You may not change it with event_set anymore */
229b7579f77SDag-Erling Smørgrav int event_add(struct event *, struct timeval *);
230b7579f77SDag-Erling Smørgrav /** remove event. You may change it again */
231b7579f77SDag-Erling Smørgrav int event_del(struct event *);
232b7579f77SDag-Erling Smørgrav 
233b7579f77SDag-Erling Smørgrav #define evtimer_add(ev, tv)             event_add(ev, tv)
234b7579f77SDag-Erling Smørgrav #define evtimer_del(ev)                 event_del(ev)
235b7579f77SDag-Erling Smørgrav 
236b7579f77SDag-Erling Smørgrav /* uses different implementation. Cannot mix fd/timeouts and signals inside
237b7579f77SDag-Erling Smørgrav  * the same struct event. create several event structs for that.  */
238b7579f77SDag-Erling Smørgrav /** install signal handler */
239b7579f77SDag-Erling Smørgrav int signal_add(struct event *, struct timeval *);
240b7579f77SDag-Erling Smørgrav /** set signal event contents */
241b7579f77SDag-Erling Smørgrav #define signal_set(ev, x, cb, arg)      \
242b7579f77SDag-Erling Smørgrav         event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
243b7579f77SDag-Erling Smørgrav /** remove signal handler */
244b7579f77SDag-Erling Smørgrav int signal_del(struct event *);
245b7579f77SDag-Erling Smørgrav 
246b7579f77SDag-Erling Smørgrav /** compare events in tree, based on timevalue, ptr for uniqueness */
247b7579f77SDag-Erling Smørgrav int mini_ev_cmp(const void* a, const void* b);
248b7579f77SDag-Erling Smørgrav 
249b7579f77SDag-Erling Smørgrav /**
250b7579f77SDag-Erling Smørgrav  * Routine for windows only, where the handling layer can signal that
251b7579f77SDag-Erling Smørgrav  * a TCP stream encountered WSAEWOULDBLOCK for a stream and thus needs
252b7579f77SDag-Erling Smørgrav  * retesting the event.
253b7579f77SDag-Erling Smørgrav  * Pass if EV_READ or EV_WRITE gave wouldblock.
254b7579f77SDag-Erling Smørgrav  */
255b7579f77SDag-Erling Smørgrav void winsock_tcp_wouldblock(struct event* ev, int eventbit);
256b7579f77SDag-Erling Smørgrav 
257b7579f77SDag-Erling Smørgrav /**
258b7579f77SDag-Erling Smørgrav  * Routine for windows only. where you pass a signal WSAEvent that
259b7579f77SDag-Erling Smørgrav  * you wait for. When the event is signaled, the callback gets called.
260b7579f77SDag-Erling Smørgrav  * The callback has to WSAResetEvent to disable the signal.
261b7579f77SDag-Erling Smørgrav  * @param base: the event base.
262b7579f77SDag-Erling Smørgrav  * @param ev: the event structure for data storage
263b7579f77SDag-Erling Smørgrav  * 	can be passed uninitialised.
264b7579f77SDag-Erling Smørgrav  * @param wsaevent: the WSAEvent that gets signaled.
265b7579f77SDag-Erling Smørgrav  * @param cb: callback routine.
266b7579f77SDag-Erling Smørgrav  * @param arg: user argument to callback routine.
267b7579f77SDag-Erling Smørgrav  * @return false on error.
268b7579f77SDag-Erling Smørgrav  */
269b7579f77SDag-Erling Smørgrav int winsock_register_wsaevent(struct event_base* base, struct event* ev,
270b7579f77SDag-Erling Smørgrav 	WSAEVENT wsaevent, void (*cb)(int, short, void*), void* arg);
271b7579f77SDag-Erling Smørgrav 
272b7579f77SDag-Erling Smørgrav /**
273b7579f77SDag-Erling Smørgrav  * Unregister a wsaevent. User has to close the WSAEVENT itself.
274b7579f77SDag-Erling Smørgrav  * @param ev: event data storage.
275b7579f77SDag-Erling Smørgrav  */
276b7579f77SDag-Erling Smørgrav void winsock_unregister_wsaevent(struct event* ev);
277b7579f77SDag-Erling Smørgrav 
278b7579f77SDag-Erling Smørgrav #endif /* USE_WINSOCK */
279b7579f77SDag-Erling Smørgrav #endif /* UTIL_WINSOCK_EVENT_H */
280