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

Merge branch 'trac1485'

parents aafa7f65 7aa18afc
......@@ -16,6 +16,8 @@
#include <dns/opcode.h>
#include <dns/messagerenderer.h>
#include <nc_test_utils.h>
#include <asio.hpp>
#include <asiolink/udp_endpoint.h>
#include <gtest/gtest.h>
......@@ -42,7 +44,9 @@ FauxServer::FauxServer(asiolink::IOService& io_service,
server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
asio::ip::udp::v4()));
server_socket_->set_option(asio::socket_base::reuse_address(true));
server_socket_->bind(asio::ip::udp::endpoint(address_.getAddress(), port_));
isc::asiolink::UDPEndpoint endpoint(address_, port_);
server_socket_->bind(endpoint.getASIOEndpoint());
}
FauxServer::FauxServer(asiolink::IOService& io_service,
......@@ -53,7 +57,8 @@ FauxServer::FauxServer(asiolink::IOService& io_service,
server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
asio::ip::udp::v4()));
server_socket_->set_option(asio::socket_base::reuse_address(true));
server_socket_->bind(asio::ip::udp::endpoint(address_.getAddress(), port_));
isc::asiolink::UDPEndpoint endpoint(address_, port_);
server_socket_->bind(endpoint.getASIOEndpoint());
}
......
......@@ -100,14 +100,25 @@ IOAddress::getFamily() const {
}
}
const asio::ip::address&
IOAddress::getAddress() const {
return asio_address_;
bool
IOAddress::isV6LinkLocal() const {
if (!asio_address_.is_v6()) {
return (false);
}
return (asio_address_.to_v6().is_link_local());
}
bool
IOAddress::isV6Multicast() const {
if (!asio_address_.is_v6()) {
return (false);
}
return (asio_address_.to_v6().is_multicast());
}
IOAddress::operator uint32_t() const {
if (getAddress().is_v4()) {
return (getAddress().to_v4().to_ulong());
if (asio_address_.is_v4()) {
return (asio_address_.to_v4().to_ulong());
} else {
isc_throw(BadValue, "Can't convert " << toText()
<< " address to IPv4.");
......
......@@ -91,14 +91,6 @@ public:
/// \return A string representation of the address.
std::string toText() const;
/// \brief Returns const reference to the underlying address object.
///
/// This is useful, when access to interface offerted by
// asio::ip::address_v4 and asio::ip::address_v6 is beneficial.
///
/// \return A const reference to asio::ip::address object
const asio::ip::address& getAddress() const;
/// \brief Returns the address family
///
/// \return AF_INET for IPv4 or AF_INET6 for IPv6.
......@@ -118,6 +110,16 @@ public:
return (asio_address_.is_v6());
}
/// \brief checks whether and address is IPv6 and is link-local
///
/// \return true if the address is IPv6 link-local, false otherwise
bool isV6LinkLocal() const;
/// \brief checks whether and address is IPv6 and is multicast
///
/// \return true if the address is IPv6 multicast, false otherwise
bool isV6Multicast() const;
/// \brief Creates an address from over wire data.
///
/// \param family AF_NET for IPv4 or AF_NET6 for IPv6.
......
......@@ -182,3 +182,38 @@ TEST(IOAddressTest, LeftShiftOperator) {
oss << addr;
EXPECT_EQ(addr.toText(), oss.str());
}
// Tests address classification methods (which were previously used by accessing
// underlying asio objects directly)
TEST(IOAddressTest, accessClassificationMethods) {
IOAddress addr1("192.0.2.5"); // IPv4
IOAddress addr2("::"); // IPv6
IOAddress addr3("2001:db8::1"); // global IPv6
IOAddress addr4("fe80::1234"); // link-local
IOAddress addr5("ff02::1:2"); // multicast
EXPECT_TRUE (addr1.isV4());
EXPECT_FALSE(addr1.isV6());
EXPECT_FALSE(addr1.isV6LinkLocal());
EXPECT_FALSE(addr1.isV6Multicast());
EXPECT_FALSE(addr2.isV4());
EXPECT_TRUE (addr2.isV6());
EXPECT_FALSE(addr2.isV6LinkLocal());
EXPECT_FALSE(addr2.isV6Multicast());
EXPECT_FALSE(addr3.isV4());
EXPECT_TRUE (addr3.isV6());
EXPECT_FALSE(addr3.isV6LinkLocal());
EXPECT_FALSE(addr3.isV6Multicast());
EXPECT_FALSE(addr4.isV4());
EXPECT_TRUE (addr4.isV6());
EXPECT_TRUE (addr4.isV6LinkLocal());
EXPECT_FALSE(addr4.isV6Multicast());
EXPECT_FALSE(addr5.isV4());
EXPECT_TRUE (addr5.isV6());
EXPECT_FALSE(addr5.isV6LinkLocal());
EXPECT_TRUE (addr5.isV6Multicast());
}
......@@ -523,7 +523,7 @@ IfaceMgr::openSockets6(const uint16_t port,
// with interface with 2 global addresses, we would bind 3 sockets
// (one for link-local and two for global). That would result in
// getting each message 3 times.
if (!addr->getAddress().to_v6().is_link_local()){
if (!addr->isV6LinkLocal()){
continue;
}
......@@ -693,7 +693,7 @@ int IfaceMgr::openSocketFromRemoteAddress(const IOAddress& remote_addr,
const uint16_t port) {
try {
// Get local address to be used to connect to remote location.
IOAddress local_address(getLocalAddress(remote_addr, port).getAddress());
IOAddress local_address(getLocalAddress(remote_addr, port));
return openSocketFromAddress(local_address, port);
} catch (const Exception& e) {
isc_throw(SocketConfigError, e.what());
......@@ -1033,7 +1033,7 @@ uint16_t IfaceMgr::getSocket(const isc::dhcp::Pkt6& pkt) {
}
// Sockets bound to multicast address are useless for sending anything.
if (s->addr_.getAddress().to_v6().is_multicast()) {
if (s->addr_.isV6Multicast()) {
continue;
}
......@@ -1050,10 +1050,10 @@ uint16_t IfaceMgr::getSocket(const isc::dhcp::Pkt6& pkt) {
// If we want to send something to link-local and the socket is
// bound to link-local or we want to send to global and the socket
// is bound to global, then use it as candidate
if ( (pkt.getRemoteAddr().getAddress().to_v6().is_link_local() &&
s->addr_.getAddress().to_v6().is_link_local()) ||
(!pkt.getRemoteAddr().getAddress().to_v6().is_link_local() &&
!s->addr_.getAddress().to_v6().is_link_local()) ) {
if ( (pkt.getRemoteAddr().isV6LinkLocal() &&
s->addr_.isV6LinkLocal()) ||
(!pkt.getRemoteAddr().isV6LinkLocal() &&
!s->addr_.isV6LinkLocal()) ) {
candidate = s;
}
}
......
......@@ -94,7 +94,7 @@ NameChangeUDPListener::~NameChangeUDPListener() {
void
NameChangeUDPListener::open(isc::asiolink::IOService& io_service) {
// create our endpoint and bind the the low level socket to it.
isc::asiolink::UDPEndpoint endpoint(ip_address_.getAddress(), port_);
isc::asiolink::UDPEndpoint endpoint(ip_address_, port_);
// Create the low level socket.
try {
......@@ -227,7 +227,7 @@ NameChangeUDPSender::~NameChangeUDPSender() {
void
NameChangeUDPSender::open(isc::asiolink::IOService& io_service) {
// create our endpoint and bind the the low level socket to it.
isc::asiolink::UDPEndpoint endpoint(ip_address_.getAddress(), port_);
isc::asiolink::UDPEndpoint endpoint(ip_address_, port_);
// Create the low level socket.
try {
......@@ -252,8 +252,7 @@ NameChangeUDPSender::open(isc::asiolink::IOService& io_service) {
// Create the server endpoint
server_endpoint_.reset(new isc::asiolink::
UDPEndpoint(server_address_.getAddress(),
server_port_));
UDPEndpoint(server_address_, server_port_));
send_callback_->setDataSource(server_endpoint_);
}
......
......@@ -152,7 +152,7 @@ CfgMgr::getSubnet6(const isc::asiolink::IOAddress& hint) {
// configuration. Such requirement makes sense in IPv4, but not in IPv6.
// The server does not need to have a global address (using just link-local
// is ok for DHCPv6 server) from the pool it serves.
if ((subnets6_.size() == 1) && hint.getAddress().to_v6().is_link_local()) {
if ((subnets6_.size() == 1) && hint.isV6LinkLocal()) {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_CFGMGR_ONLY_SUBNET6)
.arg(subnets6_[0]->toText()).arg(hint.toText());
......
......@@ -36,7 +36,7 @@ D2ClientConfig::D2ClientConfig(const bool enable_updates,
const std::string& generated_prefix,
const std::string& qualifying_suffix)
: enable_updates_(enable_updates),
server_ip_(server_ip.getAddress()),
server_ip_(server_ip),
server_port_(server_port),
ncr_protocol_(ncr_protocol),
ncr_format_(ncr_format),
......
......@@ -777,8 +777,7 @@ TestControl::openSocket() const {
} else if (options.getIpVersion() == 6) {
// If remote address is multicast we need to enable it on
// the socket that has been created.
asio::ip::address_v6 remote_v6 = remoteaddr.getAddress().to_v6();
if (remote_v6.is_multicast()) {
if (remoteaddr.isV6Multicast()) {
int hops = 1;
int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&hops, sizeof(hops));
......@@ -1640,7 +1639,7 @@ TestControl::sendRequest4(const TestControlSocket& socket,
/// Set client address.
asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
if (!yiaddr.getAddress().is_v4()) {
if (!yiaddr.isV4()) {
isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
" IPv4 address");
}
......@@ -1748,7 +1747,7 @@ TestControl::sendRequest4(const TestControlSocket& socket,
/// Set client address.
asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
if (!yiaddr.getAddress().is_v4()) {
if (!yiaddr.isV4()) {
isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
" IPv4 address");
}
......
Supports Markdown
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