18071d8d7SHajimu UMEMOTO /* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */
28071d8d7SHajimu UMEMOTO
38071d8d7SHajimu UMEMOTO /*-
4*9b10f59aSPedro F. Giffuni * SPDX-License-Identifier: BSD-1-Clause
5*9b10f59aSPedro 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_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
288071d8d7SHajimu UMEMOTO */
298071d8d7SHajimu UMEMOTO
30e0554a53SJacques Vidrine #include "namespace.h"
318071d8d7SHajimu UMEMOTO #include <sys/types.h>
328071d8d7SHajimu UMEMOTO #include <sys/socket.h>
33c86d6b6cSHajimu UMEMOTO #include <sys/sockio.h>
348071d8d7SHajimu UMEMOTO #include <net/if.h>
358071d8d7SHajimu UMEMOTO #include <net/if_dl.h>
368071d8d7SHajimu UMEMOTO #include <ifaddrs.h>
378071d8d7SHajimu UMEMOTO #include <stdlib.h>
388071d8d7SHajimu UMEMOTO #include <string.h>
398071d8d7SHajimu UMEMOTO #include <errno.h>
40e0554a53SJacques Vidrine #include <unistd.h>
41e0554a53SJacques Vidrine #include "un-namespace.h"
428071d8d7SHajimu UMEMOTO
438071d8d7SHajimu UMEMOTO /*
448071d8d7SHajimu UMEMOTO * From RFC 2553:
458071d8d7SHajimu UMEMOTO *
468071d8d7SHajimu UMEMOTO * 4.1 Name-to-Index
478071d8d7SHajimu UMEMOTO *
488071d8d7SHajimu UMEMOTO *
498071d8d7SHajimu UMEMOTO * The first function maps an interface name into its corresponding
508071d8d7SHajimu UMEMOTO * index.
518071d8d7SHajimu UMEMOTO *
528071d8d7SHajimu UMEMOTO * #include <net/if.h>
538071d8d7SHajimu UMEMOTO *
548071d8d7SHajimu UMEMOTO * unsigned int if_nametoindex(const char *ifname);
558071d8d7SHajimu UMEMOTO *
568071d8d7SHajimu UMEMOTO * If the specified interface name does not exist, the return value is
578071d8d7SHajimu UMEMOTO * 0, and errno is set to ENXIO. If there was a system error (such as
588071d8d7SHajimu UMEMOTO * running out of memory), the return value is 0 and errno is set to the
598071d8d7SHajimu UMEMOTO * proper value (e.g., ENOMEM).
608071d8d7SHajimu UMEMOTO */
618071d8d7SHajimu UMEMOTO
628071d8d7SHajimu UMEMOTO unsigned int
if_nametoindex(const char * ifname)638071d8d7SHajimu UMEMOTO if_nametoindex(const char *ifname)
648071d8d7SHajimu UMEMOTO {
65c86d6b6cSHajimu UMEMOTO int s;
66c86d6b6cSHajimu UMEMOTO struct ifreq ifr;
678071d8d7SHajimu UMEMOTO struct ifaddrs *ifaddrs, *ifa;
688071d8d7SHajimu UMEMOTO unsigned int ni;
698071d8d7SHajimu UMEMOTO
7072531978SJilles Tjoelker s = _socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
71c86d6b6cSHajimu UMEMOTO if (s != -1) {
72e44d947dSEitan Adler memset(&ifr, 0, sizeof(ifr));
73d0509082SJacques Vidrine strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
74c86d6b6cSHajimu UMEMOTO if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
75c86d6b6cSHajimu UMEMOTO _close(s);
76c86d6b6cSHajimu UMEMOTO return (ifr.ifr_index);
77c86d6b6cSHajimu UMEMOTO }
78c86d6b6cSHajimu UMEMOTO _close(s);
79c86d6b6cSHajimu UMEMOTO }
80c86d6b6cSHajimu UMEMOTO
818071d8d7SHajimu UMEMOTO if (getifaddrs(&ifaddrs) < 0)
828071d8d7SHajimu UMEMOTO return(0);
838071d8d7SHajimu UMEMOTO
848071d8d7SHajimu UMEMOTO ni = 0;
858071d8d7SHajimu UMEMOTO
868071d8d7SHajimu UMEMOTO for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
878071d8d7SHajimu UMEMOTO if (ifa->ifa_addr &&
888071d8d7SHajimu UMEMOTO ifa->ifa_addr->sa_family == AF_LINK &&
898071d8d7SHajimu UMEMOTO strcmp(ifa->ifa_name, ifname) == 0) {
90cf0d539fSMarcel Moolenaar ni = LLINDEX((struct sockaddr_dl*)ifa->ifa_addr);
918071d8d7SHajimu UMEMOTO break;
928071d8d7SHajimu UMEMOTO }
938071d8d7SHajimu UMEMOTO }
948071d8d7SHajimu UMEMOTO
958071d8d7SHajimu UMEMOTO freeifaddrs(ifaddrs);
968071d8d7SHajimu UMEMOTO if (!ni)
978071d8d7SHajimu UMEMOTO errno = ENXIO;
988071d8d7SHajimu UMEMOTO return(ni);
998071d8d7SHajimu UMEMOTO }
100