This is an automated email from the ASF dual-hosted git repository. bneradt pushed a commit to branch ipsrv in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit c0db5152dee101061368784c9ccf157d9d876b42 Author: Alan M. Carroll <[email protected]> AuthorDate: Sun Oct 23 09:15:23 2022 -0500 Checkpoint - IPsrv --- code/include/swoc/IPAddr.h | 2 + code/include/swoc/IPEndpoint.h | 104 ++++++++++++++++++++++++++++++++++ code/include/swoc/IPRange.h | 125 ----------------------------------------- code/include/swoc/swoc_ip.h | 28 ++++++++- tools/ats-drop.sh | 2 + 5 files changed, 135 insertions(+), 126 deletions(-) diff --git a/code/include/swoc/IPAddr.h b/code/include/swoc/IPAddr.h index e49f660..3a567d1 100644 --- a/code/include/swoc/IPAddr.h +++ b/code/include/swoc/IPAddr.h @@ -10,6 +10,8 @@ #include <sys/socket.h> #include "swoc/swoc_version.h" +#include "swoc/swoc_meta.h" +#include "swoc/MemSpan.h" namespace swoc { inline namespace SWOC_VERSION_NS { diff --git a/code/include/swoc/IPEndpoint.h b/code/include/swoc/IPEndpoint.h index f9e5001..4ae875c 100644 --- a/code/include/swoc/IPEndpoint.h +++ b/code/include/swoc/IPEndpoint.h @@ -5,11 +5,15 @@ */ #pragma once + #include <netinet/in.h> #include <sys/socket.h> +#include <stdexcept> + #include "swoc/swoc_version.h" #include "swoc/TextView.h" + namespace swoc { inline namespace SWOC_VERSION_NS { using ::std::string_view; @@ -169,4 +173,104 @@ union IPEndpoint { static string_view family_name(sa_family_t family); }; +inline IPEndpoint::IPEndpoint() { + sa.sa_family = AF_UNSPEC; +} + +inline IPEndpoint::IPEndpoint(IPAddr const &addr) { + this->assign(addr); +} + +inline IPEndpoint::IPEndpoint(sockaddr const *sa) { + this->assign(sa); +} + +inline IPEndpoint::IPEndpoint(IPEndpoint::self_type const &that) : self_type(&that.sa) {} + +inline IPEndpoint & +IPEndpoint::invalidate() { + sa.sa_family = AF_UNSPEC; + return *this; +} + +inline void +IPEndpoint::invalidate(sockaddr *addr) { + addr->sa_family = AF_UNSPEC; +} + +inline bool +IPEndpoint::is_valid() const { + return sa.sa_family == AF_INET || sa.sa_family == AF_INET6; +} + +inline IPEndpoint & +IPEndpoint::operator=(self_type const &that) { + self_type::assign(&sa, &that.sa); + return *this; +} + +inline IPEndpoint & +IPEndpoint::assign(sockaddr const *src) { + self_type::assign(&sa, src); + return *this; +} + +inline IPEndpoint const & +IPEndpoint::copy_to(sockaddr *addr) const { + self_type::assign(addr, &sa); + return *this; +} + +inline bool +IPEndpoint::is_ip4() const { + return AF_INET == sa.sa_family; +} + +inline bool +IPEndpoint::is_ip6() const { + return AF_INET6 == sa.sa_family; +} + +inline sa_family_t +IPEndpoint::family() const { + return sa.sa_family; +} + +inline in_port_t & +IPEndpoint::network_order_port() { + return self_type::port(&sa); +} + +inline in_port_t +IPEndpoint::network_order_port() const { + return self_type::port(&sa); +} + +inline in_port_t +IPEndpoint::host_order_port() const { + return ntohs(this->network_order_port()); +} + +inline in_port_t & +IPEndpoint::port(sockaddr *sa) { + switch (sa->sa_family) { + case AF_INET: + return reinterpret_cast<sockaddr_in *>(sa)->sin_port; + case AF_INET6: + return reinterpret_cast<sockaddr_in6 *>(sa)->sin6_port; + } + // Force a failure upstream by returning a null reference. + throw std::domain_error("sockaddr is not a valid IP address"); +} + +inline in_port_t +IPEndpoint::port(sockaddr const *sa) { + return self_type::port(const_cast<sockaddr *>(sa)); +} + +inline in_port_t +IPEndpoint::host_order_port(sockaddr const *sa) { + return ntohs(self_type::port(sa)); +} + }} // namespace swoc::SWOC_VERSION_NS diff --git a/code/include/swoc/IPRange.h b/code/include/swoc/IPRange.h index 51ed874..6e88705 100644 --- a/code/include/swoc/IPRange.h +++ b/code/include/swoc/IPRange.h @@ -1190,131 +1190,6 @@ IPSpace<PAYLOAD>::iterator::operator--() -> self_type & { // -------------------------------------------------------------------------- /// ------------------------------------------------------------------------------------ -inline IPEndpoint::IPEndpoint() { - sa.sa_family = AF_UNSPEC; -} - -inline IPEndpoint::IPEndpoint(IPAddr const &addr) { - this->assign(addr); -} - -inline IPEndpoint::IPEndpoint(sockaddr const *sa) { - this->assign(sa); -} - -inline IPEndpoint::IPEndpoint(IPEndpoint::self_type const &that) : self_type(&that.sa) {} - -inline IPEndpoint & -IPEndpoint::invalidate() { - sa.sa_family = AF_UNSPEC; - return *this; -} - -inline void -IPEndpoint::invalidate(sockaddr *addr) { - addr->sa_family = AF_UNSPEC; -} - -inline bool -IPEndpoint::is_valid() const { - return sa.sa_family == AF_INET || sa.sa_family == AF_INET6; -} - -inline IPEndpoint & -IPEndpoint::operator=(self_type const &that) { - self_type::assign(&sa, &that.sa); - return *this; -} - -inline IPEndpoint & -IPEndpoint::assign(sockaddr const *src) { - self_type::assign(&sa, src); - return *this; -} - -inline IPEndpoint const & -IPEndpoint::copy_to(sockaddr *addr) const { - self_type::assign(addr, &sa); - return *this; -} - -inline bool -IPEndpoint::is_ip4() const { - return AF_INET == sa.sa_family; -} - -inline bool -IPEndpoint::is_ip6() const { - return AF_INET6 == sa.sa_family; -} - -inline sa_family_t -IPEndpoint::family() const { - return sa.sa_family; -} - -inline in_port_t & -IPEndpoint::network_order_port() { - return self_type::port(&sa); -} - -inline in_port_t -IPEndpoint::network_order_port() const { - return self_type::port(&sa); -} - -inline in_port_t -IPEndpoint::host_order_port() const { - return ntohs(this->network_order_port()); -} - -inline in_port_t & -IPEndpoint::port(sockaddr *sa) { - switch (sa->sa_family) { - case AF_INET: - return reinterpret_cast<sockaddr_in *>(sa)->sin_port; - case AF_INET6: - return reinterpret_cast<sockaddr_in6 *>(sa)->sin6_port; - } - // Force a failure upstream by returning a null reference. - throw std::domain_error("sockaddr is not a valid IP address"); -} - -inline in_port_t -IPEndpoint::port(sockaddr const *sa) { - return self_type::port(const_cast<sockaddr *>(sa)); -} - -inline in_port_t -IPEndpoint::host_order_port(sockaddr const *sa) { - return ntohs(self_type::port(sa)); -} - -// --- Cross type address operators - -inline bool -operator==(IPAddr const &lhs, IPEndpoint const &rhs) { - return lhs == &rhs.sa; -} - -/// Equality. -inline bool -operator==(IPEndpoint const &lhs, IPAddr const &rhs) { - return &lhs.sa == rhs; -} - -/// Inequality. -inline bool -operator!=(IPAddr const &lhs, IPEndpoint const &rhs) { - return !(lhs == &rhs.sa); -} - -/// Inequality. -inline bool -operator!=(IPEndpoint const &lhs, IPAddr const &rhs) { - return !(rhs == &lhs.sa); -} - // +++ IPRange +++ inline IP4Range::IP4Range(string_view const &text) { diff --git a/code/include/swoc/swoc_ip.h b/code/include/swoc/swoc_ip.h index 514410f..f9f0cfb 100644 --- a/code/include/swoc/swoc_ip.h +++ b/code/include/swoc/swoc_ip.h @@ -14,11 +14,37 @@ #include "swoc/swoc_version.h" #include "swoc/TextView.h" -#include "swoc/DiscreteRange.h" #include "swoc/IPEndpoint.h" #include "swoc/IPAddr.h" #include "swoc/IPSrv.h" #include "swoc/IPRange.h" +namespace swoc { inline namespace SWOC_VERSION_NS { +// --- Cross type address operators + +inline bool +operator==(IPAddr const &lhs, IPEndpoint const &rhs) { + return lhs == &rhs.sa; +} + +/// Equality. +inline bool +operator==(IPEndpoint const &lhs, IPAddr const &rhs) { + return &lhs.sa == rhs; +} + +/// Inequality. +inline bool +operator!=(IPAddr const &lhs, IPEndpoint const &rhs) { + return !(lhs == &rhs.sa); +} + +/// Inequality. +inline bool +operator!=(IPEndpoint const &lhs, IPAddr const &rhs) { + return !(rhs == &lhs.sa); +} + +}} // namespace swoc::SWOC_VERSION_NS diff --git a/tools/ats-drop.sh b/tools/ats-drop.sh index a3487c0..7b948d0 100644 --- a/tools/ats-drop.sh +++ b/tools/ats-drop.sh @@ -100,8 +100,10 @@ library_include_HEADERS = \ include/swoc/RBTree.h \ include/swoc/Scalar.h \ include/swoc/swoc_file.h \ + include/swoc/IPEndpoint.h \ include/swoc/IPAddr.h \ include/swoc/IPSrv.h \ + include/swoc/IPRange.h \ include/swoc/swoc_ip.h \ include/swoc/swoc_meta.h \ include/swoc/swoc_version.h\
