From 61e770d94398b65c3fe4f47898e248a691181dfd Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 5 Aug 2024 00:28:57 -0400 Subject: [PATCH] Clean up some FreeBSD `core:net` code --- core/net/errors_freebsd.odin | 37 ++++++++++++++++++---------------- core/net/socket_freebsd.odin | 22 ++++++++++---------- core/sys/freebsd/syscalls.odin | 4 ++-- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/core/net/errors_freebsd.odin b/core/net/errors_freebsd.odin index 1f98e9228..fc8287834 100644 --- a/core/net/errors_freebsd.odin +++ b/core/net/errors_freebsd.odin @@ -27,8 +27,9 @@ Dial_Error :: enum c.int { Already_Connected = cast(c.int)freebsd.Errno.EISCONN, Timeout = cast(c.int)freebsd.Errno.ETIMEDOUT, Refused_By_Remote_Host = cast(c.int)freebsd.Errno.ECONNREFUSED, - // `Refused` alias for core:net tests. - Refused = cast(c.int)freebsd.Errno.ECONNREFUSED, + // `Refused` alias for `core:net` tests. + // The above default name `Refused_By_Remote_Host` is more explicit. + Refused = Refused_By_Remote_Host, Reset_By_Remote_Host = cast(c.int)freebsd.Errno.ECONNRESET, Network_Unreachable = cast(c.int)freebsd.Errno.ENETUNREACH, Host_Unreachable = cast(c.int)freebsd.Errno.EHOSTUNREACH, @@ -138,13 +139,15 @@ TCP_Send_Error :: enum c.int { specified, and the requested operation would block. */ Would_Block = cast(c.int)freebsd.Errno.EAGAIN, - // NOTE: This error arises for two distinct reasons. - /* The system was unable to allocate an internal buffer. - The operation may succeed when buffers become available. */ + /* NOTE: This error arises for two distinct reasons: - /* The output queue for a network interface was full. - This generally indicates that the interface has stopped - sending, but may be caused by transient congestion. */ + 1. The system was unable to allocate an internal buffer. + The operation may succeed when buffers become available. + + 2. The output queue for a network interface was full. + This generally indicates that the interface has stopped + sending, but may be caused by transient congestion. + */ No_Buffer_Space_Available = cast(c.int)freebsd.Errno.ENOBUFS, Host_Unreachable = cast(c.int)freebsd.Errno.EHOSTUNREACH, @@ -174,16 +177,16 @@ Shutdown_Error :: enum c.int { } Socket_Option_Error :: enum c.int { - None = 0, - Value_Out_Of_Range = -1, - Not_Descriptor = cast(c.int)freebsd.Errno.EBADF, - Not_Socket = cast(c.int)freebsd.Errno.ENOTSOCK, - Unknown_Option_For_Level = cast(c.int)freebsd.Errno.ENOPROTOOPT, - Argument_In_Invalid_Address_Space = cast(c.int)freebsd.Errno.EFAULT, + None = 0, + Value_Out_Of_Range = -1, + Not_Descriptor = cast(c.int)freebsd.Errno.EBADF, + Not_Socket = cast(c.int)freebsd.Errno.ENOTSOCK, + Unknown_Option_For_Level = cast(c.int)freebsd.Errno.ENOPROTOOPT, + Argument_In_Invalid_Address_Space = cast(c.int)freebsd.Errno.EFAULT, // This error can arise for many different reasons. - Invalid_Value = cast(c.int)freebsd.Errno.EINVAL, - System_Memory_Allocation_Failed = cast(c.int)freebsd.Errno.ENOMEM, - Insufficient_System_Resources = cast(c.int)freebsd.Errno.ENOBUFS, + Invalid_Value = cast(c.int)freebsd.Errno.EINVAL, + System_Memory_Allocation_Failed = cast(c.int)freebsd.Errno.ENOMEM, + Insufficient_System_Resources = cast(c.int)freebsd.Errno.ENOBUFS, } Set_Blocking_Error :: enum c.int { diff --git a/core/net/socket_freebsd.odin b/core/net/socket_freebsd.odin index 10bc4c63e..db7bb1f56 100644 --- a/core/net/socket_freebsd.odin +++ b/core/net/socket_freebsd.odin @@ -245,17 +245,17 @@ _set_option :: proc(socket: Any_Socket, option: Socket_Option, value: any, loc : .Reuse_Port, .No_SIGPIPE_From_EPIPE, .Reuse_Port_Load_Balancing: - switch real in value { - case bool: bool_value = cast(b32)real - case b8: bool_value = cast(b32)real - case b16: bool_value = cast(b32)real - case b32: bool_value = real - case b64: bool_value = cast(b32)real - case: - panic("set_option() value must be a boolean here", loc) - } - ptr = &bool_value - len = size_of(bool_value) + switch real in value { + case bool: bool_value = cast(b32)real + case b8: bool_value = cast(b32)real + case b16: bool_value = cast(b32)real + case b32: bool_value = real + case b64: bool_value = cast(b32)real + case: + panic("set_option() value must be a boolean here", loc) + } + ptr = &bool_value + len = size_of(bool_value) case .Linger, .Send_Timeout, diff --git a/core/sys/freebsd/syscalls.odin b/core/sys/freebsd/syscalls.odin index 78628a5e7..506a43bba 100644 --- a/core/sys/freebsd/syscalls.odin +++ b/core/sys/freebsd/syscalls.odin @@ -58,7 +58,7 @@ close :: proc "contextless" (fd: Fd) -> Errno { // // The getpid() function appeared in Version 7 AT&T UNIX. getpid :: proc "contextless" () -> pid_t { - // always succeeds + // This always succeeds. result, _ := intrinsics.syscall_bsd(SYS_getpid) return cast(pid_t)result } @@ -468,7 +468,7 @@ accept4_T :: proc "contextless" (s: Fd, sockaddr: ^$T, flags: Socket_Flags = {}) where intrinsics.type_is_subtype_of(T, Socket_Address_Header) { - // sockaddr must contain a valid pointer, or this will segfault because + // `sockaddr` must contain a valid pointer, or this will segfault because // we're telling the syscall that there's memory available to write to. addrlen: u32 = size_of(T)