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 $
38 RCSID("$Id: getaddrs.c,v 1.28.2.1 2000/06/23 03:29:53 assar Exp $");
40 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
41 #include <sys/ioctl.h>
49 #undef __P /* XXX hack for AIX 4.3 */
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
56 #endif /* HAVE_SYS_SOCKIO_H */
59 * Return number and list of all local adresses.
63 k_get_all_addrs (struct in_addr **l)
65 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFFLAGS) || !defined(SIOCGIFADDR)
66 char name[MaxHostNameLen];
69 if (gethostname(name, sizeof(name)) < 0)
71 he = gethostbyname (name);
74 *l = malloc(sizeof(**l));
77 memcpy (*l, he->h_addr_list[0], sizeof(*l));
90 fd = socket(AF_INET, SOCK_DGRAM, 0);
97 tmp = realloc (inbuf, in_len);
102 ifconf.ifc_len = in_len;
103 ifconf.ifc_buf = inbuf;
106 * Solaris returns EINVAL when the buffer is too small.
109 if(ioctl(fd, SIOCGIFCONF, &ifconf) < 0 && errno != EINVAL)
111 if(ifconf.ifc_len + sizeof(ifreq) < in_len)
115 num = ifconf.ifc_len / sizeof(struct ifreq);
116 *l = malloc(num * sizeof(struct in_addr));
121 ifreq.ifr_name[0] = '\0';
122 for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
123 struct ifreq *ifr = (struct ifreq *)p;
125 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
126 sz = max(sz, sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len);
129 if(strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
130 if(ioctl(fd, SIOCGIFFLAGS, ifr) < 0)
132 if (ifr->ifr_flags & IFF_UP) {
133 if(ioctl(fd, SIOCGIFADDR, ifr) < 0)
135 (*l)[j++] = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
137 memcpy(&ifreq, ifr, sizeof(ifreq));
142 tmp = realloc (*l, j * sizeof(struct in_addr));
155 #endif /* SIOCGIFCONF */