net/PeerCredentials: wrapper for struct ucred

This commit is contained in:
Max Kellermann
2025-01-29 17:58:36 +01:00
parent 2c7ca16c4e
commit c7621ec0e4
4 changed files with 90 additions and 25 deletions

View File

@@ -7,6 +7,7 @@
#include "IPv4Address.hxx"
#include "IPv6Address.hxx"
#include "UniqueSocketDescriptor.hxx"
#include "PeerCredentials.hxx"
#ifdef __linux__
#include "io/UniqueFileDescriptor.hxx"
@@ -225,19 +226,19 @@ SocketDescriptor::GetIntOption(int level, int name, int fallback) const noexcept
return value;
}
#ifdef HAVE_STRUCT_UCRED
struct ucred
SocketPeerCredentials
SocketDescriptor::GetPeerCredentials() const noexcept
{
struct ucred cred;
#ifdef HAVE_STRUCT_UCRED
SocketPeerCredentials cred;
if (GetOption(SOL_SOCKET, SO_PEERCRED,
&cred, sizeof(cred)) < sizeof(cred))
cred.pid = -1;
&cred.cred, sizeof(cred.cred)) < sizeof(cred.cred))
return SocketPeerCredentials::Undefined();
return cred;
}
#else
return SocketPeerCredentials::Undefined();
#endif
}
#ifdef __linux__