]> git.meshlink.io Git - meshlink/blob - src/event.c
Remove old tests for tinc 1.1 and add a basic test for MeshLink.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O and timeout event handling
3     Copyright (C) 2014 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         if(diff.tv_sec < 0)
37                 return -1;
38         if(diff.tv_sec > 0)
39                 return 1;
40         if(diff.tv_usec < 0)
41                 return -1;
42         if(diff.tv_usec > 0)
43                 return 1;
44         if(a < b)
45                 return -1;
46         if(a > b)
47                 return 1;
48         return 0;
49 }
50
51 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
52         if(io->cb)
53                 return;
54
55         io->fd = fd;
56         io->cb = cb;
57         io->data = data;
58         io->node.data = io;
59
60         io_set(loop, io, flags);
61
62         if(!splay_insert_node(&loop->ios, &io->node))
63                 abort();
64 }
65
66 void io_set(event_loop_t *loop, io_t *io, int flags) {
67         io->flags = flags;
68
69         if(flags & IO_READ)
70                 FD_SET(io->fd, &loop->readfds);
71         else
72                 FD_CLR(io->fd, &loop->readfds);
73
74         if(flags & IO_WRITE)
75                 FD_SET(io->fd, &loop->writefds);
76         else
77                 FD_CLR(io->fd, &loop->writefds);
78 }
79
80 void io_del(event_loop_t *loop, io_t *io) {
81         if(!io->cb)
82                 return;
83
84         io_set(loop, io, 0);
85
86         splay_unlink_node(&loop->ios, &io->node);
87         io->cb = NULL;
88 }
89
90 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
91         timeout->cb = cb;
92         timeout->data = data;
93         timeout->node.data = timeout;
94
95         timeout_set(loop, timeout, tv);
96 }
97
98 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
99         if(timerisset(&timeout->tv))
100                 splay_unlink_node(&loop->timeouts, &timeout->node);
101
102         if(!loop->now.tv_sec)
103                 gettimeofday(&loop->now, NULL);
104
105         timeradd(&loop->now, tv, &timeout->tv);
106
107         if(!splay_insert_node(&loop->timeouts, &timeout->node))
108                 abort();
109 }
110
111 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
112         if(!timeout->cb)
113                 return;
114
115         splay_unlink_node(&loop->timeouts, &timeout->node);
116         timeout->cb = 0;
117         timeout->tv = (struct timeval){0, 0};
118 }
119
120 bool event_loop_run(event_loop_t *loop) {
121         loop->running = true;
122
123         fd_set readable;
124         fd_set writable;
125
126         while(loop->running) {
127                 gettimeofday(&loop->now, NULL);
128                 struct timeval diff, *tv = NULL;
129
130                 while(loop->timeouts.head) {
131                         timeout_t *timeout = loop->timeouts.head->data;
132                         timersub(&timeout->tv, &loop->now, &diff);
133
134                         if(diff.tv_sec < 0) {
135                                 timeout->cb(loop, timeout->data);
136                                 if(timercmp(&timeout->tv, &loop->now, <))
137                                         timeout_del(loop, timeout);
138                         } else {
139                                 tv = &diff;
140                                 break;
141                         }
142                 }
143
144                 memcpy(&readable, &loop->readfds, sizeof readable);
145                 memcpy(&writable, &loop->writefds, sizeof writable);
146
147                 int fds = 0;
148
149                 if(loop->ios.tail) {
150                         io_t *last = loop->ios.tail->data;
151                         fds = last->fd + 1;
152                 }
153
154                 int n = select(fds, &readable, &writable, NULL, tv);
155
156                 if(n < 0) {
157                         if(sockwouldblock(errno))
158                                 continue;
159                         else
160                                 return false;
161                 }
162
163                 if(!n)
164                         continue;
165
166                 for splay_each(io_t, io, &loop->ios) {
167                         if(FD_ISSET(io->fd, &writable))
168                                 io->cb(loop, io->data, IO_WRITE);
169                         else if(FD_ISSET(io->fd, &readable))
170                                 io->cb(loop, io->data, IO_READ);
171                 }
172         }
173
174         return true;
175 }
176
177 void event_flush_output(event_loop_t *loop) {
178         for splay_each(io_t, io, &loop->ios)
179                 if(FD_ISSET(io->fd, &loop->writefds))
180                         io->cb(loop, io->data, IO_WRITE);
181 }
182
183 void event_loop_stop(event_loop_t *loop) {
184         loop->running = false;
185 }
186
187 void event_loop_init(event_loop_t *loop) {
188         loop->ios.compare = (splay_compare_t)io_compare;
189         loop->timeouts.compare = (splay_compare_t)timeout_compare;
190         gettimeofday(&loop->now, NULL);
191 }
192
193 void event_loop_exit(event_loop_t *loop) {
194         for splay_each(io_t, io, &loop->ios)
195                 splay_free_node(&loop->ios, node);
196         for splay_each(timeout_t, timeout, &loop->timeouts)
197                 splay_free_node(&loop->timeouts, node);
198 }