1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 21:14:28 +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

@@ -113,5 +113,10 @@ bool hw_address_lt_compare(const uint8_t* start1, const uint8_t* end1,
return lexicographical_compare(start1, end1, start2, end2);
}
bool hw_address_gt_compare(const uint8_t* start1, const uint8_t* end1,
const uint8_t* start2, const uint8_t* end2) {
return lexicographical_compare(start2, end2, start1, end1);
}
} // Internals
} // Tins

View File

@@ -153,4 +153,12 @@ IPv4Address IPv4Address::operator&(const IPv4Address& mask) const {
return IPv4Address(Endian::be_to_host(ip_addr_ & mask.ip_addr_));
}
IPv4Address IPv4Address::operator|(const IPv4Address& mask) const {
return IPv4Address(Endian::be_to_host(ip_addr_ | mask.ip_addr_));
}
IPv4Address IPv4Address::operator~() const {
return IPv4Address(Endian::be_to_host(~ip_addr_));
}
} // Tins

View File

@@ -142,13 +142,33 @@ ostream& operator<<(ostream& os, const IPv6Address& addr) {
return os << addr.to_string();
}
IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs) {
IPv6Address output = lhs;
IPv6Address::iterator addr_iter = output.begin();
IPv6Address IPv6Address::operator&(const IPv6Address& rhs) const {
IPv6Address result = *this;
IPv6Address::iterator addr_iter = result.begin();
for (IPv6Address::const_iterator it = rhs.begin(); it != rhs.end(); ++it, ++addr_iter) {
*addr_iter = *addr_iter & *it;
}
return output;
return result;
}
IPv6Address IPv6Address::operator|(const IPv6Address& rhs) const {
IPv6Address result = *this;
IPv6Address::iterator addr_iter = result.begin();
for (IPv6Address::const_iterator it = rhs.begin(); it != rhs.end(); ++it, ++addr_iter) {
*addr_iter = *addr_iter | *it;
}
return result;
}
IPv6Address IPv6Address::operator~() const {
IPv6Address result = *this;
for (IPv6Address::iterator addr_iter = result.begin(); addr_iter != result.end(); ++addr_iter) {
*addr_iter = ~*addr_iter;
}
return result;
}
} // Tins

View File

@@ -257,6 +257,35 @@ NetworkInterface::NetworkInterface(IPv4Address ip)
}
}
NetworkInterface::NetworkInterface(IPv6Address ipv6)
: iface_id_(0) {
typedef vector<Utils::Route6Entry> entries_type;
if (ipv6 == "::1") {
#if defined(BSD) || defined(__FreeBSD_kernel__)
iface_id_ = resolve_index("lo0");
#else
iface_id_ = resolve_index("lo");
#endif
}
else {
const Utils::Route6Entry* best_match = 0;
entries_type entries;
Utils::route6_entries(std::back_inserter(entries));
for (entries_type::const_iterator it(entries.begin()); it != entries.end(); ++it) {
if ((ipv6 & it->mask) == it->destination) {
if (!best_match || it->mask > best_match->mask || it->metric < best_match->metric) {
best_match = &*it;
}
}
}
if (!best_match) {
throw invalid_interface();
}
iface_id_ = resolve_index(best_match->interface.c_str());
}
}
string NetworkInterface::name() const {
#ifndef _WIN32
char iface_name[IF_NAMESIZE];

View File

@@ -439,5 +439,17 @@ bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr) {
return false;
}
bool gateway_from_ip(IPv6Address ip, IPv6Address& gw_addr) {
typedef vector<Route6Entry> entries_type;
entries_type entries =route6_entries();
for (entries_type::const_iterator it(entries.begin()); it != entries.end(); ++it) {
if ((ip & it->mask) == it->destination) {
gw_addr = it->gateway;
return true;
}
}
return false;
}
} // Utils
} // Tins