aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tun.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-08-11 12:31:59 +0200
committerHarald Welte <laforge@gnumonks.org>2017-08-11 13:12:09 +0200
commitab6d189f8f2fa7531d22636e1ac8c149c801d2e3 (patch)
tree1a3fbdee8b2966dddd6f2620ef46ea6fe4b8b737 /lib/tun.c
parent2e48a44952fdc68eb3e5d3680f944507a4a129c1 (diff)
replace hand-coded tun_gifindex() with standard POSIX if_nametoindex()
Diffstat (limited to 'lib/tun.c')
-rw-r--r--lib/tun.c28
1 files changed, 3 insertions, 25 deletions
diff --git a/lib/tun.c b/lib/tun.c
index 11ea7bc..f6869ef 100644
--- a/lib/tun.c
+++ b/lib/tun.c
@@ -87,30 +87,6 @@ int tun_nlattr(struct nlmsghdr *n, int nsize, int type, void *d, int dlen)
n->nlmsg_len = alen + len;
return 0;
}
-
-int tun_gifindex(struct tun_t *this, __u32 * index)
-{
- struct ifreq ifr;
- int fd;
-
- memset(&ifr, '\0', sizeof(ifr));
- ifr.ifr_addr.sa_family = AF_INET;
- ifr.ifr_dstaddr.sa_family = AF_INET;
- ifr.ifr_netmask.sa_family = AF_INET;
- strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
- ifr.ifr_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
- }
- if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
- SYS_ERR(DTUN, LOGL_ERROR, errno, "ioctl() failed");
- close(fd);
- return -1;
- }
- close(fd);
- *index = ifr.ifr_ifindex;
- return 0;
-}
#endif
int tun_sifflags(struct tun_t *this, int flags)
@@ -264,7 +240,9 @@ int tun_addaddr(struct tun_t *this,
req.i.ifa_prefixlen = 32; /* 32 FOR IPv4 */
req.i.ifa_flags = 0;
req.i.ifa_scope = RT_SCOPE_HOST; /* TODO or 0 */
- if (tun_gifindex(this, &req.i.ifa_index)) {
+ req.i.ifa_index = if_nametoindex(this->devname);
+ if (!req.i.ifa_index) {
+ SYS_ERR(DTUN, LOGL_ERROR, errno, "Unable to get ifindex for %s", this->devname);
return -1;
}