]> git.meshlink.io Git - meshlink/blob - src/event.c
Only add confirmed reflexive UDP addresses to ANS_KEY messages.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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 "splay_tree.h"
26 #include "utils.h"
27 #include "xalloc.h"
28
29 static int io_compare(const io_t *a, const io_t *b) {
30         return a->fd - b->fd;
31 }
32
33 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
34         struct timeval diff;
35         timersub(&a->tv, &b->tv, &diff);
36
37         if(diff.tv_sec < 0) {
38                 return -1;
39         }
40
41         if(diff.tv_sec > 0) {
42                 return 1;
43         }
44
45         if(diff.tv_usec < 0) {
46                 return -1;
47         }
48
49         if(diff.tv_usec > 0) {
50                 return 1;
51         }
52
53         if(a < b) {
54                 return -1;
55         }
56
57         if(a > b) {
58                 return 1;
59         }
60
61         return 0;
62 }
63
64 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
65         assert(!io->cb);
66
67         io->fd = fd;
68         io->cb = cb;
69         io->data = data;
70         io->node.data = io;
71
72         io_set(loop, io, flags);
73
74         splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
75         assert(node);
76 }
77
78 void io_set(event_loop_t *loop, io_t *io, int flags) {
79         assert(io->cb);
80
81         io->flags = flags;
82
83         if(flags & IO_READ) {
84                 FD_SET(io->fd, &loop->readfds);
85         } else {
86                 FD_CLR(io->fd, &loop->readfds);
87         }
88
89         if(flags & IO_WRITE) {
90                 FD_SET(io->fd, &loop->writefds);
91         } else {
92                 FD_CLR(io->fd, &loop->writefds);
93         }
94 }
95
96 void io_del(event_loop_t *loop, io_t *io) {
97         assert(io->cb);
98
99         loop->deletion = true;
100
101         io_set(loop, io, 0);
102
103         splay_unlink_node(&loop->ios, &io->node);
104         io->cb = NULL;
105 }
106
107 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
108         timeout->cb = cb;
109         timeout->data = data;
110         timeout->node.data = timeout;
111
112         timeout_set(loop, timeout, tv);
113 }
114
115 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
116         assert(timeout->cb);
117
118         if(timerisset(&timeout->tv)) {
119                 splay_unlink_node(&loop->timeouts, &timeout->node);
120         }
121
122         if(!loop->now.tv_sec) {
123                 gettimeofday(&loop->now, NULL);
124         }
125
126         timeradd(&loop->now, tv, &timeout->tv);
127
128         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
129                 abort();
130         }
131
132         loop->deletion = true;
133 }
134
135 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
136         splay_unlink_node(&loop->timeouts, &timeout->node);
137         timeout->tv = (struct timeval) {
138                 0, 0
139         };
140 }
141
142 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
143         if(!timeout->cb) {
144                 return;
145         }
146
147         if(timerisset(&timeout->tv)) {
148                 timeout_disable(loop, timeout);
149         }
150
151         timeout->cb = NULL;
152         loop->deletion = true;
153 }
154
155 static int signal_compare(const signal_t *a, const signal_t *b) {
156         return (int)a->signum - (int)b->signum;
157 }
158
159 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
160         (void)data;
161         (void)flags;
162         unsigned char signum;
163
164         if(read(loop->pipefd[0], &signum, 1) != 1) {
165                 return;
166         }
167
168         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
169                 .signum = signum
170         });
171
172         if(sig) {
173                 sig->cb(loop, sig->data);
174         }
175 }
176
177 static void pipe_init(event_loop_t *loop) {
178         int result = pipe(loop->pipefd);
179         assert(result == 0);
180
181         if(result == 0) {
182                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
183         }
184 }
185
186 static void pipe_exit(event_loop_t *loop) {
187         io_del(loop, &loop->signalio);
188
189         close(loop->pipefd[0]);
190         close(loop->pipefd[1]);
191
192         loop->pipefd[0] = -1;
193         loop->pipefd[1] = -1;
194 }
195
196 void signal_trigger(event_loop_t *loop, signal_t *sig) {
197         uint8_t signum = sig->signum;
198         write(loop->pipefd[1], &signum, 1);
199         return;
200 }
201
202 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
203         assert(!sig->cb);
204
205         sig->cb = cb;
206         sig->data = data;
207         sig->signum = signum;
208         sig->node.data = sig;
209
210         if(loop->pipefd[0] == -1) {
211                 pipe_init(loop);
212         }
213
214         if(!splay_insert_node(&loop->signals, &sig->node)) {
215                 abort();
216         }
217 }
218
219 void signal_del(event_loop_t *loop, signal_t *sig) {
220         assert(sig->cb);
221
222         loop->deletion = true;
223
224         splay_unlink_node(&loop->signals, &sig->node);
225         sig->cb = NULL;
226
227         if(!loop->signals.count && loop->pipefd[0] != -1) {
228                 pipe_exit(loop);
229         }
230 }
231
232 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
233         loop->idle_cb = cb;
234         loop->idle_data = data;
235 }
236
237 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
238         assert(mutex);
239
240         fd_set readable;
241         fd_set writable;
242
243         while(loop->running) {
244                 gettimeofday(&loop->now, NULL);
245                 struct timeval diff, it, *tv = NULL;
246
247                 while(loop->timeouts.head) {
248                         timeout_t *timeout = loop->timeouts.head->data;
249                         timersub(&timeout->tv, &loop->now, &diff);
250
251                         if(diff.tv_sec < 0) {
252                                 timeout_disable(loop, timeout);
253                                 timeout->cb(loop, timeout->data);
254                         } else {
255                                 tv = &diff;
256                                 break;
257                         }
258                 }
259
260                 if(loop->idle_cb) {
261                         it = loop->idle_cb(loop, loop->idle_data);
262
263                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
264                                 tv = &it;
265                         }
266                 }
267
268                 memcpy(&readable, &loop->readfds, sizeof(readable));
269                 memcpy(&writable, &loop->writefds, sizeof(writable));
270
271                 int fds = 0;
272
273                 if(loop->ios.tail) {
274                         io_t *last = loop->ios.tail->data;
275                         fds = last->fd + 1;
276                 }
277
278                 // release mesh mutex during select
279                 pthread_mutex_unlock(mutex);
280
281                 int n = select(fds, &readable, &writable, NULL, tv);
282
283                 pthread_mutex_lock(mutex);
284
285                 gettimeofday(&loop->now, NULL);
286
287                 if(n < 0) {
288                         if(sockwouldblock(errno)) {
289                                 continue;
290                         } else {
291                                 return false;
292                         }
293                 }
294
295                 if(!n) {
296                         continue;
297                 }
298
299                 // Normally, splay_each allows the current node to be deleted. However,
300                 // it can be that one io callback triggers the deletion of another io,
301                 // so we have to detect this and break the loop.
302
303                 loop->deletion = false;
304
305                 for splay_each(io_t, io, &loop->ios) {
306                         if(FD_ISSET(io->fd, &writable) && io->cb) {
307                                 io->cb(loop, io->data, IO_WRITE);
308                         }
309
310                         if(loop->deletion) {
311                                 break;
312                         }
313
314                         if(FD_ISSET(io->fd, &readable) && io->cb) {
315                                 io->cb(loop, io->data, IO_READ);
316                         }
317
318                         if(loop->deletion) {
319                                 break;
320                         }
321                 }
322         }
323
324         return true;
325 }
326
327 void event_flush_output(event_loop_t *loop) {
328         for splay_each(io_t, io, &loop->ios)
329                 if(FD_ISSET(io->fd, &loop->writefds)) {
330                         io->cb(loop, io->data, IO_WRITE);
331                 }
332 }
333
334 void event_loop_start(event_loop_t *loop) {
335         loop->running = true;
336 }
337
338 void event_loop_stop(event_loop_t *loop) {
339         loop->running = false;
340 }
341
342 void event_loop_init(event_loop_t *loop) {
343         loop->ios.compare = (splay_compare_t)io_compare;
344         loop->timeouts.compare = (splay_compare_t)timeout_compare;
345         loop->signals.compare = (splay_compare_t)signal_compare;
346         loop->pipefd[0] = -1;
347         loop->pipefd[1] = -1;
348         gettimeofday(&loop->now, NULL);
349 }
350
351 void event_loop_exit(event_loop_t *loop) {
352         assert(!loop->ios.count);
353         assert(!loop->timeouts.count);
354         assert(!loop->signals.count);
355
356         for splay_each(io_t, io, &loop->ios) {
357                 splay_unlink_node(&loop->ios, node);
358         }
359
360         for splay_each(timeout_t, timeout, &loop->timeouts) {
361                 splay_unlink_node(&loop->timeouts, node);
362         }
363
364         for splay_each(signal_t, signal, &loop->signals) {
365                 splay_unlink_node(&loop->signals, node);
366         }
367 }