summaryrefslogtreecommitdiff
path: root/libnetio/netio_posix.c
blob: f3e9fe0984db3b4816f224c8e5e068fd1f722702 (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
/* netio_posix.c - netio implementation for POSIX-ish systems
 *                 (actually uses a few GNU extensions)
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-Licence-Identifier: AGPL-3.0-or-later
 */

#define _GNU_SOURCE     /* for pthread_sigqueue(3gnu) */
/* misc */
#include <assert.h>     /* for assert() */
#include <errno.h>      /* for errno, EAGAIN, EINVAL */
#include <error.h>      /* for error(3gnu) */
#include <stdlib.h>     /* for abs(), shutdown(), SHUT_RD, SHUT_WR, SHUT_RDWR */
#include <unistd.h>     /* for read(), write() */
/* net */
#include <arpa/inet.h>  /* for htons(3p) */
#include <netinet/in.h> /* for struct sockaddr_in */
#include <sys/socket.h> /* for struct sockaddr, socket(), SOCK_* flags, setsockopt(), SOL_SOCKET, SO_REUSEADDR, bind(), listen(), accept() */
/* async */
#include <pthread.h>    /* for pthread_* */
#include <signal.h>     /* for siginfo_t, struct sigaction, enum sigval, sigaction(), SIGRTMIN, SIGRTMAX, SA_SIGINFO */

#include <libcr/coroutine.h>

#include <libnetio/netio.h>

/* configuration **************************************************************/

#include "config.h"

#ifndef CONFIG_NETIO_NUM_CONNS
#	error config.h must define CONFIG_NETIO_NUM_CONNS
#endif

/* common *********************************************************************/

#define UNUSED(name) /* name __attribute__ ((unused)) */

static int sig_io = 0;

static void handle_sig_io(int UNUSED(sig), siginfo_t *info, void *UNUSED(ucontext)) {
	cr_unpause_from_intrhandler((cid_t)info->si_value.sival_int);
}

static void _netio_init(void) {
	struct sigaction action = {0};

	if (sig_io)
		return;

	sig_io = SIGRTMIN;
	if (sig_io > SIGRTMAX)
		error(1, 0, "SIGRTMAX exceeded");

	action.sa_flags = SA_SIGINFO;
	action.sa_sigaction = handle_sig_io;
	if (sigaction(sig_io, &action, NULL) < 0)
		error(1, errno, "sigaction");
}

#define WAKE_COROUTINE(args) do {                                             \
		int r;                                                        \
		union sigval val = {0};                                       \
		val.sival_int = (int)((args)->cr_coroutine);                  \
		do {                                                          \
			r = pthread_sigqueue((args)->cr_thread, sig_io, val); \
			assert(r == 0 || r == EAGAIN);                        \
		} while (r == EAGAIN);                                        \
	} while (0)

#define RUN_PTHREAD(fn, args) do {                           \
		pthread_t thread;                            \
		int r;                                       \
		r = pthread_create(&thread, NULL, fn, args); \
		if (r)                                       \
			return -abs(r);                      \
		cr_pause_and_yield();                        \
		r = pthread_join(thread, NULL);              \
		if (r)                                       \
			return -abs(r);                      \
	} while (0)

/* listen() *******************************************************************/

int netio_listen(uint16_t port) {
	int sockfd;
	union {
		struct sockaddr_in in;
		struct sockaddr gen;
	} addr = { 0 };

	_netio_init();

	addr.in.sin_family = AF_INET;
	addr.in.sin_port = htons(port);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
		error(1, errno, "socket");
	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
		error(1, errno, "setsockopt");
	if (bind(sockfd, &addr.gen, sizeof addr) < 0)
		error(1, errno, "bind");
	if (listen(sockfd, CONFIG_NETIO_NUM_CONNS) < 0)
		error(1, errno, "listen");

	return sockfd;
}

/* accept() *******************************************************************/

struct _pthread_accept_args {
	pthread_t        cr_thread;
	cid_t            cr_coroutine;

	int              sockfd;

	int             *ret;
};

void *_pthread_accept(void *_args) {
	struct _pthread_accept_args *args = _args;
	*(args->ret) = accept(args->sockfd, NULL, NULL);
	if (*(args->ret) < 0)
		*(args->ret) = -errno;
	WAKE_COROUTINE(args);
	return NULL;
};

int netio_accept(int sock) {
	int ret;
	struct _pthread_accept_args args = {
		.cr_thread    = pthread_self(),
		.cr_coroutine = cr_getcid(),
		.sockfd       = sock,
		.ret          = &ret,
	};
	RUN_PTHREAD(_pthread_accept, &args);
	return ret;
}

/* read() *********************************************************************/

struct _pthread_read_args {
	pthread_t        cr_thread;
	cid_t            cr_coroutine;

	int              connfd;
	void            *buf;
	size_t           count;

	ssize_t         *ret;
};

void *_pthread_read(void *_args) {
	struct _pthread_read_args *args = _args;
	*(args->ret) = read(args->connfd, args->buf, args->count);
	if (*(args->ret) < 0)
		*(args->ret) = -errno;
	WAKE_COROUTINE(args);
	return NULL;
};

ssize_t netio_read(int conn, void *buf, size_t count) {
	ssize_t ret;
	struct _pthread_read_args args = {
		.cr_thread    = pthread_self(),
		.cr_coroutine = cr_getcid(),

		.connfd       = conn,
		.buf          = buf,
		.count        = count,

		.ret          = &ret,
	};
	RUN_PTHREAD(_pthread_read, &args);
	return ret;
}

/* write() ********************************************************************/

struct _pthread_write_args {
	pthread_t        cr_thread;
	cid_t            cr_coroutine;

	int              connfd;
	void            *buf;
	size_t           count;

	ssize_t         *ret;
};

void *_pthread_write(void *_args) {
	struct _pthread_read_args *args = _args;
	size_t done = 0;
	while (done < args->count) {
		ssize_t r = write(args->connfd, args->buf, args->count);
		if (r < 0) {
			*(args->ret) = -errno;
			break;
		}
		done += r;
	}
	if (done == args->count)
		*(args->ret) = done;
	WAKE_COROUTINE(args);
	return NULL;
};

ssize_t netio_write(int conn, void *buf, size_t count) {
	ssize_t ret;
	struct _pthread_write_args args = {
		.cr_thread    = pthread_self(),
		.cr_coroutine = cr_getcid(),

		.connfd       = conn,
		.buf          = buf,
		.count        = count,

		.ret          = &ret,
	};
	RUN_PTHREAD(_pthread_write, &args);
	return ret;
}

/* close() ********************************************************************/

int netio_close(int conn, bool rd, bool wr) {
	int how;
	if (rd && wr)
		how = SHUT_RDWR;
	else if (rd && !wr)
		how = SHUT_RD;
	else if (!rd && wr)
		how = SHUT_WR;
	else
		return -EINVAL;
	return shutdown(conn, how) ? -errno : 0;
}