xref: /dflybsd-src/lib/libc/net/if_nametoindex.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 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_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
26*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/net/if_nametoindex.c,v 1.5 2003/05/01 19:03:14 nectar Exp $
27*86d7f5d3SJohn Marino  * $DragonFly: src/lib/libc/net/if_nametoindex.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
28*86d7f5d3SJohn Marino  */
29*86d7f5d3SJohn Marino 
30*86d7f5d3SJohn Marino #include "namespace.h"
31*86d7f5d3SJohn Marino #include <sys/types.h>
32*86d7f5d3SJohn Marino #include <sys/socket.h>
33*86d7f5d3SJohn Marino #include <sys/sockio.h>
34*86d7f5d3SJohn Marino #include <net/if.h>
35*86d7f5d3SJohn Marino #include <net/if_dl.h>
36*86d7f5d3SJohn Marino #include <ifaddrs.h>
37*86d7f5d3SJohn Marino #include <stdlib.h>
38*86d7f5d3SJohn Marino #include <string.h>
39*86d7f5d3SJohn Marino #include <errno.h>
40*86d7f5d3SJohn Marino #include <unistd.h>
41*86d7f5d3SJohn Marino #include "un-namespace.h"
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino /*
44*86d7f5d3SJohn Marino  * From RFC 2553:
45*86d7f5d3SJohn Marino  *
46*86d7f5d3SJohn Marino  * 4.1 Name-to-Index
47*86d7f5d3SJohn Marino  *
48*86d7f5d3SJohn Marino  *
49*86d7f5d3SJohn Marino  *    The first function maps an interface name into its corresponding
50*86d7f5d3SJohn Marino  *    index.
51*86d7f5d3SJohn Marino  *
52*86d7f5d3SJohn Marino  *       #include <net/if.h>
53*86d7f5d3SJohn Marino  *
54*86d7f5d3SJohn Marino  *       unsigned int  if_nametoindex(const char *ifname);
55*86d7f5d3SJohn Marino  *
56*86d7f5d3SJohn Marino  *    If the specified interface name does not exist, the return value is
57*86d7f5d3SJohn Marino  *    0, and errno is set to ENXIO.  If there was a system error (such as
58*86d7f5d3SJohn Marino  *    running out of memory), the return value is 0 and errno is set to the
59*86d7f5d3SJohn Marino  *    proper value (e.g., ENOMEM).
60*86d7f5d3SJohn Marino  */
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino unsigned int
if_nametoindex(const char * ifname)63*86d7f5d3SJohn Marino if_nametoindex(const char *ifname)
64*86d7f5d3SJohn Marino {
65*86d7f5d3SJohn Marino 	int s;
66*86d7f5d3SJohn Marino 	struct ifreq ifr;
67*86d7f5d3SJohn Marino 	struct ifaddrs *ifaddrs, *ifa;
68*86d7f5d3SJohn Marino 	unsigned int ni;
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino 	s = _socket(AF_INET, SOCK_DGRAM, 0);
71*86d7f5d3SJohn Marino 	if (s != -1) {
72*86d7f5d3SJohn Marino 		strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
73*86d7f5d3SJohn Marino 		if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
74*86d7f5d3SJohn Marino 			_close(s);
75*86d7f5d3SJohn Marino 			return (ifr.ifr_index);
76*86d7f5d3SJohn Marino 		}
77*86d7f5d3SJohn Marino 		_close(s);
78*86d7f5d3SJohn Marino 	}
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino 	if (getifaddrs(&ifaddrs) < 0)
81*86d7f5d3SJohn Marino 		return(0);
82*86d7f5d3SJohn Marino 
83*86d7f5d3SJohn Marino 	ni = 0;
84*86d7f5d3SJohn Marino 
85*86d7f5d3SJohn Marino 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
86*86d7f5d3SJohn Marino 		if (ifa->ifa_addr &&
87*86d7f5d3SJohn Marino 		    ifa->ifa_addr->sa_family == AF_LINK &&
88*86d7f5d3SJohn Marino 		    strcmp(ifa->ifa_name, ifname) == 0) {
89*86d7f5d3SJohn Marino 			ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
90*86d7f5d3SJohn Marino 			break;
91*86d7f5d3SJohn Marino 		}
92*86d7f5d3SJohn Marino 	}
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino 	freeifaddrs(ifaddrs);
95*86d7f5d3SJohn Marino 	if (!ni)
96*86d7f5d3SJohn Marino 		errno = ENXIO;
97*86d7f5d3SJohn Marino 	return(ni);
98*86d7f5d3SJohn Marino }
99