components: Use CONFIG_LWIP_IPV6 to strip IPv6 function in components

This commit is contained in:
yuanjm 2021-01-18 19:14:51 +08:00 committed by bot
parent 90696dad89
commit 1ef91c72e2
14 changed files with 129 additions and 33 deletions

View File

@ -210,12 +210,16 @@ static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *s
struct sockaddr_in *p = (struct sockaddr_in *)addrinfo->ai_addr; struct sockaddr_in *p = (struct sockaddr_in *)addrinfo->ai_addr;
p->sin_port = htons(port); p->sin_port = htons(port);
addr_ptr = p; addr_ptr = p;
} else if (addrinfo->ai_family == AF_INET6) { }
#if CONFIG_LWIP_IPV6
else if (addrinfo->ai_family == AF_INET6) {
struct sockaddr_in6 *p = (struct sockaddr_in6 *)addrinfo->ai_addr; struct sockaddr_in6 *p = (struct sockaddr_in6 *)addrinfo->ai_addr;
p->sin6_port = htons(port); p->sin6_port = htons(port);
p->sin6_family = AF_INET6; p->sin6_family = AF_INET6;
addr_ptr = p; addr_ptr = p;
} else { }
#endif
else {
ESP_LOGE(TAG, "Unsupported protocol family %d", addrinfo->ai_family); ESP_LOGE(TAG, "Unsupported protocol family %d", addrinfo->ai_family);
ret = ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY; ret = ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY;
goto err_freesocket; goto err_freesocket;

View File

@ -261,19 +261,31 @@ static void httpd_thread(void *arg)
static esp_err_t httpd_server_init(struct httpd_data *hd) static esp_err_t httpd_server_init(struct httpd_data *hd)
{ {
#if CONFIG_LWIP_IPV6
int fd = socket(PF_INET6, SOCK_STREAM, 0); int fd = socket(PF_INET6, SOCK_STREAM, 0);
#else
int fd = socket(PF_INET, SOCK_STREAM, 0);
#endif
if (fd < 0) { if (fd < 0) {
ESP_LOGE(TAG, LOG_FMT("error in socket (%d)"), errno); ESP_LOGE(TAG, LOG_FMT("error in socket (%d)"), errno);
return ESP_FAIL; return ESP_FAIL;
} }
#if CONFIG_LWIP_IPV6
struct in6_addr inaddr_any = IN6ADDR_ANY_INIT; struct in6_addr inaddr_any = IN6ADDR_ANY_INIT;
struct sockaddr_in6 serv_addr = { struct sockaddr_in6 serv_addr = {
.sin6_family = PF_INET6, .sin6_family = PF_INET6,
.sin6_addr = inaddr_any, .sin6_addr = inaddr_any,
.sin6_port = htons(hd->config.server_port) .sin6_port = htons(hd->config.server_port)
}; };
#else
struct sockaddr_in serv_addr = {
.sin_family = PF_INET,
.sin_addr = {
.s_addr = htonl(INADDR_ANY)
},
.sin_port = htons(hd->config.server_port)
};
#endif
/* Enable SO_REUSEADDR to allow binding to the same /* Enable SO_REUSEADDR to allow binding to the same
* address and port when restarting the server */ * address and port when restarting the server */
int enable = 1; int enable = 1;

View File

@ -615,7 +615,7 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty
/** @addtogroup ESP_NETIF_NET_IP /** @addtogroup ESP_NETIF_NET_IP
* @{ * @{
*/ */
#if CONFIG_LWIP_IPV6
/** /**
* @brief Create interface link-local IPv6 address * @brief Create interface link-local IPv6 address
* *
@ -674,6 +674,7 @@ esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip
* number of returned IPv6 addresses * number of returned IPv6 addresses
*/ */
int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]); int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]);
#endif
/** /**
* @brief Sets IPv4 address to the specified octets * @brief Sets IPv4 address to the specified octets

View File

@ -40,6 +40,7 @@ typedef struct esp_netif_slip_config {
*/ */
esp_err_t esp_netif_slip_set_params(esp_netif_t *netif, const esp_netif_slip_config_t *config); esp_err_t esp_netif_slip_set_params(esp_netif_t *netif, const esp_netif_slip_config_t *config);
#if CONFIG_LWIP_IPV6
/** @brief Sets IPV6 address for the supplied esp-netif. /** @brief Sets IPV6 address for the supplied esp-netif.
* *
* @param[in] netif handle to slip esp-netif instance * @param[in] netif handle to slip esp-netif instance
@ -48,6 +49,7 @@ esp_err_t esp_netif_slip_set_params(esp_netif_t *netif, const esp_netif_slip_con
* @return ESP_OK on success, ESP_ERR_ESP_NETIF_INVALID_PARAMS if netif null or not SLIP * @return ESP_OK on success, ESP_ERR_ESP_NETIF_INVALID_PARAMS if netif null or not SLIP
*/ */
esp_err_t esp_netif_slip_set_ipv6(esp_netif_t *netif, const esp_ip6_addr_t *ipv6); esp_err_t esp_netif_slip_set_ipv6(esp_netif_t *netif, const esp_ip6_addr_t *ipv6);
#endif
/** /**
* @brief Data path API to write raw packet ous the SLIP interface * @brief Data path API to write raw packet ous the SLIP interface

View File

@ -1241,12 +1241,14 @@ static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg)
esp_netif_reset_ip_info(esp_netif); esp_netif_reset_ip_info(esp_netif);
} }
#if CONFIG_LWIP_IPV6
for(int8_t i = 0 ;i < LWIP_IPV6_NUM_ADDRESSES ;i++) { for(int8_t i = 0 ;i < LWIP_IPV6_NUM_ADDRESSES ;i++) {
netif_ip6_addr_set(lwip_netif, i, IP6_ADDR_ANY6); netif_ip6_addr_set(lwip_netif, i, IP6_ADDR_ANY6);
netif_ip6_addr_set_valid_life(lwip_netif, i, 0); netif_ip6_addr_set_valid_life(lwip_netif, i, 0);
netif_ip6_addr_set_pref_life(lwip_netif, i, 0); netif_ip6_addr_set_pref_life(lwip_netif, i, 0);
netif_ip6_addr_set_state(lwip_netif, i, IP6_ADDR_INVALID); netif_ip6_addr_set_state(lwip_netif, i, IP6_ADDR_INVALID);
} }
#endif
netif_set_addr(lwip_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); netif_set_addr(lwip_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
netif_set_down(lwip_netif); netif_set_down(lwip_netif);
@ -1424,8 +1426,9 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
ESP_LOGD(TAG, "set dns if=%p type=%d dns=%x", esp_netif, type, dns->ip.u_addr.ip4.addr); ESP_LOGD(TAG, "set dns if=%p type=%d dns=%x", esp_netif, type, dns->ip.u_addr.ip4.addr);
ip_addr_t *lwip_ip = (ip_addr_t*)&dns->ip; ip_addr_t *lwip_ip = (ip_addr_t*)&dns->ip;
#if CONFIG_LWIP_IPV6 && LWIP_IPV4
lwip_ip->type = IPADDR_TYPE_V4; lwip_ip->type = IPADDR_TYPE_V4;
#endif
if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
// if DHCP server configured to set DNS in dhcps API // if DHCP server configured to set DNS in dhcps API
if (type != ESP_NETIF_DNS_MAIN) { if (type != ESP_NETIF_DNS_MAIN) {
@ -1488,7 +1491,11 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty
if (dns_ip == IP_ADDR_ANY) { if (dns_ip == IP_ADDR_ANY) {
return ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED; return ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED;
} }
#if CONFIG_LWIP_IPV6
memcpy(&dns->ip.u_addr.ip4, &dns_ip->u_addr.ip4, sizeof(ip4_addr_t)); memcpy(&dns->ip.u_addr.ip4, &dns_ip->u_addr.ip4, sizeof(ip4_addr_t));
#else
memcpy(&dns->ip.u_addr.ip4, &dns_ip->addr, sizeof(ip4_addr_t));
#endif
return ESP_OK; return ESP_OK;
} }
@ -1499,6 +1506,7 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty
return esp_netif_lwip_ipc_call(esp_netif_get_dns_info_api, esp_netif, (void *)&dns_param); return esp_netif_lwip_ipc_call(esp_netif_get_dns_info_api, esp_netif, (void *)&dns_param);
} }
#if CONFIG_LWIP_IPV6
esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr) esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr)
{ {
ip6_addr_t* lwip_ip6_info = (ip6_addr_t*)ip6_addr; ip6_addr_t* lwip_ip6_info = (ip6_addr_t*)ip6_addr;
@ -1625,6 +1633,7 @@ int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[])
} }
return addr_count; return addr_count;
} }
#endif
esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif) esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif)
{ {

View File

@ -101,10 +101,12 @@ esp_err_t esp_netif_start_slip(esp_netif_t *esp_netif)
// Set the netif up // Set the netif up
netif_set_up(esp_netif->lwip_netif); netif_set_up(esp_netif->lwip_netif);
netif_set_link_up(esp_netif->lwip_netif); netif_set_link_up(esp_netif->lwip_netif);
#if CONFIG_LWIP_IPV6
int8_t addr_index = 0; int8_t addr_index = 0;
netif_ip6_addr_set(esp_netif->lwip_netif, addr_index, (ip6_addr_t *)&slip_ctx->addr); netif_ip6_addr_set(esp_netif->lwip_netif, addr_index, (ip6_addr_t *)&slip_ctx->addr);
netif_ip6_addr_set_state(esp_netif->lwip_netif, addr_index, IP6_ADDR_VALID); netif_ip6_addr_set_state(esp_netif->lwip_netif, addr_index, IP6_ADDR_VALID);
#endif
return ESP_OK; return ESP_OK;
} }
@ -125,12 +127,13 @@ esp_err_t esp_netif_slip_set_params(esp_netif_t *netif, const esp_netif_slip_con
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
memcpy(&slip_ctx->addr, &slip_config->ip6_addr, sizeof(ip6_addr_t)); memcpy(&slip_ctx->addr, &slip_config->ip6_addr, sizeof(esp_ip6_addr_t));
return ESP_OK; return ESP_OK;
} }
#if CONFIG_LWIP_IPV6
esp_err_t esp_netif_slip_set_ipv6(esp_netif_t *netif, const esp_ip6_addr_t *ipv6) esp_err_t esp_netif_slip_set_ipv6(esp_netif_t *netif, const esp_ip6_addr_t *ipv6)
{ {
lwip_slip_ctx_t *slip_ctx = (lwip_slip_ctx_t *)netif->related_data; lwip_slip_ctx_t *slip_ctx = (lwip_slip_ctx_t *)netif->related_data;
@ -150,7 +153,7 @@ esp_err_t esp_netif_slip_set_ipv6(esp_netif_t *netif, const esp_ip6_addr_t *ipv6
return ESP_OK; return ESP_OK;
} }
#endif
/** /**
* @brief Write incoming serial data to the SLIP interface * @brief Write incoming serial data to the SLIP interface
@ -198,7 +201,11 @@ void esp_netif_lwip_slip_raw_output(esp_netif_t *slip_netif, void *buffer, size_
}; };
// Call slip if output function to feed data out slip interface // Call slip if output function to feed data out slip interface
#if CONFIG_LWIP_IPV6
lwip_netif->output_ip6(lwip_netif, &p, NULL); lwip_netif->output_ip6(lwip_netif, &p, NULL);
#else
lwip_netif->output(lwip_netif, &p, NULL);
#endif
} }
/** /**

View File

@ -432,11 +432,14 @@ static BOOL xMBTCPPortMasterCheckHost(const CHAR* pcHostStr, ip_addr_t* pxHostAd
struct in_addr addr4 = ((struct sockaddr_in *) (pxAddrList->ai_addr))->sin_addr; struct in_addr addr4 = ((struct sockaddr_in *) (pxAddrList->ai_addr))->sin_addr;
inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4); inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4);
pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr)); pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr));
} else { }
#if CONFIG_LWIP_IPV6
else {
struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxAddrList->ai_addr))->sin6_addr; struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxAddrList->ai_addr))->sin6_addr;
inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6); inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6);
pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr)); pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr));
} }
#endif
if (pxHostAddr) { if (pxHostAddr) {
*pxHostAddr = xTargetAddr; *pxHostAddr = xTargetAddr;
} }
@ -496,7 +499,9 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
struct in_addr addr4 = ((struct sockaddr_in *) (pxCurAddr->ai_addr))->sin_addr; struct in_addr addr4 = ((struct sockaddr_in *) (pxCurAddr->ai_addr))->sin_addr;
inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4); inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4);
pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr)); pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr));
} else if (pxCurAddr->ai_family == AF_INET6) { }
#if CONFIG_LWIP_IPV6
else if (pxCurAddr->ai_family == AF_INET6) {
struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_addr; struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_addr;
inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6); inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6);
pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr)); pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr));
@ -504,7 +509,7 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_scope_id = ((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_scope_id =
esp_netif_get_netif_impl_index(xMbPortConfig.pvNetIface); esp_netif_get_netif_impl_index(xMbPortConfig.pvNetIface);
} }
#endif
if (pxInfo->xSockId <= 0) { if (pxInfo->xSockId <= 0) {
pxInfo->xSockId = socket(pxCurAddr->ai_family, pxCurAddr->ai_socktype, pxCurAddr->ai_protocol); pxInfo->xSockId = socket(pxCurAddr->ai_family, pxCurAddr->ai_socktype, pxCurAddr->ai_protocol);
if (pxInfo->xSockId < 0) { if (pxInfo->xSockId < 0) {

View File

@ -183,12 +183,11 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
MB_PORT_CHECK((xListenSockId > 0), -1, "Incorrect listen socket ID."); MB_PORT_CHECK((xListenSockId > 0), -1, "Incorrect listen socket ID.");
// Address structure large enough for both IPv4 or IPv6 address // Address structure large enough for both IPv4 or IPv6 address
struct sockaddr_in6 xSrcAddr; struct sockaddr_storage xSrcAddr;
CHAR cAddrStr[128]; CHAR cAddrStr[128];
int xSockId = -1; int xSockId = -1;
CHAR* pcStr = NULL; CHAR* pcStr = NULL;
socklen_t xSize = sizeof(struct sockaddr_in6); socklen_t xSize = sizeof(struct sockaddr_storage);
// Accept new socket connection if not active // Accept new socket connection if not active
xSockId = accept(xListenSockId, (struct sockaddr *)&xSrcAddr, &xSize); xSockId = accept(xListenSockId, (struct sockaddr *)&xSrcAddr, &xSize);
@ -197,11 +196,14 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
close(xSockId); close(xSockId);
} else { } else {
// Get the sender's ip address as string // Get the sender's ip address as string
if (xSrcAddr.sin6_family == PF_INET) { if (xSrcAddr.ss_family == PF_INET) {
inet_ntoa_r(((struct sockaddr_in *)&xSrcAddr)->sin_addr.s_addr, cAddrStr, sizeof(cAddrStr) - 1); inet_ntoa_r(((struct sockaddr_in *)&xSrcAddr)->sin_addr.s_addr, cAddrStr, sizeof(cAddrStr) - 1);
} else if (xSrcAddr.sin6_family == PF_INET6) {
inet6_ntoa_r(xSrcAddr.sin6_addr, cAddrStr, sizeof(cAddrStr) - 1);
} }
#if CONFIG_LWIP_IPV6
else if (xSrcAddr.ss_family == PF_INET6) {
inet6_ntoa_r(((struct sockaddr_in6 *)&xSrcAddr)->sin6_addr, cAddrStr, sizeof(cAddrStr) - 1);
}
#endif
ESP_LOGI(MB_TCP_SLAVE_PORT_TAG, "Socket (#%d), accept client connection from address: %s", xSockId, cAddrStr); ESP_LOGI(MB_TCP_SLAVE_PORT_TAG, "Socket (#%d), accept client connection from address: %s", xSockId, cAddrStr);
pcStr = calloc(1, strlen(cAddrStr) + 1); pcStr = calloc(1, strlen(cAddrStr) + 1);
if (pcStr && pcIPAddr) { if (pcStr && pcIPAddr) {

View File

@ -339,6 +339,7 @@ esp_err_t mdns_query_txt(const char * instance_name, const char * service_type,
*/ */
esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr); esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr);
#if CONFIG_LWIP_IPV6
/** /**
* @brief Query mDNS for A record * @brief Query mDNS for A record
* *
@ -353,6 +354,7 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t
* - ESP_ERR_INVALID_ARG parameter error * - ESP_ERR_INVALID_ARG parameter error
*/ */
esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr); esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr);
#endif
/** /**
* @brief System event handler * @brief System event handler

View File

@ -761,6 +761,7 @@ static uint16_t _mdns_append_a_record(uint8_t * packet, uint16_t * index, uint32
return record_length; return record_length;
} }
#if CONFIG_LWIP_IPV6
/** /**
* @brief appends AAAA record to a packet, incrementing the index * @brief appends AAAA record to a packet, incrementing the index
* *
@ -809,6 +810,7 @@ static uint16_t _mdns_append_aaaa_record(uint8_t * packet, uint16_t * index, uin
record_length += part_length; record_length += part_length;
return record_length; return record_length;
} }
#endif
/** /**
* @brief Append question to packet * @brief Append question to packet
@ -874,6 +876,7 @@ static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
return false; return false;
} }
#if CONFIG_LWIP_IPV6
/** /**
* @brief Check if IPv6 address is NULL * @brief Check if IPv6 address is NULL
*/ */
@ -888,6 +891,7 @@ static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
} }
return true; return true;
} }
#endif
/** /**
* @brief Append answer to packet * @brief Append answer to packet
@ -936,7 +940,9 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
return 2; return 2;
} }
return 1; return 1;
} else if (answer->type == MDNS_TYPE_AAAA) { }
#if CONFIG_LWIP_IPV6
else if (answer->type == MDNS_TYPE_AAAA) {
struct esp_ip6_addr if_ip6; struct esp_ip6_addr if_ip6;
if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) { if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
return 0; return 0;
@ -962,6 +968,7 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
} }
return 1; return 1;
} }
#endif
return 0; return 0;
} }
@ -1235,11 +1242,14 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip
packet->ip_protocol = ip_protocol; packet->ip_protocol = ip_protocol;
packet->port = MDNS_SERVICE_PORT; packet->port = MDNS_SERVICE_PORT;
if (ip_protocol == MDNS_IP_PROTOCOL_V4) { if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
IP_ADDR4(&packet->dst, 224, 0, 0, 251); IP4_ADDR(&packet->dst.u_addr.ip4, 224, 0, 0, 251);
} else { }
#if CONFIG_LWIP_IPV6
else {
esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
} }
#endif
return packet; return packet;
} }
@ -2250,6 +2260,7 @@ static int _mdns_check_a_collision(esp_ip4_addr_t * ip, mdns_if_t tcpip_if)
return 0;//same return 0;//same
} }
#if CONFIG_LWIP_IPV6
/** /**
* @brief Detect IPv6 address collision * @brief Detect IPv6 address collision
*/ */
@ -2283,6 +2294,7 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if)
} }
return 0;//same return 0;//same
} }
#endif
/** /**
* @brief Check if parsed name is discovery * @brief Check if parsed name is discovery
@ -2675,7 +2687,12 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE; parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED; parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
parsed_packet->id = header.id; parsed_packet->id = header.id;
#if CONFIG_LWIP_IPV6
ip_addr_copy(parsed_packet->src, packet->src); ip_addr_copy(parsed_packet->src, packet->src);
#else
ip4_addr_copy(parsed_packet->src.u_addr.ip4, packet->src.u_addr.ip4);
#endif
parsed_packet->src_port = packet->src_port; parsed_packet->src_port = packet->src_port;
if (header.questions) { if (header.questions) {
@ -2974,7 +2991,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
} }
} }
} else if (type == MDNS_TYPE_AAAA) {//ipv6 }
#if CONFIG_LWIP_IPV6
else if (type == MDNS_TYPE_AAAA) {//ipv6
esp_ip_addr_t ip6; esp_ip_addr_t ip6;
ip6.type = IPADDR_TYPE_V6; ip6.type = IPADDR_TYPE_V6;
memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE); memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
@ -3020,7 +3039,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
} }
} }
} else if (type == MDNS_TYPE_A) { }
#endif
else if (type == MDNS_TYPE_A) {
esp_ip_addr_t ip; esp_ip_addr_t ip;
ip.type = IPADDR_TYPE_V4; ip.type = IPADDR_TYPE_V4;
memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4); memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
@ -4273,13 +4294,17 @@ esp_err_t mdns_init(void)
} }
#endif #endif
uint8_t i; uint8_t i;
#if CONFIG_LWIP_IPV6
esp_ip6_addr_t tmp_addr6; esp_ip6_addr_t tmp_addr6;
#endif
esp_netif_ip_info_t if_ip_info; esp_netif_ip_info_t if_ip_info;
for (i=0; i<MDNS_IF_MAX; i++) { for (i=0; i<MDNS_IF_MAX; i++) {
#if CONFIG_LWIP_IPV6
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) { if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6); _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
} }
#endif
if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) { if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4); _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
} }
@ -4815,6 +4840,7 @@ esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * add
return ESP_ERR_NOT_FOUND; return ESP_ERR_NOT_FOUND;
} }
#if CONFIG_LWIP_IPV6
esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t * addr) esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t * addr)
{ {
mdns_result_t * result = NULL; mdns_result_t * result = NULL;
@ -4847,6 +4873,7 @@ esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t *
mdns_query_results_free(result); mdns_query_results_free(result);
return ESP_ERR_NOT_FOUND; return ESP_ERR_NOT_FOUND;
} }
#endif
#ifdef MDNS_ENABLE_DEBUG #ifdef MDNS_ENABLE_DEBUG

View File

@ -116,6 +116,7 @@ static void register_mdns_query_a(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
} }
#if CONFIG_LWIP_IPV6
static int cmd_mdns_query_aaaa(int argc, char** argv) static int cmd_mdns_query_aaaa(int argc, char** argv)
{ {
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args); int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
@ -172,6 +173,7 @@ static void register_mdns_query_aaaa(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
} }
#endif
static struct { static struct {
struct arg_str *instance; struct arg_str *instance;
@ -1049,7 +1051,9 @@ void mdns_console_register(void)
register_mdns_service_remove_all(); register_mdns_service_remove_all();
register_mdns_query_a(); register_mdns_query_a();
#if CONFIG_LWIP_IPV6
register_mdns_query_aaaa(); register_mdns_query_aaaa();
#endif
register_mdns_query_txt(); register_mdns_query_txt();
register_mdns_query_srv(); register_mdns_query_srv();
register_mdns_query_ptr(); register_mdns_query_ptr();

View File

@ -40,7 +40,7 @@ static esp_err_t _udp_pcb_main_init(void)
} }
_pcb_main->mcast_ttl = 1; _pcb_main->mcast_ttl = 1;
_pcb_main->remote_port = MDNS_SERVICE_PORT; _pcb_main->remote_port = MDNS_SERVICE_PORT;
ip_addr_copy(_pcb_main->remote_ip, ip_addr_any_type); ip_addr_copy(_pcb_main->remote_ip, *(IP_ANY_TYPE));
udp_recv(_pcb_main, &_udp_recv, _mdns_server); udp_recv(_pcb_main, &_udp_recv, _mdns_server);
return ESP_OK; return ESP_OK;
} }
@ -75,19 +75,21 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
assert(netif); assert(netif);
if (ip_protocol == MDNS_IP_PROTOCOL_V4) { if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
ip_addr_t multicast_addr; ip4_addr_t multicast_addr;
IP_ADDR4(&multicast_addr, 224, 0, 0, 251); IP4_ADDR(&multicast_addr, 224, 0, 0, 251);
if(join){ if(join){
if (igmp_joingroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) { if (igmp_joingroup_netif(netif, &multicast_addr)) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
} else { } else {
if (igmp_leavegroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) { if (igmp_leavegroup_netif(netif, &multicast_addr)) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
} }
} else { }
#if CONFIG_LWIP_IPV6
else {
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
if(join){ if(join){
@ -100,6 +102,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
} }
} }
} }
#endif
return ESP_OK; return ESP_OK;
} }
@ -127,20 +130,29 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
packet->tcpip_if = MDNS_IF_MAX; packet->tcpip_if = MDNS_IF_MAX;
packet->pb = this_pb; packet->pb = this_pb;
packet->src_port = rport; packet->src_port = rport;
#if CONFIG_LWIP_IPV6
packet->src.type = raddr->type; packet->src.type = raddr->type;
memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr)); memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr));
#else
packet->src.type = IPADDR_TYPE_V4;
memcpy(&packet->src.u_addr.ip4, &raddr->addr, sizeof(ip_addr_t));
#endif
packet->dest.type = packet->src.type; packet->dest.type = packet->src.type;
if (packet->src.type == IPADDR_TYPE_V4) { if (packet->src.type == IPADDR_TYPE_V4) {
packet->ip_protocol = MDNS_IP_PROTOCOL_V4; packet->ip_protocol = MDNS_IP_PROTOCOL_V4;
struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN); struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
packet->dest.u_addr.ip4.addr = iphdr->dest.addr; packet->dest.u_addr.ip4.addr = iphdr->dest.addr;
} else { packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4));
}
#if CONFIG_LWIP_IPV6
else {
packet->ip_protocol = MDNS_IP_PROTOCOL_V6; packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN); struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16); memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
} }
packet->multicast = ip_addr_ismulticast(&(packet->dest)); #endif
//lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces) //lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
struct netif * netif = NULL; struct netif * netif = NULL;
@ -150,8 +162,11 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i)); netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
if (pcb && netif && netif == ip_current_input_netif ()) { if (pcb && netif && netif == ip_current_input_netif ()) {
if (packet->src.type == IPADDR_TYPE_V4) { if (packet->src.type == IPADDR_TYPE_V4) {
#if CONFIG_LWIP_IPV6
if ((packet->src.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr) != (netif->ip_addr.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr)) { if ((packet->src.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr) != (netif->ip_addr.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr)) {
//packet source is not in the same subnet #else
if ((packet->src.u_addr.ip4.addr & netif->netmask.addr) != (netif->ip_addr.addr & netif->netmask.addr)) {
#endif //packet source is not in the same subnet
pcb = NULL; pcb = NULL;
break; break;
} }

View File

@ -51,6 +51,7 @@ tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif);
*/ */
esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info);
#if CONFIG_LWIP_IPV6
/** /**
* @brief Translates to esp_netif_get_ip6_linklocal * @brief Translates to esp_netif_get_ip6_linklocal
* *
@ -68,6 +69,7 @@ esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_
* @return See esp_netif_get_ip6_global * @return See esp_netif_get_ip6_global
*/ */
esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6); esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6);
#endif
/** /**
* @brief`Translates to esp_netif_dhcpc_get_status * @brief`Translates to esp_netif_dhcpc_get_status
@ -92,12 +94,14 @@ bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if);
*/ */
esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif); esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif);
#if CONFIG_LWIP_IPV6
/** /**
* @brief Translates to esp_netif_create_ip6_linklocal * @brief Translates to esp_netif_create_ip6_linklocal
* @param tcpip_if Interface type corresponding to appropriate instance of esp-netif * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif
* @return see esp_netif_create_ip6_linklocal * @return see esp_netif_create_ip6_linklocal
*/ */
esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if); esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if);
#endif
/** /**
* @brief Compatible version of setting ethernet default handlers * @brief Compatible version of setting ethernet default handlers

View File

@ -200,6 +200,7 @@ esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_i
return esp_netif_get_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info); return esp_netif_get_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
} }
#if CONFIG_LWIP_IPV6
esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6) esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6)
{ {
return esp_netif_get_ip6_linklocal(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6); return esp_netif_get_ip6_linklocal(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6);
@ -209,6 +210,7 @@ esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *
{ {
return esp_netif_get_ip6_global(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6); return esp_netif_get_ip6_global(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6);
} }
#endif
esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status) esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status)
{ {
@ -230,12 +232,12 @@ esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif)
} }
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
#if CONFIG_LWIP_IPV6
esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if) esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if)
{ {
return esp_netif_create_ip6_linklocal(netif_from_if(tcpip_if)); return esp_netif_create_ip6_linklocal(netif_from_if(tcpip_if));
} }
#endif
esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if) esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if)
{ {
return esp_netif_dhcps_stop(netif_from_if(tcpip_if)); return esp_netif_dhcps_stop(netif_from_if(tcpip_if));