Home | History | Annotate | Line # | Download | only in net
      1  1.6   msaitoh /*	$NetBSD: if_nametoindex.c,v 1.6 2018/08/22 03:12:31 msaitoh Exp $	*/
      2  1.4    itojun /*	$KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 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_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
     27  1.1    itojun  */
     28  1.2    itojun 
     29  1.2    itojun #include <sys/cdefs.h>
     30  1.2    itojun #if defined(LIBC_SCCS) && !defined(lint)
     31  1.6   msaitoh __RCSID("$NetBSD: if_nametoindex.c,v 1.6 2018/08/22 03:12:31 msaitoh Exp $");
     32  1.2    itojun #endif /* LIBC_SCCS and not lint */
     33  1.1    itojun 
     34  1.5     ozaki #ifndef RUMP_ACTION
     35  1.1    itojun #include "namespace.h"
     36  1.5     ozaki #endif
     37  1.1    itojun #include <sys/types.h>
     38  1.6   msaitoh #include <sys/ioctl.h>
     39  1.1    itojun #include <sys/socket.h>
     40  1.1    itojun #include <net/if.h>
     41  1.1    itojun #include <net/if_dl.h>
     42  1.1    itojun #include <ifaddrs.h>
     43  1.1    itojun #include <stdlib.h>
     44  1.1    itojun #include <string.h>
     45  1.6   msaitoh #include <unistd.h>
     46  1.4    itojun #include <errno.h>
     47  1.1    itojun 
     48  1.5     ozaki #ifndef RUMP_ACTION
     49  1.1    itojun #ifdef __weak_alias
     50  1.1    itojun __weak_alias(if_nametoindex,_if_nametoindex)
     51  1.1    itojun #endif
     52  1.5     ozaki #endif
     53  1.1    itojun 
     54  1.1    itojun /*
     55  1.4    itojun  * From RFC 2553:
     56  1.4    itojun  *
     57  1.4    itojun  * 4.1 Name-to-Index
     58  1.1    itojun  *
     59  1.1    itojun  *
     60  1.1    itojun  *    The first function maps an interface name into its corresponding
     61  1.1    itojun  *    index.
     62  1.1    itojun  *
     63  1.4    itojun  *       #include <net/if.h>
     64  1.1    itojun  *
     65  1.4    itojun  *       unsigned int  if_nametoindex(const char *ifname);
     66  1.1    itojun  *
     67  1.4    itojun  *    If the specified interface name does not exist, the return value is
     68  1.4    itojun  *    0, and errno is set to ENXIO.  If there was a system error (such as
     69  1.4    itojun  *    running out of memory), the return value is 0 and errno is set to the
     70  1.4    itojun  *    proper value (e.g., ENOMEM).
     71  1.1    itojun  */
     72  1.1    itojun 
     73  1.1    itojun unsigned int
     74  1.1    itojun if_nametoindex(const char *ifname)
     75  1.1    itojun {
     76  1.6   msaitoh 	int s;
     77  1.6   msaitoh 	struct ifreq ifr;
     78  1.1    itojun 	struct ifaddrs *ifaddrs, *ifa;
     79  1.1    itojun 	unsigned int ni;
     80  1.1    itojun 
     81  1.6   msaitoh 	s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
     82  1.6   msaitoh 	if (s != -1) {
     83  1.6   msaitoh 		memset(&ifr, 0, sizeof(ifr));
     84  1.6   msaitoh 		strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     85  1.6   msaitoh 		if (ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
     86  1.6   msaitoh 			close(s);
     87  1.6   msaitoh 			return (ifr.ifr_index);
     88  1.6   msaitoh 		}
     89  1.6   msaitoh 		close(s);
     90  1.6   msaitoh 	}
     91  1.6   msaitoh 
     92  1.1    itojun 	if (getifaddrs(&ifaddrs) < 0)
     93  1.6   msaitoh 		return 0;
     94  1.1    itojun 
     95  1.1    itojun 	ni = 0;
     96  1.1    itojun 
     97  1.1    itojun 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
     98  1.1    itojun 		if (ifa->ifa_addr &&
     99  1.1    itojun 		    ifa->ifa_addr->sa_family == AF_LINK &&
    100  1.1    itojun 		    strcmp(ifa->ifa_name, ifname) == 0) {
    101  1.3  christos 			ni = ((struct sockaddr_dl*)
    102  1.3  christos 			    (void *)ifa->ifa_addr)->sdl_index;
    103  1.1    itojun 			break;
    104  1.1    itojun 		}
    105  1.1    itojun 	}
    106  1.1    itojun 
    107  1.1    itojun 	freeifaddrs(ifaddrs);
    108  1.4    itojun 	if (!ni)
    109  1.4    itojun 		errno = ENXIO;
    110  1.6   msaitoh 	return ni;
    111  1.1    itojun }
    112