net: add NetworkMonitorStub
refs #4024
Change-Id: I20da6f3351f51667b045987dc07e88ba814a4456
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);