summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/sockets_utils.c
blob: 4f5658a5e085b2ba9ec37e4e4433fa41c849ef7d (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <errno.h>

#include <wasi/sockets_utils.h>

static network_own_network_t global_network;
static bool global_network_initialized = false;

network_borrow_network_t __wasi_sockets_utils__borrow_network()
{
	if (!global_network_initialized) {
		global_network = instance_network_instance_network();
		global_network_initialized = true;
	}

	return network_borrow_network(global_network);
}

int __wasi_sockets_utils__map_error(network_error_code_t wasi_error)
{
	switch (wasi_error) {
	case NETWORK_ERROR_CODE_ACCESS_DENIED:
		return EACCES;
	case NETWORK_ERROR_CODE_NOT_SUPPORTED:
		return EOPNOTSUPP;
	case NETWORK_ERROR_CODE_INVALID_ARGUMENT:
		return EINVAL;
	case NETWORK_ERROR_CODE_OUT_OF_MEMORY:
		return ENOMEM;
	case NETWORK_ERROR_CODE_TIMEOUT:
		return ETIMEDOUT;
	case NETWORK_ERROR_CODE_CONCURRENCY_CONFLICT:
		return EALREADY;
	case NETWORK_ERROR_CODE_WOULD_BLOCK:
		return EWOULDBLOCK;
	case NETWORK_ERROR_CODE_NEW_SOCKET_LIMIT:
		return EMFILE;
	case NETWORK_ERROR_CODE_ADDRESS_NOT_BINDABLE:
		return EADDRNOTAVAIL;
	case NETWORK_ERROR_CODE_ADDRESS_IN_USE:
		return EADDRINUSE;
	case NETWORK_ERROR_CODE_REMOTE_UNREACHABLE:
		return EHOSTUNREACH;
	case NETWORK_ERROR_CODE_CONNECTION_REFUSED:
		return ECONNREFUSED;
	case NETWORK_ERROR_CODE_CONNECTION_RESET:
		return ECONNRESET;
	case NETWORK_ERROR_CODE_CONNECTION_ABORTED:
		return ECONNABORTED;
	case NETWORK_ERROR_CODE_DATAGRAM_TOO_LARGE:
		return EMSGSIZE;

	case NETWORK_ERROR_CODE_INVALID_STATE:
	case NETWORK_ERROR_CODE_NOT_IN_PROGRESS:
		abort(); // If our internal state checks are working right, these errors should never show up.
		break;

	case NETWORK_ERROR_CODE_NAME_UNRESOLVABLE:
	case NETWORK_ERROR_CODE_TEMPORARY_RESOLVER_FAILURE:
	case NETWORK_ERROR_CODE_PERMANENT_RESOLVER_FAILURE:
		abort(); // These errors are specific to getaddrinfo, which should have filtered these errors out before calling this generic method
		break;

	case NETWORK_ERROR_CODE_UNKNOWN:
	default:
		return EOPNOTSUPP;
	}
}

bool __wasi_sockets_utils__parse_address(
	network_ip_address_family_t expected_family,
	const struct sockaddr *address, socklen_t len,
	network_ip_socket_address_t *result, int *error)
{
	if (address == NULL || len < sizeof(struct sockaddr)) {
		*error = EINVAL;
		return false;
	}

	switch (expected_family) {
	case NETWORK_IP_ADDRESS_FAMILY_IPV4: {
		if (address->sa_family != AF_INET) {
			*error = EAFNOSUPPORT;
			return false;
		}

		if (len < sizeof(struct sockaddr_in)) {
			*error = EINVAL;
			return false;
		}

		struct sockaddr_in *ipv4 = (struct sockaddr_in *)address;
		unsigned ip = ipv4->sin_addr.s_addr;
		unsigned short port = ipv4->sin_port;
		*result = (network_ip_socket_address_t){
            .tag = NETWORK_IP_SOCKET_ADDRESS_IPV4,
            .val = { .ipv4 = {
                .port = ntohs(port), // (port << 8) | (port >> 8),
                .address = { ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24 },
            } },
        };
		return true;
	}
	case NETWORK_IP_ADDRESS_FAMILY_IPV6: {
		if (address->sa_family != AF_INET6) {
			*error = EAFNOSUPPORT;
			return false;
		}

		if (len < sizeof(struct sockaddr_in6)) {
			*error = EINVAL;
			return false;
		}

		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)address;
		unsigned char *ip = (unsigned char *)&(ipv6->sin6_addr.s6_addr);
		unsigned short port = ipv6->sin6_port;
		*result = (network_ip_socket_address_t){
            .tag = NETWORK_IP_SOCKET_ADDRESS_IPV6,
            .val = { .ipv6 = {
                .port = ntohs(port),
                .address = {
                    (((unsigned short)ip[0]) << 8) | ip[1],
                    (((unsigned short)ip[2]) << 8) | ip[3],
                    (((unsigned short)ip[4]) << 8) | ip[5],
                    (((unsigned short)ip[6]) << 8) | ip[7],
                    (((unsigned short)ip[8]) << 8) | ip[9],
                    (((unsigned short)ip[10]) << 8) | ip[11],
                    (((unsigned short)ip[12]) << 8) | ip[13],
                    (((unsigned short)ip[14]) << 8) | ip[15],
                },
                // TODO wasi-sockets: do these need to be endian-reversed?
                .flow_info = ipv6->sin6_flowinfo,
                .scope_id = ipv6->sin6_scope_id,
            } }
        };
		return true;
	}
	default: /* unreachable */
		abort();
	}
}

