List IP addresses on startup

This commit is contained in:
Thomas Kolb 2013-01-30 21:26:06 +01:00
parent bd21ab6dd7
commit 0e71bbf605
1 changed files with 45 additions and 0 deletions

45
main.c
View File

@ -10,6 +10,11 @@
#include <fcntl.h>
#include <signal.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <sys/socket.h>
#include <linux/limits.h>
#include <microhttpd.h>
@ -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);
}