From: Brendan Hansen Date: Thu, 23 Nov 2023 05:25:45 +0000 (-0600) Subject: changed: `net.make_ipv4_address` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=421ec4b125251c717d0a6c405830747152832a35;p=onyx.git changed: `net.make_ipv4_address` --- diff --git a/CHANGELOG b/CHANGELOG index 89890db3..b02334eb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -24,6 +24,8 @@ Removals: Changes: - Renamed `--no-std` flag to `--no-core`, since Onyx does not call its standard library "std", the name did not make any sense. +- `net.make_ipv4_address` now has a reasonable definition using a string for the IP, + instead of an integer. Bugfixes: - Formatting of days and months were incorrect `time.strftime`. diff --git a/core/alloc/memdebug.onyx b/core/alloc/memdebug.onyx index c47440a8..9df3a61e 100644 --- a/core/alloc/memdebug.onyx +++ b/core/alloc/memdebug.onyx @@ -85,7 +85,7 @@ enable_in_scope :: macro (a: Allocator, port := DEFAULT_PORT) { use core.alloc.memdebug addr: net.SocketAddress; - net.make_ipv4_address(&addr, 0, ~~port); + net.make_ipv4_address(&addr, "0.0.0.0", ~~port); old_allocator := a; dbg := memdebug.make(old_allocator, &addr); diff --git a/core/net/net.onyx b/core/net/net.onyx index 4b4c4f9a..b0d27b73 100644 --- a/core/net/net.onyx +++ b/core/net/net.onyx @@ -122,13 +122,20 @@ SocketAddress.addr_as_str :: (this: &SocketAddress, allocator := context.allocat }; } -make_ipv4_address :: (out: &SocketAddress, addr: u32, port: u16) { - n := (addr & 0xFF) << 24 - | (addr & 0xFF00) << 8 - | (addr & 0xFF0000) >> 8 - | (addr & 0xFF000000) >> 24; +make_ipv4_address :: #match #local {} - *out = .{ Inet = .{ port = port, addr = n } }; +#overload +make_ipv4_address :: (addr: str, port: u16) -> SocketAddress { + ip := str_to_ipv4(addr); + + return .{ Inet = .{ port = port, addr = ip } }; +} + +#overload +make_ipv4_address :: (out: &SocketAddress, addr: str, port: u16) { + ip := str_to_ipv4(addr); + + *out = .{ Inet = .{ port = port, addr = ip } }; } make_unix_address :: (out: &SocketAddress, path: str) { @@ -363,14 +370,6 @@ socket_recvfrom :: (s: &Socket, buffer: [] u8) -> ? SocketRecvFromResult { return .{ Some = .{ sender_addr, res.Ok->unwrap() } }; } -// TODO: Add these back -// host_to_network :: #match #local {} -// #match host_to_network (x: u16) => __net_host_to_net_s(x); -// #match host_to_network (x: u32) => __net_host_to_net_l(x); - -// network_to_host :: #match #local {} -// #match network_to_host (x: u16) => __net_net_to_host_s(x); -// #match network_to_host (x: u32) => __net_net_to_host_l(x); #local __net_socket_vtable := io.Stream_Vtable.{ read = (use s: &Socket, buffer: [] u8) -> (io.Error, u32) { @@ -454,12 +453,13 @@ str_to_ipv4 :: (ip: str) -> u32 { ip_ := ip; res: u32; + shift := 0; for 4 { octet := string.read_until(&ip_, #char "."); string.advance(&ip_, 1); - res = res << 8; - res |= ~~(conv.str_to_i64(octet) & cast(i64) 0xFF); + res |= cast(u32) (conv.str_to_i64(octet) & cast(i64) 0xFF) << shift; + shift += 8; } return res; diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 9b451e99..7f80c764 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -163,7 +163,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); + make_ipv4_address(&sa, "0.0.0.0", port); if !socket->bind(&sa) do return false; socket->listen();