1*2fe8fb19SBen Gras /* $NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $ */
2*2fe8fb19SBen Gras /* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */
3*2fe8fb19SBen Gras
4*2fe8fb19SBen Gras /*-
5*2fe8fb19SBen Gras * Copyright (c) 1997, 2000
6*2fe8fb19SBen Gras * Berkeley Software Design, Inc. All rights reserved.
7*2fe8fb19SBen Gras *
8*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
9*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
10*2fe8fb19SBen Gras * are met:
11*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
12*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
13*2fe8fb19SBen Gras *
14*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
15*2fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*2fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*2fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
18*2fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*2fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*2fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*2fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*2fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*2fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*2fe8fb19SBen Gras * SUCH DAMAGE.
25*2fe8fb19SBen Gras *
26*2fe8fb19SBen Gras * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp
27*2fe8fb19SBen Gras */
28*2fe8fb19SBen Gras
29*2fe8fb19SBen Gras #include <sys/cdefs.h>
30*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*2fe8fb19SBen Gras __RCSID("$NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $");
32*2fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
33*2fe8fb19SBen Gras
34*2fe8fb19SBen Gras #ifndef RUMP_ACTION
35*2fe8fb19SBen Gras #include "namespace.h"
36*2fe8fb19SBen Gras #endif
37*2fe8fb19SBen Gras #include <sys/types.h>
38*2fe8fb19SBen Gras #include <sys/socket.h>
39*2fe8fb19SBen Gras #include <net/if_dl.h>
40*2fe8fb19SBen Gras #include <net/if.h>
41*2fe8fb19SBen Gras #include <ifaddrs.h>
42*2fe8fb19SBen Gras #include <stdlib.h>
43*2fe8fb19SBen Gras #include <string.h>
44*2fe8fb19SBen Gras #include <errno.h>
45*2fe8fb19SBen Gras
46*2fe8fb19SBen Gras #ifndef RUMP_ACTION
47*2fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(if_indextoname,_if_indextoname)48*2fe8fb19SBen Gras __weak_alias(if_indextoname,_if_indextoname)
49*2fe8fb19SBen Gras #endif
50*2fe8fb19SBen Gras #endif
51*2fe8fb19SBen Gras
52*2fe8fb19SBen Gras /*
53*2fe8fb19SBen Gras * From RFC 2533:
54*2fe8fb19SBen Gras *
55*2fe8fb19SBen Gras * The second function maps an interface index into its corresponding
56*2fe8fb19SBen Gras * name.
57*2fe8fb19SBen Gras *
58*2fe8fb19SBen Gras * #include <net/if.h>
59*2fe8fb19SBen Gras *
60*2fe8fb19SBen Gras * char *if_indextoname(unsigned int ifindex, char *ifname);
61*2fe8fb19SBen Gras *
62*2fe8fb19SBen Gras * The ifname argument must point to a buffer of at least IF_NAMESIZE
63*2fe8fb19SBen Gras * bytes into which the interface name corresponding to the specified
64*2fe8fb19SBen Gras * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and
65*2fe8fb19SBen Gras * its value includes a terminating null byte at the end of the
66*2fe8fb19SBen Gras * interface name.) This pointer is also the return value of the
67*2fe8fb19SBen Gras * function. If there is no interface corresponding to the specified
68*2fe8fb19SBen Gras * index, NULL is returned, and errno is set to ENXIO, if there was a
69*2fe8fb19SBen Gras * system error (such as running out of memory), if_indextoname returns
70*2fe8fb19SBen Gras * NULL and errno would be set to the proper value (e.g., ENOMEM).
71*2fe8fb19SBen Gras */
72*2fe8fb19SBen Gras
73*2fe8fb19SBen Gras char *
74*2fe8fb19SBen Gras if_indextoname(unsigned int ifindex, char *ifname)
75*2fe8fb19SBen Gras {
76*2fe8fb19SBen Gras struct ifaddrs *ifaddrs, *ifa;
77*2fe8fb19SBen Gras int error = 0;
78*2fe8fb19SBen Gras
79*2fe8fb19SBen Gras if (getifaddrs(&ifaddrs) < 0)
80*2fe8fb19SBen Gras return(NULL); /* getifaddrs properly set errno */
81*2fe8fb19SBen Gras
82*2fe8fb19SBen Gras for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
83*2fe8fb19SBen Gras if (ifa->ifa_addr &&
84*2fe8fb19SBen Gras ifa->ifa_addr->sa_family == AF_LINK &&
85*2fe8fb19SBen Gras ifindex == ((struct sockaddr_dl*)
86*2fe8fb19SBen Gras (void *)ifa->ifa_addr)->sdl_index)
87*2fe8fb19SBen Gras break;
88*2fe8fb19SBen Gras }
89*2fe8fb19SBen Gras
90*2fe8fb19SBen Gras if (ifa == NULL) {
91*2fe8fb19SBen Gras error = ENXIO;
92*2fe8fb19SBen Gras ifname = NULL;
93*2fe8fb19SBen Gras }
94*2fe8fb19SBen Gras else
95*2fe8fb19SBen Gras strlcpy(ifname, ifa->ifa_name, IFNAMSIZ);
96*2fe8fb19SBen Gras
97*2fe8fb19SBen Gras freeifaddrs(ifaddrs);
98*2fe8fb19SBen Gras
99*2fe8fb19SBen Gras errno = error;
100*2fe8fb19SBen Gras return(ifname);
101*2fe8fb19SBen Gras }
102