net/AllocatedSocketAddress: add SetLocal() overload with std::string_view

This commit is contained in:
Max Kellermann 2024-04-16 20:49:06 +02:00 committed by Max Kellermann
parent 4ba288501d
commit 6830cf9dcf
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -141,6 +141,7 @@ public:
* address.
*/
void SetLocal(const char *path) noexcept;
void SetLocal(std::string_view path) noexcept;
#endif
#ifdef HAVE_TCP