Skip to content

Commit

Permalink
fix(mdns): fix compiling issue when disabling IPv4
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Nov 13, 2023
1 parent 24f07f9 commit b5a29ae
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 55 deletions.
64 changes: 40 additions & 24 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mdns_networking.h"
#include "esp_log.h"
#include "esp_random.h"
#include "lwip/opt.h"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
#if CONFIG_LWIP_IPV6
#if LWIP_IPV6
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
}
#endif
#if LWIP_IPV4
if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
}
#endif
}

if (_mdns_service_task_start()) {
//service start failed!
err = ESP_FAIL;
Expand Down
79 changes: 48 additions & 31 deletions components/mdns/mdns_networking_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -152,45 +157,52 @@ 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;
bool found = false;
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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b5a29ae

Please sign in to comment.