xref: /netbsd/usr.sbin/traceroute/ifaddrlist.c (revision c4a72b64)
1 /*	$NetBSD: ifaddrlist.c,v 1.5 2002/07/06 21:51:30 wiz Exp $	*/
2 
3 /*
4  * Copyright (c) 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the Computer Systems
18  *	Engineering Group at Lawrence Berkeley Laboratory.
19  * 4. Neither the name of the University nor of the Laboratory may be used
20  *    to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static const char rcsid[] =
40     "@(#) Header: ifaddrlist.c,v 1.2 97/04/22 13:31:05 leres Exp  (LBL)";
41 #else
42 __RCSID("$NetBSD: ifaddrlist.c,v 1.5 2002/07/06 21:51:30 wiz Exp $");
43 #endif
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #ifdef HAVE_SYS_SOCKIO_H
51 #include <sys/sockio.h>
52 #endif
53 #include <sys/time.h>				/* concession to AIX */
54 
55 struct mbuf;
56 struct rtentry;
57 
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 
62 #include <ctype.h>
63 #include <errno.h>
64 #include <memory.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #ifdef HAVE_IFADDRS_H
70 #include <ifaddrs.h>
71 #endif
72 
73 #include "gnuc.h"
74 #ifdef HAVE_OS_PROTO_H
75 #include "os-proto.h"
76 #endif
77 
78 #include "ifaddrlist.h"
79 #include "savestr.h"
80 
81 
82 /* Not all systems have IFF_LOOPBACK */
83 #ifdef HAVE_IFADDRS_H
84 #ifdef IFF_LOOPBACK
85 #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
86 #else
87 #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
88 #endif
89 #else
90 #ifdef IFF_LOOPBACK
91 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
92 #else
93 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
94 #endif
95 #endif
96 
97 #define MAX_IPADDR 256
98 
99 /*
100  * Return the interface list
101  */
102 int
103 ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, int buflen)
104 {
105 #ifdef HAVE_IFADDRS_H
106 	int nipaddr;
107 	struct sockaddr_in *sin;
108 	struct ifaddrs *ifap, *ifa;
109 	struct ifaddrlist *al;
110 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
111 
112 	al = ifaddrlist;
113 	nipaddr = 0;
114 
115 	if (getifaddrs(&ifap) != 0) {
116 		(void)snprintf(errbuf, buflen, "getifaddrs: %s",
117 		    strerror(errno));
118 		return (-1);
119 	}
120 
121 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
122 		if (ifa->ifa_addr->sa_family != AF_INET)
123 			continue;
124 
125 		/* Must be up */
126 		if ((ifa->ifa_flags & IFF_UP) == 0)
127 			continue;
128 
129 		/*
130 		 * Must not be a loopback address (127/8)
131 		 */
132 		sin = (struct sockaddr_in *)ifa->ifa_addr;
133 		if (ISLOOPBACK(ifa))
134 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
135 				continue;
136 
137 		al->addr = sin->sin_addr.s_addr;
138 		al->device = savestr(ifa->ifa_name);
139 		++al;
140 		++nipaddr;
141 	}
142 	*ipaddrp = ifaddrlist;
143 	freeifaddrs(ifap);
144 	return (nipaddr);
145 #else
146 	int fd, nipaddr;
147 #ifdef HAVE_SOCKADDR_SA_LEN
148 	int n;
149 #endif
150 	struct ifreq *ifrp, *ifend, *ifnext, *mp;
151 	struct sockaddr_in *sin;
152 	struct ifaddrlist *al;
153 	struct ifconf ifc;
154 	struct ifreq ibuf[MAX_IPADDR], ifr;
155 	char device[sizeof(ifr.ifr_name) + 1];
156 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
157 
158 	fd = socket(AF_INET, SOCK_DGRAM, 0);
159 	if (fd < 0) {
160 		(void)snprintf(errbuf, buflen, "socket: %s", strerror(errno));
161 		return (-1);
162 	}
163 	ifc.ifc_len = sizeof(ibuf);
164 	ifc.ifc_buf = (caddr_t)ibuf;
165 
166 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
167 	    ifc.ifc_len < sizeof(struct ifreq)) {
168 		(void)snprintf(errbuf, buflen, "SIOCGIFCONF: %s", strerror(errno));
169 		(void)close(fd);
170 		return (-1);
171 	}
172 	ifrp = ibuf;
173 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
174 
175 	al = ifaddrlist;
176 	mp = NULL;
177 	nipaddr = 0;
178 	for (; ifrp < ifend; ifrp = ifnext) {
179 #ifdef HAVE_SOCKADDR_SA_LEN
180 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
181 		if (n < sizeof(*ifrp))
182 			ifnext = ifrp + 1;
183 		else
184 			ifnext = (struct ifreq *)((char *)ifrp + n);
185 		if (ifrp->ifr_addr.sa_family != AF_INET)
186 			continue;
187 #else
188 		ifnext = ifrp + 1;
189 #endif
190 		/*
191 		 * Need a template to preserve address info that is
192 		 * used below to locate the next entry.  (Otherwise,
193 		 * SIOCGIFFLAGS stomps over it because the requests
194 		 * are returned in a union.)
195 		 */
196 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
197 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
198 			if (errno == ENXIO)
199 				continue;
200 			(void)snprintf(errbuf, buflen, "SIOCGIFFLAGS: %.*s: %s",
201 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
202 			    strerror(errno));
203 			(void)close(fd);
204 			return (-1);
205 		}
206 
207 		/* Must be up */
208 		if ((ifr.ifr_flags & IFF_UP) == 0)
209 			continue;
210 
211 		/*
212 		 * Must not be a loopback address (127/8)
213 		 */
214 		sin = (struct sockaddr_in *)&ifrp->ifr_addr;
215 		if (ISLOOPBACK(&ifr))
216 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
217 				continue;
218 
219 		(void)strncpy(device, ifrp->ifr_name, sizeof(ifrp->ifr_name));
220 		device[sizeof(device) - 1] = '\0';
221 
222 		al->addr = sin->sin_addr.s_addr;
223 		al->device = savestr(device);
224 		++al;
225 		++nipaddr;
226 	}
227 	(void)close(fd);
228 
229 	*ipaddrp = ifaddrlist;
230 	return (nipaddr);
231 #endif
232 }
233