diff options
author | Max Kellermann <max@musicpd.org> | 2018-08-20 15:59:17 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2018-08-20 15:59:17 +0200 |
commit | 11396d4fba1346ccbd534b13b1a4aa8500a9a60c (patch) | |
tree | 955527bfc80feb935ec5521cc449f7c006f72c80 /src/net/SocketAddress.cxx | |
parent | 4c6ae4e9e8c30e5eb970ef4f599fc2c72df1fd9f (diff) |
net/SocketAddress: add method GetLocalRaw()
Diffstat (limited to 'src/net/SocketAddress.cxx')
-rw-r--r-- | src/net/SocketAddress.cxx | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/net/SocketAddress.cxx b/src/net/SocketAddress.cxx index 8b28832f8..70175a29f 100644 --- a/src/net/SocketAddress.cxx +++ b/src/net/SocketAddress.cxx @@ -29,9 +29,14 @@ #include "config.h" #include "SocketAddress.hxx" +#include "util/StringView.hxx" #include <string.h> +#ifdef HAVE_UN +#include <sys/un.h> +#endif + #ifdef HAVE_TCP #ifdef _WIN32 #include <ws2tcpip.h> @@ -46,6 +51,28 @@ SocketAddress::operator==(SocketAddress other) const noexcept return size == other.size && memcmp(address, other.address, size) == 0; } +#ifdef HAVE_UN + +StringView +SocketAddress::GetLocalRaw() const noexcept +{ + if (IsNull() || GetFamily() != AF_LOCAL) + /* not applicable */ + return nullptr; + + const auto sun = (const struct sockaddr_un *)GetAddress(); + const auto start = (const char *)sun; + const auto path = sun->sun_path; + const size_t header_size = path - start; + if (size < header_size) + /* malformed address */ + return nullptr; + + return {path, size - header_size}; +} + +#endif + #ifdef HAVE_TCP bool |