Skip to content

Commit 1623c0e

Browse files
ESP-YJMsuren-gabrielyan-espressif
authored andcommitted
components: Use CONFIG_LWIP_IPV6 to strip IPv6 function in components
* Original commit: espressif/esp-idf@da58235
1 parent b114ed6 commit 1623c0e

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

components/mdns/include/mdns.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ esp_err_t mdns_query_txt(const char * instance_name, const char * service_type,
339339
*/
340340
esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr);
341341

342+
#if CONFIG_LWIP_IPV6
342343
/**
343344
* @brief Query mDNS for A record
344345
*
@@ -353,6 +354,7 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t
353354
* - ESP_ERR_INVALID_ARG parameter error
354355
*/
355356
esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr);
357+
#endif
356358

357359
/**
358360
* @brief System event handler

components/mdns/mdns.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ static uint16_t _mdns_append_a_record(uint8_t * packet, uint16_t * index, uint32
761761
return record_length;
762762
}
763763

764+
#if CONFIG_LWIP_IPV6
764765
/**
765766
* @brief appends AAAA record to a packet, incrementing the index
766767
*
@@ -809,6 +810,7 @@ static uint16_t _mdns_append_aaaa_record(uint8_t * packet, uint16_t * index, uin
809810
record_length += part_length;
810811
return record_length;
811812
}
813+
#endif
812814

813815
/**
814816
* @brief Append question to packet
@@ -874,6 +876,7 @@ static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
874876
return false;
875877
}
876878

879+
#if CONFIG_LWIP_IPV6
877880
/**
878881
* @brief Check if IPv6 address is NULL
879882
*/
@@ -888,6 +891,7 @@ static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
888891
}
889892
return true;
890893
}
894+
#endif
891895

892896
/**
893897
* @brief Append answer to packet
@@ -936,7 +940,9 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
936940
return 2;
937941
}
938942
return 1;
939-
} else if (answer->type == MDNS_TYPE_AAAA) {
943+
}
944+
#if CONFIG_LWIP_IPV6
945+
else if (answer->type == MDNS_TYPE_AAAA) {
940946
struct esp_ip6_addr if_ip6;
941947
if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
942948
return 0;
@@ -962,6 +968,7 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
962968
}
963969
return 1;
964970
}
971+
#endif
965972
return 0;
966973
}
967974

@@ -1235,11 +1242,14 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip
12351242
packet->ip_protocol = ip_protocol;
12361243
packet->port = MDNS_SERVICE_PORT;
12371244
if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
1238-
IP_ADDR4(&packet->dst, 224, 0, 0, 251);
1239-
} else {
1245+
IP4_ADDR(&packet->dst.u_addr.ip4, 224, 0, 0, 251);
1246+
}
1247+
#if CONFIG_LWIP_IPV6
1248+
else {
12401249
esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
12411250
memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
12421251
}
1252+
#endif
12431253
return packet;
12441254
}
12451255

@@ -2253,6 +2263,7 @@ static int _mdns_check_a_collision(esp_ip4_addr_t * ip, mdns_if_t tcpip_if)
22532263
return 0;//same
22542264
}
22552265

2266+
#if CONFIG_LWIP_IPV6
22562267
/**
22572268
* @brief Detect IPv6 address collision
22582269
*/
@@ -2286,6 +2297,7 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if)
22862297
}
22872298
return 0;//same
22882299
}
2300+
#endif
22892301

