Skip to content

Commit

Permalink
IP: Add "contains" method to IPRange and IPRangeView.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidWallOfCode committed Dec 8, 2023
1 parent 142f2db commit a9d6d47
Showing 1 changed file with 94 additions and 12 deletions.
106 changes: 94 additions & 12 deletions code/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

0 comments on commit a9d6d47

Please sign in to comment.