Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-8]: src/lib/libc/net Pull up following revision(s) (requested by ...



details:   https://anonhg.NetBSD.org/src/rev/5c2d2619cbe3
branches:  netbsd-8
changeset: 851974:5c2d2619cbe3
user:      martin <martin%NetBSD.org@localhost>
date:      Wed Sep 05 08:45:52 2018 +0000

description:
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.

diffstat:

 lib/libc/net/if_nametoindex.c |  23 +++++++++++++++++++----
 1 files changed, 19 insertions(+), 4 deletions(-)

diffs (65 lines):

diff -r 78801aefa2db -r 5c2d2619cbe3 lib/libc/net/if_nametoindex.c
--- a/lib/libc/net/if_nametoindex.c     Wed Sep 05 08:42:22 2018 +0000
+++ b/lib/libc/net/if_nametoindex.c     Wed Sep 05 08:45:52 2018 +0000
@@ -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 @@
 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 @@
        freeifaddrs(ifaddrs);
        if (!ni)
                errno = ENXIO;
-       return(ni);
+       return ni;
 }



Home | Main Index | Thread Index | Old Index