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