summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/bind.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:08:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:08:03 +0000
commitf1db79e6e5c383cf76f3bf0dd42115d19591a72b (patch)
tree3f9509008e8a130c45b7e31b1520d66d720493ec /libc-bottom-half/sources/bind.c
parentAdding upstream version 0.0~git20230821.ec4566b. (diff)
downloadwasi-libc-upstream.tar.xz
wasi-libc-upstream.zip
Adding upstream version 0.0~git20240411.9e8c542.upstream/0.0_git20240411.9e8c542upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-bottom-half/sources/bind.c')
-rw-r--r--libc-bottom-half/sources/bind.c53
1 files changed, 53 insertions, 0 deletions
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 <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;
+ }
+}