summaryrefslogtreecommitdiff
path: root/src/print_ip_addr.c
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2018-06-04 19:59:09 +0200
committerOlivier Gayot <olivier.gayot@sigexec.com>2018-06-04 19:59:09 +0200
commit99a6fb5e49515accfb93dc6bc79b439d0a4f85ca (patch)
tree9555178f3daa865a950183800bc300d7723cc6e7 /src/print_ip_addr.c
parent9f08fe297a59553e6a0f1c0078014afa1209e51b (diff)
Simplify the algorithm used to determine the IP address
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'src/print_ip_addr.c')
-rw-r--r--src/print_ip_addr.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/print_ip_addr.c b/src/print_ip_addr.c
index c955c1c..cf476ae 100644
--- a/src/print_ip_addr.c
+++ b/src/print_ip_addr.c
@@ -37,22 +37,25 @@ const char *get_ip_addr(const char *interface, int family) {
return NULL;
/* 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) {
+ /* The interface does not have the right name, skip it. */
+ continue;
+ }
- (addrp != NULL &&
- (strncmp(addrp->ifa_name, interface, interface_len) != 0 ||
- addrp->ifa_addr == NULL ||
- addrp->ifa_addr->sa_family != family));
+ if (addrp->ifa_addr != NULL && addrp->ifa_addr->sa_family == family) {
+ /* We found the right interface with the right address. */
+ break;
+ }
- addrp = addrp->ifa_next) {
- /* Check if the interface is down */
- if (strncmp(addrp->ifa_name, interface, interface_len) != 0)
- continue;
- found = true;
+ /* Check if the interface is down. If it is, no need to look any
+ * further. */
if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
freeifaddrs(ifaddr);
return NULL;
}
+
+ found = true;
}
if (addrp == NULL) {