Commit 3d817285 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[3242] Implemented function which returns IPv4 address for interface.

parent 52dc5669
......@@ -200,6 +200,24 @@ void Iface::addUnicast(const isc::asiolink::IOAddress& addr) {
Iface::getAddress4(isc::asiolink::IOAddress& address) const {
// Iterate over existing addresses assigned to the interface.
// Try to find the one that is IPv4.
const AddressCollection& addrs = getAddresses();
for (AddressCollection::const_iterator addr = addrs.begin();
addr != addrs.end(); ++addr) {
// If address is IPv4, we assign it to the function argument
// and return true.
if (addr->isV4()) {
address = *addr;
return (true);
// There is no IPv4 address assigned to this interface.
return (false);
void IfaceMgr::closeSockets() {
for (IfaceCollection::iterator iface = ifaces_.begin();
iface != ifaces_.end(); ++iface) {
......@@ -246,6 +246,17 @@ public:
/// @return collection of addresses
const AddressCollection& getAddresses() const { return addrs_; }
/// @brief Returns IPv4 address assigned to the interface.
/// This function looks for an IPv4 address assigned to the interface
/// and returns it through the argument.
/// @param [out] address IPv4 address assigned to the interface.
/// @return Boolean value which informs whether IPv4 address has been found
/// for the interface (if true), or not (false).
bool getAddress4(isc::asiolink::IOAddress& address) const;
/// @brief Adds an address to an interface.
/// This only adds an address to collection, it does not physically
......@@ -19,6 +19,7 @@
#include <dhcp/iface_mgr.h>
#include <dhcp/pkt6.h>
#include <dhcp/pkt_filter.h>
#include <dhcp/tests/iface_mgr_test_config.h>
#include <dhcp/tests/pkt_filter6_test_utils.h>
#include <boost/bind.hpp>
......@@ -529,6 +530,32 @@ TEST_F(IfaceMgrTest, ifaceClass) {
EXPECT_STREQ("eth5/7", iface.getFullName().c_str());
// Test that the IPv4 address can be retrieved for the interface.
TEST_F(IfaceMgrTest, ifaceGetAddress) {
Iface iface("eth0", 0);
IOAddress addr("::1");
// Initially, the Iface has no addresses assigned.
// Add some addresses with IPv4 address in the middle.
// The v4 address should be returned.
EXPECT_EQ("", addr.toText());
// Delete the IPv4 address and leave only two IPv6 addresses.
// The IPv4 address should not be returned.
// Add a different IPv4 address at the end of the list.
// This new address should now be returned.
EXPECT_EQ("", addr.toText());
// TODO: Implement getPlainMac() test as soon as interface detection
// is implemented.
TEST_F(IfaceMgrTest, getIface) {
......@@ -1381,31 +1408,29 @@ TEST_F(IfaceMgrTest, openSockets4) {
// This test verifies that the socket is not open on the interface which is
// down, but sockets are open on all other non-loopback interfaces.
TEST_F(IfaceMgrTest, openSockets4IfaceDown) {
NakedIfaceMgr ifacemgr;
// Remove all real interfaces and create a set of dummy interfaces.
boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter());
IfaceMgrTestConfig config(true);
// Boolean parameters specify that eth0 is:
// - not a loopback
// - is "down" (not up)
// - is not running
// - is active (is not inactive)
ifacemgr.setIfaceFlags("eth0", false, false, true, false, false);
ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, NULL));
config.setIfaceFlags("eth0", FlagLoopback(false), FlagUp(false),
FlagRunning(false), FlagInactive4(false),
ASSERT_NO_THROW(IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true,
// There should be no socket on eth0 open, because interface was down.
// Expecting that the socket is open on eth1 because it was up, running
// and active.
EXPECT_EQ(1, ifacemgr.getIface("eth1")->getSockets().size());
EXPECT_EQ(1, IfaceMgr::instance().getIface("eth1")->getSockets().size());
// Never open socket on loopback interface.
// This test verifies that the socket is not open on the interface which is
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