Commit c4291199 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[1186] from_bytes() method implement in IOAddress

- similar to from_string() used in underlying boost::asio::address
parent 9d968071
......@@ -54,6 +54,21 @@ IOAddress::toText() const {
return (asio_address_.to_string());
}
IOAddress
IOAddress::from_bytes(short family, const char* data) {
static char addr_str[INET6_ADDRSTRLEN];
if (data == NULL) {
isc_throw(BadValue, "NULL pointer received.");
}
if ( (family != AF_INET) && (family != AF_INET6) ) {
isc_throw(BadValue, "Invalid family type. Only AF_INET and AF_INET6"
<< "are supported");
}
inet_ntop(family, data, addr_str,INET6_ADDRSTRLEN);
return IOAddress(string(addr_str));
}
short
IOAddress::getFamily() const {
if (asio_address_.is_v4()) {
......@@ -63,7 +78,7 @@ IOAddress::getFamily() const {
}
}
const asio::ip::address&
const asio::ip::address&
IOAddress::getAddress() const {
return asio_address_;
}
......
......@@ -87,6 +87,16 @@ public:
/// \return AF_INET for IPv4 or AF_INET6 for IPv6.
short getFamily() const;
/// \brief Creates an address from over wire data.
///
/// \param family AF_NET for IPv4 or AF_NET6 for IPv6.
/// \param data pointer to first char of data
///
/// \return Created IOAddress object
static IOAddress
from_bytes(short family, const char* data);
/// \brief Compare addresses for equality
///
/// \param other Address to compare against.
......
......@@ -63,3 +63,23 @@ TEST(IOAddressTest, Family) {
EXPECT_EQ(AF_INET, IOAddress("192.0.2.1").getFamily());
EXPECT_EQ(AF_INET6, IOAddress("2001:0DB8:0:0::0012").getFamily());
}
TEST(IOAddressTest, from_bytes) {
// 2001:db8:1::dead:beef
char v6[] = {
0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0, 0,
0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef };
char v4[] = { 192, 0 , 2, 3 };
IOAddress addr("::");
EXPECT_NO_THROW({
addr = IOAddress::from_bytes(AF_INET6, v6);
});
EXPECT_EQ("2001:db8:1::dead:beef", addr.toText());
EXPECT_NO_THROW({
addr = IOAddress::from_bytes(AF_INET, v4);
});
EXPECT_EQ(addr, IOAddress("192.0.2.3"));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment