Commit 6b7ef74b authored by Marcin Siodelski's avatar Marcin Siodelski

[2549] Simplified functions writing an address to a buffer.

parent f104a60a
......@@ -470,7 +470,7 @@ IfaceMgr::getLocalAddress(const IOAddress& remote_addr, const uint16_t port) {
asio::error_code err_code;
// If remote address is broadcast address we have to
// allow this on the socket.
if (remote_addr.getAddress().is_v4() &&
if (remote_addr.isV4() &&
(remote_addr == IOAddress(DHCP_IPV4_BROADCAST_ADDRESS))) {
// Socket has to be open prior to setting the broadcast
// option. Otherwise set_option will complain about
......@@ -557,9 +557,7 @@ int IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, uint16_t port) {
addr6.sin6_scope_id = if_nametoindex(iface.getName().c_str());
}
memcpy(&addr6.sin6_addr,
addr.getAddress().to_v6().to_bytes().data(),
sizeof(addr6.sin6_addr));
memcpy(&addr6.sin6_addr, &addr.toBytes()[0], sizeof(addr6.sin6_addr));
#ifdef HAVE_SA_LEN
addr6.sin6_len = sizeof(addr6);
#endif
......@@ -661,7 +659,7 @@ IfaceMgr::send(const Pkt6Ptr& pkt) {
to.sin6_family = AF_INET6;
to.sin6_port = htons(pkt->getRemotePort());
memcpy(&to.sin6_addr,
pkt->getRemoteAddr().getAddress().to_v6().to_bytes().data(),
&pkt->getRemoteAddr().toBytes()[0],
16);
to.sin6_scope_id = pkt->getIndex();
......
......@@ -72,7 +72,7 @@ void Option6AddrLst::pack(isc::util::OutputBuffer& buf) {
for (AddressContainer::const_iterator addr=addrs_.begin();
addr!=addrs_.end(); ++addr) {
buf.writeData(addr->getAddress().to_v6().to_bytes().data(), V6ADDRESS_LEN);
buf.writeData(&addr->toBytes()[0], V6ADDRESS_LEN);
}
}
......
......@@ -52,8 +52,7 @@ void Option6IAAddr::pack(isc::util::OutputBuffer& buf) {
buf.writeUint16(len() - getHeaderLen());
buf.writeData(addr_.getAddress().to_v6().to_bytes().data(),
isc::asiolink::V6ADDRESS_LEN);
buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
buf.writeUint32(preferred_);
buf.writeUint32(valid_);
......
......@@ -146,27 +146,8 @@ OptionDataTypeUtil::readAddress(const std::vector<uint8_t>& buf,
void
OptionDataTypeUtil::writeAddress(const asiolink::IOAddress& address,
std::vector<uint8_t>& buf) {
// @todo There is a ticket 2396 submitted, which adds the
// functionality to return a buffer representation of
// IOAddress. If so, this function can be simplified.
if (address.getAddress().is_v4()) {
asio::ip::address_v4::bytes_type addr_bytes =
address.getAddress().to_v4().to_bytes();
// Increase the buffer size by the size of IPv4 address.
buf.resize(buf.size() + addr_bytes.size());
std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
buf.end());
} else if (address.getAddress().is_v6()) {
asio::ip::address_v6::bytes_type addr_bytes =
address.getAddress().to_v6().to_bytes();
// Incresase the buffer size by the size of IPv6 address.
buf.resize(buf.size() + addr_bytes.size());
std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
buf.end());
} else {
isc_throw(BadDataTypeCast, "the address " << address.toText()
<< " is neither valid IPv4 not IPv6 address.");
}
const std::vector<uint8_t>& vec = address.toBytes();
buf.insert(buf.end(), vec.begin(), vec.end());
}
void
......
......@@ -382,7 +382,7 @@ OptionDefinition::writeToBuffer(const std::string& value,
if (!address.isV4() && !address.isV6()) {
isc_throw(BadDataTypeCast, "provided address " << address.toText()
<< " is not a valid "
<< (address.getAddress().is_v4() ? "IPv4" : "IPv6")
<< (address.isV4() ? "IPv4" : "IPv6")
<< " address");
}
OptionDataTypeUtil::writeAddress(address, buf);
......
......@@ -38,15 +38,8 @@ public:
/// @param [out] buf output buffer.
void writeAddress(const asiolink::IOAddress& address,
std::vector<uint8_t>& buf) {
if (address.isV4()) {
asio::ip::address_v4::bytes_type buf_addr =
address.getAddress().to_v4().to_bytes();
buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
} else if (address.isV6()) {
asio::ip::address_v6::bytes_type buf_addr =
address.getAddress().to_v6().to_bytes();
buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
}
const std::vector<uint8_t>& vec = address.toBytes();
buf.insert(buf.end(), vec.begin(), vec.end());
}
/// @brief Write integer (signed or unsigned) into a buffer.
......
......@@ -34,15 +34,8 @@ public:
/// @param [out] buf output buffer.
void writeAddress(const asiolink::IOAddress& address,
std::vector<uint8_t>& buf) {
if (address.isV4()) {
asio::ip::address_v4::bytes_type buf_addr =
address.getAddress().to_v4().to_bytes();
buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
} else if (address.isV6()) {
asio::ip::address_v6::bytes_type buf_addr =
address.getAddress().to_v6().to_bytes();
buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
}
const std::vector<uint8_t>& vec = address.toBytes();
buf.insert(buf.end(), vec.begin(), vec.end());
}
/// @brief Write integer (signed or unsigned) into a buffer.
......
......@@ -207,10 +207,9 @@ TEST_F(OptionDefinitionTest, ipv6AddressArray) {
// Write addresses to the buffer.
OptionBuffer buf(addrs.size() * asiolink::V6ADDRESS_LEN);
for (int i = 0; i < addrs.size(); ++i) {
asio::ip::address_v6::bytes_type addr_bytes =
addrs[i].getAddress().to_v6().to_bytes();
ASSERT_EQ(asiolink::V6ADDRESS_LEN, addr_bytes.size());
std::copy(addr_bytes.begin(), addr_bytes.end(),
const std::vector<uint8_t>& vec = addrs[i].toBytes();
ASSERT_EQ(asiolink::V6ADDRESS_LEN, vec.size());
std::copy(vec.begin(), vec.end(),
buf.begin() + i * asiolink::V6ADDRESS_LEN);
}
// Create DHCPv6 option from this buffer. Once option is created it is
......@@ -306,10 +305,9 @@ TEST_F(OptionDefinitionTest, ipv4AddressArray) {
// Write addresses to the buffer.
OptionBuffer buf(addrs.size() * asiolink::V4ADDRESS_LEN);
for (int i = 0; i < addrs.size(); ++i) {
asio::ip::address_v4::bytes_type addr_bytes =
addrs[i].getAddress().to_v4().to_bytes();
ASSERT_EQ(asiolink::V4ADDRESS_LEN, addr_bytes.size());
std::copy(addr_bytes.begin(), addr_bytes.end(),
const std::vector<uint8_t> vec = addrs[i].toBytes();
ASSERT_EQ(asiolink::V4ADDRESS_LEN, vec.size());
std::copy(vec.begin(), vec.end(),
buf.begin() + i * asiolink::V4ADDRESS_LEN);
}
// Create DHCPv6 option from this buffer. Once option is created it is
......@@ -512,11 +510,10 @@ TEST_F(OptionDefinitionTest, recordIAAddr6) {
OptionPtr option_v6;
asiolink::IOAddress addr_v6("2001:0db8::ff00:0042:8329");
OptionBuffer buf(asiolink::V6ADDRESS_LEN);
ASSERT_TRUE(addr_v6.getAddress().is_v6());
asio::ip::address_v6::bytes_type addr_bytes =
addr_v6.getAddress().to_v6().to_bytes();
ASSERT_EQ(asiolink::V6ADDRESS_LEN, addr_bytes.size());
std::copy(addr_bytes.begin(), addr_bytes.end(), buf.begin());
ASSERT_TRUE(addr_v6.isV6());
const std::vector<uint8_t>& vec = addr_v6.toBytes();
ASSERT_EQ(asiolink::V6ADDRESS_LEN, vec.size());
std::copy(vec.begin(), vec.end(), buf.begin());
for (int i = 0; i < option6_iaaddr_len - asiolink::V6ADDRESS_LEN; ++i) {
buf.push_back(i);
......
......@@ -54,7 +54,7 @@ isc::asiolink::IOAddress firstAddrInPrefix6(const isc::asiolink::IOAddress& pref
// First we copy the whole address as 16 bytes.
uint8_t packed[V6ADDRESS_LEN];
memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
memcpy(packed, &prefix.toBytes()[0], 16);
// If the length is divisible by 8, it is simple. We just zero out the host
// part. Otherwise we need to handle the byte that has to be partially
......@@ -132,7 +132,7 @@ isc::asiolink::IOAddress lastAddrInPrefix6(const isc::asiolink::IOAddress& prefi
// First we copy the whole address as 16 bytes.
uint8_t packed[V6ADDRESS_LEN];
memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
memcpy(packed, &prefix.toBytes()[0], 16);
// if the length is divisible by 8, it is simple. We just fill the host part
// with ones. Otherwise we need to handle the byte that has to be partially
......
......@@ -36,11 +36,11 @@ AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress&
// First we copy the whole address as 16 bytes.
if (addr.isV4()) {
// IPv4
std::memcpy(packed, addr.getAddress().to_v4().to_bytes().data(), 4);
std::memcpy(packed, &addr.toBytes()[0], 4);
len = 4;
} else {
// IPv6
std::memcpy(packed, addr.getAddress().to_v6().to_bytes().data(), 16);
std::memcpy(packed, &addr.toBytes()[0], 16);
len = 16;
}
......
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