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 | |
| 25 | #ifndef NDN_UTIL_NETWORK_MONITOR_HPP |
| 26 | #define NDN_UTIL_NETWORK_MONITOR_HPP |
| 27 | |
| 28 | #include "signal.hpp" |
| 29 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 32 | // forward declaration |
| 33 | namespace boost { |
| 34 | namespace asio { |
| 35 | class io_service; |
| 36 | } // namespace asio |
| 37 | } // namespace boost |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 38 | |
| 39 | namespace ndn { |
| 40 | namespace util { |
| 41 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 42 | class NetworkInterface; |
| 43 | |
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 | * @brief Network interfaces monitor |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 46 | * |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 47 | * Maintains an up-to-date view of every system network interface and notifies when an interface |
| 48 | * is added or removed. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 49 | * |
| 50 | * @note Implementation of this class is platform dependent and not all supported platforms |
| 51 | * are supported: |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 52 | * - OS X: CFNotificationCenterAddObserver (incomplete) |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 53 | * - Linux: rtnetlink notifications |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 54 | * |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 55 | * @todo macOS implementation needs to be updated to emit the new signals and keep track of |
| 56 | * interfaces (links) and addresses |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 57 | */ |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 58 | class NetworkMonitor : noncopyable |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | class Error : public std::runtime_error |
| 62 | { |
| 63 | public: |
| 64 | explicit |
| 65 | Error(const std::string& what) |
| 66 | : std::runtime_error(what) |
| 67 | { |
| 68 | } |
| 69 | }; |
| 70 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 71 | class Impl; |
| 72 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 73 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 74 | * @brief Construct instance, request enumeration of all network interfaces, and start |
| 75 | * monitoring for network state changes |
| 76 | * |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 77 | * @param io io_service thread that will dispatch events |
| 78 | * @throw Error when network monitoring is not supported or there is an error starting monitoring |
| 79 | */ |
| 80 | explicit |
| 81 | NetworkMonitor(boost::asio::io_service& io); |
| 82 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 83 | ~NetworkMonitor(); |
| 84 | |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 85 | enum Capability : uint32_t { |
| 86 | /// NetworkMonitor is not supported and is a no-op |
| 87 | CAP_NONE = 0, |
| 88 | /// listNetworkInterfaces() and getNetworkInterface() are supported |
| 89 | CAP_ENUM = 1 << 0, |
| 90 | /// NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported |
| 91 | CAP_IF_ADD_REMOVE = 1 << 1, |
| 92 | /// NetworkInterface onStateChanged signal is supported |
| 93 | CAP_STATE_CHANGE = 1 << 2, |
| 94 | /// NetworkInterface onMtuChanged signal is supported |
| 95 | CAP_MTU_CHANGE = 1 << 3, |
| 96 | /// NetworkInterface onAddressAdded and onAddressRemoved signals are supported |
| 97 | CAP_ADDR_ADD_REMOVE = 1 << 4 |
| 98 | }; |
| 99 | |
| 100 | /** \return bitwise OR'ed \p Capability supported on current platform |
| 101 | */ |
| 102 | uint32_t |
| 103 | getCapabilities() const; |
| 104 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 105 | shared_ptr<NetworkInterface> |
| 106 | getNetworkInterface(const std::string& ifname) const; |
| 107 | |
| 108 | std::vector<shared_ptr<NetworkInterface>> |
| 109 | listNetworkInterfaces() const; |
| 110 | |
| 111 | public: // signals |
| 112 | /** @brief Fires when network interfaces enumeration is complete |
| 113 | */ |
| 114 | Signal<NetworkMonitor> onEnumerationCompleted; |
| 115 | |
| 116 | /** @brief Fires when a new interface is added |
| 117 | */ |
| 118 | Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceAdded; |
| 119 | |
| 120 | /** |
| 121 | * @brief Fires when an interface is removed |
| 122 | * @note The NetworkInterface object is no longer present in the network |
| 123 | * interfaces map when the signal is emitted |
| 124 | */ |
| 125 | Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceRemoved; |
| 126 | |
| 127 | // only for backward compatibility |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 128 | Signal<NetworkMonitor> onNetworkStateChanged; |
| 129 | |
| 130 | private: |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 131 | const unique_ptr<Impl> m_impl; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace util |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 135 | } // namespace ndn |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 136 | |
| 137 | #endif // NDN_UTIL_NETWORK_MONITOR_HPP |