Skip to content

Commit

Permalink
fix(mdns): define CONFIG_LWIP_IPV4 in mdns_private.h if they are not …
Browse files Browse the repository at this point in the history
…defined for IDF v5.0
  • Loading branch information
wqx6 committed Nov 17, 2023
1 parent 562987d commit d492d8a
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 123 deletions.
14 changes: 0 additions & 14 deletions components/mdns/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ menu "mDNS"
Configures period of mDNS timer, which periodically transmits packets
and schedules mDNS searches.

config MDNS_IPV4
bool "Enable IPv4 for mDNS"
default y
select LWIP_IPV4
help
Enable IPv4 for mDNS

config MDNS_IPV6
bool "Enable IPv6 for mDNS"
default y
select LWIP_IPV6
help
Enable IPv6 for mDNS

config MDNS_NETWORKING_SOCKET
bool "Use BSD sockets for mDNS networking"
default n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static void query_mdns_hosts_async(const char *host_name)
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}

#if LWIP_IPV4
static void query_mdns_host(const char *host_name)
{
ESP_LOGI(TAG, "Query A: %s.local", host_name);
Expand All @@ -247,6 +247,7 @@ static void query_mdns_host(const char *host_name)

ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
}
#endif // LWIP_IPV4

static void initialise_button(void)
{
Expand All @@ -265,7 +266,9 @@ static void check_button(void)
bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO);
if (!new_level && old_level) {
query_mdns_hosts_async("esp32-mdns");
#if LWIP_IPV4
query_mdns_host("esp32");
#endif
query_mdns_service("_arduino", "_tcp");
query_mdns_service("_http", "_tcp");
query_mdns_service("_printer", "_tcp");
Expand All @@ -286,7 +289,9 @@ static void mdns_example_task(void *pvParameters)
{
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
/* Send initial queries that are started by CI tester */
#if LWIP_IPV4
query_mdns_host("tinytester");
#endif
query_mdns_host_with_gethostbyname("tinytester-lwip.local");
query_mdns_host_with_getaddrinfo("tinytester-lwip.local");
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ CONFIG_IDF_TARGET="esp32"
CONFIG_MDNS_RESOLVE_TEST_SERVICES=y
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y
CONFIG_MDNS_PUBLISH_DELEGATE_HOST=y
CONFIG_MDNS_IPV4=n
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
CONFIG_LWIP_IPV4=n
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ CONFIG_IDF_TARGET="esp32"
CONFIG_MDNS_RESOLVE_TEST_SERVICES=y
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y
CONFIG_MDNS_PUBLISH_DELEGATE_HOST=y
CONFIG_MDNS_IPV6=n
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
CONFIG_LWIP_IPV6=n
CONFIG_EXAMPLE_CONNECT_IPV6=n
Expand Down
98 changes: 51 additions & 47 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ 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 || (s_esp_netifs[i].predefined && esp_netif == esp_netif_from_preset_if(s_esp_netifs[i].predef_if))) {
// The predefined netifs in the static array are NULL when firstly calling this function
// if IPv4 is disabled. Set these netifs here.
if (s_esp_netifs[i].netif == NULL && s_esp_netifs[i].predefined) {
s_esp_netifs[i].netif = esp_netif_from_preset_if(s_esp_netifs[i].predef_if);
}
if (esp_netif == s_esp_netifs[i].netif) {
return i;
}
}
Expand Down Expand Up @@ -1060,7 +1065,7 @@ static uint16_t _mdns_append_srv_record(uint8_t *packet, uint16_t *index, mdns_s
return record_length;
}

#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
/**
* @brief appends A record to a packet, incrementing the index
*
Expand Down Expand Up @@ -1110,9 +1115,9 @@ static uint16_t _mdns_append_a_record(uint8_t *packet, uint16_t *index, const ch
record_length += 4;
return record_length;
}
#endif /* CONFIG_MDNS_IPV4 */
#endif /* CONFIG_LWIP_IPV4 */

#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
/**
* @brief appends AAAA record to a packet, incrementing the index
*
Expand Down Expand Up @@ -1235,7 +1240,7 @@ static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
return false;
}

#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
/**
* @brief Check if IPv6 address is NULL
*/
Expand All @@ -1250,7 +1255,7 @@ static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
}
return true;
}
#endif
#endif /* CONFIG_LWIP_IPV6 */

