Skip to content

Commit

Permalink
fix(mdns): fix compiling issue when disabling IPv4
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Nov 3, 2023
1 parent 7c5a832 commit b02c99d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 67 deletions.
25 changes: 15 additions & 10 deletions common_components/protocol_examples_common/addr_from_stdin.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad
}
for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr));
#if LWIP_IPV4
if (cur->ai_family == AF_INET) {
*ip_protocol = IPPROTO_IP;
*addr_family = AF_INET;
Expand All @@ -50,17 +51,21 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad
return ESP_OK;

}
#if CONFIG_LWIP_IPV6
else if (cur->ai_family == AF_INET6) {
*ip_protocol = IPPROTO_IPV6;
*addr_family = AF_INET6;
// add port and interface number and return on first IPv6 match
((struct sockaddr_in6 *)dest_addr)->sin6_port = htons(port);
((struct sockaddr_in6 *)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
freeaddrinfo( addr_list );
return ESP_OK;
}
#endif // LWIP_IPV4
#if LWIP_IPV4 && LWIP_IPV6
else
#endif
#if LWIP_IPV6
if (cur->ai_family == AF_INET6) {
*ip_protocol = IPPROTO_IPV6;
*addr_family = AF_INET6;
// add port and interface number and return on first IPv6 match
((struct sockaddr_in6 *)dest_addr)->sin6_port = htons(port);
((struct sockaddr_in6 *)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
freeaddrinfo( addr_list );
return ESP_OK;
}
#endif // LWIP_IPV6
}
// no match found
freeaddrinfo( addr_list );
Expand Down
9 changes: 7 additions & 2 deletions common_components/protocol_examples_common/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
#define MAX_IP6_ADDRS_PER_NETIF (5)
#if LWIP_IPV4
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2)
#else
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR s_active_interfaces
#endif

#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK)
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
Expand Down Expand Up @@ -213,14 +217,15 @@ esp_err_t example_connect(void)
}
// iterate over active interfaces, and print out IPs of "our" netifs
esp_netif_t *netif = NULL;
esp_netif_ip_info_t ip;
for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
netif = esp_netif_next(netif);
if (is_our_netif(TAG, netif)) {
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
#if LWIP_IPV4
esp_netif_ip_info_t ip;
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));

ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
#endif
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
Expand Down
64 changes: 40 additions & 24 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mdns_networking.h"
#include "esp_log.h"
#include "esp_random.h"
#include "lwip/opt.h"

#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
#include "esp_eth.h"
Expand Down Expand Up @@ -185,7 +186,7 @@ static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if)
static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif)
{
for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
if (esp_netif == s_esp_netifs[i].netif) {
if (esp_netif == s_esp_netifs[i].netif || (s_esp_netifs[i].predefined && esp_netif == esp_netif_from_preset_if(s_esp_netifs[i].predef_if))) {
return i;
}
}
Expand Down Expand Up @@ -1361,7 +1362,9 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
} else if (answer->type == MDNS_TYPE_SDPTR) {
return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
} else if (answer->type == MDNS_TYPE_A) {
}
#if LWIP_IPV4
else if (answer->type == MDNS_TYPE_A) {
if (answer->host == &_mdns_self_host) {
esp_netif_ip_info_t if_ip_info;
if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
Expand All @@ -1388,7 +1391,8 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye);
}
}
#if CONFIG_LWIP_IPV6
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
else if (answer->type == MDNS_TYPE_AAAA) {
if (answer->host == &_mdns_self_host) {
struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS];
Expand Down Expand Up @@ -1426,7 +1430,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
answer->bye);
}
}
#endif
#endif /* LWIP_IPV6 */
return 0;
}

Expand Down Expand Up @@ -2878,6 +2882,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if)
}
}

#if LWIP_IPV4
/**
* @brief Detect IPv4 address collision
*/
Expand All @@ -2891,7 +2896,6 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
return 1;//they win
}

int ret = memcmp((uint8_t *)&if_ip_info.ip.addr, (uint8_t *)&ip->addr, sizeof(esp_ip4_addr_t));
if (ret > 0) {
return -1;//we win
Expand All @@ -2912,8 +2916,9 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
}
return 0;//same
}
#endif // LWIP_IPV4

