Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 | |
| 34 | #include <arpa/inet.h> // for inet_ntop() |
| 35 | #include <netinet/in.h> // for struct sockaddr_in{,6} |
| 36 | #include <ifaddrs.h> // for getifaddrs() |
| 37 | |
| 38 | #if defined(__linux__) |
| 39 | #include <net/if_arp.h> // for ARPHRD_* constants |
| 40 | #include <netpacket/packet.h> // for struct sockaddr_ll |
Alexander Afanasyev | 500b253 | 2014-03-31 12:29:25 -0700 | [diff] [blame] | 41 | #elif defined(__APPLE__) || defined(__FreeBSD__) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 42 | #include <net/if_dl.h> // for struct sockaddr_dl |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 43 | #include <net/if_types.h> // for IFT_* constants |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 46 | NFD_LOG_INIT("NetworkInterfaceInfo"); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 47 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 48 | namespace nfd { |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 49 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 50 | static_assert(std::is_standard_layout<NetworkInterfaceInfo>::value, |
| 51 | "NetworkInterfaceInfo must be a standard layout type"); |
| 52 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 53 | static_assert(std::is_default_constructible<NetworkInterfaceInfo>::value, |
| 54 | "NetworkInterfaceInfo must provide a default constructor"); |
| 55 | #endif |
| 56 | |
| 57 | std::vector<NetworkInterfaceInfo> |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 58 | listNetworkInterfaces() |
| 59 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 60 | using namespace boost::asio::ip; |
| 61 | using std::strerror; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 62 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 63 | std::unordered_map<std::string, NetworkInterfaceInfo> ifmap; |
| 64 | ifaddrs* ifa_list = nullptr; |
| 65 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 66 | if (::getifaddrs(&ifa_list) < 0) |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 67 | throw std::runtime_error(std::string("getifaddrs() failed: ") + strerror(errno)); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 68 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 69 | for (ifaddrs* ifa = ifa_list; ifa != nullptr; ifa = ifa->ifa_next) { |
| 70 | std::string ifname(ifa->ifa_name); |
| 71 | NetworkInterfaceInfo& netif = ifmap[ifname]; |
| 72 | netif.name = ifa->ifa_name; |
| 73 | netif.flags = ifa->ifa_flags; |
| 74 | |
| 75 | if (ifa->ifa_addr == nullptr) |
| 76 | continue; |
| 77 | |
| 78 | switch (ifa->ifa_addr->sa_family) { |
| 79 | |
| 80 | case AF_INET: { |
| 81 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr); |
| 82 | char address[INET_ADDRSTRLEN]; |
| 83 | if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) { |
| 84 | netif.ipv4Addresses.push_back(address_v4::from_string(address)); |
| 85 | NFD_LOG_TRACE(ifname << ": added IPv4 address " << address); |
| 86 | } |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 87 | else |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 88 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) failed: " << strerror(errno)); |
| 89 | break; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 92 | case AF_INET6: { |
| 93 | const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); |
| 94 | char address[INET6_ADDRSTRLEN]; |
| 95 | if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) { |
| 96 | netif.ipv6Addresses.push_back(address_v6::from_string(address)); |
| 97 | NFD_LOG_TRACE(ifname << ": added IPv6 address " << address); |
| 98 | } |
| 99 | else |
| 100 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET6) failed: " << strerror(errno)); |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | #if defined(__linux__) |
| 105 | case AF_PACKET: { |
| 106 | const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr); |
| 107 | netif.index = sll->sll_ifindex; |
| 108 | if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) { |
| 109 | netif.etherAddress = ethernet::Address(sll->sll_addr); |
| 110 | NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress); |
| 111 | } |
| 112 | else if (sll->sll_hatype != ARPHRD_LOOPBACK) { |
| 113 | NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled hardware type " |
| 114 | << sll->sll_hatype); |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | #elif defined(__APPLE__) || defined(__FreeBSD__) |
| 120 | case AF_LINK: { |
| 121 | const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr); |
| 122 | netif.index = sdl->sdl_index; |
| 123 | if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) { |
| 124 | netif.etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl))); |
| 125 | NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress); |
| 126 | } |
| 127 | else if (sdl->sdl_type != IFT_LOOP) { |
| 128 | NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled interface type " |
| 129 | << sdl->sdl_type); |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | if (netif.isBroadcastCapable() && ifa->ifa_broadaddr != nullptr) { |
| 137 | const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr); |
| 138 | char address[INET_ADDRSTRLEN]; |
| 139 | if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) { |
| 140 | netif.broadcastAddress = address_v4::from_string(address); |
| 141 | NFD_LOG_TRACE(ifname << ": added IPv4 broadcast address " << address); |
| 142 | } |
| 143 | else |
| 144 | NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) for broadaddr failed: " << strerror(errno)); |
| 145 | } |
| 146 | } |
| 147 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 148 | ::freeifaddrs(ifa_list); |
| 149 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 150 | std::vector<NetworkInterfaceInfo> v; |
| 151 | v.reserve(ifmap.size()); |
| 152 | for (auto&& element : ifmap) { |
| 153 | v.push_back(element.second); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 156 | return v; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace nfd |