xref: /freebsd/lib/libc/net/if_nametoindex.c (revision 9b10f59a10b7adff0a5ea3b0fd182d521c3da643)
1 /*	$KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-1-Clause
5  *
6  * Copyright (c) 1997, 2000
7  *	Berkeley Software Design, Inc.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "namespace.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <net/if.h>
38 #include <net/if_dl.h>
39 #include <ifaddrs.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include "un-namespace.h"
45 
46 /*
47  * From RFC 2553:
48  *
49  * 4.1 Name-to-Index
50  *
51  *
52  *    The first function maps an interface name into its corresponding
53  *    index.
54  *
55  *       #include <net/if.h>
56  *
57  *       unsigned int  if_nametoindex(const char *ifname);
58  *
59  *    If the specified interface name does not exist, the return value is
60  *    0, and errno is set to ENXIO.  If there was a system error (such as
61  *    running out of memory), the return value is 0 and errno is set to the
62  *    proper value (e.g., ENOMEM).
63  */
64 
65 unsigned int
66 if_nametoindex(const char *ifname)
67 {
68 	int s;
69 	struct ifreq ifr;
70 	struct ifaddrs *ifaddrs, *ifa;
71 	unsigned int ni;
72 
73 	s = _socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
74 	if (s != -1) {
75 		memset(&ifr, 0, sizeof(ifr));
76 		strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
77 		if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
78 			_close(s);
79 			return (ifr.ifr_index);
80 		}
81 		_close(s);
82 	}
83 
84 	if (getifaddrs(&ifaddrs) < 0)
85 		return(0);
86 
87 	ni = 0;
88 
89 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
90 		if (ifa->ifa_addr &&
91 		    ifa->ifa_addr->sa_family == AF_LINK &&
92 		    strcmp(ifa->ifa_name, ifname) == 0) {
93 			ni = LLINDEX((struct sockaddr_dl*)ifa->ifa_addr);
94 			break;
95 		}
96 	}
97 
98 	freeifaddrs(ifaddrs);
99 	if (!ni)
100 		errno = ENXIO;
101 	return(ni);
102 }
103