Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 28 | #include "network-interface.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 31 | namespace boost { |
| 32 | namespace asio { |
| 33 | class io_service; |
| 34 | } // namespace asio |
| 35 | } // namespace boost |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 38 | namespace net { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 39 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 40 | class NetworkMonitorImpl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 41 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 42 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 43 | * @brief Network interfaces monitor |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 44 | * |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 45 | * Maintains an up-to-date view of every system network interface and notifies when an interface |
| 46 | * is added or removed. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 47 | * |
| 48 | * @note Implementation of this class is platform dependent and not all supported platforms |
| 49 | * are supported: |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 50 | * - OS X: SystemConfiguration and CFNotificationCenterAddObserver notifications (no |
| 51 | * notification on MTU change) |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 52 | * - Linux: rtnetlink notifications |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 53 | */ |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 54 | class NetworkMonitor : noncopyable |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 55 | { |
| 56 | public: |
| 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 Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 68 | * @brief Construct instance, request enumeration of all network interfaces, and start |
| 69 | * monitoring for network state changes |
| 70 | * |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 71 | * @param io io_service thread that will dispatch events |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 72 | * @throw Error error starting monitoring |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 73 | */ |
| 74 | explicit |
| 75 | NetworkMonitor(boost::asio::io_service& io); |
| 76 | |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 77 | 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 | |
| 92 | /** \return bitwise OR'ed \p Capability supported on current platform |
| 93 | */ |
| 94 | uint32_t |
| 95 | getCapabilities() const; |
| 96 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 97 | shared_ptr<const NetworkInterface> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 98 | getNetworkInterface(const std::string& ifname) const; |
| 99 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 100 | std::vector<shared_ptr<const NetworkInterface>> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 101 | listNetworkInterfaces() const; |
| 102 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 103 | protected: |
| 104 | explicit |
| 105 | NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl); |
| 106 | |
| 107 | NetworkMonitorImpl& |
| 108 | getImpl() |
| 109 | { |
| 110 | return *m_impl; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | const unique_ptr<NetworkMonitorImpl> m_impl; |
| 115 | // Intentional violation of code-style rule 1.4: m_impl must be assigned before its signals can |
| 116 | // be assigned to references below. |
| 117 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 118 | public: // signals |
| 119 | /** @brief Fires when network interfaces enumeration is complete |
| 120 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 121 | util::Signal<NetworkMonitorImpl>& onEnumerationCompleted; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 122 | |
| 123 | /** @brief Fires when a new interface is added |
| 124 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 125 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceAdded; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * @brief Fires when an interface is removed |
| 129 | * @note The NetworkInterface object is no longer present in the network |
| 130 | * interfaces map when the signal is emitted |
| 131 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 132 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceRemoved; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 133 | |
| 134 | // only for backward compatibility |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 135 | util::Signal<NetworkMonitorImpl>& onNetworkStateChanged; |
| 136 | }; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 137 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 138 | class NetworkMonitorImpl : noncopyable |
| 139 | { |
| 140 | public: |
| 141 | virtual |
| 142 | ~NetworkMonitorImpl() = default; |
| 143 | |
| 144 | virtual uint32_t |
| 145 | getCapabilities() const = 0; |
| 146 | |
| 147 | virtual shared_ptr<const NetworkInterface> |
| 148 | getNetworkInterface(const std::string&) const = 0; |
| 149 | |
| 150 | virtual std::vector<shared_ptr<const NetworkInterface>> |
| 151 | listNetworkInterfaces() const = 0; |
| 152 | |
| 153 | protected: |
| 154 | static shared_ptr<NetworkInterface> |
| 155 | makeNetworkInterface(); |
| 156 | |
| 157 | public: |
| 158 | util::Signal<NetworkMonitorImpl> onEnumerationCompleted; |
| 159 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceAdded; |
| 160 | util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>> onInterfaceRemoved; |
| 161 | util::Signal<NetworkMonitorImpl> onNetworkStateChanged; |
| 162 | |
| 163 | protected: |
| 164 | DECLARE_SIGNAL_EMIT(onEnumerationCompleted) |
| 165 | DECLARE_SIGNAL_EMIT(onInterfaceAdded) |
| 166 | DECLARE_SIGNAL_EMIT(onInterfaceRemoved) |
| 167 | DECLARE_SIGNAL_EMIT(onNetworkStateChanged) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 170 | } // namespace net |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 171 | } // namespace ndn |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 172 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 173 | #endif // NDN_NET_NETWORK_MONITOR_HPP |