blob: 4b53a1d83a97e2e4bee3218177cc746c243748f9 [file] [log] [blame]
Davide Pesavento2bf35a62017-04-02 00:41:06 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
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.
20 *
21 * @author Davide Pesavento <davide.pesavento@lip6.fr>
22 */
23
24#ifndef NDN_UTIL_NETWORK_INTERFACE_HPP
25#define NDN_UTIL_NETWORK_INTERFACE_HPP
26
27#include "ethernet.hpp"
28#include "network-address.hpp"
29#include "network-monitor.hpp"
30#include "signal.hpp"
31
32#include <set>
33
34namespace ndn {
35namespace util {
36
37/** @brief Indicates the hardware type of a network interface
38 */
39enum class InterfaceType {
40 UNKNOWN,
41 LOOPBACK,
42 ETHERNET,
43 // we do not support anything else for now
44};
45
46std::ostream&
47operator<<(std::ostream& os, InterfaceType type);
48
49/** @brief Indicates the state of a network interface
50 */
51enum class InterfaceState {
52 UNKNOWN, ///< interface is in an unknown state
53 DOWN, ///< interface is administratively down
54 NO_CARRIER, ///< interface is administratively up but has no carrier
55 DORMANT, ///< interface has a carrier but it cannot send or receive normal user traffic yet
56 RUNNING, ///< interface can be used to send and receive packets
57};
58
59std::ostream&
60operator<<(std::ostream& os, InterfaceState state);
61
62/**
63 * @brief Represents one network interface attached to the host.
64 *
65 * Each network interface has a unique index, a name, and a set of flags indicating its
66 * capabilities and current state. It may contain one hardware (Ethernet) address, and
67 * zero or more network-layer (IP) addresses. Specific signals are emitted when the
68 * interface data change.
69 */
70class NetworkInterface
71{
72public: // signals
73 /** @brief Fires when interface state changes
74 */
75 Signal<NetworkInterface, InterfaceState /*old*/, InterfaceState /*new*/> onStateChanged;
76
77 /** @brief Fires when interface mtu changes
78 */
79 Signal<NetworkInterface, uint32_t /*old*/, uint32_t /*new*/> onMtuChanged;
80
81 /** @brief Fires when a network-layer address is added to the interface
82 */
83 Signal<NetworkInterface, NetworkAddress> onAddressAdded;
84
85 /** @brief Fires when a network-layer address is removed from the interface
86 */
87 Signal<NetworkInterface, NetworkAddress> onAddressRemoved;
88
89public: // getters
90 /** @brief Returns an opaque ID that uniquely identifies the interface on the system
91 */
92 int
93 getIndex() const
94 {
95 return m_index;
96 }
97
98 /** @brief Returns the name of the interface, unique on the system
99 */
100 std::string
101 getName() const
102 {
103 return m_name;
104 }
105
106 /** @brief Returns the hardware type of the interface
107 */
108 InterfaceType
109 getType() const
110 {
111 return m_type;
112 }
113
114 /** @brief Returns a bitset of platform-specific flags enabled on the interface
115 */
116 uint32_t
117 getFlags() const
118 {
119 return m_flags;
120 }
121
122 /** @brief Returns the current state of the interface
123 */
124 InterfaceState
125 getState() const
126 {
127 return m_state;
128 }
129
130 /** @brief Returns the MTU (maximum transmission unit) of the interface
131 */
132 uint32_t
133 getMtu() const
134 {
135 return m_mtu;
136 }
137
138 /** @brief Returns the link-layer (Ethernet) address of the interface
139 */
140 ethernet::Address
141 getEthernetAddress() const
142 {
143 return m_etherAddress;
144 }
145
146 /** @brief Returns the link-layer (Ethernet) broadcast address of the interface
147 */
148 ethernet::Address
149 getEthernetBroadcastAddress() const
150 {
151 return m_etherBrdAddress;
152 }
153
154 /** @brief Returns a list of all network-layer addresses present on the interface
155 */
156 const std::set<NetworkAddress>&
157 getNetworkAddresses() const
158 {
159 return m_netAddresses;
160 }
161
162 /** @brief Returns true if the interface is a loopback interface
163 */
164 bool
165 isLoopback() const
166 {
167 return (m_flags & IFF_LOOPBACK) != 0;
168 }
169
170 /** @brief Returns true if the interface is a point-to-point interface
171 */
172 bool
173 isPointToPoint() const
174 {
175 return (m_flags & IFF_POINTOPOINT) != 0;
176 }
177
178 /** @brief Returns true if the interface supports broadcast communication
179 */
180 bool
181 canBroadcast() const
182 {
183 return (m_flags & IFF_BROADCAST) != 0;
184 }
185
186 /** @brief Returns true if the interface supports multicast communication
187 */
188 bool
189 canMulticast() const
190 {
191 return (m_flags & IFF_MULTICAST) != 0;
192 }
193
194 /** @brief Returns true if the interface is administratively up
195 */
196 bool
197 isUp() const
198 {
199 return (m_flags & IFF_UP) != 0;
200 }
201
202private: // constructor
203 NetworkInterface();
204
205private: // modifiers
206 bool
207 addNetworkAddress(const NetworkAddress& address);
208
209 bool
210 removeNetworkAddress(const NetworkAddress& address);
211
212 void
213 setIndex(int index);
214
215 void
216 setName(const std::string& name);
217
218 void
219 setType(InterfaceType type);
220
221 void
222 setFlags(uint32_t flags);
223
224 void
225 setState(InterfaceState state);
226
227 void
228 setMtu(uint32_t mtu);
229
230 void
231 setEthernetAddress(const ethernet::Address& address);
232
233 void
234 setEthernetBroadcastAddress(const ethernet::Address& address);
235
236private:
237 friend class NetworkMonitor::Impl;
238
239 int m_index;
240 std::string m_name;
241 InterfaceType m_type;
242 uint32_t m_flags; // IFF_* in <net/if.h>
243 InterfaceState m_state;
244 uint32_t m_mtu;
245 ethernet::Address m_etherAddress;
246 ethernet::Address m_etherBrdAddress;
247 std::set<NetworkAddress> m_netAddresses;
248};
249
250std::ostream&
251operator<<(std::ostream& os, const NetworkInterface& interface);
252
253} // namespace util
254} // namespace ndn
255
256#endif // NDN_UTIL_NETWORK_INTERFACE_HPP