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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [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. |
| 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 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 53 | #include "network-monitor-impl-osx.hpp" |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 54 | |
| 55 | #include "../network-address.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 | |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 59 | #include <ifaddrs.h> // for getifaddrs() |
| 60 | #include <net/if.h> // for if_nametoindex() |
| 61 | #include <net/if_dl.h> // for struct sockaddr_dl |
| 62 | #include <net/if_types.h> // for IFT_* constants |
| 63 | #include <netinet/in.h> // for struct sockaddr_in{,6} |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 64 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 65 | #include <boost/asio/io_service.hpp> |
| 66 | #include <boost/asio/ip/address.hpp> |
| 67 | #include <boost/asio/ip/udp.hpp> |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 68 | |
Davide Pesavento | 755f8a8 | 2018-08-24 00:06:45 -0400 | [diff] [blame] | 69 | NDN_LOG_INIT(ndn.NetworkMonitor); |
| 70 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 71 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 72 | namespace net { |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 73 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 74 | using util::CFReleaser; |
| 75 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 76 | class IfAddrs : noncopyable |
| 77 | { |
| 78 | public: |
| 79 | IfAddrs() |
| 80 | { |
| 81 | if (::getifaddrs(&m_ifaList) < 0) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 82 | BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error("getifaddrs() failed: "s + strerror(errno))); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | ~IfAddrs() |
| 87 | { |
| 88 | if (m_ifaList != nullptr) { |
| 89 | ::freeifaddrs(m_ifaList); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ifaddrs* |
| 94 | get() const noexcept |
| 95 | { |
| 96 | return m_ifaList; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | ifaddrs* m_ifaList = nullptr; |
| 101 | }; |
| 102 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 103 | NetworkMonitorImplOsx::NetworkMonitorImplOsx(boost::asio::io_service& io) |
| 104 | : m_scheduler(io) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 105 | , m_cfLoopEvent(m_scheduler) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 106 | , m_context{0, this, nullptr, nullptr, nullptr} |
| 107 | , m_scStore(SCDynamicStoreCreate(nullptr, CFSTR("net.named-data.ndn-cxx.NetworkMonitor"), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 108 | &NetworkMonitorImplOsx::onConfigChanged, &m_context)) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 109 | , m_loopSource(SCDynamicStoreCreateRunLoopSource(nullptr, m_scStore.get(), 0)) |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 110 | , m_ioctlSocket(io, boost::asio::ip::udp::v4()) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 111 | { |
| 112 | scheduleCfLoop(); |
| 113 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 114 | // Notifications from Darwin Notify Center: |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 115 | // |
| 116 | // com.apple.system.config.network_change |
| 117 | // |
| 118 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 119 | static_cast<void*>(this), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 120 | &NetworkMonitorImplOsx::afterNotificationCenterEvent, |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 121 | CFSTR("com.apple.system.config.network_change"), |
| 122 | nullptr, // object to observe |
| 123 | CFNotificationSuspensionBehaviorDeliverImmediately); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 124 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 125 | CFRunLoopAddSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode); |
| 126 | |
| 127 | // Notifications from SystemConfiguration: |
| 128 | // |
| 129 | // State:/Network/Interface/.*/Link |
| 130 | // State:/Network/Interface/.*/IPv4 |
| 131 | // State:/Network/Interface/.*/IPv6 |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 132 | // State:/Network/Interface/.*/AirPort (not used) |
| 133 | // |
| 134 | // https://developer.apple.com/library/content/documentation/Networking/Conceptual/SystemConfigFrameworks/SC_UnderstandSchema/SC_UnderstandSchema.html |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 135 | // |
| 136 | auto patterns = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks); |
| 137 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/Link")); |
| 138 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv4")); |
| 139 | CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv6")); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 140 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 141 | if (!SCDynamicStoreSetNotificationKeys(m_scStore.get(), nullptr, patterns)) { |
| 142 | BOOST_THROW_EXCEPTION(Error("SCDynamicStoreSetNotificationKeys failed")); |
| 143 | } |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 144 | |
| 145 | io.post([this] { enumerateInterfaces(); }); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 148 | NetworkMonitorImplOsx::~NetworkMonitorImplOsx() |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 149 | { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 150 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode); |
| 151 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 152 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 153 | static_cast<void*>(this)); |
| 154 | } |
| 155 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 156 | shared_ptr<const NetworkInterface> |
| 157 | NetworkMonitorImplOsx::getNetworkInterface(const std::string& ifname) const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 158 | { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 159 | auto it = m_interfaces.find(ifname); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 160 | return it == m_interfaces.end() ? nullptr : it->second; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 163 | std::vector<shared_ptr<const NetworkInterface>> |
| 164 | NetworkMonitorImplOsx::listNetworkInterfaces() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 165 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 166 | std::vector<shared_ptr<const NetworkInterface>> v; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 167 | v.reserve(m_interfaces.size()); |
| 168 | |
| 169 | for (const auto& e : m_interfaces) { |
| 170 | v.push_back(e.second); |
| 171 | } |
| 172 | return v; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 175 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 176 | NetworkMonitorImplOsx::afterNotificationCenterEvent(CFNotificationCenterRef center, |
| 177 | void* observer, |
| 178 | CFStringRef name, |
| 179 | const void* object, |
| 180 | CFDictionaryRef userInfo) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 181 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 182 | static_cast<NetworkMonitorImplOsx*>(observer)->emitSignal(onNetworkStateChanged); |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 186 | NetworkMonitorImplOsx::scheduleCfLoop() |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 187 | { |
| 188 | // poll each second for new events |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 189 | m_cfLoopEvent = m_scheduler.scheduleEvent(1_s, [this] { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 190 | // this should dispatch ready events and exit |
| 191 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); |
| 192 | scheduleCfLoop(); |
| 193 | }); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 197 | NetworkMonitorImplOsx::enumerateInterfaces() |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 198 | { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 199 | IfAddrs ifaList; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 200 | for (const auto& ifName : getInterfaceNames()) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 201 | addNewInterface(ifName, ifaList); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 202 | } |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 203 | this->emitSignal(onEnumerationCompleted); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static std::string |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 207 | convertToStdString(CFStringRef cfStr) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 208 | { |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 209 | const char* cStr = CFStringGetCStringPtr(cfStr, kCFStringEncodingASCII); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 210 | if (cStr != nullptr) { |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 211 | // fast path |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 212 | return cStr; |
| 213 | } |
| 214 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 215 | // reserve space for the string + null terminator |
| 216 | std::string str(CFStringGetLength(cfStr) + 1, '\0'); |
| 217 | if (!CFStringGetCString(cfStr, &str.front(), str.size(), kCFStringEncodingASCII)) { |
| 218 | BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error("CFString conversion failed")); |
| 219 | } |
| 220 | |
| 221 | // drop the null terminator, std::string doesn't need it |
| 222 | str.pop_back(); |
| 223 | return str; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | std::set<std::string> |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 227 | NetworkMonitorImplOsx::getInterfaceNames() const |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 228 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 229 | CFReleaser<CFDictionaryRef> dict = |
| 230 | (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface")); |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 231 | if (dict.get() == nullptr) { |
| 232 | return {}; |
| 233 | } |
| 234 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 235 | CFArrayRef interfaces = (CFArrayRef)CFDictionaryGetValue(dict.get(), CFSTR("Interfaces")); |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 236 | if (interfaces == nullptr) { |
| 237 | return {}; |
| 238 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 239 | |
| 240 | std::set<std::string> ifNames; |
| 241 | size_t count = CFArrayGetCount(interfaces); |
| 242 | for (size_t i = 0; i != count; ++i) { |
| 243 | auto ifName = (CFStringRef)CFArrayGetValueAtIndex(interfaces, i); |
| 244 | ifNames.insert(convertToStdString(ifName)); |
| 245 | } |
| 246 | return ifNames; |
| 247 | } |
| 248 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 249 | void |
| 250 | NetworkMonitorImplOsx::addNewInterface(const std::string& ifName, const IfAddrs& ifaList) |
| 251 | { |
| 252 | shared_ptr<NetworkInterface> interface = makeNetworkInterface(); |
| 253 | interface->setName(ifName); |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 254 | interface->setState(getInterfaceState(*interface)); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 255 | updateInterfaceInfo(*interface, ifaList); |
| 256 | |
| 257 | if (interface->getType() == InterfaceType::UNKNOWN) { |
| 258 | NDN_LOG_DEBUG("ignoring " << ifName << " due to unhandled interface type"); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | NDN_LOG_DEBUG("adding interface " << interface->getName()); |
| 263 | m_interfaces[interface->getName()] = interface; |
| 264 | this->emitSignal(onInterfaceAdded, interface); |
| 265 | } |
| 266 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 267 | InterfaceState |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 268 | NetworkMonitorImplOsx::getInterfaceState(const NetworkInterface& netif) const |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 269 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 270 | CFReleaser<CFStringRef> linkName = |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 271 | CFStringCreateWithCString(kCFAllocatorDefault, |
| 272 | ("State:/Network/Interface/" + netif.getName() + "/Link").data(), |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 273 | kCFStringEncodingASCII); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 274 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 275 | CFReleaser<CFDictionaryRef> dict = |
| 276 | (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), linkName.get()); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 277 | if (dict.get() == nullptr) { |
| 278 | return InterfaceState::UNKNOWN; |
| 279 | } |
| 280 | |
| 281 | CFBooleanRef isActive = (CFBooleanRef)CFDictionaryGetValue(dict.get(), CFSTR("Active")); |
| 282 | if (isActive == nullptr) { |
| 283 | return InterfaceState::UNKNOWN; |
| 284 | } |
| 285 | |
| 286 | return CFBooleanGetValue(isActive) ? InterfaceState::RUNNING : InterfaceState::DOWN; |
| 287 | } |
| 288 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 289 | size_t |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 290 | NetworkMonitorImplOsx::getInterfaceMtu(const NetworkInterface& netif) |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 291 | { |
| 292 | ifreq ifr{}; |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 293 | std::strncpy(ifr.ifr_name, netif.getName().data(), sizeof(ifr.ifr_name) - 1); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 294 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 295 | if (::ioctl(m_ioctlSocket.native_handle(), SIOCGIFMTU, &ifr) == 0) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 296 | return static_cast<size_t>(ifr.ifr_mtu); |
| 297 | } |
| 298 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 299 | NDN_LOG_WARN("failed to get MTU of " << netif.getName() << ": " << std::strerror(errno)); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 300 | return ethernet::MAX_DATA_LEN; |
| 301 | } |
| 302 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 303 | template<typename AddressBytes> |
| 304 | static uint8_t |
| 305 | computePrefixLength(const AddressBytes& mask) |
| 306 | { |
| 307 | uint8_t prefixLength = 0; |
| 308 | for (auto byte : mask) { |
| 309 | while (byte != 0) { |
| 310 | ++prefixLength; |
| 311 | byte <<= 1; |
| 312 | } |
| 313 | } |
| 314 | return prefixLength; |
| 315 | } |
| 316 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 317 | void |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 318 | NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif, const IfAddrs& ifaList) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 319 | { |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 320 | BOOST_ASSERT(!netif.getName().empty()); |
| 321 | |
| 322 | netif.setMtu(getInterfaceMtu(netif)); |
| 323 | |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 324 | for (ifaddrs* ifa = ifaList.get(); ifa != nullptr; ifa = ifa->ifa_next) { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 325 | if (ifa->ifa_name != netif.getName()) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 326 | continue; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 327 | |
| 328 | netif.setFlags(ifa->ifa_flags); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 329 | |
| 330 | if (ifa->ifa_addr == nullptr) |
| 331 | continue; |
| 332 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 333 | namespace ip = boost::asio::ip; |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 334 | AddressFamily addrFamily = AddressFamily::UNSPECIFIED; |
| 335 | ip::address ipAddr, broadcastAddr; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 336 | uint8_t prefixLength = 0; |
| 337 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 338 | switch (ifa->ifa_addr->sa_family) { |
| 339 | case AF_INET: { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 340 | addrFamily = AddressFamily::V4; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 341 | |
| 342 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 343 | ip::address_v4::bytes_type bytes; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 344 | 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] | 345 | ipAddr = ip::address_v4(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 346 | |
| 347 | const sockaddr_in* sinMask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask); |
| 348 | 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] | 349 | prefixLength = computePrefixLength(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | |
| 353 | case AF_INET6: { |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 354 | addrFamily = AddressFamily::V6; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 355 | |
| 356 | const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 357 | ip::address_v6::bytes_type bytes; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 358 | std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin()); |
Davide Pesavento | 2520371 | 2017-10-09 23:50:03 -0400 | [diff] [blame] | 359 | ip::address_v6 v6Addr(bytes); |
| 360 | if (v6Addr.is_link_local()) |
| 361 | v6Addr.scope_id(if_nametoindex(netif.getName().data())); |
| 362 | ipAddr = v6Addr; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 363 | |
| 364 | const sockaddr_in6* sinMask = reinterpret_cast<sockaddr_in6*>(ifa->ifa_netmask); |
| 365 | 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] | 366 | prefixLength = computePrefixLength(bytes); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | |
| 370 | case AF_LINK: { |
| 371 | const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr); |
| 372 | netif.setIndex(sdl->sdl_index); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 373 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 374 | if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) { |
| 375 | netif.setType(InterfaceType::ETHERNET); |
| 376 | netif.setEthernetAddress(ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)))); |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 377 | NDN_LOG_TRACE(netif.getName() << " has Ethernet address " << netif.getEthernetAddress()); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 378 | } |
| 379 | else if (sdl->sdl_type == IFT_LOOP) { |
| 380 | netif.setType(InterfaceType::LOOPBACK); |
| 381 | } |
| 382 | else { |
| 383 | netif.setType(InterfaceType::UNKNOWN); |
| 384 | } |
| 385 | break; |
| 386 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (netif.canBroadcast()) { |
| 390 | netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress()); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 391 | |
| 392 | if (addrFamily == AddressFamily::V4 && ifa->ifa_broadaddr != nullptr) { |
| 393 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr); |
| 394 | ip::address_v4::bytes_type bytes; |
| 395 | std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin()); |
| 396 | broadcastAddr = ip::address_v4(bytes); |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 397 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 400 | if (addrFamily == AddressFamily::UNSPECIFIED) |
| 401 | continue; |
| 402 | |
Davide Pesavento | b2ae336 | 2017-07-13 01:43:14 -0400 | [diff] [blame] | 403 | AddressScope scope = AddressScope::GLOBAL; |
| 404 | if (ipAddr.is_loopback()) { |
| 405 | scope = AddressScope::HOST; |
| 406 | } |
| 407 | else if ((ipAddr.is_v4() && (ipAddr.to_v4().to_ulong() & 0xFFFF0000) == 0xA9FE0000) || |
| 408 | (ipAddr.is_v6() && ipAddr.to_v6().is_link_local())) { |
| 409 | scope = AddressScope::LINK; |
| 410 | } |
| 411 | |
| 412 | netif.addNetworkAddress(NetworkAddress(addrFamily, ipAddr, broadcastAddr, prefixLength, scope, 0)); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 413 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 417 | NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 418 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 419 | static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 423 | NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys) |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 424 | { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 425 | IfAddrs ifaList; |
| 426 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 427 | size_t count = CFArrayGetCount(changedKeys); |
| 428 | for (size_t i = 0; i != count; ++i) { |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 429 | Name key(convertToStdString((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i))); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 430 | std::string ifName = key.at(-2).toUri(); |
| 431 | |
| 432 | auto ifIt = m_interfaces.find(ifName); |
| 433 | if (ifIt == m_interfaces.end()) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 434 | addNewInterface(ifName, ifaList); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 438 | auto removeInterface = [&] { |
| 439 | NDN_LOG_DEBUG("removing interface " << ifName); |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 440 | shared_ptr<NetworkInterface> removedNetif = ifIt->second; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 441 | m_interfaces.erase(ifIt); |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 442 | this->emitSignal(onInterfaceRemoved, removedNetif); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 443 | }; |
| 444 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 445 | NetworkInterface& netif = *ifIt->second; |
| 446 | std::string changedItem = key.at(-1).toUri(); |
| 447 | if (changedItem == "Link") { |
| 448 | auto newState = getInterfaceState(netif); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 449 | if (newState == InterfaceState::UNKNOWN) { |
| 450 | // check if it is really unknown or interface removed |
| 451 | if (getInterfaceNames().count(ifName) == 0) { |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 452 | removeInterface(); |
| 453 | return; |
| 454 | } |
| 455 | } |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 456 | NDN_LOG_TRACE(ifName << " status changed from " << netif.getState() << " to " << newState); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 457 | netif.setState(newState); |
| 458 | } |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 459 | else if (changedItem == "IPv4" || changedItem == "IPv6") { |
| 460 | auto updatedNetif = makeNetworkInterface(); |
| 461 | updatedNetif->setName(ifName); |
| 462 | updateInterfaceInfo(*updatedNetif, ifaList); |
| 463 | if (updatedNetif->getType() == InterfaceType::UNKNOWN) { |
Davide Pesavento | c008798 | 2017-07-16 23:32:34 -0400 | [diff] [blame] | 464 | NDN_LOG_DEBUG(ifName << " type changed to unknown"); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 465 | removeInterface(); |
| 466 | return; |
| 467 | } |
| 468 | |
Davide Pesavento | f0c827e | 2017-10-03 17:25:00 -0400 | [diff] [blame] | 469 | const auto& newAddrs = updatedNetif->getNetworkAddresses(); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 470 | const auto& oldAddrs = netif.getNetworkAddresses(); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 471 | std::set<NetworkAddress> added; |
| 472 | std::set<NetworkAddress> removed; |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 473 | std::set_difference(newAddrs.begin(), newAddrs.end(), |
| 474 | oldAddrs.begin(), oldAddrs.end(), std::inserter(added, added.end())); |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 475 | std::set_difference(oldAddrs.begin(), oldAddrs.end(), |
| 476 | newAddrs.begin(), newAddrs.end(), std::inserter(removed, removed.end())); |
| 477 | |
| 478 | for (const auto& addr : removed) { |
| 479 | netif.removeNetworkAddress(addr); |
| 480 | } |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 481 | for (const auto& addr : added) { |
| 482 | netif.addNetworkAddress(addr); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 488 | } // namespace net |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 489 | } // namespace ndn |