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" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 29 | #include "../util/signal.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 30 | #include <set> |
| 31 | |
| 32 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 33 | namespace net { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 34 | |
| 35 | /** @brief Indicates the hardware type of a network interface |
| 36 | */ |
| 37 | enum class InterfaceType { |
| 38 | UNKNOWN, |
| 39 | LOOPBACK, |
| 40 | ETHERNET, |
| 41 | // we do not support anything else for now |
| 42 | }; |
| 43 | |
| 44 | std::ostream& |
| 45 | operator<<(std::ostream& os, InterfaceType type); |
| 46 | |
| 47 | /** @brief Indicates the state of a network interface |
| 48 | */ |
| 49 | enum class InterfaceState { |
| 50 | UNKNOWN, ///< interface is in an unknown state |
| 51 | DOWN, ///< interface is administratively down |
| 52 | NO_CARRIER, ///< interface is administratively up but has no carrier |
| 53 | DORMANT, ///< interface has a carrier but it cannot send or receive normal user traffic yet |
| 54 | RUNNING, ///< interface can be used to send and receive packets |
| 55 | }; |
| 56 | |
| 57 | std::ostream& |
| 58 | operator<<(std::ostream& os, InterfaceState state); |
| 59 | |
| 60 | /** |
| 61 | * @brief Represents one network interface attached to the host. |
| 62 | * |
| 63 | * Each network interface has a unique index, a name, and a set of flags indicating its |
| 64 | * capabilities and current state. It may contain one hardware (Ethernet) address, and |
| 65 | * zero or more network-layer (IP) addresses. Specific signals are emitted when the |
| 66 | * interface data change. |
| 67 | */ |
| 68 | class NetworkInterface |
| 69 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 70 | public: // signals, marked 'mutable' so they can be connected on 'const NetworkInterface' |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 71 | /** @brief Fires when interface state changes |
| 72 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 73 | mutable util::Signal<NetworkInterface, InterfaceState /*old*/, InterfaceState /*new*/> onStateChanged; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 74 | |
| 75 | /** @brief Fires when interface mtu changes |
| 76 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 77 | mutable util::Signal<NetworkInterface, uint32_t /*old*/, uint32_t /*new*/> onMtuChanged; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 78 | |
| 79 | /** @brief Fires when a network-layer address is added to the interface |
| 80 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 81 | mutable util::Signal<NetworkInterface, NetworkAddress> onAddressAdded; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 82 | |
| 83 | /** @brief Fires when a network-layer address is removed from the interface |
| 84 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 85 | mutable util::Signal<NetworkInterface, NetworkAddress> onAddressRemoved; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 86 | |
| 87 | public: // getters |
| 88 | /** @brief Returns an opaque ID that uniquely identifies the interface on the system |
| 89 | */ |
| 90 | int |
| 91 | getIndex() const |
| 92 | { |
| 93 | return m_index; |
| 94 | } |
| 95 | |
| 96 | /** @brief Returns the name of the interface, unique on the system |
| 97 | */ |
| 98 | std::string |
| 99 | getName() const |
| 100 | { |
| 101 | return m_name; |
| 102 | } |
| 103 | |
| 104 | /** @brief Returns the hardware type of the interface |
| 105 | */ |
| 106 | InterfaceType |
| 107 | getType() const |
| 108 | { |
| 109 | return m_type; |
| 110 | } |
| 111 | |
| 112 | /** @brief Returns a bitset of platform-specific flags enabled on the interface |
| 113 | */ |
| 114 | uint32_t |
| 115 | getFlags() const |
| 116 | { |
| 117 | return m_flags; |
| 118 | } |
| 119 | |
| 120 | /** @brief Returns the current state of the interface |
| 121 | */ |
| 122 | InterfaceState |
| 123 | getState() const |
| 124 | { |
| 125 | return m_state; |
| 126 | } |
| 127 | |
| 128 | /** @brief Returns the MTU (maximum transmission unit) of the interface |
| 129 | */ |
| 130 | uint32_t |
| 131 | getMtu() const |
| 132 | { |
| 133 | return m_mtu; |
| 134 | } |
| 135 | |
| 136 | /** @brief Returns the link-layer (Ethernet) address of the interface |
| 137 | */ |
| 138 | ethernet::Address |
| 139 | getEthernetAddress() const |
| 140 | { |
| 141 | return m_etherAddress; |
| 142 | } |
| 143 | |
| 144 | /** @brief Returns the link-layer (Ethernet) broadcast address of the interface |
| 145 | */ |
| 146 | ethernet::Address |
| 147 | getEthernetBroadcastAddress() const |
| 148 | { |
| 149 | return m_etherBrdAddress; |
| 150 | } |
| 151 | |
| 152 | /** @brief Returns a list of all network-layer addresses present on the interface |
| 153 | */ |
| 154 | const std::set<NetworkAddress>& |
| 155 | getNetworkAddresses() const |
| 156 | { |
| 157 | return m_netAddresses; |
| 158 | } |
| 159 | |
| 160 | /** @brief Returns true if the interface is a loopback interface |
| 161 | */ |
| 162 | bool |
| 163 | isLoopback() const |
| 164 | { |
| 165 | return (m_flags & IFF_LOOPBACK) != 0; |
| 166 | } |
| 167 | |
| 168 | /** @brief Returns true if the interface is a point-to-point interface |
| 169 | */ |
| 170 | bool |
| 171 | isPointToPoint() const |
| 172 | { |
| 173 | return (m_flags & IFF_POINTOPOINT) != 0; |
| 174 | } |
| 175 | |
| 176 | /** @brief Returns true if the interface supports broadcast communication |
| 177 | */ |
| 178 | bool |
| 179 | canBroadcast() const |
| 180 | { |
| 181 | return (m_flags & IFF_BROADCAST) != 0; |
| 182 | } |
| 183 | |
| 184 | /** @brief Returns true if the interface supports multicast communication |
| 185 | */ |
| 186 | bool |
| 187 | canMulticast() const |
| 188 | { |
| 189 | return (m_flags & IFF_MULTICAST) != 0; |
| 190 | } |
| 191 | |
| 192 | /** @brief Returns true if the interface is administratively up |
| 193 | */ |
| 194 | bool |
| 195 | isUp() const |
| 196 | { |
| 197 | return (m_flags & IFF_UP) != 0; |
| 198 | } |
| 199 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 200 | public: // modifiers: they update information on this instance, but do not change netif in the OS |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 201 | bool |
| 202 | addNetworkAddress(const NetworkAddress& address); |
| 203 | |
| 204 | bool |
| 205 | removeNetworkAddress(const NetworkAddress& address); |
| 206 | |
| 207 | void |
| 208 | setIndex(int index); |
| 209 | |
| 210 | void |
| 211 | setName(const std::string& name); |
| 212 | |
| 213 | void |
| 214 | setType(InterfaceType type); |
| 215 | |
| 216 | void |
| 217 | setFlags(uint32_t flags); |
| 218 | |
| 219 | void |
| 220 | setState(InterfaceState state); |
| 221 | |
| 222 | void |
| 223 | setMtu(uint32_t mtu); |
| 224 | |
| 225 | void |
| 226 | setEthernetAddress(const ethernet::Address& address); |
| 227 | |
| 228 | void |
| 229 | setEthernetBroadcastAddress(const ethernet::Address& address); |
| 230 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 231 | private: // constructor |
| 232 | NetworkInterface(); // accessible through NetworkMonitorImpl::makeNetworkInterface |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 233 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 234 | private: |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 235 | int m_index; |
| 236 | std::string m_name; |
| 237 | InterfaceType m_type; |
| 238 | uint32_t m_flags; // IFF_* in <net/if.h> |
| 239 | InterfaceState m_state; |
| 240 | uint32_t m_mtu; |
| 241 | ethernet::Address m_etherAddress; |
| 242 | ethernet::Address m_etherBrdAddress; |
| 243 | std::set<NetworkAddress> m_netAddresses; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 244 | |
| 245 | friend class NetworkMonitorImpl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | std::ostream& |
| 249 | operator<<(std::ostream& os, const NetworkInterface& interface); |
| 250 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 251 | } // namespace net |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 252 | } // namespace ndn |
| 253 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 254 | #endif // NDN_NET_NETWORK_INTERFACE_HPP |