Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 99 | BOOST_THROW_EXCEPTION(Error("Cannot create netlink socket ("s + std::strerror(errno) + ")")); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 100 | } |
| 101 | m_socket->assign(fd); |
| 102 | |
| 103 | sockaddr_nl addr{}; |
| 104 | addr.nl_family = AF_NETLINK; |
| 105 | addr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY | |
| 106 | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | |
| 107 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; |
| 108 | if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Error("Cannot bind netlink socket ("s + std::strerror(errno) + ")")); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // find out what pid has been assigned to us |
| 113 | socklen_t len = sizeof(addr); |
| 114 | if (::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &len) < 0) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(Error("Cannot obtain netlink socket address ("s + std::strerror(errno) + ")")); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 116 | } |
| 117 | if (len != sizeof(addr)) { |
| 118 | BOOST_THROW_EXCEPTION(Error("Wrong address length (" + to_string(len) + ")")); |
| 119 | } |
| 120 | if (addr.nl_family != AF_NETLINK) { |
| 121 | BOOST_THROW_EXCEPTION(Error("Wrong address family (" + to_string(addr.nl_family) + ")")); |
| 122 | } |
| 123 | m_pid = addr.nl_pid; |
| 124 | NDN_LOG_TRACE("our pid is " << m_pid); |
| 125 | } |
| 126 | |
| 127 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 128 | NetworkMonitorImplRtnl::sendDumpRequest(uint16_t nlmsgType) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 129 | { |
| 130 | auto request = make_shared<RtnlRequest>(); |
| 131 | request->nlh.nlmsg_len = sizeof(RtnlRequest); |
| 132 | request->nlh.nlmsg_type = nlmsgType; |
| 133 | request->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 134 | request->nlh.nlmsg_seq = ++m_sequenceNo; |
| 135 | request->nlh.nlmsg_pid = m_pid; |
| 136 | request->ifi.ifi_family = AF_UNSPEC; |
| 137 | request->rta.rta_type = IFLA_EXT_MASK; |
| 138 | request->rta.rta_len = RTA_LENGTH(sizeof(request->rtext)); |
| 139 | request->rtext = 1 << 3; // RTEXT_FILTER_SKIP_STATS |
| 140 | |
| 141 | boost::asio::async_write(*m_socket, boost::asio::buffer(request.get(), sizeof(RtnlRequest)), |
| 142 | // capture 'request' to prevent its premature deallocation |
| 143 | [request] (const boost::system::error_code& error, size_t) { |
| 144 | if (error && error != boost::asio::error::operation_aborted) { |
| 145 | NDN_LOG_ERROR("write failed: " << error.message()); |
| 146 | BOOST_THROW_EXCEPTION(Error("Failed to send netlink request (" + error.message() + ")")); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | static const char* |
| 152 | nlmsgTypeToString(uint16_t type) |
| 153 | { |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 154 | #define NLMSG_STRINGIFY(x) case NLMSG_##x: return "<" #x ">" |
| 155 | #define RTM_STRINGIFY(x) case RTM_##x: return "<" #x ">" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 156 | switch (type) { |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 157 | NLMSG_STRINGIFY(NOOP); |
| 158 | NLMSG_STRINGIFY(ERROR); |
| 159 | NLMSG_STRINGIFY(DONE); |
| 160 | NLMSG_STRINGIFY(OVERRUN); |
| 161 | RTM_STRINGIFY(NEWLINK); |
| 162 | RTM_STRINGIFY(DELLINK); |
| 163 | RTM_STRINGIFY(NEWADDR); |
| 164 | RTM_STRINGIFY(DELADDR); |
| 165 | RTM_STRINGIFY(NEWROUTE); |
| 166 | RTM_STRINGIFY(DELROUTE); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 167 | default: |
| 168 | return ""; |
| 169 | } |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 170 | #undef NLMSG_STRINGIFY |
| 171 | #undef RTM_STRINGIFY |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static InterfaceType |
| 175 | ifiTypeToInterfaceType(uint16_t type) |
| 176 | { |
| 177 | switch (type) { |
| 178 | case ARPHRD_ETHER: |
| 179 | return InterfaceType::ETHERNET; |
| 180 | case ARPHRD_LOOPBACK: |
| 181 | return InterfaceType::LOOPBACK; |
| 182 | default: |
| 183 | return InterfaceType::UNKNOWN; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static AddressFamily |
| 188 | ifaFamilyToAddressFamily(uint8_t family) |
| 189 | { |
| 190 | switch (family) { |
| 191 | case AF_INET: |
| 192 | return AddressFamily::V4; |
| 193 | case AF_INET6: |
| 194 | return AddressFamily::V6; |
| 195 | default: |
| 196 | return AddressFamily::UNSPECIFIED; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static AddressScope |
| 201 | ifaScopeToAddressScope(uint8_t scope) |
| 202 | { |
| 203 | switch (scope) { |
| 204 | case RT_SCOPE_NOWHERE: |
| 205 | return AddressScope::NOWHERE; |
| 206 | case RT_SCOPE_HOST: |
| 207 | return AddressScope::HOST; |
| 208 | case RT_SCOPE_LINK: |
| 209 | return AddressScope::LINK; |
| 210 | default: |
| 211 | return AddressScope::GLOBAL; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 216 | NetworkMonitorImplRtnl::asyncRead() |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 217 | { |
| 218 | m_socket->async_read_some(boost::asio::buffer(m_buffer), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 219 | bind(&NetworkMonitorImplRtnl::handleRead, this, _1, _2, m_socket)); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 223 | NetworkMonitorImplRtnl::handleRead(const boost::system::error_code& error, size_t nBytesRead, |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 224 | const shared_ptr<boost::asio::posix::stream_descriptor>& socket) |
| 225 | { |
| 226 | if (!socket->is_open() || |
| 227 | error == boost::asio::error::operation_aborted) { |
| 228 | // socket was closed, ignore the error |
| 229 | NDN_LOG_TRACE("socket closed or operation aborted"); |
| 230 | return; |
| 231 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 232 | if (error) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 233 | NDN_LOG_ERROR("read failed: " << error.message()); |
| 234 | BOOST_THROW_EXCEPTION(Error("Netlink socket read failed (" + error.message() + ")")); |
| 235 | } |
| 236 | |
| 237 | NDN_LOG_TRACE("read " << nBytesRead << " bytes from netlink socket"); |
| 238 | |
| 239 | const nlmsghdr* nlh = reinterpret_cast<const nlmsghdr*>(m_buffer.data()); |
| 240 | if (!isEnumerating() || (nlh->nlmsg_seq == m_sequenceNo && nlh->nlmsg_pid == m_pid)) { |
| 241 | parseNetlinkMessage(nlh, nBytesRead); |
| 242 | } |
| 243 | else { |
| 244 | NDN_LOG_TRACE("seq/pid mismatch, ignoring"); |
| 245 | } |
| 246 | |
| 247 | asyncRead(); |
| 248 | } |
| 249 | |
| 250 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 251 | NetworkMonitorImplRtnl::parseNetlinkMessage(const nlmsghdr* nlh, size_t len) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 252 | { |
| 253 | while (NLMSG_OK(nlh, len)) { |
| 254 | NDN_LOG_TRACE("parsing " << (nlh->nlmsg_flags & NLM_F_MULTI ? "multi-part " : "") << |
| 255 | "message type=" << nlh->nlmsg_type << nlmsgTypeToString(nlh->nlmsg_type) << |
| 256 | " len=" << nlh->nlmsg_len << |
| 257 | " seq=" << nlh->nlmsg_seq << |
| 258 | " pid=" << nlh->nlmsg_pid); |
| 259 | |
| 260 | if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) { |
| 261 | NDN_LOG_ERROR("netlink dump was interrupted"); |
| 262 | // TODO: technically we should retry the dump... |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | if (nlh->nlmsg_type == NLMSG_DONE) |
| 267 | break; |
| 268 | |
| 269 | switch (nlh->nlmsg_type) { |
| 270 | case RTM_NEWLINK: |
| 271 | case RTM_DELLINK: |
| 272 | parseLinkMessage(nlh, reinterpret_cast<const ifinfomsg*>(NLMSG_DATA(nlh))); |
| 273 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 274 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | case RTM_NEWADDR: |
| 278 | case RTM_DELADDR: |
| 279 | parseAddressMessage(nlh, reinterpret_cast<const ifaddrmsg*>(NLMSG_DATA(nlh))); |
| 280 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 281 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 282 | break; |
| 283 | |
| 284 | case RTM_NEWROUTE: |
| 285 | case RTM_DELROUTE: |
| 286 | parseRouteMessage(nlh, reinterpret_cast<const rtmsg*>(NLMSG_DATA(nlh))); |
| 287 | if (!isEnumerating()) |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 288 | this->emitSignal(onNetworkStateChanged); // backward compat |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 289 | break; |
| 290 | |
| 291 | case NLMSG_ERROR: { |
| 292 | const nlmsgerr* err = reinterpret_cast<const nlmsgerr*>(NLMSG_DATA(nlh)); |
| 293 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(nlmsgerr))) |
| 294 | NDN_LOG_ERROR("truncated NLMSG_ERROR"); |
| 295 | else if (err->error == 0) |
| 296 | // an error code of zero indicates an ACK message, not an error |
| 297 | NDN_LOG_TRACE("ACK"); |
| 298 | else |
| 299 | NDN_LOG_ERROR("NLMSG_ERROR: " << std::strerror(std::abs(err->error))); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | nlh = NLMSG_NEXT(nlh, len); |
| 305 | } |
| 306 | |
| 307 | if (nlh->nlmsg_type == NLMSG_DONE && m_isEnumeratingLinks) { |
| 308 | // links enumeration complete, now request all the addresses |
| 309 | m_isEnumeratingLinks = false; |
| 310 | NDN_LOG_TRACE("enumerating addresses"); |
| 311 | sendDumpRequest(RTM_GETADDR); |
| 312 | m_isEnumeratingAddresses = true; |
| 313 | } |
| 314 | else if (nlh->nlmsg_type == NLMSG_DONE && m_isEnumeratingAddresses) { |
| 315 | // links and addresses enumeration complete |
| 316 | m_isEnumeratingAddresses = false; |
| 317 | // TODO: enumerate routes |
| 318 | NDN_LOG_DEBUG("enumeration complete"); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 319 | this->emitSignal(onEnumerationCompleted); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 324 | NetworkMonitorImplRtnl::parseLinkMessage(const nlmsghdr* nlh, const ifinfomsg* ifi) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 325 | { |
| 326 | if (ifiTypeToInterfaceType(ifi->ifi_type) == InterfaceType::UNKNOWN) { |
| 327 | NDN_LOG_DEBUG("unhandled interface type " << ifi->ifi_type); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 331 | shared_ptr<NetworkInterface> interface; |
| 332 | auto it = m_interfaces.find(ifi->ifi_index); |
| 333 | if (it != m_interfaces.end()) { |
| 334 | interface = it->second; |
| 335 | BOOST_ASSERT(interface != nullptr); |
| 336 | BOOST_ASSERT(interface->getIndex() == ifi->ifi_index); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 339 | if (nlh->nlmsg_type == RTM_DELLINK) { |
| 340 | if (interface != nullptr) { |
| 341 | NDN_LOG_DEBUG("removing interface " << interface->getName()); |
| 342 | m_interfaces.erase(it); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 343 | this->emitSignal(onInterfaceRemoved, interface); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 344 | } |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | if (interface == nullptr) { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 349 | interface = makeNetworkInterface(); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 350 | interface->setIndex(ifi->ifi_index); |
| 351 | } |
| 352 | interface->setType(ifiTypeToInterfaceType(ifi->ifi_type)); |
| 353 | interface->setFlags(ifi->ifi_flags); |
| 354 | |
| 355 | const rtattr* rta = reinterpret_cast<const rtattr*>(IFLA_RTA(ifi)); |
| 356 | size_t rtaTotalLen = IFLA_PAYLOAD(nlh); |
| 357 | uint8_t operState = linux_if::OPER_STATE_UNKNOWN; |
| 358 | |
| 359 | while (RTA_OK(rta, rtaTotalLen)) { |
| 360 | size_t attrLen = RTA_PAYLOAD(rta); |
| 361 | |
| 362 | switch (rta->rta_type) { |
| 363 | case IFLA_ADDRESS: |
| 364 | if (attrLen == ethernet::ADDR_LEN) { |
| 365 | ethernet::Address addr(reinterpret_cast<const uint8_t*>(RTA_DATA(rta))); |
| 366 | interface->setEthernetAddress(addr); |
| 367 | } |
| 368 | break; |
| 369 | |
| 370 | case IFLA_BROADCAST: |
| 371 | if (attrLen == ethernet::ADDR_LEN) { |
| 372 | ethernet::Address addr(reinterpret_cast<const uint8_t*>(RTA_DATA(rta))); |
| 373 | interface->setEthernetBroadcastAddress(addr); |
| 374 | } |
| 375 | break; |
| 376 | |
| 377 | case IFLA_IFNAME: { |
| 378 | auto attrData = reinterpret_cast<const char*>(RTA_DATA(rta)); |
| 379 | if (::strnlen(attrData, attrLen) <= attrLen) |
| 380 | interface->setName(attrData); |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | case IFLA_MTU: |
| 385 | if (attrLen == sizeof(uint32_t)) |
| 386 | interface->setMtu(*(reinterpret_cast<const uint32_t*>(RTA_DATA(rta)))); |
| 387 | break; |
| 388 | |
| 389 | case IFLA_OPERSTATE: |
| 390 | if (attrLen == sizeof(uint8_t)) |
| 391 | operState = *(reinterpret_cast<const uint8_t*>RTA_DATA(rta)); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | rta = RTA_NEXT(rta, rtaTotalLen); |
| 396 | } |
| 397 | |
| 398 | updateInterfaceState(*interface, operState); |
| 399 | |
| 400 | if (it == m_interfaces.end()) { |
| 401 | NDN_LOG_DEBUG("adding interface " << interface->getName()); |
| 402 | m_interfaces[interface->getIndex()] = interface; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 403 | this->emitSignal(onInterfaceAdded, interface); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 408 | NetworkMonitorImplRtnl::parseAddressMessage(const nlmsghdr* nlh, const ifaddrmsg* ifa) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 409 | { |
| 410 | auto it = m_interfaces.find(ifa->ifa_index); |
| 411 | if (it == m_interfaces.end()) { |
| 412 | // unknown interface, ignore message |
| 413 | NDN_LOG_TRACE("unknown interface index " << ifa->ifa_index); |
| 414 | return; |
| 415 | } |
| 416 | auto interface = it->second; |
| 417 | BOOST_ASSERT(interface != nullptr); |
| 418 | |
| 419 | namespace ip = boost::asio::ip; |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 420 | ip::address ipAddr, broadcastAddr; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 421 | 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] | 422 | |
| 423 | const rtattr* rta = reinterpret_cast<const rtattr*>(IFA_RTA(ifa)); |
| 424 | size_t rtaTotalLen = IFA_PAYLOAD(nlh); |
| 425 | |
| 426 | while (RTA_OK(rta, rtaTotalLen)) { |
| 427 | auto attrData = reinterpret_cast<const unsigned char*>(RTA_DATA(rta)); |
| 428 | size_t attrLen = RTA_PAYLOAD(rta); |
| 429 | |
| 430 | switch (rta->rta_type) { |
| 431 | case IFA_LOCAL: |
| 432 | if (ifa->ifa_family == AF_INET && attrLen == sizeof(ip::address_v4::bytes_type)) { |
| 433 | ip::address_v4::bytes_type bytes; |
| 434 | std::copy_n(attrData, bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 435 | ipAddr = ip::address_v4(bytes); |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 436 | } |
| 437 | break; |
| 438 | |
| 439 | case IFA_ADDRESS: |
| 440 | if (ifa->ifa_family == AF_INET6 && attrLen == sizeof(ip::address_v6::bytes_type)) { |
| 441 | ip::address_v6::bytes_type bytes; |
| 442 | std::copy_n(attrData, bytes.size(), bytes.begin()); |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 443 | ip::address_v6 v6Addr(bytes); |
| 444 | if (v6Addr.is_link_local()) |
| 445 | v6Addr.scope_id(ifa->ifa_index); |
| 446 | ipAddr = v6Addr; |
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()); |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 454 | broadcastAddr = 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 | |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 469 | NetworkAddress address(ifaFamilyToAddressFamily(ifa->ifa_family), |
| 470 | ipAddr, |
| 471 | broadcastAddr, |
| 472 | ifa->ifa_prefixlen, |
| 473 | ifaScopeToAddressScope(ifa->ifa_scope), |
| 474 | flags); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 475 | BOOST_ASSERT(address.getFamily() != AddressFamily::UNSPECIFIED); |
| 476 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 477 | if (nlh->nlmsg_type == RTM_NEWADDR) |
| 478 | interface->addNetworkAddress(address); |
| 479 | else if (nlh->nlmsg_type == RTM_DELADDR) |
| 480 | interface->removeNetworkAddress(address); |
| 481 | } |
| 482 | |
| 483 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 484 | NetworkMonitorImplRtnl::parseRouteMessage(const nlmsghdr* nlh, const rtmsg* rtm) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 485 | { |
| 486 | // TODO |
| 487 | } |
| 488 | |
| 489 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 490 | NetworkMonitorImplRtnl::updateInterfaceState(NetworkInterface& interface, uint8_t operState) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 491 | { |
| 492 | if (operState == linux_if::OPER_STATE_UP) { |
| 493 | interface.setState(InterfaceState::RUNNING); |
| 494 | } |
| 495 | else if (operState == linux_if::OPER_STATE_DORMANT) { |
| 496 | interface.setState(InterfaceState::DORMANT); |
| 497 | } |
| 498 | else { |
| 499 | // fallback to flags |
| 500 | auto flags = interface.getFlags(); |
| 501 | if ((flags & linux_if::FLAG_LOWER_UP) && !(flags & linux_if::FLAG_DORMANT)) |
| 502 | interface.setState(InterfaceState::RUNNING); |
| 503 | else if (flags & IFF_UP) |
| 504 | interface.setState(InterfaceState::NO_CARRIER); |
| 505 | else |
| 506 | interface.setState(InterfaceState::DOWN); |
| 507 | } |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 510 | } // namespace net |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 511 | } // namespace ndn |