if_nametoindex.c revision 235640
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: head/lib/libc/net/if_nametoindex.c 235640 2012-05-19 02:39:43Z marcel $");
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
71107378Sume	s = _socket(AF_INET, SOCK_DGRAM, 0);
72107378Sume	if (s != -1) {
73234329Seadler#ifdef PURIFY
74234329Seadler		memset(&ifr, 0, sizeof(ifr));
75234329Seadler#endif
76114443Snectar		strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
77107378Sume		if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
78107378Sume			_close(s);
79107378Sume			return (ifr.ifr_index);
80107378Sume		}
81107378Sume		_close(s);
82107378Sume	}
83107378Sume
84100138Sume	if (getifaddrs(&ifaddrs) < 0)
85100138Sume		return(0);
86100138Sume
87100138Sume	ni = 0;
88100138Sume
89100138Sume	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
90100138Sume		if (ifa->ifa_addr &&
91100138Sume		    ifa->ifa_addr->sa_family == AF_LINK &&
92100138Sume		    strcmp(ifa->ifa_name, ifname) == 0) {
93235640Smarcel			ni = LLINDEX((struct sockaddr_dl*)ifa->ifa_addr);
94100138Sume			break;
95100138Sume		}
96100138Sume	}
97100138Sume
98100138Sume	freeifaddrs(ifaddrs);
99100138Sume	if (!ni)
100100138Sume		errno = ENXIO;
101100138Sume	return(ni);
102100138Sume}
103