diff options
author | Max Kellermann <max@musicpd.org> | 2019-02-19 12:42:29 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2019-02-19 12:50:40 +0100 |
commit | 864c87e6c05de13f221413b5a581d4d569495e2b (patch) | |
tree | d0ae3eb03204b6aa1a656541204ba4c733f275bb /src | |
parent | 1a516cf3c0abadcb0086d9fcdbc6f86550b2b336 (diff) |
net/SocketAddress: add method GetLocalPath()
Diffstat (limited to 'src')
-rw-r--r-- | src/net/AllocatedSocketAddress.hxx | 8 | ||||
-rw-r--r-- | src/net/SocketAddress.cxx | 17 | ||||
-rw-r--r-- | src/net/SocketAddress.hxx | 9 |
3 files changed, 32 insertions, 2 deletions
diff --git a/src/net/AllocatedSocketAddress.hxx b/src/net/AllocatedSocketAddress.hxx index 2bd6b372d..62a9ffba6 100644 --- a/src/net/AllocatedSocketAddress.hxx +++ b/src/net/AllocatedSocketAddress.hxx @@ -147,6 +147,14 @@ public: StringView GetLocalRaw() const noexcept; /** + * @see SocketAddress::GetLocalPath() + */ + gcc_pure + const char *GetLocalPath() const noexcept { + return ((SocketAddress)*this).GetLocalPath(); + } + + /** * Make this a "local" address (UNIX domain socket). If the path * begins with a '@', then the rest specifies an "abstract" local * address. diff --git a/src/net/SocketAddress.cxx b/src/net/SocketAddress.cxx index a845cb8de..2e30e33c3 100644 --- a/src/net/SocketAddress.cxx +++ b/src/net/SocketAddress.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -73,6 +73,21 @@ SocketAddress::GetLocalRaw() const noexcept return {path, size - header_size}; } +const char * +SocketAddress::GetLocalPath() const noexcept +{ + const auto raw = GetLocalRaw(); + return !raw.empty() && + /* must be an absolute path */ + raw.front() == '/' && + /* must be null-terminated */ + raw.back() == 0 && + /* there must not be any other null byte */ + memchr(raw.data, 0, raw.size - 1) == nullptr + ? raw.data + : nullptr; +} + #endif #ifdef HAVE_TCP diff --git a/src/net/SocketAddress.hxx b/src/net/SocketAddress.hxx index 7bc475832..392892c01 100644 --- a/src/net/SocketAddress.hxx +++ b/src/net/SocketAddress.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -106,6 +106,13 @@ public: */ gcc_pure StringView GetLocalRaw() const noexcept; + + /** + * Returns the local socket path or nullptr if not applicable + * (or if the path is corrupt). + */ + gcc_pure + const char *GetLocalPath() const noexcept; #endif #ifdef HAVE_TCP |