summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2018-08-20 16:11:17 +0200
committerMax Kellermann <max@musicpd.org>2018-08-20 16:11:17 +0200
commit0ebba73cb8d74d18d6af87bbed99e91bac6a4eac (patch)
treefb35e0fab135ddcfad517bdd46de0c3ac6acfd4d /src/net
parent50c733796540d7a9c1a06a055f26f0023e31ad85 (diff)
net/SocketDescriptor: add method GetPeerCredentials()
Diffstat (limited to 'src/net')
-rw-r--r--src/net/SocketDescriptor.cxx26
-rw-r--r--src/net/SocketDescriptor.hxx14
2 files changed, 40 insertions, 0 deletions
diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx
index 395ed5401..a5f752342 100644
--- a/src/net/SocketDescriptor.cxx
+++ b/src/net/SocketDescriptor.cxx
@@ -220,6 +220,32 @@ SocketDescriptor::GetError()
: errno;
}
+size_t
+SocketDescriptor::GetOption(int level, int name,
+ void *value, size_t size) const
+{
+ assert(IsDefined());
+
+ socklen_t size2 = size;
+ return getsockopt(fd, level, name, (char *)value, &size2) == 0
+ ? size2
+ : 0;
+}
+
+#ifdef HAVE_STRUCT_UCRED
+
+struct ucred
+SocketDescriptor::GetPeerCredentials() const noexcept
+{
+ struct ucred cred;
+ if (GetOption(SOL_SOCKET, SO_PEERCRED,
+ &cred, sizeof(cred)) < sizeof(cred))
+ cred.pid = -1;
+ return cred;
+}
+
+#endif
+
#ifdef _WIN32
bool
diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx
index 33bde8c79..8122b9f9e 100644
--- a/src/net/SocketDescriptor.hxx
+++ b/src/net/SocketDescriptor.hxx
@@ -150,6 +150,20 @@ public:
int GetError();
+ /**
+ * @return the value size or 0 on error
+ */
+ size_t GetOption(int level, int name, void *value, size_t size) const;
+
+#ifdef HAVE_STRUCT_UCRED
+ /**
+ * Receive peer credentials (SO_PEERCRED). On error, the pid
+ * is -1.
+ */
+ gcc_pure
+ struct ucred GetPeerCredentials() const noexcept;
+#endif
+
bool SetOption(int level, int name, const void *value, size_t size);
bool SetBoolOption(int level, int name, bool _value) {