Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $DragonFly: src/crypto/kerberosIV/lib/krb/Attic/getaddrs.c,v 1.2 2003/06/17 04:24:36 dillon Exp $
35  */
36
37 #include "krb_locl.h"
38
39 RCSID("$Id: getaddrs.c,v 1.28.2.1 2000/06/23 03:29:53 assar Exp $");
40
41 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
42 #include <sys/ioctl.h>
43 #endif
44 #ifdef HAVE_NET_IF_H
45 #ifdef __osf__
46 struct rtentry;
47 struct mbuf;
48 #endif
49 #ifdef _AIX
50 #undef __P /* XXX hack for AIX 4.3 */
51 #endif
52 #include <net/if.h>
53 #endif
54
55 #ifdef HAVE_SYS_SOCKIO_H
56 #include <sys/sockio.h>
57 #endif /* HAVE_SYS_SOCKIO_H */
58
59 /*
60  * Return number and list of all local adresses.
61  */
62
63 int
64 k_get_all_addrs (struct in_addr **l)
65 {
66 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFFLAGS) || !defined(SIOCGIFADDR)
67      char name[MaxHostNameLen];
68      struct hostent *he;
69
70      if (gethostname(name, sizeof(name)) < 0)
71           return -1;
72      he = gethostbyname (name);
73      if (he == NULL)
74           return -1;
75      *l = malloc(sizeof(**l));
76      if (*l == NULL)
77           return -1;
78      memcpy (*l, he->h_addr_list[0], sizeof(*l));
79      return 1;
80 #else
81      int fd;
82      char *inbuf = NULL;
83      size_t in_len = 8192;
84      struct ifreq ifreq;
85      struct ifconf ifconf;
86      int num, j;
87      char *p;
88      size_t sz;
89
90      *l = NULL;
91      fd = socket(AF_INET, SOCK_DGRAM, 0);
92      if (fd < 0)
93           return -1;
94
95      for(;;) {
96          void *tmp;
97
98          tmp = realloc (inbuf, in_len);
99          if (tmp == NULL)
100              goto fail;
101          inbuf = tmp;
102
103          ifconf.ifc_len = in_len;
104          ifconf.ifc_buf = inbuf;
105
106          /*
107           * Solaris returns EINVAL when the buffer is too small.
108           */
109
110          if(ioctl(fd, SIOCGIFCONF, &ifconf) < 0 && errno != EINVAL)
111              goto fail;
112          if(ifconf.ifc_len + sizeof(ifreq) < in_len)
113              break;
114          in_len *= 2;
115      }
116      num = ifconf.ifc_len / sizeof(struct ifreq);
117      *l = malloc(num * sizeof(struct in_addr));
118      if(*l == NULL)
119          goto fail;
120
121      j = 0;
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;
125           sz = sizeof(*ifr);
126 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
127           sz = max(sz, sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len);
128 #endif
129
130           if(strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
131               if(ioctl(fd, SIOCGIFFLAGS, ifr) < 0)
132                   continue;
133               if (ifr->ifr_flags & IFF_UP) {
134                   if(ioctl(fd, SIOCGIFADDR, ifr) < 0) 
135                       continue;
136                   (*l)[j++] = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
137                }
138               memcpy(&ifreq, ifr, sizeof(ifreq));
139           }
140      }
141      if (j != num) {
142          void *tmp;
143          tmp = realloc (*l, j * sizeof(struct in_addr));
144          if(tmp == NULL)
145              goto fail;
146          *l = tmp;
147      }
148      close (fd);
149      free(inbuf);
150      return j;
151 fail:
152      close(fd);
153      free(inbuf);
154      free(*l);
155      return -1;
156 #endif /* SIOCGIFCONF */
157 }