From 534ab8d451b8a383876e6e6ea9eb8cad37c576bc Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 16 Oct 2023 22:22:27 -0500 Subject: [PATCH] misc bugfixes; added binding to ipv4 address --- core/io/reader.onyx | 4 +++- core/net/net.onyx | 6 +++--- core/net/tcp.onyx | 2 +- core/runtime/platform/onyx/net.onyx | 5 +++++ runtime/onyx_runtime.c | 1 + runtime/src/ort_net_linux.h | 18 ++++++++++++++++++ 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/io/reader.onyx b/core/io/reader.onyx index 503d4972..4d25f063 100644 --- a/core/io/reader.onyx +++ b/core/io/reader.onyx @@ -212,7 +212,9 @@ read_fill_buffer :: (use reader: &Reader, bytes: [] u8) -> Error { write_index += to_write; start += to_write; - reader_read_next_chunk(reader); + if n > 0 { + reader_read_next_chunk(reader); + } } last_byte = cast(i32) bytes[write_index - 1]; diff --git a/core/net/net.onyx b/core/net/net.onyx index d537041d..b19aa548 100644 --- a/core/net/net.onyx +++ b/core/net/net.onyx @@ -436,10 +436,10 @@ str_to_ipv4 :: (ip: str) -> u32 { ipv4_to_str :: (addr: u32) -> str { #persist out: [64] u8; str_addr := conv.format(out, "{}.{}.{}.{}", - (addr >> 24) & 0xff, + (addr >> 0) & 0xff, + (addr >> 8) & 0xff, (addr >> 16) & 0xff, - (addr >> 8) & 0xff, - (addr >> 0) & 0xff); + (addr >> 24) & 0xff); return str_addr; } diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index faddf2f3..699593ad 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -168,7 +168,7 @@ tcp_server_make :: (max_clients := 32, allocator := context.allocator) -> &TCP_S tcp_server_listen :: (use server: &TCP_Server, port: u16) -> bool { sa: SocketAddress; make_ipv4_address(&sa, 0x00000000, port); - if !(socket->bind(&sa)) do return false; + if !socket->bind(&sa) do return false; socket->listen(); socket->option(.NonBlocking, true); diff --git a/core/runtime/platform/onyx/net.onyx b/core/runtime/platform/onyx/net.onyx index 0a619b73..63be33ca 100644 --- a/core/runtime/platform/onyx/net.onyx +++ b/core/runtime/platform/onyx/net.onyx @@ -42,6 +42,10 @@ __net_sock_bind :: (s: SocketData, addr: &SocketAddress) -> bool { return __net_bind_unix(s, ~~path); } + case ipv4: .Inet { + return __net_bind_ipv4(s, ipv4.addr, ipv4.port); + } + case host: .HostPort { return __net_bind_host(s, host.host, host.port); } @@ -261,6 +265,7 @@ __net_resolve :: (host: str, port: u16, out_addrs: [] SocketAddress) -> i32 { __net_connect_host :: (handle: SocketData, host: str, port: u16) -> SocketError --- __net_bind_unix :: (handle: SocketData, path: cstr) -> bool --- + __net_bind_ipv4 :: (handle: SocketData, addr: u32, port: u16) -> bool --- __net_bind_host :: (handle: SocketData, host: str, port: u16) -> bool --- __net_accept :: (handle: SocketData, out_buf: rawptr, out_len: &i32) -> SocketData --- diff --git a/runtime/onyx_runtime.c b/runtime/onyx_runtime.c index 879b69c2..e756e253 100644 --- a/runtime/onyx_runtime.c +++ b/runtime/onyx_runtime.c @@ -95,6 +95,7 @@ ONYX_LIBRARY { ONYX_FUNC(__net_close_socket) ONYX_FUNC(__net_setting_flag) ONYX_FUNC(__net_bind_unix) + ONYX_FUNC(__net_bind_ipv4) ONYX_FUNC(__net_bind_host) ONYX_FUNC(__net_listen) ONYX_FUNC(__net_accept) diff --git a/runtime/src/ort_net_linux.h b/runtime/src/ort_net_linux.h index a2d25129..89175799 100644 --- a/runtime/src/ort_net_linux.h +++ b/runtime/src/ort_net_linux.h @@ -113,6 +113,24 @@ ONYX_DEF(__net_bind_unix, (WASM_I32, WASM_I32), (WASM_I32)) { return NULL; } +ONYX_DEF(__net_bind_ipv4, (WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { + struct sockaddr_in bind_addr; + memset(&bind_addr, 0, sizeof(bind_addr)); + + bind_addr.sin_family = AF_INET; + bind_addr.sin_addr.s_addr = params->data[1].of.i32; + bind_addr.sin_port = htons(params->data[2].of.i32); + + if (bind(params->data[0].of.i32, &bind_addr, sizeof(bind_addr)) >= 0) { + results->data[0] = WASM_I32_VAL(1); + } else { + results->data[0] = WASM_I32_VAL(0); + } + + return NULL; +} + + ONYX_DEF(__net_bind_host, (WASM_I32, WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { int hostlen = params->data[2].of.i32; char *hostname = alloca(hostlen + 1); -- 2.25.1