Skip to content

Commit b5a29ae

Browse files
committed
fix(mdns): fix compiling issue when disabling IPv4
1 parent 24f07f9 commit b5a29ae

File tree

2 files changed

+88
-55
lines changed

2 files changed

+88
-55
lines changed

components/mdns/mdns.c

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mdns_networking.h"
1818
#include "esp_log.h"
1919
#include "esp_random.h"
20+
#include "lwip/opt.h"
2021

2122
#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
2223
#include "esp_eth.h"
@@ -185,7 +186,7 @@ static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if)
185186
static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif)
186187
{
187188
for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
188-
if (esp_netif == s_esp_netifs[i].netif) {
189+
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))) {
189190
return i;
190191
}
191192
}
@@ -1361,7 +1362,9 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
13611362
return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
13621363
} else if (answer->type == MDNS_TYPE_SDPTR) {
13631364
return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
1364-
} else if (answer->type == MDNS_TYPE_A) {
1365+
}
1366+
#if LWIP_IPV4
1367+
else if (answer->type == MDNS_TYPE_A) {
13651368
if (answer->host == &_mdns_self_host) {
13661369
esp_netif_ip_info_t if_ip_info;
13671370
if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
@@ -1388,7 +1391,8 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
13881391
return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye);
13891392
}
13901393
}
1391-
#if CONFIG_LWIP_IPV6
1394+
#endif /* LWIP_IPV4 */
1395+
#if LWIP_IPV6
13921396
else if (answer->type == MDNS_TYPE_AAAA) {
13931397
if (answer->host == &_mdns_self_host) {
13941398
struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS];
@@ -1426,7 +1430,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
14261430
answer->bye);
14271431
}
14281432
}
1429-
#endif
1433+
#endif /* LWIP_IPV6 */
14301434
return 0;
14311435
}
14321436

@@ -2878,6 +2882,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if)
28782882
}
28792883
}
28802884

2885+
#if LWIP_IPV4
28812886
/**
28822887
* @brief Detect IPv4 address collision
28832888
*/
@@ -2891,7 +2896,6 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
28912896
if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
28922897
return 1;//they win
28932898
}
2894-
28952899
int ret = memcmp((uint8_t *)&if_ip_info.ip.addr, (uint8_t *)&ip->addr, sizeof(esp_ip4_addr_t));
28962900
if (ret > 0) {
28972901
return -1;//we win
@@ -2912,8 +2916,9 @@ static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
29122916
}
29132917
return 0;//same
29142918
}
2919+
#endif // LWIP_IPV4
29152920

2916-
#if CONFIG_LWIP_IPV6
2921+
#if LWIP_IPV6
29172922
/**
29182923
* @brief Detect IPv6 address collision
29192924
*/
@@ -2947,7 +2952,7 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if)
29472952
}
29482953
return 0;//same
29492954
}
2950-
#endif
2955+
#endif /* LWIP_IPV6 */
29512956

29522957
static bool _hostname_is_ours(const char *hostname)
29532958
{
@@ -3510,22 +3515,28 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
35103515

35113516
#ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
35123517
// Check if the packet wasn't sent by us
3518+
#if LWIP_IPV4
35133519
if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) {
35143520
esp_netif_ip_info_t if_ip_info;
35153521
if (esp_netif_get_ip_info(_mdns_get_esp_netif(packet->tcpip_if), &if_ip_info) == ESP_OK &&
35163522
memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) {
35173523
return;
35183524
}
3519-
#if CONFIG_LWIP_IPV6
3520-
} else {
3521-
struct esp_ip6_addr if_ip6;
3522-
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK &&
3523-
memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
3524-
return;
3525-
}
3526-
#endif
35273525
}
3526+
#endif // LWIP_IPV4
3527+
#if LWIP_IPV4 && LWIP_IPV6
3528+
else
35283529
#endif
3530+
#if LWIP_IPV6
3531+
if (packet->ip_protocol == MDNS_IP_PROTOCOL_V6) {
3532+
struct esp_ip6_addr if_ip6;
3533+
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK &&
3534+
memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
3535+
return;
3536+
}
3537+
}
3538+
#endif // LWIP_IPV6
3539+
#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
35293540

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

