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 | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 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. |
| 20 | * |
| 21 | * |
| 22 | * Parts of this implementation is based on daemondo command of MacPorts |
| 23 | * (https://www.macports.org/): |
| 24 | * |
| 25 | * Copyright (c) 2005-2007 James Berry <jberry@macports.org> |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * Redistribution and use in source and binary forms, with or without |
| 29 | * modification, are permitted provided that the following conditions |
| 30 | * are met: |
| 31 | * 1. Redistributions of source code must retain the above copyright |
| 32 | * notice, this list of conditions and the following disclaimer. |
| 33 | * 2. Redistributions in binary form must reproduce the above copyright |
| 34 | * notice, this list of conditions and the following disclaimer in the |
| 35 | * documentation and/or other materials provided with the distribution. |
| 36 | * 3. Neither the name of The MacPorts Project nor the names of its contributors |
| 37 | * may be used to endorse or promote products derived from this software |
| 38 | * without specific prior written permission. |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 41 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 44 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 45 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 46 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 47 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 48 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 50 | * POSSIBILITY OF SUCH DAMAGE. |
| 51 | */ |
| 52 | |
| 53 | #include "ndn-cxx-config.hpp" |
| 54 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 55 | #include "network-monitor-impl-osx.hpp" |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 56 | #include "../../name.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 57 | #include "../../util/logger.hpp" |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 58 | #include "../network-address.hpp" |
| 59 | |
| 60 | #include <ifaddrs.h> // for getifaddrs() |
| 61 | #include <arpa/inet.h> // for inet_ntop() |
| 62 | #include <netinet/in.h> // for struct sockaddr_in{,6} |
| 63 | #include <net/if_dl.h> // for struct sockaddr_dl |
| 64 | #include <net/if_types.h> // for IFT_* constants |
| 65 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 66 | #include <boost/asio/io_service.hpp> |
| 67 | #include <boost/asio/ip/address.hpp> |
| 68 | #include <boost/asio/ip/udp.hpp> |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 69 | |
| 70 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 71 | namespace net { |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 72 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 73 | using util::CFReleaser; |
| 74 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 75 | NDN_LOG_INIT(ndn.NetworkMonitor); |
| 76 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 77 | class IfAddrs : noncopyable |
| 78 | { |
| 79 | public: |
| 80 | IfAddrs() |
| 81 | { |
| 82 | if (::getifaddrs(&m_ifaList) < 0) { |
| 83 | BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error(std::string("getifaddrs() failed: ") + |
| 84 | strerror(errno))); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | ~IfAddrs() |
| 89 | { |
| 90 | if (m_ifaList != nullptr) { |
| 91 | ::freeifaddrs(m_ifaList); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ifaddrs* |
| 96 | get() const noexcept |
| 97 | { |
| 98 | return m_ifaList; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | ifaddrs* m_ifaList = nullptr; |
| 103 | }; |
| 104 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 105 | NetworkMonitorImplOsx::NetworkMonitorImplOsx(boost::asio::io_service& io) |
| 106 | : m_scheduler(io) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 107 | , m_cfLoopEvent(m_scheduler) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 108 | , m_context{0, this, nullptr, nullptr, nullptr} |
| 109 | , m_scStore(SCDynamicStoreCreate(nullptr, CFSTR("net.named-data.ndn-cxx.NetworkMonitor"), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 110 | &NetworkMonitorImplOsx::onConfigChanged, &m_context)) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 111 | , m_loopSource(SCDynamicStoreCreateRunLoopSource(nullptr, m_scStore.get(), 0)) |
| 112 | , m_nullUdpSocket(io, boost::asio::ip::udp::v4()) |
| 113 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 114 | { |
| 115 | scheduleCfLoop(); |
| 116 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 117 | // Notifications from Darwin Notify Center: |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 118 | // |
| 119 | // com.apple.system.config.network_change |
| 120 | // |
| 121 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 122 | static_cast<void*>(this), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 123 | &NetworkMonitorImplOsx::afterNotificationCenterEvent, |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 124 | CFSTR("com.apple.system.config.network_change"), |
| 125 | nullptr, // object to observe |
| 126 | CFNotificationSuspensionBehaviorDeliverImmediately); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 127 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 128 | CFRunLoopAddSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode); |
| 129 | |
| 130 | // Notifications from SystemConfiguration: |
| 131 | // |
| 132 | // State:/Network/Interface/.*/Link |
| 133 | // State:/Network/Interface/.*/IPv4 |
| 134 | // State:/Network/Interface/.*/IPv6 |
| 135 | // State:/Network/Global/DNS |
| 136 | // State:/Network/Global/IPv4 |
| 137 | // |
| 138 | auto patterns = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks); |
| 139 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/Link")); |
| 140 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv4")); |
| 141 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv6")); |
| 142 | // CFArrayAppendValue(patterns, CFSTR("State:/Network/Global/DNS")); |
| 143 | // CFArrayAppendValue(patterns, CFSTR("State:/Network/Global/IPv4")); |
| 144 | |
| 145 | SCDynamicStoreSetNotificationKeys(m_scStore.get(), nullptr, patterns); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 146 | |
| 147 | io.post([this] { enumerateInterfaces(); }); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 150 | NetworkMonitorImplOsx::~NetworkMonitorImplOsx() |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 151 | { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 152 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode); |
| 153 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 154 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 155 | static_cast<void*>(this)); |
| 156 | } |
| 157 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 158 | shared_ptr<const NetworkInterface> |
| 159 | NetworkMonitorImplOsx::getNetworkInterface(const std::string& ifname) const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 161 | auto it = m_interfaces.find(ifname); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 162 | return it == m_interfaces.end() ? nullptr : it->second; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 165 | std::vector<shared_ptr<const NetworkInterface>> |
| 166 | NetworkMonitorImplOsx::listNetworkInterfaces() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 167 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 168 | std::vector<shared_ptr<const NetworkInterface>> v; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 169 | v.reserve(m_interfaces.size()); |
| 170 | |
| 171 | for (const auto& e : m_interfaces) { |
| 172 | v.push_back(e.second); |
| 173 | } |
| 174 | return v; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 177 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 178 | NetworkMonitorImplOsx::afterNotificationCenterEvent(CFNotificationCenterRef center, |
| 179 | void* observer, |
| 180 | CFStringRef name, |
| 181 | const void* object, |
| 182 | CFDictionaryRef userInfo) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 183 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 184 | static_cast<NetworkMonitorImplOsx*>(observer)->emitSignal(onNetworkStateChanged); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 188 | NetworkMonitorImplOsx::scheduleCfLoop() |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 189 | { |
| 190 | // poll each second for new events |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 191 | m_cfLoopEvent = m_scheduler.scheduleEvent(time::seconds(1), [this] { |
| 192 | // this should dispatch ready events and exit |
| 193 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); |
| 194 | scheduleCfLoop(); |
| 195 | }); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 199 | NetworkMonitorImplOsx::enumerateInterfaces() |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 200 | { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 201 | IfAddrs ifaList; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 202 | for (const auto& ifName : getInterfaceNames()) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 203 | addNewInterface(ifName, ifaList); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 204 | } |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 205 | this->emitSignal(onEnumerationCompleted); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static std::string |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 209 | convertToStdString(CFStringRef cfStr) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 210 | { |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 211 | const char* cStr = CFStringGetCStringPtr(cfStr, kCFStringEncodingASCII); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 212 | if (cStr != nullptr) { |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 213 | // fast path |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 214 | return cStr; |
| 215 | } |
| 216 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 217 | // reserve space for the string + null terminator |
| 218 | std::string str(CFStringGetLength(cfStr) + 1, '\0'); |
| 219 | if (!CFStringGetCString(cfStr, &str.front(), str.size(), kCFStringEncodingASCII)) { |
| 220 | BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error("CFString conversion failed")); |
| 221 | } |
| 222 | |
| 223 | // drop the null terminator, std::string doesn't need it |
| 224 | str.pop_back(); |
| 225 | return str; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | std::set<std::string> |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 229 | NetworkMonitorImplOsx::getInterfaceNames() const |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 230 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 231 | CFReleaser<CFDictionaryRef> dict = |
| 232 | (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface")); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 233 | CFArrayRef interfaces = (CFArrayRef)CFDictionaryGetValue(dict.get(), CFSTR("Interfaces")); |
| 234 | |
| 235 | std::set<std::string> ifNames; |
| 236 | size_t count = CFArrayGetCount(interfaces); |
| 237 | for (size_t i = 0; i != count; ++i) { |
| 238 | auto ifName = (CFStringRef)CFArrayGetValueAtIndex(interfaces, i); |
| 239 | ifNames.insert(convertToStdString(ifName)); |
| 240 | } |
| 241 | return ifNames; |
| 242 | } |
| 243 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 244 | void |
| 245 | NetworkMonitorImplOsx::addNewInterface(const std::string& ifName, const IfAddrs& ifaList) |
| 246 | { |
| 247 | shared_ptr<NetworkInterface> interface = makeNetworkInterface(); |
| 248 | interface->setName(ifName); |
| 249 | interface->setState(getInterfaceState(interface->getName())); |
| 250 | updateInterfaceInfo(*interface, ifaList); |
| 251 | |
| 252 | if (interface->getType() == InterfaceType::UNKNOWN) { |
| 253 | NDN_LOG_DEBUG("ignoring " << ifName << " due to unhandled interface type"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | NDN_LOG_DEBUG("adding interface " << interface->getName()); |
| 258 | m_interfaces[interface->getName()] = interface; |
| 259 | this->emitSignal(onInterfaceAdded, interface); |
| 260 | } |
| 261 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 262 | InterfaceState |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 263 | NetworkMonitorImplOsx::getInterfaceState(const std::string& ifName) const |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 264 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 265 | CFReleaser<CFStringRef> linkName = |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 266 | CFStringCreateWithCString(nullptr, ("State:/Network/Interface/" + ifName + "/Link").data(), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 267 | kCFStringEncodingASCII); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 268 | |
| 269 | CFReleaser<CFDictionaryRef> dict = (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), linkName.get()); |
| 270 | if (dict.get() == nullptr) { |
| 271 | return InterfaceState::UNKNOWN; |
| 272 | } |
| 273 | |
| 274 | CFBooleanRef isActive = (CFBooleanRef)CFDictionaryGetValue(dict.get(), CFSTR("Active")); |
| 275 | if (isActive == nullptr) { |
| 276 | return InterfaceState::UNKNOWN; |
| 277 | } |
| 278 | |
| 279 | return CFBooleanGetValue(isActive) ? InterfaceState::RUNNING : InterfaceState::DOWN; |
| 280 | } |
| 281 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 282 | size_t |
| 283 | NetworkMonitorImplOsx::getInterfaceMtu(const std::string& ifName) |
| 284 | { |
| 285 | ifreq ifr{}; |
| 286 | std::strncpy(ifr.ifr_name, ifName.data(), sizeof(ifr.ifr_name) - 1); |
| 287 | |
| 288 | if (::ioctl(m_nullUdpSocket.native_handle(), SIOCGIFMTU, &ifr) == 0) { |
| 289 | return static_cast<size_t>(ifr.ifr_mtu); |
| 290 | } |
| 291 | |
| 292 | NDN_LOG_WARN("failed to get MTU of " << ifName << ": " << std::strerror(errno)); |
| 293 | return ethernet::MAX_DATA_LEN; |
| 294 | } |
| 295 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 296 | template<typename AddressBytes> |
| 297 | static uint8_t |
| 298 | computePrefixLength(const AddressBytes& mask) |
| 299 | { |
| 300 | uint8_t prefixLength = 0; |
| 301 | for (auto byte : mask) { |
| 302 | while (byte != 0) { |
| 303 | ++prefixLength; |
| 304 | byte <<= 1; |
| 305 | } |
| 306 | } |
| 307 | return prefixLength; |
| 308 | } |
| 309 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 310 | void |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 311 | NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif, const IfAddrs& ifaList) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 312 | { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 313 | for (ifaddrs* ifa = ifaList.get(); ifa != nullptr; ifa = ifa->ifa_next) { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 314 | if (ifa->ifa_name != netif.getName()) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 315 | continue; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 316 | |
| 317 | netif.setFlags(ifa->ifa_flags); |
| 318 | netif.setMtu(getInterfaceMtu(netif.getName())); |
| 319 | |
| 320 | if (ifa->ifa_addr == nullptr) |
| 321 | continue; |
| 322 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 323 | namespace ip = boost::asio::ip; |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 324 | AddressFamily addrFamily = AddressFamily::UNSPECIFIED; |
| 325 | ip::address ipAddr, broadcastAddr; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 326 | uint8_t prefixLength = 0; |
| 327 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 328 | switch (ifa->ifa_addr->sa_family) { |
| 329 | case AF_INET: { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 330 | addrFamily = AddressFamily::V4; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 331 | |
| 332 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 333 | ip::address_v4::bytes_type bytes; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 334 | std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 335 | ipAddr = ip::address_v4(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 336 | |
| 337 | const sockaddr_in* sinMask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask); |
| 338 | std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin_addr), bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 339 | prefixLength = computePrefixLength(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | |
| 343 | case AF_INET6: { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 344 | addrFamily = AddressFamily::V6; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 345 | |
| 346 | const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 347 | ip::address_v6::bytes_type bytes; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 348 | std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 349 | ipAddr = ip::address_v6(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 350 | |
| 351 | const sockaddr_in6* sinMask = reinterpret_cast<sockaddr_in6*>(ifa->ifa_netmask); |
| 352 | std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin6_addr), bytes.size(), bytes.begin()); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 353 | prefixLength = computePrefixLength(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | |
| 357 | case AF_LINK: { |
| 358 | const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr); |
| 359 | netif.setIndex(sdl->sdl_index); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 360 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 361 | if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) { |
| 362 | netif.setType(InterfaceType::ETHERNET); |
| 363 | netif.setEthernetAddress(ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)))); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 364 | NDN_LOG_TRACE(netif.getName() << " has Ethernet address " << netif.getEthernetAddress()); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 365 | } |
| 366 | else if (sdl->sdl_type == IFT_LOOP) { |
| 367 | netif.setType(InterfaceType::LOOPBACK); |
| 368 | } |
| 369 | else { |
| 370 | netif.setType(InterfaceType::UNKNOWN); |
| 371 | } |
| 372 | break; |
| 373 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (netif.canBroadcast()) { |
| 377 | netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress()); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 378 | |
| 379 | if (addrFamily == AddressFamily::V4 && ifa->ifa_broadaddr != nullptr) { |
| 380 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr); |
| 381 | ip::address_v4::bytes_type bytes; |
| 382 | std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin()); |
| 383 | broadcastAddr = ip::address_v4(bytes); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 384 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 385 | } |
| 386 | |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 387 | AddressScope scope = AddressScope::GLOBAL; |
| 388 | if (ipAddr.is_loopback()) { |
| 389 | scope = AddressScope::HOST; |
| 390 | } |
| 391 | else if ((ipAddr.is_v4() && (ipAddr.to_v4().to_ulong() & 0xFFFF0000) == 0xA9FE0000) || |
| 392 | (ipAddr.is_v6() && ipAddr.to_v6().is_link_local())) { |
| 393 | scope = AddressScope::LINK; |
| 394 | } |
| 395 | |
| 396 | netif.addNetworkAddress(NetworkAddress(addrFamily, ipAddr, broadcastAddr, prefixLength, scope, 0)); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 397 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 401 | NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 402 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 403 | static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 407 | NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 408 | { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 409 | IfAddrs ifaList; |
| 410 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 411 | size_t count = CFArrayGetCount(changedKeys); |
| 412 | for (size_t i = 0; i != count; ++i) { |
| 413 | std::string keyName = convertToStdString((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i)); |
| 414 | Name key(keyName); |
| 415 | std::string ifName = key.at(-2).toUri(); |
| 416 | |
| 417 | auto ifIt = m_interfaces.find(ifName); |
| 418 | if (ifIt == m_interfaces.end()) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 419 | addNewInterface(ifName, ifaList); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
| 423 | NetworkInterface& netif = *ifIt->second; |
| 424 | |
| 425 | auto removeInterface = [&] { |
| 426 | NDN_LOG_DEBUG("removing interface " << ifName); |
| 427 | shared_ptr<NetworkInterface> removedInterface = ifIt->second; |
| 428 | m_interfaces.erase(ifIt); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 429 | this->emitSignal(onInterfaceRemoved, removedInterface); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | if (key.at(-1).toUri() == "Link") { |
| 433 | auto newState = getInterfaceState(ifName); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 434 | if (newState == InterfaceState::UNKNOWN) { |
| 435 | // check if it is really unknown or interface removed |
| 436 | if (getInterfaceNames().count(ifName) == 0) { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 437 | removeInterface(); |
| 438 | return; |
| 439 | } |
| 440 | } |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 441 | NDN_LOG_TRACE(ifName << " status changed from " << netif.getState() << " to " << newState); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 442 | netif.setState(newState); |
| 443 | } |
| 444 | |
| 445 | if (key.at(-1).toUri() == "IPv4" || key.at(-1).toUri() == "IPv6") { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 446 | shared_ptr<NetworkInterface> updatedInterface = makeNetworkInterface(); |
| 447 | updatedInterface->setName(ifName); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 448 | updateInterfaceInfo(*updatedInterface, ifaList); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 449 | if (updatedInterface->getType() == InterfaceType::UNKNOWN) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 450 | NDN_LOG_DEBUG(ifName << " type changed to unknown"); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 451 | removeInterface(); |
| 452 | return; |
| 453 | } |
| 454 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 455 | const auto& newAddrs = updatedInterface->getNetworkAddresses(); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 456 | const auto& oldAddrs = netif.getNetworkAddresses(); |
| 457 | |
| 458 | std::set<NetworkAddress> added; |
| 459 | std::set<NetworkAddress> removed; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 460 | std::set_difference(newAddrs.begin(), newAddrs.end(), |
| 461 | oldAddrs.begin(), oldAddrs.end(), std::inserter(added, added.end())); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 462 | std::set_difference(oldAddrs.begin(), oldAddrs.end(), |
| 463 | newAddrs.begin(), newAddrs.end(), std::inserter(removed, removed.end())); |
| 464 | |
| 465 | for (const auto& addr : removed) { |
| 466 | netif.removeNetworkAddress(addr); |
| 467 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 468 | for (const auto& addr : added) { |
| 469 | netif.addNetworkAddress(addr); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 475 | } // namespace net |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 476 | } // namespace ndn |