13 #include <boost/static_assert.hpp> 17 #include <sys/socket.h> 18 #include <netinet/in.h> 21 using boost::asio::ip::udp;
22 using boost::asio::ip::tcp;
31 IOAddress::IOAddress(
const std::string& address_str) {
32 boost::system::error_code err;
33 asio_address_ = ip::address::from_string(address_str, err);
36 << address_str <<
"': " << err.message());
40 IOAddress::IOAddress(
const boost::asio::ip::address& asio_address) :
41 asio_address_(asio_address)
45 asio_address_(
boost::asio::ip::address_v4(v4address)) {
51 return (asio_address_.to_string());
59 if ( (family != AF_INET) && (family != AF_INET6) ) {
64 BOOST_STATIC_ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
65 char addr_str[INET6_ADDRSTRLEN];
66 inet_ntop(family, data, addr_str, INET6_ADDRSTRLEN);
72 if (asio_address_.is_v4()) {
73 const boost::asio::ip::address_v4::bytes_type bytes4 =
74 asio_address_.to_v4().to_bytes();
75 return (std::vector<uint8_t>(bytes4.begin(), bytes4.end()));
80 const boost::asio::ip::address_v6::bytes_type bytes6 =
81 asio_address_.to_v6().to_bytes();
82 return (std::vector<uint8_t>(bytes6.begin(), bytes6.end()));
87 if (asio_address_.is_v4()) {
96 if (!asio_address_.is_v6()) {
99 return (asio_address_.to_v6().is_link_local());
104 if (!asio_address_.is_v6()) {
107 return (asio_address_.to_v6().is_multicast());
112 if (asio_address_.is_v4()) {
113 return (asio_address_.to_v4().to_ulong());
116 <<
" address to IPv4.");
138 vector<uint8_t> a_vec = a.
toBytes();
139 vector<uint8_t> b_vec = b.
toBytes();
142 vector<uint8_t> result(V6ADDRESS_LEN,0);
151 for (
int i = a_vec.size() - 1; i >= 0; --i) {
152 result[i] = a_vec[i] - b_vec[i] - carry;
153 carry = (a_vec[i] < b_vec[i] + carry);
156 return (
fromBytes(AF_INET6, &result[0]));
162 std::vector<uint8_t> packed(addr.
toBytes());
165 for (
int i = packed.size() - 1; i >= 0; --i) {
167 if (++packed[i] != 0) {
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-dhcp-ddns.
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.