1 /***************************************************************************/ /**
2  * @file
3  * @brief IP networking types
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 #pragma once
31 #include "stdint.h"
32 
33 /**
34  * @addtogroup SL_NET_CONSTANTS
35  * @{
36  */
37 
38 /// Enumeration of IP version
39 typedef enum {
40   SL_IPV4_VERSION = 4, ///< IPv4 version
41   SL_IPV6_VERSION = 6  ///< IPv6 version
42 } sl_ip_version_t;
43 
44 /// Enumeration of IP address types/// Enumeration of IP address types.
45 typedef enum {
46   SL_IPV4            = (1 << 2),    ///< IPv4 address
47   SL_IPV6            = (1 << 3),    ///< IPv6, Unspecified sub-type
48   SL_IPV6_LINK_LOCAL = SL_IPV6 + 1, ///< IPv6, Link local address
49   SL_IPV6_SITE_LOCAL = SL_IPV6 + 2, ///< IPv6, Site local address
50   SL_IPV6_GLOBAL     = SL_IPV6 + 3, ///< IPv6, Global address
51   SL_INVALID_IP      = 0            ///< This enumeration value represents an invalid IP address.
52 } sl_ip_address_type_t;
53 
54 /// Enumeration of IP Management type
55 typedef enum {
56   SL_IP_MANAGEMENT_STATIC_IP = 1, ///< Assign STATIC IP address to an interface
57   SL_IP_MANAGEMENT_DHCP,          ///< Assign IP address to an interface dynamically using DHCP
58   SL_IP_MANAGEMENT_LINK_LOCAL     ///< Assign IP address using link-local addressing
59 } sl_ip_management_t;
60 
61 /** @} */
62 
63 /** \addtogroup SL_NET_TYPES */
64 /** @{ */
65 
66 /// IPv4 address object
67 typedef union {
68   uint32_t value;   ///< IPv4 address as a uint32_t
69   uint8_t bytes[4]; ///< IPv4 address as uint8_t[4]
70 } sl_ipv4_address_t;
71 
72 /// IPv6 address object
73 typedef union {
74   uint32_t value[4]; ///< IPv6 address as a uint32_t[4]
75   uint8_t bytes[16]; ///< IPv6 address as uint8_t[16]
76 } sl_ipv6_address_t;
77 
78 /// Generic IP Address Structure. Supports both IPv4 and IPv6 addresses
79 #pragma pack(1)
80 typedef struct {
81   /// IP address object
82   union {
83     sl_ipv4_address_t v4; ///< IPv4 address
84     sl_ipv6_address_t v6; ///< IPv6 address
85   } ip;
86 
87   sl_ip_address_type_t type; ///< IP address type
88 } sl_ip_address_t;
89 #pragma pack()
90 
91 /** @} */
92 
93 /// Macro to assist initializing an IPv4 address
94 #define SL_IPV4_ADDRESS(a, b, c, d)                \
95   {                                                \
96     .ip.v4.bytes = { a, b, c, d }, .type = SL_IPV4 \
97   }
98