xref: /openbsd-src/lib/libc/net/if_nameindex.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*
2  * Copyright (c) 1998-1999, Craig Metz, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    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  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * 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
30  * SUCH DAMAGE.
31  */
32 
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <net/if.h>
38 #include <net/if_dl.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <string.h>
42 
43 struct if_nameindex *
44 if_nameindex(void)
45 {
46 	int     i, j, fd = -1, extra, len = 0;
47 	struct if_nameindex *nameindex = NULL;
48 	struct ifconf ifconf;
49 	char    lastname[IFNAMSIZ], *c, *inbuf;
50 	struct if_nameindex *n;
51 	struct sockaddr *sa;
52 	void   *p;
53 
54 	ifconf.ifc_buf = NULL;
55 
56 	if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
57 		goto ret;
58 
59 	/*
60 	 * Try ifc_len == 0 hack first, to get the actual length.
61 	 * If that fails, revert to a loop which grows the ifc_buf
62 	 * until it is sufficiently large.
63 	 */
64 	extra = sizeof(struct ifreq);
65 	while (1) {
66 		ifconf.ifc_len = len;
67 		if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf) == -1 &&
68 		    ifconf.ifc_buf)
69 			goto ret;
70 		if (ifconf.ifc_buf &&
71 		    ifconf.ifc_len + extra < len)
72 			break;
73 		if (ifconf.ifc_buf) {
74 			if (len == 0)
75 				len = 4096;
76 			ifconf.ifc_len = len *= 2;
77 		} else {
78 			len = ifconf.ifc_len;
79 			extra = 0;
80 		}
81 		inbuf = realloc(ifconf.ifc_buf, ifconf.ifc_len);
82 		if (inbuf == NULL)
83 			goto ret;
84 		ifconf.ifc_buf = inbuf;
85 	}
86 
87 	i = sizeof(struct if_nameindex);
88 	j = 0;
89 	p = ifconf.ifc_buf;
90 	len = ifconf.ifc_len;
91 	lastname[0] = 0;
92 	lastname[sizeof(lastname)-1] = 0;
93 
94 	while (len > 0) {
95 		if (len < (IFNAMSIZ + sizeof(struct sockaddr)))
96 			goto ret;
97 		if (strncmp(lastname, p, IFNAMSIZ)) {
98 			strlcpy(lastname, p, sizeof(lastname));
99 			i += sizeof(struct if_nameindex);
100 			j += strlen(lastname) + 1;
101 		}
102 		len -= IFNAMSIZ;
103 		p += IFNAMSIZ;
104 		sa = p;
105 
106 		if (len < sa->sa_len)
107 			goto ret;
108 		len -= sa->sa_len;
109 		p += sa->sa_len;
110 	}
111 
112 	nameindex = malloc(i + j);
113 	if (nameindex == NULL) {
114 		errno = ENOMEM;
115 		goto ret;
116 	}
117 	memset(nameindex, 0, i + j);
118 
119 	n = nameindex;
120 	p = ifconf.ifc_buf;
121 	c = (void *) nameindex + i;
122 	i = 0;
123 	len = ifconf.ifc_len;
124 	lastname[0] = 0;
125 
126 	while (len > 0) {
127 		if (len < IFNAMSIZ + sizeof(struct sockaddr))
128 			goto ret;
129 		if (strncmp(lastname, p, IFNAMSIZ)) {
130 			if (i) {
131 				if (!n->if_index)
132 					n->if_index = i;
133 				n++;
134 			}
135 			i++;
136 			memcpy(lastname, p, sizeof(lastname));
137 			strlcpy(c, lastname, sizeof(lastname));
138 			n->if_name = c;
139 			c += strlen(c) + 1;
140 		}
141 		len -= IFNAMSIZ;
142 		p += IFNAMSIZ;
143 		sa = p;
144 
145 		if (len < sa->sa_len)
146 			goto ret;
147 		if (sa->sa_family == AF_LINK) {
148 			struct sockaddr_dl *sd = (struct sockaddr_dl *)sa;
149 			n->if_index = sd->sdl_index;
150 		}
151 		len -= sa->sa_len;
152 		p += sa->sa_len;
153 	}
154 
155 	if (n->if_index == 0)
156 		n->if_index = i;
157 
158 ret:
159 	if (fd != -1)
160 		close(fd);
161 	if (ifconf.ifc_buf)
162 		free(ifconf.ifc_buf);
163 	return (nameindex);
164 }
165