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