diff --git a/main.c b/main.c index 4f890ad..ef6866e 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,11 @@ #include #include +#include +#include +#include +#include + #include #include @@ -311,7 +316,44 @@ void init_signal_handlers(void) { if(sigaction(SIGPIPE, &sa, NULL) == -1) { LOG(LVL_ERR, "sigaction [SIGPIPE] failed: %s", strerror(errno)); } +} +void print_addresses(void) { + struct ifaddrs *ifaddr, *ifa; + int family, s; + char host[INET6_ADDRSTRLEN]; + + if(getifaddrs(&ifaddr) == -1) { + LOG(LVL_ERR, "getifaddrs failed: %s", strerror(errno)); + return; + } + + for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if(ifa->ifa_addr == NULL) { + continue; + } + + family = ifa->ifa_addr->sa_family; + + if(family == AF_INET || family == AF_INET6) { + s = getnameinfo(ifa->ifa_addr, + (family == AF_INET) ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6), + host, + INET6_ADDRSTRLEN, + NULL, 0, + NI_NUMERICHOST); + + if(s != 0) { + LOG(LVL_ERR, "getnameinfo failed: %s", gai_strerror(s)); + continue; + } + + LOG(LVL_INFO, "%-8s: %s", ifa->ifa_name, host); + } + } + + freeifaddrs(ifaddr); } int main(int argc, char ** argv) { @@ -419,6 +461,9 @@ int main(int argc, char ** argv) { return EXIT_FAILURE; } + LOG(LVL_INFO, "Startup successful. Here are the IP addresses for this computer:"); + print_addresses(); + while(running) { sleep(60); }