From 1239e075b8f17d89151329a617c25ec08b023a0b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 22 Nov 2023 11:59:10 +0100 Subject: [PATCH] net/SocketDescriptor: add method GetIntOption() --- src/net/SocketDescriptor.cxx | 17 +++++++++-------- src/net/SocketDescriptor.hxx | 3 +++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index aceb605ab..c6846c6d0 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -23,14 +23,7 @@ int SocketDescriptor::GetType() const noexcept { - assert(IsDefined()); - - int type; - socklen_t size = sizeof(type); - return getsockopt(fd, SOL_SOCKET, SO_TYPE, - (char *)&type, &size) == 0 - ? type - : -1; + return GetIntOption(SOL_SOCKET, SO_TYPE, -1); } bool @@ -210,6 +203,14 @@ SocketDescriptor::GetOption(int level, int name, : 0; } +int +SocketDescriptor::GetIntOption(int level, int name, int fallback) const noexcept +{ + int value = fallback; + GetOption(level, name, &value, sizeof(value)); + return value; +} + #ifdef HAVE_STRUCT_UCRED struct ucred diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 1b6120309..0f90fb9d5 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -188,6 +188,9 @@ public: std::size_t GetOption(int level, int name, void *value, std::size_t size) const noexcept; + [[gnu::pure]] + int GetIntOption(int level, int name, int fallback) const noexcept; + #ifdef HAVE_STRUCT_UCRED /** * Receive peer credentials (SO_PEERCRED). On error, the pid