mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 05:24:26 +01:00
Added AddressRange class template.
This commit is contained in:
@@ -39,6 +39,8 @@
|
||||
#include "rawpdu.h"
|
||||
#include "dot1q.h"
|
||||
#include "pppoe.h"
|
||||
#include "ip_address.h"
|
||||
#include "ipv6_address.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
@@ -149,5 +151,45 @@ Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag) {
|
||||
return Constants::Ethernet::UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
bool increment(IPv4Address &addr) {
|
||||
uint32_t addr_int = Endian::be_to_host<uint32_t>(addr);
|
||||
bool reached_end = ++addr_int == 0xffffffff;
|
||||
addr = IPv4Address(Endian::be_to_host<uint32_t>(addr_int));
|
||||
return reached_end;
|
||||
}
|
||||
|
||||
bool increment(IPv6Address &addr) {
|
||||
IPv6Address::iterator it = addr.end() - 1;
|
||||
while(it >= addr.begin() && *it == 0xff) {
|
||||
*it = 0;
|
||||
--it;
|
||||
}
|
||||
// reached end
|
||||
if(it < addr.begin())
|
||||
return true;
|
||||
(*it)++;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool decrement(IPv4Address &addr) {
|
||||
uint32_t addr_int = Endian::be_to_host<uint32_t>(addr);
|
||||
bool reached_end = --addr_int == 0;
|
||||
addr = IPv4Address(Endian::be_to_host<uint32_t>(addr_int));
|
||||
return reached_end;
|
||||
}
|
||||
|
||||
bool decrement(IPv6Address &addr) {
|
||||
IPv6Address::iterator it = addr.end() - 1;
|
||||
while(it >= addr.begin() && *it == 0) {
|
||||
*it = 0xff;
|
||||
--it;
|
||||
}
|
||||
// reached end
|
||||
if(it < addr.begin())
|
||||
return true;
|
||||
(*it)--;
|
||||
return false;
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace Tins
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <sstream>
|
||||
#include "ip_address.h"
|
||||
#include "endianness.h"
|
||||
#include "address_range.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
@@ -94,4 +95,13 @@ std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
|
||||
}
|
||||
return output;;
|
||||
}
|
||||
|
||||
AddressRange<IPv4Address> operator/(const IPv4Address &addr, int mask) {
|
||||
if(mask > 32)
|
||||
throw std::logic_error("Prefix length cannot exceed 32");
|
||||
return AddressRange<IPv4Address>::from_mask(
|
||||
addr,
|
||||
IPv4Address(Endian::host_to_be(0xffffffff << (32 - mask)))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include "ipv6_address.h"
|
||||
#include "address_range.h"
|
||||
|
||||
namespace Tins {
|
||||
IPv6Address::IPv6Address() {
|
||||
@@ -96,6 +97,19 @@ namespace Tins {
|
||||
#endif
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
AddressRange<IPv6Address> operator/(const IPv6Address &addr, int mask) {
|
||||
if(mask > 128)
|
||||
throw std::logic_error("Prefix length cannot exceed 128");
|
||||
IPv6Address last_addr;
|
||||
IPv6Address::iterator it = last_addr.begin();
|
||||
while(mask > 8) {
|
||||
*it = 0xff;
|
||||
++it;
|
||||
mask -= 8;
|
||||
}
|
||||
*it = 0xff << (8 - mask);
|
||||
return AddressRange<IPv6Address>::from_mask(addr, last_addr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user