blob: 8de354ca8b62e1b102c96539813d7c0d26625336 [file] [log] [blame]
Davide Pesavento9a8bae52016-02-24 20:33:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
Junxiao Shi25467942017-06-30 02:53:14 +000022#ifndef NDN_NET_NETWORK_MONITOR_IMPL_OSX_HPP
23#define NDN_NET_NETWORK_MONITOR_IMPL_OSX_HPP
Davide Pesavento9a8bae52016-02-24 20:33:08 +010024
Davide Pesavento2bf35a62017-04-02 00:41:06 -040025#include "ndn-cxx-config.hpp"
Davide Pesavento9a8bae52016-02-24 20:33:08 +010026#include "../network-monitor.hpp"
27
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050028#ifndef NDN_CXX_HAVE_OSX_FRAMEWORKS
Davide Pesavento2bf35a62017-04-02 00:41:06 -040029#error "This file should not be compiled ..."
30#endif
31
Junxiao Shi0b1b4672017-07-02 22:02:31 -070032#include "../../util/cf-releaser-osx.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000033#include "../../util/scheduler.hpp"
34#include "../../util/scheduler-scoped-event-id.hpp"
Davide Pesavento9a8bae52016-02-24 20:33:08 +010035
36#include <CoreFoundation/CoreFoundation.h>
37#include <SystemConfiguration/SystemConfiguration.h>
38
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050039#include <boost/asio/ip/udp.hpp>
40
Davide Pesavento9a8bae52016-02-24 20:33:08 +010041namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000042namespace net {
Davide Pesavento9a8bae52016-02-24 20:33:08 +010043
Junxiao Shi2dc416d2017-07-03 04:46:16 +000044class NetworkMonitorImplOsx : public NetworkMonitorImpl
Davide Pesavento9a8bae52016-02-24 20:33:08 +010045{
46public:
Junxiao Shi2dc416d2017-07-03 04:46:16 +000047 using Error = NetworkMonitor::Error;
Davide Pesavento9a8bae52016-02-24 20:33:08 +010048
Junxiao Shi2dc416d2017-07-03 04:46:16 +000049 NetworkMonitorImplOsx(boost::asio::io_service& io);
50
51 ~NetworkMonitorImplOsx();
Davide Pesavento9a8bae52016-02-24 20:33:08 +010052
Junxiao Shif4c1eb32017-05-30 17:05:10 +000053 uint32_t
Junxiao Shi2dc416d2017-07-03 04:46:16 +000054 getCapabilities() const final
Junxiao Shif4c1eb32017-05-30 17:05:10 +000055 {
Junxiao Shi2dc416d2017-07-03 04:46:16 +000056 return NetworkMonitor::CAP_ENUM |
57 NetworkMonitor::CAP_IF_ADD_REMOVE |
58 NetworkMonitor::CAP_STATE_CHANGE |
59 NetworkMonitor::CAP_ADDR_ADD_REMOVE;
Junxiao Shif4c1eb32017-05-30 17:05:10 +000060 }
61
Junxiao Shi2dc416d2017-07-03 04:46:16 +000062 shared_ptr<const NetworkInterface>
63 getNetworkInterface(const std::string& ifname) const final;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040064
Junxiao Shi2dc416d2017-07-03 04:46:16 +000065 std::vector<shared_ptr<const NetworkInterface>>
66 listNetworkInterfaces() const final;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040067
Davide Pesavento9a8bae52016-02-24 20:33:08 +010068 static void
69 afterNotificationCenterEvent(CFNotificationCenterRef center,
Davide Pesavento2bf35a62017-04-02 00:41:06 -040070 void* observer,
Davide Pesavento9a8bae52016-02-24 20:33:08 +010071 CFStringRef name,
Davide Pesavento2bf35a62017-04-02 00:41:06 -040072 const void* object,
Davide Pesavento9a8bae52016-02-24 20:33:08 +010073 CFDictionaryRef userInfo);
74
75private:
76 void
77 scheduleCfLoop();
78
79 void
80 pollCfLoop();
81
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050082 void
83 addNewInterface(const std::string& ifName);
84
85 void
86 enumerateInterfaces();
87
88 std::set<std::string>
89 getInterfaceNames();
90
91 InterfaceState
92 getInterfaceState(const std::string& ifName);
93
94 void
95 updateInterfaceInfo(NetworkInterface& netif);
96
97 size_t
98 getInterfaceMtu(const std::string& ifName);
99
100 static void
101 onConfigChanged(SCDynamicStoreRef store, CFArrayRef changedKeys, void* context);
102
103 void
104 onConfigChanged(CFArrayRef changedKeys);
105
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100106private:
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000107 std::map<std::string, shared_ptr<NetworkInterface>> m_interfaces; ///< ifname => interface
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100108
Junxiao Shi25467942017-06-30 02:53:14 +0000109 util::Scheduler m_scheduler;
110 util::scheduler::ScopedEventId m_cfLoopEvent;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500111
112 SCDynamicStoreContext m_context;
Junxiao Shi0b1b4672017-07-02 22:02:31 -0700113 util::CFReleaser<SCDynamicStoreRef> m_scStore;
114 util::CFReleaser<CFRunLoopSourceRef> m_loopSource;
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -0500115
116 boost::asio::ip::udp::socket m_nullUdpSocket;
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100117};
118
Junxiao Shi25467942017-06-30 02:53:14 +0000119} // namespace net
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100120} // namespace ndn
121
Junxiao Shi25467942017-06-30 02:53:14 +0000122#endif // NDN_NET_NETWORK_MONITOR_IMPL_OSX_HPP