diff --git a/daemon/core/network-interface.cpp b/daemon/core/network-interface.cpp new file mode 100644 index 00000000..65a690c1 --- /dev/null +++ b/daemon/core/network-interface.cpp @@ -0,0 +1,109 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "network-interface.hpp" + +#include + +#include // for inet_ntop() +#include // for struct sockaddr_in{,6} +#include // for getifaddrs() + +#if defined(__linux__) +#include // for ARPHRD_* constants +#include // for struct sockaddr_ll +#elif defined(__APPLE__) +#include // for struct sockaddr_dl +#else +#error Platform not supported +#endif + +namespace nfd { + +NFD_LOG_INIT("NetworkInterfaceInfo") + +std::list< shared_ptr > +listNetworkInterfaces() +{ + typedef std::map< std::string, shared_ptr > InterfacesMap; + InterfacesMap ifmap; + + ifaddrs* ifa_list; + if (::getifaddrs(&ifa_list) < 0) + throw std::runtime_error("getifaddrs() failed"); + + for (ifaddrs* ifa = ifa_list; ifa != 0; ifa = ifa->ifa_next) + { + shared_ptr netif; + std::string ifname(ifa->ifa_name); + InterfacesMap::iterator i = ifmap.find(ifname); + if (i == ifmap.end()) + { + netif = make_shared(); + netif->name = ifname; + netif->flags = ifa->ifa_flags; + ifmap[ifname] = netif; + } + else + { + netif = i->second; + } + + if (!ifa->ifa_addr) + continue; + + switch (ifa->ifa_addr->sa_family) + { + case AF_INET: { + const sockaddr_in* sin = reinterpret_cast(ifa->ifa_addr); + char address[INET_ADDRSTRLEN]; + if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) + netif->ipv4Addresses.push_back(boost::asio::ip::address_v4::from_string(address)); + else + NFD_LOG_WARN("inet_ntop() failed on " << ifname); + } + break; + case AF_INET6: { + const sockaddr_in6* sin6 = reinterpret_cast(ifa->ifa_addr); + char address[INET6_ADDRSTRLEN]; + if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) + netif->ipv6Addresses.push_back(boost::asio::ip::address_v6::from_string(address)); + else + NFD_LOG_WARN("inet_ntop() failed on " << ifname); + } + break; +#if defined(__linux__) + case AF_PACKET: { + const sockaddr_ll* sll = reinterpret_cast(ifa->ifa_addr); + netif->index = sll->sll_ifindex; + if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) + netif->etherAddress = ethernet::Address(sll->sll_addr); + else if (sll->sll_hatype != ARPHRD_LOOPBACK) + NFD_LOG_WARN("Unrecognized hardware address on " << ifname); + } + break; +#elif defined(__APPLE__) + case AF_LINK: { + const sockaddr_dl* sdl = reinterpret_cast(ifa->ifa_addr); + netif->index = sdl->sdl_index; + netif->etherAddress = ethernet::Address(reinterpret_cast(LLADDR(sdl))); + } + break; +#endif + } + } + + ::freeifaddrs(ifa_list); + + std::list< shared_ptr > list; + BOOST_FOREACH(InterfacesMap::value_type elem, ifmap) { + list.push_back(elem.second); + } + + return list; +} + +} // namespace nfd diff --git a/daemon/core/network-interface.hpp b/daemon/core/network-interface.hpp new file mode 100644 index 00000000..d02171ab --- /dev/null +++ b/daemon/core/network-interface.hpp @@ -0,0 +1,60 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_CORE_NETWORK_INTERFACE_HPP +#define NFD_CORE_NETWORK_INTERFACE_HPP + +#include "common.hpp" +#include "face/ethernet.hpp" + +#include + +namespace nfd { + +class NetworkInterfaceInfo +{ +public: + int index; + std::string name; + ethernet::Address etherAddress; + std::vector ipv4Addresses; + std::vector ipv6Addresses; + unsigned int flags; + + bool + isLoopback() const; + + bool + isMulticastCapable() const; + + bool + isUp() const; +}; + +inline bool +NetworkInterfaceInfo::isLoopback() const +{ + return (flags & IFF_LOOPBACK) != 0; +} + +inline bool +NetworkInterfaceInfo::isMulticastCapable() const +{ + return (flags & IFF_MULTICAST) != 0; +} + +inline bool +NetworkInterfaceInfo::isUp() const +{ + return (flags & IFF_UP) != 0; +} + +std::list< shared_ptr > +listNetworkInterfaces(); + +} // namespace nfd + +#endif // NFD_CORE_NETWORK_INTERFACE_HPP diff --git a/daemon/face/ethernet.hpp b/daemon/face/ethernet.hpp index 66ee715a..f12b3fb3 100644 --- a/daemon/face/ethernet.hpp +++ b/daemon/face/ethernet.hpp @@ -52,6 +52,10 @@ public: bool isMulticast() const; + /// True if this is a null address (00-00-00-00-00-00) + bool + isNull() const; + std::string toString(char sep = '-') const; }; @@ -114,6 +118,13 @@ Address::isMulticast() const return (elems[0] & 1) != 0; } +inline bool +Address::isNull() const +{ + return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 && + elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0; +} + inline std::string Address::toString(char sep) const { diff --git a/tests/core/network-interface.cpp b/tests/core/network-interface.cpp new file mode 100644 index 00000000..bdfd21fb --- /dev/null +++ b/tests/core/network-interface.cpp @@ -0,0 +1,39 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "core/network-interface.hpp" +#include "tests/test-common.hpp" + +#include + +namespace nfd { +namespace tests { + +BOOST_FIXTURE_TEST_SUITE(CoreNetworkInterface, BaseFixture) + +BOOST_AUTO_TEST_CASE(ListNetworkInterfaces) +{ + std::list< shared_ptr > netifs; + BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces()); + + BOOST_FOREACH(shared_ptr netif, netifs) + { + BOOST_TEST_MESSAGE(netif->index << ": " << netif->name); + BOOST_TEST_MESSAGE("\tether " << netif->etherAddress); + BOOST_FOREACH(boost::asio::ip::address_v4 address, netif->ipv4Addresses) + BOOST_TEST_MESSAGE("\tinet " << address); + BOOST_FOREACH(boost::asio::ip::address_v6 address, netif->ipv6Addresses) + BOOST_TEST_MESSAGE("\tinet6 " << address); + BOOST_TEST_MESSAGE("\tloopback : " << netif->isLoopback()); + BOOST_TEST_MESSAGE("\tmulticast : " << netif->isMulticastCapable()); + BOOST_TEST_MESSAGE("\tup : " << netif->isUp()); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace tests +} // namespace nfd