1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Add missing operators to address classes (#275)

* * add or-operator and a simlple unit test for hw_address, ip_address, ipv6_address
* add not-operator and a simlple unit test for hw_address, ip_address, ipv6_address
* add greater-then-operator and a simlple unit test for ipv6_address
* add new constructor and a simlple unit test for network_interface, which use a ipv6_address to find the nic
* add override the function gateway_from_ip for ipv6_address parameter (untested)

* change the ipv6_address in NotMaskAdress_Test, so that the expceted addresses are valid for the winsock api

* Delete CMakeLists.txt.user

* * add  <=, >, >= operator for HWAddress with tests

* add  <=, >, >= operator for IPv4Address with tests
* add  <=,>= operator for IPv6Address with tests

* refactoring the  & , |, ~ operator of ipv6_address to "regular" operator
This commit is contained in:
stubbfel
2018-03-29 05:44:28 +02:00
committed by Matias Fontanini
parent 7848e28b62
commit 342e2c77a7
14 changed files with 371 additions and 7 deletions

View File

@@ -59,6 +59,9 @@ TINS_API bool hw_address_equal_compare(const uint8_t* start1, const uint8_t* end
TINS_API bool hw_address_lt_compare(const uint8_t* start1, const uint8_t* end1,
const uint8_t* start2, const uint8_t* end2);
TINS_API bool hw_address_gt_compare(const uint8_t* start1, const uint8_t* end1,
const uint8_t* start2, const uint8_t* end2);
/**
* \endcond
*/
@@ -268,6 +271,39 @@ public:
return Internals::hw_address_lt_compare(begin(), end(), rhs.begin(), rhs.end());
}
/**
* \brief Compares this HWAddress for less-than equality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether this address is equal or less-than rhs.
*/
bool operator<=(const HWAddress& rhs) const {
return !operator>(rhs);
}
/**
* \brief Compares this HWAddress for greater-than inequality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether this address is greater-than rhs.
*/
bool operator>(const HWAddress& rhs) const {
return Internals::hw_address_gt_compare(begin(), end(), rhs.begin(), rhs.end());
}
/**
* \brief Compares this HWAddress for greater-than equality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether this address is equal or greater-than rhs.
*/
bool operator>=(const HWAddress& rhs) const {
return !operator<(rhs);
}
/**
* \brief Apply a mask to this address
*
@@ -281,7 +317,33 @@ public:
}
return output;
}
/**
* \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 not operator
* \return The result of applying the mask to this address
*/
HWAddress operator~() const {
HWAddress<n> output = *this;
for (size_t i = 0; i < n; ++i) {
output[i] = ~output[i];
}
return output;
}
/**
* \brief Retrieves the size of this address.
*

View File

@@ -135,6 +135,38 @@ public:
return ip_addr_ < rhs.ip_addr_;
}
/**
* \brief Compares this address for less-than equality.
*
* \param rhs The address to be compared to.
*
* \return bool indicating whether this address is equal or less-than rhs.
*/
bool operator<=(const IPv4Address& rhs) const {
return !operator>(rhs);
}
/**
* \brief Compare this IPv4Address for greater-than inequality.
*
* \param rhs The address to be compared.
* \return bool indicating whether this address is greater-than rhs.
*/
bool operator>(const IPv4Address& rhs) const {
return ip_addr_ > rhs.ip_addr_;
}
/**
* \brief Compares this address for greater-than equality.
*
* \param rhs The address to be compared to.
*
* \return bool indicating whether this address is equal or greater-than rhs.
*/
bool operator>=(const IPv4Address& rhs) const {
return !operator<(rhs);
}
/**
* \brief Apply a mask to this address
*
@@ -142,6 +174,19 @@ public:
* \return The result of applying the mask to this address
*/
IPv4Address operator&(const IPv4Address& mask) const;
/**
* \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;
/**
* not operator (invert)
*/
IPv4Address operator~() const;
/**
* \brief Returns true if this is a private IPv4 address.

View File

@@ -160,7 +160,40 @@ public:
bool operator<(const IPv6Address& rhs) const {
return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
}
/**
* \brief Compares this address for less-than equality.
*
* \param rhs The address to be compared to.
*
* \return bool indicating whether this address is equal or less-than rhs.
*/
bool operator<=(const IPv6Address& rhs) const {
return !operator>(rhs);
}
/**
* \brief Compares this address for greater-than inequality.
*
* \param rhs The address to be compared to.
*
* \return bool indicating whether this address is greater-than rhs.
*/
bool operator>(const IPv6Address& rhs) const {
return std::lexicographical_compare(rhs.begin(), rhs.end(), begin(), end());
}
/**
* \brief Compares this address for greater-than equality.
*
* \param rhs The address to be compared to.
*
* \return bool indicating whether this address is equal or greater-than rhs.
*/
bool operator>=(const IPv6Address& rhs) const {
return !operator<(rhs);
}
/**
* \brief Helper function which copies the address into an output
* iterator.
@@ -218,7 +251,17 @@ public:
/**
* Applies a mask to an address
*/
TINS_API friend IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs);
IPv6Address operator&(const IPv6Address& rhs) const;
/**
* or a mask to an address
*/
IPv6Address operator|(const IPv6Address& rhs) const;
/**
* not operator (invert)
*/
IPv6Address operator~() const ;
private:
void init(const char* addr);

View File

@@ -118,6 +118,17 @@ public:
* \param ip The ip address being looked up.
*/
NetworkInterface(IPv4Address ip);
/**
* \brief Constructs a NetworkInterface from an ipv6 address.
*
* This abstracted interface will be the one that would be the gateway
* when sending a packet to the given ip.
*
* \param ip The ipv6 address being looked up.
*/
NetworkInterface(IPv6Address ipv6);
/**
* \brief Getter for this interface's identifier.

View File

@@ -110,6 +110,14 @@ struct Route6Entry {
template<typename ForwardIterator>
void route_entries(ForwardIterator output);
/**
* \brief Retrieves entries in the routing table.
*
* \brief output ForwardIterator in which entries will be stored.
*/
template<typename ForwardIterator>
void route6_entries(ForwardIterator output);
/**
* \brief Retrieves entries in the routing table.
*
@@ -117,6 +125,7 @@ void route_entries(ForwardIterator output);
*/
TINS_API std::vector<RouteEntry> route_entries();
/**
* \brief Retrieves entries in the routing table.
*
@@ -146,6 +155,19 @@ TINS_API std::set<std::string> network_interfaces();
*/
TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr);
/**
* \brief Finds the gateway's IP address for the given IP
* address.
*
* \param ip The IP address for which the default gateway will
* be searched.
* \param gw_addr This parameter will contain the gateway's IP
* address in case it is found.
*
* \return bool indicating whether the lookup was successfull.
*/
TINS_API bool gateway_from_ip(IPv6Address ip, IPv6Address& gw_addr);
} // Utils
} // Tins
@@ -158,4 +180,13 @@ void Tins::Utils::route_entries(ForwardIterator output) {
}
}
template<typename ForwardIterator>
void Tins::Utils::route6_entries(ForwardIterator output) {
std::vector<Route6Entry> entries = route6_entries();
for (size_t i = 0; i < entries.size(); ++i) {
*output = entries[i];
++output;
}
}
#endif // TINS_ROUTING_UTILS_H