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-2024 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"
30 
31 #include <set>
32 
33 namespace ndn::net {
34 
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 
51 enum class InterfaceState {
52  UNKNOWN,
53  DOWN,
54  NO_CARRIER,
55  DORMANT,
56  RUNNING,
57 };
58 
59 std::ostream&
60 operator<<(std::ostream& os, InterfaceState state);
61 
71 {
72 public: // signals, marked 'mutable' so they can be connected on a 'const NetworkInterface'
75 
77  mutable signal::Signal<NetworkInterface, uint32_t /*old*/, uint32_t /*new*/> onMtuChanged;
78 
81 
84 
85 public: // getters
89  int
90  getIndex() const
91  {
92  return m_index;
93  }
94 
98  std::string
99  getName() const
100  {
101  return m_name;
102  }
103 
108  getType() const
109  {
110  return m_type;
111  }
112 
116  uint32_t
117  getFlags() const
118  {
119  return m_flags;
120  }
121 
126  getState() const
127  {
128  return m_state;
129  }
130 
134  uint32_t
135  getMtu() const
136  {
137  return m_mtu;
138  }
139 
145  {
146  return m_etherAddress;
147  }
148 
154  {
155  return m_etherBrdAddress;
156  }
157 
161  const std::set<NetworkAddress>&
163  {
164  return m_netAddresses;
165  }
166 
170  bool
171  isLoopback() const
172  {
173  return (m_flags & IFF_LOOPBACK) != 0;
174  }
175 
179  bool
181  {
182  return (m_flags & IFF_POINTOPOINT) != 0;
183  }
184 
188  bool
189  canBroadcast() const
190  {
191  return (m_flags & IFF_BROADCAST) != 0;
192  }
193 
197  bool
198  canMulticast() const
199  {
200  return (m_flags & IFF_MULTICAST) != 0;
201  }
202 
206  bool
207  isUp() const
208  {
209  return (m_flags & IFF_UP) != 0;
210  }
211 
212 public: // modifiers: they update the info on this in-memory object, but do not modify the interface in the OS
213  bool
214  addNetworkAddress(const NetworkAddress& address);
215 
216  bool
217  removeNetworkAddress(const NetworkAddress& address);
218 
219  void
220  setIndex(int index);
221 
222  void
223  setName(const std::string& name);
224 
225  void
226  setType(InterfaceType type);
227 
228  void
229  setFlags(uint32_t flags);
230 
231  void
232  setState(InterfaceState state);
233 
234  void
235  setMtu(uint32_t mtu);
236 
237  void
238  setEthernetAddress(const ethernet::Address& address);
239 
240  void
242 
243 private: // constructor
244  NetworkInterface(); // accessible through NetworkMonitorImpl::makeNetworkInterface
245 
246 private:
247  int m_index = 0;
248  std::string m_name;
250  uint32_t m_flags = 0; // IFF_* in <net/if.h>
252  uint32_t m_mtu = 0;
253  ethernet::Address m_etherAddress;
254  ethernet::Address m_etherBrdAddress;
255  std::set<NetworkAddress> m_netAddresses;
256 
257  friend class NetworkMonitorImpl;
258 };
259 
260 std::ostream&
261 operator<<(std::ostream& os, const NetworkInterface& interface);
262 
263 } // namespace ndn::net
264 
265 #endif // NDN_CXX_NET_NETWORK_INTERFACE_HPP
Represents an Ethernet hardware address.
Definition: ethernet.hpp:52
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 all network-layer addresses present on the interface.
bool canMulticast() const
Returns true if the interface supports multicast communication.
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.
signal::Signal< NetworkInterface, uint32_t, uint32_t > onMtuChanged
Fires when the interface MTU changes.
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.
signal::Signal< NetworkInterface, NetworkAddress > onAddressRemoved
Fires when a network-layer address is removed from the interface.
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.
signal::Signal< NetworkInterface, NetworkAddress > onAddressAdded
Fires when a network-layer address is added to the interface.
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)
signal::Signal< NetworkInterface, InterfaceState, InterfaceState > onStateChanged
Fires when the interface state changes.
int getIndex() const
Returns an opaque ID that uniquely identifies the interface on the system.
Provides a lightweight signal / event system.
Definition: signal.hpp:51
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