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
|
#include <errno.h>
#include <netinet/in.h>
#include <wasi/api.h>
#include <wasi/descriptor_table.h>
#include <wasi/sockets_utils.h>
int tcp_bind(tcp_socket_t *socket, const struct sockaddr *addr,
socklen_t addrlen)
{
network_ip_socket_address_t local_address;
int parse_err;
if (!__wasi_sockets_utils__parse_address(socket->family, addr, addrlen,
&local_address, &parse_err)) {
errno = parse_err;
return -1;
}
return __wasi_sockets_utils__tcp_bind(socket, &local_address);
}
int udp_bind(udp_socket_t *socket, const struct sockaddr *addr,
socklen_t addrlen)
{
network_ip_socket_address_t local_address;
int parse_err;
if (!__wasi_sockets_utils__parse_address(socket->family, addr, addrlen,
&local_address, &parse_err)) {
errno = parse_err;
return -1;
}
return __wasi_sockets_utils__udp_bind(socket, &local_address);
}
int bind(int socket, const struct sockaddr *addr, socklen_t addrlen)
{
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_bind(&entry->tcp_socket, addr, addrlen);
case DESCRIPTOR_TABLE_ENTRY_UDP_SOCKET:
return udp_bind(&entry->udp_socket, addr, addrlen);
default:
errno = EOPNOTSUPP;
return -1;
}
}
|