Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 4 | * |
| 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 Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 22 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 25 | #ifndef NDN_NET_NETWORK_MONITOR_HPP |
| 26 | #define NDN_NET_NETWORK_MONITOR_HPP |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 28 | #include "asio-fwd.hpp" |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 29 | #include "network-interface.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 30 | |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 31 | #include <vector> |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 34 | namespace net { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 35 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 36 | class NetworkMonitorImpl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 37 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 38 | /** |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 39 | * @brief Network interface monitor. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 40 | * |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 41 | * Maintains an up-to-date view of every system network interface and notifies when an interface |
| 42 | * is added or removed. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 43 | * |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 44 | * @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). |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 47 | * On Linux, @e netlink notifications from the kernel are used. See getCapabilities() for |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 48 | * the detailed set of capabilities supported by the platform backend currently in use. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 49 | */ |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 50 | class NetworkMonitor : noncopyable |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 51 | { |
| 52 | public: |
| 53 | class Error : public std::runtime_error |
| 54 | { |
| 55 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 56 | using std::runtime_error::runtime_error; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 60 | * @brief Construct instance, request enumeration of all network interfaces, and start |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 61 | * monitoring for network state changes. |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 62 | * @param io io_service instance that will dispatch events |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 63 | */ |
| 64 | explicit |
| 65 | NetworkMonitor(boost::asio::io_service& io); |
| 66 | |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 67 | enum Capability : uint32_t { |
| 68 | /// NetworkMonitor is not supported and is a no-op |
| 69 | CAP_NONE = 0, |
| 70 | /// listNetworkInterfaces() and getNetworkInterface() are supported |
| 71 | CAP_ENUM = 1 << 0, |
| 72 | /// NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported |
| 73 | CAP_IF_ADD_REMOVE = 1 << 1, |
| 74 | /// NetworkInterface onStateChanged signal is supported |
| 75 | CAP_STATE_CHANGE = 1 << 2, |
| 76 | /// NetworkInterface onMtuChanged signal is supported |
| 77 | CAP_MTU_CHANGE = 1 << 3, |
| 78 | /// NetworkInterface onAddressAdded and onAddressRemoved signals are supported |
| 79 | CAP_ADDR_ADD_REMOVE = 1 << 4 |
| 80 | }; |
| 81 | |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 82 | /// Returns a bitwise OR'ed set of #Capability flags supported on the current platform. |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 83 | uint32_t |
| 84 | getCapabilities() const; |
| 85 | |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 86 | /// Returns the NetworkInterface with the given name, or @c nullptr if it does not exist. |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 87 | shared_ptr<const NetworkInterface> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 88 | getNetworkInterface(const std::string& ifname) const; |
| 89 | |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 90 | /** |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 91 | * @brief Lists all network interfaces currently available on the system. |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 92 | * @warning May return incomplete results if called before the |
| 93 | * #onEnumerationCompleted signal has been emitted. |
| 94 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 95 | std::vector<shared_ptr<const NetworkInterface>> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 96 | listNetworkInterfaces() const; |
| 97 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 98 | protected: |
| 99 | explicit |
| 100 | NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl); |
| 101 | |
| 102 | NetworkMonitorImpl& |
| 103 | getImpl() |
| 104 | { |
| 105 | return *m_impl; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | const unique_ptr<NetworkMonitorImpl> m_impl; |
| 110 | // Intentional violation of code-style rule 1.4: m_impl must be assigned before its signals can |
| 111 | // be assigned to references below. |
| 112 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 113 | public: // signals |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 114 | /// Fires when network interfaces enumeration is complete |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 115 | util::Signal<NetworkMonitorImpl>& onEnumerationCompleted; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 116 | |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 117 | /// Fires when a new interface is added |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 118 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceAdded; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * @brief Fires when an interface is removed |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 122 | * @note The NetworkInterface object is no longer present in the internal list of |
| 123 | * network interfaces when this signal is emitted. |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 124 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 125 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceRemoved; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 126 | |
Davide Pesavento | 4346290 | 2017-07-05 02:06:07 -0400 | [diff] [blame] | 127 | /// @deprecated Only for backward compatibility |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 128 | util::Signal<NetworkMonitorImpl>& onNetworkStateChanged; |
| 129 | }; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 130 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 131 | class NetworkMonitorImpl : noncopyable |
| 132 | { |
| 133 | public: |
Davide Pesavento | a0b2a2c | 2018-07-19 01:01:06 -0400 | [diff] [blame] | 134 | using Error = NetworkMonitor::Error; |
| 135 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 136 | virtual |
| 137 | ~NetworkMonitorImpl() = default; |
| 138 | |
| 139 | virtual uint32_t |
| 140 | getCapabilities() const = 0; |
| 141 | |
| 142 | virtual shared_ptr<const NetworkInterface> |
| 143 | getNetworkInterface(const std::string&) const = 0; |
| 144 | |
| 145 | virtual std::vector<shared_ptr<const NetworkInterface>> |
| 146 | listNetworkInterfaces() const = 0; |
| 147 | |
| 148 | protected: |
| 149 | static shared_ptr<NetworkInterface> |
| 150 | makeNetworkInterface(); |
| 151 | |
| 152 | public: |
| 153 | util::Signal<NetworkMonitorImpl> onEnumerationCompleted; |
| 154 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceAdded; |
| 155 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceRemoved; |
| 156 | util::Signal<NetworkMonitorImpl> onNetworkStateChanged; |
| 157 | |
| 158 | protected: |
| 159 | DECLARE_SIGNAL_EMIT(onEnumerationCompleted) |
| 160 | DECLARE_SIGNAL_EMIT(onInterfaceAdded) |
| 161 | DECLARE_SIGNAL_EMIT(onInterfaceRemoved) |
| 162 | DECLARE_SIGNAL_EMIT(onNetworkStateChanged) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 165 | } // namespace net |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 166 | } // namespace ndn |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 167 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 168 | #endif // NDN_NET_NETWORK_MONITOR_HPP |