From 1623c0e729e8899da8733164d86398eac74e92e2 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Mon, 18 Jan 2021 19:14:51 +0800 Subject: [PATCH] components: Use CONFIG_LWIP_IPV6 to strip IPv6 function in components * Original commit: espressif/esp-idf@da58235a0ee262ff552c5f1155d531b5c31e8de6 --- components/mdns/include/mdns.h | 2 ++ components/mdns/mdns.c | 37 ++++++++++++++++++++++++++----- components/mdns/mdns_console.c | 4 ++++ components/mdns/mdns_networking.c | 33 +++++++++++++++++++-------- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index 6cee1e313..aeda9a071 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -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 * @@ -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 diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 35cbe2293..fd3706f9f 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -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 * @@ -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 @@ -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 */ @@ -888,6 +891,7 @@ static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6) } return true; } +#endif /** * @brief Append answer to packet @@ -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; @@ -962,6 +968,7 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_ } return 1; } +#endif return 0; } @@ -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; } @@ -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 */ @@ -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 @@ -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) { @@ -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); @@ -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); @@ -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; imcast_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; } @@ -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){ @@ -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; } @@ -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; @@ -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; }