Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / getaddrs.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
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.
16  * 
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.
20  * 
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
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/crypto/kerberosIV/lib/krb/getaddrs.c,v 1.2.2.1 2001/03/04 12:52:49 markm Exp $
34  */
35
36 #include "krb_locl.h"
37
38 RCSID("$Id: getaddrs.c,v 1.28.2.1 2000/06/23 03:29:53 assar Exp $");
39
40 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
41 #include <sys/ioctl.h>
42 #endif
43 #ifdef HAVE_NET_IF_H
44 #ifdef __osf__
45 struct rtentry;
46 struct mbuf;
47 #endif
48 #ifdef _AIX
49 #undef __P /* XXX hack for AIX 4.3 */
50 #endif
51 #include <net/if.h>
52 #endif
53
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
56 #endif /* HAVE_SYS_SOCKIO_H */
57
58 /*
59  * Return number and list of all local adresses.
60  */
61
62 int
63 k_get_all_addrs (struct in_addr **l)
64 {
65 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFFLAGS) || !defined(SIOCGIFADDR)
66      char name[MaxHostNameLen];
67      struct hostent *he;
68
69      if (gethostname(name, sizeof(name)) < 0)
70           return -1;
71      he = gethostbyname (name);
72      if (he == NULL)
73           return -1;
74      *l = malloc(sizeof(**l));
75      if (*l == NULL)
76           return -1;
77      memcpy (*l, he->h_addr_list[0], sizeof(*l));
78      return 1;
79 #else
80      int fd;
81      char *inbuf = NULL;
82      size_t in_len = 8192;
83      struct ifreq ifreq;
84      struct ifconf ifconf;
85      int num, j;
86      char *p;
87      size_t sz;
88
89      *l = NULL;
90      fd = socket(AF_INET, SOCK_DGRAM, 0);
91      if (fd < 0)
92           return -1;
93
94      for(;;) {
95          void *tmp;
96
97          tmp = realloc (inbuf, in_len);
98          if (tmp == NULL)
99              goto fail;
100          inbuf = tmp;
101
102          ifconf.ifc_len = in_len;
103          ifconf.ifc_buf = inbuf;
104
105          /*
106           * Solaris returns EINVAL when the buffer is too small.
107           */
108
109          if(ioctl(fd, SIOCGIFCONF, &ifconf) < 0 && errno != EINVAL)
110              goto fail;
111          if(ifconf.ifc_len + sizeof(ifreq) < in_len)
112              break;
113          in_len *= 2;
114      }
115      num = ifconf.ifc_len / sizeof(struct ifreq);
116      *l = malloc(num * sizeof(struct in_addr));
117      if(*l == NULL)
118          goto fail;
119
120      j = 0;
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;
124           sz = sizeof(*ifr);
125 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
126           sz = max(sz, sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len);
127 #endif
128
129           if(strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
130               if(ioctl(fd, SIOCGIFFLAGS, ifr) < 0)
131                   continue;
132               if (ifr->ifr_flags & IFF_UP) {
133                   if(ioctl(fd, SIOCGIFADDR, ifr) < 0) 
134                       continue;
135                   (*l)[j++] = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
136                }
137               memcpy(&ifreq, ifr, sizeof(ifreq));
138           }
139      }
140      if (j != num) {
141          void *tmp;
142          tmp = realloc (*l, j * sizeof(struct in_addr));
143          if(tmp == NULL)
144              goto fail;
145          *l = tmp;
146      }
147      close (fd);
148      free(inbuf);
149      return j;
150 fail:
151      close(fd);
152      free(inbuf);
153      free(*l);
154      return -1;
155 #endif /* SIOCGIFCONF */
156 }