blob: ad9485ebfd05cc23038ba8c6b6b8655d38d9091f [file] [log] [blame]
Davide Pesavento248d6bd2014-03-09 10:24:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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"
Junxiao Shi0d82d042017-07-07 06:15:27 +000028#include <ndn-cxx/net/network-monitor-stub.hpp>
Davide Pesavento248d6bd2014-03-09 10:24:08 +010029
Davide Pesaventob499a602014-11-18 22:36:56 +010030#include <cerrno>
31#include <cstring>
32#include <type_traits>
33#include <unordered_map>
Davide Pesavento248d6bd2014-03-09 10:24:08 +010034
Alexander Afanasyev49343f62015-01-26 21:58:07 -080035#ifdef HAVE_IFADDRS_H
36#include <ifaddrs.h> // for getifaddrs()
37
Davide Pesavento248d6bd2014-03-09 10:24:08 +010038#include <arpa/inet.h> // for inet_ntop()
39#include <netinet/in.h> // for struct sockaddr_in{,6}
Davide Pesavento248d6bd2014-03-09 10:24:08 +010040
41#if defined(__linux__)
42#include <net/if_arp.h> // for ARPHRD_* constants
43#include <netpacket/packet.h> // for struct sockaddr_ll
Alexander Afanasyev500b2532014-03-31 12:29:25 -070044#elif defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesavento248d6bd2014-03-09 10:24:08 +010045#include <net/if_dl.h> // for struct sockaddr_dl
Davide Pesaventob499a602014-11-18 22:36:56 +010046#include <net/if_types.h> // for IFT_* constants
Davide Pesavento248d6bd2014-03-09 10:24:08 +010047#endif
48
Alexander Afanasyev49343f62015-01-26 21:58:07 -080049#endif // HAVE_IFADDRS_H
50
51
Davide Pesavento1bdef282014-04-08 20:59:50 +020052NFD_LOG_INIT("NetworkInterfaceInfo");
Davide Pesavento248d6bd2014-03-09 10:24:08 +010053
Davide Pesavento1bdef282014-04-08 20:59:50 +020054namespace nfd {
Davide Pesavento248d6bd2014-03-09 10:24:08 +010055
Davide Pesaventob499a602014-11-18 22:36:56 +010056static_assert(std::is_standard_layout<NetworkInterfaceInfo>::value,
57 "NetworkInterfaceInfo must be a standard layout type");
58#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
59static_assert(std::is_default_constructible<NetworkInterfaceInfo>::value,
60 "NetworkInterfaceInfo must provide a default constructor");
61#endif
62
Junxiao Shi0d82d042017-07-07 06:15:27 +000063shared_ptr<ndn::net::NetworkInterface>
64NetworkInterfaceInfo::asNetworkInterface() const
65{
66 using namespace ndn::net;
67
68 shared_ptr<NetworkInterface> netif = NetworkMonitorStub::makeNetworkInterface();
69 netif->setIndex(this->index);
70 netif->setName(this->name);
71 netif->setEthernetAddress(this->etherAddress);
72 netif->setFlags(this->flags);
73
74 for (const auto& a4 : this->ipv4Addresses) {
75 netif->addNetworkAddress(NetworkAddress(AddressFamily::V4, a4, this->broadcastAddress,
76 32, AddressScope::GLOBAL, 0));
77 }
78 for (const auto& a6 : this->ipv6Addresses) {
79 netif->addNetworkAddress(NetworkAddress(AddressFamily::V6, a6, {},
80 128, AddressScope::GLOBAL, 0));
81 }
82
83 return netif;
84}
85
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080086#ifdef WITH_TESTS
87static shared_ptr<std::vector<NetworkInterfaceInfo>> s_debugNetworkInterfaces = nullptr;
88
89void
90setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces)
91{
92 s_debugNetworkInterfaces = interfaces;
93}
94#endif
95
Davide Pesaventob499a602014-11-18 22:36:56 +010096std::vector<NetworkInterfaceInfo>
Davide Pesavento248d6bd2014-03-09 10:24:08 +010097listNetworkInterfaces()
98{
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080099#ifdef WITH_TESTS
100 if (s_debugNetworkInterfaces != nullptr) {
101 return *s_debugNetworkInterfaces;
102 }
103#endif
104
Alexander Afanasyev49343f62015-01-26 21:58:07 -0800105#ifdef HAVE_IFADDRS_H
Davide Pesaventob499a602014-11-18 22:36:56 +0100106 using namespace boost::asio::ip;
107 using std::strerror;
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100108
Davide Pesaventob499a602014-11-18 22:36:56 +0100109 std::unordered_map<std::string, NetworkInterfaceInfo> ifmap;
110 ifaddrs* ifa_list = nullptr;
111
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100112 if (::getifaddrs(&ifa_list) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700113 BOOST_THROW_EXCEPTION(std::runtime_error(std::string("getifaddrs() failed: ") +
114 strerror(errno)));
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100115
Davide Pesaventob499a602014-11-18 22:36:56 +0100116 for (ifaddrs* ifa = ifa_list; ifa != nullptr; ifa = ifa->ifa_next) {
117 std::string ifname(ifa->ifa_name);
118 NetworkInterfaceInfo& netif = ifmap[ifname];
119 netif.name = ifa->ifa_name;
120 netif.flags = ifa->ifa_flags;
121
122 if (ifa->ifa_addr == nullptr)
123 continue;
124
125 switch (ifa->ifa_addr->sa_family) {
126
Junxiao Shi0d82d042017-07-07 06:15:27 +0000127 case AF_INET: {
128 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
129 char address[INET_ADDRSTRLEN];
130 if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) {
131 netif.ipv4Addresses.push_back(address_v4::from_string(address));
132 NFD_LOG_TRACE(ifname << ": added IPv4 address " << address);
133 }
134 else
135 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) failed: " << strerror(errno));
136 break;
Davide Pesaventob499a602014-11-18 22:36:56 +0100137 }
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100138
Junxiao Shi0d82d042017-07-07 06:15:27 +0000139 case AF_INET6: {
140 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
141 char address[INET6_ADDRSTRLEN];
142 if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) {
143 netif.ipv6Addresses.push_back(address_v6::from_string(address));
144 NFD_LOG_TRACE(ifname << ": added IPv6 address " << address);
145 }
146 else
147 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET6) failed: " << strerror(errno));
148 break;
Davide Pesaventob499a602014-11-18 22:36:56 +0100149 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100150
151#if defined(__linux__)
Junxiao Shi0d82d042017-07-07 06:15:27 +0000152 case AF_PACKET: {
153 const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr);
154 netif.index = sll->sll_ifindex;
155 if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) {
156 netif.etherAddress = ethernet::Address(sll->sll_addr);
157 NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress);
158 }
159 else if (sll->sll_hatype != ARPHRD_LOOPBACK) {
160 NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled hardware type "
161 << sll->sll_hatype);
162 }
163 break;
Davide Pesaventob499a602014-11-18 22:36:56 +0100164 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100165
166#elif defined(__APPLE__) || defined(__FreeBSD__)
Junxiao Shi0d82d042017-07-07 06:15:27 +0000167 case AF_LINK: {
168 const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
169 netif.index = sdl->sdl_index;
170 if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ethernet::ADDR_LEN) {
171 netif.etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)));
172 NFD_LOG_TRACE(ifname << ": added Ethernet address " << netif.etherAddress);
173 }
174 else if (sdl->sdl_type != IFT_LOOP) {
175 NFD_LOG_DEBUG(ifname << ": ignoring link-layer address for unhandled interface type "
176 << sdl->sdl_type);
177 }
178 break;
Davide Pesaventob499a602014-11-18 22:36:56 +0100179 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100180#endif
181 }
182
183 if (netif.isBroadcastCapable() && ifa->ifa_broadaddr != nullptr) {
184 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
185 char address[INET_ADDRSTRLEN];
186 if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) {
187 netif.broadcastAddress = address_v4::from_string(address);
188 NFD_LOG_TRACE(ifname << ": added IPv4 broadcast address " << address);
189 }
190 else
191 NFD_LOG_WARN(ifname << ": inet_ntop(AF_INET) for broadaddr failed: " << strerror(errno));
192 }
193 }
194
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100195 ::freeifaddrs(ifa_list);
196
Davide Pesaventob499a602014-11-18 22:36:56 +0100197 std::vector<NetworkInterfaceInfo> v;
198 v.reserve(ifmap.size());
199 for (auto&& element : ifmap) {
200 v.push_back(element.second);
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100201 }
202
Davide Pesaventob499a602014-11-18 22:36:56 +0100203 return v;
Alexander Afanasyev49343f62015-01-26 21:58:07 -0800204#else
205 return {};
206#endif // HAVE_IFADDRS_H
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100207}
208
209} // namespace nfd