Browse Source

Clean up some FreeBSD `core:net` code

Feoramund 1 year ago
parent
commit
61e770d943
3 changed files with 33 additions and 30 deletions
  1. 20 17
      core/net/errors_freebsd.odin
  2. 11 11
      core/net/socket_freebsd.odin
  3. 2 2
      core/sys/freebsd/syscalls.odin

+ 20 - 17
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 {

+ 11 - 11
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,

+ 2 - 2
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)