summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2009-07-22 18:27:55 +0200
committerMichael Stapelberg <michael@stapelberg.de>2009-07-22 18:27:55 +0200
commit4a6deddf7b3e38203d27a10baf7ba21ff90d96d3 (patch)
tree2498936743b213c14f4dfe33a57f65b0407a46cf
parentbb50e22fd3ad82381dbf750a790b8380a1a1bba5 (diff)
Obtain IP address in a portable way, patch by Baptiste Daroussin
-rw-r--r--src/get_ip_addr.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/src/get_ip_addr.c b/src/get_ip_addr.c
index ba39a3d..354ae08 100644
--- a/src/get_ip_addr.c
+++ b/src/get_ip_addr.c
@@ -1,12 +1,12 @@
// vim:ts=8:expandtab
#include <net/if.h>
#include <sys/socket.h>
-#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
+#include <ifaddrs.h>
#include "i3status.h"
@@ -17,24 +17,40 @@
*/
const char *get_ip_addr(const char *interface) {
static char part[512];
- struct ifreq ifr;
socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
- /* First check if the interface is running */
- (void)strcpy(ifr.ifr_name, interface);
- if (ioctl(general_socket, SIOCGIFFLAGS, &ifr) < 0 ||
- !(ifr.ifr_flags & IFF_RUNNING))
- return NULL;
+ struct ifaddrs *ifaddr, *addrp;
- /* Interface is up, get the IP address */
- (void)strcpy(ifr.ifr_name, interface);
- ifr.ifr_addr.sa_family = AF_INET;
- if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
+ 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 eth_interface */
+ for (addrp = ifaddr;
+
+ (addrp != NULL &&
+ (strcmp(addrp->ifa_name, eth_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, eth_interface) == 0 &&
+ (addrp->ifa_flags & IFF_RUNNING) == 0)
+ return NULL;
+ }
+
+ if (addrp == NULL)
return "no IP";
int ret;
- if ((ret = getnameinfo(&ifr.ifr_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
+ if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
return "no IP";
}