esp_netif_ip_addr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_NETIF_IP_ADDR_H_
  7. #define _ESP_NETIF_IP_ADDR_H_
  8. #include <stdint.h>
  9. #include <machine/endian.h>
  10. #include "sdkconfig.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #if BYTE_ORDER == BIG_ENDIAN
  15. #define esp_netif_htonl(x) ((uint32_t)(x))
  16. #else
  17. #define esp_netif_htonl(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \
  18. (((x) & (uint32_t)0x0000ff00UL) << 8) | \
  19. (((x) & (uint32_t)0x00ff0000UL) >> 8) | \
  20. (((x) & (uint32_t)0xff000000UL) >> 24))
  21. #endif
  22. #define esp_netif_ip4_makeu32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
  23. ((uint32_t)((b) & 0xff) << 16) | \
  24. ((uint32_t)((c) & 0xff) << 8) | \
  25. (uint32_t)((d) & 0xff))
  26. // Access address in 16-bit block
  27. #define ESP_IP6_ADDR_BLOCK1(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0]) >> 16) & 0xffff))
  28. #define ESP_IP6_ADDR_BLOCK2(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0])) & 0xffff))
  29. #define ESP_IP6_ADDR_BLOCK3(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1]) >> 16) & 0xffff))
  30. #define ESP_IP6_ADDR_BLOCK4(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1])) & 0xffff))
  31. #define ESP_IP6_ADDR_BLOCK5(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2]) >> 16) & 0xffff))
  32. #define ESP_IP6_ADDR_BLOCK6(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2])) & 0xffff))
  33. #define ESP_IP6_ADDR_BLOCK7(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3]) >> 16) & 0xffff))
  34. #define ESP_IP6_ADDR_BLOCK8(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3])) & 0xffff))
  35. #define IPSTR "%d.%d.%d.%d"
  36. #define esp_ip4_addr_get_byte(ipaddr, idx) (((const uint8_t*)(&(ipaddr)->addr))[idx])
  37. #define esp_ip4_addr1(ipaddr) esp_ip4_addr_get_byte(ipaddr, 0)
  38. #define esp_ip4_addr2(ipaddr) esp_ip4_addr_get_byte(ipaddr, 1)
  39. #define esp_ip4_addr3(ipaddr) esp_ip4_addr_get_byte(ipaddr, 2)
  40. #define esp_ip4_addr4(ipaddr) esp_ip4_addr_get_byte(ipaddr, 3)
  41. #define esp_ip4_addr1_16(ipaddr) ((uint16_t)esp_ip4_addr1(ipaddr))
  42. #define esp_ip4_addr2_16(ipaddr) ((uint16_t)esp_ip4_addr2(ipaddr))
  43. #define esp_ip4_addr3_16(ipaddr) ((uint16_t)esp_ip4_addr3(ipaddr))
  44. #define esp_ip4_addr4_16(ipaddr) ((uint16_t)esp_ip4_addr4(ipaddr))
  45. #define IP2STR(ipaddr) esp_ip4_addr1_16(ipaddr), \
  46. esp_ip4_addr2_16(ipaddr), \
  47. esp_ip4_addr3_16(ipaddr), \
  48. esp_ip4_addr4_16(ipaddr)
  49. #define IPV6STR "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
  50. #define IPV62STR(ipaddr) ESP_IP6_ADDR_BLOCK1(&(ipaddr)), \
  51. ESP_IP6_ADDR_BLOCK2(&(ipaddr)), \
  52. ESP_IP6_ADDR_BLOCK3(&(ipaddr)), \
  53. ESP_IP6_ADDR_BLOCK4(&(ipaddr)), \
  54. ESP_IP6_ADDR_BLOCK5(&(ipaddr)), \
  55. ESP_IP6_ADDR_BLOCK6(&(ipaddr)), \
  56. ESP_IP6_ADDR_BLOCK7(&(ipaddr)), \
  57. ESP_IP6_ADDR_BLOCK8(&(ipaddr))
  58. #define ESP_IPADDR_TYPE_V4 0U
  59. #define ESP_IPADDR_TYPE_V6 6U
  60. #define ESP_IPADDR_TYPE_ANY 46U
  61. #define ESP_IP4TOUINT32(a,b,c,d) (((uint32_t)((a) & 0xffU) << 24) | \
  62. ((uint32_t)((b) & 0xffU) << 16) | \
  63. ((uint32_t)((c) & 0xffU) << 8) | \
  64. (uint32_t)((d) & 0xffU))
  65. #define ESP_IP4TOADDR(a,b,c,d) esp_netif_htonl(ESP_IP4TOUINT32(a, b, c, d))
  66. #define ESP_IP4ADDR_INIT(a, b, c, d) { .type = ESP_IPADDR_TYPE_V4, .u_addr = { .ip4 = { .addr = ESP_IP4TOADDR(a, b, c, d) }}}
  67. #define ESP_IP6ADDR_INIT(a, b, c, d) { .type = ESP_IPADDR_TYPE_V6, .u_addr = { .ip6 = { .addr = { a, b, c, d }, .zone = 0 }}}
  68. #ifndef IP4ADDR_STRLEN_MAX
  69. #define IP4ADDR_STRLEN_MAX 16
  70. #endif
  71. #if defined(CONFIG_LWIP_IPV4) && defined(CONFIG_LWIP_IPV6)
  72. #define ESP_IP_IS_ANY(addr) ((addr.type == ESP_IPADDR_TYPE_V4 && ip4_addr_isany_val(addr.u_addr.ip4)) || \
  73. (addr.type == ESP_IPADDR_TYPE_V6 && ip6_addr_isany_val(addr.u_addr.ip6)))
  74. #elif defined(CONFIG_LWIP_IPV4)
  75. #define ESP_IP_IS_ANY(addr) (ip4_addr_isany_val(addr.u_addr.ip4))
  76. #elif defined(CONFIG_LWIP_IPV6)
  77. #define ESP_IP_IS_ANY(addr) (ip6_addr_isany_val(addr.u_addr.ip6))
  78. #endif
  79. /**
  80. * @brief IPv6 address
  81. *
  82. */
  83. struct esp_ip6_addr {
  84. uint32_t addr[4]; /*!< IPv6 address */
  85. uint8_t zone; /*!< zone ID */
  86. };
  87. /**
  88. * @brief IPv4 address
  89. *
  90. */
  91. struct esp_ip4_addr {
  92. uint32_t addr; /*!< IPv4 address */
  93. };
  94. typedef struct esp_ip4_addr esp_ip4_addr_t;
  95. typedef struct esp_ip6_addr esp_ip6_addr_t;
  96. /**
  97. * @brief IP address
  98. *
  99. */
  100. typedef struct _ip_addr {
  101. union {
  102. esp_ip6_addr_t ip6; /*!< IPv6 address type */
  103. esp_ip4_addr_t ip4; /*!< IPv4 address type */
  104. } u_addr; /*!< IP address union */
  105. uint8_t type; /*!< ipaddress type */
  106. } esp_ip_addr_t;
  107. typedef enum {
  108. ESP_IP6_ADDR_IS_UNKNOWN,
  109. ESP_IP6_ADDR_IS_GLOBAL,
  110. ESP_IP6_ADDR_IS_LINK_LOCAL,
  111. ESP_IP6_ADDR_IS_SITE_LOCAL,
  112. ESP_IP6_ADDR_IS_UNIQUE_LOCAL,
  113. ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6
  114. } esp_ip6_addr_type_t;
  115. /**
  116. * @brief Get the IPv6 address type
  117. *
  118. * @param[in] ip6_addr IPv6 type
  119. *
  120. * @return IPv6 type in form of enum esp_ip6_addr_type_t
  121. */
  122. esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr);
  123. /**
  124. * @brief Copy IP addresses
  125. *
  126. * @param[out] dest destination IP
  127. * @param[in] src source IP
  128. */
  129. static inline void esp_netif_ip_addr_copy(esp_ip_addr_t *dest, const esp_ip_addr_t *src)
  130. {
  131. dest->type = src->type;
  132. if (src->type == ESP_IPADDR_TYPE_V6) {
  133. dest->u_addr.ip6.addr[0] = src->u_addr.ip6.addr[0];
  134. dest->u_addr.ip6.addr[1] = src->u_addr.ip6.addr[1];
  135. dest->u_addr.ip6.addr[2] = src->u_addr.ip6.addr[2];
  136. dest->u_addr.ip6.addr[3] = src->u_addr.ip6.addr[3];
  137. dest->u_addr.ip6.zone = src->u_addr.ip6.zone;
  138. } else {
  139. dest->u_addr.ip4.addr = src->u_addr.ip4.addr;
  140. dest->u_addr.ip6.addr[1] = 0;
  141. dest->u_addr.ip6.addr[2] = 0;
  142. dest->u_addr.ip6.addr[3] = 0;
  143. dest->u_addr.ip6.zone = 0;
  144. }
  145. }
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif //_ESP_NETIF_IP_ADDR_H_