Skip to content

Commit

Permalink
mdns: append all ipv6 address in mdns answer
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqinan committed Mar 22, 2023
1 parent 5b3346f commit 5ed3e9a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
44 changes: 26 additions & 18 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1385,32 +1385,40 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
#if CONFIG_LWIP_IPV6
else if (answer->type == MDNS_TYPE_AAAA) {
if (answer->host == &_mdns_self_host) {
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) {
struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS];
uint8_t count = 0;
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;
}
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
return 0;
}
if (_ipv6_address_is_zero(if_ip6)) {
return 0;
}
if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)if_ip6.addr, answer->flush, answer->bye) <= 0) {
return 0;
count = esp_netif_get_all_ip6(_mdns_get_esp_netif(tcpip_if), if_ip6s);
assert(count <= NETIF_IPV6_MAX_NUMS);
for (int i = 0; i < count; i++) {
if (_ipv6_address_is_zero(if_ip6s[i])) {
return 0;
}
if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)if_ip6s[i].addr,
answer->flush, answer->bye) <= 0) {
return 0;
}
}
if (!_mdns_if_is_dup(tcpip_if)) {
return 1;
return count;
}
mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &if_ip6)) {
return 1;

mdns_if_t other_if = _mdns_get_other_if(tcpip_if);
struct esp_ip6_addr other_ip6;
if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) {
return count;
}
if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)if_ip6.addr, answer->flush, answer->bye) > 0) {
return 2;
if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)other_ip6.addr,
answer->flush, answer->bye) > 0) {
return 1 + count;
}
return 1;
return count;
} else if (answer->host != NULL) {
return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush, answer->bye);
return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush,
answer->bye);
}
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions components/mdns/private_include/mdns_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#endif
#define MDNS_MAX_PREDEF_INTERFACES (CONFIG_MDNS_PREDEF_NETIF_STA + CONFIG_MDNS_PREDEF_NETIF_AP + CONFIG_MDNS_PREDEF_NETIF_ETH)

#ifdef CONFIG_LWIP_IPV6_NUM_ADDRESSES
#define NETIF_IPV6_MAX_NUMS CONFIG_LWIP_IPV6_NUM_ADDRESSES
#else
#define NETIF_IPV6_MAX_NUMS 3
#endif

/** Number of configured interfaces */
#if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES
#warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat
return ESP_OK;
}

int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[])
{
if (esp_netif == NULL) {
return 0;
}
struct ifaddrs *addrs, *tmp;
int addr_count = 0;
getifaddrs(&addrs);
tmp = addrs;

while (tmp) {
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET6) {
struct sockaddr_in6 *pAddr = (struct sockaddr_in6 *)tmp->ifa_addr;
if (strcmp(esp_netif->if_desc, tmp->ifa_name) == 0) {
memcpy(&if_ip6[addr_count++], &pAddr->sin6_addr, 4 * 4);
}
}
tmp = tmp->ifa_next;
}

freeifaddrs(addrs);
return addr_count;
}

esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
{
Expand Down

0 comments on commit 5ed3e9a

Please sign in to comment.