summaryrefslogtreecommitdiff
path: root/src/print_ip_addr.c
blob: c955c1cea0cfa84a0671148e10bca046c4d26a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// vim:ts=4:sw=4:expandtab
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>

#include "i3status.h"

/*
 * Return the IP address for the given interface or "no IP" if the
 * interface is up and running but hasn't got an IP address yet
 *
 */
const char *get_ip_addr(const char *interface, int family) {
    static char part[512];
    socklen_t len = 0;
    if (family == AF_INET)
        len = sizeof(struct sockaddr_in);
    else if (family == AF_INET6)
        len = sizeof(struct sockaddr_in6);

    memset(part, 0, sizeof(part));

    struct ifaddrs *ifaddr, *addrp;
    bool found = false;
    int interface_len = strlen(interface);

    getifaddrs(&ifaddr);

    if (ifaddr == NULL)
        return NULL;

    /* Skip until we are at the input family address of interface */
    for (addrp = ifaddr;

         (addrp != NULL &&
          (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;
        found = true;
        if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
            freeifaddrs(ifaddr);
            return NULL;
        }
    }

    if (addrp == NULL) {
        freeifaddrs(ifaddr);
        return (found ? "no IP" : NULL);
    }

    int ret;
    if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
        fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
        freeifaddrs(ifaddr);
        return "no IP";
    }

    freeifaddrs(ifaddr);
    return part;
}