blob: 7319e97ad32311737768fc8619674c69dd5f022d [file] [log] [blame]
Davide Pesavento248d6bd2014-03-09 10:24:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "network-interface.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06008#include "core/logger.hpp"
Davide Pesavento248d6bd2014-03-09 10:24:08 +01009
10#include <boost/foreach.hpp>
11
12#include <arpa/inet.h> // for inet_ntop()
13#include <netinet/in.h> // for struct sockaddr_in{,6}
14#include <ifaddrs.h> // for getifaddrs()
15
16#if defined(__linux__)
17#include <net/if_arp.h> // for ARPHRD_* constants
18#include <netpacket/packet.h> // for struct sockaddr_ll
19#elif defined(__APPLE__)
20#include <net/if_dl.h> // for struct sockaddr_dl
21#else
22#error Platform not supported
23#endif
24
25namespace nfd {
26
27NFD_LOG_INIT("NetworkInterfaceInfo")
28
29std::list< shared_ptr<NetworkInterfaceInfo> >
30listNetworkInterfaces()
31{
32 typedef std::map< std::string, shared_ptr<NetworkInterfaceInfo> > InterfacesMap;
33 InterfacesMap ifmap;
34
35 ifaddrs* ifa_list;
36 if (::getifaddrs(&ifa_list) < 0)
37 throw std::runtime_error("getifaddrs() failed");
38
39 for (ifaddrs* ifa = ifa_list; ifa != 0; ifa = ifa->ifa_next)
40 {
41 shared_ptr<NetworkInterfaceInfo> netif;
42 std::string ifname(ifa->ifa_name);
43 InterfacesMap::iterator i = ifmap.find(ifname);
44 if (i == ifmap.end())
45 {
46 netif = make_shared<NetworkInterfaceInfo>();
47 netif->name = ifname;
48 netif->flags = ifa->ifa_flags;
49 ifmap[ifname] = netif;
50 }
51 else
52 {
53 netif = i->second;
54 }
55
56 if (!ifa->ifa_addr)
57 continue;
58
59 switch (ifa->ifa_addr->sa_family)
60 {
61 case AF_INET: {
62 const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
63 char address[INET_ADDRSTRLEN];
64 if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address)))
65 netif->ipv4Addresses.push_back(boost::asio::ip::address_v4::from_string(address));
66 else
67 NFD_LOG_WARN("inet_ntop() failed on " << ifname);
68 }
69 break;
70 case AF_INET6: {
71 const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
72 char address[INET6_ADDRSTRLEN];
73 if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address)))
74 netif->ipv6Addresses.push_back(boost::asio::ip::address_v6::from_string(address));
75 else
76 NFD_LOG_WARN("inet_ntop() failed on " << ifname);
77 }
78 break;
79#if defined(__linux__)
80 case AF_PACKET: {
81 const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr);
82 netif->index = sll->sll_ifindex;
83 if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN)
84 netif->etherAddress = ethernet::Address(sll->sll_addr);
85 else if (sll->sll_hatype != ARPHRD_LOOPBACK)
86 NFD_LOG_WARN("Unrecognized hardware address on " << ifname);
87 }
88 break;
89#elif defined(__APPLE__)
90 case AF_LINK: {
91 const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
92 netif->index = sdl->sdl_index;
93 netif->etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)));
94 }
95 break;
96#endif
97 }
98 }
99
100 ::freeifaddrs(ifa_list);
101
102 std::list< shared_ptr<NetworkInterfaceInfo> > list;
103 BOOST_FOREACH(InterfacesMap::value_type elem, ifmap) {
104 list.push_back(elem.second);
105 }
106
107 return list;
108}
109
110} // namespace nfd