38903901
}
3891-
#if CONFIG_LWIP_IPV6
3902+
#if LWIP_IPV6
38923903
else if (type == MDNS_TYPE_AAAA) {//ipv6
38933904
esp_ip_addr_t ip6;
38943905
ip6.type = ESP_IPADDR_TYPE_V6;
@@ -3941,7 +3952,8 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
39413952
}
39423953

39433954
}
3944-
#endif
3955+
#endif /* LWIP_IPV6 */
3956+
#if LWIP_IPV4
39453957
else if (type == MDNS_TYPE_A) {
39463958
esp_ip_addr_t ip;
39473959
ip.type = ESP_IPADDR_TYPE_V4;
@@ -3994,6 +4006,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
39944006
}
39954007

39964008
}
4009+
#endif /* LWIP_IPV4 */
39974010
}
39984011
//end while
39994012
if (parsed_packet->authoritative) {
@@ -4095,6 +4108,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
40954108
}
40964109

40974110
#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
4111+
#if LWIP_IPV4
40984112
if (action & MDNS_EVENT_IP4_REVERSE_LOOKUP) {
40994113
esp_netif_ip_info_t if_ip_info;
41004114
if (esp_netif_get_ip_info(_mdns_get_esp_netif(mdns_if), &if_ip_info) == ESP_OK) {
@@ -4108,14 +4122,13 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
41084122
}
41094123
}
41104124
}
4111-
4112-
#ifdef CONFIG_LWIP_IPV6
4125+
#endif /* LWIP_IPV4 */
4126+
#if LWIP_IPV6
41134127
if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) {
41144128
esp_ip6_addr_t addr6;
41154129
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(addr6)) {
41164130
uint8_t *paddr = (uint8_t *)&addr6.addr;
41174131
const char sub[] = "ip6";
4118-
const size_t query_name_size = 4 * sizeof(addr6.addr) /* (2 nibbles + 2 dots)/per byte of IP address */ + sizeof(sub);
41194132
char *reverse_query_name = malloc(query_name_size);
41204133
if (reverse_query_name) {
41214134
char *ptr = &reverse_query_name[query_name_size]; // point to the end
@@ -4133,7 +4146,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
41334146
}
41344147
}
41354148
}
4136-
#endif /* CONFIG_LWIP_IPV6 */
4149+
#endif /* LWIP_IPV6 */
41374150
#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
41384151
}
41394152

@@ -5484,22 +5497,25 @@ esp_err_t mdns_init(void)
54845497
#endif
54855498

54865499
uint8_t i;
5487-
#if CONFIG_LWIP_IPV6
5500+
#if LWIP_IPV6
54885501
esp_ip6_addr_t tmp_addr6;
54895502
#endif
5503+
#if LWIP_IPV4
54905504
esp_netif_ip_info_t if_ip_info;
5505+
#endif
54915506

54925507
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
5493-
#if CONFIG_LWIP_IPV6
5508+
#if LWIP_IPV6
54945509
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
54955510
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
54965511
}
54975512
#endif
5513+
#if LWIP_IPV4
54985514
if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
54995515
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
55005516
}
5517+
#endif
55015518
}
5502-
55035519
if (_mdns_service_task_start()) {
55045520
//service start failed!
55055521
err = ESP_FAIL;

components/mdns/mdns_networking_lwip.c

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
9696
netif = esp_netif_get_netif_impl(tcpip_if);
9797
assert(netif);
9898

99+
#if LWIP_IPV4
99100
if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
100101
ip4_addr_t multicast_addr;
101102
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
110111
}
111112
}
112113
}
113-
#if CONFIG_LWIP_IPV6
114-
else {
115-
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
114+
#endif // LWIP_IPV4
115+
#if LWIP_IPV4 && LWIP_IPV6
116+
else
117+
#endif
118+
#if LWIP_IPV6
119+
if (ip_protocol == MDNS_IP_PROTOCOL_V6) {
120+
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
116121

117-
if (join) {
118-
if (mld6_joingroup_netif(netif, &(multicast_addr.u_addr.ip6))) {
119-
return ESP_ERR_INVALID_STATE;
120-
}
121-
} else {
122-
if (mld6_leavegroup_netif(netif, &(multicast_addr.u_addr.ip6))) {
123-
return ESP_ERR_INVALID_STATE;
122+
if (join) {
123+
if (mld6_joingroup_netif(netif, ip_2_ip6(&multicast_addr))) {
124+
return ESP_ERR_INVALID_STATE;
125+
}
126+
} else {
127+
if (mld6_leavegroup_netif(netif, ip_2_ip6(&multicast_addr))) {
128+
return ESP_ERR_INVALID_STATE;
129+
}
124130
}
125131
}
126-
}
127-
#endif
132+
#endif // LWIP_IPV6
128133
return ESP_OK;
129134
}
130135

@@ -152,45 +157,52 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
152157
packet->tcpip_if = MDNS_MAX_INTERFACES;
153158
packet->pb = this_pb;
154159
packet->src_port = rport;
155-
#if CONFIG_LWIP_IPV6
160+
#if LWIP_IPV4 && LWIP_IPV6
156161
packet->src.type = raddr->type;
157162
memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr));
158-
#else
163+
#elif LWIP_IPV4
159164
packet->src.type = IPADDR_TYPE_V4;
160-
memcpy(&packet->src.u_addr.ip4, &raddr->addr, sizeof(ip_addr_t));
165+
packet->src.u_addr.ip4.addr = raddr.addr;
166+
#elif LWIP_IPV6
167+
packet->src.type = IPADDR_TYPE_V6;
168+
memcpy(&packet->src.u_addr.ip6, raddr, sizeof(ip_addr_t));
161169
#endif
162170
packet->dest.type = packet->src.type;
163171

