Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 20 | * |
| 21 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 24 | #include "network-monitor-impl-rtnl.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 25 | #include "linux-if-constants.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 26 | #include "../network-address.hpp" |
| 27 | #include "../network-interface.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 28 | #include "../../util/logger.hpp" |
| 29 | #include "../../util/time.hpp" |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 30 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 31 | #include <boost/asio/write.hpp> |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 32 | |
| 33 | #include <cerrno> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 34 | #include <cstdlib> |
| 35 | #include <net/if_arp.h> |
| 36 | #include <sys/socket.h> |
| 37 | |
| 38 | NDN_LOG_INIT(ndn.NetworkMonitor); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 39 | |
| 40 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 41 | namespace net { |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 42 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 43 | NetworkMonitorImplRtnl::NetworkMonitorImplRtnl(boost::asio::io_service& io) |
| 44 | : m_socket(make_shared<boost::asio::posix::stream_descriptor>(io)) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 45 | , m_pid(0) |
| 46 | , m_sequenceNo(static_cast<uint32_t>(time::system_clock::now().time_since_epoch().count())) |
| 47 | , m_isEnumeratingLinks(false) |
| 48 | , m_isEnumeratingAddresses(false) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 49 | { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 50 | initSocket(); |
| 51 | asyncRead(); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 52 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 53 | NDN_LOG_TRACE("enumerating links"); |
| 54 | sendDumpRequest(RTM_GETLINK); |
| 55 | m_isEnumeratingLinks = true; |
| 56 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 57 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 58 | NetworkMonitorImplRtnl::~NetworkMonitorImplRtnl() |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 59 | { |
| 60 | boost::system::error_code error; |
| 61 | m_socket->close(error); |
| 62 | } |
| 63 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 64 | shared_ptr<const NetworkInterface> |
| 65 | NetworkMonitorImplRtnl::getNetworkInterface(const std::string& ifname) const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 66 | { |
| 67 | for (const auto& e : m_interfaces) { |
| 68 | if (e.second->getName() == ifname) |
| 69 | return e.second; |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 70 | } |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 71 | return nullptr; |
| 72 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 73 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 74 | std::vector<shared_ptr<const NetworkInterface>> |
| 75 | NetworkMonitorImplRtnl::listNetworkInterfaces() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 76 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 77 | std::vector<shared_ptr<const NetworkInterface>> v; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 78 | v.reserve(m_interfaces.size()); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 79 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 80 | for (const auto& e : m_interfaces) { |
| 81 | v.push_back(e.second); |
| 82 | } |
| 83 | return v; |
| 84 | } |
| 85 | |
| 86 | bool |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 87 | NetworkMonitorImplRtnl::isEnumerating() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 88 | { |
| 89 | return m_isEnumeratingLinks || m_isEnumeratingAddresses; |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 93 | NetworkMonitorImplRtnl::initSocket() |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 94 | { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 95 | NDN_LOG_TRACE("creating netlink socket"); |
| 96 | |
| 97 | int fd = ::socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); |
| 98 | if (fd < 0) { |
| 99 | BOOST_THROW_EXCEPTION(Error(std::string("Cannot create netlink socket (") + |
| 100 | std::strerror(errno) + ")")); |
| 101 | } |
| 102 | m_socket->assign(fd); |
| 103 | |
| 104 | sockaddr_nl addr{}; |
| 105 | addr.nl_family = AF_NETLINK; |
| 106 | addr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY | |
| 107 | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | |
| 108 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; |
| 109 | if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) { |
| 110 | BOOST_THROW_EXCEPTION(Error(std::string("Cannot bind netlink socket (") + |
| 111 | std::strerror(errno) + ")")); |
| 112 | } |
| 113 | |
| 114 | // find out what pid has been assigned to us |
| 115 | socklen_t len = sizeof(addr); |
| 116 | if (::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &len) < 0) { |
| 117 | BOOST_THROW_EXCEPTION(Error(std::string("Cannot obtain netlink socket address (") + |
| 118 | std::strerror(errno) + ")")); |
| 119 | } |
| 120 | if (len != sizeof(addr)) { |
| 121 | BOOST_THROW_EXCEPTION(Error("Wrong address length (" + to_string(len) + ")")); |
| 122 | } |
| 123 | if (addr.nl_family != AF_NETLINK) { |
| 124 | BOOST_THROW_EXCEPTION(Error("Wrong address family (" + to_string(addr.nl_family) + ")")); |
| 125 | } |
| 126 | m_pid = addr.nl_pid; |
| 127 | NDN_LOG_TRACE("our pid is " << m_pid); |
| 128 | } |
| 129 | |
| 130 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 131 | NetworkMonitorImplRtnl::sendDumpRequest(uint16_t nlmsgType) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 132 | { |
| 133 | auto request = make_shared<RtnlRequest>(); |
| 134 | request->nlh.nlmsg_len = sizeof(RtnlRequest); |
| 135 | request->nlh.nlmsg_type = nlmsgType; |
| 136 | request->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 137 | request->nlh.nlmsg_seq = ++m_sequenceNo; |
| 138 | request->nlh.nlmsg_pid = m_pid; |
| 139 | request->ifi.ifi_family = AF_UNSPEC; |
| 140 | request->rta.rta_type = IFLA_EXT_MASK; |
| 141 | request->rta.rta_len = RTA_LENGTH(sizeof(request->rtext)); |
| 142 | request->rtext = 1 << 3; // RTEXT_FILTER_SKIP_STATS |
| 143 | |
| 144 | boost::asio::async_write(*m_socket, boost::asio::buffer(request.get(), sizeof(RtnlRequest)), |
| 145 | // capture 'request' to prevent its premature deallocation |
| 146 | [request] (const boost::system::error_code& error, size_t) { |
| 147 | if (error && error != boost::asio::error::operation_aborted) { |
| 148 | NDN_LOG_ERROR("write failed: " << error.message()); |
| 149 | BOOST_THROW_EXCEPTION(Error("Failed to send netlink request (" + error.message() + ")")); |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | static const char* |
| 155 | nlmsgTypeToString(uint16_t type) |
| 156 | { |
| 157 | #define NDN_NLMSG_STRING(x) case NLMSG_##x: return "<" #x ">" |
| 158 | #define NDN_RTM_STRING(x) case RTM_##x: return "<" #x ">" |
| 159 | switch (type) { |
| 160 | NDN_NLMSG_STRING(NOOP); |
| 161 | NDN_NLMSG_STRING(ERROR); |
| 162 | NDN_NLMSG_STRING(DONE); |
| 163 | NDN_NLMSG_STRING(OVERRUN); |
| 164 | NDN_RTM_STRING(NEWLINK); |
| 165 | NDN_RTM_STRING(DELLINK); |
| 166 | NDN_RTM_STRING(NEWADDR); |
| 167 | NDN_RTM_STRING(DELADDR); |
| 168 | NDN_RTM_STRING(NEWROUTE); |
| 169 | NDN_RTM_STRING(DELROUTE); |
| 170 | default: |
| 171 | return ""; |
| 172 | } |
| 173 | #undef NDN_NLMSG_STRING |
| 174 | #undef NDN_RTM_STRING |
| 175 | } |
| 176 | |
| 177 | static InterfaceType |
| 178 | ifiTypeToInterfaceType(uint16_t type) |
| 179 | { |
| 180 | switch (type) { |
| 181 | case ARPHRD_ETHER: |
| 182 | return InterfaceType::ETHERNET; |
| 183 | case ARPHRD_LOOPBACK: |
| 184 | return InterfaceType::LOOPBACK; |
| 185 | default: |
| 186 | return InterfaceType::UNKNOWN; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static AddressFamily |
| 191 | ifaFamilyToAddressFamily(uint8_t family) |
| 192 | { |
| 193 | switch (family) { |
| 194 | case AF_INET: |
| 195 | return AddressFamily::V4; |
| 196 | case AF_INET6: |
| 197 | return AddressFamily::V6; |
| 198 | default: |
| 199 | return AddressFamily::UNSPECIFIED; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static AddressScope |
| 204 | ifaScopeToAddressScope(uint8_t scope) |
| 205 | { |
| 206 | switch (scope) { |
| 207 | case RT_SCOPE_NOWHERE: |
| 208 | return AddressScope::NOWHERE; |
| 209 | case RT_SCOPE_HOST: |
| 210 | return AddressScope::HOST; |
| 211 | case RT_SCOPE_LINK: |
| 212 | return AddressScope::LINK; |
| 213 | default: |
| 214 | return AddressScope::GLOBAL; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 219 | NetworkMonitorImplRtnl::asyncRead() |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 220 | { |
| 221 | m_socket->async_read_some(boost::asio::buffer(m_buffer), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 222 | bind(&NetworkMonitorImplRtnl::handleRead, this, _1, _2, m_socket)); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 226 | NetworkMonitorImplRtnl::handleRead(const boost::system::error_code& error, size_t nBytesRead, |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 227 | const shared_ptr<boost::asio::posix::stream_descriptor>& socket) |
| 228 | { |
| 229 | if (!socket->is_open() || |
| 230 | error == boost::asio::error::operation_aborted) { |
| 231 | // socket was closed, ignore the error |
| 232 | NDN_LOG_TRACE("socket closed or operation aborted"); |
| 233 | return; |
| 234 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 235 | if (error) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 236 | NDN_LOG_ERROR("read failed: " << error.message()); |
| 237 | BOOST_THROW_EXCEPTION(Error("Netlink socket read failed (" + error.message() + ")")); |
| 238 | } |
| 239 | |
| 240 | NDN_LOG_TRACE("read " << nBytesRead << " bytes from netlink socket"); |
| 241 | |
| 242 | const nlmsghdr* nlh = reinterpret_cast<const nlmsghdr*>(m_buffer.data()); |
| 243 | if (!isEnumerating() || (nlh->nlmsg_seq == m_sequenceNo && nlh->nlmsg_pid == m_pid)) { |
| 244 | parseNetlinkMessage(nlh, nBytesRead); |
| 245 | } |
| 246 | else { |
| 247 | NDN_LOG_TRACE("seq/pid mismatch, ignoring"); |
| 248 | } |
| 249 | |
| 250 | asyncRead(); |
| 251 | } |
| 252 | |
| 253 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 254 | NetworkMonitorImplRtnl::parseNetlinkMessage(const nlmsghdr* nlh, size_t len) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 255 | { |
| 256 | while (NLMSG_OK(nlh, len)) { |
| 257 | NDN_LOG_TRACE("parsing " << (nlh->nlmsg_flags & NLM_F_MULTI ? "multi-part " : "") << |
| 258 | "message type=" << nlh->nlmsg_type << nlmsgTypeToString(nlh->nlmsg_type) << |
| 259 | " len=" << nlh->nlmsg_len << |
| 260 | " seq=" << nlh->nlmsg_seq << |
| 261 | " pid=" << nlh->nlmsg_pid); |
| 262 | |
| 263 | if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) { |
| 264 | NDN_LOG_ERROR("netlink dump was interrupted"); |
| 265 | // TODO: technically we should retry the dump... |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | if (nlh->nlmsg_type == NLMSG_DONE) |
| 270 | break; |
| 271 | |
| 272 | switch (nlh->nlmsg_type) { |
| 273 | case RTM_NEWLINK: |
| 274 | case RTM_DELLINK: |
| 275 | parseLinkMessage(nlh, reinterpret_cast<const ifinfomsg*>(NLMSG_DATA(nlh))); |
| 276 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 277 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 278 | break; |
| 279 | |
| 280 | case RTM_NEWADDR: |
| 281 | case RTM_DELADDR: |
| 282 | parseAddressMessage(nlh, reinterpret_cast<const ifaddrmsg*>(NLMSG_DATA(nlh))); |
| 283 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 284 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | case RTM_NEWROUTE: |
| 288 | case RTM_DELROUTE: |
| 289 | parseRouteMessage(nlh, reinterpret_cast<const rtmsg*>(NLMSG_DATA(nlh))); |
| 290 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 291 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 292 | break; |
| 293 | |
| 294 | case NLMSG_ERROR: { |
| 295 | const nlmsgerr* err = reinterpret_cast<const nlmsgerr*>(NLMSG_DATA(nlh)); |
| 296 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(nlmsgerr))) |
| 297 | NDN_LOG_ERROR("truncated NLMSG_ERROR"); |
| 298 | else if (err->error == 0) |
| 299 | // an error code of zero indicates an ACK message, not an error |
| 300 | NDN_LOG_TRACE("ACK"); |
| 301 | else |
| 302 | NDN_LOG_ERROR("NLMSG_ERROR: " << std::strerror(std::abs(err->error))); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | nlh = NLMSG_NEXT(nlh, len); |
| 308 | } |
| 309 | |
| 310 | if (nlh->nlmsg_type == NLMSG_DONE && m_isEnumeratingLinks) { |
| 311 | // links enumeration complete, now request all the addresses |
| 312 | m_isEnumeratingLinks = false; |
| 313 | NDN_LOG_TRACE("enumerating addresses"); |
| 314 | sendDumpRequest(RTM_GETADDR); |
| 315 | m_isEnumeratingAddresses = true; |
| 316 | } |
| 317 | else if (nlh->nlmsg_type == NLMSG_DONE && m_isEnumeratingAddresses) { |
| 318 | // links and addresses enumeration complete |
| 319 | m_isEnumeratingAddresses = false; |
| 320 | // TODO: enumerate routes |
| 321 | NDN_LOG_DEBUG("enumeration complete"); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 322 | this->emitSignal(onEnumerationCompleted); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 327 | NetworkMonitorImplRtnl::parseLinkMessage(const nlmsghdr* nlh, const ifinfomsg* ifi) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 328 | { |
| 329 | if (ifiTypeToInterfaceType(ifi->ifi_type) == InterfaceType::UNKNOWN) { |
| 330 | NDN_LOG_DEBUG("unhandled interface type " << ifi->ifi_type); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 334 | shared_ptr<NetworkInterface> interface; |
| 335 | auto it = m_interfaces.find(ifi->ifi_index); |
| 336 | if (it != m_interfaces.end()) { |
| 337 | interface = it->second; |
| 338 | BOOST_ASSERT(interface != nullptr); |
| 339 | BOOST_ASSERT(interface->getIndex() == ifi->ifi_index); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 342 | if (nlh->nlmsg_type == RTM_DELLINK) { |
| 343 | if (interface != nullptr) { |
| 344 | NDN_LOG_DEBUG("removing interface " << interface->getName()); |
| 345 | m_interfaces.erase(it); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 346 | this->emitSignal(onInterfaceRemoved, interface); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 347 | } |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | if (interface == nullptr) { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 352 | interface = makeNetworkInterface(); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 353 | interface->setIndex(ifi->ifi_index); |
| 354 | } |
| 355 | interface->setType(ifiTypeToInterfaceType(ifi->ifi_type)); |
| 356 | interface->setFlags(ifi->ifi_flags); |
| 357 | |
| 358 | const rtattr* rta = reinterpret_cast<const rtattr*>(IFLA_RTA(ifi)); |
| 359 | size_t rtaTotalLen = IFLA_PAYLOAD(nlh); |
| 360 | uint8_t operState = linux_if::OPER_STATE_UNKNOWN; |
| 361 | |
| 362 | while (RTA_OK(rta, rtaTotalLen)) { |
| 363 | size_t attrLen = RTA_PAYLOAD(rta); |
| 364 | |
| 365 | switch (rta->rta_type) { |
| 366 | case IFLA_ADDRESS: |
| 367 | if (attrLen == ethernet::ADDR_LEN) { |
| 368 | ethernet::Address addr(reinterpret_cast<const uint8_t*>(RTA_DATA(rta))); |
| 369 | interface->setEthernetAddress(addr); |
| 370 | } |
| 371 | break; |
| 372 | |
| 373 | case IFLA_BROADCAST: |
| 374 | if (attrLen == ethernet::ADDR_LEN) { |
| 375 | ethernet::Address addr(reinterpret_cast<const uint8_t*>(RTA_DATA(rta))); |
| 376 | interface->setEthernetBroadcastAddress(addr); |
| 377 | } |
| 378 | break; |
| 379 | |
| 380 | case IFLA_IFNAME: { |
| 381 | auto attrData = reinterpret_cast<const char*>(RTA_DATA(rta)); |
| 382 | if (::strnlen(attrData, attrLen) <= attrLen) |
| 383 | interface->setName(attrData); |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | case IFLA_MTU: |
| 388 | if (attrLen == sizeof(uint32_t)) |
| 389 | interface->setMtu(*(reinterpret_cast<const uint32_t*>(RTA_DATA(rta)))); |
| 390 | break; |
| 391 | |
| 392 | case IFLA_OPERSTATE: |
| 393 | if (attrLen == sizeof(uint8_t)) |
| 394 | operState = *(reinterpret_cast<const uint8_t*>RTA_DATA(rta)); |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | rta = RTA_NEXT(rta, rtaTotalLen); |
| 399 | } |
| 400 | |
| 401 | updateInterfaceState(*interface, operState); |
| 402 | |
| 403 | if (it == m_interfaces.end()) { |
| 404 | NDN_LOG_DEBUG("adding interface " << interface->getName()); |
| 405 | m_interfaces[interface->getIndex()] = interface; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 406 | this->emitSignal(onInterfaceAdded, interface); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 411 | NetworkMonitorImplRtnl::parseAddressMessage(const nlmsghdr* nlh, const ifaddrmsg* ifa) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 412 | { |
| 413 | auto it = m_interfaces.find(ifa->ifa_index); |
| 414 | if (it == m_interfaces.end()) { |
| 415 | // unknown interface, ignore message |
| 416 | NDN_LOG_TRACE("unknown interface index " << ifa->ifa_index); |
| 417 | return; |
| 418 | } |
| 419 | auto interface = it->second; |
| 420 | BOOST_ASSERT(interface != nullptr); |
| 421 | |
| 422 | namespace ip = boost::asio::ip; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 423 | ip::address ipAddr, broadcast; |
| 424 | uint32_t flags = ifa->ifa_flags; // will be overridden by IFA_FLAGS if the attribute is present |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 425 | |
| 426 | const rtattr* rta = reinterpret_cast<const rtattr*>(IFA_RTA(ifa)); |
| 427 | size_t rtaTotalLen = IFA_PAYLOAD(nlh); |
| 428 | |
| 429 | while (RTA_OK(rta, rtaTotalLen)) { |
| 430 | auto attrData = reinterpret_cast<const unsigned char*>(RTA_DATA(rta)); |
| 431 | size_t attrLen = RTA_PAYLOAD(rta); |
| 432 | |
| 433 | switch (rta->rta_type) { |
| 434 | case IFA_LOCAL: |
| 435 | if (ifa->ifa_family == AF_INET && attrLen == sizeof(ip::address_v4::bytes_type)) { |
| 436 | ip::address_v4::bytes_type bytes; |
| 437 | std::copy_n(attrData, bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 438 | ipAddr = ip::address_v4(bytes); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 439 | } |
| 440 | break; |
| 441 | |
| 442 | case IFA_ADDRESS: |
| 443 | if (ifa->ifa_family == AF_INET6 && attrLen == sizeof(ip::address_v6::bytes_type)) { |
| 444 | ip::address_v6::bytes_type bytes; |
| 445 | std::copy_n(attrData, bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 446 | ipAddr = ip::address_v6(bytes); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 447 | } |
| 448 | break; |
| 449 | |
| 450 | case IFA_BROADCAST: |
| 451 | if (ifa->ifa_family == AF_INET && attrLen == sizeof(ip::address_v4::bytes_type)) { |
| 452 | ip::address_v4::bytes_type bytes; |
| 453 | std::copy_n(attrData, bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 454 | broadcast = ip::address_v4(bytes); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | |
| 458 | #ifdef NDN_CXX_HAVE_IFA_FLAGS |
| 459 | case IFA_FLAGS: |
| 460 | if (attrLen == sizeof(uint32_t)) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 461 | flags = *(reinterpret_cast<const uint32_t*>(attrData)); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 462 | break; |
| 463 | #endif // NDN_CXX_HAVE_IFA_FLAGS |
| 464 | } |
| 465 | |
| 466 | rta = RTA_NEXT(rta, rtaTotalLen); |
| 467 | } |
| 468 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 469 | NetworkAddress address( |
| 470 | ifaFamilyToAddressFamily(ifa->ifa_family), |
| 471 | ipAddr, |
| 472 | broadcast, |
| 473 | ifa->ifa_prefixlen, |
| 474 | ifaScopeToAddressScope(ifa->ifa_scope), |
| 475 | flags); |
| 476 | BOOST_ASSERT(address.getFamily() != AddressFamily::UNSPECIFIED); |
| 477 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 478 | if (nlh->nlmsg_type == RTM_NEWADDR) |
| 479 | interface->addNetworkAddress(address); |
| 480 | else if (nlh->nlmsg_type == RTM_DELADDR) |
| 481 | interface->removeNetworkAddress(address); |
| 482 | } |
| 483 | |
| 484 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 485 | NetworkMonitorImplRtnl::parseRouteMessage(const nlmsghdr* nlh, const rtmsg* rtm) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 486 | { |
| 487 | // TODO |
| 488 | } |
| 489 | |
| 490 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame^] | 491 | NetworkMonitorImplRtnl::updateInterfaceState(NetworkInterface& interface, uint8_t operState) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 492 | { |
| 493 | if (operState == linux_if::OPER_STATE_UP) { |
| 494 | interface.setState(InterfaceState::RUNNING); |
| 495 | } |
| 496 | else if (operState == linux_if::OPER_STATE_DORMANT) { |
| 497 | interface.setState(InterfaceState::DORMANT); |
| 498 | } |
| 499 | else { |
| 500 | // fallback to flags |
| 501 | auto flags = interface.getFlags(); |
| 502 | if ((flags & linux_if::FLAG_LOWER_UP) && !(flags & linux_if::FLAG_DORMANT)) |
| 503 | interface.setState(InterfaceState::RUNNING); |
| 504 | else if (flags & IFF_UP) |
| 505 | interface.setState(InterfaceState::NO_CARRIER); |
| 506 | else |
| 507 | interface.setState(InterfaceState::DOWN); |
| 508 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 511 | } // namespace net |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 512 | } // namespace ndn |