Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
| 22 | */ |
| 23 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 24 | #ifndef NDN_NET_NETWORK_INTERFACE_HPP |
| 25 | #define NDN_NET_NETWORK_INTERFACE_HPP |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 26 | |
| 27 | #include "ethernet.hpp" |
| 28 | #include "network-address.hpp" |
| 29 | #include "network-monitor.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 30 | #include "../util/signal.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 31 | |
| 32 | #include <set> |
| 33 | |
| 34 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 35 | namespace net { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 36 | |
| 37 | /** @brief Indicates the hardware type of a network interface |
| 38 | */ |
| 39 | enum class InterfaceType { |
| 40 | UNKNOWN, |
| 41 | LOOPBACK, |
| 42 | ETHERNET, |
| 43 | // we do not support anything else for now |
| 44 | }; |
| 45 | |
| 46 | std::ostream& |
| 47 | operator<<(std::ostream& os, InterfaceType type); |
| 48 | |
| 49 | /** @brief Indicates the state of a network interface |
| 50 | */ |
| 51 | enum class InterfaceState { |
| 52 | UNKNOWN, ///< interface is in an unknown state |
| 53 | DOWN, ///< interface is administratively down |
| 54 | NO_CARRIER, ///< interface is administratively up but has no carrier |
| 55 | DORMANT, ///< interface has a carrier but it cannot send or receive normal user traffic yet |
| 56 | RUNNING, ///< interface can be used to send and receive packets |
| 57 | }; |
| 58 | |
| 59 | std::ostream& |
| 60 | operator<<(std::ostream& os, InterfaceState state); |
| 61 | |
| 62 | /** |
| 63 | * @brief Represents one network interface attached to the host. |
| 64 | * |
| 65 | * Each network interface has a unique index, a name, and a set of flags indicating its |
| 66 | * capabilities and current state. It may contain one hardware (Ethernet) address, and |
| 67 | * zero or more network-layer (IP) addresses. Specific signals are emitted when the |
| 68 | * interface data change. |
| 69 | */ |
| 70 | class NetworkInterface |
| 71 | { |
| 72 | public: // signals |
| 73 | /** @brief Fires when interface state changes |
| 74 | */ |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 75 | util::Signal<NetworkInterface, InterfaceState /*old*/, InterfaceState /*new*/> onStateChanged; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 76 | |
| 77 | /** @brief Fires when interface mtu changes |
| 78 | */ |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 79 | util::Signal<NetworkInterface, uint32_t /*old*/, uint32_t /*new*/> onMtuChanged; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 80 | |
| 81 | /** @brief Fires when a network-layer address is added to the interface |
| 82 | */ |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 83 | util::Signal<NetworkInterface, NetworkAddress> onAddressAdded; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 84 | |
| 85 | /** @brief Fires when a network-layer address is removed from the interface |
| 86 | */ |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 87 | util::Signal<NetworkInterface, NetworkAddress> onAddressRemoved; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 88 | |
| 89 | public: // getters |
| 90 | /** @brief Returns an opaque ID that uniquely identifies the interface on the system |
| 91 | */ |
| 92 | int |
| 93 | getIndex() const |
| 94 | { |
| 95 | return m_index; |
| 96 | } |
| 97 | |
| 98 | /** @brief Returns the name of the interface, unique on the system |
| 99 | */ |
| 100 | std::string |
| 101 | getName() const |
| 102 | { |
| 103 | return m_name; |
| 104 | } |
| 105 | |
| 106 | /** @brief Returns the hardware type of the interface |
| 107 | */ |
| 108 | InterfaceType |
| 109 | getType() const |
| 110 | { |
| 111 | return m_type; |
| 112 | } |
| 113 | |
| 114 | /** @brief Returns a bitset of platform-specific flags enabled on the interface |
| 115 | */ |
| 116 | uint32_t |
| 117 | getFlags() const |
| 118 | { |
| 119 | return m_flags; |
| 120 | } |
| 121 | |
| 122 | /** @brief Returns the current state of the interface |
| 123 | */ |
| 124 | InterfaceState |
| 125 | getState() const |
| 126 | { |
| 127 | return m_state; |
| 128 | } |
| 129 | |
| 130 | /** @brief Returns the MTU (maximum transmission unit) of the interface |
| 131 | */ |
| 132 | uint32_t |
| 133 | getMtu() const |
| 134 | { |
| 135 | return m_mtu; |
| 136 | } |
| 137 | |
| 138 | /** @brief Returns the link-layer (Ethernet) address of the interface |
| 139 | */ |
| 140 | ethernet::Address |
| 141 | getEthernetAddress() const |
| 142 | { |
| 143 | return m_etherAddress; |
| 144 | } |
| 145 | |
| 146 | /** @brief Returns the link-layer (Ethernet) broadcast address of the interface |
| 147 | */ |
| 148 | ethernet::Address |
| 149 | getEthernetBroadcastAddress() const |
| 150 | { |
| 151 | return m_etherBrdAddress; |
| 152 | } |
| 153 | |
| 154 | /** @brief Returns a list of all network-layer addresses present on the interface |
| 155 | */ |
| 156 | const std::set<NetworkAddress>& |
| 157 | getNetworkAddresses() const |
| 158 | { |
| 159 | return m_netAddresses; |
| 160 | } |
| 161 | |
| 162 | /** @brief Returns true if the interface is a loopback interface |
| 163 | */ |
| 164 | bool |
| 165 | isLoopback() const |
| 166 | { |
| 167 | return (m_flags & IFF_LOOPBACK) != 0; |
| 168 | } |
| 169 | |
| 170 | /** @brief Returns true if the interface is a point-to-point interface |
| 171 | */ |
| 172 | bool |
| 173 | isPointToPoint() const |
| 174 | { |
| 175 | return (m_flags & IFF_POINTOPOINT) != 0; |
| 176 | } |
| 177 | |
| 178 | /** @brief Returns true if the interface supports broadcast communication |
| 179 | */ |
| 180 | bool |
| 181 | canBroadcast() const |
| 182 | { |
| 183 | return (m_flags & IFF_BROADCAST) != 0; |
| 184 | } |
| 185 | |
| 186 | /** @brief Returns true if the interface supports multicast communication |
| 187 | */ |
| 188 | bool |
| 189 | canMulticast() const |
| 190 | { |
| 191 | return (m_flags & IFF_MULTICAST) != 0; |
| 192 | } |
| 193 | |
| 194 | /** @brief Returns true if the interface is administratively up |
| 195 | */ |
| 196 | bool |
| 197 | isUp() const |
| 198 | { |
| 199 | return (m_flags & IFF_UP) != 0; |
| 200 | } |
| 201 | |
| 202 | private: // constructor |
| 203 | NetworkInterface(); |
| 204 | |
| 205 | private: // modifiers |
| 206 | bool |
| 207 | addNetworkAddress(const NetworkAddress& address); |
| 208 | |
| 209 | bool |
| 210 | removeNetworkAddress(const NetworkAddress& address); |
| 211 | |
| 212 | void |
| 213 | setIndex(int index); |
| 214 | |
| 215 | void |
| 216 | setName(const std::string& name); |
| 217 | |
| 218 | void |
| 219 | setType(InterfaceType type); |
| 220 | |
| 221 | void |
| 222 | setFlags(uint32_t flags); |
| 223 | |
| 224 | void |
| 225 | setState(InterfaceState state); |
| 226 | |
| 227 | void |
| 228 | setMtu(uint32_t mtu); |
| 229 | |
| 230 | void |
| 231 | setEthernetAddress(const ethernet::Address& address); |
| 232 | |
| 233 | void |
| 234 | setEthernetBroadcastAddress(const ethernet::Address& address); |
| 235 | |
| 236 | private: |
| 237 | friend class NetworkMonitor::Impl; |
| 238 | |
| 239 | int m_index; |
| 240 | std::string m_name; |
| 241 | InterfaceType m_type; |
| 242 | uint32_t m_flags; // IFF_* in <net/if.h> |
| 243 | InterfaceState m_state; |
| 244 | uint32_t m_mtu; |
| 245 | ethernet::Address m_etherAddress; |
| 246 | ethernet::Address m_etherBrdAddress; |
| 247 | std::set<NetworkAddress> m_netAddresses; |
| 248 | }; |
| 249 | |
| 250 | std::ostream& |
| 251 | operator<<(std::ostream& os, const NetworkInterface& interface); |
| 252 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 253 | } // namespace net |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 254 | } // namespace ndn |
| 255 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 256 | #endif // NDN_NET_NETWORK_INTERFACE_HPP |