Simplify the algorithm used to determine the IP address
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
This commit is contained in:
parent
9f08fe297a
commit
99a6fb5e49
@ -37,22 +37,25 @@ const char *get_ip_addr(const char *interface, int family) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Skip until we are at the input family address of interface */
|
/* Skip until we are at the input family address of interface */
|
||||||
for (addrp = ifaddr;
|
for (addrp = ifaddr; addrp != NULL; addrp = addrp->ifa_next) {
|
||||||
|
if (strncmp(addrp->ifa_name, interface, interface_len) != 0) {
|
||||||
(addrp != NULL &&
|
/* The interface does not have the right name, skip it. */
|
||||||
(strncmp(addrp->ifa_name, interface, interface_len) != 0 ||
|
|
||||||
addrp->ifa_addr == NULL ||
|
|
||||||
addrp->ifa_addr->sa_family != family));
|
|
||||||
|
|
||||||
addrp = addrp->ifa_next) {
|
|
||||||
/* Check if the interface is down */
|
|
||||||
if (strncmp(addrp->ifa_name, interface, interface_len) != 0)
|
|
||||||
continue;
|
continue;
|
||||||
found = true;
|
}
|
||||||
|
|
||||||
|
if (addrp->ifa_addr != NULL && addrp->ifa_addr->sa_family == family) {
|
||||||
|
/* We found the right interface with the right address. */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the interface is down. If it is, no need to look any
|
||||||
|
* further. */
|
||||||
if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
|
if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
|
||||||
freeifaddrs(ifaddr);
|
freeifaddrs(ifaddr);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
found = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addrp == NULL) {
|
if (addrp == NULL) {
|
||||||
|
Loading…
Reference in New Issue
Block a user