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;
33 IOAddress::IOAddress(
const std::string& address_str) {
34 boost::system::error_code err;
35 asio_address_ = ip::address::from_string(address_str, err);
38 << address_str <<
"': " << err.message());
42 IOAddress::IOAddress(
const boost::asio::ip::address& asio_address) :
43 asio_address_(asio_address)
47 asio_address_(
boost::asio::ip::address_v4(v4address)) {
53 return (asio_address_.to_string());
61 if ( (family != AF_INET) && (family != AF_INET6) ) {
66 BOOST_STATIC_ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
67 char addr_str[INET6_ADDRSTRLEN];
68 inet_ntop(family, data, addr_str, INET6_ADDRSTRLEN);
74 if (asio_address_.is_v4()) {
75 const boost::asio::ip::address_v4::bytes_type bytes4 =
76 asio_address_.to_v4().to_bytes();
77 return (std::vector<uint8_t>(bytes4.begin(), bytes4.end()));
82 const boost::asio::ip::address_v6::bytes_type bytes6 =
83 asio_address_.to_v6().to_bytes();
84 return (std::vector<uint8_t>(bytes6.begin(), bytes6.end()));
89 if (asio_address_.is_v4()) {
98 if (!asio_address_.is_v6()) {
101 return (asio_address_.to_v6().is_link_local());
106 if (!asio_address_.is_v6()) {
109 return (asio_address_.to_v6().is_multicast());
114 if (asio_address_.is_v4()) {
115 return (asio_address_.to_v4().to_ulong());
118 <<
" address to IPv4.");
140 vector<uint8_t> a_vec = a.
toBytes();
141 vector<uint8_t> b_vec = b.
toBytes();
144 vector<uint8_t> result(V6ADDRESS_LEN,0);
153 for (
int i = a_vec.size() - 1; i >= 0; --i) {
154 result[i] = a_vec[i] - b_vec[i] - carry;
155 carry = (a_vec[i] < b_vec[i] + carry);
158 return (
fromBytes(AF_INET6, &result[0]));
164 std::vector<uint8_t> packed(addr.
toBytes());
167 for (
int i = packed.size() - 1; i >= 0; --i) {
169 if (++packed[i] != 0) {
179 if (address.
isV4()) {
180 boost::hash<uint32_t> hasher;
181 return (hasher(address.
toUint32()));
183 boost::hash<std::vector<uint8_t> > hasher;
184 return (hasher(address.
toBytes()));
size_t hash_value(const IOAddress &address)
Hash the IOAddress.
IOAddress(const std::string &address_str)
Constructor from string.
bool isV6LinkLocal() const
checks whether and address is IPv6 and is link-local
static IOAddress subtract(const IOAddress &a, const IOAddress &b)
Subtracts one address from another (a - b)
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
std::ostream & operator<<(std::ostream &os, const IOAddress &address)
Insert the IOAddress as a string into stream.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
bool isV6Multicast() const
checks whether and address is IPv6 and is multicast
bool isV4() const
Convenience function to check for an IPv4 address.
Defines the logger used by the top-level component of kea-lfc.
std::string toText() const
Convert the address to a string.
A wrapper interface for the ASIO library.
An exception that is thrown if an error occurs within the IO module.
The IOAddress class represents an IP addresses (version agnostic)
static IOAddress increase(const IOAddress &addr)
Returns an address increased by one.
short getFamily() const
Returns the address family.