net: add NetworkMonitorStub
refs #4024
Change-Id: I20da6f3351f51667b045987dc07e88ba814a4456
diff --git a/src/net/detail/network-monitor-impl-noop.hpp b/src/net/detail/network-monitor-impl-noop.hpp
index f0c5fb0..919d231 100644
--- a/src/net/detail/network-monitor-impl-noop.hpp
+++ b/src/net/detail/network-monitor-impl-noop.hpp
@@ -29,27 +29,26 @@
namespace ndn {
namespace net {
-class NetworkMonitor::Impl
+class NetworkMonitorImplNoop : public NetworkMonitorImpl
{
public:
- Impl(NetworkMonitor& nm, boost::asio::io_service& io)
- {
- }
+ explicit
+ NetworkMonitorImplNoop(boost::asio::io_service& io) = default;
uint32_t
- getCapabilities() const
+ getCapabilities() const final
{
return NetworkMonitor::CAP_NONE;
}
- shared_ptr<NetworkInterface>
- getNetworkInterface(const std::string&) const
+ shared_ptr<const NetworkInterface>
+ getNetworkInterface(const std::string&) const final
{
return {};
}
- std::vector<shared_ptr<NetworkInterface>>
- listNetworkInterfaces() const
+ std::vector<shared_ptr<const NetworkInterface>>
+ listNetworkInterfaces() const final
{
return {};
}
diff --git a/src/net/detail/network-monitor-impl-osx.cpp b/src/net/detail/network-monitor-impl-osx.cpp
index 97f3e7c..b2129b5 100644
--- a/src/net/detail/network-monitor-impl-osx.cpp
+++ b/src/net/detail/network-monitor-impl-osx.cpp
@@ -70,13 +70,12 @@
NDN_LOG_INIT(ndn.NetworkMonitor);
-NetworkMonitor::Impl::Impl(NetworkMonitor& nm, boost::asio::io_service& io)
- : m_nm(nm)
- , m_scheduler(io)
+NetworkMonitorImplOsx::NetworkMonitorImplOsx(boost::asio::io_service& io)
+ : m_scheduler(io)
, m_cfLoopEvent(m_scheduler)
, m_context{0, this, nullptr, nullptr, nullptr}
, m_scStore(SCDynamicStoreCreate(nullptr, CFSTR("net.named-data.ndn-cxx.NetworkMonitor"),
- &Impl::onConfigChanged, &m_context))
+ &NetworkMonitorImplOsx::onConfigChanged, &m_context))
, m_loopSource(SCDynamicStoreCreateRunLoopSource(nullptr, m_scStore.get(), 0))
, m_nullUdpSocket(io, boost::asio::ip::udp::v4())
@@ -89,7 +88,7 @@
//
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
static_cast<void*>(this),
- &Impl::afterNotificationCenterEvent,
+ &NetworkMonitorImplOsx::afterNotificationCenterEvent,
CFSTR("com.apple.system.config.network_change"),
nullptr, // object to observe
CFNotificationSuspensionBehaviorDeliverImmediately);
@@ -116,7 +115,7 @@
SCDynamicStoreSetNotificationKeys(m_scStore.get(), nullptr, patterns);
}
-NetworkMonitor::Impl::~Impl()
+NetworkMonitorImplOsx::~NetworkMonitorImplOsx()
{
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode);
@@ -124,22 +123,17 @@
static_cast<void*>(this));
}
-shared_ptr<NetworkInterface>
-NetworkMonitor::Impl::getNetworkInterface(const std::string& ifname) const
+shared_ptr<const NetworkInterface>
+NetworkMonitorImplOsx::getNetworkInterface(const std::string& ifname) const
{
auto it = m_interfaces.find(ifname);
- if (it != m_interfaces.end()) {
- return it->second;
- }
- else {
- return nullptr;
- }
+ return it == m_interfaces.end() ? nullptr : it->second;
}
-std::vector<shared_ptr<NetworkInterface>>
-NetworkMonitor::Impl::listNetworkInterfaces() const
+std::vector<shared_ptr<const NetworkInterface>>
+NetworkMonitorImplOsx::listNetworkInterfaces() const
{
- std::vector<shared_ptr<NetworkInterface>> v;
+ std::vector<shared_ptr<const NetworkInterface>> v;
v.reserve(m_interfaces.size());
for (const auto& e : m_interfaces) {
@@ -149,24 +143,24 @@
}
void
-NetworkMonitor::Impl::afterNotificationCenterEvent(CFNotificationCenterRef center,
- void* observer,
- CFStringRef name,
- const void* object,
- CFDictionaryRef userInfo)
+NetworkMonitorImplOsx::afterNotificationCenterEvent(CFNotificationCenterRef center,
+ void* observer,
+ CFStringRef name,
+ const void* object,
+ CFDictionaryRef userInfo)
{
- static_cast<Impl*>(observer)->m_nm.onNetworkStateChanged();
+ static_cast<NetworkMonitorImplOsx*>(observer)->emitSignal(onNetworkStateChanged);
}
void
-NetworkMonitor::Impl::scheduleCfLoop()
+NetworkMonitorImplOsx::scheduleCfLoop()
{
// poll each second for new events
- m_cfLoopEvent = m_scheduler.scheduleEvent(time::seconds(1), bind(&Impl::pollCfLoop, this));
+ m_cfLoopEvent = m_scheduler.scheduleEvent(time::seconds(1), [this] { pollCfLoop(); });
}
void
-NetworkMonitor::Impl::pollCfLoop()
+NetworkMonitorImplOsx::pollCfLoop()
{
// this should dispatch ready events and exit
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
@@ -175,10 +169,9 @@
}
void
-NetworkMonitor::Impl::addNewInterface(const std::string& ifName)
+NetworkMonitorImplOsx::addNewInterface(const std::string& ifName)
{
- shared_ptr<NetworkInterface> interface(new NetworkInterface);
-
+ shared_ptr<NetworkInterface> interface = makeNetworkInterface();
interface->setName(ifName);
interface->setState(getInterfaceState(interface->getName()));
updateInterfaceInfo(*interface);
@@ -189,16 +182,16 @@
NDN_LOG_DEBUG("adding interface " << interface->getName());
m_interfaces.insert(make_pair(interface->getName(), interface));
- m_nm.onInterfaceAdded(interface);
+ this->emitSignal(onInterfaceAdded, interface);
}
void
-NetworkMonitor::Impl::enumerateInterfaces()
+NetworkMonitorImplOsx::enumerateInterfaces()
{
for (const auto& ifName : getInterfaceNames()) {
addNewInterface(ifName);
}
- m_nm.onEnumerationCompleted();
+ this->emitSignal(onEnumerationCompleted);
}
static std::string
@@ -209,18 +202,19 @@
return cStr;
}
- size_t stringSize = CFStringGetLength(cfString);
+ size_t stringSize = CFStringGetLength(cfString);
char* buffer = new char[stringSize + 1];
CFStringGetCString(cfString, buffer, sizeof(buffer), kCFStringEncodingASCII);
std::string retval = buffer;
- delete [] buffer;
+ delete[] buffer;
return retval;
}
std::set<std::string>
-NetworkMonitor::Impl::getInterfaceNames()
+NetworkMonitorImplOsx::getInterfaceNames()
{
- CFReleaser<CFDictionaryRef> dict = (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface"));
+ CFReleaser<CFDictionaryRef> dict =
+ (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface"));
CFArrayRef interfaces = (CFArrayRef)CFDictionaryGetValue(dict.get(), CFSTR("Interfaces"));
std::set<std::string> ifNames;
@@ -233,11 +227,11 @@
}
InterfaceState
-NetworkMonitor::Impl::getInterfaceState(const std::string& ifName)
+NetworkMonitorImplOsx::getInterfaceState(const std::string& ifName)
{
- CFReleaser<CFStringRef> linkName = CFStringCreateWithCString(nullptr,
- ("State:/Network/Interface/" + ifName + "/Link").c_str(),
- kCFStringEncodingASCII);
+ CFReleaser<CFStringRef> linkName =
+ CFStringCreateWithCString(nullptr, ("State:/Network/Interface/" + ifName + "/Link").c_str(),
+ kCFStringEncodingASCII);
CFReleaser<CFDictionaryRef> dict = (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), linkName.get());
if (dict.get() == nullptr) {
@@ -252,8 +246,22 @@
return CFBooleanGetValue(isActive) ? InterfaceState::RUNNING : InterfaceState::DOWN;
}
+template<typename AddressBytes>
+static uint8_t
+computePrefixLength(const AddressBytes& mask)
+{
+ uint8_t prefixLength = 0;
+ for (auto byte : mask) {
+ while (byte != 0) {
+ ++prefixLength;
+ byte <<= 1;
+ }
+ }
+ return prefixLength;
+}
+
void
-NetworkMonitor::Impl::updateInterfaceInfo(NetworkInterface& netif)
+NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif)
{
ifaddrs* ifa_list = nullptr;
if (::getifaddrs(&ifa_list) < 0) {
@@ -271,48 +279,38 @@
if (ifa->ifa_addr == nullptr)
continue;
- NetworkAddress address;
+ namespace ip = boost::asio::ip;
+ AddressFamily addressFamily = AddressFamily::UNSPECIFIED;
+ ip::address ipAddr, broadcast;
+ uint8_t prefixLength = 0;
+
switch (ifa->ifa_addr->sa_family) {
case AF_INET: {
- address.m_family = AddressFamily::V4;
+ addressFamily = AddressFamily::V4;
const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
- boost::asio::ip::address_v4::bytes_type bytes;
+ ip::address_v4::bytes_type bytes;
std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
- address.m_ip = boost::asio::ip::address_v4(bytes);
+ ipAddr = ip::address_v4(bytes);
const sockaddr_in* sinMask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask);
std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin_addr), bytes.size(), bytes.begin());
- uint8_t mask = 0;
- for (auto byte : bytes) {
- while (byte != 0) {
- ++mask;
- byte <<= 1;
- }
- }
- address.m_prefixLength = mask;
+ prefixLength = computePrefixLength(bytes);
break;
}
case AF_INET6: {
- address.m_family = AddressFamily::V6;
+ addressFamily = AddressFamily::V6;
const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
- boost::asio::ip::address_v6::bytes_type bytes;
+ ip::address_v6::bytes_type bytes;
std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin());
- address.m_ip = boost::asio::ip::address_v6(bytes);
+ ipAddr = ip::address_v6(bytes);
const sockaddr_in6* sinMask = reinterpret_cast<sockaddr_in6*>(ifa->ifa_netmask);
std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin6_addr), bytes.size(), bytes.begin());
- uint8_t mask = 0;
- for (auto byte : bytes) {
- while (byte != 0) {
- ++mask;
- byte <<= 1;
- }
- }
- address.m_prefixLength = mask;
+ prefixLength = computePrefixLength(bytes);
break;
}
@@ -339,24 +337,26 @@
if (netif.canBroadcast() && ifa->ifa_broadaddr != nullptr) {
const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
- boost::asio::ip::address_v4::bytes_type bytes;
+ ip::address_v4::bytes_type bytes;
std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
- address.m_broadcast = boost::asio::ip::address_v4(bytes);
- NDN_LOG_TRACE(netif.getName() << ": set IPv4 broadcast address " << address.m_broadcast);
+ broadcast = ip::address_v4(bytes);
+ NDN_LOG_TRACE(netif.getName() << ": set IPv4 broadcast address " << broadcast);
}
if (netif.canBroadcast()) {
netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress());
}
- netif.addNetworkAddress(address);
+ netif.addNetworkAddress(NetworkAddress(addressFamily, ipAddr, broadcast, prefixLength,
+ AddressScope::NOWHERE, 0));
+ ///\todo #3817 extract AddressScope from OS
}
::freeifaddrs(ifa_list);
}
size_t
-NetworkMonitor::Impl::getInterfaceMtu(const std::string& ifName)
+NetworkMonitorImplOsx::getInterfaceMtu(const std::string& ifName)
{
ifreq ifr{};
std::strncpy(ifr.ifr_name, ifName.c_str(), sizeof(ifr.ifr_name) - 1);
@@ -370,13 +370,13 @@
}
void
-NetworkMonitor::Impl::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context)
+NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context)
{
- static_cast<Impl*>(context)->onConfigChanged(changedKeys);
+ static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys);
}
void
-NetworkMonitor::Impl::onConfigChanged(CFArrayRef changedKeys)
+NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys)
{
size_t count = CFArrayGetCount(changedKeys);
for (size_t i = 0; i != count; ++i) {
@@ -396,7 +396,7 @@
NDN_LOG_DEBUG("removing interface " << ifName);
shared_ptr<NetworkInterface> removedInterface = ifIt->second;
m_interfaces.erase(ifIt);
- m_nm.onInterfaceRemoved(removedInterface);
+ this->emitSignal(onInterfaceRemoved, removedInterface);
};
if (key.at(-1).toUri() == "Link") {
@@ -416,17 +416,17 @@
}
if (key.at(-1).toUri() == "IPv4" || key.at(-1).toUri() == "IPv6") {
- NetworkInterface updatedInterface;
- updatedInterface.setName(ifName);
- updateInterfaceInfo(updatedInterface);
- if (updatedInterface.getType() == InterfaceType::UNKNOWN) {
+ shared_ptr<NetworkInterface> updatedInterface = makeNetworkInterface();
+ updatedInterface->setName(ifName);
+ updateInterfaceInfo(*updatedInterface);
+ if (updatedInterface->getType() == InterfaceType::UNKNOWN) {
// somehow, type of interface changed to unknown
NDN_LOG_DEBUG("Removing " << ifName << " because it changed to unhandled interface type");
removeInterface();
return;
}
- const auto& newAddrs = updatedInterface.getNetworkAddresses();
+ const auto& newAddrs = updatedInterface->getNetworkAddresses();
const auto& oldAddrs = netif.getNetworkAddresses();
std::set<NetworkAddress> added;
diff --git a/src/net/detail/network-monitor-impl-osx.hpp b/src/net/detail/network-monitor-impl-osx.hpp
index 3aac23f..d1ce8f2 100644
--- a/src/net/detail/network-monitor-impl-osx.hpp
+++ b/src/net/detail/network-monitor-impl-osx.hpp
@@ -29,7 +29,6 @@
#error "This file should not be compiled ..."
#endif
-#include "../network-interface.hpp"
#include "../../security/tpm/helper-osx.hpp"
#include "../../util/scheduler.hpp"
#include "../../util/scheduler-scoped-event-id.hpp"
@@ -42,25 +41,29 @@
namespace ndn {
namespace net {
-class NetworkMonitor::Impl
+class NetworkMonitorImplOsx : public NetworkMonitorImpl
{
public:
- Impl(NetworkMonitor& nm, boost::asio::io_service& io);
+ using Error = NetworkMonitor::Error;
- ~Impl();
+ NetworkMonitorImplOsx(boost::asio::io_service& io);
+
+ ~NetworkMonitorImplOsx();
uint32_t
- getCapabilities() const
+ getCapabilities() const final
{
- return NetworkMonitor::CAP_ENUM | NetworkMonitor::CAP_IF_ADD_REMOVE |
- NetworkMonitor::CAP_STATE_CHANGE | NetworkMonitor::CAP_ADDR_ADD_REMOVE;
+ return NetworkMonitor::CAP_ENUM |
+ NetworkMonitor::CAP_IF_ADD_REMOVE |
+ NetworkMonitor::CAP_STATE_CHANGE |
+ NetworkMonitor::CAP_ADDR_ADD_REMOVE;
}
- shared_ptr<NetworkInterface>
- getNetworkInterface(const std::string& ifname) const;
+ shared_ptr<const NetworkInterface>
+ getNetworkInterface(const std::string& ifname) const final;
- std::vector<shared_ptr<NetworkInterface>>
- listNetworkInterfaces() const;
+ std::vector<shared_ptr<const NetworkInterface>>
+ listNetworkInterfaces() const final;
static void
afterNotificationCenterEvent(CFNotificationCenterRef center,
@@ -101,8 +104,7 @@
onConfigChanged(CFArrayRef changedKeys);
private:
- NetworkMonitor& m_nm;
- std::map<std::string /*ifname*/, shared_ptr<NetworkInterface>> m_interfaces; ///< interface map
+ std::map<std::string, shared_ptr<NetworkInterface>> m_interfaces; ///< ifname => interface
util::Scheduler m_scheduler;
util::scheduler::ScopedEventId m_cfLoopEvent;
diff --git a/src/net/detail/network-monitor-impl-rtnl.cpp b/src/net/detail/network-monitor-impl-rtnl.cpp
index 1a05986..71274a1 100644
--- a/src/net/detail/network-monitor-impl-rtnl.cpp
+++ b/src/net/detail/network-monitor-impl-rtnl.cpp
@@ -40,9 +40,8 @@
namespace ndn {
namespace net {
-NetworkMonitor::Impl::Impl(NetworkMonitor& nm, boost::asio::io_service& io)
- : m_nm(nm)
- , m_socket(make_shared<boost::asio::posix::stream_descriptor>(io))
+NetworkMonitorImplRtnl::NetworkMonitorImplRtnl(boost::asio::io_service& io)
+ : m_socket(make_shared<boost::asio::posix::stream_descriptor>(io))
, m_pid(0)
, m_sequenceNo(static_cast<uint32_t>(time::system_clock::now().time_since_epoch().count()))
, m_isEnumeratingLinks(false)
@@ -56,14 +55,14 @@
m_isEnumeratingLinks = true;
}
-NetworkMonitor::Impl::~Impl()
+NetworkMonitorImplRtnl::~NetworkMonitorImplRtnl()
{
boost::system::error_code error;
m_socket->close(error);
}
-shared_ptr<NetworkInterface>
-NetworkMonitor::Impl::getNetworkInterface(const std::string& ifname) const
+shared_ptr<const NetworkInterface>
+NetworkMonitorImplRtnl::getNetworkInterface(const std::string& ifname) const
{
for (const auto& e : m_interfaces) {
if (e.second->getName() == ifname)
@@ -72,10 +71,10 @@
return nullptr;
}
-std::vector<shared_ptr<NetworkInterface>>
-NetworkMonitor::Impl::listNetworkInterfaces() const
+std::vector<shared_ptr<const NetworkInterface>>
+NetworkMonitorImplRtnl::listNetworkInterfaces() const
{
- std::vector<shared_ptr<NetworkInterface>> v;
+ std::vector<shared_ptr<const NetworkInterface>> v;
v.reserve(m_interfaces.size());
for (const auto& e : m_interfaces) {
@@ -85,13 +84,13 @@
}
bool
-NetworkMonitor::Impl::isEnumerating() const
+NetworkMonitorImplRtnl::isEnumerating() const
{
return m_isEnumeratingLinks || m_isEnumeratingAddresses;
}
void
-NetworkMonitor::Impl::initSocket()
+NetworkMonitorImplRtnl::initSocket()
{
NDN_LOG_TRACE("creating netlink socket");
@@ -129,7 +128,7 @@
}
void
-NetworkMonitor::Impl::sendDumpRequest(uint16_t nlmsgType)
+NetworkMonitorImplRtnl::sendDumpRequest(uint16_t nlmsgType)
{
auto request = make_shared<RtnlRequest>();
request->nlh.nlmsg_len = sizeof(RtnlRequest);
@@ -217,14 +216,14 @@
}
void
-NetworkMonitor::Impl::asyncRead()
+NetworkMonitorImplRtnl::asyncRead()
{
m_socket->async_read_some(boost::asio::buffer(m_buffer),
- bind(&Impl::handleRead, this, _1, _2, m_socket));
+ bind(&NetworkMonitorImplRtnl::handleRead, this, _1, _2, m_socket));
}
void
-NetworkMonitor::Impl::handleRead(const boost::system::error_code& error, size_t nBytesRead,
+NetworkMonitorImplRtnl::handleRead(const boost::system::error_code& error, size_t nBytesRead,
const shared_ptr<boost::asio::posix::stream_descriptor>& socket)
{
if (!socket->is_open() ||
@@ -252,7 +251,7 @@
}
void
-NetworkMonitor::Impl::parseNetlinkMessage(const nlmsghdr* nlh, size_t len)
+NetworkMonitorImplRtnl::parseNetlinkMessage(const nlmsghdr* nlh, size_t len)
{
while (NLMSG_OK(nlh, len)) {
NDN_LOG_TRACE("parsing " << (nlh->nlmsg_flags & NLM_F_MULTI ? "multi-part " : "") <<
@@ -275,21 +274,21 @@
case RTM_DELLINK:
parseLinkMessage(nlh, reinterpret_cast<const ifinfomsg*>(NLMSG_DATA(nlh)));
if (!isEnumerating())
- m_nm.onNetworkStateChanged(); // backward compat
+ this->emitSignal(onNetworkStateChanged); // backward compat
break;
case RTM_NEWADDR:
case RTM_DELADDR:
parseAddressMessage(nlh, reinterpret_cast<const ifaddrmsg*>(NLMSG_DATA(nlh)));
if (!isEnumerating())
- m_nm.onNetworkStateChanged(); // backward compat
+ this->emitSignal(onNetworkStateChanged); // backward compat
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
parseRouteMessage(nlh, reinterpret_cast<const rtmsg*>(NLMSG_DATA(nlh)));
if (!isEnumerating())
- m_nm.onNetworkStateChanged(); // backward compat
+ this->emitSignal(onNetworkStateChanged); // backward compat
break;
case NLMSG_ERROR: {
@@ -320,12 +319,12 @@
m_isEnumeratingAddresses = false;
// TODO: enumerate routes
NDN_LOG_DEBUG("enumeration complete");
- m_nm.onEnumerationCompleted();
+ this->emitSignal(onEnumerationCompleted);
}
}
void
-NetworkMonitor::Impl::parseLinkMessage(const nlmsghdr* nlh, const ifinfomsg* ifi)
+NetworkMonitorImplRtnl::parseLinkMessage(const nlmsghdr* nlh, const ifinfomsg* ifi)
{
if (ifiTypeToInterfaceType(ifi->ifi_type) == InterfaceType::UNKNOWN) {
NDN_LOG_DEBUG("unhandled interface type " << ifi->ifi_type);
@@ -344,14 +343,13 @@
if (interface != nullptr) {
NDN_LOG_DEBUG("removing interface " << interface->getName());
m_interfaces.erase(it);
- m_nm.onInterfaceRemoved(interface);
+ this->emitSignal(onInterfaceRemoved, interface);
}
return;
}
if (interface == nullptr) {
- // cannot use make_shared because NetworkInterface constructor is private
- interface.reset(new NetworkInterface);
+ interface = makeNetworkInterface();
interface->setIndex(ifi->ifi_index);
}
interface->setType(ifiTypeToInterfaceType(ifi->ifi_type));
@@ -405,12 +403,12 @@
if (it == m_interfaces.end()) {
NDN_LOG_DEBUG("adding interface " << interface->getName());
m_interfaces[interface->getIndex()] = interface;
- m_nm.onInterfaceAdded(interface);
+ this->emitSignal(onInterfaceAdded, interface);
}
}
void
-NetworkMonitor::Impl::parseAddressMessage(const nlmsghdr* nlh, const ifaddrmsg* ifa)
+NetworkMonitorImplRtnl::parseAddressMessage(const nlmsghdr* nlh, const ifaddrmsg* ifa)
{
auto it = m_interfaces.find(ifa->ifa_index);
if (it == m_interfaces.end()) {
@@ -422,13 +420,8 @@
BOOST_ASSERT(interface != nullptr);
namespace ip = boost::asio::ip;
-
- NetworkAddress address;
- address.m_family = ifaFamilyToAddressFamily(ifa->ifa_family);
- BOOST_ASSERT(address.m_family != AddressFamily::UNSPECIFIED);
- address.m_prefixLength = ifa->ifa_prefixlen;
- address.m_flags = ifa->ifa_flags; // will be overridden by IFA_FLAGS below, if the attribute is present
- address.m_scope = ifaScopeToAddressScope(ifa->ifa_scope);
+ ip::address ipAddr, broadcast;
+ uint32_t flags = ifa->ifa_flags; // will be overridden by IFA_FLAGS if the attribute is present
const rtattr* rta = reinterpret_cast<const rtattr*>(IFA_RTA(ifa));
size_t rtaTotalLen = IFA_PAYLOAD(nlh);
@@ -442,7 +435,7 @@
if (ifa->ifa_family == AF_INET && attrLen == sizeof(ip::address_v4::bytes_type)) {
ip::address_v4::bytes_type bytes;
std::copy_n(attrData, bytes.size(), bytes.begin());
- address.m_ip = ip::address_v4(bytes);
+ ipAddr = ip::address_v4(bytes);
}
break;
@@ -450,7 +443,7 @@
if (ifa->ifa_family == AF_INET6 && attrLen == sizeof(ip::address_v6::bytes_type)) {
ip::address_v6::bytes_type bytes;
std::copy_n(attrData, bytes.size(), bytes.begin());
- address.m_ip = ip::address_v6(bytes);
+ ipAddr = ip::address_v6(bytes);
}
break;
@@ -458,14 +451,14 @@
if (ifa->ifa_family == AF_INET && attrLen == sizeof(ip::address_v4::bytes_type)) {
ip::address_v4::bytes_type bytes;
std::copy_n(attrData, bytes.size(), bytes.begin());
- address.m_broadcast = ip::address_v4(bytes);
+ broadcast = ip::address_v4(bytes);
}
break;
#ifdef NDN_CXX_HAVE_IFA_FLAGS
case IFA_FLAGS:
if (attrLen == sizeof(uint32_t))
- address.m_flags = *(reinterpret_cast<const uint32_t*>(attrData));
+ flags = *(reinterpret_cast<const uint32_t*>(attrData));
break;
#endif // NDN_CXX_HAVE_IFA_FLAGS
}
@@ -473,6 +466,15 @@
rta = RTA_NEXT(rta, rtaTotalLen);
}
+ NetworkAddress address(
+ ifaFamilyToAddressFamily(ifa->ifa_family),
+ ipAddr,
+ broadcast,
+ ifa->ifa_prefixlen,
+ ifaScopeToAddressScope(ifa->ifa_scope),
+ flags);
+ BOOST_ASSERT(address.getFamily() != AddressFamily::UNSPECIFIED);
+
if (nlh->nlmsg_type == RTM_NEWADDR)
interface->addNetworkAddress(address);
else if (nlh->nlmsg_type == RTM_DELADDR)
@@ -480,13 +482,13 @@
}
void
-NetworkMonitor::Impl::parseRouteMessage(const nlmsghdr* nlh, const rtmsg* rtm)
+NetworkMonitorImplRtnl::parseRouteMessage(const nlmsghdr* nlh, const rtmsg* rtm)
{
// TODO
}
void
-NetworkMonitor::Impl::updateInterfaceState(NetworkInterface& interface, uint8_t operState)
+NetworkMonitorImplRtnl::updateInterfaceState(NetworkInterface& interface, uint8_t operState)
{
if (operState == linux_if::OPER_STATE_UP) {
interface.setState(InterfaceState::RUNNING);
diff --git a/src/net/detail/network-monitor-impl-rtnl.hpp b/src/net/detail/network-monitor-impl-rtnl.hpp
index d43abce..ce15dd5 100644
--- a/src/net/detail/network-monitor-impl-rtnl.hpp
+++ b/src/net/detail/network-monitor-impl-rtnl.hpp
@@ -44,17 +44,20 @@
namespace ndn {
namespace net {
-class NetworkMonitor::Impl
+class NetworkMonitorImplRtnl : public NetworkMonitorImpl
{
public:
+ using Error = NetworkMonitor::Error;
+
/** \brief initialize netlink socket and start enumerating interfaces
*/
- Impl(NetworkMonitor& nm, boost::asio::io_service& io);
+ explicit
+ NetworkMonitorImplRtnl(boost::asio::io_service& io);
- ~Impl();
+ ~NetworkMonitorImplRtnl();
uint32_t
- getCapabilities() const
+ getCapabilities() const final
{
return NetworkMonitor::CAP_ENUM |
NetworkMonitor::CAP_IF_ADD_REMOVE |
@@ -63,11 +66,11 @@
NetworkMonitor::CAP_ADDR_ADD_REMOVE;
}
- shared_ptr<NetworkInterface>
- getNetworkInterface(const std::string& ifname) const;
+ shared_ptr<const NetworkInterface>
+ getNetworkInterface(const std::string& ifname) const final;
- std::vector<shared_ptr<NetworkInterface>>
- listNetworkInterfaces() const;
+ std::vector<shared_ptr<const NetworkInterface>>
+ listNetworkInterfaces() const final;
private:
struct RtnlRequest
@@ -110,9 +113,8 @@
updateInterfaceState(NetworkInterface& interface, uint8_t operState);
private:
- NetworkMonitor& m_nm;
- std::map<int /*ifindex*/, shared_ptr<NetworkInterface>> m_interfaces; ///< interface map
- std::array<uint8_t, 16384> m_buffer; ///< holds netlink messages received from the kernel
+ std::map<int, shared_ptr<NetworkInterface>> m_interfaces; ///< ifindex => interface
+ std::array<uint8_t, 16384> m_buffer; ///< netlink messages received from the kernel
shared_ptr<boost::asio::posix::stream_descriptor> m_socket; ///< the netlink socket
uint32_t m_pid; ///< our port ID (unicast address for netlink sockets)
uint32_t m_sequenceNo; ///< sequence number of the last netlink request sent to the kernel