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