Skip to content

Commit

Permalink
components: Use CONFIG_LWIP_IPV6 to strip IPv6 function in components
Browse files Browse the repository at this point in the history
* Original commit: espressif/esp-idf@da58235
  • Loading branch information
ESP-YJM authored and suren-gabrielyan-espressif committed May 27, 2022
1 parent b114ed6 commit 1623c0e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
2 changes: 2 additions & 0 deletions components/mdns/include/mdns.h
Expand Up @@ -339,6 +339,7 @@ esp_err_t mdns_query_txt(const char * instance_name, const char * service_type,
*/
esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr);

#if CONFIG_LWIP_IPV6
/**
* @brief Query mDNS for A record
*
Expand All @@ -353,6 +354,7 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr);
#endif

/**
* @brief System event handler
Expand Down
37 changes: 32 additions & 5 deletions components/mdns/mdns.c
Expand Up @@ -761,6 +761,7 @@ static uint16_t _mdns_append_a_record(uint8_t * packet, uint16_t * index, uint32
return record_length;
}

#if CONFIG_LWIP_IPV6
/**
* @brief appends AAAA record to a packet, incrementing the index
*
Expand Down Expand Up @@ -809,6 +810,7 @@ static uint16_t _mdns_append_aaaa_record(uint8_t * packet, uint16_t * index, uin
record_length += part_length;
return record_length;
}
#endif

/**
* @brief Append question to packet
Expand Down Expand Up @@ -874,6 +876,7 @@ static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
return false;
}

#if CONFIG_LWIP_IPV6
/**
* @brief Check if IPv6 address is NULL
*/
Expand All @@ -888,6 +891,7 @@ static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
}
return true;
}
#endif

/**
* @brief Append answer to packet
Expand Down Expand Up @@ -936,7 +940,9 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
return 2;
}
return 1;
} else if (answer->type == MDNS_TYPE_AAAA) {
}
#if CONFIG_LWIP_IPV6
else if (answer->type == MDNS_TYPE_AAAA) {
struct esp_ip6_addr if_ip6;
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) {
return 0;
Expand All @@ -962,6 +968,7 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_
}
return 1;
}
#endif
return 0;
}

Expand Down Expand Up @@ -1235,11 +1242,14 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip
packet->ip_protocol = ip_protocol;
packet->port = MDNS_SERVICE_PORT;
if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
IP_ADDR4(&packet->dst, 224, 0, 0, 251);
} else {
IP4_ADDR(&packet->dst.u_addr.ip4, 224, 0, 0, 251);
}
#if CONFIG_LWIP_IPV6
else {
esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
}
#endif
return packet;
}

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

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

/**
* @brief Check if parsed name is discovery
Expand Down Expand Up @@ -2678,7 +2690,12 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
parsed_packet->id = header.id;
#if CONFIG_LWIP_IPV6
ip_addr_copy(parsed_packet->src, packet->src);
#else
ip4_addr_copy(parsed_packet->src.u_addr.ip4, packet->src.u_addr.ip4);
#endif

parsed_packet->src_port = packet->src_port;

if (header.questions) {
Expand Down Expand Up @@ -2977,7 +2994,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
}
}

} else if (type == MDNS_TYPE_AAAA) {//ipv6
}
#if CONFIG_LWIP_IPV6
else if (type == MDNS_TYPE_AAAA) {//ipv6
esp_ip_addr_t ip6;
ip6.type = IPADDR_TYPE_V6;
memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
Expand Down Expand Up @@ -3023,7 +3042,9 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
}
}

} else if (type == MDNS_TYPE_A) {
}
#endif
else if (type == MDNS_TYPE_A) {
esp_ip_addr_t ip;
ip.type = IPADDR_TYPE_V4;
memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
Expand Down Expand Up @@ -4276,13 +4297,17 @@ esp_err_t mdns_init(void)
}
#endif
uint8_t i;
#if CONFIG_LWIP_IPV6
esp_ip6_addr_t tmp_addr6;
#endif
esp_netif_ip_info_t if_ip_info;

for (i=0; i<MDNS_IF_MAX; i++) {
#if 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
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 @@ -4818,6 +4843,7 @@ esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * add
return ESP_ERR_NOT_FOUND;
}

#if 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;
Expand Down Expand Up @@ -4850,6 +4876,7 @@ esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t *
mdns_query_results_free(result);
return ESP_ERR_NOT_FOUND;
}
#endif

#ifdef MDNS_ENABLE_DEBUG

Expand Down
4 changes: 4 additions & 0 deletions components/mdns/mdns_console.c
Expand Up @@ -116,6 +116,7 @@ static void register_mdns_query_a(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
}

#if CONFIG_LWIP_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 @@ -172,6 +173,7 @@ static void register_mdns_query_aaaa(void)

ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
}
#endif

static struct {
struct arg_str *instance;
Expand Down Expand Up @@ -1049,7 +1051,9 @@ void mdns_console_register(void)
register_mdns_service_remove_all();

register_mdns_query_a();
#if CONFIG_LWIP_IPV6
register_mdns_query_aaaa();
#endif
register_mdns_query_txt();
register_mdns_query_srv();
register_mdns_query_ptr();
Expand Down
33 changes: 24 additions & 9 deletions components/mdns/mdns_networking.c
Expand Up @@ -40,7 +40,7 @@ static esp_err_t _udp_pcb_main_init(void)
}
_pcb_main->mcast_ttl = 1;
_pcb_main->remote_port = MDNS_SERVICE_PORT;
ip_addr_copy(_pcb_main->remote_ip, ip_addr_any_type);
ip_addr_copy(_pcb_main->remote_ip, *(IP_ANY_TYPE));
udp_recv(_pcb_main, &_udp_recv, _mdns_server);
return ESP_OK;
}
Expand Down Expand Up @@ -75,19 +75,21 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
assert(netif);

if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
ip_addr_t multicast_addr;
IP_ADDR4(&multicast_addr, 224, 0, 0, 251);
ip4_addr_t multicast_addr;
IP4_ADDR(&multicast_addr, 224, 0, 0, 251);

if(join){
if (igmp_joingroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) {
if (igmp_joingroup_netif(netif, &multicast_addr)) {
return ESP_ERR_INVALID_STATE;
}
} else {
if (igmp_leavegroup_netif(netif, (const struct ip4_addr *)&multicast_addr.u_addr.ip4)) {
if (igmp_leavegroup_netif(netif, &multicast_addr)) {
return ESP_ERR_INVALID_STATE;
}
}
} else {
}
#if CONFIG_LWIP_IPV6
else {
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);

if(join){
Expand All @@ -100,6 +102,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
}
}
}
#endif
return ESP_OK;
}

Expand Down Expand Up @@ -127,20 +130,29 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
packet->tcpip_if = MDNS_IF_MAX;
packet->pb = this_pb;
packet->src_port = rport;
#if CONFIG_LWIP_IPV6
packet->src.type = raddr->type;
memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr));
#else
packet->src.type = IPADDR_TYPE_V4;
memcpy(&packet->src.u_addr.ip4, &raddr->addr, sizeof(ip_addr_t));
#endif
packet->dest.type = packet->src.type;

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;
} else {
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));
}
packet->multicast = ip_addr_ismulticast(&(packet->dest));
#endif

//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;
Expand All @@ -150,8 +162,11 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
if (pcb && netif && netif == ip_current_input_netif ()) {
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)) {
//packet source is not in the same subnet
#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
pcb = NULL;
break;
}
Expand Down

0 comments on commit 1623c0e

Please sign in to comment.