diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-10 14:40:20 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-10 14:40:20 +0100 |
commit | 3abd448fd6d9171515a4951cc6a629b0ff83ddd4 (patch) | |
tree | 83c3c5d12cd0e2b66a3bc6fd5723b84701b9b539 | |
parent | a469bc2454c177448fed4bba434a2b0a7464153b (diff) | |
download | econ-3abd448fd6d9171515a4951cc6a629b0ff83ddd4.tar.gz econ-3abd448fd6d9171515a4951cc6a629b0ff83ddd4.tar.bz2 econ-3abd448fd6d9171515a4951cc6a629b0ff83ddd4.zip |
util: Split sock_get_netmask into a utility function sock_get_ifreq
Which may be reused for getting e.g. hwaddr.
-rw-r--r-- | util.c | 61 |
1 files changed, 24 insertions, 37 deletions
@@ -28,61 +28,48 @@ sock_get_ipv4_addr(int fd) return own->sin_addr.s_addr; } -uint32_t -sock_get_netmask(int fd) +static struct ifreq * +sock_get_ifreq(int fd) { -#if 0 - struct ifreq ifreq; -#endif struct ifconf ifconf; - struct ifreq ifreqs[16]; + static struct ifreq ifreqs[16]; + struct sockaddr_in *sin; + uint32_t own_ip; int i; - uint32_t own_ip, ip; own_ip = sock_get_ipv4_addr(fd); -#if 0 - memset(&ifreq, 0, sizeof ifreq); -#endif - ifconf.ifc_len = sizeof ifreqs; ifconf.ifc_req = ifreqs; if (ioctl(fd, SIOCGIFCONF, &ifconf) < 0) { fprintf(stderr, "retrieving interfaces failed: %s\n", strerror(errno)); - return 0; + return NULL; } for (i = 0; i < ifconf.ifc_len/sizeof(ifreqs[0]); i++) { - ip = ((struct sockaddr_in *) &ifreqs[i].ifr_addr)->sin_addr.s_addr; - - printf("ifname: %s: ip: %08x", ifreqs[i].ifr_name, - ((struct sockaddr_in *) &ifreqs[i].ifr_addr)->sin_addr.s_addr); - - if (ip == own_ip) { - if (ioctl(fd, SIOCGIFNETMASK, &ifreqs[i]) < 0) - continue; - printf(", netmask: %08x\n", - ((struct sockaddr_in *) &ifreqs[i].ifr_netmask)->sin_addr.s_addr); - return ((struct sockaddr_in *) &ifreqs[i].ifr_netmask)->sin_addr.s_addr; - } - printf("\n"); + sin = (struct sockaddr_in *) &ifreqs[i].ifr_addr; + + if (sin->sin_addr.s_addr == own_ip) + return &ifreqs[i]; } - /* Failure */ - return 0; - -#if 0 - /* FIXME: do not hardcode interface name */ - strcpy(ifreq.ifr_name, "wlp3s0"); + return NULL; +} - if (ioctl(fd, SIOCGIFNETMASK, &ifreq) < 0) { - fprintf(stderr, "get netmask failed: %s\n", strerror(errno)); - return 0; - } +uint32_t +sock_get_netmask(int fd) +{ + struct ifreq *ifreq; - return ((struct sockaddr_in *) &ifreq.ifr_netmask)->sin_addr.s_addr; -#endif + ifreq = sock_get_ifreq(fd); + if (ifreq == NULL) + return 0; + + if (ioctl(fd, SIOCGIFNETMASK, ifreq) < 0) + return 0; + + return ((struct sockaddr_in *) &ifreq->ifr_netmask)->sin_addr.s_addr; } uint32_t |