172+
#if LWIP_IPV4
164173
if (packet->src.type == IPADDR_TYPE_V4) {
165174
packet->ip_protocol = MDNS_IP_PROTOCOL_V4;
166175
struct ip_hdr *iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
167176
packet->dest.u_addr.ip4.addr = iphdr->dest.addr;
168177
packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4));
169178
}
170-
#if CONFIG_LWIP_IPV6
171-
else {
172-
packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
173-
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
174-
memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
175-
packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
176-
}
179+
#endif // LWIP_IPV4
180+
#if LWIP_IPV4 && LWIP_IPV6
181+
else
177182
#endif
183+
#if LWIP_IPV6
184+
if (packet->src.type == IPADDR_TYPE_V6) {
185+
packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
186+
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
187+
memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
188+
packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
189+
}
190+
#endif // LWIP_IPV6
178191

179192
//lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
180193
struct netif *netif = NULL;
181194
bool found = false;
182195
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
183196
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
184197
if (s_interfaces[i].proto && netif && netif == ip_current_input_netif ()) {
198+
#if LWIP_IPV4
185199
if (packet->src.type == IPADDR_TYPE_V4) {
186-
#if CONFIG_LWIP_IPV6
187-
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)) {
188-
#else
189-
if ((packet->src.u_addr.ip4.addr & netif->netmask.addr) != (netif->ip_addr.addr & netif->netmask.addr)) {
190-
#endif //packet source is not in the same subnet
200+
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)) {
201+
//packet source is not in the same subnet
191202
break;
192203
}
193204
}
205+
#endif // LWIP_IPV4
194206
packet->tcpip_if = i;
195207
found = true;
196208
break;
@@ -347,12 +359,17 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c
347359
memcpy((uint8_t *)pbt->payload, data, len);
348360

349361
ip_addr_t ip_add_copy;
350-
#if CONFIG_LWIP_IPV6
362+
#if LWIP_IPV6 && LWIP_IPV4
351363
ip_add_copy.type = ip->type;
352364
memcpy(&(ip_add_copy.u_addr), &(ip->u_addr), sizeof(ip_add_copy.u_addr));
353-
#else
354-
memcpy(&(ip_add_copy.addr), &(ip->u_addr), sizeof(ip_add_copy.addr));
355-
#endif // CONFIG_LWIP_IPV6
365+
#elif LWIP_IPV4
366+
ip_add_copy.addr = ip->u_addr.ip4.addr;
367+
#elif LWIP_IPV6
368+
#if LWIP_IPV6_SCOPES
369+
ip_add_copy.zone = ip->u_addr.ip6.zone;
370+
#endif // LWIP_IPV6_SCOPES
371+
memcpy(ip_add_copy.addr, ip->u_addr.ip6.addr, sizeof(ip_add_copy.addr));
372+
#endif
356373

357374
mdns_api_call_t msg = {
358375
.tcpip_if = tcpip_if,

0 commit comments

Comments
 (0)