blob: 1aad63e34ec3bb4b0379a069b0c3652e78103877 [file] [log] [blame]
Davide Pesavento248d6bd2014-03-09 10:24:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob499a602014-11-18 22:36:56 +01003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Pesaventob499a602014-11-18 22:36:56 +010024 */
Davide Pesavento248d6bd2014-03-09 10:24:08 +010025
26#include "network-interface.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Davide Pesavento248d6bd2014-03-09 10:24:08 +010028
Davide Pesaventob499a602014-11-18 22:36:56 +010029#include <cerrno>
30#include <cstring>
31#include <type_traits>
32#include <unordered_map>
Davide Pesavento248d6bd2014-03-09 10:24:08 +010033
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 Afanasyev500b2532014-03-31 12:29:25 -070041#elif defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesavento248d6bd2014-03-09 10:24:08 +010042#include <net/if_dl.h> // for struct sockaddr_dl
Davide Pesaventob499a602014-11-18 22:36:56 +010043#include <net/if_types.h> // for IFT_* constants
Davide Pesavento248d6bd2014-03-09 10:24:08 +010044#endif
45
Davide Pesavento1bdef282014-04-08 20:59:50 +020046NFD_LOG_INIT("NetworkInterfaceInfo");
Davide Pesavento248d6bd2014-03-09 10:24:08 +010047
Davide Pesavento1bdef282014-04-08 20:59:50 +020048namespace nfd {
Davide Pesavento248d6bd2014-03-09 10:24:08 +010049
Davide Pesaventob499a602014-11-18 22:36:56 +010050static_assert(std::is_standard_layout<NetworkInterfaceInfo>::value,
51 "NetworkInterfaceInfo must be a standard layout type");
52#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
53static_assert(std::is_default_constructible<NetworkInterfaceInfo>::value,
54 "NetworkInterfaceInfo must provide a default constructor");
55#endif
56
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080057#ifdef WITH_TESTS
58static shared_ptr<std::vector<NetworkInterfaceInfo>> s_debugNetworkInterfaces = nullptr;
59
60void
61setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces)
62{
63 s_debugNetworkInterfaces = interfaces;
64}
65#endif
66
Davide Pesaventob499a602014-11-18 22:36:56 +010067std::vector<NetworkInterfaceInfo>
Davide Pesavento248d6bd2014-03-09 10:24:08 +010068listNetworkInterfaces()
69{
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080070#ifdef WITH_TESTS
71 if (s_debugNetworkInterfaces != nullptr) {
72 return *s_debugNetworkInterfaces;
73 }
74#endif
75
Davide Pesaventob499a602014-11-18 22:36:56 +010076 using namespace boost::asio::ip;
77 using std::strerror;
Davide Pesavento248d6bd2014-03-09 10:24:08 +010078
Davide Pesaventob499a602014-11-18 22:36:56 +010079 std::unordered_map<std::string, NetworkInterfaceInfo> ifmap;
80 ifaddrs* ifa_list = nullptr;
81
Davide Pesavento248d6bd2014-03-09 10:24:08 +010082 if (::getifaddrs(&ifa_list) < 0)
Davide Pesaventob499a602014-11-18 22:36:56 +010083 throw std::runtime_error(std::string("getifaddrs() failed: ") + strerror(errno));
Davide Pesavento248d6bd2014-03-09 10:24:08 +010084
Davide Pesaventob499a602014-11-18 22:36:56 +010085 for (ifaddrs* ifa = ifa_list; ifa != nullptr; ifa = ifa->ifa_next) {
86 std::string ifname(ifa->ifa_name);
87 NetworkInterfaceInfo& netif = ifmap[ifname];
88 netif.name = ifa->ifa_name;
89 netif.flags = ifa->ifa_flags;
90
91 if (ifa->ifa_addr == nullptr)
92 continue;
93
94 switch (ifa->ifa_addr->sa_family) {
95
96 case AF_INET: {
97 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
98 char address[INET_ADDRSTRLEN];
99 if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) {
100 netif.ipv4Addresses.push_back(address_v4::from_string(address));
101 NFD_LOG_TRACE(ifname << ": added IPv4 address " << address);
102 }
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100103 else
Davide Pesaventob499a602014-11-18 22:36:56 +0100104 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) failed: " << strerror(errno));
105 break;
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100106 }
107
Davide Pesaventob499a602014-11-18 22:36:56 +0100108 case AF_INET6: {
109 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
110 char address[INET6_ADDRSTRLEN];
111 if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) {
112 netif.ipv6Addresses.push_back(address_v6::from_string(address));
113 NFD_LOG_TRACE(ifname << ": added IPv6 address " << address);
114 }
115 else
116 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET6) failed: " << strerror(errno));
117 break;
118 }
119
120#if defined(__linux__)
121 case AF_PACKET: {
122 const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr);
123 netif.index = sll->sll_ifindex;
124 if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) {
125 netif.etherAddress = ethernet::Address(sll->sll_addr);
126 NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress);
127 }
128 else if (sll->sll_hatype != ARPHRD_LOOPBACK) {
129 NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled hardware type "
130 << sll->sll_hatype);
131 }
132 break;
133 }
134
135#elif defined(__APPLE__) || defined(__FreeBSD__)
136 case AF_LINK: {
137 const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
138 netif.index = sdl->sdl_index;
139 if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) {
140 netif.etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)));
141 NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress);
142 }
143 else if (sdl->sdl_type != IFT_LOOP) {
144 NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled interface type "
145 << sdl->sdl_type);
146 }
147 break;
148 }
149#endif
150 }
151
152 if (netif.isBroadcastCapable() && ifa->ifa_broadaddr != nullptr) {
153 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
154 char address[INET_ADDRSTRLEN];
155 if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) {
156 netif.broadcastAddress = address_v4::from_string(address);
157 NFD_LOG_TRACE(ifname << ": added IPv4 broadcast address " << address);
158 }
159 else
160 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) for broadaddr failed: " << strerror(errno));
161 }
162 }
163
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100164 ::freeifaddrs(ifa_list);
165
Davide Pesaventob499a602014-11-18 22:36:56 +0100166 std::vector<NetworkInterfaceInfo> v;
167 v.reserve(ifmap.size());
168 for (auto&& element : ifmap) {
169 v.push_back(element.second);
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100170 }
171
Davide Pesaventob499a602014-11-18 22:36:56 +0100172 return v;
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100173}
174
175} // namespace nfd