bool __wasi_sockets_utils__output_addr_validate(
	network_ip_address_family_t expected_family, struct sockaddr *addr,
	socklen_t *addrlen, output_sockaddr_t *result)
{
	// The address parameters must be either both null or both _not_ null.

	if (addr == NULL && addrlen == NULL) {
		*result = (output_sockaddr_t){ .tag = OUTPUT_SOCKADDR_NULL,
					       .null = {} };
		return true;

	} else if (addr != NULL && addrlen != NULL) {
		if (expected_family == NETWORK_IP_ADDRESS_FAMILY_IPV4) {
			if (*addrlen < sizeof(struct sockaddr_in)) {
				return false;
			}

			*result =
				(output_sockaddr_t){ .tag = OUTPUT_SOCKADDR_V4,
						     .v4 = {
							     .addr = (struct sockaddr_in
									      *)
								     addr,
							     .addrlen = addrlen,
						     } };
			return true;

		} else if (expected_family == NETWORK_IP_ADDRESS_FAMILY_IPV6) {
			if (*addrlen < sizeof(struct sockaddr_in6)) {
				return false;
			}

			*result =
				(output_sockaddr_t){ .tag = OUTPUT_SOCKADDR_V6,
						     .v6 = {
							     .addr = (struct sockaddr_in6
									      *)
								     addr,
							     .addrlen = addrlen,
						     } };
			return true;

		} else {
			abort();
		}

	} else {
		return false;
	}
}

void __wasi_sockets_utils__output_addr_write(
	const network_ip_socket_address_t input, output_sockaddr_t *output)
{
	switch (input.tag) {
	case NETWORK_IP_SOCKET_ADDRESS_IPV4: {
		if (output->tag != OUTPUT_SOCKADDR_V4) {
			abort();
		}

		network_ipv4_socket_address_t input_v4 = input.val.ipv4;
		network_ipv4_address_t ip = input_v4.address;

		*output->v4.addrlen = sizeof(struct sockaddr_in);
		*output->v4.addr = (struct sockaddr_in){
			.sin_family = AF_INET,
			.sin_port = htons(input_v4.port),
			.sin_addr = { .s_addr = ip.f0 | (ip.f1 << 8) |
						(ip.f2 << 16) | (ip.f3 << 24) },
		};
		return;
	}
	case NETWORK_IP_SOCKET_ADDRESS_IPV6: {
		if (output->tag != OUTPUT_SOCKADDR_V6) {
			abort();
		}

		network_ipv6_socket_address_t input_v6 = input.val.ipv6;
		network_ipv6_address_t ip = input_v6.address;

		*output->v6.addrlen = sizeof(struct sockaddr_in6);
		*output->v6.addr = (struct sockaddr_in6) {
            .sin6_family = AF_INET6,
            .sin6_port = htons(input_v6.port),
            .sin6_addr = { .s6_addr = {
                ip.f0 >> 8, ip.f0 & 0xFF,
                ip.f1 >> 8, ip.f1 & 0xFF,
                ip.f2 >> 8, ip.f2 & 0xFF,
                ip.f3 >> 8, ip.f3 & 0xFF,
                ip.f4 >> 8, ip.f4 & 0xFF,
                ip.f5 >> 8, ip.f5 & 0xFF,
                ip.f6 >> 8, ip.f6 & 0xFF,
                ip.f7 >> 8, ip.f7 & 0xFF,
            } },
            // TODO wasi-sockets: do these need to be endian-reversed?
            .sin6_flowinfo = input_v6.flow_info,
            .sin6_scope_id = input_v6.scope_id,
        };
		return;
	}
	default: /* unreachable */
		abort();
	}
}

