summaryrefslogtreecommitdiffstats
path: root/src/poll.c
blob: 499a33912de982e6872731b4974b1aae8a771263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
  Copyright (c) 2012-2015, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   \file

   Portable polling API implementations
*/


#include "poll.h"
#include "async.h"
#include "peer.h"

#include <signal.h>


#ifdef USE_EPOLL

#include <sys/epoll.h>
#include <sys/syscall.h>

#endif

#ifdef USE_SELECT

#include <sys/select.h>

#endif


/** Returns the time to the next handshake or -1 */
static inline int handshake_timeout(void) {
	if (!ctx.handshake_queue.next)
		return -1;

	fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry);

	int diff_msec = peer->next_handshake - ctx.now;
	if (diff_msec < 0)
		return 0;
	else
		return diff_msec;
}


#ifdef USE_EPOLL


#ifndef SYS_epoll_pwait
#define SYS_epoll_pwait __NR_epoll_pwait
#endif

/** Simplified epoll_pwait wrapper (as there are systems without or with broken epoll_pwait) */
static inline int epoll_wait_unblocked(int epfd, struct epoll_event *events, int maxevents, int timeout) {
	const uint8_t buf[_NSIG/8] = {};
	return syscall(SYS_epoll_pwait, epfd, events, maxevents, timeout, buf, sizeof(buf));
}


void fastd_poll_init(void) {
	ctx.epoll_fd = epoll_create(1);
	if (ctx.epoll_fd < 0)
		exit_errno("epoll_create1");

	struct epoll_event event_async = {
		.events = EPOLLIN,
		.data.ptr = &ctx.async_rfd,
	};
	if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.async_rfd, &event_async) < 0)
		exit_errno("epoll_ctl");

#ifdef WITH_STATUS_SOCKET
	if (ctx.status_fd >= 0) {
		struct epoll_event event_status = {
			.events = EPOLLIN,
			.data.ptr = &ctx.status_fd,
		};

		if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.status_fd, &event_status) < 0)
			exit_errno("epoll_ctl");
	}
#endif
}

void fastd_poll_free(void) {
	if (close(ctx.epoll_fd))
		pr_warn_errno("closing EPOLL: close");
}

void fastd_poll_set_fd_tuntap(void) {
	struct epoll_event event = {
		.events = EPOLLIN,
		.data.ptr = &ctx.tunfd,
	};
	if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.tunfd, &event) < 0)
		exit_errno("epoll_ctl");
}

void fastd_poll_set_fd_sock(size_t i) {
	struct epoll_event event = {
		.events = EPOLLIN,
		.data.ptr = &ctx.socks[i],
	};
	if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.socks[i].fd, &event) < 0)
		exit_errno("epoll_ctl");
}

void fastd_poll_set_fd_peer(size_t i) {
	fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i);

	if (!peer->sock || !fastd_peer_is_socket_dynamic(peer))
		return;

	struct epoll_event event = {
		.events = EPOLLIN,
		.data.ptr = peer->sock,
	};
	if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, peer->sock->fd, &event) < 0)
		exit_errno("epoll_ctl");
}

void fastd_poll_add_peer(void) {
}

void fastd_poll_delete_peer(UNUSED size_t i) {
}


void fastd_poll_handle(void) {
	int maintenance_timeout = ctx.next_maintenance - ctx.now;

	if (maintenance_timeout < 0)
		maintenance_timeout = 0;

	int timeout = handshake_timeout();
	if (timeout < 0 || timeout > maintenance_timeout)
		timeout = maintenance_timeout;

	struct epoll_event events[16];
	int ret = epoll_wait_unblocked(ctx.epoll_fd, events, 16, timeout);
	if (ret < 0 && errno != EINTR)
		exit_errno("epoll_pwait");

	fastd_update_time();

	if (ret < 0)
		return;

	size_t i;
	for (i = 0; i < (size_t)ret; i++) {
		if (events[i].data.ptr == &ctx.tunfd) {
			if (events[i].events & EPOLLIN)
				fastd_tuntap_handle();
		}
		else if (events[i].data.ptr == &ctx.async_rfd) {
			if (events[i].events & EPOLLIN)
				fastd_async_handle();
		}
#ifdef WITH_STATUS_SOCKET
		else if (events[i].data.ptr == &ctx.status_fd) {
			if (events[i].events & EPOLLIN)
				fastd_status_handle();
		}
#endif
		else {
			fastd_socket_t *sock = events[i].data.ptr;

			if (events[i].events & (EPOLLERR|EPOLLHUP)) {
				if (sock->peer)
					fastd_peer_reset_socket(sock->peer);
				else
					fastd_socket_error(sock);
			}
			else if (events[i].events & EPOLLIN) {
				fastd_receive(sock);
			}
		}
	}
}

#else

void fastd_poll_init(void) {
	VECTOR_RESIZE(ctx.pollfds, 3 + ctx.n_socks + VECTOR_LEN(ctx.peers));

	VECTOR_INDEX(ctx.pollfds, 0) = (struct pollfd) {
		.fd = -1,
		.events = POLLIN,
		.revents = 0,
	};

	VECTOR_INDEX(ctx.pollfds, 1) = (struct pollfd) {
		.fd = ctx.async_rfd,
		.events = POLLIN,
		.revents = 0,
	};

	VECTOR_INDEX(ctx.pollfds, 2) = (struct pollfd) {
#ifdef WITH_STATUS_SOCKET
		.fd = ctx.status_fd,
#else
		.fd = -1,
#endif
		.events = POLLIN,
		.revents = 0,
	};

	size_t i;
	for (i = 0; i < ctx.n_socks + VECTOR_LEN(ctx.peers); i++) {
		VECTOR_INDEX(ctx.pollfds, 3+i) = (struct pollfd) {
			.fd = -1,
			.events = POLLIN,
			.revents = 0,
		};
	}
}

