Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 24 | */ |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 25 | |
| 26 | #include "network-interface.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 28 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 29 | #include <cerrno> |
| 30 | #include <cstring> |
| 31 | #include <type_traits> |
| 32 | #include <unordered_map> |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 33 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 34 | #ifdef HAVE_IFADDRS_H |
| 35 | #include <ifaddrs.h> // for getifaddrs() |
| 36 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 37 | #include <arpa/inet.h> // for inet_ntop() |
| 38 | #include <netinet/in.h> // for struct sockaddr_in{,6} |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 39 | |
| 40 | #if defined(__linux__) |
| 41 | #include <net/if_arp.h> // for ARPHRD_* constants |
| 42 | #include <netpacket/packet.h> // for struct sockaddr_ll |
Alexander Afanasyev | 500b253 | 2014-03-31 12:29:25 -0700 | [diff] [blame] | 43 | #elif defined(__APPLE__) || defined(__FreeBSD__) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 44 | #include <net/if_dl.h> // for struct sockaddr_dl |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 45 | #include <net/if_types.h> // for IFT_* constants |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 46 | #endif |
| 47 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 48 | #endif // HAVE_IFADDRS_H |
| 49 | |
| 50 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 51 | NFD_LOG_INIT("NetworkInterfaceInfo"); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 52 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 53 | namespace nfd { |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 54 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 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"); |
| 60 | #endif |
| 61 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 62 | #ifdef WITH_TESTS |
| 63 | static shared_ptr<std::vector<NetworkInterfaceInfo>> s_debugNetworkInterfaces = nullptr; |
| 64 | |
| 65 | void |
| 66 | setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces) |
| 67 | { |
| 68 | s_debugNetworkInterfaces = interfaces; |
| 69 | } |
| 70 | #endif |
| 71 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 72 | std::vector<NetworkInterfaceInfo> |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 73 | listNetworkInterfaces() |
| 74 | { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 75 | #ifdef WITH_TESTS |
| 76 | if (s_debugNetworkInterfaces != nullptr) { |
| 77 | return *s_debugNetworkInterfaces; |
| 78 | } |
| 79 | #endif |
| 80 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 81 | #ifdef HAVE_IFADDRS_H |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 82 | using namespace boost::asio::ip; |
| 83 | using std::strerror; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 84 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 85 | std::unordered_map<std::string, NetworkInterfaceInfo> ifmap; |
| 86 | ifaddrs* ifa_list = nullptr; |
| 87 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 88 | if (::getifaddrs(&ifa_list) < 0) |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 89 | throw std::runtime_error(std::string("getifaddrs() failed: ") + strerror(errno)); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 90 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 91 | for (ifaddrs* ifa = ifa_list; ifa != nullptr; ifa = ifa->ifa_next) { |
| 92 | std::string ifname(ifa->ifa_name); |
| 93 | NetworkInterfaceInfo& netif = ifmap[ifname]; |
| 94 | netif.name = ifa->ifa_name; |
| 95 | netif.flags = ifa->ifa_flags; |
| 96 | |
| 97 | if (ifa->ifa_addr == nullptr) |
| 98 | continue; |
| 99 | |
| 100 | switch (ifa->ifa_addr->sa_family) { |
| 101 | |
| 102 | case AF_INET: { |
| 103 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr); |
| 104 | char address[INET_ADDRSTRLEN]; |
| 105 | if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) { |
| 106 | netif.ipv4Addresses.push_back(address_v4::from_string(address)); |
| 107 | NFD_LOG_TRACE(ifname << ": added IPv4 address " << address); |
| 108 | } |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 109 | else |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 110 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) failed: " << strerror(errno)); |
| 111 | break; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 114 | case AF_INET6: { |
| 115 | const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); |
| 116 | char address[INET6_ADDRSTRLEN]; |
| 117 | if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) { |
| 118 | netif.ipv6Addresses.push_back(address_v6::from_string(address)); |
| 119 | NFD_LOG_TRACE(ifname << ": added IPv6 address " << address); |
| 120 | } |
| 121 | else |
| 122 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET6) failed: " << strerror(errno)); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | #if defined(__linux__) |
| 127 | case AF_PACKET: { |
| 128 | const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr); |
| 129 | netif.index = sll->sll_ifindex; |
| 130 | if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) { |
| 131 | netif.etherAddress = ethernet::Address(sll->sll_addr); |
| 132 | NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress); |
| 133 | } |
| 134 | else if (sll->sll_hatype != ARPHRD_LOOPBACK) { |
| 135 | NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled hardware type " |
| 136 | << sll->sll_hatype); |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | #elif defined(__APPLE__) || defined(__FreeBSD__) |
| 142 | case AF_LINK: { |
| 143 | const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr); |
| 144 | netif.index = sdl->sdl_index; |
| 145 | if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) { |
| 146 | netif.etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl))); |
| 147 | NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress); |
| 148 | } |
| 149 | else if (sdl->sdl_type != IFT_LOOP) { |
| 150 | NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled interface type " |
| 151 | << sdl->sdl_type); |
| 152 | } |
| 153 | break; |
| 154 | } |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | if (netif.isBroadcastCapable() && ifa->ifa_broadaddr != nullptr) { |
| 159 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr); |
| 160 | char address[INET_ADDRSTRLEN]; |
| 161 | if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) { |
| 162 | netif.broadcastAddress = address_v4::from_string(address); |
| 163 | NFD_LOG_TRACE(ifname << ": added IPv4 broadcast address " << address); |
| 164 | } |
| 165 | else |
| 166 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) for broadaddr failed: " << strerror(errno)); |
| 167 | } |
| 168 | } |
| 169 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 170 | ::freeifaddrs(ifa_list); |
| 171 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 172 | std::vector<NetworkInterfaceInfo> v; |
| 173 | v.reserve(ifmap.size()); |
| 174 | for (auto&& element : ifmap) { |
| 175 | v.push_back(element.second); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 178 | return v; |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 179 | #else |
| 180 | return {}; |
| 181 | #endif // HAVE_IFADDRS_H |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } // namespace nfd |