net/SocketAddress: GetSteadyPart() returns std::span
This commit is contained in:
parent
d256d3dabe
commit
d5db4ca0e7
|
@ -64,7 +64,7 @@ AllocatedSocketAddress::SetSize(size_type new_size) noexcept
|
||||||
|
|
||||||
#ifdef HAVE_UN
|
#ifdef HAVE_UN
|
||||||
|
|
||||||
StringView
|
std::string_view
|
||||||
AllocatedSocketAddress::GetLocalRaw() const noexcept
|
AllocatedSocketAddress::GetLocalRaw() const noexcept
|
||||||
{
|
{
|
||||||
return SocketAddress(*this).GetLocalRaw();
|
return SocketAddress(*this).GetLocalRaw();
|
||||||
|
|
|
@ -35,10 +35,13 @@
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#ifdef HAVE_UN
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct sockaddr;
|
struct sockaddr;
|
||||||
struct StringView;
|
|
||||||
|
|
||||||
class AllocatedSocketAddress {
|
class AllocatedSocketAddress {
|
||||||
public:
|
public:
|
||||||
|
@ -145,7 +148,7 @@ public:
|
||||||
* @see SocketAddress::GetLocalRaw()
|
* @see SocketAddress::GetLocalRaw()
|
||||||
*/
|
*/
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
StringView GetLocalRaw() const noexcept;
|
std::string_view GetLocalRaw() const noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see SocketAddress::GetLocalPath()
|
* @see SocketAddress::GetLocalPath()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 Max Kellermann <max.kellermann@gmail.com>
|
* Copyright 2012-2022 Max Kellermann <max.kellermann@gmail.com>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
@ -56,12 +56,12 @@ SocketAddress::operator==(SocketAddress other) const noexcept
|
||||||
|
|
||||||
#ifdef HAVE_UN
|
#ifdef HAVE_UN
|
||||||
|
|
||||||
StringView
|
std::string_view
|
||||||
SocketAddress::GetLocalRaw() const noexcept
|
SocketAddress::GetLocalRaw() const noexcept
|
||||||
{
|
{
|
||||||
if (IsNull() || GetFamily() != AF_LOCAL)
|
if (IsNull() || GetFamily() != AF_LOCAL)
|
||||||
/* not applicable */
|
/* not applicable */
|
||||||
return nullptr;
|
return {};
|
||||||
|
|
||||||
const auto *sun = &CastTo<struct sockaddr_un>();
|
const auto *sun = &CastTo<struct sockaddr_un>();
|
||||||
const auto start = (const char *)sun;
|
const auto start = (const char *)sun;
|
||||||
|
@ -69,7 +69,7 @@ SocketAddress::GetLocalRaw() const noexcept
|
||||||
const size_t header_size = path - start;
|
const size_t header_size = path - start;
|
||||||
if (size < size_type(header_size))
|
if (size < size_type(header_size))
|
||||||
/* malformed address */
|
/* malformed address */
|
||||||
return nullptr;
|
return {};
|
||||||
|
|
||||||
return {path, size - header_size};
|
return {path, size - header_size};
|
||||||
}
|
}
|
||||||
|
@ -84,8 +84,8 @@ SocketAddress::GetLocalPath() const noexcept
|
||||||
/* must be null-terminated */
|
/* must be null-terminated */
|
||||||
raw.back() == 0 &&
|
raw.back() == 0 &&
|
||||||
/* there must not be any other null byte */
|
/* there must not be any other null byte */
|
||||||
std::memchr(raw.data, 0, raw.size - 1) == nullptr
|
std::memchr(raw.data(), 0, raw.size() - 1) == nullptr
|
||||||
? raw.data
|
? raw.data()
|
||||||
: nullptr;
|
: nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,30 +131,36 @@ SocketAddress::GetPort() const noexcept
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr ConstBuffer<void>
|
static std::span<const std::byte>
|
||||||
GetSteadyPart(const struct sockaddr_in &address) noexcept
|
GetSteadyPart(const struct sockaddr_in &address) noexcept
|
||||||
{
|
{
|
||||||
return {&address.sin_addr, sizeof(address.sin_addr)};
|
return {
|
||||||
|
reinterpret_cast<const std::byte *>(&address.sin_addr),
|
||||||
|
sizeof(address.sin_addr),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr ConstBuffer<void>
|
static std::span<const std::byte>
|
||||||
GetSteadyPart(const struct sockaddr_in6 &address) noexcept
|
GetSteadyPart(const struct sockaddr_in6 &address) noexcept
|
||||||
{
|
{
|
||||||
return {&address.sin6_addr, sizeof(address.sin6_addr)};
|
return {
|
||||||
|
reinterpret_cast<const std::byte *>(&address.sin6_addr),
|
||||||
|
sizeof(address.sin6_addr),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ConstBuffer<void>
|
std::span<const std::byte>
|
||||||
SocketAddress::GetSteadyPart() const noexcept
|
SocketAddress::GetSteadyPart() const noexcept
|
||||||
{
|
{
|
||||||
if (IsNull())
|
if (IsNull())
|
||||||
return nullptr;
|
return {};
|
||||||
|
|
||||||
switch (GetFamily()) {
|
switch (GetFamily()) {
|
||||||
#ifdef HAVE_UN
|
#ifdef HAVE_UN
|
||||||
case AF_LOCAL:
|
case AF_LOCAL:
|
||||||
return GetLocalRaw().ToVoid();
|
return std::as_bytes(std::span<const char>{GetLocalRaw()});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
|
@ -166,6 +172,6 @@ SocketAddress::GetSteadyPart() const noexcept
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 Max Kellermann <max.kellermann@gmail.com>
|
* Copyright 2012-2022 Max Kellermann <max.kellermann@gmail.com>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
@ -32,17 +32,19 @@
|
||||||
|
|
||||||
#include "Features.hxx"
|
#include "Features.hxx"
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <span>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h> // IWYU pragma: export
|
#include <winsock2.h> // IWYU pragma: export
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h> // IWYU pragma: export
|
#include <sys/socket.h> // IWYU pragma: export
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename T> struct ConstBuffer;
|
#include <cstddef>
|
||||||
struct StringView;
|
#include <span>
|
||||||
|
|
||||||
|
#ifdef HAVE_UN
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
class IPv4Address;
|
class IPv4Address;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,7 +125,7 @@ public:
|
||||||
* nullptr if not applicable.
|
* nullptr if not applicable.
|
||||||
*/
|
*/
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
StringView GetLocalRaw() const noexcept;
|
std::string_view GetLocalRaw() const noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the local socket path or nullptr if not applicable
|
* Returns the local socket path or nullptr if not applicable
|
||||||
|
@ -175,7 +177,7 @@ public:
|
||||||
* not supported.
|
* not supported.
|
||||||
*/
|
*/
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
ConstBuffer<void> GetSteadyPart() const noexcept;
|
std::span<const std::byte> GetSteadyPart() const noexcept;
|
||||||
|
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
bool operator==(const SocketAddress other) const noexcept;
|
bool operator==(const SocketAddress other) const noexcept;
|
||||||
|
|
Loading…
Reference in New Issue