Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/swoc/include/swoc/Errata.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,26 +337,38 @@ class Errata {
* @param text Text of the message.
* @return *this
*
* The error code is set to the default.
* @a text is localized to @a this and does not need to be persistent.
* The severity is updated to @a severity if the latter is more severe.
*/
self_type &note(Severity severity, std::string_view text);

/** Add an @c Annotation to the top with @a text and local @a severity.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text? cut-n-paste error?

* @param severity The local severity.
* @return *this
*
* The annotation uses the format @c AUTOTEXT_SEVERITY with the argument @a severity.
* @see AUTOTEXT_SEVERITY
*/
self_type &note(Severity severity);

/** Add an @c Annotation to the top based on error code @a code.
* @param code Error code.
* @return *this
*
* The annotation text is constructed as the short, long, and numeric value of @a code.
* The annotation uses the format @c AUTOTEXT_CODE with the argument @a ec.
* @see AUTOTEXT_CODE
*
* @note @a ec is used only for formatting, the @c Errata error code is unchanged.
*/
self_type &note(code_type const &code);
self_type &note(code_type const &ec);

/** Append an @c Annotation to the top based on error code @a code with @a severity.
* @param severity Local severity.
* @param code Error code.
* @return *this
*
* The annotation text is constructed as the short, long, and numeric value of @a code.
* The annotation uses the format @c AUTOTEXT_SEVERITY_CODE with the argument @a severity.
* @see AUTOTEXT_SEVERITY_CODE
*/
self_type &note(code_type const &code, Severity severity);

Expand Down Expand Up @@ -1173,6 +1185,21 @@ Errata::note(Severity severity, std::string_view text) {
return this->note_s(severity, text);
}

inline Errata &
Errata::note(Severity severity) {
return this->note(severity, AUTOTEXT_SEVERITY, severity);
}

inline Errata &
Errata::note(code_type const &code) {
return this->note(AUTOTEXT_CODE, code);
}

inline Errata &
Errata::note(code_type const &ec, Severity severity) {
return this->note(severity, AUTOTEXT_SEVERITY_CODE, severity, ec);
}

template <typename... Args>
Errata &
Errata::note_sv(std::optional<Severity> severity, std::string_view fmt, std::tuple<Args...> const &args) {
Expand Down
27 changes: 10 additions & 17 deletions lib/swoc/include/swoc/IPAddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

#pragma once

#include <netinet/in.h>
#include <sys/socket.h>

#include <cstddef>

#include "swoc/swoc_version.h"
#include "swoc/swoc_meta.h"
#include "swoc/MemSpan.h"
#include "swoc/swoc_ip_util.h"

namespace swoc { inline namespace SWOC_VERSION_NS {

Expand Down Expand Up @@ -836,6 +834,7 @@ IP4Addr::copy_to(sockaddr *sa) const {
}

/// Equality.

inline bool
operator==(IP4Addr const &lhs, IP4Addr const &rhs) {
return lhs._addr == rhs._addr;
Expand Down Expand Up @@ -900,16 +899,12 @@ IP4Addr::is_multicast() const {

inline bool
IP4Addr::is_link_local() const {
return (_addr & 0xFFFF0000) == 0xA9FE0000; // 169.254.0.0/16
return ip::is_link_local_host_order(_addr);
}

inline bool
IP4Addr::is_private() const {
return (((_addr & 0xFF000000) == 0x0A000000) || // 10.0.0.0/8
((_addr & 0xFFC00000) == 0x64400000) || // 100.64.0.0/10
((_addr & 0xFFF00000) == 0xAC100000) || // 172.16.0.0/12
((_addr & 0xFFFF0000) == 0xC0A80000) // 192.168.0.0/16
);
return ip::is_private_host_order(_addr);
}

inline uint8_t
Expand Down Expand Up @@ -1388,14 +1383,12 @@ inline IPAddr::operator IP6Addr() const {

inline bool
IPAddr::operator==(self_type const &that) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self_type has more characters than IPAddr. Are you having a hard time staying busy in retirement?

switch (_family) {
case AF_INET:
return that._family == AF_INET && _addr._ip4 == that._addr._ip4;
case AF_INET6:
return that._family == AF_INET6 && _addr._ip6 == that._addr._ip6;
default:
return !that.is_valid();
}
return _family == that.family() &&
(
( this->is_ip4() && _addr._ip4 == that._addr._ip4 ) ||
( this->is_ip6() && _addr._ip6 == that._addr._ip6 ) ||
_family == AF_UNSPEC
);
}

inline bool
Expand Down
71 changes: 65 additions & 6 deletions lib/swoc/include/swoc/IPEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

#pragma once

#include <netinet/in.h>
#include <sys/socket.h>

#include <stdexcept>

#include "swoc/swoc_version.h"
#include "swoc/MemSpan.h"
#include "swoc/TextView.h"
#include "swoc/string_view_util.h"
#include "swoc/swoc_ip_util.h"

namespace swoc { inline namespace SWOC_VERSION_NS {

Expand Down Expand Up @@ -198,6 +196,24 @@ union IPEndpoint {
/// @return @c true if this is a loopback address, @c false if not.
bool is_loopback() const;

/// @return @c true if the address is in the link local network.
bool is_link_local() const;

/// @return @c true if the address is in the link local network.
static bool is_link_local(sockaddr const * sa);

/// @return @c true if the address is private.
bool is_private() const;

/// @return @c true if the address is private.
static bool is_private(sockaddr const * sa);

/// @return @c true if the address is multicast.
bool is_multicast() const;

/// @return @c true if the address is multicast.
static bool is_multicast(sockaddr const * sa);

/** Port in network order.
*
* @return The port or 0 if not a valid IP address.
Expand All @@ -217,8 +233,9 @@ union IPEndpoint {
static bool is_valid(sockaddr const *sa);

/// Direct access to port.
/// @return Refernec to the port in the socket address.
/// @return Reference to the port in the socket address.
/// @note If @a sa is not a valid IP address an assertion is thrown.
/// @note The raw port is in network order.
/// @a is_valid
static in_port_t &port(sockaddr *sa);

Expand All @@ -237,10 +254,16 @@ union IPEndpoint {
static in_port_t host_order_port(sockaddr const *sa);

/// Automatic conversion to @c sockaddr.
operator sockaddr *() { return &sa; }
operator sockaddr *();

/// Automatic conversion to @c sockaddr.
operator sockaddr const *() const { return &sa; }
operator sockaddr const *() const;

/// Size of the sockaddr variant based on the family.
size_t sa_size() const;

/// Size of the sockaddr based on the family.
static size_t sa_size(sockaddr const* sa);

/** The address as a byte sequence.
*
Expand Down Expand Up @@ -401,4 +424,40 @@ IPEndpoint::raw_addr() const {
return {};
}

inline bool IPEndpoint::is_link_local() const {
return swoc::ip::is_link_local(&sa);
}

inline bool IPEndpoint::is_link_local(sockaddr const * sa) {
return swoc::ip::is_link_local(sa);
}

inline bool IPEndpoint::is_private() const {
return swoc::ip::is_private(&sa);
}

inline bool IPEndpoint::is_private(sockaddr const * sa) {
return swoc::ip::is_private(sa);
}

inline bool IPEndpoint::is_multicast() const {
return swoc::ip::is_multicast(&sa);
}

inline bool IPEndpoint::is_multicast(sockaddr const * sa) {
return swoc::ip::is_multicast(sa);
}

inline IPEndpoint::operator sockaddr *() { return &sa; }

inline IPEndpoint::operator sockaddr const *() const { return &sa; }

inline size_t IPEndpoint::sa_size() const {
return sa_size(&sa);
}

inline size_t IPEndpoint::sa_size(sockaddr const* sa) {
return AF_INET == sa->sa_family ? sizeof(sockaddr_in) : AF_INET6 == sa->sa_family ? sizeof(sockaddr_in6) : sizeof(sockaddr);
}

}} // namespace swoc::SWOC_VERSION_NS
106 changes: 94 additions & 12 deletions lib/swoc/include/swoc/IPRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,27 @@ class IPRange {
*/
bool load(std::string_view const &text);

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IPAddr const& addr) const;

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IP6Addr const& addr) const;

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IP4Addr const& addr) const;

/// @return The minimum address in the range.
IPAddr min() const;

Expand All @@ -445,22 +466,13 @@ class IPRange {
self_type &clear();

/// @return The IPv4 range.
IP4Range const &
ip4() const {
return _range._ip4;
}
IP4Range const & ip4() const;

/// @return The IPv6 range.
IP6Range const &
ip6() const {
return _range._ip6;
}
IP6Range const & ip6() const;

/// @return The range family.
sa_family_t
family() const {
return _family;
}
sa_family_t family() const;

/** Compute the mask for @a this as a network.
*
Expand Down Expand Up @@ -642,6 +654,27 @@ class IPRangeView {
*/
bool is(sa_family_t family) const;

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IPAddr const& addr) const;

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IP6Addr const& addr) const;

/** Test if an address is in the range.
*
* @param addr Address to test.
* @return @c true if in @a this range, @c false if not.
*/
bool contains(IP4Addr const& addr) const;

/// @return Reference to the viewed IPv4 range.
IP4Range const &ip4() const;

Expand Down Expand Up @@ -1933,6 +1966,17 @@ IPRange::is_ip6() const {
return AF_INET6 == _family;
}

inline sa_family_t IPRange::family() const {
return _family;
}
inline IP4Range const& IPRange::ip4() const {
return _range._ip4;
}

inline IP6Range const& IPRange::ip6() const {
return _range._ip6;
}

inline auto
IPRangeView::clear() -> self_type & {
_family = AF_UNSPEC;
Expand All @@ -1959,6 +2003,26 @@ IPRangeView::is(sa_family_t f) const {
return f == _family;
}

inline bool IPRange::contains(IPAddr const & addr) const {
if (addr.family() != _family) {
return false;
}
if (this->ip4()) {
return _range._ip4.contains(addr.ip4());
} else if (this->is_ip6()) {
return _range._ip6.contains(addr.ip6());
}
return false;
}

inline bool IPRange::contains(IP6Addr const & addr) const {
return this->is_ip6() && _range._ip6.contains(addr);
}

inline bool IPRange::contains(IP4Addr const & addr) const {
return this->is_ip4() && _range._ip4.contains(addr);
}

inline IP4Range const &
IPRangeView::ip4() const {
return *_raw._4;
Expand Down Expand Up @@ -2003,6 +2067,24 @@ IPRangeView::max() const {
return AF_INET == _family ? _raw._4->max() : AF_INET6 == _family ? _raw._6->max() : IPAddr::INVALID;
}

inline bool IPRangeView::contains(IPAddr const& addr) const {
if (_family != addr.family()) {
return false;
}
return (_family == addr.family() ) &&
( ( this->is_ip4() && _raw._4->contains(addr.ip4()) ) ||
( this->is_ip6() && _raw._6->contains(addr.ip6()) )
);
}

inline bool IPRangeView::contains(IP6Addr const& addr) const {
return this->is_ip6() && _raw._6->contains(addr);
}

inline bool IPRangeView::contains(IP4Addr const& addr) const {
return this->is_ip4() && _raw._4->contains(addr);
}

// +++ IPNet +++

inline IP4Net::IP4Net(swoc::IP4Addr addr, swoc::IPMask mask) : _addr(addr & mask), _mask(mask) {}
Expand Down
Loading