int __wasi_sockets_utils__posix_family(network_ip_address_family_t wasi_family)
{
	switch (wasi_family) {
	case NETWORK_IP_ADDRESS_FAMILY_IPV4:
		return AF_INET;
	case NETWORK_IP_ADDRESS_FAMILY_IPV6:
		return AF_INET6;
	default: /* unreachable */
		abort();
	}
}

network_ip_socket_address_t
__wasi_sockets_utils__any_addr(network_ip_address_family_t family)
{
	switch (family) {
	case NETWORK_IP_ADDRESS_FAMILY_IPV4:
		return (network_ip_socket_address_t){ .tag = NETWORK_IP_SOCKET_ADDRESS_IPV4,
						      .val = {
							      .ipv4 = {
								      .port = 0,
								      .address = { 0,
										   0,
										   0,
										   0 },
							      } } };
	case NETWORK_IP_ADDRESS_FAMILY_IPV6:
		return (network_ip_socket_address_t){ .tag = NETWORK_IP_SOCKET_ADDRESS_IPV6,
						      .val = {
							      .ipv6 = {
								      .port = 0,
								      .address = { 0,
										   0,
										   0,
										   0,
										   0,
										   0,
										   0,
										   0 },
								      .flow_info =
									      0,
								      .scope_id =
									      0,
							      } } };
	default: /* unreachable */
		abort();
	}
}

int __wasi_sockets_utils__tcp_bind(tcp_socket_t *socket,
				   network_ip_socket_address_t *address)
{
	tcp_socket_state_unbound_t unbound;
	if (socket->state.tag == TCP_SOCKET_STATE_UNBOUND) {
		unbound = socket->state.unbound;
	} else {
		errno = EINVAL;
		return -1;
	}

	network_error_code_t error;
	network_borrow_network_t network_borrow =
		__wasi_sockets_utils__borrow_network();
	tcp_borrow_tcp_socket_t socket_borrow =
		tcp_borrow_tcp_socket(socket->socket);

	if (!tcp_method_tcp_socket_start_bind(socket_borrow, network_borrow,
					      address, &error)) {
		errno = __wasi_sockets_utils__map_error(error);
		return -1;
	}

	// Bind has successfully started. Attempt to finish it:
	while (!tcp_method_tcp_socket_finish_bind(socket_borrow, &error)) {
		if (error == NETWORK_ERROR_CODE_WOULD_BLOCK) {
			poll_borrow_pollable_t pollable_borrow =
				poll_borrow_pollable(socket->socket_pollable);
			poll_method_pollable_block(pollable_borrow);
		} else {
			errno = __wasi_sockets_utils__map_error(error);
			return -1;
		}
	}

	// Bind successful.

	socket->state =
		(tcp_socket_state_t){ .tag = TCP_SOCKET_STATE_BOUND,
				      .bound = { /* No additional state */ } };
	return 0;
}

