Skip to content

Commit

Permalink
fix(mdns): Remove lwip include in mdns.c file
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Nov 16, 2023
1 parent b5a29ae commit 9a490aa
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 73 deletions.
14 changes: 14 additions & 0 deletions components/mdns/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions components/mdns/examples/query_advertise/main/mdns_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions components/mdns/examples/query_advertise/sdkconfig.ci.eth_no_ipv4
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 39 additions & 33 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1061,6 +1060,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
*
Expand Down Expand Up @@ -1110,8 +1110,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
*
Expand Down Expand Up @@ -1234,7 +1235,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
*/
Expand All @@ -1259,17 +1260,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;
Expand Down Expand Up @@ -1363,7 +1366,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;
Expand Down Expand Up @@ -1391,8 +1394,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];
Expand Down Expand Up @@ -1430,7 +1433,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;
}

Expand Down Expand Up @@ -1718,12 +1721,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));
}
Expand Down Expand Up @@ -2882,7 +2887,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if)
}
}

#if LWIP_IPV4
#ifdef CONFIG_MDNS_IPV4
/**
* @brief Detect IPv4 address collision
*/
Expand Down Expand Up @@ -2916,9 +2921,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
*/
Expand Down Expand Up @@ -2952,7 +2957,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)
{
Expand Down Expand Up @@ -3515,27 +3520,27 @@ 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 &&
memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) {
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 &&
memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
return;
}
}
#endif // LWIP_IPV6
#endif /* CONFIG_MDNS_IPV6 */
#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES

// Check for the minimum size of mdns packet
Expand Down Expand Up @@ -3899,7 +3904,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;
Expand Down Expand Up @@ -3952,8 +3957,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;
Expand Down Expand Up @@ -4006,7 +4011,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
}

}
#endif /* LWIP_IPV4 */
#endif /* CONFIG_MDNS_IPV4 */
}
//end while
if (parsed_packet->authoritative) {
Expand Down Expand Up @@ -4108,7 +4113,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) {
Expand All @@ -4122,13 +4127,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
Expand All @@ -4146,7 +4152,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 */
}

Expand Down Expand Up @@ -5497,20 +5503,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);
}
Expand Down Expand Up @@ -6521,7 +6527,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 MDNS_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
13 changes: 8 additions & 5 deletions components/mdns/mdns_console.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 9a490aa

Please sign in to comment.