Module Name:    src
Committed By:   martin
Date:           Wed Sep  5 08:45:52 UTC 2018

Modified Files:
        src/lib/libc/net [netbsd-8]: if_nametoindex.c

Log Message:
Pull up following revision(s) (requested by msaitoh in ticket #1007):

        lib/libc/net/if_nametoindex.c: revision 1.6

- SIOCGIFINDEX was added in 2013, but if_freenameindex(3) have not used it
   for years. Use it to improve performance. Same as FreeBSD.

- KNF.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.5.8.1 src/lib/libc/net/if_nametoindex.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/net/if_nametoindex.c
diff -u src/lib/libc/net/if_nametoindex.c:1.5 src/lib/libc/net/if_nametoindex.c:1.5.8.1
--- src/lib/libc/net/if_nametoindex.c:1.5	Tue Sep  1 09:54:34 2015
+++ src/lib/libc/net/if_nametoindex.c	Wed Sep  5 08:45:52 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_nametoindex.c,v 1.5 2015/09/01 09:54:34 ozaki-r Exp $	*/
+/*	$NetBSD: if_nametoindex.c,v 1.5.8.1 2018/09/05 08:45:52 martin Exp $	*/
 /*	$KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $	*/
 
 /*-
@@ -28,19 +28,21 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: if_nametoindex.c,v 1.5 2015/09/01 09:54:34 ozaki-r Exp $");
+__RCSID("$NetBSD: if_nametoindex.c,v 1.5.8.1 2018/09/05 08:45:52 martin Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #ifndef RUMP_ACTION
 #include "namespace.h"
 #endif
 #include <sys/types.h>
+#include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <net/if.h>
 #include <net/if_dl.h>
 #include <ifaddrs.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <errno.h>
 
 #ifndef RUMP_ACTION
@@ -71,11 +73,24 @@ __weak_alias(if_nametoindex,_if_nametoin
 unsigned int
 if_nametoindex(const char *ifname)
 {
+	int s;
+	struct ifreq ifr;
 	struct ifaddrs *ifaddrs, *ifa;
 	unsigned int ni;
 
+	s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+	if (s != -1) {
+		memset(&ifr, 0, sizeof(ifr));
+		strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+		if (ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
+			close(s);
+			return (ifr.ifr_index);
+		}
+		close(s);
+	}
+
 	if (getifaddrs(&ifaddrs) < 0)
-		return(0);
+		return 0;
 
 	ni = 0;
 
@@ -92,5 +107,5 @@ if_nametoindex(const char *ifname)
 	freeifaddrs(ifaddrs);
 	if (!ni)
 		errno = ENXIO;
-	return(ni);
+	return ni;
 }

Reply via email to