Matter SDK Coverage Report
Current view: top level - inet - IPAddress.h (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 100.0 % 6 6
Test Date: 2025-01-17 19:00:11 Functions: 100.0 % 5 5

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

Generated by: LCOV version 2.0-1