Browse Source

Coalesce more.

Jeroen van Rijn 2 years ago
parent
commit
5267a864db

+ 9 - 9
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        <[email protected]>
 	Copyright 2022 Colin Davidson  <[email protected]>
@@ -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"
 
 /*

+ 8 - 7
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        <[email protected]>
 	Copyright 2022 Colin Davidson  <[email protected]>
@@ -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),
-}
+}

+ 7 - 6
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        <[email protected]>
 	Copyright 2022 Colin Davidson  <[email protected]>
@@ -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"
 

+ 7 - 6
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        <[email protected]>
 	Copyright 2022 Colin Davidson  <[email protected]>
@@ -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"
 

+ 9 - 0
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`.
 */

+ 12 - 3
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()
+}

+ 3 - 8
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[:], {}
 }

+ 11 - 15
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        <[email protected]>
 	Copyright 2022 Colin Davidson  <[email protected]>
@@ -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()
-}
+}