summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/if_nametoindex.c
diff options
context:
space:
mode:
authormillert <>2002-03-07 22:40:23 +0000
committermillert <>2002-03-07 22:40:23 +0000
commitac55e95265194286ad7db771d95bd73b521e92b6 (patch)
treef13fe9b86c4ef03932d1e2d666076fea9acd6350 /src/lib/libc/net/if_nametoindex.c
parent409c42a31740ca9da36e066b8f180e668520958c (diff)
downloadopenbsd-ac55e95265194286ad7db771d95bd73b521e92b6.tar.gz
openbsd-ac55e95265194286ad7db771d95bd73b521e92b6.tar.bz2
openbsd-ac55e95265194286ad7db771d95bd73b521e92b6.zip
Replace SIOCGIFCONF-using NRL versions with KAME versions that use
getifaddrs(3). Fixes problems on LP64 platforms.
Diffstat (limited to 'src/lib/libc/net/if_nametoindex.c')
-rw-r--r--src/lib/libc/net/if_nametoindex.c146
1 files changed, 47 insertions, 99 deletions
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c
index e7e8b6c22a..8bd792b949 100644
--- a/src/lib/libc/net/if_nametoindex.c
+++ b/src/lib/libc/net/if_nametoindex.c
@@ -1,26 +1,20 @@
1/* 1/* $OpenBSD: if_nametoindex.c,v 1.8 2002/03/07 22:40:23 millert Exp $ */
2 * Copyright (c) 1998-1999, Craig Metz, All rights reserved. 2/* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */
3
4/*-
5 * Copyright (c) 1997, 2000
6 * Berkeley Software Design, Inc. All rights reserved.
3 * 7 *
4 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
6 * are met: 10 * are met:
7 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Craig Metz and
15 * by other contributors.
16 * 4. Neither the name of the author nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 * 13 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@@ -28,106 +22,60 @@
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE. 24 * SUCH DAMAGE.
25 *
26 * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
31 */ 27 */
32 28
33#include <sys/types.h> 29#include <sys/types.h>
34#include <stdlib.h>
35#include <sys/socket.h> 30#include <sys/socket.h>
36#include <sys/ioctl.h>
37#include <net/if.h> 31#include <net/if.h>
38#include <net/if_dl.h> 32#include <net/if_dl.h>
33#include <ifaddrs.h>
34#include <stdlib.h>
39#include <string.h> 35#include <string.h>
40#include <unistd.h>
41#include <errno.h> 36#include <errno.h>
42 37
43unsigned int 38/*
44if_nametoindex(const char *name) 39 * From RFC 2553:
40 *
41 * 4.1 Name-to-Index
42 *
43 *
44 * The first function maps an interface name into its corresponding
45 * index.
46 *
47 * #include <net/if.h>
48 *
49 * unsigned int if_nametoindex(const char *ifname);
50 *
51 * If the specified interface name does not exist, the return value is
52 * 0, and errno is set to ENXIO. If there was a system error (such as
53 * running out of memory), the return value is 0 and errno is set to the
54 * proper value (e.g., ENOMEM).
55 */
56
57unsigned int
58if_nametoindex(const char *ifname)
45{ 59{
46 int i, fd = -1, extra, len = 0; 60 struct ifaddrs *ifaddrs, *ifa;
47 struct ifconf ifconf; 61 unsigned int ni;
48 char lastname[IFNAMSIZ], *thisname, *inbuf;
49 unsigned int index = 0;
50 struct sockaddr *sa;
51 void *p;
52 62
53 ifconf.ifc_buf = 0; 63 if (getifaddrs(&ifaddrs) < 0)
64 return(0);
54 65
55 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 66 ni = 0;
56 goto ret;
57 67
58 /* 68 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
59 * Try ifc_len == 0 hack first, to get the actual length. 69 if (ifa->ifa_addr &&
60 * If that fails, revert to a loop which grows the ifc_buf 70 ifa->ifa_addr->sa_family == AF_LINK &&
61 * until it is sufficiently large. 71 strcmp(ifa->ifa_name, ifname) == 0) {
62 */ 72 ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
63 extra = sizeof(struct ifreq);
64 while (1) {
65 ifconf.ifc_len = len;
66 if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf) == -1 &&
67 ifconf.ifc_buf)
68 goto ret;
69 if (ifconf.ifc_buf &&
70 ifconf.ifc_len + extra < len)
71 break; 73 break;
72 if (ifconf.ifc_buf) {
73 if (len == 0)
74 len = 4096;
75 ifconf.ifc_len = len *= 2;
76 } else {
77 len = ifconf.ifc_len;
78 extra = 0;
79 }
80 inbuf = realloc(ifconf.ifc_buf, ifconf.ifc_len);
81 if (inbuf == NULL)
82 goto ret;
83 ifconf.ifc_buf = inbuf;
84 }
85
86 i = 0;
87 p = ifconf.ifc_buf;
88 len = ifconf.ifc_len;
89 lastname[0] = 0;
90 lastname[sizeof(lastname)-1] = 0;
91
92 while (len > 0) {
93 if (len < IFNAMSIZ + sizeof(struct sockaddr))
94 goto ret;
95
96 thisname = p;
97 if (strncmp(lastname, p, IFNAMSIZ)) {
98 if (!strcmp(lastname, name)) {
99 index = i;
100 goto ret;
101 }
102 memcpy(lastname, thisname, IFNAMSIZ);
103 i++;
104 } 74 }
105
106 len -= IFNAMSIZ;
107 p += IFNAMSIZ;
108 sa = (struct sockaddr *)p;
109
110 if (!strncmp(thisname, name, IFNAMSIZ) &&
111 sa->sa_family == AF_LINK) {
112 struct sockaddr_dl *sd = p;
113
114 index = sd->sdl_index;
115 goto ret;
116 }
117
118 if (len < sa->sa_len)
119 goto ret;
120 len -= sa->sa_len;
121 p += sa->sa_len;
122 } 75 }
123 76
124 if (!strcmp(lastname, name)) 77 freeifaddrs(ifaddrs);
125 index = i; 78 if (!ni)
126 79 errno = ENXIO;
127ret: 80 return(ni);
128 if (fd != -1)
129 close(fd);
130 if (ifconf.ifc_buf)
131 free(ifconf.ifc_buf);
132 return index;
133} 81}