Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 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" |
Junxiao Shi | 83be1da | 2017-06-30 13:37:37 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/net/ethernet.hpp> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/net/network-interface.hpp> |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 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 { |
Junxiao Shi | 83be1da | 2017-06-30 13:37:37 +0000 | [diff] [blame] | 38 | using namespace ndn::ethernet; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 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: |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 46 | bool |
| 47 | isLoopback() const; |
| 48 | |
| 49 | bool |
| 50 | isMulticastCapable() const; |
| 51 | |
| 52 | bool |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 53 | isBroadcastCapable() const; |
| 54 | |
| 55 | bool |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 56 | isUp() const; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 57 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 58 | /** \brief Export to ndn::net::NetworkInterface |
| 59 | * \warning This is a temporary adaptation during the transition from NetworkInterfaceInfo to |
| 60 | * ndn::net::NetworkInterface. The return value carries the information in this instance |
| 61 | * and will not track changes in the actual network interface or emit signals. |
| 62 | */ |
| 63 | shared_ptr<ndn::net::NetworkInterface> |
| 64 | asNetworkInterface() const; |
| 65 | |
| 66 | public: |
| 67 | int index; |
| 68 | std::string name; |
| 69 | ethernet::Address etherAddress; |
| 70 | std::vector<boost::asio::ip::address_v4> ipv4Addresses; |
| 71 | std::vector<boost::asio::ip::address_v6> ipv6Addresses; |
| 72 | boost::asio::ip::address_v4 broadcastAddress; |
| 73 | unsigned int flags; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | inline bool |
| 77 | NetworkInterfaceInfo::isLoopback() const |
| 78 | { |
| 79 | return (flags & IFF_LOOPBACK) != 0; |
| 80 | } |
| 81 | |
| 82 | inline bool |
| 83 | NetworkInterfaceInfo::isMulticastCapable() const |
| 84 | { |
| 85 | return (flags & IFF_MULTICAST) != 0; |
| 86 | } |
| 87 | |
| 88 | inline bool |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 89 | NetworkInterfaceInfo::isBroadcastCapable() const |
| 90 | { |
| 91 | return (flags & IFF_BROADCAST) != 0; |
| 92 | } |
| 93 | |
| 94 | inline bool |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 95 | NetworkInterfaceInfo::isUp() const |
| 96 | { |
| 97 | return (flags & IFF_UP) != 0; |
| 98 | } |
| 99 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 100 | /** \brief List configured network interfaces on the system and their info |
Alexander Afanasyev | 7e14816 | 2014-12-15 12:05:37 -0800 | [diff] [blame] | 101 | * \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] | 102 | */ |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 103 | std::vector<NetworkInterfaceInfo> |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 104 | listNetworkInterfaces(); |
| 105 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 106 | #ifdef WITH_TESTS |
| 107 | /** \brief Set a list of network interfaces to be returned by subsequent listNetworkInterfaces call |
| 108 | * \note To reset to normal behavior, use `setDebugNetworkInterfaces(nullptr);` |
| 109 | */ |
| 110 | void |
| 111 | setDebugNetworkInterfaces(shared_ptr<std::vector<NetworkInterfaceInfo>> interfaces); |
| 112 | #endif |
| 113 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 114 | } // namespace nfd |
| 115 | |
| 116 | #endif // NFD_CORE_NETWORK_INTERFACE_HPP |