From 864c87e6c05de13f221413b5a581d4d569495e2b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 19 Feb 2019 12:42:29 +0100 Subject: net/SocketAddress: add method GetLocalPath() --- src/net/AllocatedSocketAddress.hxx | 8 ++++++++ src/net/SocketAddress.cxx | 17 ++++++++++++++++- src/net/SocketAddress.hxx | 9 ++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) (limited to 'src') 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 @@ -146,6 +146,14 @@ public: gcc_pure 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 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 + * Copyright 2012-2019 Max Kellermann * * 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 + * Copyright 2012-2019 Max Kellermann * * 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 -- cgit v1.2.3