WSAEVENT is a pointer, so we cannot simply return the different of two
[tinc] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2012-2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "dropin.h"
23 #include "event.h"
24 #include "net.h"
25 #include "utils.h"
26 #include "xalloc.h"
27
28 struct timeval now;
29
30 #ifndef HAVE_MINGW
31 static fd_set readfds;
32 static fd_set writefds;
33 #else
34 static const long READ_EVENTS = FD_READ | FD_ACCEPT | FD_CLOSE;
35 static const long WRITE_EVENTS = FD_WRITE | FD_CONNECT;
36 static DWORD event_count = 0;
37 #endif
38 static bool running;
39
40 static int io_compare(const io_t *a, const io_t *b) {
41 #ifndef HAVE_MINGW
42         return a->fd - b->fd;
43 #else
44         if(a->event < b->event) {
45                 return -1;
46         }
47         if(a->event > b->event) {
48                 return 1;
49         }
50         return 0;
51 #endif
52 }
53
54 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
55         struct timeval diff;
56         timersub(&a->tv, &b->tv, &diff);
57
58         if(diff.tv_sec < 0) {
59                 return -1;
60         }
61
62         if(diff.tv_sec > 0) {
63                 return 1;
64         }
65
66         if(diff.tv_usec < 0) {
67                 return -1;
68         }
69
70         if(diff.tv_usec > 0) {
71                 return 1;
72         }
73
74         if(a < b) {
75                 return -1;
76         }
77
78         if(a > b) {
79                 return 1;
80         }
81
82         return 0;
83 }
84
85 static splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
86 static splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
87
88 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
89         if(io->cb) {
90                 return;
91         }
92
93         io->fd = fd;
94 #ifdef HAVE_MINGW
95
96         if(io->fd != -1) {
97                 io->event = WSACreateEvent();
98
99                 if(io->event == WSA_INVALID_EVENT) {
100                         abort();
101                 }
102         }
103
104         event_count++;
105 #endif
106         io->cb = cb;
107         io->data = data;
108         io->node.data = io;
109
110         io_set(io, flags);
111
112         if(!splay_insert_node(&io_tree, &io->node)) {
113                 abort();
114         }
115 }
116
117 #ifdef HAVE_MINGW
118 void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event) {
119         io->event = event;
120         io_add(io, cb, data, -1, 0);
121 }
122 #endif
123
124 void io_set(io_t *io, int flags) {
125         if(flags == io->flags) {
126                 return;
127         }
128
129         io->flags = flags;
130
131         if(io->fd == -1) {
132                 return;
133         }
134
135 #ifndef HAVE_MINGW
136
137         if(flags & IO_READ) {
138                 FD_SET(io->fd, &readfds);
139         } else {
140                 FD_CLR(io->fd, &readfds);
141         }
142
143         if(flags & IO_WRITE) {
144                 FD_SET(io->fd, &writefds);
145         } else {
146                 FD_CLR(io->fd, &writefds);
147         }
148
149 #else
150         long events = 0;
151
152         if(flags & IO_WRITE) {
153                 events |= WRITE_EVENTS;
154         }
155
156         if(flags & IO_READ) {
157                 events |= READ_EVENTS;
158         }
159
160         if(WSAEventSelect(io->fd, io->event, events) != 0) {
161                 abort();
162         }
163
164 #endif
165 }
166
167 void io_del(io_t *io) {
168         if(!io->cb) {
169                 return;
170         }
171
172         io_set(io, 0);
173 #ifdef HAVE_MINGW
174
175         if(io->fd != -1 && WSACloseEvent(io->event) == FALSE) {
176                 abort();
177         }
178
179         event_count--;
180 #endif
181
182         splay_unlink_node(&io_tree, &io->node);
183         io->cb = NULL;
184 }
185
186 void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
187         timeout->cb = cb;
188         timeout->data = data;
189         timeout->node.data = timeout;
190
191         timeout_set(timeout, tv);
192 }
193
194 void timeout_set(timeout_t *timeout, struct timeval *tv) {
195         if(timerisset(&timeout->tv)) {
196                 splay_unlink_node(&timeout_tree, &timeout->node);
197         }
198
199         if(!now.tv_sec) {
200                 gettimeofday(&now, NULL);
201         }
202
203         timeradd(&now, tv, &timeout->tv);
204
205         if(!splay_insert_node(&timeout_tree, &timeout->node)) {
206                 abort();
207         }
208 }
209
210 void timeout_del(timeout_t *timeout) {
211         if(!timeout->cb) {
212                 return;
213         }
214
215         splay_unlink_node(&timeout_tree, &timeout->node);
216         timeout->cb = 0;
217         timeout->tv = (struct timeval) {
218                 0, 0
219         };
220 }
221
222 #ifndef HAVE_MINGW
223 static int signal_compare(const signal_t *a, const signal_t *b) {
224         return a->signum - b->signum;
225 }
226
227 static io_t signalio;
228 static int pipefd[2] = {-1, -1};
229 static splay_tree_t signal_tree = {.compare = (splay_compare_t)signal_compare};
230
231 static void signal_handler(int signum) {
232         unsigned char num = signum;
233         write(pipefd[1], &num, 1);
234 }
235
236 static void signalio_handler(void *data, int flags) {
237         unsigned char signum;
238
239         if(read(pipefd[0], &signum, 1) != 1) {
240                 return;
241         }
242
243         signal_t *sig = splay_search(&signal_tree, &((signal_t) {
244                 .signum = signum
245         }));
246
247         if(sig) {
248                 sig->cb(sig->data);
249         }
250 }
251
252 static void pipe_init(void) {
253         if(!pipe(pipefd)) {
254                 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
255         }
256 }
257
258 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
259         if(sig->cb) {
260                 return;
261         }
262
263         sig->cb = cb;
264         sig->data = data;
265         sig->signum = signum;
266         sig->node.data = sig;
267
268         if(pipefd[0] == -1) {
269                 pipe_init();
270         }
271
272         signal(sig->signum, signal_handler);
273
274         if(!splay_insert_node(&signal_tree, &sig->node)) {
275                 abort();
276         }
277 }
278
279 void signal_del(signal_t *sig) {
280         if(!sig->cb) {
281                 return;
282         }
283
284         signal(sig->signum, SIG_DFL);
285
286         splay_unlink_node(&signal_tree, &sig->node);
287         sig->cb = NULL;
288 }
289 #endif
290
291 static struct timeval *get_time_remaining(struct timeval *diff) {
292         gettimeofday(&now, NULL);
293         struct timeval *tv = NULL;
294
295         while(timeout_tree.head) {
296                 timeout_t *timeout = timeout_tree.head->data;
297                 timersub(&timeout->tv, &now, diff);
298
299                 if(diff->tv_sec < 0) {
300                         timeout->cb(timeout->data);
301
302                         if(timercmp(&timeout->tv, &now, <)) {
303                                 timeout_del(timeout);
304                         }
305                 } else {
306                         tv = diff;
307                         break;
308                 }
309         }
310
311         return tv;
312 }
313
314 bool event_loop(void) {
315         running = true;
316
317 #ifndef HAVE_MINGW
318         fd_set readable;
319         fd_set writable;
320
321         while(running) {
322                 struct timeval diff;
323                 struct timeval *tv = get_time_remaining(&diff);
324                 memcpy(&readable, &readfds, sizeof(readable));
325                 memcpy(&writable, &writefds, sizeof(writable));
326
327                 int fds = 0;
328
329                 if(io_tree.tail) {
330                         io_t *last = io_tree.tail->data;
331                         fds = last->fd + 1;
332                 }
333
334                 int n = select(fds, &readable, &writable, NULL, tv);
335
336                 if(n < 0) {
337                         if(sockwouldblock(sockerrno)) {
338                                 continue;
339                         } else {
340                                 return false;
341                         }
342                 }
343
344                 if(!n) {
345                         continue;
346                 }
347
348                 for splay_each(io_t, io, &io_tree) {
349                         if(FD_ISSET(io->fd, &writable)) {
350                                 io->cb(io->data, IO_WRITE);
351                         } else if(FD_ISSET(io->fd, &readable)) {
352                                 io->cb(io->data, IO_READ);
353                         } else {
354                                 continue;
355                         }
356
357                         /*
358                            There are scenarios in which the callback will remove another io_t from the tree
359                            (e.g. closing a double connection). Since splay_each does not support that, we
360                            need to exit the loop now. That's okay, since any remaining events will get picked
361                            up by the next select() call.
362                          */
363                         break;
364                 }
365         }
366
367 #else
368
369         while(running) {
370                 struct timeval diff;
371                 struct timeval *tv = get_time_remaining(&diff);
372                 DWORD timeout_ms = tv ? (tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1) : WSA_INFINITE;
373
374                 if(!event_count) {
375                         Sleep(timeout_ms);
376                         continue;
377                 }
378
379                 /*
380                    For some reason, Microsoft decided to make the FD_WRITE event edge-triggered instead of level-triggered,
381                    which is the opposite of what select() does. In practice, that means that if a FD_WRITE event triggers,
382                    it will never trigger again until a send() returns EWOULDBLOCK. Since the semantics of this event loop
383                    is that write events are level-triggered (i.e. they continue firing until the socket is full), we need
384                    to emulate these semantics by making sure we fire each IO_WRITE that is still writeable.
385
386                    Note that technically FD_CLOSE has the same problem, but it's okay because user code does not rely on
387                    this event being fired again if ignored.
388                 */
389                 io_t *writeable_io = NULL;
390
391                 for splay_each(io_t, io, &io_tree)
392                         if(io->flags & IO_WRITE && send(io->fd, NULL, 0, 0) == 0) {
393                                 writeable_io = io;
394                                 break;
395                         }
396
397                 if(writeable_io) {
398                         writeable_io->cb(writeable_io->data, IO_WRITE);
399                         continue;
400                 }
401
402                 WSAEVENT *events = xmalloc(event_count * sizeof(*events));
403                 DWORD event_index = 0;
404
405                 for splay_each(io_t, io, &io_tree) {
406                         events[event_index] = io->event;
407                         event_index++;
408                 }
409
410                 DWORD result = WSAWaitForMultipleEvents(event_count, events, FALSE, timeout_ms, FALSE);
411
412                 WSAEVENT event;
413
414                 if(result >= WSA_WAIT_EVENT_0 && result < WSA_WAIT_EVENT_0 + event_count) {
415                         event = events[result - WSA_WAIT_EVENT_0];
416                 }
417
418                 free(events);
419
420                 if(result == WSA_WAIT_TIMEOUT) {
421                         continue;
422                 }
423
424                 if(result < WSA_WAIT_EVENT_0 || result >= WSA_WAIT_EVENT_0 + event_count) {
425                         return false;
426                 }
427
428                 io_t *io = splay_search(&io_tree, &((io_t) {
429                         .event = event
430                 }));
431
432                 if(!io) {
433                         abort();
434                 }
435
436                 if(io->fd == -1) {
437                         io->cb(io->data, 0);
438                 } else {
439                         WSANETWORKEVENTS network_events;
440
441                         if(WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0) {
442                                 return false;
443                         }
444
445                         if(network_events.lNetworkEvents & READ_EVENTS) {
446                                 io->cb(io->data, IO_READ);
447                         }
448
449                         /*
450                             The fd might be available for write too. However, if we already fired the read callback, that
451                             callback might have deleted the io (e.g. through terminate_connection()), so we can't fire the
452                             write callback here. Instead, we loop back and let the writable io loop above handle it.
453                          */
454                 }
455         }
456
457 #endif
458
459         return true;
460 }
461
462 void event_exit(void) {
463         running = false;
464 }