Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 Project CHIP Authors
4 : * Copyright (c) 2019 Google LLC.
5 : * Copyright (c) 2013-2018 Nest Labs, Inc.
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 class <tt>Inet::IPAddress</tt> and
23 : * related enumerated constants. The CHIP Inet Layer uses objects
24 : * of this class to represent Internet protocol addresses of both
25 : * IPv4 and IPv6 address families. (IPv4 addresses are stored
26 : * internally as IPv4-Mapped IPv6 addresses.)
27 : */
28 :
29 : #pragma once
30 :
31 : #include <stddef.h>
32 : #include <stdint.h>
33 : #include <string.h>
34 : #include <type_traits>
35 :
36 : #include <lib/core/CHIPError.h>
37 : #include <lib/support/BitFlags.h>
38 : #include <lib/support/DLLUtil.h>
39 :
40 : #include <inet/InetConfig.h>
41 : #include <inet/InetError.h>
42 :
43 : #include "inet/IANAConstants.h"
44 :
45 : #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
46 : #include <lwip/init.h>
47 : #include <lwip/ip_addr.h>
48 : #if INET_CONFIG_ENABLE_IPV4
49 : #include <lwip/ip4_addr.h>
50 : #endif // INET_CONFIG_ENABLE_IPV4
51 : #include <lwip/inet.h>
52 : #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
53 :
54 : #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
55 : #include <openthread/icmp6.h>
56 : #include <openthread/ip6.h>
57 : #endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
58 :
59 : #if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
60 : #include <net/if.h>
61 : #include <netinet/in.h>
62 : #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
63 :
64 : #if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS
65 : #include <sys/socket.h>
66 : #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS
67 :
68 : #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
69 : #include <zephyr/net/socket.h>
70 : #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
71 :
72 : #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && INET_CONFIG_ENABLE_IPV4
73 : #error Forbidden : native Open Thread implementation with IPV4 enabled
74 : #endif
75 :
76 : #include <inet/InetInterface.h>
77 :
78 : #define NL_INET_IPV6_ADDR_LEN_IN_BYTES (16)
79 : #define NL_INET_IPV6_MCAST_GROUP_LEN_IN_BYTES (14)
80 :
81 : namespace chip {
82 : namespace Inet {
83 :
84 : /**
85 : * Internet protocol address family
86 : */
87 : enum class IPAddressType : uint8_t
88 : {
89 : kUnknown = 0, ///< Not used.
90 : #if INET_CONFIG_ENABLE_IPV4
91 : kIPv4 = 1, ///< Internet protocol version 4.
92 : #endif // INET_CONFIG_ENABLE_IPV4
93 : kIPv6 = 2, ///< Internet protocol version 6.
94 : kAny = 3 ///< The unspecified internet address (independent of protocol version).
95 : };
96 :
97 : /**
98 : * Internet protocol v6 multicast flags
99 : *
100 : * Values of the \c IPv6MulticastFlag type are used to call the <tt>IPAddress::MakeIPv6Multicast()</tt> methods.
101 : * They indicate the type of IPv6 multicast address to create. These numbers are registered by IETF with IANA.
102 : */
103 : enum class IPv6MulticastFlag : uint8_t
104 : {
105 : /** The multicast address is (1) transient (i.e., dynamically-assigned) rather than (0) well-known (i.e, IANA-assigned). */
106 : kTransient = 0x01,
107 :
108 : /** The multicast address is (1) based on a network prefix. */
109 : kPrefix = 0x02
110 : };
111 : using IPv6MulticastFlags = BitFlags<IPv6MulticastFlag>;
112 :
113 : #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
114 : /**
115 : * SockAddr should be used when calling any API that returns (by copying into
116 : * it) a sockaddr, because that will need enough storage that it can hold data
117 : * for any socket type.
118 : *
119 : * It can also be used when calling an API that accepts a sockaddr, to simplify
120 : * the type-punning needed.
121 : */
122 : union SockAddr
123 : {
124 : sockaddr any;
125 : sockaddr_in in;
126 : sockaddr_in6 in6;
127 : sockaddr_storage storage;
128 : };
129 :
130 : /**
131 : * SockAddrWithoutStorage can be used any time we want to do the sockaddr
132 : * type-punning but will not store the data ourselves (e.g. we're working with
133 : * an existing sockaddr pointer, and reintepret it as a
134 : * pointer-to-SockAddrWithoutStorage).
135 : */
136 : union SockAddrWithoutStorage
137 : {
138 : sockaddr any;
139 : sockaddr_in in;
140 : sockaddr_in6 in6;
141 : };
142 : #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
143 :
144 : /**
145 : * @brief Internet protocol address
146 : *
147 : * @details
148 : * The CHIP Inet Layer uses objects of this class to represent Internet
149 : * protocol addresses (independent of protocol version).
150 : *
151 : */
152 : class DLL_EXPORT IPAddress
153 : {
154 : public:
155 : /**
156 : * Maximum length of the string representation of an IP address, including a terminating NUL.
157 : */
158 : #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
159 : static constexpr uint16_t kMaxStringLength = IP6ADDR_STRLEN_MAX;
160 : #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
161 : #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
162 : static constexpr uint16_t kMaxStringLength = INET6_ADDRSTRLEN;
163 : #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
164 :
165 : #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
166 : #ifndef INET6_ADDRSTRLEN
167 : #define INET6_ADDRSTRLEN OT_IP6_ADDRESS_STRING_SIZE
168 : #endif
169 : static constexpr uint16_t kMaxStringLength = OT_IP6_ADDRESS_STRING_SIZE;
170 : #endif
171 :
172 : IPAddress() = default;
173 :
174 : #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
175 : explicit IPAddress(const ip6_addr_t & ipv6Addr);
176 : #if INET_CONFIG_ENABLE_IPV4 || LWIP_IPV4
177 : explicit IPAddress(const ip4_addr_t & ipv4Addr);
178 : explicit IPAddress(const ip_addr_t & addr);
179 : #endif // INET_CONFIG_ENABLE_IPV4
180 : #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
181 :
182 : #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
183 : explicit IPAddress(const struct in6_addr & ipv6Addr);
184 : #if INET_CONFIG_ENABLE_IPV4
185 : explicit IPAddress(const struct in_addr & ipv4Addr);
186 : #endif // INET_CONFIG_ENABLE_IPV4
187 : #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
188 :
189 : #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
190 : explicit IPAddress(const otIp6Address & ipv6Addr);
191 : #endif
192 :
193 : /**
194 : * @brief Opaque word array to contain IP addresses (independent of protocol version)
195 : *
196 : * @details
197 : * IPv6 address use all 128-bits split into four 32-bit network byte
198 : * ordered unsigned integers. IPv4 addresses are IPv4-Mapped IPv6 addresses,
199 : * i.e. the first two words are zero, the third word contains 0xFFFF in
200 : * network byte order, and the fourth word contains the IPv4
201 : * address in network byte order.
202 : */
203 : uint32_t Addr[4];
204 :
205 : /**
206 : * @brief Test whether address is IPv6 compatible.
207 : *
208 : * @details
209 : * Use this method to check if the address belongs to the IPv6 address
210 : * family. Note well: the unspecified address is not an IPv6 address.
211 : *
212 : * @retval true The address is IPv6 and not the unspecified address.
213 : * @retval false The address is IPv4 or the unspecified address.
214 : */
215 : bool IsIPv6() const;
216 :
217 : /**
218 : * @brief Test whether address is IPv6 global unicast address.
219 : *
220 : * @details
221 : * Use this method to check if the address belongs to the IPv6 address
222 : * family and has the global unicast address prefix.
223 : *
224 : * @retval true Address is IPv6 global unicast
225 : * @retval false Otherwise
226 : */
227 : bool IsIPv6GlobalUnicast() const;
228 :
229 : /**
230 : * @brief Test whether address is IPv6 unique-local address (ULA).
231 : *
232 : * @details
233 : * Use this method to check if the address belongs to the IPv6 address
234 : * family and has the reserved IPv6 unique-local address prefix.
235 : *
236 : * @retval true Address is IPv6 unique-local
237 : * @retval false Otherwise
238 : */
239 : bool IsIPv6ULA() const;
240 :
241 : /**
242 : * @brief Test whether address is IPv6 link-local address (LL).
243 : *
244 : * @details
245 : * Use this method to check if the address belongs to the IPv6 address
246 : * family and has the reserved IPv6 link-local address prefix.
247 : *
248 : * @retval true Address is IPv6 link-local
249 : * @retval false Otherwise
250 : */
251 : bool IsIPv6LinkLocal() const;
252 :
253 : /**
254 : * @brief Test whether address is IPv6 multicast.
255 : *
256 : * @details
257 : * Use this method to check if the address belongs to the IPv6 address
258 : * family and has the reserved IPv6 multicast address prefix.
259 : *
260 : * @retval true Address is IPv6 multicast
261 : * @retval false Otherwise
262 : */
263 : bool IsIPv6Multicast() const;
264 :
265 : /**
266 : * @brief Test whether address is IPv4 or IPv6 multicast.
267 : *
268 : * @details
269 : * Use this method to check if the address belongs to the IPv4 or IPv6 address
270 : * family and has the reserved IPv4 or IPv6 multicast address prefix.
271 : *
272 : * @retval true Address is IPv4 or IPv6 multicast
273 : * @retval false Otherwise
274 : */
275 : bool IsMulticast() const;
276 :
277 : /**
278 : * @brief Extract the IID of an IPv6 ULA address.
279 : *
280 : * @details
281 : * Use this method with an IPv6 unique-local address (ULA) to extract the
282 : * identifier identifier (IID), which is the least significant 64 bits of
283 : * the address.
284 : *
285 : * @return 64-bit interface identifier, or zero if the IP address is not
286 : * an IPv6 unique-local address.
287 : */
288 : uint64_t InterfaceId() const;
289 :
290 : /**
291 : * @brief Extract the 16-bit subnet identifier of an IPv6 ULA address.
292 : *
293 : * @details
294 : * Use this method with an IPv6 unique-local address (ULA) to extract the
295 : * subnet identifier, which is the least significant 16 bits of the
296 : * network prefix. The network prefix is the most significant 64 bits of
297 : * of the address. In other words, the subnet identifier is located in
298 : * the 7th and 8th bytes of a 16-byte address.
299 : *
300 : * @return 16-bit subnet identifier, or zero if the IP address is not
301 : * an IPv6 unique-local address.
302 : */
303 : uint16_t Subnet() const;
304 :
305 : /**
306 : * @brief Extract the 16-bit global network identifier of an IPv6 ULA
307 : * address.
308 : *
309 : * @details
310 : * Use this method with an IPv6 unique-local address (ULA) to extract the
311 : * global network identifier, which is the 40 bits immediately following
312 : * the distinguished ULA network prefix, i.e. fd00::/8. In other words,
313 : * the global network identifier is located in the five bytes from the 2nd
314 : * 2nd through the 6th bytes in the address.
315 : *
316 : * @return 40-bit global network identifier, or zero if the IP address
317 : * is not an IPv6 unique-local address.
318 : */
319 : uint64_t GlobalId() const;
320 :
321 : /**
322 : * @brief Extract the type of the IP address.
323 : *
324 : * @details
325 : * Use this method to return an value of the enumerated type \c
326 : * IPAddressType to indicate the type of the IP address.
327 : *
328 : * @retval IPAddressType::kIPv4 The address is IPv4.
329 : * @retval IPAddressType::kIPv6 The address is IPv6.
330 : * @retval IPAddressType::kAny The address is the unspecified address.
331 : */
332 : IPAddressType Type() const;
333 :
334 : /**
335 : * @brief Compare this IP address with another for equivalence.
336 : *
337 : * @param[in] other The address to compare.
338 : *
339 : * @retval true If equivalent to \c other
340 : * @retval false Otherwise
341 : */
342 : bool operator==(const IPAddress & other) const;
343 :
344 : /**
345 : * @brief Compare this IP address with another for inequivalence.
346 : *
347 : * @param[in] other The address to compare.
348 : *
349 : * @retval true If equivalent to \c other
350 : * @retval false Otherwise
351 : */
352 : bool operator!=(const IPAddress & other) const;
353 :
354 : /**
355 : * @brief Emit the IP address in conventional text presentation format.
356 : *
357 : * @param[out] buf The address of the emitted text.
358 : * @param[in] bufSize The size of the buffer for the emitted text.
359 : *
360 : * @details
361 : * Use <tt>ToString(char *buf, uint32_t bufSize) const</tt> to write the
362 : * conventional text presentation form of the IP address to the memory
363 : * located at \c buf and extending as much as \c bufSize bytes, including
364 : * its NUL termination character.
365 : *
366 : * Note Well: not compliant with RFC 5952 on some platforms. Specifically,
367 : * zero compression may not be applied according to section 4.2.
368 : *
369 : * @return The argument \c buf if no formatting error, or zero otherwise.
370 : */
371 : char * ToString(char * buf, uint32_t bufSize) const;
372 :
373 : /**
374 : * A version of ToString that writes to a literal and deduces how much space
375 : * it as to work with.
376 : */
377 : template <uint32_t N>
378 2365 : inline char * ToString(char (&buf)[N]) const
379 : {
380 2365 : return ToString(buf, N);
381 : }
382 :
383 : /**
384 : * @brief Scan the IP address from its conventional presentation text.
385 : *
386 : * @param[in] str The address of the emitted text.
387 : * @param[out] output The object to set to the scanned address.
388 : *
389 : * @details
390 : * Use <tt>FromString(const char *str, IPAddress& output)</tt> to
391 : * overwrite an IP address by scanning the conventional text presentation
392 : * located at \c str.
393 : *
394 : * @retval true The presentation format is valid
395 : * @retval false Otherwise
396 : */
397 : static bool FromString(const char * str, IPAddress & output);
398 :
399 : /**
400 : * @brief Scan the IP address from its conventional presentation text.
401 : *
402 : * @param[in] str A pointer to the text to be scanned.
403 : * @param[in] strLen The length of the text to be scanned.
404 : * @param[out] output The object to set to the scanned address.
405 : *
406 : * @details
407 : * Use <tt>FromString(const char *str, size_t strLen, IPAddress& output)</tt> to
408 : * overwrite an IP address by scanning the conventional text presentation
409 : * located at \c str.
410 : *
411 : * @retval true The presentation format is valid
412 : * @retval false Otherwise
413 : */
414 : static bool FromString(const char * str, size_t strLen, IPAddress & output);
415 :
416 : /**
417 : * @brief
418 : * Scan the IP address from its conventional presentation text, including
419 : * the interface ID if present. (e.g. "fe80::2%wlan0"). If no interface ID
420 : * is present, then ifaceOutput will be set to the null interface ID.
421 : *
422 : * @param[in] str A pointer to the text to be scanned.
423 : * @param[out] addrOutput The object to set to the IP address.
424 : * @param[out] ifaceOutput The object to set to the interface ID.
425 : *
426 : * @retval true The presentation format is valid
427 : * @retval false Otherwise
428 : */
429 : static bool FromString(const char * str, IPAddress & addrOutput, class InterfaceId & ifaceOutput);
430 :
431 : /**
432 : * @brief Emit the IP address in standard network representation.
433 : *
434 : * @param[in,out] p Reference to the cursor to use for writing.
435 : *
436 : * @details
437 : * Use <tt>WriteAddress(uint8_t *&p)</tt> to encode the IP address in
438 : * the binary format defined by RFC 4291 for IPv6 addresses. IPv4
439 : * addresses are encoded according to section 2.5.5.2 "IPv4-Mapped
440 : * IPv6 Address".
441 : */
442 : void WriteAddress(uint8_t *& p) const;
443 :
444 : /**
445 : * @brief Emit the IP address in standard network representation.
446 : *
447 : * @param[in,out] p Reference to the cursor to use for reading.
448 : * @param[out] output Object to receive decoded IP address.
449 : *
450 : * @details
451 : * Use <tt>ReadAddress(uint8_t *&p, IPAddress &output)</tt> to decode
452 : * the IP address at \c p to the object \c output.
453 : */
454 : static void ReadAddress(const uint8_t *& p, IPAddress & output);
455 :
456 : /**
457 : * @brief Test whether address is IPv4 compatible.
458 : *
459 : * @details
460 : * Use this method to check if the address belongs to the IPv4 address
461 : * family. Note well: the unspecified address is not an IPv4 address.
462 : *
463 : * @retval true The address is IPv4 and not the unspecified address.
464 : * @retval false The address is IPv6 or the unspecified address.
465 : */
466 : bool IsIPv4() const;
467 :
468 : /**
469 : * @brief Test whether address is IPv4 multicast.
470 : *
471 : * @details
472 : * Use this method to check if the address is an IPv4 multicast
473 : * address.
474 : *
475 : * @retval true Address is the IPv4 multicast
476 : * @retval false Otherwise
477 : */
478 : bool IsIPv4Multicast() const;
479 :
480 : /**
481 : * @brief Test whether address is IPv4 broadcast.
482 : *
483 : * @details
484 : * Use this method to check if the address is the special purpose IPv4
485 : * broadcast address.
486 : *
487 : * @retval true Address is the IPv4 broadcast
488 : * @retval false Otherwise
489 : */
490 : bool IsIPv4Broadcast() const;
491 :
492 : /**
493 : * @fn ToIPv4() const
494 : *
495 : * @brief Extract the IPv4 address as a platform data structure.
496 : *
497 : * @details
498 : * Use <tt>ToIPv4() const</tt> to extract the content as an IPv4 address,
499 : * if possible. IPv6 addresses and the unspecified address are
500 : * extracted as <tt>0.0.0.0</tt>.
501 : *
502 : * The result is either of type <tt>struct in_addr</tt> (on POSIX) or
503 : * <tt>ip4_addr_t</tt> (on LwIP).
504 : *
505 : * @return The encapsulated IPv4 address, or \c 0.0.0.0 if the address is
506 : * either unspecified or not an IPv4 address.
507 : */
508 :
509 : /**
510 : * @fn ToIPv6() const
511 : *
512 : * @brief Extract the IPv6 address as a platform data structure.
513 : *
514 : * @details
515 : * Use <tt>ToIPv6() const</tt> to extract the content as an IPv6 address,
516 : * if possible. IPv4 addresses and the unspecified address are extracted
517 : * as <tt>[::]</tt>.
518 : *
519 : * The result is either of type <tt>struct in6_addr</tt> (on POSIX) or
520 : * <tt>ip6_addr_t</tt> (on LwIP).
521 : *
522 : * @return The encapsulated IPv4 address, or \c [::] if the address is
523 : * either unspecified or not an IPv4 address.
524 : */
525 :
526 : #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
527 :
528 : /**
529 : * @fn ToLwIPAddr() const
530 : *
531 : * @brief Extract the IP address as a LwIP ip_addr_t structure.
532 : *
533 : * @details
534 : * Use <tt>ToLwIPAddr() const</tt> to extract the content as an IP address,
535 : * if possible.
536 : *
537 : * @return An LwIP ip_addr_t structure corresponding to the IP address.
538 : */
539 : ip_addr_t ToLwIPAddr(void) const;
540 :
541 : /**
542 : * Extract the IP address as a LwIP ip_addr_t structure.
543 : *
544 : * If the IP address is Any, the result is IP6_ADDR_ANY unless the requested addressType is kIPv4.
545 : * If the requested addressType is IPAddressType::kAny, extracts the IP address as an LwIP ip_addr_t structure.
546 : * Otherwise, returns INET_ERROR_WRONG_ADDRESS_TYPE if the requested addressType does not match the IP address.
547 : */
548 : CHIP_ERROR ToLwIPAddr(IPAddressType addressType, ip_addr_t & outAddress) const;
549 :
550 : /**
551 : * @brief Convert the INET layer address type to its underlying LwIP type.
552 : *
553 : * @details
554 : * Use <tt>ToLwIPAddrType(IPAddressType)</tt> to convert the IP address type
555 : * to its underlying LwIP address type code.
556 : */
557 : static lwip_ip_addr_type ToLwIPAddrType(IPAddressType);
558 :
559 : ip6_addr_t ToIPv6(void) const;
560 :
561 : #if INET_CONFIG_ENABLE_IPV4
562 : ip4_addr_t ToIPv4(void) const;
563 : #endif // INET_CONFIG_ENABLE_IPV4
564 :
565 : #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
566 :
567 : #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
568 :
569 : struct in6_addr ToIPv6() const;
570 :
571 : #if INET_CONFIG_ENABLE_IPV4
572 : struct in_addr ToIPv4() const;
573 : #endif // INET_CONFIG_ENABLE_IPV4
574 :
575 : /**
576 : * Get the IP address from a SockAddr.
577 : */
578 : static CHIP_ERROR GetIPAddressFromSockAddr(const SockAddrWithoutStorage & sockaddr, IPAddress & outIPAddress);
579 947 : static CHIP_ERROR GetIPAddressFromSockAddr(const sockaddr & sockaddr, IPAddress & outIPAddress)
580 : {
581 947 : return GetIPAddressFromSockAddr(reinterpret_cast<const SockAddrWithoutStorage &>(sockaddr), outIPAddress);
582 : }
583 428 : static IPAddress FromSockAddr(const sockaddr_in6 & sockaddr) { return IPAddress(sockaddr.sin6_addr); }
584 : #if INET_CONFIG_ENABLE_IPV4
585 519 : static IPAddress FromSockAddr(const sockaddr_in & sockaddr) { return IPAddress(sockaddr.sin_addr); }
586 : #endif // INET_CONFIG_ENABLE_IPV4
587 :
588 : #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_USE_NETWORK_FRAMEWORK
589 :
590 : #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
591 : otIp6Address ToIPv6() const;
592 : static IPAddress FromOtAddr(const otIp6Address & address);
593 : #endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
594 :
595 : /**
596 : * @brief Construct an IPv6 unique-local address (ULA) from its parts.
597 : *
598 : * @details
599 : * Use <tt>MakeULA(uint64_t globalId, uint16_t subnet, uint64_t
600 : * interfaceId)</tt> to construct a unique-local address (ULA) with global
601 : * network identifier \c globalId, subnet identifier \c subnet and
602 : * interface identifier (IID) \c interfaceId.
603 : *
604 : * @return The constructed IP address.
605 : */
606 : static IPAddress MakeULA(uint64_t globalId, uint16_t subnet, uint64_t interfaceId);
607 :
608 : /**
609 : * @brief Construct an IPv6 link-local address (LL) from its IID.
610 : *
611 : * @details
612 : * Use <tt>MakeLLA(uint64_t interfaceId)</tt> to construct an IPv6
613 : * link-local address (LL) with interface identifier \c interfaceId.
614 : *
615 : * @return The constructed IP address.
616 : */
617 : static IPAddress MakeLLA(uint64_t interfaceId);
618 :
619 : /**
620 : * @brief Construct an IPv6 multicast address from its parts.
621 : *
622 : * @details
623 : * Use <tt>MakeIPv6Multicast(uint8_t flags, uint8_t scope,
624 : * uint8_t groupId[14])</tt> to construct an IPv6 multicast
625 : * address with \c flags for routing scope \c scope and group
626 : * identifier octets \c groupId.
627 : *
628 : * @return The constructed IP address.
629 : */
630 : static IPAddress MakeIPv6Multicast(IPv6MulticastFlags aFlags, uint8_t aScope,
631 : const uint8_t aGroupId[NL_INET_IPV6_MCAST_GROUP_LEN_IN_BYTES]);
632 :
633 : /**
634 : * @brief Construct an IPv6 multicast address from its parts.
635 : *
636 : * @details
637 : * Use <tt>MakeIPv6Multicast(uint8_t flags, uint8_t scope,
638 : * uint32_t groupId)</tt> to construct an IPv6 multicast
639 : * address with \c flags for routing scope \c scope and group
640 : * identifier \c groupId.
641 : *
642 : * @return The constructed IP address.
643 : */
644 : static IPAddress MakeIPv6Multicast(IPv6MulticastFlags aFlags, uint8_t aScope, uint32_t aGroupId);
645 :
646 : /**
647 : * @brief Construct a well-known IPv6 multicast address from its parts.
648 : *
649 : * @details
650 : * Use <tt>MakeIPv6WellKnownMulticast(uint8_t scope, uint32_t
651 : * groupId)</tt> to construct an IPv6 multicast address for
652 : * routing scope \c scope and group identifier \c groupId.
653 : *
654 : * @return The constructed IP address.
655 : */
656 : static IPAddress MakeIPv6WellKnownMulticast(uint8_t aScope, uint32_t aGroupId);
657 :
658 : /**
659 : * @brief Construct a transient IPv6 multicast address from its parts.
660 : *
661 : * @details
662 : * Use <tt>MakeIPv6TransientMulticast(uint8_t flags, uint8_t scope,
663 : * uint8_t groupId[14])</tt> to construct a transient IPv6
664 : * multicast address with \c flags for routing scope \c scope and
665 : * group identifier octets \c groupId.
666 : *
667 : * @return The constructed IP address.
668 : */
669 : static IPAddress MakeIPv6TransientMulticast(IPv6MulticastFlags aFlags, uint8_t aScope,
670 : const uint8_t aGroupId[NL_INET_IPV6_MCAST_GROUP_LEN_IN_BYTES]);
671 :
672 : /**
673 : * @brief Construct a transient, prefix IPv6 multicast address from its parts.
674 : *
675 : * @details
676 : * Use <tt>MakeIPv6PrefixMulticast(uint8_t scope, uint8_t
677 : * prefixlen, const uint64_t prefix, uint32_t groupId)</tt> to
678 : * construct a transient, prefix IPv6 multicast address with for
679 : * routing scope \c scope and group identifier octets \c groupId,
680 : * qualified by the prefix \c prefix of length \c prefixlen bits.
681 : *
682 : * @return The constructed IP address.
683 : */
684 : static IPAddress MakeIPv6PrefixMulticast(uint8_t aScope, uint8_t aPrefixLength, const uint64_t & aPrefix, uint32_t aGroupId);
685 :
686 : /**
687 : * @brief Construct an IPv4 broadcast address.
688 : *
689 : * @return The constructed IP address.
690 : */
691 : static IPAddress MakeIPv4Broadcast();
692 :
693 : /**
694 : * @brief The distinguished unspecified IP address object.
695 : *
696 : * @details
697 : * This object is used as a constant for equivalence comparisons. It must
698 : * not be modified by users of the CHIP Inet Layer.
699 : */
700 : static IPAddress Any;
701 :
702 : /**
703 : * Creates a loopback of the specified type. Type MUST be IPv6/v4.
704 : *
705 : * If type is anything else (or IPv4 is not available) an IPv6
706 : * loopback will be created.
707 : */
708 : static IPAddress Loopback(IPAddressType type);
709 : };
710 :
711 : static_assert(std::is_trivial<IPAddress>::value, "IPAddress is not trivial");
712 :
713 : } // namespace Inet
714 : } // namespace chip
|