blob: 9b2d08da143c136abe9a1739cdc2aab076f9e62f [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4d0d0962017-12-19 22:23:14 -05002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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
Davide Pesavento4d0d0962017-12-19 22:23:14 -050028#include "asio-fwd.hpp"
Junxiao Shi2dc416d2017-07-03 04:46:16 +000029#include "network-interface.hpp"
Davide Pesavento2bf35a62017-04-02 00:41:06 -040030
Davide Pesavento4d0d0962017-12-19 22:23:14 -050031#include <vector>
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080032
33namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000034namespace net {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080035
Junxiao Shi2dc416d2017-07-03 04:46:16 +000036class NetworkMonitorImpl;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040037
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080038/**
Davide Pesavento43462902017-07-05 02:06:07 -040039 * @brief Network interface monitor
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080040 *
Davide Pesavento2bf35a62017-04-02 00:41:06 -040041 * Maintains an up-to-date view of every system network interface and notifies when an interface
42 * is added or removed.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080043 *
Davide Pesavento43462902017-07-05 02:06:07 -040044 * @note The implementation of this class is highly platform dependent, and not all platform
45 * backends provide all the features. On macOS, @e SystemConfiguration and
46 * @e CFNotificationCenterAddObserver are used (notification of MTU change is not supported).
47 * On Linux, @e rtnetlink notifications from the kernel are used. See getCapabilities() for
48 * the detailed set of capabilities supported by the platform backend currently in use.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080049 */
Davide Pesavento2bf35a62017-04-02 00:41:06 -040050class NetworkMonitor : noncopyable
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080051{
52public:
53 class Error : public std::runtime_error
54 {
55 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060056 using std::runtime_error::runtime_error;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080057 };
58
59 /**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040060 * @brief Construct instance, request enumeration of all network interfaces, and start
61 * monitoring for network state changes
62 *
Davide Pesavento43462902017-07-05 02:06:07 -040063 * @param io io_service instance that will dispatch events
Junxiao Shi2dc416d2017-07-03 04:46:16 +000064 * @throw Error error starting monitoring
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080065 */
66 explicit
67 NetworkMonitor(boost::asio::io_service& io);
68
Junxiao Shif4c1eb32017-05-30 17:05:10 +000069 enum Capability : uint32_t {
70 /// NetworkMonitor is not supported and is a no-op
71 CAP_NONE = 0,
72 /// listNetworkInterfaces() and getNetworkInterface() are supported
73 CAP_ENUM = 1 << 0,
74 /// NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported
75 CAP_IF_ADD_REMOVE = 1 << 1,
76 /// NetworkInterface onStateChanged signal is supported
77 CAP_STATE_CHANGE = 1 << 2,
78 /// NetworkInterface onMtuChanged signal is supported
79 CAP_MTU_CHANGE = 1 << 3,
80 /// NetworkInterface onAddressAdded and onAddressRemoved signals are supported
81 CAP_ADDR_ADD_REMOVE = 1 << 4
82 };
83
Davide Pesavento43462902017-07-05 02:06:07 -040084 /// Returns a bitwise OR'ed set of @ref Capability flags supported on the current platform
Junxiao Shif4c1eb32017-05-30 17:05:10 +000085 uint32_t
86 getCapabilities() const;
87
Davide Pesavento43462902017-07-05 02:06:07 -040088 /// Returns the NetworkInterface with the given name, or @c nullptr if it does not exist
Junxiao Shi2dc416d2017-07-03 04:46:16 +000089 shared_ptr<const NetworkInterface>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040090 getNetworkInterface(const std::string& ifname) const;
91
Davide Pesavento43462902017-07-05 02:06:07 -040092 /**
93 * @brief Lists all network interfaces currently available on the system
94 * @warning May return incomplete results if called before the
95 * #onEnumerationCompleted signal has been emitted.
96 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +000097 std::vector<shared_ptr<const NetworkInterface>>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040098 listNetworkInterfaces() const;
99
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000100protected:
101 explicit
102 NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl);
103
104 NetworkMonitorImpl&
105 getImpl()
106 {
107 return *m_impl;
108 }
109
110private:
111 const unique_ptr<NetworkMonitorImpl> m_impl;
112 // Intentional violation of code-style rule 1.4: m_impl must be assigned before its signals can
113 // be assigned to references below.
114
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400115public: // signals
Davide Pesavento43462902017-07-05 02:06:07 -0400116 /// Fires when network interfaces enumeration is complete
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000117 util::Signal<NetworkMonitorImpl>& onEnumerationCompleted;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400118
Davide Pesavento43462902017-07-05 02:06:07 -0400119 /// Fires when a new interface is added
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000120 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceAdded;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400121
122 /**
123 * @brief Fires when an interface is removed
Davide Pesavento43462902017-07-05 02:06:07 -0400124 * @note The NetworkInterface object is no longer present in the internal list of
125 * network interfaces when this signal is emitted.
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400126 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000127 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceRemoved;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400128
Davide Pesavento43462902017-07-05 02:06:07 -0400129 /// @deprecated Only for backward compatibility
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000130 util::Signal<NetworkMonitorImpl>& onNetworkStateChanged;
131};
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800132
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000133class NetworkMonitorImpl : noncopyable
134{
135public:
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -0400136 using Error = NetworkMonitor::Error;
137
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000138 virtual
139 ~NetworkMonitorImpl() = default;
140
141 virtual uint32_t
142 getCapabilities() const = 0;
143
144 virtual shared_ptr<const NetworkInterface>
145 getNetworkInterface(const std::string&) const = 0;
146
147 virtual std::vector<shared_ptr<const NetworkInterface>>
148 listNetworkInterfaces() const = 0;
149
150protected:
151 static shared_ptr<NetworkInterface>
152 makeNetworkInterface();
153
154public:
155 util::Signal<NetworkMonitorImpl> onEnumerationCompleted;
156 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceAdded;
157 util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceRemoved;
158 util::Signal<NetworkMonitorImpl> onNetworkStateChanged;
159
160protected:
161 DECLARE_SIGNAL_EMIT(onEnumerationCompleted)
162 DECLARE_SIGNAL_EMIT(onInterfaceAdded)
163 DECLARE_SIGNAL_EMIT(onInterfaceRemoved)
164 DECLARE_SIGNAL_EMIT(onNetworkStateChanged)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800165};
166
Junxiao Shi25467942017-06-30 02:53:14 +0000167} // namespace net
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400168} // namespace ndn
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800169
Junxiao Shi25467942017-06-30 02:53:14 +0000170#endif // NDN_NET_NETWORK_MONITOR_HPP