1*0a6a1f1dSLionel Sambuc /* $NetBSD: if_nametoindex.c,v 1.5 2015/09/01 09:54:34 ozaki-r Exp $ */
22fe8fb19SBen Gras /* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */
32fe8fb19SBen Gras
42fe8fb19SBen Gras /*-
52fe8fb19SBen Gras * Copyright (c) 1997, 2000
62fe8fb19SBen Gras * Berkeley Software Design, Inc. All rights reserved.
72fe8fb19SBen Gras *
82fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras * are met:
112fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras *
142fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
152fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
182fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242fe8fb19SBen Gras * SUCH DAMAGE.
252fe8fb19SBen Gras *
262fe8fb19SBen Gras * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
272fe8fb19SBen Gras */
282fe8fb19SBen Gras
292fe8fb19SBen Gras #include <sys/cdefs.h>
302fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: if_nametoindex.c,v 1.5 2015/09/01 09:54:34 ozaki-r Exp $");
322fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
332fe8fb19SBen Gras
34*0a6a1f1dSLionel Sambuc #ifndef RUMP_ACTION
352fe8fb19SBen Gras #include "namespace.h"
36*0a6a1f1dSLionel Sambuc #endif
372fe8fb19SBen Gras #include <sys/types.h>
382fe8fb19SBen Gras #include <sys/socket.h>
392fe8fb19SBen Gras #include <net/if.h>
402fe8fb19SBen Gras #include <net/if_dl.h>
412fe8fb19SBen Gras #include <ifaddrs.h>
422fe8fb19SBen Gras #include <stdlib.h>
432fe8fb19SBen Gras #include <string.h>
442fe8fb19SBen Gras #include <errno.h>
452fe8fb19SBen Gras
46*0a6a1f1dSLionel Sambuc #ifndef RUMP_ACTION
472fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(if_nametoindex,_if_nametoindex)482fe8fb19SBen Gras __weak_alias(if_nametoindex,_if_nametoindex)
492fe8fb19SBen Gras #endif
50*0a6a1f1dSLionel Sambuc #endif
512fe8fb19SBen Gras
522fe8fb19SBen Gras /*
532fe8fb19SBen Gras * From RFC 2553:
542fe8fb19SBen Gras *
552fe8fb19SBen Gras * 4.1 Name-to-Index
562fe8fb19SBen Gras *
572fe8fb19SBen Gras *
582fe8fb19SBen Gras * The first function maps an interface name into its corresponding
592fe8fb19SBen Gras * index.
602fe8fb19SBen Gras *
612fe8fb19SBen Gras * #include <net/if.h>
622fe8fb19SBen Gras *
632fe8fb19SBen Gras * unsigned int if_nametoindex(const char *ifname);
642fe8fb19SBen Gras *
652fe8fb19SBen Gras * If the specified interface name does not exist, the return value is
662fe8fb19SBen Gras * 0, and errno is set to ENXIO. If there was a system error (such as
672fe8fb19SBen Gras * running out of memory), the return value is 0 and errno is set to the
682fe8fb19SBen Gras * proper value (e.g., ENOMEM).
692fe8fb19SBen Gras */
702fe8fb19SBen Gras
712fe8fb19SBen Gras unsigned int
722fe8fb19SBen Gras if_nametoindex(const char *ifname)
732fe8fb19SBen Gras {
742fe8fb19SBen Gras struct ifaddrs *ifaddrs, *ifa;
752fe8fb19SBen Gras unsigned int ni;
762fe8fb19SBen Gras
772fe8fb19SBen Gras if (getifaddrs(&ifaddrs) < 0)
782fe8fb19SBen Gras return(0);
792fe8fb19SBen Gras
802fe8fb19SBen Gras ni = 0;
812fe8fb19SBen Gras
822fe8fb19SBen Gras for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
832fe8fb19SBen Gras if (ifa->ifa_addr &&
842fe8fb19SBen Gras ifa->ifa_addr->sa_family == AF_LINK &&
852fe8fb19SBen Gras strcmp(ifa->ifa_name, ifname) == 0) {
862fe8fb19SBen Gras ni = ((struct sockaddr_dl*)
872fe8fb19SBen Gras (void *)ifa->ifa_addr)->sdl_index;
882fe8fb19SBen Gras break;
892fe8fb19SBen Gras }
902fe8fb19SBen Gras }
912fe8fb19SBen Gras
922fe8fb19SBen Gras freeifaddrs(ifaddrs);
932fe8fb19SBen Gras if (!ni)
942fe8fb19SBen Gras errno = ENXIO;
952fe8fb19SBen Gras return(ni);
962fe8fb19SBen Gras }
97