udp_multicast_example: better handling wrong addresses

inet_aton returns 0 on failure, but socket_add_ipv4_multicast_group has to return negative values for failures
getaddrinfo sets res to zero of address could not resolved, but doesn't necessarily return an
error. res is now checked for zero before dereferencing

Merges https://github.com/espressif/esp-idf/pull/2814
This commit is contained in:
gfrodo 2018-12-10 11:51:54 +01:00 committed by bot
parent 853dd3ff7a
commit 269c4c74d2

View file

@ -74,6 +74,8 @@ static int socket_add_ipv4_multicast_group(int sock, bool assign_source_if)
err = inet_aton(MULTICAST_IPV4_ADDR, &imreq.imr_multiaddr.s_addr);
if (err != 1) {
ESP_LOGE(V4TAG, "Configured IPV4 multicast address '%s' is invalid.", MULTICAST_IPV4_ADDR);
// Errors in the return value have to be negative
err = -1;
goto err;
}
ESP_LOGI(TAG, "Configured IPV4 Multicast address %s", inet_ntoa(imreq.imr_multiaddr.s_addr));
@ -426,6 +428,10 @@ static void mcast_example_task(void *pvParameters)
ESP_LOGE(TAG, "getaddrinfo() failed for IPV4 destination address. error: %d", err);
break;
}
if (res == 0) {
ESP_LOGE(TAG, "getaddrinfo() did not return any addresses");
break;
}
#ifdef CONFIG_EXAMPLE_IPV4_ONLY
((struct sockaddr_in *)res->ai_addr)->sin_port = htons(UDP_PORT);
inet_ntoa_r(((struct sockaddr_in *)res->ai_addr)->sin_addr, addrbuf, sizeof(addrbuf)-1);