blob: 7af533716728fa78f83eb610b0d079cd64277417 [file] [log] [blame]
Davide Pesavento9a8bae52016-02-24 20:33:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob2ae3362017-07-13 01:43:14 -04002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Davide Pesavento9a8bae52016-02-24 20:33:08 +01004 *
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 Pesavento9a8bae52016-02-24 20:33:08 +010053#include "network-monitor-impl-osx.hpp"
Davide Pesaventof0c827e2017-10-03 17:25:00 -040054
55#include "../network-address.hpp"
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050056#include "../../name.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000057#include "../../util/logger.hpp"
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050058
Davide Pesavento25203712017-10-09 23:50:03 -040059#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 Afanasyev3b3355c2017-03-26 11:57:13 -050064
Davide Pesaventof35c4272017-07-14 11:13:34 -040065#include <boost/asio/io_service.hpp>
66#include <boost/asio/ip/address.hpp>
67#include <boost/asio/ip/udp.hpp>
Davide Pesavento9a8bae52016-02-24 20:33:08 +010068
69namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000070namespace net {
Davide Pesavento9a8bae52016-02-24 20:33:08 +010071
Junxiao Shi0b1b4672017-07-02 22:02:31 -070072using util::CFReleaser;
73
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050074NDN_LOG_INIT(ndn.NetworkMonitor);
75
Davide Pesaventoc0087982017-07-16 23:32:34 -040076class IfAddrs : noncopyable
77{
78public:
79 IfAddrs()
80 {
81 if (::getifaddrs(&m_ifaList) < 0) {
82 BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error(std::string("getifaddrs() failed: ") +
83 strerror(errno)));
84 }
85 }
86
87 ~IfAddrs()
88 {
89 if (m_ifaList != nullptr) {
90 ::freeifaddrs(m_ifaList);
91 }
92 }
93
94 ifaddrs*
95 get() const noexcept
96 {
97 return m_ifaList;
98 }
99
100private:
101 ifaddrs* m_ifaList = nullptr;
102};
103
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000104NetworkMonitorImplOsx::NetworkMonitorImplOsx(boost::asio::io_service& io)
105 : m_scheduler(io)
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100106 , m_cfLoopEvent(m_scheduler)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500107 , m_context{0, this, nullptr, nullptr, nullptr}
108 , m_scStore(SCDynamicStoreCreate(nullptr, CFSTR("net.named-data.ndn-cxx.NetworkMonitor"),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000109 &NetworkMonitorImplOsx::onConfigChanged, &m_context))
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500110 , m_loopSource(SCDynamicStoreCreateRunLoopSource(nullptr, m_scStore.get(), 0))
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400111 , m_ioctlSocket(io, boost::asio::ip::udp::v4())
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100112{
113 scheduleCfLoop();
114
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500115 // Notifications from Darwin Notify Center:
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100116 //
117 // com.apple.system.config.network_change
118 //
119 CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
120 static_cast<void*>(this),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000121 &NetworkMonitorImplOsx::afterNotificationCenterEvent,
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100122 CFSTR("com.apple.system.config.network_change"),
123 nullptr, // object to observe
124 CFNotificationSuspensionBehaviorDeliverImmediately);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500125
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500126 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode);
127
128 // Notifications from SystemConfiguration:
129 //
130 // State:/Network/Interface/.*/Link
131 // State:/Network/Interface/.*/IPv4
132 // State:/Network/Interface/.*/IPv6
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400133 // State:/Network/Interface/.*/AirPort (not used)
134 //
135 // https://developer.apple.com/library/content/documentation/Networking/Conceptual/SystemConfigFrameworks/SC_UnderstandSchema/SC_UnderstandSchema.html
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500136 //
137 auto patterns = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks);
138 CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/Link"));
139 CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv4"));
140 CFArrayAppendValue(patterns, CFSTR("State:/Network/Interface/.*/IPv6"));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500141
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400142 if (!SCDynamicStoreSetNotificationKeys(m_scStore.get(), nullptr, patterns)) {
143 BOOST_THROW_EXCEPTION(Error("SCDynamicStoreSetNotificationKeys failed"));
144 }
Davide Pesaventoc0087982017-07-16 23:32:34 -0400145
146 io.post([this] { enumerateInterfaces(); });
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100147}
148
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000149NetworkMonitorImplOsx::~NetworkMonitorImplOsx()
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100150{
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500151 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode);
152
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100153 CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(),
154 static_cast<void*>(this));
155}
156
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000157shared_ptr<const NetworkInterface>
158NetworkMonitorImplOsx::getNetworkInterface(const std::string& ifname) const
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400159{
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500160 auto it = m_interfaces.find(ifname);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000161 return it == m_interfaces.end() ? nullptr : it->second;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400162}
163
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000164std::vector<shared_ptr<const NetworkInterface>>
165NetworkMonitorImplOsx::listNetworkInterfaces() const
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400166{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000167 std::vector<shared_ptr<const NetworkInterface>> v;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500168 v.reserve(m_interfaces.size());
169
170 for (const auto& e : m_interfaces) {
171 v.push_back(e.second);
172 }
173 return v;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400174}
175
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100176void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000177NetworkMonitorImplOsx::afterNotificationCenterEvent(CFNotificationCenterRef center,
178 void* observer,
179 CFStringRef name,
180 const void* object,
181 CFDictionaryRef userInfo)
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100182{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000183 static_cast<NetworkMonitorImplOsx*>(observer)->emitSignal(onNetworkStateChanged);
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100184}
185
186void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000187NetworkMonitorImplOsx::scheduleCfLoop()
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100188{
189 // poll each second for new events
Davide Pesavento0f830802018-01-16 23:58:58 -0500190 m_cfLoopEvent = m_scheduler.scheduleEvent(1_s, [this] {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400191 // this should dispatch ready events and exit
192 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
193 scheduleCfLoop();
194 });
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500195}
196
197void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000198NetworkMonitorImplOsx::enumerateInterfaces()
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500199{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400200 IfAddrs ifaList;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500201 for (const auto& ifName : getInterfaceNames()) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400202 addNewInterface(ifName, ifaList);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500203 }
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000204 this->emitSignal(onEnumerationCompleted);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500205}
206
207static std::string
Davide Pesaventof35c4272017-07-14 11:13:34 -0400208convertToStdString(CFStringRef cfStr)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500209{
Davide Pesaventof35c4272017-07-14 11:13:34 -0400210 const char* cStr = CFStringGetCStringPtr(cfStr, kCFStringEncodingASCII);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500211 if (cStr != nullptr) {
Davide Pesaventof35c4272017-07-14 11:13:34 -0400212 // fast path
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500213 return cStr;
214 }
215
Davide Pesaventof35c4272017-07-14 11:13:34 -0400216 // reserve space for the string + null terminator
217 std::string str(CFStringGetLength(cfStr) + 1, '\0');
218 if (!CFStringGetCString(cfStr, &str.front(), str.size(), kCFStringEncodingASCII)) {
219 BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error("CFString conversion failed"));
220 }
221
222 // drop the null terminator, std::string doesn't need it
223 str.pop_back();
224 return str;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500225}
226
227std::set<std::string>
Davide Pesaventoc0087982017-07-16 23:32:34 -0400228NetworkMonitorImplOsx::getInterfaceNames() const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500229{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000230 CFReleaser<CFDictionaryRef> dict =
231 (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface"));
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400232 if (dict.get() == nullptr) {
233 return {};
234 }
235
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500236 CFArrayRef interfaces = (CFArrayRef)CFDictionaryGetValue(dict.get(), CFSTR("Interfaces"));
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400237 if (interfaces == nullptr) {
238 return {};
239 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500240
241 std::set<std::string> ifNames;
242 size_t count = CFArrayGetCount(interfaces);
243 for (size_t i = 0; i != count; ++i) {
244 auto ifName = (CFStringRef)CFArrayGetValueAtIndex(interfaces, i);
245 ifNames.insert(convertToStdString(ifName));
246 }
247 return ifNames;
248}
249
Davide Pesaventoc0087982017-07-16 23:32:34 -0400250void
251NetworkMonitorImplOsx::addNewInterface(const std::string& ifName, const IfAddrs& ifaList)
252{
253 shared_ptr<NetworkInterface> interface = makeNetworkInterface();
254 interface->setName(ifName);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400255 interface->setState(getInterfaceState(*interface));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400256 updateInterfaceInfo(*interface, ifaList);
257
258 if (interface->getType() == InterfaceType::UNKNOWN) {
259 NDN_LOG_DEBUG("ignoring " << ifName << " due to unhandled interface type");
260 return;
261 }
262
263 NDN_LOG_DEBUG("adding interface " << interface->getName());
264 m_interfaces[interface->getName()] = interface;
265 this->emitSignal(onInterfaceAdded, interface);
266}
267
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500268InterfaceState
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400269NetworkMonitorImplOsx::getInterfaceState(const NetworkInterface& netif) const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500270{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000271 CFReleaser<CFStringRef> linkName =
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400272 CFStringCreateWithCString(kCFAllocatorDefault,
273 ("State:/Network/Interface/" + netif.getName() + "/Link").data(),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000274 kCFStringEncodingASCII);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500275
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400276 CFReleaser<CFDictionaryRef> dict =
277 (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), linkName.get());
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500278 if (dict.get() == nullptr) {
279 return InterfaceState::UNKNOWN;
280 }
281
282 CFBooleanRef isActive = (CFBooleanRef)CFDictionaryGetValue(dict.get(), CFSTR("Active"));
283 if (isActive == nullptr) {
284 return InterfaceState::UNKNOWN;
285 }
286
287 return CFBooleanGetValue(isActive) ? InterfaceState::RUNNING : InterfaceState::DOWN;
288}
289
Davide Pesaventoc0087982017-07-16 23:32:34 -0400290size_t
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400291NetworkMonitorImplOsx::getInterfaceMtu(const NetworkInterface& netif)
Davide Pesaventoc0087982017-07-16 23:32:34 -0400292{
293 ifreq ifr{};
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400294 std::strncpy(ifr.ifr_name, netif.getName().data(), sizeof(ifr.ifr_name) - 1);
Davide Pesaventoc0087982017-07-16 23:32:34 -0400295
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400296 if (::ioctl(m_ioctlSocket.native_handle(), SIOCGIFMTU, &ifr) == 0) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400297 return static_cast<size_t>(ifr.ifr_mtu);
298 }
299
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400300 NDN_LOG_WARN("failed to get MTU of " << netif.getName() << ": " << std::strerror(errno));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400301 return ethernet::MAX_DATA_LEN;
302}
303
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000304template<typename AddressBytes>
305static uint8_t
306computePrefixLength(const AddressBytes& mask)
307{
308 uint8_t prefixLength = 0;
309 for (auto byte : mask) {
310 while (byte != 0) {
311 ++prefixLength;
312 byte <<= 1;
313 }
314 }
315 return prefixLength;
316}
317
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500318void
Davide Pesaventoc0087982017-07-16 23:32:34 -0400319NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif, const IfAddrs& ifaList)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500320{
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400321 BOOST_ASSERT(!netif.getName().empty());
322
323 netif.setMtu(getInterfaceMtu(netif));
324
Davide Pesaventoc0087982017-07-16 23:32:34 -0400325 for (ifaddrs* ifa = ifaList.get(); ifa != nullptr; ifa = ifa->ifa_next) {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400326 if (ifa->ifa_name != netif.getName())
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500327 continue;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500328
329 netif.setFlags(ifa->ifa_flags);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500330
331 if (ifa->ifa_addr == nullptr)
332 continue;
333
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000334 namespace ip = boost::asio::ip;
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400335 AddressFamily addrFamily = AddressFamily::UNSPECIFIED;
336 ip::address ipAddr, broadcastAddr;
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000337 uint8_t prefixLength = 0;
338
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500339 switch (ifa->ifa_addr->sa_family) {
340 case AF_INET: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400341 addrFamily = AddressFamily::V4;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500342
343 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000344 ip::address_v4::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500345 std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000346 ipAddr = ip::address_v4(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500347
348 const sockaddr_in* sinMask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask);
349 std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000350 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500351 break;
352 }
353
354 case AF_INET6: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400355 addrFamily = AddressFamily::V6;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500356
357 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000358 ip::address_v6::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500359 std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin());
Davide Pesavento25203712017-10-09 23:50:03 -0400360 ip::address_v6 v6Addr(bytes);
361 if (v6Addr.is_link_local())
362 v6Addr.scope_id(if_nametoindex(netif.getName().data()));
363 ipAddr = v6Addr;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500364
365 const sockaddr_in6* sinMask = reinterpret_cast<sockaddr_in6*>(ifa->ifa_netmask);
366 std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin6_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000367 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500368 break;
369 }
370
371 case AF_LINK: {
372 const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
373 netif.setIndex(sdl->sdl_index);
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400374
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500375 if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) {
376 netif.setType(InterfaceType::ETHERNET);
377 netif.setEthernetAddress(ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl))));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400378 NDN_LOG_TRACE(netif.getName() << " has Ethernet address " << netif.getEthernetAddress());
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500379 }
380 else if (sdl->sdl_type == IFT_LOOP) {
381 netif.setType(InterfaceType::LOOPBACK);
382 }
383 else {
384 netif.setType(InterfaceType::UNKNOWN);
385 }
386 break;
387 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500388 }
389
390 if (netif.canBroadcast()) {
391 netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress());
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400392
393 if (addrFamily == AddressFamily::V4 && ifa->ifa_broadaddr != nullptr) {
394 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
395 ip::address_v4::bytes_type bytes;
396 std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
397 broadcastAddr = ip::address_v4(bytes);
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400398 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500399 }
400
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400401 if (addrFamily == AddressFamily::UNSPECIFIED)
402 continue;
403
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400404 AddressScope scope = AddressScope::GLOBAL;
405 if (ipAddr.is_loopback()) {
406 scope = AddressScope::HOST;
407 }
408 else if ((ipAddr.is_v4() && (ipAddr.to_v4().to_ulong() & 0xFFFF0000) == 0xA9FE0000) ||
409 (ipAddr.is_v6() && ipAddr.to_v6().is_link_local())) {
410 scope = AddressScope::LINK;
411 }
412
413 netif.addNetworkAddress(NetworkAddress(addrFamily, ipAddr, broadcastAddr, prefixLength, scope, 0));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500414 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500415}
416
417void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000418NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500419{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000420 static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500421}
422
423void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000424NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500425{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400426 IfAddrs ifaList;
427
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500428 size_t count = CFArrayGetCount(changedKeys);
429 for (size_t i = 0; i != count; ++i) {
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400430 Name key(convertToStdString((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i)));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500431 std::string ifName = key.at(-2).toUri();
432
433 auto ifIt = m_interfaces.find(ifName);
434 if (ifIt == m_interfaces.end()) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400435 addNewInterface(ifName, ifaList);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500436 return;
437 }
438
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500439 auto removeInterface = [&] {
440 NDN_LOG_DEBUG("removing interface " << ifName);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400441 shared_ptr<NetworkInterface> removedNetif = ifIt->second;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500442 m_interfaces.erase(ifIt);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400443 this->emitSignal(onInterfaceRemoved, removedNetif);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500444 };
445
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400446 NetworkInterface& netif = *ifIt->second;
447 std::string changedItem = key.at(-1).toUri();
448 if (changedItem == "Link") {
449 auto newState = getInterfaceState(netif);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500450 if (newState == InterfaceState::UNKNOWN) {
451 // check if it is really unknown or interface removed
452 if (getInterfaceNames().count(ifName) == 0) {
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500453 removeInterface();
454 return;
455 }
456 }
Davide Pesaventoc0087982017-07-16 23:32:34 -0400457 NDN_LOG_TRACE(ifName << " status changed from " << netif.getState() << " to " << newState);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500458 netif.setState(newState);
459 }
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400460 else if (changedItem == "IPv4" || changedItem == "IPv6") {
461 auto updatedNetif = makeNetworkInterface();
462 updatedNetif->setName(ifName);
463 updateInterfaceInfo(*updatedNetif, ifaList);
464 if (updatedNetif->getType() == InterfaceType::UNKNOWN) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400465 NDN_LOG_DEBUG(ifName << " type changed to unknown");
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500466 removeInterface();
467 return;
468 }
469
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400470 const auto& newAddrs = updatedNetif->getNetworkAddresses();
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500471 const auto& oldAddrs = netif.getNetworkAddresses();
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500472 std::set<NetworkAddress> added;
473 std::set<NetworkAddress> removed;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500474 std::set_difference(newAddrs.begin(), newAddrs.end(),
475 oldAddrs.begin(), oldAddrs.end(), std::inserter(added, added.end()));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500476 std::set_difference(oldAddrs.begin(), oldAddrs.end(),
477 newAddrs.begin(), newAddrs.end(), std::inserter(removed, removed.end()));
478
479 for (const auto& addr : removed) {
480 netif.removeNetworkAddress(addr);
481 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500482 for (const auto& addr : added) {
483 netif.addNetworkAddress(addr);
484 }
485 }
486 }
487}
488
Junxiao Shi25467942017-06-30 02:53:14 +0000489} // namespace net
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100490} // namespace ndn