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