From ede7434901e843674c99a5c5f8328532799ebdd9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 21 Aug 2018 10:57:46 +0200 Subject: [PATCH] net/SocketAddress: add GetSteadyPart() --- src/net/SocketAddress.cxx | 37 +++++++++++++++++++++++++++++++++++++ src/net/SocketAddress.hxx | 11 +++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/net/SocketAddress.cxx b/src/net/SocketAddress.cxx index b4d1628db..a3abb8c3f 100644 --- a/src/net/SocketAddress.cxx +++ b/src/net/SocketAddress.cxx @@ -107,4 +107,41 @@ SocketAddress::GetPort() const noexcept } } +static constexpr ConstBuffer +GetSteadyPart(const struct sockaddr_in &address) noexcept +{ + return {&address.sin_addr, sizeof(address.sin_addr)}; +} + +static constexpr ConstBuffer +GetSteadyPart(const struct sockaddr_in6 &address) noexcept +{ + return {&address.sin6_addr, sizeof(address.sin6_addr)}; +} + #endif + +ConstBuffer +SocketAddress::GetSteadyPart() const noexcept +{ + if (IsNull()) + return nullptr; + + switch (GetFamily()) { +#ifdef HAVE_UN + case AF_LOCAL: + return GetLocalRaw().ToVoid(); +#endif + +#ifdef HAVE_TCP + case AF_INET: + return ::GetSteadyPart(*(const struct sockaddr_in *)(const void *)GetAddress()); + + case AF_INET6: + return ::GetSteadyPart(*(const struct sockaddr_in6 *)(const void *)GetAddress()); +#endif + + default: + return nullptr; + } +} diff --git a/src/net/SocketAddress.hxx b/src/net/SocketAddress.hxx index 7b0ac1b92..c385b232a 100644 --- a/src/net/SocketAddress.hxx +++ b/src/net/SocketAddress.hxx @@ -41,6 +41,7 @@ #include #endif +template struct ConstBuffer; struct StringView; /** @@ -127,6 +128,16 @@ public: unsigned GetPort() const noexcept; #endif + /** + * Return a buffer pointing to the "steady" portion of the + * address, i.e. without volatile parts like the port number. + * This buffer is useful for hashing the address, but not so + * much for anything else. Returns nullptr if the address is + * not supported. + */ + gcc_pure + ConstBuffer GetSteadyPart() const noexcept; + gcc_pure bool operator==(const SocketAddress other) const noexcept;