xref: /freebsd/lib/libc/net/if_indextoname.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
18071d8d7SHajimu UMEMOTO /*	$KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $	*/
28071d8d7SHajimu UMEMOTO 
38071d8d7SHajimu UMEMOTO /*-
49b10f59aSPedro F. Giffuni  * SPDX-License-Identifier: BSD-1-Clause
59b10f59aSPedro F. Giffuni  *
68071d8d7SHajimu UMEMOTO  * Copyright (c) 1997, 2000
78071d8d7SHajimu UMEMOTO  *	Berkeley Software Design, Inc.  All rights reserved.
88071d8d7SHajimu UMEMOTO  *
98071d8d7SHajimu UMEMOTO  * Redistribution and use in source and binary forms, with or without
108071d8d7SHajimu UMEMOTO  * modification, are permitted provided that the following conditions
118071d8d7SHajimu UMEMOTO  * are met:
128071d8d7SHajimu UMEMOTO  * 1. Redistributions of source code must retain the above copyright
138071d8d7SHajimu UMEMOTO  *    notice, this list of conditions and the following disclaimer.
148071d8d7SHajimu UMEMOTO  *
158071d8d7SHajimu UMEMOTO  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
168071d8d7SHajimu UMEMOTO  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178071d8d7SHajimu UMEMOTO  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
188071d8d7SHajimu UMEMOTO  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
198071d8d7SHajimu UMEMOTO  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208071d8d7SHajimu UMEMOTO  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
218071d8d7SHajimu UMEMOTO  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
228071d8d7SHajimu UMEMOTO  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
238071d8d7SHajimu UMEMOTO  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
248071d8d7SHajimu UMEMOTO  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
258071d8d7SHajimu UMEMOTO  * SUCH DAMAGE.
268071d8d7SHajimu UMEMOTO  *
278071d8d7SHajimu UMEMOTO  *	BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp
288071d8d7SHajimu UMEMOTO  */
298071d8d7SHajimu UMEMOTO 
308071d8d7SHajimu UMEMOTO #include <sys/types.h>
318071d8d7SHajimu UMEMOTO #include <sys/socket.h>
328071d8d7SHajimu UMEMOTO #include <net/if_dl.h>
338071d8d7SHajimu UMEMOTO #include <net/if.h>
348071d8d7SHajimu UMEMOTO #include <ifaddrs.h>
358071d8d7SHajimu UMEMOTO #include <stdlib.h>
368071d8d7SHajimu UMEMOTO #include <string.h>
378071d8d7SHajimu UMEMOTO #include <errno.h>
388071d8d7SHajimu UMEMOTO 
398071d8d7SHajimu UMEMOTO /*
40*fa556e83SGordon Bergling  * From RFC 2553:
418071d8d7SHajimu UMEMOTO  *
428071d8d7SHajimu UMEMOTO  * The second function maps an interface index into its corresponding
438071d8d7SHajimu UMEMOTO  * name.
448071d8d7SHajimu UMEMOTO  *
458071d8d7SHajimu UMEMOTO  *    #include <net/if.h>
468071d8d7SHajimu UMEMOTO  *
478071d8d7SHajimu UMEMOTO  *    char  *if_indextoname(unsigned int ifindex, char *ifname);
488071d8d7SHajimu UMEMOTO  *
498071d8d7SHajimu UMEMOTO  * The ifname argument must point to a buffer of at least IF_NAMESIZE
508071d8d7SHajimu UMEMOTO  * bytes into which the interface name corresponding to the specified
518071d8d7SHajimu UMEMOTO  * index is returned.  (IF_NAMESIZE is also defined in <net/if.h> and
528071d8d7SHajimu UMEMOTO  * its value includes a terminating null byte at the end of the
538071d8d7SHajimu UMEMOTO  * interface name.) This pointer is also the return value of the
548071d8d7SHajimu UMEMOTO  * function.  If there is no interface corresponding to the specified
558071d8d7SHajimu UMEMOTO  * index, NULL is returned, and errno is set to ENXIO, if there was a
568071d8d7SHajimu UMEMOTO  * system error (such as running out of memory), if_indextoname returns
578071d8d7SHajimu UMEMOTO  * NULL and errno would be set to the proper value (e.g., ENOMEM).
588071d8d7SHajimu UMEMOTO  */
598071d8d7SHajimu UMEMOTO 
608071d8d7SHajimu UMEMOTO char *
if_indextoname(unsigned int ifindex,char * ifname)618071d8d7SHajimu UMEMOTO if_indextoname(unsigned int ifindex, char *ifname)
628071d8d7SHajimu UMEMOTO {
638071d8d7SHajimu UMEMOTO 	struct ifaddrs *ifaddrs, *ifa;
648071d8d7SHajimu UMEMOTO 	int error = 0;
658071d8d7SHajimu UMEMOTO 
6636dcd97dSKristof Provost 	if (ifindex == 0) {
6736dcd97dSKristof Provost 		errno = ENXIO;
6836dcd97dSKristof Provost 		return(NULL);
6936dcd97dSKristof Provost 	}
7036dcd97dSKristof Provost 
718071d8d7SHajimu UMEMOTO 	if (getifaddrs(&ifaddrs) < 0)
728071d8d7SHajimu UMEMOTO 		return(NULL);	/* getifaddrs properly set errno */
738071d8d7SHajimu UMEMOTO 
748071d8d7SHajimu UMEMOTO 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
758071d8d7SHajimu UMEMOTO 		if (ifa->ifa_addr &&
768071d8d7SHajimu UMEMOTO 		    ifa->ifa_addr->sa_family == AF_LINK &&
77cf0d539fSMarcel Moolenaar 		    ifindex == LLINDEX((struct sockaddr_dl*)ifa->ifa_addr))
788071d8d7SHajimu UMEMOTO 			break;
798071d8d7SHajimu UMEMOTO 	}
808071d8d7SHajimu UMEMOTO 
818071d8d7SHajimu UMEMOTO 	if (ifa == NULL) {
828071d8d7SHajimu UMEMOTO 		error = ENXIO;
838071d8d7SHajimu UMEMOTO 		ifname = NULL;
848071d8d7SHajimu UMEMOTO 	}
858071d8d7SHajimu UMEMOTO 	else
868071d8d7SHajimu UMEMOTO 		strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
878071d8d7SHajimu UMEMOTO 
888071d8d7SHajimu UMEMOTO 	freeifaddrs(ifaddrs);
898071d8d7SHajimu UMEMOTO 
908071d8d7SHajimu UMEMOTO 	errno = error;
918071d8d7SHajimu UMEMOTO 	return(ifname);
928071d8d7SHajimu UMEMOTO }
93