From 17f2d7c222cb2fe7b1268878950c73365e303468 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Sun, 25 May 2014 22:39:03 +0200 Subject: [PATCH] udp: use if_nametoindex instead of SIOCGIFINDEX ioctl if_nametoindex is more portable and shorter --- src/udp.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/udp.c b/src/udp.c index 6e11752e..1c75d3c7 100644 --- a/src/udp.c +++ b/src/udp.c @@ -95,21 +95,16 @@ udp_resolve( udp_connection_t *uc, int receiver ) } static int -udp_get_ifindex( int fd, const char *ifname ) +udp_get_ifindex( const char *ifname ) { - struct ifreq ifr; + unsigned int r; if (ifname == NULL || *ifname == '\0') return 0; - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, ifname, IFNAMSIZ); - ifr.ifr_name[IFNAMSIZ - 1] = '\0'; - if (ioctl(fd, SIOCGIFINDEX, &ifr)) + + r = if_nametoindex(ifname); + if (!r) return -1; -#if defined(PLATFORM_LINUX) - return ifr.ifr_ifindex; -#elif defined(PLATFORM_FREEBSD) - return ifr.ifr_index; -#endif + return r; } static int @@ -160,7 +155,7 @@ udp_bind ( const char *subsystem, const char *name, setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); /* Bind to interface */ - ifindex = uc->multicast ? udp_get_ifindex(fd, ifname) : 0; + ifindex = uc->multicast ? udp_get_ifindex(ifname) : 0; if (ifindex < 0) { tvherror(subsystem, "%s - could not find interface %s", name, ifname); @@ -308,7 +303,7 @@ udp_connect ( const char *subsystem, const char *name, } /* Bind to interface */ - ifindex = uc->multicast ? udp_get_ifindex(fd, ifname) : 0; + ifindex = uc->multicast ? udp_get_ifindex(ifname) : 0; if (ifindex < 0) { tvherror(subsystem, "%s - could not find interface %s", name, ifname);