summaryrefslogtreecommitdiff
path: root/src/print_ip_addr.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2009-10-11 22:11:09 +0200
committerMichael Stapelberg <michael@stapelberg.de>2009-10-11 22:14:29 +0200
commitf947d0a446b1b99020722cbc71127fc0c06086b2 (patch)
tree87b75e70a7e2c0d162685221c66beed5f6b6a9e6 /src/print_ip_addr.c
parent1d122f32e6d2b0ae1f964dd755d769885c7bf1c2 (diff)
Breaks configfiles! Major refactoring of i3status, see below
We finally switched to libconfuse for a configuration file format which does not require much work for the programmer nor for the user. Plus, it avoids the Not-Invented-Here syndrome of yet another config file format. Furthermore, as a consequence of providing format strings for every "module" (ipv6, wireless, …), we directly print the output and thus we needed to drop support for wmii. This allowed us to get rid of quite some complexity. Documentation about the new configuration file and options will follow. This commit is the beginning of what will be i3status v2.0.
Diffstat (limited to 'src/print_ip_addr.c')
-rw-r--r--src/print_ip_addr.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/print_ip_addr.c b/src/print_ip_addr.c
new file mode 100644
index 0000000..6ddd35a
--- /dev/null
+++ b/src/print_ip_addr.c
@@ -0,0 +1,67 @@
+// vim:ts=8:expandtab
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/types.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) {
+ static char part[512];
+ socklen_t len = sizeof(struct sockaddr_in);
+ memset(part, 0, sizeof(part));
+
+ struct ifaddrs *ifaddr, *addrp;
+
+ getifaddrs(&ifaddr);
+
+ if (ifaddr == NULL) {
+ (void)snprintf(part, sizeof(part), "E: down");
+ return part;
+ }
+
+ addrp = ifaddr;
+
+ /* Skip until we are at the AF_INET address of interface */
+ for (addrp = ifaddr;
+
+ (addrp != NULL &&
+ (strcmp(addrp->ifa_name, interface) != 0 ||
+ addrp->ifa_addr == NULL ||
+ addrp->ifa_addr->sa_family != AF_INET));
+
+ addrp = addrp->ifa_next) {
+ /* Check if the interface is down */
+ if (strcmp(addrp->ifa_name, interface) == 0 &&
+ (addrp->ifa_flags & IFF_RUNNING) == 0) {
+ freeifaddrs(ifaddr);
+ return NULL;
+ }
+ }
+
+ if (addrp == NULL) {
+ freeifaddrs(ifaddr);
+ return "no IP";
+ }
+
+ int ret;
+ if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
+ fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
+ freeifaddrs(ifaddr);
+ return "no IP";
+ }
+
+ freeifaddrs(ifaddr);
+ return part;
+}
+