summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/shutdown.c
blob: 3ffd479cb7d2adcdf06bf5de8008d1803566b4d4 (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
#include <sys/socket.h>

#include <errno.h>

#include <wasi/api.h>
#include <wasi/descriptor_table.h>
#include <wasi/sockets_utils.h>

int tcp_shutdown(tcp_socket_t *socket, int posix_how)
{
	tcp_shutdown_type_t wasi_how;
	switch (posix_how) {
	case SHUT_RD:
		wasi_how = TCP_SHUTDOWN_TYPE_RECEIVE;
		break;
	case SHUT_WR:
		wasi_how = TCP_SHUTDOWN_TYPE_SEND;
		break;
	case SHUT_RDWR:
		wasi_how = TCP_SHUTDOWN_TYPE_BOTH;
		break;
	default:
		errno = EINVAL;
		return -1;
	}

	tcp_socket_state_connected_t connection;
	if (socket->state.tag == TCP_SOCKET_STATE_CONNECTED) {
		connection = socket->state.connected;
	} else {
		errno = ENOTCONN;
		return -1;
	}

	network_error_code_t error;
	tcp_borrow_tcp_socket_t socket_borrow =
		tcp_borrow_tcp_socket(socket->socket);
	if (!tcp_method_tcp_socket_shutdown(socket_borrow, wasi_how, &error)) {
		errno = __wasi_sockets_utils__map_error(error);
		return -1;
	}

	if (posix_how == SHUT_RD || posix_how == SHUT_RDWR) {
		// TODO wasi-sockets: drop input stream (if not already). And
		// update `recv` to take dropped input streams into account.
	}

	if (posix_how == SHUT_WR || posix_how == SHUT_RDWR) {
		// TODO wasi-sockets: drop output stream (if not already). And
		// update `send` to take dropped output streams into account.
	}

	return 0;
}

int udp_shutdown(udp_socket_t *socket, int posix_how)
{
	// UDP has nothing to shut down.
	errno = EOPNOTSUPP;
	return -1;
}

int shutdown(int socket, int how)
{
	descriptor_table_entry_t *entry;
	if (!descriptor_table_get_ref(socket, &entry)) {
		errno = EBADF;
		return -1;
	}

	switch (entry->tag) {
	case DESCRIPTOR_TABLE_ENTRY_TCP_SOCKET:
		return tcp_shutdown(&entry->tcp_socket, how);
	case DESCRIPTOR_TABLE_ENTRY_UDP_SOCKET:
		return udp_shutdown(&entry->udp_socket, how);
	default:
		errno = EOPNOTSUPP;
		return -1;
	}
}