static uint8_t _mdns_append_host_answer(uint8_t *packet, uint16_t *index, mdns_host_item_t *host,
uint8_t address_type, bool flush, bool bye)
Expand All @@ -1260,19 +1265,19 @@ static uint8_t _mdns_append_host_answer(uint8_t *packet, uint16_t *index, mdns_h

while (addr != NULL) {
if (addr->addr.type == address_type) {
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
if (address_type == ESP_IPADDR_TYPE_V4 &&
_mdns_append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) {
break;
}
#endif /* CONFIG_MDNS_IPV4 */
#ifdef CONFIG_MDNS_IPV6
#endif /* CONFIG_LWIP_IPV4 */
#ifdef CONFIG_LWIP_IPV6
if (address_type == ESP_IPADDR_TYPE_V6 &&
_mdns_append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush,
bye) <= 0) {
break;
}
#endif /* CONFIG_MDNS_IPV6 */
#endif /* CONFIG_LWIP_IPV6 */
num_records++;
}
addr = addr->next;
Expand Down Expand Up @@ -1366,7 +1371,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
} else if (answer->type == MDNS_TYPE_SDPTR) {
return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
}
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
else if (answer->type == MDNS_TYPE_A) {
if (answer->host == &_mdns_self_host) {
esp_netif_ip_info_t if_ip_info;
Expand Down Expand Up @@ -1394,8 +1399,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);
}
}
#endif /* CONFIG_MDNS_IPV4 */
#ifdef CONFIG_MDNS_IPV6
#endif /* CONFIG_LWIP_IPV4 */
#ifdef CONFIG_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 @@ -1433,7 +1438,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
answer->bye);
}
}
#endif /* CONFIG_MDNS_IPV6 */
#endif /* CONFIG_LWIP_IPV6 */
return 0;
}

Expand Down Expand Up @@ -1721,13 +1726,13 @@ static mdns_tx_packet_t *_mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_
packet->tcpip_if = tcpip_if;
packet->ip_protocol = ip_protocol;
packet->port = MDNS_SERVICE_PORT;
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251);
memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
}
#endif
#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
if (ip_protocol == MDNS_IP_PROTOCOL_V6) {
esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000);
memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
Expand Down Expand Up @@ -2887,7 +2892,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if)
}
}

#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
/**
* @brief Detect IPv4 address collision
*/
Expand Down Expand Up @@ -2921,9 +2926,9 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
}
return 0;//same
}
#endif /* CONFIG_MDNS_IPV4 */
#endif /* CONFIG_LWIP_IPV4 */

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

static bool _hostname_is_ours(const char *hostname)
{
Expand Down Expand Up @@ -3520,27 +3525,24 @@ 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
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_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;
}
}
#endif /* CONFIG_MDNS_IPV4 */
#if defined(CONFIG_MDNS_IPV4) && defined(CONFIG_MDNS_IPV6)
else
#endif
#ifdef CONFIG_MDNS_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 /* CONFIG_LWIP_IPV4 */
#ifdef CONFIG_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 /* CONFIG_MDNS_IPV6 */
}
#endif /* CONFIG_LWIP_IPV6 */
#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES

// Check for the minimum size of mdns packet
Expand Down Expand Up @@ -3904,7 +3906,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
else if (type == MDNS_TYPE_AAAA) {//ipv6
esp_ip_addr_t ip6;
ip6.type = ESP_IPADDR_TYPE_V6;
Expand Down Expand Up @@ -3957,8 +3959,8 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#endif /* CONFIG_MDNS_IPV6 */
#ifdef CONFIG_MDNS_IPV4
#endif /* CONFIG_LWIP_IPV6 */
#ifdef CONFIG_LWIP_IPV4
else if (type == MDNS_TYPE_A) {
esp_ip_addr_t ip;
ip.type = ESP_IPADDR_TYPE_V4;
Expand Down Expand Up @@ -4011,7 +4013,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#endif /* CONFIG_MDNS_IPV4 */
#endif /* CONFIG_LWIP_IPV4 */
}
//end while
if (parsed_packet->authoritative) {
Expand Down Expand Up @@ -4113,7 +4115,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}

#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_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 @@ -4127,8 +4129,8 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}
}
}
#endif /* CONFIG_MDNS_IPV4 */
#ifdef CONFIG_MDNS_IPV6
#endif /* CONFIG_LWIP_IPV4 */
#ifdef CONFIG_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)) {
Expand All @@ -4152,7 +4154,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
}
}
}
#endif /* CONFIG_MDNS_IPV6 */
#endif /* CONFIG_LWIP_IPV6 */
#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
}

Expand Down Expand Up @@ -5503,20 +5505,20 @@ esp_err_t mdns_init(void)
#endif

uint8_t i;
#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
esp_ip6_addr_t tmp_addr6;
#endif
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_LWIP_IPV4
esp_netif_ip_info_t if_ip_info;
#endif

for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_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
#ifdef CONFIG_MDNS_IPV4
#ifdef CONFIG_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);
}
Expand Down Expand Up @@ -6490,6 +6492,7 @@ esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *servi
return ESP_OK;
}

#ifdef CONFIG_LWIP_IPV4
esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
{
mdns_result_t *result = NULL;
Expand Down Expand Up @@ -6526,8 +6529,9 @@ esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
mdns_query_results_free(result);
return ESP_ERR_NOT_FOUND;
}
#endif /* CONFIG_LWIP_IPV4 */

#ifdef CONFIG_MDNS_IPV6
#ifdef CONFIG_LWIP_IPV6
esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *addr)
{
mdns_result_t *result = NULL;
Expand Down Expand Up @@ -6564,7 +6568,7 @@ esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *ad
mdns_query_results_free(result);
return ESP_ERR_NOT_FOUND;
}
#endif
#endif /* CONFIG_LWIP_IPV6 */

#ifdef MDNS_ENABLE_DEBUG

Expand Down
Loading

0 comments on commit d492d8a

Please sign in to comment.