1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Allow masking IPv4/6 and HW addresses

This commit is contained in:
Matias Fontanini
2016-03-19 15:44:55 -07:00
parent 1f4be63d08
commit 3773443fc8
13 changed files with 127 additions and 33 deletions

View File

@@ -211,7 +211,7 @@ public:
*/
static AddressRange from_mask(const address_type& first, const address_type& mask) {
return AddressRange<address_type>(
Internals::first_address_from_mask(first, mask),
first & mask,
Internals::last_address_from_mask(first, mask),
true
);

View File

@@ -249,6 +249,20 @@ public:
bool operator<(const HWAddress& rhs) const {
return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
}
/**
* \brief Apply a mask to this address
*
* \param mask The mask to be applied
* \return The result of applying the mask to this address
*/
HWAddress operator&(const HWAddress& mask) const {
HWAddress<n> output = *this;
for (size_t i = 0; i < n; ++i) {
output[i] = output[i] & mask[i];
}
return output;
}
/**
* \brief Retrieves the size of this address.
@@ -299,6 +313,15 @@ public:
storage_type operator[](size_t i) const {
return begin()[i];
}
/**
* \brief Retrieves the i-th storage_type in this address.
*
* \param i The element to retrieve.
*/
storage_type& operator[](size_t i) {
return begin()[i];
}
/**
* \brief Writes this HWAddress in hex-notation to a std::ostream.

View File

@@ -183,18 +183,6 @@ bool decrement(HWAddress<n>& addr) {
// Compares sequence numbers as defined by RFC 1982.
int seq_compare(uint32_t seq1, uint32_t seq2);
IPv4Address first_address_from_mask(IPv4Address addr, IPv4Address mask);
IPv6Address first_address_from_mask(IPv6Address addr, const IPv6Address& mask);
template<size_t n>
HWAddress<n> first_address_from_mask(HWAddress<n> addr, const HWAddress<n>& mask) {
typename HWAddress<n>::iterator addr_iter = addr.begin();
for (typename HWAddress<n>::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) {
*addr_iter = *addr_iter & *it;
}
return addr;
}
IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask);
IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask);
template<size_t n>

View File

@@ -123,9 +123,17 @@ public:
* \param rhs The address to be compared.
* \return bool indicating whether this address is less-than rhs.
*/
bool operator< (const IPv4Address& rhs) const {
bool operator<(const IPv4Address& rhs) const {
return ip_addr_ < rhs.ip_addr_;
}
/**
* \brief Apply a mask to this address
*
* \param mask The mask to be applied
* \return The result of applying the mask to this address
*/
IPv4Address operator&(const IPv4Address& mask) const;
/**
* \brief Returns true if this is a private IPv4 address.

View File

@@ -206,6 +206,13 @@ public:
friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr) {
return os << addr.to_string();
}
/**
* Applies a mask to an address
*/
TINS_API friend IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs);
private:
void init(const char* addr);