network-interface.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 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_CXX_NET_NETWORK_INTERFACE_HPP
25 #define NDN_CXX_NET_NETWORK_INTERFACE_HPP
26 
27 #include "ndn-cxx/net/ethernet.hpp"
29 #include "ndn-cxx/util/signal.hpp"
30 
31 #include <set>
32 
33 namespace ndn {
34 namespace net {
35 
38 enum class InterfaceType {
39  UNKNOWN,
40  LOOPBACK,
41  ETHERNET,
42  // we do not support anything else for now
43 };
44 
45 std::ostream&
46 operator<<(std::ostream& os, InterfaceType type);
47 
50 enum class InterfaceState {
51  UNKNOWN,
52  DOWN,
53  NO_CARRIER,
54  DORMANT,
55  RUNNING,
56 };
57 
58 std::ostream&
59 operator<<(std::ostream& os, InterfaceState state);
60 
70 {
71 public: // signals, marked 'mutable' so they can be connected on 'const NetworkInterface'
75 
78  mutable util::Signal<NetworkInterface, uint32_t /*old*/, uint32_t /*new*/> onMtuChanged;
79 
83 
87 
88 public: // getters
91  int
92  getIndex() const
93  {
94  return m_index;
95  }
96 
99  std::string
100  getName() const
101  {
102  return m_name;
103  }
104 
108  getType() const
109  {
110  return m_type;
111  }
112 
115  uint32_t
116  getFlags() const
117  {
118  return m_flags;
119  }
120 
124  getState() const
125  {
126  return m_state;
127  }
128 
131  uint32_t
132  getMtu() const
133  {
134  return m_mtu;
135  }
136 
141  {
142  return m_etherAddress;
143  }
144 
149  {
150  return m_etherBrdAddress;
151  }
152 
155  const std::set<NetworkAddress>&
157  {
158  return m_netAddresses;
159  }
160 
163  bool
164  isLoopback() const
165  {
166  return (m_flags & IFF_LOOPBACK) != 0;
167  }
168 
171  bool
173  {
174  return (m_flags & IFF_POINTOPOINT) != 0;
175  }
176 
179  bool
180  canBroadcast() const
181  {
182  return (m_flags & IFF_BROADCAST) != 0;
183  }
184 
187  bool
188  canMulticast() const
189  {
190  return (m_flags & IFF_MULTICAST) != 0;
191  }
192 
195  bool
196  isUp() const
197  {
198  return (m_flags & IFF_UP) != 0;
199  }
200 
201 public: // modifiers: they update information on this instance, but do not change netif in the OS
202  bool
203  addNetworkAddress(const NetworkAddress& address);
204 
205  bool
206  removeNetworkAddress(const NetworkAddress& address);
207 
208  void
209  setIndex(int index);
210 
211  void
212  setName(const std::string& name);
213 
214  void
215  setType(InterfaceType type);
216 
217  void
218  setFlags(uint32_t flags);
219 
220  void
221  setState(InterfaceState state);
222 
223  void
224  setMtu(uint32_t mtu);
225 
226  void
227  setEthernetAddress(const ethernet::Address& address);
228 
229  void
231 
232 private: // constructor
233  NetworkInterface(); // accessible through NetworkMonitorImpl::makeNetworkInterface
234 
235 private:
236  int m_index;
237  std::string m_name;
238  InterfaceType m_type;
239  uint32_t m_flags; // IFF_* in <net/if.h>
240  InterfaceState m_state;
241  uint32_t m_mtu;
242  ethernet::Address m_etherAddress;
243  ethernet::Address m_etherBrdAddress;
244  std::set<NetworkAddress> m_netAddresses;
245 
246  friend class NetworkMonitorImpl;
247 };
248 
249 std::ostream&
250 operator<<(std::ostream& os, const NetworkInterface& interface);
251 
252 } // namespace net
253 } // namespace ndn
254 
255 #endif // NDN_CXX_NET_NETWORK_INTERFACE_HPP
Represents an Ethernet hardware address.
Definition: ethernet.hpp:53
Stores one IP address supported by a network interface.
Represents one network interface attached to the host.
uint32_t getMtu() const
Returns the MTU (maximum transmission unit) of the interface.
const std::set< NetworkAddress > & getNetworkAddresses() const
Returns a list of all network-layer addresses present on the interface.
bool canMulticast() const
Returns true if the interface supports multicast communication.
util::Signal< NetworkInterface, NetworkAddress > onAddressRemoved
Fires when a network-layer address is removed from the interface.
void setEthernetAddress(const ethernet::Address &address)
bool isPointToPoint() const
Returns true if the interface is a point-to-point interface.
bool isLoopback() const
Returns true if the interface is a loopback interface.
void setType(InterfaceType type)
void setState(InterfaceState state)
bool removeNetworkAddress(const NetworkAddress &address)
uint32_t getFlags() const
Returns a bitset of platform-specific flags enabled on the interface.
void setEthernetBroadcastAddress(const ethernet::Address &address)
bool canBroadcast() const
Returns true if the interface supports broadcast communication.
util::Signal< NetworkInterface, NetworkAddress > onAddressAdded
Fires when a network-layer address is added to the interface.
util::Signal< NetworkInterface, InterfaceState, InterfaceState > onStateChanged
Fires when interface state changes.
util::Signal< NetworkInterface, uint32_t, uint32_t > onMtuChanged
Fires when interface mtu changes.
bool isUp() const
Returns true if the interface is administratively up.
InterfaceState getState() const
Returns the current state of the interface.
std::string getName() const
Returns the name of the interface, unique on the system.
ethernet::Address getEthernetAddress() const
Returns the link-layer (Ethernet) address of the interface.
void setFlags(uint32_t flags)
bool addNetworkAddress(const NetworkAddress &address)
ethernet::Address getEthernetBroadcastAddress() const
Returns the link-layer (Ethernet) broadcast address of the interface.
InterfaceType getType() const
Returns the hardware type of the interface.
void setName(const std::string &name)
int getIndex() const
Returns an opaque ID that uniquely identifies the interface on the system.
Provides a lightweight signal / event system.
Definition: signal.hpp:53
std::ostream & operator<<(std::ostream &os, AddressScope scope)
InterfaceType
Indicates the hardware type of a network interface.
InterfaceState
Indicates the state of a network interface.
@ RUNNING
interface can be used to send and receive packets
@ UNKNOWN
interface is in an unknown state
@ DORMANT
interface has a carrier but it cannot send or receive normal user traffic yet
@ DOWN
interface is administratively down
@ NO_CARRIER
interface is administratively up but has no carrier
Definition: data.cpp:25