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 | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 24 | */ |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_CORE_NETWORK_INTERFACE_HPP |
| 27 | #define NFD_CORE_NETWORK_INTERFACE_HPP |
| 28 | |
| 29 | #include "common.hpp" |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 30 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/util/ethernet.hpp> |
| 32 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 33 | #include <net/if.h> |
| 34 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 35 | namespace nfd { |
| 36 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 37 | namespace ethernet { |
| 38 | using namespace ndn::util::ethernet; |
| 39 | } // namespace ethernet |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 40 | |
| 41 | /** \brief contains information about a network interface |
| 42 | */ |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 43 | class NetworkInterfaceInfo |
| 44 | { |
| 45 | public: |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 46 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 47 | int index; |
| 48 | std::string name; |
| 49 | ethernet::Address etherAddress; |
| 50 | std::vector<boost::asio::ip::address_v4> ipv4Addresses; |
| 51 | std::vector<boost::asio::ip::address_v6> ipv6Addresses; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 52 | boost::asio::ip::address_v4 broadcastAddress; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 53 | unsigned int flags; |
| 54 | |
| 55 | bool |
| 56 | isLoopback() const; |
| 57 | |
| 58 | bool |
| 59 | isMulticastCapable() const; |
| 60 | |
| 61 | bool |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | isBroadcastCapable() const; |
| 63 | |
| 64 | bool |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 65 | isUp() const; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 66 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | inline bool |
| 70 | NetworkInterfaceInfo::isLoopback() const |
| 71 | { |
| 72 | return (flags & IFF_LOOPBACK) != 0; |
| 73 | } |
| 74 | |
| 75 | inline bool |
| 76 | NetworkInterfaceInfo::isMulticastCapable() const |
| 77 | { |
| 78 | return (flags & IFF_MULTICAST) != 0; |
| 79 | } |
| 80 | |
| 81 | inline bool |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 82 | NetworkInterfaceInfo::isBroadcastCapable() const |
| 83 | { |
| 84 | return (flags & IFF_BROADCAST) != 0; |
| 85 | } |
| 86 | |
| 87 | inline bool |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 88 | NetworkInterfaceInfo::isUp() const |
| 89 | { |
| 90 | return (flags & IFF_UP) != 0; |
| 91 | } |
| 92 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 93 | /** \brief List configured network interfaces on the system and their info |
Alexander Afanasyev | 7e14816 | 2014-12-15 12:05:37 -0800 | [diff] [blame] | 94 | * \warning invalid IP addresses (e.g., 0.0.0.0) may be returned in some environments |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 95 | */ |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 96 | std::vector<NetworkInterfaceInfo> |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 97 | listNetworkInterfaces(); |
| 98 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 99 | #ifdef WITH_TESTS |
| 100 | /** \brief Set a list of network interfaces to be returned by subsequent listNetworkInterfaces call |
| 101 | * \note To reset to normal behavior, use `setDebugNetworkInterfaces(nullptr);` |
| 102 | */ |
| 103 | void |
| 104 | setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces); |
| 105 | #endif |
| 106 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 107 | } // namespace nfd |
| 108 | |
| 109 | #endif // NFD_CORE_NETWORK_INTERFACE_HPP |