blob: 507c3fdd021d30fa6a7998a6b78a7a92fa7bac6d [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"
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040057#include "../../util/cf-string-osx.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000058#include "../../util/logger.hpp"
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050059
Davide Pesavento25203712017-10-09 23:50:03 -040060#include <ifaddrs.h> // for getifaddrs()
61#include <net/if.h> // for if_nametoindex()
62#include <net/if_dl.h> // for struct sockaddr_dl
63#include <net/if_types.h> // for IFT_* constants
64#include <netinet/in.h> // for struct sockaddr_in{,6}
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050065
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
Davide Pesavento755f8a82018-08-24 00:06:45 -040070NDN_LOG_INIT(ndn.NetworkMonitor);
71
Davide Pesavento9a8bae52016-02-24 20:33:08 +010072namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000073namespace net {
Davide Pesavento9a8bae52016-02-24 20:33:08 +010074
Junxiao Shi0b1b4672017-07-02 22:02:31 -070075using util::CFReleaser;
76
Davide Pesaventoc0087982017-07-16 23:32:34 -040077class IfAddrs : noncopyable
78{
79public:
80 IfAddrs()
81 {
82 if (::getifaddrs(&m_ifaList) < 0) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040083 BOOST_THROW_EXCEPTION(NetworkMonitorImplOsx::Error("getifaddrs() failed: "s + strerror(errno)));
Davide Pesaventoc0087982017-07-16 23:32:34 -040084 }
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
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500207std::set<std::string>
Davide Pesaventoc0087982017-07-16 23:32:34 -0400208NetworkMonitorImplOsx::getInterfaceNames() const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500209{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000210 CFReleaser<CFDictionaryRef> dict =
211 (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), CFSTR("State:/Network/Interface"));
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400212 if (dict.get() == nullptr) {
213 return {};
214 }
215
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500216 CFArrayRef interfaces = (CFArrayRef)CFDictionaryGetValue(dict.get(), CFSTR("Interfaces"));
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400217 if (interfaces == nullptr) {
218 return {};
219 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500220
221 std::set<std::string> ifNames;
222 size_t count = CFArrayGetCount(interfaces);
223 for (size_t i = 0; i != count; ++i) {
224 auto ifName = (CFStringRef)CFArrayGetValueAtIndex(interfaces, i);
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400225 ifNames.insert(util::cfstring::toStdString(ifName));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500226 }
227 return ifNames;
228}
229
Davide Pesaventoc0087982017-07-16 23:32:34 -0400230void
231NetworkMonitorImplOsx::addNewInterface(const std::string& ifName, const IfAddrs& ifaList)
232{
233 shared_ptr<NetworkInterface> interface = makeNetworkInterface();
234 interface->setName(ifName);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400235 interface->setState(getInterfaceState(*interface));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400236 updateInterfaceInfo(*interface, ifaList);
237
238 if (interface->getType() == InterfaceType::UNKNOWN) {
239 NDN_LOG_DEBUG("ignoring " << ifName << " due to unhandled interface type");
240 return;
241 }
242
243 NDN_LOG_DEBUG("adding interface " << interface->getName());
244 m_interfaces[interface->getName()] = interface;
245 this->emitSignal(onInterfaceAdded, interface);
246}
247
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500248InterfaceState
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400249NetworkMonitorImplOsx::getInterfaceState(const NetworkInterface& netif) const
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500250{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000251 CFReleaser<CFStringRef> linkName =
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400252 util::cfstring::fromStdString("State:/Network/Interface/" + netif.getName() + "/Link");
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500253
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400254 CFReleaser<CFDictionaryRef> dict =
255 (CFDictionaryRef)SCDynamicStoreCopyValue(m_scStore.get(), linkName.get());
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500256 if (dict.get() == nullptr) {
257 return InterfaceState::UNKNOWN;
258 }
259
260 CFBooleanRef isActive = (CFBooleanRef)CFDictionaryGetValue(dict.get(), CFSTR("Active"));
261 if (isActive == nullptr) {
262 return InterfaceState::UNKNOWN;
263 }
264
265 return CFBooleanGetValue(isActive) ? InterfaceState::RUNNING : InterfaceState::DOWN;
266}
267
Davide Pesaventoc0087982017-07-16 23:32:34 -0400268size_t
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400269NetworkMonitorImplOsx::getInterfaceMtu(const NetworkInterface& netif)
Davide Pesaventoc0087982017-07-16 23:32:34 -0400270{
271 ifreq ifr{};
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400272 std::strncpy(ifr.ifr_name, netif.getName().data(), sizeof(ifr.ifr_name) - 1);
Davide Pesaventoc0087982017-07-16 23:32:34 -0400273
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400274 if (::ioctl(m_ioctlSocket.native_handle(), SIOCGIFMTU, &ifr) == 0) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400275 return static_cast<size_t>(ifr.ifr_mtu);
276 }
277
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400278 NDN_LOG_WARN("failed to get MTU of " << netif.getName() << ": " << std::strerror(errno));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400279 return ethernet::MAX_DATA_LEN;
280}
281
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000282template<typename AddressBytes>
283static uint8_t
284computePrefixLength(const AddressBytes& mask)
285{
286 uint8_t prefixLength = 0;
287 for (auto byte : mask) {
288 while (byte != 0) {
289 ++prefixLength;
290 byte <<= 1;
291 }
292 }
293 return prefixLength;
294}
295
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500296void
Davide Pesaventoc0087982017-07-16 23:32:34 -0400297NetworkMonitorImplOsx::updateInterfaceInfo(NetworkInterface& netif, const IfAddrs& ifaList)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500298{
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400299 BOOST_ASSERT(!netif.getName().empty());
300
301 netif.setMtu(getInterfaceMtu(netif));
302
Davide Pesaventoc0087982017-07-16 23:32:34 -0400303 for (ifaddrs* ifa = ifaList.get(); ifa != nullptr; ifa = ifa->ifa_next) {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400304 if (ifa->ifa_name != netif.getName())
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500305 continue;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500306
307 netif.setFlags(ifa->ifa_flags);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500308
309 if (ifa->ifa_addr == nullptr)
310 continue;
311
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000312 namespace ip = boost::asio::ip;
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400313 AddressFamily addrFamily = AddressFamily::UNSPECIFIED;
314 ip::address ipAddr, broadcastAddr;
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000315 uint8_t prefixLength = 0;
316
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500317 switch (ifa->ifa_addr->sa_family) {
318 case AF_INET: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400319 addrFamily = AddressFamily::V4;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500320
321 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000322 ip::address_v4::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500323 std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000324 ipAddr = ip::address_v4(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500325
326 const sockaddr_in* sinMask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask);
327 std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000328 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500329 break;
330 }
331
332 case AF_INET6: {
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400333 addrFamily = AddressFamily::V6;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500334
335 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000336 ip::address_v6::bytes_type bytes;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500337 std::copy_n(reinterpret_cast<const unsigned char*>(&sin6->sin6_addr), bytes.size(), bytes.begin());
Davide Pesavento25203712017-10-09 23:50:03 -0400338 ip::address_v6 v6Addr(bytes);
339 if (v6Addr.is_link_local())
340 v6Addr.scope_id(if_nametoindex(netif.getName().data()));
341 ipAddr = v6Addr;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500342
343 const sockaddr_in6* sinMask = reinterpret_cast<sockaddr_in6*>(ifa->ifa_netmask);
344 std::copy_n(reinterpret_cast<const unsigned char*>(&sinMask->sin6_addr), bytes.size(), bytes.begin());
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000345 prefixLength = computePrefixLength(bytes);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500346 break;
347 }
348
349 case AF_LINK: {
350 const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
351 netif.setIndex(sdl->sdl_index);
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400352
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500353 if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) {
354 netif.setType(InterfaceType::ETHERNET);
355 netif.setEthernetAddress(ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl))));
Davide Pesaventoc0087982017-07-16 23:32:34 -0400356 NDN_LOG_TRACE(netif.getName() << " has Ethernet address " << netif.getEthernetAddress());
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500357 }
358 else if (sdl->sdl_type == IFT_LOOP) {
359 netif.setType(InterfaceType::LOOPBACK);
360 }
361 else {
362 netif.setType(InterfaceType::UNKNOWN);
363 }
364 break;
365 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500366 }
367
368 if (netif.canBroadcast()) {
369 netif.setEthernetBroadcastAddress(ethernet::getBroadcastAddress());
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400370
371 if (addrFamily == AddressFamily::V4 && ifa->ifa_broadaddr != nullptr) {
372 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
373 ip::address_v4::bytes_type bytes;
374 std::copy_n(reinterpret_cast<const unsigned char*>(&sin->sin_addr), bytes.size(), bytes.begin());
375 broadcastAddr = ip::address_v4(bytes);
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400376 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500377 }
378
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400379 if (addrFamily == AddressFamily::UNSPECIFIED)
380 continue;
381
Davide Pesaventob2ae3362017-07-13 01:43:14 -0400382 AddressScope scope = AddressScope::GLOBAL;
383 if (ipAddr.is_loopback()) {
384 scope = AddressScope::HOST;
385 }
386 else if ((ipAddr.is_v4() && (ipAddr.to_v4().to_ulong() & 0xFFFF0000) == 0xA9FE0000) ||
387 (ipAddr.is_v6() && ipAddr.to_v6().is_link_local())) {
388 scope = AddressScope::LINK;
389 }
390
391 netif.addNetworkAddress(NetworkAddress(addrFamily, ipAddr, broadcastAddr, prefixLength, scope, 0));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500392 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500393}
394
395void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000396NetworkMonitorImplOsx::onConfigChanged(SCDynamicStoreRef m_scStore, CFArrayRef changedKeys, void* context)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500397{
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000398 static_cast<NetworkMonitorImplOsx*>(context)->onConfigChanged(changedKeys);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500399}
400
401void
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000402NetworkMonitorImplOsx::onConfigChanged(CFArrayRef changedKeys)
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500403{
Davide Pesaventoc0087982017-07-16 23:32:34 -0400404 IfAddrs ifaList;
405
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500406 size_t count = CFArrayGetCount(changedKeys);
407 for (size_t i = 0; i != count; ++i) {
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400408 Name key(util::cfstring::toStdString((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i)));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500409 std::string ifName = key.at(-2).toUri();
410
411 auto ifIt = m_interfaces.find(ifName);
412 if (ifIt == m_interfaces.end()) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400413 addNewInterface(ifName, ifaList);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500414 return;
415 }
416
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500417 auto removeInterface = [&] {
418 NDN_LOG_DEBUG("removing interface " << ifName);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400419 shared_ptr<NetworkInterface> removedNetif = ifIt->second;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500420 m_interfaces.erase(ifIt);
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400421 this->emitSignal(onInterfaceRemoved, removedNetif);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500422 };
423
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400424 NetworkInterface& netif = *ifIt->second;
425 std::string changedItem = key.at(-1).toUri();
426 if (changedItem == "Link") {
427 auto newState = getInterfaceState(netif);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500428 if (newState == InterfaceState::UNKNOWN) {
429 // check if it is really unknown or interface removed
430 if (getInterfaceNames().count(ifName) == 0) {
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500431 removeInterface();
432 return;
433 }
434 }
Davide Pesaventoc0087982017-07-16 23:32:34 -0400435 NDN_LOG_TRACE(ifName << " status changed from " << netif.getState() << " to " << newState);
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500436 netif.setState(newState);
437 }
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400438 else if (changedItem == "IPv4" || changedItem == "IPv6") {
439 auto updatedNetif = makeNetworkInterface();
440 updatedNetif->setName(ifName);
441 updateInterfaceInfo(*updatedNetif, ifaList);
442 if (updatedNetif->getType() == InterfaceType::UNKNOWN) {
Davide Pesaventoc0087982017-07-16 23:32:34 -0400443 NDN_LOG_DEBUG(ifName << " type changed to unknown");
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500444 removeInterface();
445 return;
446 }
447
Davide Pesaventof0c827e2017-10-03 17:25:00 -0400448 const auto& newAddrs = updatedNetif->getNetworkAddresses();
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500449 const auto& oldAddrs = netif.getNetworkAddresses();
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500450 std::set<NetworkAddress> added;
451 std::set<NetworkAddress> removed;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500452 std::set_difference(newAddrs.begin(), newAddrs.end(),
453 oldAddrs.begin(), oldAddrs.end(), std::inserter(added, added.end()));
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500454 std::set_difference(oldAddrs.begin(), oldAddrs.end(),
455 newAddrs.begin(), newAddrs.end(), std::inserter(removed, removed.end()));
456
457 for (const auto& addr : removed) {
458 netif.removeNetworkAddress(addr);
459 }
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500460 for (const auto& addr : added) {
461 netif.addNetworkAddress(addr);
462 }
463 }
464 }
465}
466
Junxiao Shi25467942017-06-30 02:53:14 +0000467} // namespace net
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100468} // namespace ndn