1 /* $KAME: ifname.c,v 1.4 2001/08/20 02:32:40 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 * - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02)
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
53 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
54 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
57 if_onametoindex(ifname)
60 struct if_nameindex *iff = if_nameindex(), *ifx;
63 if (iff == NULL) return 0;
65 while (ifx->if_name != NULL) {
66 if (strcmp(ifx->if_name, ifname) == 0) {
68 if_freenameindex(iff);
73 if_freenameindex(iff);
79 if_nametoindex(ifname)
85 s = socket(AF_INET, SOCK_DGRAM, 0);
88 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
89 if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
91 return (if_onametoindex(ifname));
94 return (ifr.ifr_index);
98 if_indextoname(ifindex, ifname)
100 char *ifname; /* at least IF_NAMESIZE */
102 struct if_nameindex *iff = if_nameindex(), *ifx;
105 if (iff == NULL) return NULL;
107 while (ifx->if_index != 0) {
108 if (ifx->if_index == ifindex) {
111 while ((*cp++ = *dp++)) ;
112 if_freenameindex(iff);
117 if_freenameindex(iff);
122 struct if_nameindex *
126 int mib[6], i, ifn = 0, off = 0, hlen;
127 char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL;
128 struct rt_msghdr *rtm;
129 struct if_msghdr *ifm;
131 struct sockaddr_dl *sdl;
132 struct if_nameindex *ret = NULL;
133 static int ifxs = 64; /* initial upper limit */
141 mib[2] = 0; /* protocol */
142 mib[3] = 0; /* wildcard address family */
143 mib[4] = NET_RT_IFLIST;
144 mib[5] = 0; /* no flags */
145 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
147 if ((buf = malloc(needed)) == NULL) {
151 /* XXX: we may have allocated too much than necessary */
152 if ((ifbuf = malloc(needed)) == NULL) {
156 if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) {
160 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
161 /* sysctl has set errno */
165 for (next = buf; next < lim; next += rtm->rtm_msglen) {
166 rtm = (struct rt_msghdr *)next;
167 if (rtm->rtm_version != RTM_VERSION) {
168 errno = EPROTONOSUPPORT;
171 switch (rtm->rtm_type) {
173 ifm = (struct if_msghdr *)rtm;
174 ifx[ifn].if_index = ifm->ifm_index;
175 ifx[ifn].if_off = off;
176 cp = (char *)(ifm + 1);
177 for (i = 1; i; i <<= 1) {
178 if (i & ifm->ifm_addrs) {
179 sa = (struct sockaddr *)cp;
181 sa->sa_family == AF_LINK) {
182 sdl = (struct sockaddr_dl *)sa;
186 off += sdl->sdl_nlen;
187 *(ifbuf + off) = '\0';
194 /* we need more memory */
198 if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) {
203 /* copy and free old data */
204 memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2);
210 hlen = sizeof(struct if_nameindex) * (ifn + 1);
211 if ((cp = (char *)malloc(hlen + off)) == NULL) {
215 bcopy(ifbuf, cp + hlen, off);
216 ret = (struct if_nameindex *)cp;
217 for (i = 0; i < ifn; i++) {
218 ret[i].if_index = ifx[i].if_index;
219 ret[i].if_name = cp + hlen + ifx[i].if_off;
221 ret[ifn].if_index = 0;
222 ret[ifn].if_name = NULL;
226 if (ifbuf) free(ifbuf);
232 void if_freenameindex(ptr)
233 struct if_nameindex *ptr;