Mercurial > notdcc
comparison dcclib/getifaddrs.c @ 0:c7f6b056b673
First import of vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Tue, 10 Mar 2009 13:49:58 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7f6b056b673 |
---|---|
1 /* Distributed Checksum Clearinghouse | |
2 * | |
3 * kludge replacement for getifaddrs(3) for systems that lack it | |
4 * | |
5 * Copyright (c) 2008 by Rhyolite Software, LLC | |
6 * | |
7 * This agreement is not applicable to any entity which sells anti-spam | |
8 * solutions to others or provides an anti-spam solution as part of a | |
9 * security solution sold to other entities, or to a private network | |
10 * which employs the DCC or uses data provided by operation of the DCC | |
11 * but does not provide corresponding data to other users. | |
12 * | |
13 * Permission to use, copy, modify, and distribute this software without | |
14 * changes for any purpose with or without fee is hereby granted, provided | |
15 * that the above copyright notice and this permission notice appear in all | |
16 * copies and any distributed versions or copies are either unchanged | |
17 * or not called anything similar to "DCC" or "Distributed Checksum | |
18 * Clearinghouse". | |
19 * | |
20 * Parties not eligible to receive a license under this agreement can | |
21 * obtain a commercial license to use DCC by contacting Rhyolite Software | |
22 * at sales@rhyolite.com. | |
23 * | |
24 * A commercial license would be for Distributed Checksum and Reputation | |
25 * Clearinghouse software. That software includes additional features. This | |
26 * free license for Distributed ChecksumClearinghouse Software does not in any | |
27 * way grant permision to use Distributed Checksum and Reputation Clearinghouse | |
28 * software | |
29 * | |
30 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL | |
31 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
32 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC | |
33 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | |
34 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
35 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
36 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
37 * SOFTWARE. | |
38 * | |
39 * Rhyolite Software DCC 1.3.103-1.19 $Revision$ | |
40 */ | |
41 | |
42 | |
43 /* let the configure script decide if this might work */ | |
44 #ifndef TEST_DCC_GETIFADDRS | |
45 #include "dcc_config.h" | |
46 #endif | |
47 | |
48 #ifndef USE_DCC_GETIFADDRS | |
49 /* global to suppress "defined but not used" warning */ | |
50 char dcc_getifaddrs_unneeded[] = "dcc_getifaddrs() unneeded"; | |
51 | |
52 #else | |
53 | |
54 #include <sys/types.h> | |
55 #define BSD_COMP /* for SunOS */ | |
56 #define _IO_TERMIOS_H /* kludge for OpenUNIX */ | |
57 #include <sys/ioctl.h> | |
58 #include <sys/socket.h> | |
59 #include <net/if.h> | |
60 #include <unistd.h> | |
61 #include "dcc_defs.h" | |
62 #include "dcc_heap_debug.h" | |
63 #include "dcc_ifaddrs.h" | |
64 | |
65 #include <errno.h> | |
66 #include <stdlib.h> | |
67 #include <string.h> | |
68 | |
69 int | |
70 dcc_getifaddrs(struct ifaddrs **pif) | |
71 { | |
72 struct ifconf ifc; | |
73 int buf_size; | |
74 char *buf; | |
75 struct ifreq *ifrp, *ifrp_next, ifr_f; | |
76 struct ifaddrs_all *result0, *result, *rp; | |
77 int i, serrno, s; | |
78 | |
79 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
80 return -1; | |
81 | |
82 /* get the list of interface names and addresses */ | |
83 buf_size = 1024; | |
84 for (;;) { | |
85 buf = dcc_malloc(buf_size); | |
86 if (!buf) { | |
87 serrno = errno; | |
88 close(s); | |
89 errno = serrno; | |
90 return -1; | |
91 } | |
92 ifc.ifc_buf = buf; | |
93 ifc.ifc_len = buf_size; | |
94 memset(buf, 0, buf_size); | |
95 if (0 > ioctl(s, SIOCGIFCONF, (char *)&ifc) | |
96 && errno != EINVAL) { | |
97 serrno = errno; | |
98 close(s); | |
99 errno = serrno; | |
100 *pif = 0; | |
101 return -1; | |
102 } | |
103 if (ifc.ifc_len < buf_size && ifc.ifc_len != 0) | |
104 break; | |
105 dcc_free(buf); | |
106 buf_size *= 2; | |
107 } | |
108 | |
109 i = (ifc.ifc_len/sizeof(struct ifreq)) * sizeof(*result); | |
110 result = (struct ifaddrs_all *)dcc_malloc(i); | |
111 if (!result) { | |
112 serrno = errno; | |
113 dcc_free(buf); | |
114 close(s); | |
115 errno = serrno; | |
116 *pif = 0; | |
117 return -1; | |
118 } | |
119 memset(result, 0, i); | |
120 result0 = result; | |
121 *pif = &result->ifa; | |
122 | |
123 for (ifrp = ifc.ifc_req; | |
124 (void *)ifrp < (void *)&ifc.ifc_buf[ifc.ifc_len]; | |
125 ifrp = ifrp_next) { | |
126 #ifdef HAVE_SA_LEN | |
127 i = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); | |
128 if (i < (int)sizeof(*ifrp)) | |
129 ifrp_next = ifrp + 1; | |
130 else | |
131 ifrp_next = (struct ifreq *)((char *)ifrp + i); | |
132 #else | |
133 ifrp_next = ifrp + 1; | |
134 #endif | |
135 | |
136 if (ifrp->ifr_addr.sa_family != AF_INET | |
137 #ifdef AF_INET6 | |
138 && ifrp->ifr_addr.sa_family != AF_INET6 | |
139 #endif | |
140 ) | |
141 continue; | |
142 | |
143 strncpy(ifr_f.ifr_name, ifrp->ifr_name, sizeof(ifr_f.ifr_name)); | |
144 if (0 > ioctl(s, SIOCGIFFLAGS, (char *)&ifr_f)) { | |
145 if (errno == ENXIO) | |
146 continue; | |
147 serrno = errno; | |
148 close(s); | |
149 dcc_free(buf); | |
150 dcc_free(*pif); | |
151 *pif = 0; | |
152 errno = serrno; | |
153 return -1; | |
154 } | |
155 | |
156 /* save the name only for debugging */ | |
157 strncpy(result->name, ifrp->ifr_name, sizeof(result->name)); | |
158 result->ifa.ifa_name = result->name; | |
159 | |
160 result->addr.sa = ifrp->ifr_addr; | |
161 result->ifa.ifa_addr = &result->addr.sa; | |
162 result->ifa.ifa_flags = ifr_f.ifr_flags; | |
163 result->ifa.ifa_next = &(result+1)->ifa; | |
164 | |
165 /* skip duplicate addresses */ | |
166 rp = result0; | |
167 for (;;) { | |
168 if (rp >= result) { | |
169 ++result; | |
170 break; | |
171 } | |
172 if (DCC_SU_EQ(&rp->addr, &result->addr)) | |
173 break; | |
174 ++rp; | |
175 } | |
176 } | |
177 | |
178 if (&result->ifa == *pif) { | |
179 *pif = 0; | |
180 } else { | |
181 (result-1)->ifa.ifa_next = 0; | |
182 } | |
183 dcc_free(buf); | |
184 close(s); | |
185 | |
186 return 0; | |
187 } | |
188 #endif /* USE_DCC_GETIFADDRS */ |