From f1db79e6e5c383cf76f3bf0dd42115d19591a72b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 26 Jun 2024 18:08:03 +0200 Subject: Adding upstream version 0.0~git20240411.9e8c542. Signed-off-by: Daniel Baumann --- libc-bottom-half/sources/bind.c | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 libc-bottom-half/sources/bind.c (limited to 'libc-bottom-half/sources/bind.c') diff --git a/libc-bottom-half/sources/bind.c b/libc-bottom-half/sources/bind.c new file mode 100644 index 0000000..6204691 --- /dev/null +++ b/libc-bottom-half/sources/bind.c @@ -0,0 +1,53 @@ +#include +#include + +#include +#include +#include + +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; + } +} -- cgit v1.2.3