22902302
/**
22912303
* @brief Check if parsed name is discovery
@@ -2678,7 +2690,12 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
26782690
parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
26792691
parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
26802692
parsed_packet->id = header.id;
2693+
#if CONFIG_LWIP_IPV6
26812694
ip_addr_copy(parsed_packet->src, packet->src);
2695+
#else
2696+
ip4_addr_copy(parsed_packet->src.u_addr.ip4, packet->src.u_addr.ip4);
2697+
#endif
2698+
26822699
parsed_packet->src_port = packet->src_port;
26832700

26842701
if (header.questions) {
@@ -2977,7 +2994,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
29772994
}
29782995
}
29792996

2980-
} else if (type == MDNS_TYPE_AAAA) {//ipv6
2997+
}
2998+
#if CONFIG_LWIP_IPV6
2999+
else if (type == MDNS_TYPE_AAAA) {//ipv6
29813000
esp_ip_addr_t ip6;
29823001
ip6.type = IPADDR_TYPE_V6;
29833002
memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
@@ -3023,7 +3042,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
30233042
}
30243043
}
30253044

3026-
} else if (type == MDNS_TYPE_A) {
3045+
}
3046+
#endif
3047+
else if (type == MDNS_TYPE_A) {
30273048
esp_ip_addr_t ip;
30283049
ip.type = IPADDR_TYPE_V4;
30293050
memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
@@ -4276,13 +4297,17 @@ esp_err_t mdns_init(void)
42764297
}
42774298
#endif
42784299
uint8_t i;
4300+
#if CONFIG_LWIP_IPV6
42794301
esp_ip6_addr_t tmp_addr6;
4302+
#endif
42804303
esp_netif_ip_info_t if_ip_info;
42814304

42824305
for (i=0; i<MDNS_IF_MAX; i++) {
4306+
#if CONFIG_LWIP_IPV6
42834307
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
42844308
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
42854309
}
4310+
#endif
42864311
if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
42874312
_mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
42884313
}
@@ -4818,6 +4843,7 @@ esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * add
48184843
return ESP_ERR_NOT_FOUND;
48194844
}
48204845

4846+
#if CONFIG_LWIP_IPV6
48214847
esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t * addr)
48224848
{
48234849
mdns_result_t * result = NULL;
@@ -4850,6 +4876,7 @@ esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t *
48504876
mdns_query_results_free(result);
48514877
return ESP_ERR_NOT_FOUND;
48524878
}
4879+
#endif
48534880

48544881
#ifdef MDNS_ENABLE_DEBUG
48554882

components/mdns/mdns_console.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static void register_mdns_query_a(void)
116116
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
117117
}
118118

119+
#if CONFIG_LWIP_IPV6
119120
static int cmd_mdns_query_aaaa(int argc, char** argv)
120121
{
121122
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
@@ -172,6 +173,7 @@ static void register_mdns_query_aaaa(void)
172173

173174
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
174175
}
176+
#endif
175177

176178
static struct {
177179
struct arg_str *instance;
@@ -1049,7 +1051,9 @@ void mdns_console_register(void)
10491051
register_mdns_service_remove_all();
10501052

10511053
register_mdns_query_a();
1054+
#if CONFIG_LWIP_IPV6
10521055
register_mdns_query_aaaa();
1056+
#endif
10531057
register_mdns_query_txt();
10541058
register_mdns_query_srv();
10551059
register_mdns_query_ptr();

components/mdns/mdns_networking.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static esp_err_t _udp_pcb_main_init(void)
4040
}
4141
_pcb_main->mcast_ttl = 1;
4242
_pcb_main->remote_port = MDNS_SERVICE_PORT;
43-
ip_addr_copy(_pcb_main->remote_ip, ip_addr_any_type);
43+
ip_addr_copy(_pcb_main->remote_ip, *(IP_ANY_TYPE));
4444
udp_recv(_pcb_main, &_udp_recv, _mdns_server);
4545
return ESP_OK;
4646
}
@@ -75,19 +75,21 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
7575
assert(netif);
7676

7777
if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
78-
ip_addr_t multicast_addr;
79-
IP_ADDR4(&multicast_addr, 224, 0, 0, 251);
78+
ip4_addr_t multicast_addr;
79+
IP4_ADDR(&multicast_addr, 224, 0, 0, 251);
8080

8181
if(join){
82-
if (igmp_joingroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) {
82+
if (igmp_joingroup_netif(netif, &multicast_addr)) {
8383
return ESP_ERR_INVALID_STATE;
8484
}
8585
} else {
86-
if (igmp_leavegroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) {
86+
if (igmp_leavegroup_netif(netif, &multicast_addr)) {
8787
return ESP_ERR_INVALID_STATE;
8888
}
8989
}
90-
} else {
90+
}
91+
#if CONFIG_LWIP_IPV6
92+
else {
9193
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
9294

9395
if(join){
@@ -100,6 +102,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
100102
}
101103
}
102104
}
105+
#endif
103106
return ESP_OK;
104107
}
105108

@@ -127,20 +130,29 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
127130
packet->tcpip_if = MDNS_IF_MAX;
128131
packet->pb = this_pb;
129132
packet->src_port = rport;
133+
#if CONFIG_LWIP_IPV6
130134
packet->src.type = raddr->type;
131135
memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr));
136+
#else
137+
packet->src.type = IPADDR_TYPE_V4;
138+
memcpy(&packet->src.u_addr.ip4, &raddr->addr, sizeof(ip_addr_t));
139+
#endif
132140
packet->dest.type = packet->src.type;
133141

134142
if (packet->src.type == IPADDR_TYPE_V4) {
135143
packet->ip_protocol = MDNS_IP_PROTOCOL_V4;
136144
struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
137145
packet->dest.u_addr.ip4.addr = iphdr->dest.addr;
138-
} else {
146+
packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4));
147+
}
148+
#if CONFIG_LWIP_IPV6
149+
else {
139150
packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
140151
struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
141152
memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
153+
packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
142154
}
143-
packet->multicast = ip_addr_ismulticast(&(packet->dest));
155+
#endif
144156

145157
//lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
146158
struct netif * netif = NULL;
@@ -150,8 +162,11 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
150162
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
151163
if (pcb && netif && netif == ip_current_input_netif ()) {
152164
if (packet->src.type == IPADDR_TYPE_V4) {
165+
#if CONFIG_LWIP_IPV6
153166
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)) {
154-
//packet source is not in the same subnet
167+
#else
168+
if ((packet->src.u_addr.ip4.addr & netif->netmask.addr) != (netif->ip_addr.addr & netif->netmask.addr)) {
169+
#endif //packet source is not in the same subnet
155170
pcb = NULL;
156171
break;
157172
}

0 commit comments

Comments
 (0)