diff options
Diffstat (limited to 'src/lib/libc/net/if_indextoname.c')
| -rw-r--r-- | src/lib/libc/net/if_indextoname.c | 37 |
1 files changed, 14 insertions, 23 deletions
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c index f1c65966df..affa42551e 100644 --- a/src/lib/libc/net/if_indextoname.c +++ b/src/lib/libc/net/if_indextoname.c | |||
| @@ -1,7 +1,8 @@ | |||
| 1 | /* $OpenBSD: if_indextoname.c,v 1.10 2015/09/14 10:47:01 guenther Exp $ */ | 1 | /* $OpenBSD: if_indextoname.c,v 1.11 2015/10/23 13:09:19 claudio Exp $ */ |
| 2 | /* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei Exp $ */ | 2 | /* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei 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 | * |
| @@ -28,9 +29,7 @@ | |||
| 28 | 29 | ||
| 29 | #include <sys/types.h> | 30 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> | 31 | #include <sys/socket.h> |
| 31 | #include <net/if_dl.h> | ||
| 32 | #include <net/if.h> | 32 | #include <net/if.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> |
| @@ -59,29 +58,21 @@ | |||
| 59 | char * | 58 | char * |
| 60 | if_indextoname(unsigned int ifindex, char *ifname) | 59 | if_indextoname(unsigned int ifindex, char *ifname) |
| 61 | { | 60 | { |
| 62 | struct ifaddrs *ifaddrs, *ifa; | 61 | struct if_nameindex *ifni, *ifni2; |
| 63 | int error = 0; | ||
| 64 | 62 | ||
| 65 | if (getifaddrs(&ifaddrs) < 0) | 63 | if ((ifni = if_nameindex()) == NULL) |
| 66 | return(NULL); /* getifaddrs properly set errno */ | 64 | return NULL; |
| 67 | 65 | ||
| 68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | 66 | for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) { |
| 69 | if (ifa->ifa_addr && | 67 | if (ifni2->if_index == ifindex) { |
| 70 | ifa->ifa_addr->sa_family == AF_LINK && | 68 | strlcpy(ifname, ifni2->if_name, IFNAMSIZ); |
| 71 | ifindex == ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index) | 69 | if_freenameindex(ifni); |
| 72 | break; | 70 | return ifname; |
| 71 | } | ||
| 73 | } | 72 | } |
| 74 | 73 | ||
| 75 | if (ifa == NULL) { | 74 | if_freenameindex(ifni); |
| 76 | error = ENXIO; | 75 | errno = ENXIO; |
| 77 | ifname = NULL; | 76 | return NULL; |
| 78 | } | ||
| 79 | else | ||
| 80 | strlcpy(ifname, ifa->ifa_name, IFNAMSIZ); | ||
| 81 | |||
| 82 | freeifaddrs(ifaddrs); | ||
| 83 | |||
| 84 | errno = error; | ||
| 85 | return(ifname); | ||
| 86 | } | 77 | } |
| 87 | DEF_WEAK(if_indextoname); | 78 | DEF_WEAK(if_indextoname); |
