2 * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * 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.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/crypto/kerberosIV/lib/krb/getaddrs.c,v 1.2.2.1 2001/03/04 12:52:49 markm Exp $
34 * $DragonFly: src/crypto/kerberosIV/lib/krb/Attic/getaddrs.c,v 1.2 2003/06/17 04:24:36 dillon Exp $
39 RCSID("$Id: getaddrs.c,v 1.28.2.1 2000/06/23 03:29:53 assar Exp $");
41 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
42 #include <sys/ioctl.h>
50 #undef __P /* XXX hack for AIX 4.3 */
55 #ifdef HAVE_SYS_SOCKIO_H
56 #include <sys/sockio.h>
57 #endif /* HAVE_SYS_SOCKIO_H */
60 * Return number and list of all local adresses.
64 k_get_all_addrs (struct in_addr **l)
66 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFFLAGS) || !defined(SIOCGIFADDR)
67 char name[MaxHostNameLen];
70 if (gethostname(name, sizeof(name)) < 0)
72 he = gethostbyname (name);
75 *l = malloc(sizeof(**l));
78 memcpy (*l, he->h_addr_list[0], sizeof(*l));
91 fd = socket(AF_INET, SOCK_DGRAM, 0);
98 tmp = realloc (inbuf, in_len);
103 ifconf.ifc_len = in_len;
104 ifconf.ifc_buf = inbuf;
107 * Solaris returns EINVAL when the buffer is too small.
110 if(ioctl(fd, SIOCGIFCONF, &ifconf) < 0 && errno != EINVAL)
112 if(ifconf.ifc_len + sizeof(ifreq) < in_len)
116 num = ifconf.ifc_len / sizeof(struct ifreq);
117 *l = malloc(num * sizeof(struct in_addr));
122 ifreq.ifr_name[0] = '\0';
123 for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
124 struct ifreq *ifr = (struct ifreq *)p;
126 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
127 sz = max(sz, sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len);
130 if(strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
131 if(ioctl(fd, SIOCGIFFLAGS, ifr) < 0)
133 if (ifr->ifr_flags & IFF_UP) {
134 if(ioctl(fd, SIOCGIFADDR, ifr) < 0)
136 (*l)[j++] = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
138 memcpy(&ifreq, ifr, sizeof(ifreq));
143 tmp = realloc (*l, j * sizeof(struct in_addr));
156 #endif /* SIOCGIFCONF */