diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-10 14:49:21 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-03-10 14:49:49 +0100 |
commit | 4860a84c768f1b85ec3b45e020a1d550b448ccae (patch) | |
tree | 992459912ad4cbbe91fecd65a7f341b4ac96445f | |
parent | 3abd448fd6d9171515a4951cc6a629b0ff83ddd4 (diff) | |
download | econ-4860a84c768f1b85ec3b45e020a1d550b448ccae.tar.gz econ-4860a84c768f1b85ec3b45e020a1d550b448ccae.tar.bz2 econ-4860a84c768f1b85ec3b45e020a1d550b448ccae.zip |
util: Add sock_get_hwaddr
And use it in econserv, instead of the hardcoded lookup for tap0.
-rw-r--r-- | econserv.c | 17 | ||||
-rw-r--r-- | util.c | 17 | ||||
-rw-r--r-- | util.h | 2 |
3 files changed, 22 insertions, 14 deletions
@@ -14,12 +14,9 @@ #include <sys/types.h> #include <sys/socket.h> -#include <sys/ioctl.h> #include <sys/select.h> #include <sys/uio.h> #include <unistd.h> -#include <net/if.h> -#include <net/if_arp.h> #include <netinet/in.h> #include "econproto.h" @@ -100,18 +97,10 @@ struct ecs { static void get_hwaddr(struct ecs *ecs) { - struct ifreq ifreq; - char *m = &ifreq.ifr_hwaddr.sa_data[0]; + uint8_t *m; - memset(&ifreq, 0, sizeof ifreq); - - strcpy(ifreq.ifr_name, "tap0"); - ioctl(ecs->fd, SIOCGIFHWADDR, &ifreq); - assert(ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER); -#if 0 - printf("hwaddr: %02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx\n", - m[0], m[1], m[2], m[3], m[4], m[5]); -#endif + m = sock_get_hwaddr(ecs->fd); + assert(m != NULL); memcpy(ecs->proj_uniq, m, 6); } @@ -72,6 +72,23 @@ sock_get_netmask(int fd) return ((struct sockaddr_in *) &ifreq->ifr_netmask)->sin_addr.s_addr; } +uint8_t * +sock_get_hwaddr(int fd) +{ + struct ifreq *ifreq; + + ifreq = sock_get_ifreq(fd); + if (ifreq == NULL) + return NULL; + + if (ioctl(fd, SIOCGIFHWADDR, ifreq) < 0) + return NULL; + if (ifreq->ifr_hwaddr.sa_family != ARPHRD_ETHER) + return NULL; + + return (uint8_t *) &ifreq->ifr_hwaddr.sa_data[0]; +} + uint32_t sock_get_peer_ipv4_addr(int fd) { @@ -15,6 +15,8 @@ uint32_t sock_get_peer_ipv4_addr(int fd); uint32_t sock_get_netmask(int fd); +uint8_t * +sock_get_hwaddr(int fd); void set_ip(uint8_t *ipbuf, uint32_t ip); |