diff --git a/core/net/common.odin b/core/net/common.odin index 6be377aad..ce8a6a721 100644 --- a/core/net/common.odin +++ b/core/net/common.odin @@ -1,3 +1,12 @@ +package net +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. + + This file collects structs, enums and settings applicable to the entire package in one handy place. + Platform-specific ones can be found in their respective `*_windows.odin` and similar files. +*/ + /* Copyright 2022 Tetralux Copyright 2022 Colin Davidson @@ -10,15 +19,6 @@ Jeroen van Rijn: Cross platform unification, code style, documentation */ -/* - Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. - For other protocols and their features, see subdirectories of this package. - - This file collects structs, enums and settings applicable to the entire package in one handy place. - Platform-specific ones can be found in their respective `*_windows.odin` and similar files. -*/ -package net - import "core:runtime" /* diff --git a/core/net/errors_darwin.odin b/core/net/errors_darwin.odin index 3ee28bc9b..8e256a9de 100644 --- a/core/net/errors_darwin.odin +++ b/core/net/errors_darwin.odin @@ -1,4 +1,11 @@ +package net // +build darwin + +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. +*/ + /* Copyright 2022 Tetralux Copyright 2022 Colin Davidson @@ -11,12 +18,6 @@ Jeroen van Rijn: Cross platform unification, code style, documentation */ -/* - Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. - For other protocols and their features, see subdirectories of this package. -*/ -package net - import "core:c" import "core:os" @@ -179,4 +180,4 @@ Socket_Option_Error :: enum c.int { Invalid_Option_For_Socket = c.int(os.ENOPROTOOPT), Reset_When_Keepalive_Set = c.int(os.ENOTCONN), Not_Socket = c.int(os.ENOTSOCK), -} +} \ No newline at end of file diff --git a/core/net/errors_linux.odin b/core/net/errors_linux.odin index 16a5411ea..fd2ccab03 100644 --- a/core/net/errors_linux.odin +++ b/core/net/errors_linux.odin @@ -1,4 +1,11 @@ +package net // +build linux + +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. +*/ + /* Copyright 2022 Tetralux Copyright 2022 Colin Davidson @@ -11,12 +18,6 @@ Jeroen van Rijn: Cross platform unification, code style, documentation */ -/* - Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. - For other protocols and their features, see subdirectories of this package. -*/ -package net - import "core:c" import "core:os" diff --git a/core/net/errors_windows.odin b/core/net/errors_windows.odin index 9f9589f29..c45ebbabf 100644 --- a/core/net/errors_windows.odin +++ b/core/net/errors_windows.odin @@ -1,4 +1,11 @@ +package net // +build windows + +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. +*/ + /* Copyright 2022 Tetralux Copyright 2022 Colin Davidson @@ -11,12 +18,6 @@ Jeroen van Rijn: Cross platform unification, code style, documentation */ -/* - Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. - For other protocols and their features, see subdirectories of this package. -*/ -package net - import "core:c" import win "core:sys/windows" diff --git a/core/net/interface.odin b/core/net/interface.odin index 5e6d09830..68eb73aa6 100644 --- a/core/net/interface.odin +++ b/core/net/interface.odin @@ -18,6 +18,15 @@ package net import "core:strings" +MAX_INTERFACE_ENUMERATION_TRIES :: 3 + +/* + `enumerate_interfaces` retrieves a list of network interfaces with their associated properties. +*/ +enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { + return _enumerate_interfaces(allocator) +} + /* `destroy_interfaces` cleans up a list of network interfaces retrieved by e.g. `enumerate_interfaces`. */ diff --git a/core/net/interface_darwin.odin b/core/net/interface_darwin.odin index a61d63377..90d996a4a 100644 --- a/core/net/interface_darwin.odin +++ b/core/net/interface_darwin.odin @@ -17,7 +17,16 @@ package net Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver Jeroen van Rijn: Cross platform unification, code style, documentation - - TODO: Implement. Can probably use the (current) Linux implementation, - which will itself be switched over to talking to the kernel via NETLINK protocol once we have raw sockets. */ + +@(private) +_enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { + context.allocator = allocator + + + // TODO: Implement. Can probably use the (current) Linux implementation, + // which will itself be switched over to talking to the kernel via NETLINK protocol + // once we have raw sockets. + + unimplemented() +} \ No newline at end of file diff --git a/core/net/interface_linux.odin b/core/net/interface_linux.odin index 255a96f0b..e889cfa97 100644 --- a/core/net/interface_linux.odin +++ b/core/net/interface_linux.odin @@ -24,10 +24,8 @@ package net import "core:os" import "core:strings" -/* - `enumerate_interfaces` retrieves a list of network interfaces with their associated properties. -*/ -enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { +@(private) +_enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { context.allocator = allocator head: ^os.ifaddrs @@ -40,7 +38,6 @@ enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []N Unlike Windows, *nix regrettably doesn't return all it knows about an interface in one big struct. We're going to have to iterate over a list and coalesce information as we go. */ - ifaces: map[string]^Network_Interface defer delete(ifaces) @@ -67,7 +64,7 @@ enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []N case os.AF_PACKET: /* - For some obscure reason the 64-bit `getifaddrs` calls returns a pointer to a + For some obscure reason the 64-bit `getifaddrs` call returns a pointer to a 32-bit `RTNL_LINK_STATS` structure, which of course means that tx/rx byte count is truncated beyond usefulness. @@ -123,7 +120,6 @@ enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []N if .LOOPBACK in ifaddr.flags { state |= {.Loopback} } - iface.link.state = state } @@ -140,6 +136,5 @@ enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []N append(&_interfaces, iface^) free(iface) } - return _interfaces[:], {} } \ No newline at end of file diff --git a/core/net/interface_windows.odin b/core/net/interface_windows.odin index c4c0ad1bc..f8bac253a 100644 --- a/core/net/interface_windows.odin +++ b/core/net/interface_windows.odin @@ -1,4 +1,11 @@ +package net //+build windows + +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. +*/ + /* Copyright 2022 Tetralux Copyright 2022 Colin Davidson @@ -11,28 +18,17 @@ Jeroen van Rijn: Cross platform unification, code style, documentation */ -/* - Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. - For other protocols and their features, see subdirectories of this package. -*/ -package net - import sys "core:sys/windows" import strings "core:strings" -MAX_INTERFACE_ENUMERATION_TRIES :: 3 - -/* - `enumerate_interfaces` retrieves a list of network interfaces with their associated properties. -*/ -enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { +_enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) { context.allocator = allocator buf: []u8 defer delete(buf) - buf_size: u32 - res: u32 + buf_size: u32 + res: u32 gaa: for _ in 1..=MAX_INTERFACE_ENUMERATION_TRIES { res = sys.get_adapters_addresses( @@ -178,4 +174,4 @@ parse_socket_address :: proc(addr_in: sys.SOCKET_ADDRESS) -> (addr: Endpoint) { case: return // Empty or invalid address type } unreachable() -} +} \ No newline at end of file