blob: e6874a1985c23e79f615488cb3220f636a754f6c [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- 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.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08004 *
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.
Davide Pesavento2bf35a62017-04-02 00:41:06 -040020 *
21 * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu>
22 * @author Davide Pesavento <davide.pesavento@lip6.fr>
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080023 */
24
Junxiao Shi25467942017-06-30 02:53:14 +000025#ifndef NDN_NET_NETWORK_MONITOR_HPP
26#define NDN_NET_NETWORK_MONITOR_HPP
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080027
Junxiao Shi2dc416d2017-07-03 04:46:16 +000028#include "network-interface.hpp"
Davide Pesavento2bf35a62017-04-02 00:41:06 -040029#include <vector>
30
Davide Pesavento537dc3a2016-02-18 19:35:26 +010031namespace boost {
32namespace asio {
33class io_service;
34} // namespace asio
35} // namespace boost
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080036
37namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000038namespace net {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080039
Junxiao Shi2dc416d2017-07-03 04:46:16 +000040class NetworkMonitorImpl;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040041
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080042/**
Davide Pesavento43462902017-07-05 02:06:07 -040043 * @brief Network interface monitor
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080044 *
Davide Pesavento2bf35a62017-04-02 00:41:06 -040045 * Maintains an up-to-date view of every system network interface and notifies when an interface
46 * is added or removed.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080047 *
Davide Pesavento43462902017-07-05 02:06:07 -040048 * @note The implementation of this class is highly platform dependent, and not all platform
49 * backends provide all the features. On macOS, @e SystemConfiguration and
50 * @e CFNotificationCenterAddObserver are used (notification of MTU change is not supported).
51 * On Linux, @e rtnetlink notifications from the kernel are used. See getCapabilities() for
52 * the detailed set of capabilities supported by the platform backend currently in use.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080053 */
Davide Pesavento2bf35a62017-04-02 00:41:06 -040054class NetworkMonitor : noncopyable
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080055{
56public:
57 class Error : public std::runtime_error
58 {
59 public:
60 explicit
61 Error(const std::string& what)
62 : std::runtime_error(what)
63 {
64 }
65 };
66
67 /**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040068 * @brief Construct instance, request enumeration of all network interfaces, and start
69 * monitoring for network state changes
70 *
Davide Pesavento43462902017-07-05 02:06:07 -040071 * @param io io_service instance that will dispatch events
Junxiao Shi2dc416d2017-07-03 04:46:16 +000072 * @throw Error error starting monitoring
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080073 */
74 explicit
75 NetworkMonitor(boost::asio::io_service& io);
76
Junxiao Shif4c1eb32017-05-30 17:05:10 +000077 enum Capability : uint32_t {
78 /// NetworkMonitor is not supported and is a no-op
79 CAP_NONE = 0,
80 /// listNetworkInterfaces() and getNetworkInterface() are supported
81 CAP_ENUM = 1 << 0,
82 /// NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported
83 CAP_IF_ADD_REMOVE = 1 << 1,
84 /// NetworkInterface onStateChanged signal is supported
85 CAP_STATE_CHANGE = 1 << 2,
86 /// NetworkInterface onMtuChanged signal is supported
87 CAP_MTU_CHANGE = 1 << 3,
88 /// NetworkInterface onAddressAdded and onAddressRemoved signals are supported
89 CAP_ADDR_ADD_REMOVE = 1 << 4
90 };
91
Davide Pesavento43462902017-07-05 02:06:07 -040092 /// Returns a bitwise OR'ed set of @ref Capability flags supported on the current platform
Junxiao Shif4c1eb32017-05-30 17:05:10 +000093 uint32_t
94 getCapabilities() const;
95
Davide Pesavento43462902017-07-05 02:06:07 -040096 /// Returns the NetworkInterface with the given name, or @c nullptr if it does not exist
Junxiao Shi2dc416d2017-07-03 04:46:16 +000097 shared_ptr<const NetworkInterface>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040098 getNetworkInterface(const std::string& ifname) const;
99
Davide Pesavento43462902017-07-05 02:06:07 -0400100 /**
101 * @brief Lists all network interfaces currently available on the system
102 * @warning May return incomplete results if called before the
103 * #onEnumerationCompleted signal has been emitted.
104 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000105 std::vector<shared_ptr<const NetworkInterface>>
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400106 listNetworkInterfaces() const;
107
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000108protected:
109 explicit
110 NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl);
111
112 NetworkMonitorImpl&
113 getImpl()
114 {
115 return *m_impl;
116 }
117
118private:
119 const unique_ptr<NetworkMonitorImpl> m_impl;
120 // Intentional violation of code-style rule 1.4: m_impl must be assigned before its signals can
121 // be assigned to references below.
122
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400123public: // signals
Davide Pesavento43462902017-07-05 02:06:07 -0400124 /// Fires when network interfaces enumeration is complete
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000125 util::Signal<NetworkMonitorImpl>& onEnumerationCompleted;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400126
Davide Pesavento43462902017-07-05 02:06:07 -0400127 /// Fires when a new interface is added
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000128 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceAdded;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400129
130 /**
131 * @brief Fires when an interface is removed
Davide Pesavento43462902017-07-05 02:06:07 -0400132 * @note The NetworkInterface object is no longer present in the internal list of
133 * network interfaces when this signal is emitted.
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400134 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000135 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceRemoved;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400136
Davide Pesavento43462902017-07-05 02:06:07 -0400137 /// @deprecated Only for backward compatibility
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000138 util::Signal<NetworkMonitorImpl>& onNetworkStateChanged;
139};
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800140
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000141class NetworkMonitorImpl : noncopyable
142{
143public:
144 virtual
145 ~NetworkMonitorImpl() = default;
146
147 virtual uint32_t
148 getCapabilities() const = 0;
149
150 virtual shared_ptr<const NetworkInterface>
151 getNetworkInterface(const std::string&) const = 0;
152
153 virtual std::vector<shared_ptr<const NetworkInterface>>
154 listNetworkInterfaces() const = 0;
155
156protected:
157 static shared_ptr<NetworkInterface>
158 makeNetworkInterface();
159
160public:
161 util::Signal<NetworkMonitorImpl> onEnumerationCompleted;
162 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceAdded;
163 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceRemoved;
164 util::Signal<NetworkMonitorImpl> onNetworkStateChanged;
165
166protected:
167 DECLARE_SIGNAL_EMIT(onEnumerationCompleted)
168 DECLARE_SIGNAL_EMIT(onInterfaceAdded)
169 DECLARE_SIGNAL_EMIT(onInterfaceRemoved)
170 DECLARE_SIGNAL_EMIT(onNetworkStateChanged)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800171};
172
Junxiao Shi25467942017-06-30 02:53:14 +0000173} // namespace net
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400174} // namespace ndn
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800175
Junxiao Shi25467942017-06-30 02:53:14 +0000176#endif // NDN_NET_NETWORK_MONITOR_HPP