From a61b1d3187f14119cf9da5418af8c321a903e34c Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Fri, 3 Nov 2023 17:45:57 +0800 Subject: [PATCH 1/4] fix(mdns): fix compiling issue when disabling IPv4 --- components/mdns/mdns.c | 64 +++++++++++++-------- components/mdns/mdns_networking_lwip.c | 79 ++++++++++++++++---------- 2 files changed, 88 insertions(+), 55 deletions(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 9d3224f0b4..6a6e00fdf4 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -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" @@ -184,7 +185,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; } } @@ -1360,7 +1361,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) { @@ -1387,7 +1390,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]; @@ -1425,7 +1429,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an answer->bye); } } -#endif +#endif /* LWIP_IPV6 */ return 0; } @@ -2877,6 +2881,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if) } } +#if LWIP_IPV4 /** * @brief Detect IPv4 address collision */ @@ -2890,7 +2895,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 @@ -2911,8 +2915,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 */ @@ -2946,7 +2951,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) { @@ -3509,22 +3514,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) { @@ -3887,7 +3898,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; @@ -3940,7 +3951,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; @@ -3993,6 +4005,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } +#endif /* LWIP_IPV4 */ } //end while if (parsed_packet->authoritative) { @@ -4094,6 +4107,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) { @@ -4107,14 +4121,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 @@ -4132,7 +4145,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 */ } @@ -5483,22 +5496,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; diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index 402f120539..46d5d0b326 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -96,6 +96,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco netif = esp_netif_get_netif_impl(tcpip_if); assert(netif); +#if LWIP_IPV4 if (ip_protocol == MDNS_IP_PROTOCOL_V4) { ip4_addr_t multicast_addr; IP4_ADDR(&multicast_addr, 224, 0, 0, 251); @@ -110,21 +111,25 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco } } } -#if CONFIG_LWIP_IPV6 - else { - ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); +#endif // LWIP_IPV4 +#if LWIP_IPV4 && LWIP_IPV6 + else +#endif +#if LWIP_IPV6 + if (ip_protocol == MDNS_IP_PROTOCOL_V6) { + ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); - if (join) { - if (mld6_joingroup_netif(netif, &(multicast_addr.u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (mld6_leavegroup_netif(netif, &(multicast_addr.u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; + if (join) { + if (mld6_joingroup_netif(netif, ip_2_ip6(&multicast_addr))) { + return ESP_ERR_INVALID_STATE; + } + } else { + if (mld6_leavegroup_netif(netif, ip_2_ip6(&multicast_addr))) { + return ESP_ERR_INVALID_STATE; + } } } - } -#endif +#endif // LWIP_IPV6 return ESP_OK; } @@ -152,29 +157,37 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip packet->tcpip_if = MDNS_MAX_INTERFACES; packet->pb = this_pb; packet->src_port = rport; -#if CONFIG_LWIP_IPV6 +#if LWIP_IPV4 && LWIP_IPV6 packet->src.type = raddr->type; memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr)); -#else +#elif LWIP_IPV4 packet->src.type = IPADDR_TYPE_V4; - memcpy(&packet->src.u_addr.ip4, &raddr->addr, sizeof(ip_addr_t)); + packet->src.u_addr.ip4.addr = raddr.addr; +#elif LWIP_IPV6 + packet->src.type = IPADDR_TYPE_V6; + memcpy(&packet->src.u_addr.ip6, raddr, sizeof(ip_addr_t)); #endif packet->dest.type = packet->src.type; +#if LWIP_IPV4 if (packet->src.type == IPADDR_TYPE_V4) { packet->ip_protocol = MDNS_IP_PROTOCOL_V4; 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->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4)); } -#if CONFIG_LWIP_IPV6 - else { - packet->ip_protocol = MDNS_IP_PROTOCOL_V6; - 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); - packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6)); - } +#endif // LWIP_IPV4 +#if LWIP_IPV4 && LWIP_IPV6 + else #endif +#if LWIP_IPV6 + if (packet->src.type == IPADDR_TYPE_V6) { + packet->ip_protocol = MDNS_IP_PROTOCOL_V6; + 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); + packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6)); + } +#endif // LWIP_IPV6 //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; @@ -182,15 +195,14 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip for (i = 0; i < MDNS_MAX_INTERFACES; i++) { netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i)); if (s_interfaces[i].proto && netif && netif == ip_current_input_netif ()) { +#if LWIP_IPV4 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)) { -#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 + if ((packet->src.u_addr.ip4.addr & ip_2_ip4(&netif->netmask)->addr) != (ip_2_ip4(&netif->ip_addr)->addr & ip_2_ip4(&netif->netmask)->addr)) { + //packet source is not in the same subnet break; } } +#endif // LWIP_IPV4 packet->tcpip_if = i; found = true; break; @@ -347,12 +359,17 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c memcpy((uint8_t *)pbt->payload, data, len); ip_addr_t ip_add_copy; -#if CONFIG_LWIP_IPV6 +#if LWIP_IPV6 && LWIP_IPV4 ip_add_copy.type = ip->type; memcpy(&(ip_add_copy.u_addr), &(ip->u_addr), sizeof(ip_add_copy.u_addr)); -#else - memcpy(&(ip_add_copy.addr), &(ip->u_addr), sizeof(ip_add_copy.addr)); -#endif // CONFIG_LWIP_IPV6 +#elif LWIP_IPV4 + ip_add_copy.addr = ip->u_addr.ip4.addr; +#elif LWIP_IPV6 +#if LWIP_IPV6_SCOPES + ip_add_copy.zone = ip->u_addr.ip6.zone; +#endif // LWIP_IPV6_SCOPES + memcpy(ip_add_copy.addr, ip->u_addr.ip6.addr, sizeof(ip_add_copy.addr)); +#endif mdns_api_call_t msg = { .tcpip_if = tcpip_if, From 40ea75f01b4e51b772ab5010d31f3db1b14deaf8 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Thu, 16 Nov 2023 14:57:23 +0800 Subject: [PATCH 2/4] fix(mdns): Remove lwip include in mdns.c file --- components/mdns/Kconfig | 14 ++++ .../query_advertise/main/mdns_example_main.c | 17 ++++- .../query_advertise/sdkconfig.ci.eth_no_ipv4 | 16 +++++ .../query_advertise/sdkconfig.ci.eth_no_ipv6 | 1 + components/mdns/mdns.c | 72 ++++++++++--------- components/mdns/mdns_console.c | 13 ++-- components/mdns/mdns_networking_lwip.c | 37 ++++++---- components/mdns/mdns_networking_socket.c | 53 +++++++++----- 8 files changed, 150 insertions(+), 73 deletions(-) create mode 100644 components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index 7252e86196..ebe7ebc9fa 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -77,6 +77,20 @@ 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 diff --git a/components/mdns/examples/query_advertise/main/mdns_example_main.c b/components/mdns/examples/query_advertise/main/mdns_example_main.c index dec439bb4c..44ce02a175 100644 --- a/components/mdns/examples/query_advertise/main/mdns_example_main.c +++ b/components/mdns/examples/query_advertise/main/mdns_example_main.c @@ -362,7 +362,16 @@ static void query_mdns_host_with_gethostbyname(char *host) if (res) { unsigned int i = 0; while (res->h_addr_list[i] != NULL) { - ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, inet_ntoa(*(struct in_addr *) (res->h_addr_list[i]))); + ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, +#if LWIP_IPV6 && LWIP_IPV4 + res->h_addrtype == AF_INET ? inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) : + inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) +#elif LWIP_IPV6 + inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) +#else + inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) +#endif + ); i++; } } @@ -384,10 +393,12 @@ static void query_mdns_host_with_getaddrinfo(char *host) if (!getaddrinfo(host, NULL, &hints, &res)) { while (res) { char *resolved_addr; -#if CONFIG_LWIP_IPV6 +#if LWIP_IPV6 && LWIP_IPV4 resolved_addr = res->ai_family == AF_INET ? inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) : - inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); + inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); +#elif LWIP_IPV6 + resolved_addr = inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); #else resolved_addr = inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr); #endif // CONFIG_LWIP_IPV6 diff --git a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 new file mode 100644 index 0000000000..335a2b7d35 --- /dev/null +++ b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 @@ -0,0 +1,16 @@ +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 +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_MDNS_BUTTON_GPIO=32 diff --git a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 index d669ec0751..7495b7c152 100644 --- a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 +++ b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 @@ -2,6 +2,7 @@ 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 diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 6a6e00fdf4..2687345dd4 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -17,7 +17,6 @@ #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" @@ -1060,6 +1059,7 @@ static uint16_t _mdns_append_srv_record(uint8_t *packet, uint16_t *index, mdns_s return record_length; } +#ifdef CONFIG_MDNS_IPV4 /** * @brief appends A record to a packet, incrementing the index * @@ -1109,8 +1109,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 */ -#if CONFIG_LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 /** * @brief appends AAAA record to a packet, incrementing the index * @@ -1233,7 +1234,7 @@ static bool _mdns_if_is_dup(mdns_if_t tcpip_if) return false; } -#if CONFIG_LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 /** * @brief Check if IPv6 address is NULL */ @@ -1258,17 +1259,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 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; } -#if CONFIG_LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV4 */ +#ifdef CONFIG_MDNS_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_LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV6 */ num_records++; } addr = addr->next; @@ -1362,7 +1365,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; } -#if LWIP_IPV4 +#ifdef CONFIG_MDNS_IPV4 else if (answer->type == MDNS_TYPE_A) { if (answer->host == &_mdns_self_host) { esp_netif_ip_info_t if_ip_info; @@ -1390,8 +1393,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 /* LWIP_IPV4 */ -#if LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV4 */ +#ifdef CONFIG_MDNS_IPV6 else if (answer->type == MDNS_TYPE_AAAA) { if (answer->host == &_mdns_self_host) { struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS]; @@ -1429,7 +1432,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an answer->bye); } } -#endif /* LWIP_IPV6 */ +#endif /* CONFIG_MDNS_IPV6 */ return 0; } @@ -1717,12 +1720,14 @@ 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 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)); } -#if CONFIG_LWIP_IPV6 - else { +#endif +#ifdef CONFIG_MDNS_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)); } @@ -2881,7 +2886,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if) } } -#if LWIP_IPV4 +#ifdef CONFIG_MDNS_IPV4 /** * @brief Detect IPv4 address collision */ @@ -2915,9 +2920,9 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if) } return 0;//same } -#endif // LWIP_IPV4 +#endif /* CONFIG_MDNS_IPV4 */ -#if LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 /** * @brief Detect IPv6 address collision */ @@ -2951,7 +2956,7 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if) } return 0;//same } -#endif /* LWIP_IPV6 */ +#endif /* CONFIG_MDNS_IPV6 */ static bool _hostname_is_ours(const char *hostname) { @@ -3514,7 +3519,7 @@ 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 +#ifdef CONFIG_MDNS_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 && @@ -3522,11 +3527,11 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) return; } } -#endif // LWIP_IPV4 -#if LWIP_IPV4 && LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV4 */ +#if defined(CONFIG_MDNS_IPV4) && defined(CONFIG_MDNS_IPV6) else #endif -#if LWIP_IPV6 +#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 && @@ -3534,7 +3539,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) return; } } -#endif // LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV6 */ #endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES // Check for the minimum size of mdns packet @@ -3898,7 +3903,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } -#if LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 else if (type == MDNS_TYPE_AAAA) {//ipv6 esp_ip_addr_t ip6; ip6.type = ESP_IPADDR_TYPE_V6; @@ -3951,8 +3956,8 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } -#endif /* LWIP_IPV6 */ -#if LWIP_IPV4 +#endif /* CONFIG_MDNS_IPV6 */ +#ifdef CONFIG_MDNS_IPV4 else if (type == MDNS_TYPE_A) { esp_ip_addr_t ip; ip.type = ESP_IPADDR_TYPE_V4; @@ -4005,7 +4010,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } -#endif /* LWIP_IPV4 */ +#endif /* CONFIG_MDNS_IPV4 */ } //end while if (parsed_packet->authoritative) { @@ -4107,7 +4112,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 +#ifdef CONFIG_MDNS_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) { @@ -4121,13 +4126,14 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) } } } -#endif /* LWIP_IPV4 */ -#if LWIP_IPV6 +#endif /* CONFIG_MDNS_IPV4 */ +#ifdef CONFIG_MDNS_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 @@ -4145,7 +4151,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) } } } -#endif /* LWIP_IPV6 */ +#endif /* CONFIG_MDNS_IPV6 */ #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ } @@ -5496,20 +5502,20 @@ esp_err_t mdns_init(void) #endif uint8_t i; -#if LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 esp_ip6_addr_t tmp_addr6; #endif -#if LWIP_IPV4 +#ifdef CONFIG_MDNS_IPV4 esp_netif_ip_info_t if_ip_info; #endif for (i = 0; i < MDNS_MAX_INTERFACES; i++) { -#if LWIP_IPV6 +#ifdef CONFIG_MDNS_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 +#ifdef CONFIG_MDNS_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); } @@ -6520,7 +6526,7 @@ esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr) return ESP_ERR_NOT_FOUND; } -#if CONFIG_LWIP_IPV6 +#ifdef CONFIG_MDNS_IPV6 esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *addr) { mdns_result_t *result = NULL; diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 992ce9a8ff..7029376225 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -43,13 +43,13 @@ static void mdns_print_results(mdns_result_t *results) r = r->next; } } - static struct { struct arg_str *hostname; struct arg_int *timeout; struct arg_end *end; } mdns_query_a_args; +#ifdef CONFIG_MDNS_IPV4 static int cmd_mdns_query_a(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args); @@ -106,8 +106,9 @@ static void register_mdns_query_a(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); } +#endif /* CONFIG_MDNS_IPV4 */ -#if CONFIG_LWIP_IPV6 +#if CONFIG_MDNS_IPV6 static int cmd_mdns_query_aaaa(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args); @@ -164,7 +165,7 @@ static void register_mdns_query_aaaa(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); } -#endif +#endif /* CONFIG_MDNS_IPV6 */ static struct { struct arg_str *instance; @@ -1042,8 +1043,10 @@ void mdns_console_register(void) register_mdns_service_txt_remove(); register_mdns_service_remove_all(); +#if CONFIG_MDNS_IPV4 register_mdns_query_a(); -#if CONFIG_LWIP_IPV6 +#endif +#if CONFIG_MDNS_IPV6 register_mdns_query_aaaa(); #endif register_mdns_query_txt(); diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index 46d5d0b326..7b24d60054 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -21,6 +21,18 @@ #include "mdns_networking.h" #include "esp_netif_net_stack.h" +#if defined(CONFIG_MDNS_IPV4) && !LWIP_IPV4 +#error "Please enable IPv4 in LwIP to use IPv4 in mDNS component" +#endif + +#if defined(CONFIG_MDNS_IPV6) && !LWIP_IPV6 +#error "Please enable IPv6 in LwIP to use IPv6 in mDNS component" +#endif + +#if !defined(CONFIG_MDNS_IPV4) && !defined(CONFIG_MDNS_IPV6) +#error "Neither IPv4 nor IPv6 is used in mDNS component" +#endif + /* * MDNS Server Networking * @@ -112,23 +124,20 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco } } #endif // LWIP_IPV4 -#if LWIP_IPV4 && LWIP_IPV6 - else -#endif #if LWIP_IPV6 - if (ip_protocol == MDNS_IP_PROTOCOL_V6) { - ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); + if (ip_protocol == MDNS_IP_PROTOCOL_V6) { + ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); - if (join) { - if (mld6_joingroup_netif(netif, ip_2_ip6(&multicast_addr))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (mld6_leavegroup_netif(netif, ip_2_ip6(&multicast_addr))) { - return ESP_ERR_INVALID_STATE; - } + if (join) { + if (mld6_joingroup_netif(netif, ip_2_ip6(&multicast_addr))) { + return ESP_ERR_INVALID_STATE; + } + } else { + if (mld6_leavegroup_netif(netif, ip_2_ip6(&multicast_addr))) { + return ESP_ERR_INVALID_STATE; } } + } #endif // LWIP_IPV6 return ESP_OK; } @@ -162,7 +171,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr)); #elif LWIP_IPV4 packet->src.type = IPADDR_TYPE_V4; - packet->src.u_addr.ip4.addr = raddr.addr; + packet->src.u_addr.ip4.addr = raddr->addr; #elif LWIP_IPV6 packet->src.type = IPADDR_TYPE_V6; memcpy(&packet->src.u_addr.ip6, raddr, sizeof(ip_addr_t)); diff --git a/components/mdns/mdns_networking_socket.c b/components/mdns/mdns_networking_socket.c index 0440299f1c..e42dd1bcab 100644 --- a/components/mdns/mdns_networking_socket.c +++ b/components/mdns/mdns_networking_socket.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -27,6 +27,13 @@ #include #endif +#if defined(CONFIG_IDF_TARGET_LINUX) +#define SOCKET_IPV4 1 +#define SOCKET_IPV6 CONFIG_LWIP_IPV6 +#else +#define SOCKET_IPV4 LWIP_IPV4 +#define SOCKET_IPV6 LWIP_IPV6 +#endif enum interface_protocol { PROTO_IPV4 = 1 << MDNS_IP_PROTOCOL_V4, PROTO_IPV6 = 1 << MDNS_IP_PROTOCOL_V6 @@ -116,13 +123,13 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) } #if defined(CONFIG_IDF_TARGET_LINUX) -#ifdef CONFIG_LWIP_IPV6 +#if SOCKET_IPV6 static char *inet6_ntoa_r(struct in6_addr addr, char *ptr, size_t size) { inet_ntop(AF_INET6, &(addr.s6_addr32[0]), ptr, size); return ptr; } -#endif // CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV6 static char *inet_ntoa_r(struct in_addr addr, char *ptr, size_t size) { char *res = inet_ntoa(addr); @@ -138,11 +145,13 @@ static inline char *get_string_address(struct sockaddr_storage *source_addr) static char address_str[40]; // 40=(8*4+7+term) is the max size of ascii IPv6 addr "XXXX:XX...XX:XXXX" char *res = NULL; // Convert ip address to string +#if SOCKET_IPV4 if (source_addr->ss_family == PF_INET) { res = inet_ntoa_r(((struct sockaddr_in *)source_addr)->sin_addr, address_str, sizeof(address_str)); } -#ifdef CONFIG_LWIP_IPV6 - else if (source_addr->ss_family == PF_INET6) { +#endif +#if SOCKET_IPV6 + if (source_addr->ss_family == PF_INET6) { res = inet6_ntoa_r(((struct sockaddr_in6 *)source_addr)->sin6_addr, address_str, sizeof(address_str)); } #endif @@ -157,6 +166,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p { size_t ss_addr_len = 0; memset(in_addr, 0, sizeof(struct sockaddr_storage)); +#if SOCKET_IPV4 if (ip_protocol == MDNS_IP_PROTOCOL_V4 && addr->type == ESP_IPADDR_TYPE_V4) { in_addr->ss_family = PF_INET; #if !defined(CONFIG_IDF_TARGET_LINUX) @@ -167,8 +177,9 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p in_addr_ip4->sin_port = port; in_addr_ip4->sin_addr.s_addr = addr->u_addr.ip4.addr; } -#if CONFIG_LWIP_IPV6 - else if (ip_protocol == MDNS_IP_PROTOCOL_V6 && addr->type == ESP_IPADDR_TYPE_V6) { +#endif +#if SOCKET_IPV6 + if (ip_protocol == MDNS_IP_PROTOCOL_V6 && addr->type == ESP_IPADDR_TYPE_V6) { memset(in_addr, 0, sizeof(struct sockaddr_storage)); in_addr->ss_family = PF_INET6; #if !defined(CONFIG_IDF_TARGET_LINUX) @@ -183,7 +194,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p u32_addr[2] = addr->u_addr.ip6.addr[2]; u32_addr[3] = addr->u_addr.ip6.addr[3]; } -#endif // CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV6 return ss_addr_len; } @@ -212,6 +223,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_ip_addr_t *addr, uint16_t *port) { +#if SOCKET_IPV4 if (in_addr->ss_family == PF_INET) { struct sockaddr_in *in_addr_ip4 = (struct sockaddr_in *)in_addr; memset(addr, 0, sizeof(esp_ip_addr_t)); @@ -219,8 +231,9 @@ static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_i addr->u_addr.ip4.addr = in_addr_ip4->sin_addr.s_addr; addr->type = ESP_IPADDR_TYPE_V4; } -#if CONFIG_LWIP_IPV6 - else if (in_addr->ss_family == PF_INET6) { +#endif /* SOCKET_IPV4 */ +#if SOCKET_IPV6 + if (in_addr->ss_family == PF_INET6) { struct sockaddr_in6 *in_addr_ip6 = (struct sockaddr_in6 *)in_addr; memset(addr, 0, sizeof(esp_ip_addr_t)); *port = in_addr_ip6->sin6_port; @@ -383,7 +396,7 @@ esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) static int create_socket(esp_netif_t *netif) { -#if CONFIG_LWIP_IPV6 +#if SOCKET_IPV6 int sock = socket(PF_INET6, SOCK_DGRAM, 0); #else int sock = socket(PF_INET, SOCK_DGRAM, 0); @@ -398,8 +411,8 @@ static int create_socket(esp_netif_t *netif) ESP_LOGE(TAG, "Failed setsockopt() to set SO_REUSEADDR. errno=%d: %s\n", errno, strerror(errno)); } // Bind the socket to any address -#if CONFIG_LWIP_IPV6 - struct sockaddr_in6 saddr = { INADDR_ANY }; +#if SOCKET_IPV6 + struct sockaddr_in6 saddr = { 0 }; saddr.sin6_family = AF_INET6; saddr.sin6_port = htons(5353); bzero(&saddr.sin6_addr.s6_addr, sizeof(saddr.sin6_addr.s6_addr)); @@ -418,7 +431,7 @@ static int create_socket(esp_netif_t *netif) ESP_LOGE(TAG, "Failed to bind socket. errno=%d: %s", errno, strerror(errno)); goto err; } -#endif // CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV6 struct ifreq ifr; esp_netif_get_netif_impl_name(netif, ifr.ifr_name); int ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(struct ifreq)); @@ -434,7 +447,7 @@ static int create_socket(esp_netif_t *netif) return -1; } -#if CONFIG_LWIP_IPV6 +#if SOCKET_IPV6 static int socket_add_ipv6_multicast_group(int sock, esp_netif_t *netif) { int ifindex = esp_netif_get_netif_impl_index(netif); @@ -455,8 +468,9 @@ static int socket_add_ipv6_multicast_group(int sock, esp_netif_t *netif) } return err; } -#endif // CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV6 +#if SOCKET_IPV4 static int socket_add_ipv4_multicast_group(int sock, esp_netif_t *netif) { struct ip_mreq imreq = { 0 }; @@ -481,16 +495,19 @@ static int socket_add_ipv4_multicast_group(int sock, esp_netif_t *netif) err: return err; } +#endif // SOCKET_IPV4 static int join_mdns_multicast_group(int sock, esp_netif_t *netif, mdns_ip_protocol_t ip_protocol) { +#if SOCKET_IPV4 if (ip_protocol == MDNS_IP_PROTOCOL_V4) { return socket_add_ipv4_multicast_group(sock, netif); } -#if CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV4 +#if SOCKET_IPV6 if (ip_protocol == MDNS_IP_PROTOCOL_V6) { return socket_add_ipv6_multicast_group(sock, netif); } -#endif // CONFIG_LWIP_IPV6 +#endif // SOCKET_IPV6 return -1; } From 0641701a6e5746ff4689cbf3a805a3e98dacb14f Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Fri, 17 Nov 2023 11:33:21 +0800 Subject: [PATCH 3/4] fix(mdns): define CONFIG_LWIP_IPV4 in mdns_private.h if it is not defined for IDF v5.0 --- .github/workflows/mdns__build-target-test.yml | 2 + components/mdns/Kconfig | 14 -- .../query_advertise/main/mdns_example_main.c | 21 ++- .../examples/query_advertise/pytest_mdns.py | 144 +++++++++--------- .../query_advertise/sdkconfig.ci.eth_no_ipv4 | 1 - .../query_advertise/sdkconfig.ci.eth_no_ipv6 | 1 - components/mdns/mdns.c | 98 ++++++------ components/mdns/mdns_console.c | 14 +- components/mdns/mdns_networking_lwip.c | 27 +--- components/mdns/mdns_networking_socket.c | 53 +++---- .../mdns/private_include/mdns_private.h | 10 +- .../components/esp_netif_linux/Kconfig | 6 + 12 files changed, 197 insertions(+), 194 deletions(-) diff --git a/.github/workflows/mdns__build-target-test.yml b/.github/workflows/mdns__build-target-test.yml index 868256a764..c0a690255b 100644 --- a/.github/workflows/mdns__build-target-test.yml +++ b/.github/workflows/mdns__build-target-test.yml @@ -71,10 +71,12 @@ jobs: - name: Run ${{ matrix.test.app }} application on ${{ matrix.idf_target }} working-directory: components/mdns/${{ matrix.test.path }} run: | + pip install dpkt unzip ci/artifacts.zip -d ci for dir in `ls -d ci/build_*`; do rm -rf build sdkconfig.defaults mv $dir build + cat build/config/sdkconfig.h python -m pytest --log-cli-level DEBUG --junit-xml=./results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${dir#"ci/build_"}.xml --target=${{ matrix.idf_target }} done - uses: actions/upload-artifact@v3 diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index ebe7ebc9fa..7252e86196 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -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 diff --git a/components/mdns/examples/query_advertise/main/mdns_example_main.c b/components/mdns/examples/query_advertise/main/mdns_example_main.c index 44ce02a175..a6aaea2d4c 100644 --- a/components/mdns/examples/query_advertise/main/mdns_example_main.c +++ b/components/mdns/examples/query_advertise/main/mdns_example_main.c @@ -21,6 +21,12 @@ #include "driver/gpio.h" #include "netdb.h" +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) +/* CONFIG_LWIP_IPV4 was introduced in IDF v5.1, set CONFIG_LWIP_IPV4 to 1 by default for IDF v5.0 */ +#ifndef CONFIG_LWIP_IPV4 +#define CONFIG_LWIP_IPV4 1 +#endif // CONFIG_LWIP_IPV4 +#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE #define EXAMPLE_BUTTON_GPIO CONFIG_MDNS_BUTTON_GPIO @@ -227,7 +233,7 @@ static void query_mdns_hosts_async(const char *host_name) vTaskDelay(50 / portTICK_PERIOD_MS); } } - +#ifdef CONFIG_LWIP_IPV4 static void query_mdns_host(const char *host_name) { ESP_LOGI(TAG, "Query A: %s.local", host_name); @@ -247,6 +253,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 // CONFIG_LWIP_IPV4 static void initialise_button(void) { @@ -265,7 +272,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"); +#ifdef CONFIG_LWIP_IPV4 query_mdns_host("esp32"); +#endif query_mdns_service("_arduino", "_tcp"); query_mdns_service("_http", "_tcp"); query_mdns_service("_printer", "_tcp"); @@ -286,7 +295,9 @@ static void mdns_example_task(void *pvParameters) { #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1 /* Send initial queries that are started by CI tester */ +#ifdef CONFIG_LWIP_IPV4 query_mdns_host("tinytester"); +#endif query_mdns_host_with_gethostbyname("tinytester-lwip.local"); query_mdns_host_with_getaddrinfo("tinytester-lwip.local"); #endif @@ -363,10 +374,10 @@ static void query_mdns_host_with_gethostbyname(char *host) unsigned int i = 0; while (res->h_addr_list[i] != NULL) { ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, -#if LWIP_IPV6 && LWIP_IPV4 +#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4) res->h_addrtype == AF_INET ? inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) : inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) -#elif LWIP_IPV6 +#elif defined(CONFIG_LWIP_IPV6) inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) #else inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) @@ -393,11 +404,11 @@ static void query_mdns_host_with_getaddrinfo(char *host) if (!getaddrinfo(host, NULL, &hints, &res)) { while (res) { char *resolved_addr; -#if LWIP_IPV6 && LWIP_IPV4 +#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4) resolved_addr = res->ai_family == AF_INET ? inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) : inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); -#elif LWIP_IPV6 +#elif defined(CONFIG_LWIP_IPV6) resolved_addr = inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); #else resolved_addr = inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr); diff --git a/components/mdns/examples/query_advertise/pytest_mdns.py b/components/mdns/examples/query_advertise/pytest_mdns.py index 42364dfe31..e78e094dd7 100644 --- a/components/mdns/examples/query_advertise/pytest_mdns.py +++ b/components/mdns/examples/query_advertise/pytest_mdns.py @@ -87,26 +87,30 @@ def mdns_server(esp_host, events): continue data, addr = sock.recvfrom(1024) dns = dpkt.dns.DNS(data) - if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A: - if dns.qd[0].name == TESTER_NAME: - print('Received query: {} '.format(dns.__repr__())) - sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), - (MCAST_GRP, UDP_PORT)) - elif dns.qd[0].name == TESTER_NAME_LWIP: - print('Received query: {} '.format(dns.__repr__())) - sock.sendto( - get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), - addr) - if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A: - print('Received answer from {}'.format(dns.an[0].name)) - if dns.an[0].name == esp_host + u'.local': - print('Received answer to esp32-mdns query: {}'.format( - dns.__repr__())) - events['esp_answered'].set() - if dns.an[0].name == esp_host + u'-delegated.local': - print('Received answer to esp32-mdns-delegate query: {}'. - format(dns.__repr__())) - events['esp_delegated_answered'].set() + if len(dns.qd) > 0: + for dns_query in dns.qd: + if dns_query.type == dpkt.dns.DNS_A: + if dns_query.name == TESTER_NAME: + print('Received query: {} '.format(dns.__repr__())) + sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), + (MCAST_GRP, UDP_PORT)) + elif dns_query.name == TESTER_NAME_LWIP: + print('Received query: {} '.format(dns.__repr__())) + sock.sendto( + get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), + addr) + if len(dns.an) > 0: + for dns_answer in dns.an: + if dns_answer.type == dpkt.dns.DNS_A: + print('Received answer from {}'.format(dns_answer.name)) + if dns_answer.name == esp_host + u'.local': + print('Received answer to esp32-mdns query: {}'.format( + dns.__repr__())) + events['esp_answered'].set() + if dns_answer.name == esp_host + u'-delegated.local': + print('Received answer to esp32-mdns-delegate query: {}'.format( + dns.__repr__())) + events['esp_delegated_answered'].set() except socket.timeout: break except dpkt.UnpackError: @@ -133,62 +137,66 @@ def test_examples_protocol_mdns(dut): } mdns_responder = Thread(target=mdns_server, args=(str(specific_host), mdns_server_events)) - ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', - timeout=30)[1].decode() - ip_addresses = [ipv4] + ip_addresses = [] + if dut.app.sdkconfig.get('LWIP_IPV4') is True: + ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', + timeout=30)[1].decode() + ip_addresses.append(ipv4) if dut.app.sdkconfig.get('LWIP_IPV6') is True: ipv6_r = r':'.join((r'[0-9a-fA-F]{4}', ) * 8) ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode() ip_addresses.append(ipv6) print('Connected with IP addresses: {}'.format(','.join(ip_addresses))) try: - # 3. check the mdns name is accessible. + # TODO: Add test for example disabling IPV4 mdns_responder.start() - if not mdns_server_events['esp_answered'].wait(timeout=30): - raise ValueError( - 'Test has failed: did not receive mdns answer within timeout') - if not mdns_server_events['esp_delegated_answered'].wait(timeout=30): - raise ValueError( - 'Test has failed: did not receive mdns answer for delegated host within timeout' + if dut.app.sdkconfig.get('LWIP_IPV4') is True: + # 3. check the mdns name is accessible. + if not mdns_server_events['esp_answered'].wait(timeout=30): + raise ValueError( + 'Test has failed: did not receive mdns answer within timeout') + if not mdns_server_events['esp_delegated_answered'].wait(timeout=30): + raise ValueError( + 'Test has failed: did not receive mdns answer for delegated host within timeout' + ) + # 4. check DUT output if mdns advertized host is resolved + dut.expect( + re.compile( + b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1') ) - # 4. check DUT output if mdns advertized host is resolved - dut.expect( - re.compile( - b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1') - ) - dut.expect( - re.compile( - b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1' - )) - dut.expect( - re.compile( - b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1' - )) - # 5. check the DUT answers to `dig` command - dig_output = subprocess.check_output([ - 'dig', '+short', '-p', '5353', '@224.0.0.251', - '{}.local'.format(specific_host) - ]) - print('Resolving {} using "dig" succeeded with:\n{}'.format( - specific_host, dig_output)) - if not ipv4.encode('utf-8') in dig_output: - raise ValueError( - 'Test has failed: Incorrectly resolved DUT hostname using dig' - "Output should've contained DUT's IP address:{}".format(ipv4)) - # 6. check the DUT reverse lookup - if dut.app.sdkconfig.get('MDNS_RESPOND_REVERSE_QUERIES') is True: - for ip_address in ip_addresses: - dig_output = subprocess.check_output([ - 'dig', '+short', '-p', '5353', '@224.0.0.251', '-x', - '{}'.format(ip_address) - ]) - print('Reverse lookup for {} using "dig" succeeded with:\n{}'. - format(ip_address, dig_output)) - if specific_host not in dig_output.decode(): - raise ValueError( - 'Test has failed: Incorrectly resolved DUT IP address using dig' - "Output should've contained DUT's name:{}".format( - specific_host)) + dut.expect( + re.compile( + b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1' + )) + dut.expect( + re.compile( + b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1' + )) + # 5. check the DUT answers to `dig` command + dig_output = subprocess.check_output([ + 'dig', '+short', '-p', '5353', '@224.0.0.251', + '{}.local'.format(specific_host) + ]) + print('Resolving {} using "dig" succeeded with:\n{}'.format( + specific_host, dig_output)) + if not ipv4.encode('utf-8') in dig_output: + raise ValueError( + 'Test has failed: Incorrectly resolved DUT hostname using dig' + "Output should've contained DUT's IP address:{}".format(ipv4)) + # 6. check the DUT reverse lookup + if dut.app.sdkconfig.get('MDNS_RESPOND_REVERSE_QUERIES') is True: + for ip_address in ip_addresses: + dig_output = subprocess.check_output([ + 'dig', '+short', '-p', '5353', '@224.0.0.251', '-x', + '{}'.format(ip_address) + ]) + print('Reverse lookup for {} using "dig" succeeded with:\n{}'. + format(ip_address, dig_output)) + if specific_host not in dig_output.decode(): + raise ValueError( + 'Test has failed: Incorrectly resolved DUT IP address using dig' + "Output should've contained DUT's name:{}".format( + specific_host)) finally: mdns_server_events['stop'].set() diff --git a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 index 335a2b7d35..b77a752705 100644 --- a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 +++ b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4 @@ -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 diff --git a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 index 7495b7c152..d669ec0751 100644 --- a/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 +++ b/components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv6 @@ -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 diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 2687345dd4..0b2691ebb6 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -184,7 +184,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; } } @@ -1059,7 +1064,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 * @@ -1109,9 +1114,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 * @@ -1234,7 +1239,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 */ @@ -1249,7 +1254,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) @@ -1259,19 +1264,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; @@ -1365,7 +1370,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; @@ -1393,8 +1398,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]; @@ -1432,7 +1437,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; } @@ -1720,13 +1725,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)); @@ -2886,7 +2891,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if) } } -#ifdef CONFIG_MDNS_IPV4 +#ifdef CONFIG_LWIP_IPV4 /** * @brief Detect IPv4 address collision */ @@ -2920,9 +2925,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 */ @@ -2956,7 +2961,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) { @@ -3519,7 +3524,7 @@ 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 && @@ -3527,19 +3532,16 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) 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 @@ -3903,7 +3905,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; @@ -3956,8 +3958,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; @@ -4010,7 +4012,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } -#endif /* CONFIG_MDNS_IPV4 */ +#endif /* CONFIG_LWIP_IPV4 */ } //end while if (parsed_packet->authoritative) { @@ -4112,7 +4114,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) { @@ -4126,8 +4128,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)) { @@ -4151,7 +4153,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 */ } @@ -5502,20 +5504,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); } @@ -6489,6 +6491,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; @@ -6525,8 +6528,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; @@ -6563,7 +6567,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 diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 7029376225..075be0682d 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -8,6 +8,7 @@ #include "esp_console.h" #include "argtable3/argtable3.h" #include "mdns.h" +#include "mdns_private.h" static const char *ip_protocol_str[] = {"V4", "V6", "MAX"}; @@ -43,13 +44,14 @@ static void mdns_print_results(mdns_result_t *results) r = r->next; } } + static struct { struct arg_str *hostname; struct arg_int *timeout; struct arg_end *end; } mdns_query_a_args; -#ifdef CONFIG_MDNS_IPV4 +#ifdef CONFIG_LWIP_IPV4 static int cmd_mdns_query_a(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args); @@ -106,9 +108,9 @@ static void register_mdns_query_a(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); } -#endif /* CONFIG_MDNS_IPV4 */ +#endif /* CONFIG_LWIP_IPV4 */ -#if CONFIG_MDNS_IPV6 +#ifdef CONFIG_LWIP_IPV6 static int cmd_mdns_query_aaaa(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args); @@ -165,7 +167,7 @@ static void register_mdns_query_aaaa(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) ); } -#endif /* CONFIG_MDNS_IPV6 */ +#endif /* CONFIG_LWIP_IPV6 */ static struct { struct arg_str *instance; @@ -1043,10 +1045,10 @@ void mdns_console_register(void) register_mdns_service_txt_remove(); register_mdns_service_remove_all(); -#if CONFIG_MDNS_IPV4 +#ifdef CONFIG_LWIP_IPV4 register_mdns_query_a(); #endif -#if CONFIG_MDNS_IPV6 +#ifdef CONFIG_LWIP_IPV6 register_mdns_query_aaaa(); #endif register_mdns_query_txt(); diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index 7b24d60054..635f7e352e 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -21,18 +21,6 @@ #include "mdns_networking.h" #include "esp_netif_net_stack.h" -#if defined(CONFIG_MDNS_IPV4) && !LWIP_IPV4 -#error "Please enable IPv4 in LwIP to use IPv4 in mDNS component" -#endif - -#if defined(CONFIG_MDNS_IPV6) && !LWIP_IPV6 -#error "Please enable IPv6 in LwIP to use IPv6 in mDNS component" -#endif - -#if !defined(CONFIG_MDNS_IPV4) && !defined(CONFIG_MDNS_IPV6) -#error "Neither IPv4 nor IPv6 is used in mDNS component" -#endif - /* * MDNS Server Networking * @@ -186,16 +174,13 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4)); } #endif // LWIP_IPV4 -#if LWIP_IPV4 && LWIP_IPV6 - else -#endif #if LWIP_IPV6 - if (packet->src.type == IPADDR_TYPE_V6) { - packet->ip_protocol = MDNS_IP_PROTOCOL_V6; - 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); - packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6)); - } + if (packet->src.type == IPADDR_TYPE_V6) { + packet->ip_protocol = MDNS_IP_PROTOCOL_V6; + 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); + packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6)); + } #endif // LWIP_IPV6 //lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces) diff --git a/components/mdns/mdns_networking_socket.c b/components/mdns/mdns_networking_socket.c index e42dd1bcab..8411e26e6d 100644 --- a/components/mdns/mdns_networking_socket.c +++ b/components/mdns/mdns_networking_socket.c @@ -27,13 +27,6 @@ #include #endif -#if defined(CONFIG_IDF_TARGET_LINUX) -#define SOCKET_IPV4 1 -#define SOCKET_IPV6 CONFIG_LWIP_IPV6 -#else -#define SOCKET_IPV4 LWIP_IPV4 -#define SOCKET_IPV6 LWIP_IPV6 -#endif enum interface_protocol { PROTO_IPV4 = 1 << MDNS_IP_PROTOCOL_V4, PROTO_IPV6 = 1 << MDNS_IP_PROTOCOL_V6 @@ -123,13 +116,13 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) } #if defined(CONFIG_IDF_TARGET_LINUX) -#if SOCKET_IPV6 +#ifdef CONFIG_LWIP_IPV6 static char *inet6_ntoa_r(struct in6_addr addr, char *ptr, size_t size) { inet_ntop(AF_INET6, &(addr.s6_addr32[0]), ptr, size); return ptr; } -#endif // SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV6 static char *inet_ntoa_r(struct in_addr addr, char *ptr, size_t size) { char *res = inet_ntoa(addr); @@ -145,12 +138,12 @@ static inline char *get_string_address(struct sockaddr_storage *source_addr) static char address_str[40]; // 40=(8*4+7+term) is the max size of ascii IPv6 addr "XXXX:XX...XX:XXXX" char *res = NULL; // Convert ip address to string -#if SOCKET_IPV4 +#ifdef CONFIG_LWIP_IPV4 if (source_addr->ss_family == PF_INET) { res = inet_ntoa_r(((struct sockaddr_in *)source_addr)->sin_addr, address_str, sizeof(address_str)); } #endif -#if SOCKET_IPV6 +#ifdef CONFIG_LWIP_IPV6 if (source_addr->ss_family == PF_INET6) { res = inet6_ntoa_r(((struct sockaddr_in6 *)source_addr)->sin6_addr, address_str, sizeof(address_str)); } @@ -166,7 +159,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p { size_t ss_addr_len = 0; memset(in_addr, 0, sizeof(struct sockaddr_storage)); -#if SOCKET_IPV4 +#ifdef CONFIG_LWIP_IPV4 if (ip_protocol == MDNS_IP_PROTOCOL_V4 && addr->type == ESP_IPADDR_TYPE_V4) { in_addr->ss_family = PF_INET; #if !defined(CONFIG_IDF_TARGET_LINUX) @@ -177,8 +170,8 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p in_addr_ip4->sin_port = port; in_addr_ip4->sin_addr.s_addr = addr->u_addr.ip4.addr; } -#endif -#if SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV4 +#ifdef CONFIG_LWIP_IPV6 if (ip_protocol == MDNS_IP_PROTOCOL_V6 && addr->type == ESP_IPADDR_TYPE_V6) { memset(in_addr, 0, sizeof(struct sockaddr_storage)); in_addr->ss_family = PF_INET6; @@ -194,7 +187,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p u32_addr[2] = addr->u_addr.ip6.addr[2]; u32_addr[3] = addr->u_addr.ip6.addr[3]; } -#endif // SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV6 return ss_addr_len; } @@ -223,7 +216,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_ip_addr_t *addr, uint16_t *port) { -#if SOCKET_IPV4 +#ifdef CONFIG_LWIP_IPV4 if (in_addr->ss_family == PF_INET) { struct sockaddr_in *in_addr_ip4 = (struct sockaddr_in *)in_addr; memset(addr, 0, sizeof(esp_ip_addr_t)); @@ -231,8 +224,8 @@ static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_i addr->u_addr.ip4.addr = in_addr_ip4->sin_addr.s_addr; addr->type = ESP_IPADDR_TYPE_V4; } -#endif /* SOCKET_IPV4 */ -#if SOCKET_IPV6 +#endif /* CONFIG_LWIP_IPV4 */ +#ifdef CONFIG_LWIP_IPV6 if (in_addr->ss_family == PF_INET6) { struct sockaddr_in6 *in_addr_ip6 = (struct sockaddr_in6 *)in_addr; memset(addr, 0, sizeof(esp_ip_addr_t)); @@ -396,7 +389,7 @@ esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) static int create_socket(esp_netif_t *netif) { -#if SOCKET_IPV6 +#ifdef CONFIG_LWIP_IPV6 int sock = socket(PF_INET6, SOCK_DGRAM, 0); #else int sock = socket(PF_INET, SOCK_DGRAM, 0); @@ -411,8 +404,8 @@ static int create_socket(esp_netif_t *netif) ESP_LOGE(TAG, "Failed setsockopt() to set SO_REUSEADDR. errno=%d: %s\n", errno, strerror(errno)); } // Bind the socket to any address -#if SOCKET_IPV6 - struct sockaddr_in6 saddr = { 0 }; +#ifdef CONFIG_LWIP_IPV6 + struct sockaddr_in6 saddr = { INADDR_ANY }; saddr.sin6_family = AF_INET6; saddr.sin6_port = htons(5353); bzero(&saddr.sin6_addr.s6_addr, sizeof(saddr.sin6_addr.s6_addr)); @@ -431,7 +424,7 @@ static int create_socket(esp_netif_t *netif) ESP_LOGE(TAG, "Failed to bind socket. errno=%d: %s", errno, strerror(errno)); goto err; } -#endif // SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV6 struct ifreq ifr; esp_netif_get_netif_impl_name(netif, ifr.ifr_name); int ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(struct ifreq)); @@ -447,7 +440,7 @@ static int create_socket(esp_netif_t *netif) return -1; } -#if SOCKET_IPV6 +#ifdef CONFIG_LWIP_IPV6 static int socket_add_ipv6_multicast_group(int sock, esp_netif_t *netif) { int ifindex = esp_netif_get_netif_impl_index(netif); @@ -468,9 +461,9 @@ static int socket_add_ipv6_multicast_group(int sock, esp_netif_t *netif) } return err; } -#endif // SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV6 -#if SOCKET_IPV4 +#ifdef CONFIG_LWIP_IPV4 static int socket_add_ipv4_multicast_group(int sock, esp_netif_t *netif) { struct ip_mreq imreq = { 0 }; @@ -495,19 +488,19 @@ static int socket_add_ipv4_multicast_group(int sock, esp_netif_t *netif) err: return err; } -#endif // SOCKET_IPV4 +#endif // CONFIG_LWIP_IPV4 static int join_mdns_multicast_group(int sock, esp_netif_t *netif, mdns_ip_protocol_t ip_protocol) { -#if SOCKET_IPV4 +#ifdef CONFIG_LWIP_IPV4 if (ip_protocol == MDNS_IP_PROTOCOL_V4) { return socket_add_ipv4_multicast_group(sock, netif); } -#endif // SOCKET_IPV4 -#if SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV4 +#ifdef CONFIG_LWIP_IPV6 if (ip_protocol == MDNS_IP_PROTOCOL_V6) { return socket_add_ipv6_multicast_group(sock, netif); } -#endif // SOCKET_IPV6 +#endif // CONFIG_LWIP_IPV6 return -1; } diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 627d42c3e1..56d76b28ea 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -39,6 +39,14 @@ #define NETIF_IPV6_MAX_NUMS 3 #endif +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) +/* CONFIG_LWIP_IPV4 was introduced in IDF v5.1 */ +/* For IDF v5.0, set CONFIG_LWIP_IPV4 to 1 by default */ +#ifndef CONFIG_LWIP_IPV4 +#define CONFIG_LWIP_IPV4 1 +#endif // CONFIG_LWIP_IPV4 +#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) + /** Number of configured interfaces */ #if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES #warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES. diff --git a/components/mdns/tests/host_test/components/esp_netif_linux/Kconfig b/components/mdns/tests/host_test/components/esp_netif_linux/Kconfig index f91ec201bd..3b3c309920 100644 --- a/components/mdns/tests/host_test/components/esp_netif_linux/Kconfig +++ b/components/mdns/tests/host_test/components/esp_netif_linux/Kconfig @@ -6,4 +6,10 @@ menu "LWIP-MOCK-CONFIG" help Enable/disable IPv6 + config LWIP_IPV4 + bool "Enable IPv4" + default y + help + Enable/disable IPv4 + endmenu From 78825e4d5eb3c39df686fdf4c254218a7eff2a08 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Fri, 17 Nov 2023 11:33:21 +0800 Subject: [PATCH 4/4] fix(mdns): define CONFIG_LWIP_IPV4 in mdns_private.h if it is not defined for IDF v5.0 --- components/mdns/mdns_networking_socket.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/mdns/mdns_networking_socket.c b/components/mdns/mdns_networking_socket.c index 8411e26e6d..c752bb1013 100644 --- a/components/mdns/mdns_networking_socket.c +++ b/components/mdns/mdns_networking_socket.c @@ -144,7 +144,7 @@ static inline char *get_string_address(struct sockaddr_storage *source_addr) } #endif #ifdef CONFIG_LWIP_IPV6 - if (source_addr->ss_family == PF_INET6) { + else if (source_addr->ss_family == PF_INET6) { res = inet6_ntoa_r(((struct sockaddr_in6 *)source_addr)->sin6_addr, address_str, sizeof(address_str)); } #endif @@ -172,7 +172,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p } #endif // CONFIG_LWIP_IPV4 #ifdef CONFIG_LWIP_IPV6 - if (ip_protocol == MDNS_IP_PROTOCOL_V6 && addr->type == ESP_IPADDR_TYPE_V6) { + else if (ip_protocol == MDNS_IP_PROTOCOL_V6 && addr->type == ESP_IPADDR_TYPE_V6) { memset(in_addr, 0, sizeof(struct sockaddr_storage)); in_addr->ss_family = PF_INET6; #if !defined(CONFIG_IDF_TARGET_LINUX) @@ -226,7 +226,7 @@ static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_i } #endif /* CONFIG_LWIP_IPV4 */ #ifdef CONFIG_LWIP_IPV6 - if (in_addr->ss_family == PF_INET6) { + else if (in_addr->ss_family == PF_INET6) { struct sockaddr_in6 *in_addr_ip6 = (struct sockaddr_in6 *)in_addr; memset(addr, 0, sizeof(esp_ip_addr_t)); *port = in_addr_ip6->sin6_port;