diff options
author | claudio <> | 2015-10-23 13:09:19 +0000 |
---|---|---|
committer | claudio <> | 2015-10-23 13:09:19 +0000 |
commit | fbbeb39659d02bb968bbfbf1dd7200def1255ebd (patch) | |
tree | c1ea5409812cdf61c97e07d7308d28642748532d /src/lib/libc/net/if_nametoindex.c | |
parent | c82806a7e5b196fb801dc058799301df205737c7 (diff) | |
download | openbsd-fbbeb39659d02bb968bbfbf1dd7200def1255ebd.tar.gz openbsd-fbbeb39659d02bb968bbfbf1dd7200def1255ebd.tar.bz2 openbsd-fbbeb39659d02bb968bbfbf1dd7200def1255ebd.zip |
Switch if_nameindex(3) to use the new NET_RT_IFNAMES sysctl to get the
list of interface names. At the same time switch if_nametoindex(3) and
if_indextoname(3) to use if_nameindex(3) instead of getifaddrs(3).
if_nameindex(3) exposes much less then getifaddrs(3) and is allowed by
pledge(2).
With and OK deraadt@
Diffstat (limited to 'src/lib/libc/net/if_nametoindex.c')
-rw-r--r-- | src/lib/libc/net/if_nametoindex.c | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c index 8b2cbf743f..7a4492e37d 100644 --- a/src/lib/libc/net/if_nametoindex.c +++ b/src/lib/libc/net/if_nametoindex.c | |||
@@ -1,7 +1,8 @@ | |||
1 | /* $OpenBSD: if_nametoindex.c,v 1.9 2015/09/14 10:47:01 guenther Exp $ */ | 1 | /* $OpenBSD: if_nametoindex.c,v 1.10 2015/10/23 13:09:19 claudio Exp $ */ |
2 | /* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */ | 2 | /* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */ |
3 | 3 | ||
4 | /*- | 4 | /*- |
5 | * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org> | ||
5 | * Copyright (c) 1997, 2000 | 6 | * Copyright (c) 1997, 2000 |
6 | * Berkeley Software Design, Inc. All rights reserved. | 7 | * Berkeley Software Design, Inc. All rights reserved. |
7 | * | 8 | * |
@@ -29,8 +30,6 @@ | |||
29 | #include <sys/types.h> | 30 | #include <sys/types.h> |
30 | #include <sys/socket.h> | 31 | #include <sys/socket.h> |
31 | #include <net/if.h> | 32 | #include <net/if.h> |
32 | #include <net/if_dl.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | 33 | #include <stdlib.h> |
35 | #include <string.h> | 34 | #include <string.h> |
36 | #include <errno.h> | 35 | #include <errno.h> |
@@ -57,26 +56,22 @@ | |||
57 | unsigned int | 56 | unsigned int |
58 | if_nametoindex(const char *ifname) | 57 | if_nametoindex(const char *ifname) |
59 | { | 58 | { |
60 | struct ifaddrs *ifaddrs, *ifa; | 59 | struct if_nameindex *ifni, *ifni2; |
61 | unsigned int ni; | 60 | unsigned int index; |
62 | 61 | ||
63 | if (getifaddrs(&ifaddrs) < 0) | 62 | if ((ifni = if_nameindex()) == NULL) |
64 | return(0); | 63 | return(0); |
65 | 64 | ||
66 | ni = 0; | 65 | for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) { |
67 | 66 | if (strcmp(ifni2->if_name, ifname) == 0) { | |
68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | 67 | index = ifni2->if_index; |
69 | if (ifa->ifa_addr && | 68 | if_freenameindex(ifni); |
70 | ifa->ifa_addr->sa_family == AF_LINK && | 69 | return index; |
71 | strcmp(ifa->ifa_name, ifname) == 0) { | ||
72 | ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index; | ||
73 | break; | ||
74 | } | 70 | } |
75 | } | 71 | } |
76 | 72 | ||
77 | freeifaddrs(ifaddrs); | 73 | if_freenameindex(ifni); |
78 | if (!ni) | 74 | errno = ENXIO; |
79 | errno = ENXIO; | 75 | return 0; |
80 | return(ni); | ||
81 | } | 76 | } |
82 | DEF_WEAK(if_nametoindex); | 77 | DEF_WEAK(if_nametoindex); |