diff --git a/common_components/protocol_examples_common/addr_from_stdin.c b/common_components/protocol_examples_common/addr_from_stdin.c index 0ea452b596..71ac6d126f 100644 --- a/common_components/protocol_examples_common/addr_from_stdin.c +++ b/common_components/protocol_examples_common/addr_from_stdin.c @@ -41,6 +41,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad } for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) { memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); +#if LWIP_IPV4 if (cur->ai_family == AF_INET) { *ip_protocol = IPPROTO_IP; *addr_family = AF_INET; @@ -50,17 +51,21 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad return ESP_OK; } -#if CONFIG_LWIP_IPV6 - else if (cur->ai_family == AF_INET6) { - *ip_protocol = IPPROTO_IPV6; - *addr_family = AF_INET6; - // add port and interface number and return on first IPv6 match - ((struct sockaddr_in6 *)dest_addr)->sin6_port = htons(port); - ((struct sockaddr_in6 *)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); - freeaddrinfo( addr_list ); - return ESP_OK; - } +#endif // LWIP_IPV4 +#if LWIP_IPV4 && LWIP_IPV6 + else #endif +#if LWIP_IPV6 + if (cur->ai_family == AF_INET6) { + *ip_protocol = IPPROTO_IPV6; + *addr_family = AF_INET6; + // add port and interface number and return on first IPv6 match + ((struct sockaddr_in6 *)dest_addr)->sin6_port = htons(port); + ((struct sockaddr_in6 *)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); + freeaddrinfo( addr_list ); + return ESP_OK; + } +#endif // LWIP_IPV6 } // no match found freeaddrinfo( addr_list ); diff --git a/common_components/protocol_examples_common/connect.c b/common_components/protocol_examples_common/connect.c index 3fae508bd6..93a71baef1 100644 --- a/common_components/protocol_examples_common/connect.c +++ b/common_components/protocol_examples_common/connect.c @@ -31,7 +31,11 @@ #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 #define MAX_IP6_ADDRS_PER_NETIF (5) +#if LWIP_IPV4 #define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2) +#else +#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR s_active_interfaces +#endif #if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL @@ -213,14 +217,15 @@ esp_err_t example_connect(void) } // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; - esp_netif_ip_info_t ip; for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { netif = esp_netif_next(netif); if (is_our_netif(TAG, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); +#if LWIP_IPV4 + esp_netif_ip_info_t ip; ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); - ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip)); +#endif #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 82602a8a6c..324b7a8974 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -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" @@ -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; } } @@ -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) { @@ -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]; @@ -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; } @@ -2878,6 +2882,7 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if) } } +#if LWIP_IPV4 /** * @brief Detect IPv4 address collision */ @@ -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 @@ -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 */ @@ -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) { @@ -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) { @@ -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; @@ -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; @@ -3994,6 +4006,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet) } } +#endif /* LWIP_IPV4 */ } //end while if (parsed_packet->authoritative) { @@ -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) { @@ -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 @@ -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 */ } @@ -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; diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index 402f120539..a8a192d2cb 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -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); @@ -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; } @@ -152,29 +157,37 @@ 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; @@ -183,13 +196,12 @@ 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 (s_interfaces[i].proto && 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)) { -#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 LWIP_IPV4 + 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; @@ -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,