blob: 8a182294476a6ed4e56bfe236352adb9257a56aa [file] [log] [blame]
Pankaj Garg2efbcce2020-03-10 12:05:08 -07001/*
2 *
Rob Walkere812e672020-03-31 17:51:57 -07003 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
5 * Copyright (c) 2013-2017 Nest Labs, Inc.
Pankaj Garg2efbcce2020-03-10 12:05:08 -07006 *
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 Litvin1873e8c2020-10-12 10:52:26 -040027#pragma once
Pankaj Garg2efbcce2020-03-10 12:05:08 -070028
Michael Spang1fcb6dd2020-08-24 11:19:04 -040029#include <inet/InetConfig.h>
30
Pankaj Gargd4aa1df2020-03-24 08:56:20 -070031#include <inet/IPAddress.h>
Rob Walker29e104a2020-05-13 20:38:36 -070032#include <inet/InetError.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080033#include <lib/support/DLLUtil.h>
Pankaj Garg2efbcce2020-03-10 12:05:08 -070034
Jean-Francois Penven75bdf792022-04-14 13:32:17 -040035#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -070036#include <lwip/netif.h>
37#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
38
Damian Królik34a4a932020-07-20 20:07:50 +020039#if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
Pankaj Garg2efbcce2020-03-10 12:05:08 -070040struct if_nameindex;
41struct ifaddrs;
Damian Królik34a4a932020-07-20 20:07:50 +020042#endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
43
44#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Alex Tsitsiura0e012b12022-09-30 22:51:02 +030045#include <zephyr/device.h>
Andrei Litvin2b58bdf2021-02-19 17:41:37 -050046
Damian Królik34a4a932020-07-20 20:07:50 +020047struct net_if;
48struct net_if_ipv4;
49struct net_if_ipv6;
50#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Pankaj Garg2efbcce2020-03-10 12:05:08 -070051
Jean-Francois Penven75bdf792022-04-14 13:32:17 -040052#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
53struct otIp6AddressInfo;
54#endif
55
Rob Walker29e104a2020-05-13 20:38:36 -070056#include <stddef.h>
57#include <stdint.h>
58
Pankaj Garg2efbcce2020-03-10 12:05:08 -070059namespace chip {
60namespace Inet {
61
62class IPAddress;
63class IPPrefix;
64
Pankaj Garg2efbcce2020-03-10 12:05:08 -070065/**
Kamil Kasperczyk1b241372021-12-07 14:00:03 +010066 * Data type describing interface type.
67 */
68enum class InterfaceType
69{
70 Unknown = 0,
71 WiFi = 1,
72 Ethernet = 2,
73 Cellular = 3,
74 Thread = 4,
75};
76
77/**
Kevin Schoedel014d85c2021-10-27 09:25:05 -040078 * Indicator for system network interfaces.
Pankaj Garg2efbcce2020-03-10 12:05:08 -070079 */
Kevin Schoedel014d85c2021-10-27 09:25:05 -040080class InterfaceId
81{
82public:
Jean-Francois Penven75bdf792022-04-14 13:32:17 -040083#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Kevin Schoedel014d85c2021-10-27 09:25:05 -040084 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 Garg2efbcce2020-03-10 12:05:08 -070087
Kevin Schoedel014d85c2021-10-27 09:25:05 -040088#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ólik34a4a932020-07-20 20:07:50 +020091#endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
92
93#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Kevin Schoedel014d85c2021-10-27 09:25:05 -040094 using PlatformType = int;
95 static constexpr size_t kMaxIfNameLength = Z_DEVICE_MAX_NAME_LEN;
Damian Królik34a4a932020-07-20 20:07:50 +020096#endif
Pankaj Garg2efbcce2020-03-10 12:05:08 -070097
Jean-Francois Penven75bdf792022-04-14 13:32:17 -040098#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
99 using PlatformType = unsigned int;
100 static constexpr size_t kMaxIfNameLength = 6;
101#endif
102
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400103 ~InterfaceId() = default;
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700104
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400105 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 Schoedel415ca522021-11-12 14:12:51 -0500160 /**
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 Penvenb2819032022-07-28 09:52:49 -0400188 * any address configured
189 * or if no link local (fe80::)
190 * address is present.
Kevin Schoedel415ca522021-11-12 14:12:51 -0500191 * @retval #CHIP_NO_ERROR On success.
192 */
Andrei Litvind43e6a72022-03-17 17:39:24 -0400193 CHIP_ERROR GetLinkLocalAddr(IPAddress * llAddr) const;
Kevin Schoedel415ca522021-11-12 14:12:51 -0500194
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400195private:
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400196#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400197 static constexpr PlatformType kPlatformNull = nullptr;
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700198#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
199
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400200#if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
201 static constexpr PlatformType kPlatformNull = 0;
202#endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700203
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400204#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
205 static constexpr PlatformType kPlatformNull = 0;
206#endif
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400207
208#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
209 static constexpr PlatformType kPlatformNull = 0;
210#endif
211
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400212 PlatformType mPlatformInterface;
213};
Kevin Schoedel77a22232021-10-20 20:04:36 -0400214
215/**
216 * Compute a prefix length from a variable-length netmask.
217 */
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700218extern 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 */
241class InterfaceIterator
242{
243public:
Kevin Schoedel77a22232021-10-20 20:04:36 -0400244 /**
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 Spangb0e38c22020-09-30 14:20:50 -0400250 InterfaceIterator();
251 ~InterfaceIterator();
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700252
Kevin Schoedel77a22232021-10-20 20:04:36 -0400253 /**
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 Spangb0e38c22020-09-30 14:20:50 -0400259 bool HasCurrent();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400260
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 Spangb0e38c22020-09-30 14:20:50 -0400278 bool Next();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400279
280 /**
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400281 * InterfaceId InterfaceIterator::GetInterfaceId(void)
Kevin Schoedel77a22232021-10-20 20:04:36 -0400282 *
283 * Returns the network interface id at the current iterator position.
284 *
285 * @retval id The current network interface id.
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400286 * @retval InterfaceId() If advanced beyond the end of the list.
Kevin Schoedel77a22232021-10-20 20:04:36 -0400287 */
Michael Spangb0e38c22020-09-30 14:20:50 -0400288 InterfaceId GetInterfaceId();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400289
290 /**
Kevin Schoedel77a22232021-10-20 20:04:36 -0400291 * 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 Schoedel9c012582021-06-30 16:20:44 -0400303 CHIP_ERROR GetInterfaceName(char * nameBuf, size_t nameBufSize);
Kevin Schoedel77a22232021-10-20 20:04:36 -0400304
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 Spangb0e38c22020-09-30 14:20:50 -0400311 bool IsUp();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400312
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 Spangb0e38c22020-09-30 14:20:50 -0400319 bool SupportsMulticast();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400320
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 Spangb0e38c22020-09-30 14:20:50 -0400327 bool HasBroadcastAddress();
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700328
Kamil Kasperczyk1b241372021-12-07 14:00:03 +0100329 /**
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 Garg2efbcce2020-03-10 12:05:08 -0700345protected:
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400346#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700347 struct netif * mCurNetif;
348#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
349
Damian Królik34a4a932020-07-20 20:07:50 +0200350#if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
Martin Turoncd17ad42020-03-18 20:03:53 -0700351 struct if_nameindex * mIntfArray;
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700352 size_t mCurIntf;
353 short mIntfFlags;
354 bool mIntfFlagsCached;
355
Michael Spangb0e38c22020-09-30 14:20:50 -0400356 short GetFlags();
Damian Królik34a4a932020-07-20 20:07:50 +0200357#endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
358
359#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400360 InterfaceId::PlatformType mCurrentId = 1;
361 net_if * mCurrentInterface = nullptr;
Damian Królik34a4a932020-07-20 20:07:50 +0200362#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400363#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
364 struct otIp6AddressInfo * mCurNetif;
365#endif
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700366};
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 */
390class DLL_EXPORT InterfaceAddressIterator
391{
392public:
Kevin Schoedel77a22232021-10-20 20:04:36 -0400393 /**
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 Spangb0e38c22020-09-30 14:20:50 -0400399 InterfaceAddressIterator();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400400
401 /**
402 * Destroys an InterfaceAddressIterator object.
403 *
404 * Recycles any resources allocated by the constructor.
405 */
Michael Spangb0e38c22020-09-30 14:20:50 -0400406 ~InterfaceAddressIterator();
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700407
Kevin Schoedel77a22232021-10-20 20:04:36 -0400408 /**
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 Spangb0e38c22020-09-30 14:20:50 -0400414 bool HasCurrent();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400415
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 Spangb0e38c22020-09-30 14:20:50 -0400432 bool Next();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400433
434 /**
435 * @fn IPAddress InterfaceAddressIterator::GetAddress(void)
436 *
437 * @brief Get the current interface address.
438 *
Kevin Schoedelf86f21f2021-12-16 07:17:46 -0500439 * @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 Schoedel77a22232021-10-20 20:04:36 -0400444 */
Kevin Schoedelf86f21f2021-12-16 07:17:46 -0500445 CHIP_ERROR GetAddress(IPAddress & outIPAddress);
Kevin Schoedel77a22232021-10-20 20:04:36 -0400446
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 Spangb0e38c22020-09-30 14:20:50 -0400464 uint8_t GetPrefixLength();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400465
466 /**
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400467 * @fn InterfaceId InterfaceAddressIterator::GetInterfaceId(void)
Kevin Schoedel77a22232021-10-20 20:04:36 -0400468 *
469 * @brief Returns the network interface id associated with the current
470 * interface address.
471 *
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400472 * @return the interface id or \c InterfaceId() if the iterator
Kevin Schoedel77a22232021-10-20 20:04:36 -0400473 * is positioned beyond the end of the address list.
474 */
Michael Spangb0e38c22020-09-30 14:20:50 -0400475 InterfaceId GetInterfaceId();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400476
477 /**
Kevin Schoedel77a22232021-10-20 20:04:36 -0400478 * @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 Schoedel9c012582021-06-30 16:20:44 -0400497 CHIP_ERROR GetInterfaceName(char * nameBuf, size_t nameBufSize);
Kevin Schoedel77a22232021-10-20 20:04:36 -0400498
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 Spangb0e38c22020-09-30 14:20:50 -0400505 bool IsUp();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400506
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 Spangb0e38c22020-09-30 14:20:50 -0400513 bool SupportsMulticast();
Kevin Schoedel77a22232021-10-20 20:04:36 -0400514
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 Spangb0e38c22020-09-30 14:20:50 -0400521 bool HasBroadcastAddress();
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700522
523private:
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400524#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700525 enum
526 {
527 kBeforeStartIndex = -1
528 };
529
530 InterfaceIterator mIntfIter;
531 int mCurAddrIndex;
532#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
533
Damian Królik34a4a932020-07-20 20:07:50 +0200534#if CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700535 struct ifaddrs * mAddrsList;
536 struct ifaddrs * mCurAddr;
Damian Królik34a4a932020-07-20 20:07:50 +0200537#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 Penven75bdf792022-04-14 13:32:17 -0400544#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Jean-Francois Penvenfa087ea2022-04-22 14:53:57 -0400545 const otNetifAddress * mNetifAddrList;
546 const otNetifAddress * mCurAddr;
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400547#endif // #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700548};
549
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400550#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
551inline InterfaceIterator::InterfaceIterator(void) {}
Jean-Francois Penven75bdf792022-04-14 13:32:17 -0400552inline InterfaceIterator::~InterfaceIterator() = default;
553inline InterfaceAddressIterator::~InterfaceAddressIterator() = default;
554inline 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 Garg2efbcce2020-03-10 12:05:08 -0700561
562inline InterfaceIterator::InterfaceIterator(void)
563{
564 mCurNetif = netif_list;
565}
566
Martin Turoncd17ad42020-03-18 20:03:53 -0700567inline InterfaceIterator::~InterfaceIterator(void) {}
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700568
569inline bool InterfaceIterator::HasCurrent(void)
570{
571 return mCurNetif != NULL;
572}
573
574inline InterfaceId InterfaceIterator::GetInterfaceId(void)
575{
Kevin Schoedel014d85c2021-10-27 09:25:05 -0400576 return InterfaceId(mCurNetif);
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700577}
578
579inline InterfaceAddressIterator::InterfaceAddressIterator(void)
580{
Martin Turoncd17ad42020-03-18 20:03:53 -0700581 mCurAddrIndex = kBeforeStartIndex;
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700582}
583
Martin Turoncd17ad42020-03-18 20:03:53 -0700584inline InterfaceAddressIterator::~InterfaceAddressIterator(void) {}
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700585
586#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
587
Damian Królik34a4a932020-07-20 20:07:50 +0200588#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
589inline InterfaceIterator::~InterfaceIterator() = default;
590inline InterfaceAddressIterator::~InterfaceAddressIterator() = default;
591#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
592
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700593} // namespace Inet
594} // namespace chip