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