Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/net - SIOCGIFINDEX was added in 2013, but if_freena...



details:   https://anonhg.NetBSD.org/src/rev/085db9ca7fe3
branches:  trunk
changeset: 834651:085db9ca7fe3
user:      msaitoh <msaitoh%NetBSD.org@localhost>
date:      Wed Aug 22 03:12:31 2018 +0000

description:
- 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 5b721cd68a2e -r 085db9ca7fe3 lib/libc/net/if_nametoindex.c
--- a/lib/libc/net/if_nametoindex.c     Wed Aug 22 01:05:21 2018 +0000
+++ b/lib/libc/net/if_nametoindex.c     Wed Aug 22 03:12:31 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.6 2018/08/22 03:12:31 msaitoh 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.6 2018/08/22 03:12:31 msaitoh 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