blob: 34d53edf957760d006b38b3dedc4a9c665fd6ca2 [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 Pesavento2bf35a62017-04-02 00:41:06 -04003 * Copyright (c) 2013-2017 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
53#include "ndn-cxx-config.hpp"
54
Davide Pesavento9a8bae52016-02-24 20:33:08 +010055#include "network-monitor-impl-osx.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#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 Pesaventof35c4272017-07-14 11:13:34 -040066#include <boost/asio/io_service.hpp>
67#include <boost/asio/ip/address.hpp>
68#include <boost/asio/ip/udp.hpp>
Davide Pesavento9a8bae52016-02-24 20:33:08 +010069
70namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000071namespace net {
Davide Pesavento9a8bae52016-02-24 20:33:08 +010072
Junxiao Shi0b1b4672017-07-02 22:02:31 -070073using util::CFReleaser;
74
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050075NDN_LOG_INIT(ndn.NetworkMonitor);
76
Davide Pesaventoc0087982017-07-16 23:32:34 -040077class IfAddrs : noncopyable
78{
79public:
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
101private:
102 ifaddrs* m_ifaList = nullptr;
103};
104
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000105NetworkMonitorImplOsx::NetworkMonitorImplOsx(boost::asio::io_service& io)
106 : m_scheduler(io)
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100107 , m_cfLoopEvent(m_scheduler)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500108 , m_context{0, this, nullptr, nullptr, nullptr}
109 , m_scStore(SCDynamicStoreCreate(nullptr, CFSTR("net.named-data.ndn-cxx.NetworkMonitor"),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000110 &NetworkMonitorImplOsx::onConfigChanged, &m_context))
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500111 , m_loopSource(SCDynamicStoreCreateRunLoopSource(nullptr, m_scStore.get(), 0))
112 , m_nullUdpSocket(io, boost::asio::ip::udp::v4())
113
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100114{
115 scheduleCfLoop();
116
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500117 // Notifications from Darwin Notify Center:
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100118 //
119 // com.apple.system.config.network_change
120 //
121 CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
122 static_cast<void*>(this),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000123 &NetworkMonitorImplOsx::afterNotificationCenterEvent,
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100124 CFSTR("com.apple.system.config.network_change"),
125 nullptr, // object to observe
126 CFNotificationSuspensionBehaviorDeliverImmediately);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500127
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500128 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 Pesaventoc0087982017-07-16 23:32:34 -0400146
147 io.post([this] { enumerateInterfaces(); });
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100148}
149
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000150NetworkMonitorImplOsx::~NetworkMonitorImplOsx()
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100151{
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500152 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_loopSource.get(), kCFRunLoopDefaultMode);
153
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100154 CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(),
155 static_cast<void*>(this));
156}
157
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000158shared_ptr<const NetworkInterface>
159NetworkMonitorImplOsx::getNetworkInterface(const std::string& ifname) const
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400160{
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500161 auto it = m_interfaces.find(ifname);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000162 return it == m_interfaces.end() ? nullptr : it->second;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400163}
164
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000165std::vector<shared_ptr<const NetworkInterface>>
166NetworkMonitorImplOsx::listNetworkInterfaces() const
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400167{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000168 std::vector<shared_ptr<const NetworkInterface>> v;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500169 v.reserve(m_interfaces.size());
170
171 for (const auto& e : m_interfaces) {
172 v.push_back(e.second);
173 }
174 return v;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400175}
176
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100177void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000178NetworkMonitorImplOsx::afterNotificationCenterEvent(CFNotificationCenterRef center,
179 void* observer,
180 CFStringRef name,
181 const void* object,
182 CFDictionaryRef userInfo)
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100183{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000184 static_cast<NetworkMonitorImplOsx*>(observer)->emitSignal(onNetworkStateChanged);
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100185}
186
187void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000188NetworkMonitorImplOsx::scheduleCfLoop()
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100189{
190 // poll each second for new events
Davide Pesaventoc0087982017-07-16 23:32:34 -0400191 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 Afanasyev3b3355c2017-03-26 11:57:13 -0500196}
197
198void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000199NetworkMonitorImplOsx::enumerateInterfaces()
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500200{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400201 IfAddrs ifaList;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500202 for (const auto& ifName : getInterfaceNames()) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400203 addNewInterface(ifName, ifaList);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500204 }
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000205 this->emitSignal(onEnumerationCompleted);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500206}
207
208static std::string
Davide Pesaventof35c4272017-07-14 11:13:34 -0400209convertToStdString(CFStringRef cfStr)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500210{
Davide Pesaventof35c4272017-07-14 11:13:34 -0400211 const char* cStr = CFStringGetCStringPtr(cfStr, kCFStringEncodingASCII);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500212 if (cStr != nullptr) {
Davide Pesaventof35c4272017-07-14 11:13:34 -0400213 // fast path
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500214 return cStr;
215 }
216
Davide Pesaventof35c4272017-07-14 11:13:34 -0400217 // 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 Afanasyev3b3355c2017-03-26 11:57:13 -0500226}
227
228std::set<std::string>
Davide Pesaventoc0087982017-07-16 23:32:34 -0400229NetworkMonitorImplOsx::getInterfaceNames() const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500230{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000231 CFReleaser<CFDictionaryRef> dict =
232 (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface"));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500233 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 Pesaventoc0087982017-07-16 23:32:34 -0400244void
245NetworkMonitorImplOsx::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 Afanasyev3b3355c2017-03-26 11:57:13 -0500262InterfaceState
Davide Pesaventoc0087982017-07-16 23:32:34 -0400263NetworkMonitorImplOsx::getInterfaceState(const std::string& ifName) const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500264{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000265 CFReleaser<CFStringRef> linkName =
Davide Pesaventoc0087982017-07-16 23:32:34 -0400266 CFStringCreateWithCString(nullptr, ("State:/Network/Interface/" + ifName + "/Link").data(),
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000267 kCFStringEncodingASCII);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500268
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 Pesaventoc0087982017-07-16 23:32:34 -0400282size_t
283NetworkMonitorImplOsx::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 Shi2dc416d2017-07-03 04:46:16 +0000296template<typename AddressBytes>
297static uint8_t
298computePrefixLength(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 Afanasyev3b3355c2017-03-26 11:57:13 -0500310void
Davide Pesaventoc0087982017-07-16 23:32:34 -0400311NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif, const IfAddrs& ifaList)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500312{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400313 for (ifaddrs* ifa = ifaList.get(); ifa != nullptr; ifa = ifa->ifa_next) {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400314 if (ifa->ifa_name != netif.getName())
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500315 continue;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500316
317 netif.setFlags(ifa->ifa_flags);
318 netif.setMtu(getInterfaceMtu(netif.getName()));
319
320 if (ifa->ifa_addr == nullptr)
321 continue;
322
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000323 namespace ip = boost::asio::ip;
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400324 AddressFamily addrFamily = AddressFamily::UNSPECIFIED;
325 ip::address ipAddr, broadcastAddr;
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000326 uint8_t prefixLength = 0;
327
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500328 switch (ifa->ifa_addr->sa_family) {
329 case AF_INET: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400330 addrFamily = AddressFamily::V4;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500331
332 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000333 ip::address_v4::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500334 std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000335 ipAddr = ip::address_v4(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500336
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 Shi2dc416d2017-07-03 04:46:16 +0000339 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500340 break;
341 }
342
343 case AF_INET6: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400344 addrFamily = AddressFamily::V6;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500345
346 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000347 ip::address_v6::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500348 std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000349 ipAddr = ip::address_v6(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500350
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 Shi2dc416d2017-07-03 04:46:16 +0000353 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500354 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 Pesaventob2ae3362017-07-13 01:43:14 -0400360
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500361 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 Pesaventoc0087982017-07-16 23:32:34 -0400364 NDN_LOG_TRACE(netif.getName() << " has Ethernet address " << netif.getEthernetAddress());
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500365 }
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 Afanasyev3b3355c2017-03-26 11:57:13 -0500374 }
375
376 if (netif.canBroadcast()) {
377 netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress());
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400378
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 Pesaventob2ae3362017-07-13 01:43:14 -0400384 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500385 }
386
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400387 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 Afanasyev3b3355c2017-03-26 11:57:13 -0500397 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500398}
399
400void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000401NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500402{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000403 static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500404}
405
406void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000407NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500408{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400409 IfAddrs ifaList;
410
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500411 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 Pesaventoc0087982017-07-16 23:32:34 -0400419 addNewInterface(ifName, ifaList);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500420 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 Shi2dc416d2017-07-03 04:46:16 +0000429 this->emitSignal(onInterfaceRemoved, removedInterface);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500430 };
431
432 if (key.at(-1).toUri() == "Link") {
433 auto newState = getInterfaceState(ifName);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500434 if (newState == InterfaceState::UNKNOWN) {
435 // check if it is really unknown or interface removed
436 if (getInterfaceNames().count(ifName) == 0) {
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500437 removeInterface();
438 return;
439 }
440 }
Davide Pesaventoc0087982017-07-16 23:32:34 -0400441 NDN_LOG_TRACE(ifName << " status changed from " << netif.getState() << " to " << newState);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500442 netif.setState(newState);
443 }
444
445 if (key.at(-1).toUri() == "IPv4" || key.at(-1).toUri() == "IPv6") {
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000446 shared_ptr<NetworkInterface> updatedInterface = makeNetworkInterface();
447 updatedInterface->setName(ifName);
Davide Pesaventoc0087982017-07-16 23:32:34 -0400448 updateInterfaceInfo(*updatedInterface, ifaList);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000449 if (updatedInterface->getType() == InterfaceType::UNKNOWN) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400450 NDN_LOG_DEBUG(ifName << " type changed to unknown");
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500451 removeInterface();
452 return;
453 }
454
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000455 const auto& newAddrs = updatedInterface->getNetworkAddresses();
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500456 const auto& oldAddrs = netif.getNetworkAddresses();
457
458 std::set<NetworkAddress> added;
459 std::set<NetworkAddress> removed;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500460 std::set_difference(newAddrs.begin(), newAddrs.end(),
461 oldAddrs.begin(), oldAddrs.end(), std::inserter(added, added.end()));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500462 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 Afanasyev3b3355c2017-03-26 11:57:13 -0500468 for (const auto& addr : added) {
469 netif.addNetworkAddress(addr);
470 }
471 }
472 }
473}
474
Junxiao Shi25467942017-06-30 02:53:14 +0000475} // namespace net
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100476} // namespace ndn