13 #include <boost/static_assert.hpp>
15 #include <boost/functional/hash.hpp>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
23 using boost::asio::ip::udp;
24 using boost::asio::ip::tcp;
32 IOAddress::Hash::operator()(
const IOAddress &io_address)
const {
38 IOAddress::IOAddress(
const std::string& address_str) {
39 boost::system::error_code err;
40 asio_address_ = ip::address::from_string(address_str, err);
43 << address_str <<
"': " << err.message());
47 IOAddress::IOAddress(
const boost::asio::ip::address& asio_address) :
48 asio_address_(asio_address)
52 asio_address_(
boost::asio::ip::address_v4(v4address)) {
58 return (asio_address_.to_string());
66 if ( (family != AF_INET) && (family != AF_INET6) ) {
71 BOOST_STATIC_ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
72 char addr_str[INET6_ADDRSTRLEN];
73 inet_ntop(family, data, addr_str, INET6_ADDRSTRLEN);
79 if (asio_address_.is_v4()) {
80 const boost::asio::ip::address_v4::bytes_type bytes4 =
81 asio_address_.to_v4().to_bytes();
82 return (std::vector<uint8_t>(bytes4.begin(), bytes4.end()));
87 const boost::asio::ip::address_v6::bytes_type bytes6 =
88 asio_address_.to_v6().to_bytes();
89 return (std::vector<uint8_t>(bytes6.begin(), bytes6.end()));
94 if (asio_address_.is_v4()) {
103 if (!asio_address_.is_v6()) {
106 return (asio_address_.to_v6().is_link_local());
111 if (!asio_address_.is_v6()) {
114 return (asio_address_.to_v6().is_multicast());
119 if (asio_address_.is_v4()) {
120 return (asio_address_.to_v4().to_ulong());
123 <<
" address to IPv4.");
145 vector<uint8_t> a_vec = a.
toBytes();
146 vector<uint8_t> b_vec = b.
toBytes();
149 vector<uint8_t> result(V6ADDRESS_LEN,0);
158 for (
int i = a_vec.size() - 1; i >= 0; --i) {
159 result[i] = a_vec[i] - b_vec[i] - carry;
160 carry = (a_vec[i] < b_vec[i] + carry);
163 return (
fromBytes(AF_INET6, &result[0]));
169 std::vector<uint8_t> packed(addr.
toBytes());
172 for (
int i = packed.size() - 1; i >= 0; --i) {
174 if (++packed[i] != 0) {
184 if (address.
isV4()) {
185 boost::hash<uint32_t> hasher;
186 return (hasher(address.
toUint32()));
188 boost::hash<std::vector<uint8_t> > hasher;
189 return (hasher(address.
toBytes()));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
The IOAddress class represents an IP addresses (version agnostic)
static IOAddress subtract(const IOAddress &a, const IOAddress &b)
Subtracts one address from another (a - b)
std::string toText() const
Convert the address to a string.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
bool isV4() const
Convenience function to check for an IPv4 address.
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
bool isV6Multicast() const
checks whether and address is IPv6 and is multicast
static IOAddress increase(const IOAddress &addr)
Returns an address increased by one.
IOAddress(const std::string &address_str)
Constructor from string.
bool isV6LinkLocal() const
checks whether and address is IPv6 and is link-local
short getFamily() const
Returns the address family.
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
An exception that is thrown if an error occurs within the IO module.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A wrapper interface for the ASIO library.
std::ostream & operator<<(std::ostream &os, const IOAddress &address)
Insert the IOAddress as a string into stream.
size_t hash_value(const IOAddress &address)
Hash the IOAddress.
Defines the logger used by the top-level component of kea-lfc.