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