1100138Sume/* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */ 2100138Sume 3100138Sume/*- 4100138Sume * Copyright (c) 1997, 2000 5100138Sume * Berkeley Software Design, Inc. All rights reserved. 6100138Sume * 7100138Sume * Redistribution and use in source and binary forms, with or without 8100138Sume * modification, are permitted provided that the following conditions 9100138Sume * are met: 10100138Sume * 1. Redistributions of source code must retain the above copyright 11100138Sume * notice, this list of conditions and the following disclaimer. 12100138Sume * 13100138Sume * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 14100138Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15100138Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16100138Sume * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 17100138Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18100138Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19100138Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20100138Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21100138Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22100138Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23100138Sume * SUCH DAMAGE. 24100138Sume * 25100138Sume * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp 26100138Sume */ 27100138Sume 28100138Sume#include <sys/cdefs.h> 29100138Sume__FBSDID("$FreeBSD$"); 30100138Sume 31111010Snectar#include "namespace.h" 32100138Sume#include <sys/types.h> 33100138Sume#include <sys/socket.h> 34107378Sume#include <sys/sockio.h> 35100138Sume#include <net/if.h> 36100138Sume#include <net/if_dl.h> 37100138Sume#include <ifaddrs.h> 38100138Sume#include <stdlib.h> 39100138Sume#include <string.h> 40100138Sume#include <errno.h> 41111010Snectar#include <unistd.h> 42111010Snectar#include "un-namespace.h" 43100138Sume 44100138Sume/* 45100138Sume * From RFC 2553: 46100138Sume * 47100138Sume * 4.1 Name-to-Index 48100138Sume * 49100138Sume * 50100138Sume * The first function maps an interface name into its corresponding 51100138Sume * index. 52100138Sume * 53100138Sume * #include <net/if.h> 54100138Sume * 55100138Sume * unsigned int if_nametoindex(const char *ifname); 56100138Sume * 57100138Sume * If the specified interface name does not exist, the return value is 58100138Sume * 0, and errno is set to ENXIO. If there was a system error (such as 59100138Sume * running out of memory), the return value is 0 and errno is set to the 60100138Sume * proper value (e.g., ENOMEM). 61100138Sume */ 62100138Sume 63100138Sumeunsigned int 64100138Sumeif_nametoindex(const char *ifname) 65100138Sume{ 66107378Sume int s; 67107378Sume struct ifreq ifr; 68100138Sume struct ifaddrs *ifaddrs, *ifa; 69100138Sume unsigned int ni; 70100138Sume 71255328Sjilles s = _socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); 72107378Sume if (s != -1) { 73234329Seadler memset(&ifr, 0, sizeof(ifr)); 74114443Snectar strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 75107378Sume if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) { 76107378Sume _close(s); 77107378Sume return (ifr.ifr_index); 78107378Sume } 79107378Sume _close(s); 80107378Sume } 81107378Sume 82100138Sume if (getifaddrs(&ifaddrs) < 0) 83100138Sume return(0); 84100138Sume 85100138Sume ni = 0; 86100138Sume 87100138Sume for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { 88100138Sume if (ifa->ifa_addr && 89100138Sume ifa->ifa_addr->sa_family == AF_LINK && 90100138Sume strcmp(ifa->ifa_name, ifname) == 0) { 91235640Smarcel ni = LLINDEX((struct sockaddr_dl*)ifa->ifa_addr); 92100138Sume break; 93100138Sume } 94100138Sume } 95100138Sume 96100138Sume freeifaddrs(ifaddrs); 97100138Sume if (!ni) 98100138Sume errno = ENXIO; 99100138Sume return(ni); 100100138Sume} 101