int __wasi_sockets_utils__udp_bind(udp_socket_t *socket,
				   network_ip_socket_address_t *address)
{
	udp_socket_state_unbound_t unbound;
	if (socket->state.tag == UDP_SOCKET_STATE_UNBOUND) {
		unbound = socket->state.unbound;
	} else {
		errno = EINVAL;
		return -1;
	}

	network_error_code_t error;
	network_borrow_network_t network_borrow =
		__wasi_sockets_utils__borrow_network();
	udp_borrow_udp_socket_t socket_borrow =
		udp_borrow_udp_socket(socket->socket);

	if (!udp_method_udp_socket_start_bind(socket_borrow, network_borrow,
					      address, &error)) {
		errno = __wasi_sockets_utils__map_error(error);
		return -1;
	}

	// Bind has successfully started. Attempt to finish it:
	while (!udp_method_udp_socket_finish_bind(socket_borrow, &error)) {
		if (error == NETWORK_ERROR_CODE_WOULD_BLOCK) {
			poll_borrow_pollable_t pollable_borrow =
				poll_borrow_pollable(socket->socket_pollable);
			poll_method_pollable_block(pollable_borrow);
		} else {
			errno = __wasi_sockets_utils__map_error(error);
			return -1;
		}
	}

	// Bind successful.

	socket->state =
		(udp_socket_state_t){ .tag = UDP_SOCKET_STATE_BOUND_NOSTREAMS,
				      .bound_nostreams = {} };
	return 0;
}

bool __wasi_sockets_utils__create_streams(
	udp_borrow_udp_socket_t socket_borrow,
	network_ip_socket_address_t *remote_address,
	udp_socket_streams_t *result, network_error_code_t *error)
{
	udp_tuple2_own_incoming_datagram_stream_own_outgoing_datagram_stream_t
		io;
	if (!udp_method_udp_socket_stream(socket_borrow, remote_address, &io,
					  error)) {
		return false;
	}

	udp_own_incoming_datagram_stream_t incoming = io.f0;
	udp_borrow_incoming_datagram_stream_t incoming_borrow =
		udp_borrow_incoming_datagram_stream(incoming);
	poll_own_pollable_t incoming_pollable =
		udp_method_incoming_datagram_stream_subscribe(incoming_borrow);

	udp_own_outgoing_datagram_stream_t outgoing = io.f1;
	udp_borrow_outgoing_datagram_stream_t outgoing_borrow =
		udp_borrow_outgoing_datagram_stream(outgoing);
	poll_own_pollable_t outgoing_pollable =
		udp_method_outgoing_datagram_stream_subscribe(outgoing_borrow);

	*result = (udp_socket_streams_t){
		.incoming = incoming,
		.incoming_pollable = incoming_pollable,
		.outgoing = outgoing,
		.outgoing_pollable = outgoing_pollable,
	};
	return true;
}

void __wasi_sockets_utils__drop_streams(udp_socket_streams_t streams)
{
	poll_pollable_drop_own(streams.incoming_pollable);
	poll_pollable_drop_own(streams.outgoing_pollable);
	udp_incoming_datagram_stream_drop_own(streams.incoming);
	udp_outgoing_datagram_stream_drop_own(streams.outgoing);
}

bool __wasi_sockets_utils__stream(
	udp_socket_t *socket,
	network_ip_socket_address_t
		*remote_address, // May be null to "disconnect"
	udp_socket_streams_t *result, network_error_code_t *error)
{
	// Assert that:
	// - We're already bound. This is required by WASI.
	// - We have no active streams. From WASI:
	//   > Implementations may trap if the streams returned by a previous
	//   > invocation haven't been dropped yet before calling `stream` again.
	if (socket->state.tag != UDP_SOCKET_STATE_BOUND_NOSTREAMS) {
		abort();
	}

	udp_borrow_udp_socket_t socket_borrow =
		udp_borrow_udp_socket(socket->socket);

	if (!__wasi_sockets_utils__create_streams(socket_borrow, remote_address,
						  result, error)) {
		return false;
	}

	if (remote_address != NULL) {
		socket->state =
			(udp_socket_state_t){ .tag = UDP_SOCKET_STATE_CONNECTED,
					      .connected = {
						      .streams = *result,
					      } };
	} else {
		socket->state =
			(udp_socket_state_t){ .tag = UDP_SOCKET_STATE_BOUND_STREAMING,
					      .bound_streaming = {
						      .streams = *result,
					      } };
	}

	return true;
}