Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Rob Walker | e812e67 | 2020-03-31 17:51:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2020 Project CHIP Authors |
| 4 | * Copyright (c) 2019 Google LLC. |
| 5 | * Copyright (c) 2013-2017 Nest Labs, Inc. |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * @file |
| 22 | * This file defines the <tt>Inet::InterfaceId</tt> type alias and related |
| 23 | * classes for iterating on the list of system network interfaces and the list |
| 24 | * of system interface addresses. |
| 25 | */ |
| 26 | |
Andrei Litvin | 1873e8c | 2020-10-12 10:52:26 -0400 | [diff] [blame] | 27 | #pragma once |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 28 | |
Michael Spang | 1fcb6dd | 2020-08-24 11:19:04 -0400 | [diff] [blame] | 29 | #include <inet/InetConfig.h> |
| 30 | |
Pankaj Garg | d4aa1df | 2020-03-24 08:56:20 -0700 | [diff] [blame] | 31 | #include <inet/IPAddress.h> |
Rob Walker | 29e104a | 2020-05-13 20:38:36 -0700 | [diff] [blame] | 32 | #include <inet/InetError.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 33 | #include <lib/support/DLLUtil.h> |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 34 | |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 35 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 36 | #include <lwip/netif.h> |
| 37 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 38 | |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 39 | #if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 40 | struct if_nameindex; |
| 41 | struct ifaddrs; |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 42 | #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 43 | |
| 44 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Alex Tsitsiura | 0e012b1 | 2022-09-30 22:51:02 +0300 | [diff] [blame] | 45 | #include <zephyr/device.h> |
Andrei Litvin | 2b58bdf | 2021-02-19 17:41:37 -0500 | [diff] [blame] | 46 | |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 47 | struct net_if; |
| 48 | struct net_if_ipv4; |
| 49 | struct net_if_ipv6; |
| 50 | #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 51 | |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 52 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 53 | struct otIp6AddressInfo; |
| 54 | #endif |
| 55 | |
Rob Walker | 29e104a | 2020-05-13 20:38:36 -0700 | [diff] [blame] | 56 | #include <stddef.h> |
| 57 | #include <stdint.h> |
| 58 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 59 | namespace chip { |
| 60 | namespace Inet { |
| 61 | |
| 62 | class IPAddress; |
| 63 | class IPPrefix; |
| 64 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 65 | /** |
Kamil Kasperczyk | 1b24137 | 2021-12-07 14:00:03 +0100 | [diff] [blame] | 66 | * Data type describing interface type. |
| 67 | */ |
| 68 | enum class InterfaceType |
| 69 | { |
| 70 | Unknown = 0, |
| 71 | WiFi = 1, |
| 72 | Ethernet = 2, |
| 73 | Cellular = 3, |
| 74 | Thread = 4, |
| 75 | }; |
| 76 | |
| 77 | /** |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 78 | * Indicator for system network interfaces. |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 79 | */ |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 80 | class InterfaceId |
| 81 | { |
| 82 | public: |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 83 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 84 | using PlatformType = struct netif *; |
| 85 | static constexpr size_t kMaxIfNameLength = 13; // Names are formatted as %c%c%d |
| 86 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 87 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 88 | #if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 89 | using PlatformType = unsigned int; |
| 90 | static constexpr size_t kMaxIfNameLength = IF_NAMESIZE; |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 91 | #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 92 | |
| 93 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 94 | using PlatformType = int; |
| 95 | static constexpr size_t kMaxIfNameLength = Z_DEVICE_MAX_NAME_LEN; |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 96 | #endif |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 97 | |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 98 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 99 | using PlatformType = unsigned int; |
| 100 | static constexpr size_t kMaxIfNameLength = 6; |
| 101 | #endif |
| 102 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 103 | ~InterfaceId() = default; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 104 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 105 | constexpr InterfaceId() : mPlatformInterface(kPlatformNull) {} |
| 106 | explicit constexpr InterfaceId(PlatformType interface) : mPlatformInterface(interface) {} |
| 107 | |
| 108 | constexpr InterfaceId(const InterfaceId & other) : mPlatformInterface(other.mPlatformInterface) {} |
| 109 | constexpr InterfaceId & operator=(const InterfaceId & other) |
| 110 | { |
| 111 | mPlatformInterface = other.mPlatformInterface; |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | static constexpr InterfaceId Null() { return InterfaceId(); } |
| 116 | |
| 117 | constexpr bool operator==(const InterfaceId & other) const { return mPlatformInterface == other.mPlatformInterface; } |
| 118 | constexpr bool operator!=(const InterfaceId & other) const { return mPlatformInterface != other.mPlatformInterface; } |
| 119 | |
| 120 | /** |
| 121 | * Test for inequivalence with the null interface. |
| 122 | */ |
| 123 | bool IsPresent() const { return mPlatformInterface != kPlatformNull; } |
| 124 | |
| 125 | /** |
| 126 | * Get the underlying platform representation of the interface. |
| 127 | */ |
| 128 | PlatformType GetPlatformInterface() const { return mPlatformInterface; } |
| 129 | |
| 130 | /** |
| 131 | * Get the name of the network interface |
| 132 | * |
| 133 | * @param[in] nameBuf Region of memory to write the interface name. |
| 134 | * @param[in] nameBufSize Size of the region denoted by \c nameBuf. |
| 135 | * |
| 136 | * @retval CHIP_NO_ERROR Successful result, interface name written. |
| 137 | * @retval CHIP_ERROR_BUFFER_TOO_SMALL Buffer is too small for the interface name. |
| 138 | * @retval other Another system or platform error. |
| 139 | * |
| 140 | * Writes the name of the network interface as a \c NUL terminated text string at \c nameBuf. |
| 141 | * The name of the unspecified network interface is the empty string. |
| 142 | */ |
| 143 | CHIP_ERROR GetInterfaceName(char * nameBuf, size_t nameBufSize) const; |
| 144 | |
| 145 | /** |
| 146 | * Search the list of network interfaces for the indicated name. |
| 147 | * |
| 148 | * @param[in] intfName Name of the network interface to find. |
| 149 | * @param[out] intfId Indicator of the network interface to assign. |
| 150 | * |
| 151 | * @retval CHIP_NO_ERROR Success, network interface indicated. |
| 152 | * @retval INET_ERROR_UNKNOWN_INTERFACE No network interface found. |
| 153 | * @retval other Another system or platform error. |
| 154 | * |
| 155 | * @note |
| 156 | * On LwIP, this function must be called with the LwIP stack lock acquired. |
| 157 | */ |
| 158 | static CHIP_ERROR InterfaceNameToId(const char * intfName, InterfaceId & intfId); |
| 159 | |
Kevin Schoedel | 415ca52 | 2021-11-12 14:12:51 -0500 | [diff] [blame] | 160 | /** |
| 161 | * Get the interface identifier for the specified IP address. If the |
| 162 | * interface identifier cannot be derived it is set to the default InterfaceId. |
| 163 | * |
| 164 | * @note |
| 165 | * This function fetches the first interface (from the configured list |
| 166 | * of interfaces) that matches the specified IP address. |
| 167 | */ |
| 168 | static InterfaceId FromIPAddress(const IPAddress & addr); |
| 169 | |
| 170 | /** |
| 171 | * Check if there is a prefix match between the specified IPv6 address and any of |
| 172 | * the locally configured IPv6 addresses. |
| 173 | * |
| 174 | * @param[in] addr The IPv6 address to check for the prefix-match. |
| 175 | * @return true if a successful match is found, otherwise false. |
| 176 | */ |
| 177 | static bool MatchLocalIPv6Subnet(const IPAddress & addr); |
| 178 | |
| 179 | /** |
| 180 | * Get the link local IPv6 address. |
| 181 | * |
| 182 | * @param[out] llAddr The link local IPv6 address for the link. |
| 183 | * |
| 184 | * @retval #CHIP_ERROR_NOT_IMPLEMENTED If IPv6 is not supported. |
| 185 | * @retval #CHIP_ERROR_INVALID_ARGUMENT If the link local address |
| 186 | * is nullptr. |
| 187 | * @retval #INET_ERROR_ADDRESS_NOT_FOUND If the link does not have |
Jean-Francois Penven | b281903 | 2022-07-28 09:52:49 -0400 | [diff] [blame] | 188 | * any address configured |
| 189 | * or if no link local (fe80::) |
| 190 | * address is present. |
Kevin Schoedel | 415ca52 | 2021-11-12 14:12:51 -0500 | [diff] [blame] | 191 | * @retval #CHIP_NO_ERROR On success. |
| 192 | */ |
Andrei Litvin | d43e6a7 | 2022-03-17 17:39:24 -0400 | [diff] [blame] | 193 | CHIP_ERROR GetLinkLocalAddr(IPAddress * llAddr) const; |
Kevin Schoedel | 415ca52 | 2021-11-12 14:12:51 -0500 | [diff] [blame] | 194 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 195 | private: |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 196 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 197 | static constexpr PlatformType kPlatformNull = nullptr; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 198 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 199 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 200 | #if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 201 | static constexpr PlatformType kPlatformNull = 0; |
| 202 | #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 203 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 204 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
| 205 | static constexpr PlatformType kPlatformNull = 0; |
| 206 | #endif |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 207 | |
| 208 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 209 | static constexpr PlatformType kPlatformNull = 0; |
| 210 | #endif |
| 211 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 212 | PlatformType mPlatformInterface; |
| 213 | }; |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Compute a prefix length from a variable-length netmask. |
| 217 | */ |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 218 | extern uint8_t NetmaskToPrefixLength(const uint8_t * netmask, uint16_t netmaskLen); |
| 219 | |
| 220 | /** |
| 221 | * @brief Iterator for the list of system network interfaces. |
| 222 | * |
| 223 | * @details |
| 224 | * Use objects of this class to iterate the list of system network interfaces. |
| 225 | * |
| 226 | * Methods on an individual instance of this class are *not* thread-safe; |
| 227 | * however separate instances may be used simultaneously by multiple threads. |
| 228 | * |
| 229 | * On multi-threaded LwIP systems, instances are thread-safe relative to other |
| 230 | * threads accessing the global LwIP state provided that the other threads hold |
| 231 | * the LwIP core lock while mutating the list of netifs, and that netif object |
| 232 | * themselves are never destroyed. |
| 233 | * |
| 234 | * On sockets-based systems, iteration is always stable in the face of changes |
| 235 | * to the underlying system's interfaces. |
| 236 | * |
| 237 | * On LwIP systems, iteration is stable except in the case where the currently |
| 238 | * selected interface is removed from the list, in which case iteration ends |
| 239 | * immediately. |
| 240 | */ |
| 241 | class InterfaceIterator |
| 242 | { |
| 243 | public: |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 244 | /** |
| 245 | * Constructs an InterfaceIterator object. |
| 246 | * |
| 247 | * Starts the iterator at the first network interface. On some platforms, |
| 248 | * this constructor may allocate resources recycled by the destructor. |
| 249 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 250 | InterfaceIterator(); |
| 251 | ~InterfaceIterator(); |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 252 | |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 253 | /** |
| 254 | * Test whether the iterator is positioned on an interface |
| 255 | * |
| 256 | * @return \c true if the iterator is positioned on an interface; |
| 257 | * \c false if positioned beyond the end of the interface list. |
| 258 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 259 | bool HasCurrent(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Advance the iterator to the next network interface. |
| 263 | * |
| 264 | * @return \c false if advanced beyond the end, else \c true. |
| 265 | * |
| 266 | * Advances the internal iterator to the next network interface or to a position |
| 267 | * beyond the end of the interface list. |
| 268 | * |
| 269 | * On multi-threaded LwIP systems, this method is thread-safe relative to other |
| 270 | * threads accessing the global LwIP state provided that: 1) the other threads |
| 271 | * hold the LwIP core lock while mutating the list of netifs; and 2) netif objects |
| 272 | * themselves are never destroyed. |
| 273 | * |
| 274 | * Iteration is stable in the face of changes to the underlying system's |
| 275 | * interfaces, *except* in the case of LwIP systems when the currently selected |
| 276 | * interface is removed from the list, which causes iteration to end immediately. |
| 277 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 278 | bool Next(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 279 | |
| 280 | /** |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 281 | * InterfaceId InterfaceIterator::GetInterfaceId(void) |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 282 | * |
| 283 | * Returns the network interface id at the current iterator position. |
| 284 | * |
| 285 | * @retval id The current network interface id. |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 286 | * @retval InterfaceId() If advanced beyond the end of the list. |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 287 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 288 | InterfaceId GetInterfaceId(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 289 | |
| 290 | /** |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 291 | * Get the name of the current network interface |
| 292 | * |
| 293 | * @param[in] nameBuf Region of memory to write the interface name. |
| 294 | * @param[in] nameBufSize Size of the region denoted by \c nameBuf. |
| 295 | * |
| 296 | * @retval CHIP_NO_ERROR Successful result, interface name written. |
| 297 | * @retval CHIP_ERROR_INCORRECT_STATE Iterator is positioned beyond the end of the list. |
| 298 | * @retval CHIP_ERROR_BUFFER_TOO_SMALL Name is too large to be written in buffer. |
| 299 | * @retval other Another system or platform error. |
| 300 | * |
| 301 | * Writes the name of the network interface as \c NUL terminated text string at \c nameBuf. |
| 302 | */ |
Kevin Schoedel | 9c01258 | 2021-06-30 16:20:44 -0400 | [diff] [blame] | 303 | CHIP_ERROR GetInterfaceName(char * nameBuf, size_t nameBufSize); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * Returns whether the current network interface is up. |
| 307 | * |
| 308 | * @return \c true if current network interface is up, \c false if not |
| 309 | * or if the iterator is positioned beyond the end of the list. |
| 310 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 311 | bool IsUp(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * Returns whether the current network interface supports multicast. |
| 315 | * |
| 316 | * @return \c true if current network interface supports multicast, \c false |
| 317 | * if not, or if the iterator is positioned beyond the end of the list. |
| 318 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 319 | bool SupportsMulticast(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * Returns whether the current network interface has a broadcast address. |
| 323 | * |
| 324 | * @return \c true if current network interface has a broadcast address, \c false |
| 325 | * if not, or if the iterator is positioned beyond the end of the list. |
| 326 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 327 | bool HasBroadcastAddress(); |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 328 | |
Kamil Kasperczyk | 1b24137 | 2021-12-07 14:00:03 +0100 | [diff] [blame] | 329 | /** |
| 330 | * Get the interface type of the current network interface. |
| 331 | * |
| 332 | * @param[out] type Object to save the interface type. |
| 333 | */ |
| 334 | CHIP_ERROR GetInterfaceType(InterfaceType & type); |
| 335 | |
| 336 | /** |
| 337 | * Get the hardware address of the current network interface |
| 338 | * |
| 339 | * @param[out] addressBuffer Region of memory to write the hardware address. |
| 340 | * @param[out] addressSize Size of the address saved to a buffer. |
| 341 | * @param[in] addressBufferSize Maximum size of a buffer to save data. |
| 342 | */ |
| 343 | CHIP_ERROR GetHardwareAddress(uint8_t * addressBuffer, uint8_t & addressSize, uint8_t addressBufferSize); |
| 344 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 345 | protected: |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 346 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 347 | struct netif * mCurNetif; |
| 348 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 349 | |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 350 | #if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 351 | struct if_nameindex * mIntfArray; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 352 | size_t mCurIntf; |
| 353 | short mIntfFlags; |
| 354 | bool mIntfFlagsCached; |
| 355 | |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 356 | short GetFlags(); |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 357 | #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 358 | |
| 359 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 360 | InterfaceId::PlatformType mCurrentId = 1; |
| 361 | net_if * mCurrentInterface = nullptr; |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 362 | #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 363 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 364 | struct otIp6AddressInfo * mCurNetif; |
| 365 | #endif |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | /** |
| 369 | * @brief Iterator for the list of system network interface IP addresses. |
| 370 | * |
| 371 | * @details |
| 372 | * Use objects of this class to iterate the list of system network interface |
| 373 | * interface IP addresses. |
| 374 | * |
| 375 | * Methods on an individual instance of this class are *not* thread-safe; |
| 376 | * however separate instances may be used simultaneously by multiple threads. |
| 377 | * |
| 378 | * On multi-threaded LwIP systems, instances are thread-safe relative to other |
| 379 | * threads accessing the global LwIP state provided that: 1) other threads hold |
| 380 | * the LwIP core lock while mutating the list of netifs; and 2) netif object |
| 381 | * themselves are never destroyed. |
| 382 | * |
| 383 | * On sockets-based systems, iteration is always stable in the face of changes |
| 384 | * to the underlying system's interfaces and/or addresses. |
| 385 | * |
| 386 | * On LwIP systems, iteration is stable except in the case where the interface |
| 387 | * associated with the current address is removed, in which case iteration may |
| 388 | * end prematurely. |
| 389 | */ |
| 390 | class DLL_EXPORT InterfaceAddressIterator |
| 391 | { |
| 392 | public: |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 393 | /** |
| 394 | * Constructs an InterfaceAddressIterator object. |
| 395 | * |
| 396 | * Starts the iterator at the first network address. On some platforms, |
| 397 | * this constructor may allocate resources recycled by the destructor. |
| 398 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 399 | InterfaceAddressIterator(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Destroys an InterfaceAddressIterator object. |
| 403 | * |
| 404 | * Recycles any resources allocated by the constructor. |
| 405 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 406 | ~InterfaceAddressIterator(); |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 407 | |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 408 | /** |
| 409 | * Test whether the iterator is positioned on an interface address |
| 410 | * |
| 411 | * @return \c true if the iterator is positioned on an interface address; |
| 412 | * \c false if positioned beyond the end of the address list. |
| 413 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 414 | bool HasCurrent(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 415 | |
| 416 | /** |
| 417 | * @fn bool InterfaceAddressIterator::Next(void) |
| 418 | * |
| 419 | * @brief Advance the iterator to the next interface address. |
| 420 | * |
| 421 | * @return \c false if advanced beyond the end, else \c true. |
| 422 | * |
| 423 | * @details |
| 424 | * Advances the iterator to the next interface address or to a position |
| 425 | * beyond the end of the address list. |
| 426 | * |
| 427 | * On LwIP, this method is thread-safe provided that: 1) other threads hold |
| 428 | * the LwIP core lock while mutating the netif list; and 2) netif objects |
| 429 | * themselves are never destroyed. Additionally, iteration on LwIP systems |
| 430 | * will terminate early if the current interface is removed from the list. |
| 431 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 432 | bool Next(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 433 | |
| 434 | /** |
| 435 | * @fn IPAddress InterfaceAddressIterator::GetAddress(void) |
| 436 | * |
| 437 | * @brief Get the current interface address. |
| 438 | * |
Kevin Schoedel | f86f21f | 2021-12-16 07:17:46 -0500 | [diff] [blame] | 439 | * @param[out] outIPAddress The current interface address |
| 440 | * |
| 441 | * @return CHIP_NO_ERROR if the result IPAddress is valid. |
| 442 | * @return CHIP_ERROR_SENTINEL if the iterator is positioned beyond the end of the address list. |
| 443 | * @return other error from lower-level code |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 444 | */ |
Kevin Schoedel | f86f21f | 2021-12-16 07:17:46 -0500 | [diff] [blame] | 445 | CHIP_ERROR GetAddress(IPAddress & outIPAddress); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * @fn uint8_t InterfaceAddressIterator::GetPrefixLength(void) |
| 449 | * |
| 450 | * @brief Gets the network prefix associated with the current interface address. |
| 451 | * |
| 452 | * @return the network prefix (in bits) or 0 if the iterator is positioned beyond |
| 453 | * the end of the address list. |
| 454 | * |
| 455 | * @details |
| 456 | * On LwIP, this method simply returns the hard-coded constant 64. |
| 457 | * |
| 458 | * Note Well: the standard subnet prefix on all links other than PPP |
| 459 | * links is 64 bits. On PPP links and some non-broadcast multipoint access |
| 460 | * links, the convention is either 127 bits or 128 bits, but it might be |
| 461 | * something else. On most platforms, the system's interface address |
| 462 | * structure can represent arbitrary prefix lengths between 0 and 128. |
| 463 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 464 | uint8_t GetPrefixLength(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 465 | |
| 466 | /** |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 467 | * @fn InterfaceId InterfaceAddressIterator::GetInterfaceId(void) |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 468 | * |
| 469 | * @brief Returns the network interface id associated with the current |
| 470 | * interface address. |
| 471 | * |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 472 | * @return the interface id or \c InterfaceId() if the iterator |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 473 | * is positioned beyond the end of the address list. |
| 474 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 475 | InterfaceId GetInterfaceId(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 476 | |
| 477 | /** |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 478 | * @fn CHIP_ERROR InterfaceAddressIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize) |
| 479 | * |
| 480 | * @brief Get the name of the network interface associated with the |
| 481 | * current interface address. |
| 482 | * |
| 483 | * @param[in] nameBuf region of memory to write the interface name |
| 484 | * @param[in] nameBufSize size of the region denoted by \c nameBuf |
| 485 | * |
| 486 | * @retval CHIP_NO_ERROR successful result, interface name written |
| 487 | * @retval CHIP_ERROR_BUFFER_TOO_SMALL name is too large to be written in buffer |
| 488 | * @retval CHIP_ERROR_INCORRECT_STATE |
| 489 | * the iterator is not currently positioned on an |
| 490 | * interface address |
| 491 | * @retval other another system or platform error |
| 492 | * |
| 493 | * @details |
| 494 | * Writes the name of the network interface as \c NUL terminated text string |
| 495 | * at \c nameBuf. |
| 496 | */ |
Kevin Schoedel | 9c01258 | 2021-06-30 16:20:44 -0400 | [diff] [blame] | 497 | CHIP_ERROR GetInterfaceName(char * nameBuf, size_t nameBufSize); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Returns whether the network interface associated with the current interface address is up. |
| 501 | * |
| 502 | * @return \c true if current network interface is up, \c false if not, or |
| 503 | * if the iterator is not positioned on an interface address. |
| 504 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 505 | bool IsUp(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 506 | |
| 507 | /** |
| 508 | * Returns whether the network interface associated with the current interface address supports multicast. |
| 509 | * |
| 510 | * @return \c true if multicast is supported, \c false if not, or |
| 511 | * if the iterator is not positioned on an interface address. |
| 512 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 513 | bool SupportsMulticast(); |
Kevin Schoedel | 77a2223 | 2021-10-20 20:04:36 -0400 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * Returns whether the network interface associated with the current interface address has an IPv4 broadcast address. |
| 517 | * |
| 518 | * @return \c true if the interface has a broadcast address, \c false if not, or |
| 519 | * if the iterator is not positioned on an interface address. |
| 520 | */ |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 521 | bool HasBroadcastAddress(); |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 522 | |
| 523 | private: |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 524 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 525 | enum |
| 526 | { |
| 527 | kBeforeStartIndex = -1 |
| 528 | }; |
| 529 | |
| 530 | InterfaceIterator mIntfIter; |
| 531 | int mCurAddrIndex; |
| 532 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 533 | |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 534 | #if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 535 | struct ifaddrs * mAddrsList; |
| 536 | struct ifaddrs * mCurAddr; |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 537 | #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS |
| 538 | |
| 539 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
| 540 | InterfaceIterator mIntfIter; |
| 541 | net_if_ipv6 * mIpv6 = nullptr; |
| 542 | int mCurAddrIndex = -1; |
| 543 | #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 544 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Jean-Francois Penven | fa087ea | 2022-04-22 14:53:57 -0400 | [diff] [blame] | 545 | const otNetifAddress * mNetifAddrList; |
| 546 | const otNetifAddress * mCurAddr; |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 547 | #endif // #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 550 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 551 | inline InterfaceIterator::InterfaceIterator(void) {} |
Jean-Francois Penven | 75bdf79 | 2022-04-14 13:32:17 -0400 | [diff] [blame] | 552 | inline InterfaceIterator::~InterfaceIterator() = default; |
| 553 | inline InterfaceAddressIterator::~InterfaceAddressIterator() = default; |
| 554 | inline bool InterfaceIterator::HasCurrent(void) |
| 555 | { |
| 556 | return mCurNetif != NULL; |
| 557 | } |
| 558 | #endif |
| 559 | |
| 560 | #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 561 | |
| 562 | inline InterfaceIterator::InterfaceIterator(void) |
| 563 | { |
| 564 | mCurNetif = netif_list; |
| 565 | } |
| 566 | |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 567 | inline InterfaceIterator::~InterfaceIterator(void) {} |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 568 | |
| 569 | inline bool InterfaceIterator::HasCurrent(void) |
| 570 | { |
| 571 | return mCurNetif != NULL; |
| 572 | } |
| 573 | |
| 574 | inline InterfaceId InterfaceIterator::GetInterfaceId(void) |
| 575 | { |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 576 | return InterfaceId(mCurNetif); |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | inline InterfaceAddressIterator::InterfaceAddressIterator(void) |
| 580 | { |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 581 | mCurAddrIndex = kBeforeStartIndex; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 584 | inline InterfaceAddressIterator::~InterfaceAddressIterator(void) {} |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 585 | |
| 586 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 587 | |
Damian Królik | 34a4a93 | 2020-07-20 20:07:50 +0200 | [diff] [blame] | 588 | #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
| 589 | inline InterfaceIterator::~InterfaceIterator() = default; |
| 590 | inline InterfaceAddressIterator::~InterfaceAddressIterator() = default; |
| 591 | #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF |
| 592 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 593 | } // namespace Inet |
| 594 | } // namespace chip |