void fastd_poll_free(void) {
	VECTOR_FREE(ctx.pollfds);
}


void fastd_poll_set_fd_tuntap(void) {
	VECTOR_INDEX(ctx.pollfds, 0).fd = ctx.tunfd;
}

void fastd_poll_set_fd_sock(size_t i) {
	VECTOR_INDEX(ctx.pollfds, 3+i).fd = ctx.socks[i].fd;
}

void fastd_poll_set_fd_peer(size_t i) {
	if (!VECTOR_LEN(ctx.pollfds))
		exit_bug("fastd_poll_set_fd_peer: polling not initialized yet");

	fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i);

	if (!peer->sock || !fastd_peer_is_socket_dynamic(peer))
		VECTOR_INDEX(ctx.pollfds, 3+ctx.n_socks+i).fd = -1;
	else
		VECTOR_INDEX(ctx.pollfds, 3+ctx.n_socks+i).fd = peer->sock->fd;
}

void fastd_poll_add_peer(void) {
	if (!VECTOR_LEN(ctx.pollfds))
		/* Polling is not initialized yet */
		return;

	struct pollfd pollfd = {
		.fd = -1,
		.events = POLLIN,
		.revents = 0,
	};

	VECTOR_ADD(ctx.pollfds, pollfd);
}

void fastd_poll_delete_peer(size_t i) {
	VECTOR_DELETE(ctx.pollfds, 3+ctx.n_socks+i);
}


void fastd_poll_handle(void) {
	size_t i;

	int maintenance_timeout = ctx.next_maintenance - ctx.now;

	if (maintenance_timeout < 0)
		maintenance_timeout = 0;

	int timeout = handshake_timeout();
	if (timeout < 0 || timeout > maintenance_timeout)
		timeout = maintenance_timeout;

	if (VECTOR_LEN(ctx.pollfds) != 3 + ctx.n_socks + VECTOR_LEN(ctx.peers))
		exit_bug("fd count mismatch");

	sigset_t set, oldset;
	sigemptyset(&set);
	pthread_sigmask(SIG_SETMASK, &set, &oldset);

	int ret = 0;

#ifdef USE_SELECT
	/* Inefficient implementation for OSX... */
	fd_set readfds;
	FD_ZERO(&readfds);
	int maxfd = -1;

	for (i = 0; i < VECTOR_LEN(ctx.pollfds); i++) {
		struct pollfd *pollfd = &VECTOR_INDEX(ctx.pollfds, i);
		if (pollfd->fd >= 0) {
			FD_SET(pollfd->fd, &readfds);

			if (pollfd->fd > maxfd)
				maxfd = pollfd->fd;
		}
	}

	fd_set errfds = readfds;

	if (maxfd >= 0) {
		struct timeval tv = {}, *tvp = NULL;
		if (timeout >= 0) {
			tvp = &tv;
			tv.tv_sec = timeout/1000;
			tv.tv_usec = (timeout%1000)*1000;
		}
		ret = select(maxfd+1, &readfds, NULL, &errfds, tvp);
		if (ret < 0 && errno != EINTR)
			exit_errno("select");
	}

	if (ret > 0) {
		for (i = 0; i < VECTOR_LEN(ctx.pollfds); i++) {
			struct pollfd *pollfd = &VECTOR_INDEX(ctx.pollfds, i);
			pollfd->revents = 0;

			if (pollfd->fd < 0)
				continue;

			if (FD_ISSET(pollfd->fd, &readfds))
				pollfd->revents |= POLLIN;
			if (FD_ISSET(pollfd->fd, &errfds))
				pollfd->revents |= POLLERR;
		}
	}

#else
	ret = poll(VECTOR_DATA(ctx.pollfds), VECTOR_LEN(ctx.pollfds), timeout);
	if (ret < 0 && errno != EINTR)
		exit_errno("poll");
#endif

	pthread_sigmask(SIG_SETMASK, &oldset, NULL);
	fastd_update_time();

	if (ret <= 0)
		return;

	if (VECTOR_INDEX(ctx.pollfds, 0).revents & POLLIN)
		fastd_tuntap_handle();
	if (VECTOR_INDEX(ctx.pollfds, 1).revents & POLLIN)
		fastd_async_handle();

#ifdef WITH_STATUS_SOCKET
	if (VECTOR_INDEX(ctx.pollfds, 2).revents & POLLIN)
		fastd_status_handle();
#endif

	for (i = 0; i < ctx.n_socks; i++) {
		if (VECTOR_INDEX(ctx.pollfds, 3+i).revents & (POLLERR|POLLHUP|POLLNVAL)) {
			fastd_socket_error(&ctx.socks[i]);
			VECTOR_INDEX(ctx.pollfds, 3+i).fd = -1;
		}
		else if (VECTOR_INDEX(ctx.pollfds, 3+i).revents & POLLIN) {
			fastd_receive(&ctx.socks[i]);
		}
	}

	for (i = 0; i < VECTOR_LEN(ctx.peers); i++) {
		fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i);

		if (VECTOR_INDEX(ctx.pollfds, 3+ctx.n_socks+i).revents & (POLLERR|POLLHUP|POLLNVAL)) {
			fastd_peer_reset_socket(peer);
		}
		else if (VECTOR_INDEX(ctx.pollfds, 3+ctx.n_socks+i).revents & POLLIN) {
			fastd_receive(peer->sock);
		}
	}

	if (VECTOR_LEN(ctx.pollfds) != 3 + ctx.n_socks + VECTOR_LEN(ctx.peers))
		exit_bug("fd count mismatch");
}

#endif