#if CONFIG_LWIP_IPV6
#if LWIP_IPV6
/**
* @brief Detect IPv6 address collision
*/
Expand Down Expand Up @@ -2947,7 +2952,7 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if)
}
return 0;//same
}
#endif
#endif /* LWIP_IPV6 */

static bool _hostname_is_ours(const char *hostname)
{
Expand Down Expand Up @@ -3510,22 +3515,28 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)

#ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
// Check if the packet wasn't sent by us
#if LWIP_IPV4
if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) {
esp_netif_ip_info_t if_ip_info;
if (esp_netif_get_ip_info(_mdns_get_esp_netif(packet->tcpip_if), &if_ip_info) == ESP_OK &&
memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) {
return;
}
#if CONFIG_LWIP_IPV6
} else {
struct esp_ip6_addr if_ip6;
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK &&
memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
return;
}
#endif
}
#endif // LWIP_IPV4
#if LWIP_IPV4 && LWIP_IPV6
else
#endif
#if LWIP_IPV6
if (packet->ip_protocol == MDNS_IP_PROTOCOL_V6) {
struct esp_ip6_addr if_ip6;
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK &&
memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
return;
}
}
#endif // LWIP_IPV6
#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES

// Check for the minimum size of mdns packet
if (len <= MDNS_HEAD_ADDITIONAL_OFFSET) {
Expand Down Expand Up @@ -3888,7 +3899,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#if CONFIG_LWIP_IPV6
#if LWIP_IPV6
else if (type == MDNS_TYPE_AAAA) {//ipv6
esp_ip_addr_t ip6;
ip6.type = ESP_IPADDR_TYPE_V6;
Expand Down Expand Up @@ -3941,7 +3952,8 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#endif
#endif /* LWIP_IPV6 */
#if LWIP_IPV4
else if (type == MDNS_TYPE_A) {
esp_ip_addr_t ip;
ip.type = ESP_IPADDR_TYPE_V4;
Expand Down Expand Up @@ -3994,6 +4006,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#endif /* LWIP_IPV4 */
}
//end while
if (parsed_packet->authoritative) {
Expand Down Expand Up @@ -4095,6 +4108,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}

#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
#if LWIP_IPV4
if (action & MDNS_EVENT_IP4_REVERSE_LOOKUP) {
esp_netif_ip_info_t if_ip_info;
if (esp_netif_get_ip_info(_mdns_get_esp_netif(mdns_if), &if_ip_info) == ESP_OK) {
Expand All @@ -4108,14 +4122,13 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}
}
}

#ifdef CONFIG_LWIP_IPV6
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) {
esp_ip6_addr_t addr6;
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(addr6)) {
uint8_t *paddr = (uint8_t *)&addr6.addr;
const char sub[] = "ip6";
const size_t query_name_size = 4 * sizeof(addr6.addr) /* (2 nibbles + 2 dots)/per byte of IP address */ + sizeof(sub);
char *reverse_query_name = malloc(query_name_size);
if (reverse_query_name) {
char *ptr = &reverse_query_name[query_name_size]; // point to the end
Expand All @@ -4133,7 +4146,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}
}
}
#endif /* CONFIG_LWIP_IPV6 */
#endif /* LWIP_IPV6 */
#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
}

Expand Down Expand Up @@ -5484,22 +5497,25 @@ esp_err_t mdns_init(void)
#endif

uint8_t i;
#if CONFIG_LWIP_IPV6
#if LWIP_IPV6
esp_ip6_addr_t tmp_addr6;
#endif
#if LWIP_IPV4
esp_netif_ip_info_t if_ip_info;
#endif

for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
#if CONFIG_LWIP_IPV6
#if LWIP_IPV6
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);
}
#endif
#if LWIP_IPV4
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);
}
#endif
}

if (_mdns_service_task_start()) {
//service start failed!
err = ESP_FAIL;
Expand Down
Loading

0 comments on commit b02c99d

Please sign in to comment.