diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-09 13:20:15 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-09 13:20:15 +0100 |
commit | e0895a9185e803ba0f62d32565160083b59ae1f8 (patch) | |
tree | c4b6d3dfd2d34b8f30174f7fd7ec1475752efc1e | |
parent | a64ae1a291f0aa59725cb4f87203100d52b25db8 (diff) | |
download | econ-e0895a9185e803ba0f62d32565160083b59ae1f8.tar.gz econ-e0895a9185e803ba0f62d32565160083b59ae1f8.tar.bz2 econ-e0895a9185e803ba0f62d32565160083b59ae1f8.zip |
util: Proper implement sock_get_netmask
By looping through all interfaces, search for the ip address we're bound
to and use that subnetmask.
-rw-r--r-- | util.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -31,10 +31,48 @@ sock_get_ipv4_addr(int fd) uint32_t sock_get_netmask(int fd) { +#if 0 struct ifreq ifreq; +#endif + struct ifconf ifconf; + struct ifreq ifreqs[16]; + 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; + } + + 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"); + } + /* Failure */ + return 0; + +#if 0 /* FIXME: do not hardcode interface name */ strcpy(ifreq.ifr_name, "wlp3s0"); @@ -44,6 +82,7 @@ sock_get_netmask(int fd) } return ((struct sockaddr_in *) &ifreq.ifr_netmask)->sin_addr.s_addr; +#endif } uint32_t |