diff --git a/src/net/AllocatedSocketAddress.cxx b/src/net/AllocatedSocketAddress.cxx index e2ec195a7..6b27e6e26 100644 --- a/src/net/AllocatedSocketAddress.cxx +++ b/src/net/AllocatedSocketAddress.cxx @@ -62,6 +62,27 @@ AllocatedSocketAddress::SetLocal(const char *path) noexcept sun->sun_path[0] = 0; } +void +AllocatedSocketAddress::SetLocal(std::string_view path) noexcept +{ + const bool is_abstract = path.starts_with('@'); + + /* sun_path must be null-terminated unless it's an abstract + socket */ + const size_t path_length = path.size() + !is_abstract; + + struct sockaddr_un *sun; + SetSize(sizeof(*sun) - sizeof(sun->sun_path) + path_length); + sun = (struct sockaddr_un *)address; + sun->sun_family = AF_LOCAL; + + auto out = std::copy(path.begin(), path.end(), sun->sun_path); + if (is_abstract) + sun->sun_path[0] = 0; + else + *out = 0; +} + #endif #ifdef HAVE_TCP diff --git a/src/net/AllocatedSocketAddress.hxx b/src/net/AllocatedSocketAddress.hxx index a2ac7a471..7e8869fbf 100644 --- a/src/net/AllocatedSocketAddress.hxx +++ b/src/net/AllocatedSocketAddress.hxx @@ -141,6 +141,7 @@ public: * address. */ void SetLocal(const char *path) noexcept; + void SetLocal(std::string_view path) noexcept; #endif #ifdef HAVE_TCP