ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
network-interface.cpp
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 <[email protected]>
22 */
23
25#include "ndn-cxx/net/impl/linux-if-constants.hpp"
28
29#include <net/if.h>
30
31namespace ndn::net {
32
33NDN_LOG_INIT(ndn.NetworkMonitor);
34
35NetworkInterface::NetworkInterface() = default;
36
37bool
39{
40 if (!address.getIp().is_unspecified()) {
41 // need to erase the existing address before inserting
42 // because the address flags may have changed
43 bool isNew = m_netAddresses.erase(address) == 0;
44 m_netAddresses.insert(address);
45 if (isNew) {
46 NDN_LOG_DEBUG("added address " << address << " to " << m_name);
47 onAddressAdded(address);
48 return true;
49 }
50 }
51 return false;
52}
53
54bool
56{
57 if (m_netAddresses.erase(address) > 0) {
58 NDN_LOG_DEBUG("removed address " << address << " from " << m_name);
59 onAddressRemoved(address);
60 return true;
61 }
62 return false;
63}
64
65void
67{
68 m_index = index;
69}
70
71void
72NetworkInterface::setName(const std::string& name)
73{
74 BOOST_ASSERT(!name.empty());
75 m_name = name;
76}
77
78void
80{
81 m_type = type;
82}
83
84void
86{
87 m_flags = flags;
88}
89
90void
92{
93 if (m_state != state) {
94 std::swap(m_state, state);
95 onStateChanged(state, m_state);
96 }
97}
98
99void
101{
102 if (m_mtu != mtu) {
103 std::swap(m_mtu, mtu);
104 onMtuChanged(mtu, m_mtu);
105 }
106}
107
108void
110{
111 m_etherAddress = address;
112}
113
114void
116{
117 m_etherBrdAddress = address;
118}
119
120std::ostream&
121operator<<(std::ostream& os, InterfaceType type)
122{
123 switch (type) {
125 return os << "unknown";
127 return os << "loopback";
129 return os << "ether";
130 }
131 return os;
132}
133
134std::ostream&
135operator<<(std::ostream& os, InterfaceState state)
136{
137 switch (state) {
139 return os << "unknown";
141 return os << "down";
143 return os << "no-carrier";
145 return os << "dormant";
147 return os << "running";
148 }
149 return os;
150}
151
152static void
153printFlag(std::ostream& os, uint32_t& flags, uint32_t flagVal, const char* flagStr)
154{
155 if (flags & flagVal) {
156 flags &= ~flagVal;
157 os << flagStr << (flags ? "," : "");
158 }
159}
160
161std::ostream&
162operator<<(std::ostream& os, const NetworkInterface& netif)
163{
164 os << netif.getIndex() << ": " << netif.getName() << ": ";
165
166 auto flags = netif.getFlags();
167 os << "<";
168#define PRINT_IFF(flag) printFlag(os, flags, IFF_##flag, #flag)
169 PRINT_IFF(UP);
170 PRINT_IFF(BROADCAST);
171 PRINT_IFF(DEBUG);
173 PRINT_IFF(POINTOPOINT);
174#if defined(IFF_NOTRAILERS)
175 PRINT_IFF(NOTRAILERS);
176#endif
178 PRINT_IFF(NOARP);
179 PRINT_IFF(PROMISC);
180 PRINT_IFF(ALLMULTI);
181 PRINT_IFF(MULTICAST);
182#if defined(__linux__)
183 PRINT_IFF(MASTER);
184 PRINT_IFF(SLAVE);
185 PRINT_IFF(PORTSEL);
186 PRINT_IFF(AUTOMEDIA);
187 PRINT_IFF(DYNAMIC);
188#elif defined(__APPLE__) || defined(__FreeBSD__)
189 PRINT_IFF(OACTIVE);
190 PRINT_IFF(SIMPLEX);
191 PRINT_IFF(LINK0);
192 PRINT_IFF(LINK1);
193 PRINT_IFF(LINK2);
194#endif
195#if defined(__FreeBSD__)
196 PRINT_IFF(CANTCONFIG);
197 PRINT_IFF(PPROMISC);
198 PRINT_IFF(MONITOR);
199 PRINT_IFF(STATICARP);
200 PRINT_IFF(DYING);
201 PRINT_IFF(RENAMING);
202#endif
203#undef PRINT_IFF
204#if defined(__linux__)
205#define PRINT_IF_FLAG(flag) printFlag(os, flags, linux_if::FLAG_##flag, #flag)
206 PRINT_IF_FLAG(LOWER_UP);
207 PRINT_IF_FLAG(DORMANT);
208 PRINT_IF_FLAG(ECHO);
209#undef PRINT_IF_FLAG
210#endif
211 if (flags) {
212 // print unknown flags in hex
213 os << AsHex{flags};
214 }
215 os << ">";
216
217 os << " state " << netif.getState() << " mtu " << netif.getMtu() << "\n"
218 << " link/" << netif.getType() << " " << netif.getEthernetAddress()
219 << " brd " << netif.getEthernetBroadcastAddress() << "\n";
220
221 for (const auto& addr : netif.getNetworkAddresses()) {
222 os << " " << (addr.getFamily() == AddressFamily::V4 ? "inet " : "inet6 ") << addr;
223 if (netif.canBroadcast() && !addr.getBroadcast().is_unspecified()) {
224 os << " brd " << addr.getBroadcast();
225 }
226 os << " scope " << addr.getScope();
227 if (addr.isDeprecated()) {
228 os << " deprecated";
229 }
230 os << "\n";
231 }
232
233 return os;
234}
235
236} // namespace ndn::net
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Represents an Ethernet hardware address.
Definition ethernet.hpp:52
Stores one IP address supported by a network interface.
boost::asio::ip::address getIp() const
Returns the IP address (v4 or v6)
Represents one network interface attached to the host.
uint32_t getMtu() const
Returns the MTU (maximum transmission unit) of the interface.
void setEthernetAddress(const ethernet::Address &address)
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.
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.
const std::set< NetworkAddress > & getNetworkAddresses() const
Returns all network-layer addresses present on the interface.
int getIndex() const
Returns an opaque ID that uniquely identifies the interface on the system.
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition logger.hpp:260
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition logger.hpp:169
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
#define PRINT_IFF(flag)