31 #include <type_traits> 32 #include <unordered_map> 37 #include <arpa/inet.h> 38 #include <netinet/in.h> 40 #if defined(__linux__) 41 #include <net/if_arp.h> 42 #include <netpacket/packet.h> 43 #elif defined(__APPLE__) || defined(__FreeBSD__) 44 #include <net/if_dl.h> 45 #include <net/if_types.h> 48 #endif // HAVE_IFADDRS_H 55 static_assert(std::is_standard_layout<NetworkInterfaceInfo>::value,
56 "NetworkInterfaceInfo must be a standard layout type");
57 #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE 58 static_assert(std::is_default_constructible<NetworkInterfaceInfo>::value,
59 "NetworkInterfaceInfo must provide a default constructor");
63 static shared_ptr<std::vector<NetworkInterfaceInfo>> s_debugNetworkInterfaces =
nullptr;
66 setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces)
68 s_debugNetworkInterfaces = interfaces;
72 std::vector<NetworkInterfaceInfo>
76 if (s_debugNetworkInterfaces !=
nullptr) {
77 return *s_debugNetworkInterfaces;
85 std::unordered_map<std::string, NetworkInterfaceInfo> ifmap;
86 ifaddrs* ifa_list =
nullptr;
88 if (::getifaddrs(&ifa_list) < 0)
89 BOOST_THROW_EXCEPTION(std::runtime_error(std::string(
"getifaddrs() failed: ") +
92 for (ifaddrs* ifa = ifa_list; ifa !=
nullptr; ifa = ifa->ifa_next) {
93 std::string ifname(ifa->ifa_name);
95 netif.
name = ifa->ifa_name;
96 netif.
flags = ifa->ifa_flags;
98 if (ifa->ifa_addr ==
nullptr)
101 switch (ifa->ifa_addr->sa_family) {
104 const sockaddr_in* sin =
reinterpret_cast<sockaddr_in*
>(ifa->ifa_addr);
105 char address[INET_ADDRSTRLEN];
106 if (::inet_ntop(AF_INET, &sin->sin_addr, address,
sizeof(address))) {
107 netif.
ipv4Addresses.push_back(address_v4::from_string(address));
111 NFD_LOG_WARN(ifname <<
": inet_ntop(AF_INET) failed: " << strerror(errno));
116 const sockaddr_in6* sin6 =
reinterpret_cast<sockaddr_in6*
>(ifa->ifa_addr);
117 char address[INET6_ADDRSTRLEN];
118 if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address,
sizeof(address))) {
119 netif.
ipv6Addresses.push_back(address_v6::from_string(address));
123 NFD_LOG_WARN(ifname <<
": inet_ntop(AF_INET6) failed: " << strerror(errno));
127 #if defined(__linux__) 129 const sockaddr_ll* sll =
reinterpret_cast<sockaddr_ll*
>(ifa->ifa_addr);
130 netif.
index = sll->sll_ifindex;
135 else if (sll->sll_hatype != ARPHRD_LOOPBACK) {
136 NFD_LOG_DEBUG(ifname <<
": ignoring link-layer address for unhandled hardware type " 142 #elif defined(__APPLE__) || defined(__FreeBSD__) 144 const sockaddr_dl* sdl =
reinterpret_cast<sockaddr_dl*
>(ifa->ifa_addr);
145 netif.
index = sdl->sdl_index;
150 else if (sdl->sdl_type != IFT_LOOP) {
151 NFD_LOG_DEBUG(ifname <<
": ignoring link-layer address for unhandled interface type " 160 const sockaddr_in* sin =
reinterpret_cast<sockaddr_in*
>(ifa->ifa_broadaddr);
161 char address[INET_ADDRSTRLEN];
162 if (::inet_ntop(AF_INET, &sin->sin_addr, address,
sizeof(address))) {
164 NFD_LOG_TRACE(ifname <<
": added IPv4 broadcast address " << address);
167 NFD_LOG_WARN(ifname <<
": inet_ntop(AF_INET) for broadaddr failed: " << strerror(errno));
171 ::freeifaddrs(ifa_list);
173 std::vector<NetworkInterfaceInfo> v;
174 v.reserve(ifmap.size());
175 for (
auto&& element : ifmap) {
176 v.push_back(element.second);
182 #endif // HAVE_IFADDRS_H
const size_t ADDR_LEN
Octets in one Ethernet address.
represents an Ethernet hardware address
contains information about a network interface
#define NFD_LOG_DEBUG(expression)
ethernet::Address etherAddress
bool isBroadcastCapable() const
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
#define NFD_LOG_TRACE(expression)
boost::asio::ip::address_v4 broadcastAddress
Copyright (c) 2011-2015 Regents of the University of California.
std::vector< boost::asio::ip::address_v6 > ipv6Addresses
std::vector< boost::asio::ip::address_v4 > ipv4Addresses
#define NFD_LOG_WARN(expression)
#define NFD_LOG_INIT(name)