esp_netif_ip_addr.h 5.2 KB

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