blob: f3326b7b26a2a712339b29fe1b6c4818b5f7081f [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 Shi25467942017-06-30 02:53:14 +000028#include "../util/signal.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080029
Davide Pesavento2bf35a62017-04-02 00:41:06 -040030#include <vector>
31
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032// forward declaration
33namespace boost {
34namespace asio {
35class io_service;
36} // namespace asio
37} // namespace boost
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080038
39namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000040namespace net {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080041
Davide Pesavento2bf35a62017-04-02 00:41:06 -040042class NetworkInterface;
43
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080044/**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040045 * @brief Network interfaces monitor
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080046 *
Davide Pesavento2bf35a62017-04-02 00:41:06 -040047 * Maintains an up-to-date view of every system network interface and notifies when an interface
48 * is added or removed.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080049 *
50 * @note Implementation of this class is platform dependent and not all supported platforms
51 * are supported:
Alexander Afanasyev3b3355c2017-03-26 11:57:13 -050052 * - OS X: SystemConfiguration and CFNotificationCenterAddObserver notifications (no
53 * notification on MTU change)
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080054 * - Linux: rtnetlink notifications
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080055 */
Davide Pesavento2bf35a62017-04-02 00:41:06 -040056class NetworkMonitor : noncopyable
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080057{
58public:
59 class Error : public std::runtime_error
60 {
61 public:
62 explicit
63 Error(const std::string& what)
64 : std::runtime_error(what)
65 {
66 }
67 };
68
Davide Pesavento2bf35a62017-04-02 00:41:06 -040069 class Impl;
70
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080071 /**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040072 * @brief Construct instance, request enumeration of all network interfaces, and start
73 * monitoring for network state changes
74 *
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080075 * @param io io_service thread that will dispatch events
76 * @throw Error when network monitoring is not supported or there is an error starting monitoring
77 */
78 explicit
79 NetworkMonitor(boost::asio::io_service& io);
80
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080081 ~NetworkMonitor();
82
Junxiao Shif4c1eb32017-05-30 17:05:10 +000083 enum Capability : uint32_t {
84 /// NetworkMonitor is not supported and is a no-op
85 CAP_NONE = 0,
86 /// listNetworkInterfaces() and getNetworkInterface() are supported
87 CAP_ENUM = 1 << 0,
88 /// NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported
89 CAP_IF_ADD_REMOVE = 1 << 1,
90 /// NetworkInterface onStateChanged signal is supported
91 CAP_STATE_CHANGE = 1 << 2,
92 /// NetworkInterface onMtuChanged signal is supported
93 CAP_MTU_CHANGE = 1 << 3,
94 /// NetworkInterface onAddressAdded and onAddressRemoved signals are supported
95 CAP_ADDR_ADD_REMOVE = 1 << 4
96 };
97
98 /** \return bitwise OR'ed \p Capability supported on current platform
99 */
100 uint32_t
101 getCapabilities() const;
102
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400103 shared_ptr<NetworkInterface>
104 getNetworkInterface(const std::string& ifname) const;
105
106 std::vector<shared_ptr<NetworkInterface>>
107 listNetworkInterfaces() const;
108
109public: // signals
110 /** @brief Fires when network interfaces enumeration is complete
111 */
Junxiao Shi25467942017-06-30 02:53:14 +0000112 util::Signal<NetworkMonitor> onEnumerationCompleted;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400113
114 /** @brief Fires when a new interface is added
115 */
Junxiao Shi25467942017-06-30 02:53:14 +0000116 util::Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceAdded;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400117
118 /**
119 * @brief Fires when an interface is removed
120 * @note The NetworkInterface object is no longer present in the network
121 * interfaces map when the signal is emitted
122 */
Junxiao Shi25467942017-06-30 02:53:14 +0000123 util::Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceRemoved;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400124
125 // only for backward compatibility
Junxiao Shi25467942017-06-30 02:53:14 +0000126 util::Signal<NetworkMonitor> onNetworkStateChanged;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800127
128private:
Davide Pesavento794f6872017-05-15 23:33:38 -0400129 const unique_ptr<Impl> m_impl;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800130};
131
Junxiao Shi25467942017-06-30 02:53:14 +0000132} // namespace net
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400133} // namespace ndn
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800134
Junxiao Shi25467942017-06-30 02:53:14 +0000135#endif // NDN_NET_NETWORK_MONITOR_HPP