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