From 18b4ce4e7dcd658e7e411b50aca4313328953135 Mon Sep 17 00:00:00 2001 From: Book-reader Date: Tue, 19 Aug 2025 21:36:18 +1200 Subject: [PATCH] fix Socket.get_option calling `setsockopt` instead of `getsockopt` (#2421) * fix Socket.get_option * `Socket.get_option` didn't properly call `getsockopt`, and `getsockopt` had an invalid signature. --------- Co-authored-by: Christoffer Lerno --- lib/std/net/os/common.c3 | 31 ++++++++++++++++++++++++++++++- lib/std/net/socket.c3 | 3 ++- releasenotes.md | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/std/net/os/common.c3 b/lib/std/net/os/common.c3 index 59a6df2d9..e85dbb44c 100644 --- a/lib/std/net/os/common.c3 +++ b/lib/std/net/os/common.c3 @@ -53,10 +53,39 @@ const AIFamily AF_APPLETALK = PLATFORM_AF_APPLETALK; const O_NONBLOCK = PLATFORM_O_NONBLOCK; + +<* + The getaddrinfo() function is used to get a list of IP addresses and port numbers for host hostname and service servname. + + @param [in] nodename + @param [in] servname + @param [in] hints + @param [out] res + @require (void*)nodename || (void*)servname : "One the names must be non-null" +*> extern fn CInt getaddrinfo(ZString nodename, ZString servname, AddrInfo* hints, AddrInfo** res) @if(SUPPORTS_INET); + +<* + freeaddrinfo() frees an AddrInfo created by getaddrinfo. + + @param [&in] res +*> extern fn void freeaddrinfo(AddrInfo* res) @if(SUPPORTS_INET); + +<* + Set options on a socket. + + @param [out] optval +*> extern fn CInt setsockopt(NativeSocket socket, CInt level, CInt optname, void* optval, Socklen_t optlen) @if(SUPPORTS_INET); -extern fn CInt getsockopt(NativeSocket socket, CInt level, CInt optname, void* optval, Socklen_t optlen) @if(SUPPORTS_INET); + +<* + Get options on a socket + + @param [in] optval + @param [inout] optlen +*> +extern fn CInt getsockopt(NativeSocket socket, CInt level, CInt optname, void* optval, Socklen_t* optlen) @if(SUPPORTS_INET); module std::net::os @if(!env::LIBC || !(env::WIN32 || env::DARWIN || env::LINUX || env::ANDROID || env::OPENBSD)); diff --git a/lib/std/net/socket.c3 b/lib/std/net/socket.c3 index ab7af7a35..3bd60c88a 100644 --- a/lib/std/net/socket.c3 +++ b/lib/std/net/socket.c3 @@ -116,7 +116,8 @@ fn void? Socket.set_option(&self, SocketOption option, bool value) fn bool? Socket.get_option(&self, SocketOption option) { CInt flag; - int errcode = os::setsockopt(self.sock, os::SOL_SOCKET, option.value, &flag, CInt.sizeof); + Socklen_t socklen = CInt.sizeof; + int errcode = os::getsockopt(self.sock, os::SOL_SOCKET, option.value, &flag, &socklen); if (errcode != 0) return SOCKOPT_FAILED?; return (bool)flag; } diff --git a/releasenotes.md b/releasenotes.md index 67347c21e..80d614272 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -54,6 +54,7 @@ - Fix incorrect panic message when slicing with negative size. - Incorrect type checking when &[] and [] return optional values. - Failed to find subscript overloading on optional values. +- `Socket.get_option` didn't properly call `getsockopt`, and `getsockopt` had an invalid signature. ### Stdlib changes - Add `==` to `Pair`, `Triple` and TzDateTime. Add print to `Pair` and `Triple`.