esp_netif_ip_addr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP_NETIF_IP_ADDR_H_
  14. #define _ESP_NETIF_IP_ADDR_H_
  15. #include <machine/endian.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #if BYTE_ORDER == BIG_ENDIAN
  20. #define esp_netif_htonl(x) ((uint32_t)(x))
  21. #else
  22. #define esp_netif_htonl(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \
  23. (((x) & (uint32_t)0x0000ff00UL) << 8) | \
  24. (((x) & (uint32_t)0x00ff0000UL) >> 8) | \
  25. (((x) & (uint32_t)0xff000000UL) >> 24))
  26. #endif
  27. #define esp_netif_ip4_makeu32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
  28. ((uint32_t)((b) & 0xff) << 16) | \
  29. ((uint32_t)((c) & 0xff) << 8) | \
  30. (uint32_t)((d) & 0xff))
  31. // Access address in 16-bit block
  32. #define ESP_IP6_ADDR_BLOCK1(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0]) >> 16) & 0xffff))
  33. #define ESP_IP6_ADDR_BLOCK2(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0])) & 0xffff))
  34. #define ESP_IP6_ADDR_BLOCK3(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1]) >> 16) & 0xffff))
  35. #define ESP_IP6_ADDR_BLOCK4(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1])) & 0xffff))
  36. #define ESP_IP6_ADDR_BLOCK5(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2]) >> 16) & 0xffff))
  37. #define ESP_IP6_ADDR_BLOCK6(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2])) & 0xffff))
  38. #define ESP_IP6_ADDR_BLOCK7(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3]) >> 16) & 0xffff))
  39. #define ESP_IP6_ADDR_BLOCK8(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3])) & 0xffff))
  40. #define IPSTR "%d.%d.%d.%d"
  41. #define esp_ip4_addr_get_byte(ipaddr, idx) (((const uint8_t*)(&(ipaddr)->addr))[idx])
  42. #define esp_ip4_addr1(ipaddr) esp_ip4_addr_get_byte(ipaddr, 0)
  43. #define esp_ip4_addr2(ipaddr) esp_ip4_addr_get_byte(ipaddr, 1)
  44. #define esp_ip4_addr3(ipaddr) esp_ip4_addr_get_byte(ipaddr, 2)
  45. #define esp_ip4_addr4(ipaddr) esp_ip4_addr_get_byte(ipaddr, 3)
  46. #define esp_ip4_addr1_16(ipaddr) ((uint16_t)esp_ip4_addr1(ipaddr))
  47. #define esp_ip4_addr2_16(ipaddr) ((uint16_t)esp_ip4_addr2(ipaddr))
  48. #define esp_ip4_addr3_16(ipaddr) ((uint16_t)esp_ip4_addr3(ipaddr))
  49. #define esp_ip4_addr4_16(ipaddr) ((uint16_t)esp_ip4_addr4(ipaddr))
  50. #define IP2STR(ipaddr) esp_ip4_addr1_16(ipaddr), \
  51. esp_ip4_addr2_16(ipaddr), \
  52. esp_ip4_addr3_16(ipaddr), \
  53. esp_ip4_addr4_16(ipaddr)
  54. #define IPV6STR "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
  55. #define IPV62STR(ipaddr) ESP_IP6_ADDR_BLOCK1(&(ipaddr)), \
  56. ESP_IP6_ADDR_BLOCK2(&(ipaddr)), \
  57. ESP_IP6_ADDR_BLOCK3(&(ipaddr)), \
  58. ESP_IP6_ADDR_BLOCK4(&(ipaddr)), \
  59. ESP_IP6_ADDR_BLOCK5(&(ipaddr)), \
  60. ESP_IP6_ADDR_BLOCK6(&(ipaddr)), \
  61. ESP_IP6_ADDR_BLOCK7(&(ipaddr)), \
  62. ESP_IP6_ADDR_BLOCK8(&(ipaddr))
  63. #define ESP_IPADDR_TYPE_V4 0U
  64. #define ESP_IPADDR_TYPE_V6 6U
  65. #define ESP_IPADDR_TYPE_ANY 46U
  66. #define ESP_IP4TOUINT32(a,b,c,d) (((uint32_t)((a) & 0xffU) << 24) | \
  67. ((uint32_t)((b) & 0xffU) << 16) | \
  68. ((uint32_t)((c) & 0xffU) << 8) | \
  69. (uint32_t)((d) & 0xffU))
  70. #define ESP_IP4TOADDR(a,b,c,d) esp_netif_htonl(ESP_IP4TOUINT32(a, b, c, d))
  71. struct esp_ip6_addr {
  72. uint32_t addr[4];
  73. uint8_t zone;
  74. };
  75. struct esp_ip4_addr {
  76. uint32_t addr;
  77. };
  78. typedef struct esp_ip4_addr esp_ip4_addr_t;
  79. typedef struct esp_ip6_addr esp_ip6_addr_t;
  80. typedef struct _ip_addr {
  81. union {
  82. esp_ip6_addr_t ip6;
  83. esp_ip4_addr_t ip4;
  84. } u_addr;
  85. uint8_t type;
  86. } esp_ip_addr_t;
  87. typedef enum {
  88. ESP_IP6_ADDR_IS_UNKNOWN,
  89. ESP_IP6_ADDR_IS_GLOBAL,
  90. ESP_IP6_ADDR_IS_LINK_LOCAL,
  91. ESP_IP6_ADDR_IS_SITE_LOCAL,
  92. ESP_IP6_ADDR_IS_UNIQUE_LOCAL,
  93. ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6
  94. } esp_ip6_addr_type_t;
  95. /**
  96. * @brief Get the IPv6 address type
  97. *
  98. * @param[in] ip6_addr IPv6 type
  99. *
  100. * @return IPv6 type in form of enum esp_ip6_addr_type_t
  101. */
  102. esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif //_ESP_NETIF_IP_ADDR_H_