From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Thu, 8 Jul 2021 20:37:40 +0000 (+0000) Subject: Check return status from getifaddrs. X-Git-Tag: 1.2~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=706cc465d9efc92ecd35f6716af9dfed9812dd35;p=thirdparty%2Fnqptp.git Check return status from getifaddrs. --- diff --git a/nqptp-clock-sources.c b/nqptp-clock-sources.c index a3ca06f..8e4aa13 100644 --- a/nqptp-clock-sources.c +++ b/nqptp-clock-sources.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef CONFIG_FOR_FREEBSD #include @@ -125,34 +126,38 @@ void update_clock_self_identifications(clock_source_private_data *clocks_private struct ifaddrs *ifap, *ifa; void *addr = NULL; short family; - getifaddrs(&ifap); - for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - family = ifa->ifa_addr->sa_family; + int response = getifaddrs(&ifap); + if (response == 0) { + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + family = ifa->ifa_addr->sa_family; #ifdef AF_INET6 - if (ifa->ifa_addr && family == AF_INET6) { - struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)ifa->ifa_addr; - addr = &(sa6->sin6_addr); - } + if (ifa->ifa_addr && family == AF_INET6) { + struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)ifa->ifa_addr; + addr = &(sa6->sin6_addr); + } #endif - if (ifa->ifa_addr && family == AF_INET) { - struct sockaddr_in *sa4 = (struct sockaddr_in *)ifa->ifa_addr; - addr = &(sa4->sin_addr); - } - char ip_string[64]; - memset(ip_string, 0, sizeof(ip_string)); - if (addr != NULL) - inet_ntop(family, addr, ip_string, sizeof(ip_string)); - if (strlen(ip_string) != 0) { - // now set the is_one_of_ours flag of any clock with this ip - for (i = 0; i < MAX_CLOCKS; i++) { - if (strcasecmp(ip_string, clocks_private_info[i].ip) == 0) { - debug(2, "found an entry for one of our clocks"); - clocks_private_info[i].is_one_of_ours = 1; + if (ifa->ifa_addr && family == AF_INET) { + struct sockaddr_in *sa4 = (struct sockaddr_in *)ifa->ifa_addr; + addr = &(sa4->sin_addr); + } + char ip_string[64]; + memset(ip_string, 0, sizeof(ip_string)); + if (addr != NULL) + inet_ntop(family, addr, ip_string, sizeof(ip_string)); + if (strlen(ip_string) != 0) { + // now set the is_one_of_ours flag of any clock with this ip + for (i = 0; i < MAX_CLOCKS; i++) { + if (strcasecmp(ip_string, clocks_private_info[i].ip) == 0) { + debug(2, "found an entry for one of our clocks"); + clocks_private_info[i].is_one_of_ours = 1; + } } } } + freeifaddrs(ifap); + } else { + debug(1,"getifaddrs error - %s.", strerror(errno)); } - freeifaddrs(ifap); } void debug_log_nqptp_status(int level) {