source: vendor/3.6.23/source3/lib/interfaces.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 7.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 return a list of network interfaces
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Jeremy Allison 2007
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "interfaces.h"
23
24/****************************************************************************
25 Create a struct sockaddr_storage with the netmask bits set to 1.
26****************************************************************************/
27
28bool make_netmask(struct sockaddr_storage *pss_out,
29 const struct sockaddr_storage *pss_in,
30 unsigned long masklen)
31{
32 *pss_out = *pss_in;
33 /* Now apply masklen bits of mask. */
34#if defined(HAVE_IPV6)
35 if (pss_in->ss_family == AF_INET6) {
36 char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
37 unsigned int i;
38
39 if (masklen > 128) {
40 return false;
41 }
42 for (i = 0; masklen >= 8; masklen -= 8, i++) {
43 *p++ = 0xff;
44 }
45 /* Deal with the partial byte. */
46 *p++ &= (0xff & ~(0xff>>masklen));
47 i++;
48 for (;i < sizeof(struct in6_addr); i++) {
49 *p++ = '\0';
50 }
51 return true;
52 }
53#endif
54 if (pss_in->ss_family == AF_INET) {
55 if (masklen > 32) {
56 return false;
57 }
58 ((struct sockaddr_in *)pss_out)->sin_addr.s_addr =
59 htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL));
60 return true;
61 }
62 return false;
63}
64
65/****************************************************************************
66 Create a struct sockaddr_storage set to the broadcast or network adress from
67 an incoming sockaddr_storage.
68****************************************************************************/
69
70static void make_bcast_or_net(struct sockaddr_storage *pss_out,
71 const struct sockaddr_storage *pss_in,
72 const struct sockaddr_storage *nmask,
73 bool make_bcast_p)
74{
75 unsigned int i = 0, len = 0;
76 char *pmask = NULL;
77 char *p = NULL;
78 *pss_out = *pss_in;
79
80 /* Set all zero netmask bits to 1. */
81#if defined(HAVE_IPV6)
82 if (pss_in->ss_family == AF_INET6) {
83 p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
84 pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr;
85 len = 16;
86 }
87#endif
88 if (pss_in->ss_family == AF_INET) {
89 p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr;
90 pmask = (char *)&((struct sockaddr_in *)nmask)->sin_addr;
91 len = 4;
92 }
93
94 for (i = 0; i < len; i++, p++, pmask++) {
95 if (make_bcast_p) {
96 *p = (*p & *pmask) | (*pmask ^ 0xff);
97 } else {
98 /* make_net */
99 *p = (*p & *pmask);
100 }
101 }
102}
103
104void make_bcast(struct sockaddr_storage *pss_out,
105 const struct sockaddr_storage *pss_in,
106 const struct sockaddr_storage *nmask)
107{
108 make_bcast_or_net(pss_out, pss_in, nmask, true);
109}
110
111void make_net(struct sockaddr_storage *pss_out,
112 const struct sockaddr_storage *pss_in,
113 const struct sockaddr_storage *nmask)
114{
115 make_bcast_or_net(pss_out, pss_in, nmask, false);
116}
117
118/****************************************************************************
119 Try the "standard" getifaddrs/freeifaddrs interfaces.
120 Also gets IPv6 interfaces.
121****************************************************************************/
122
123/****************************************************************************
124 Get the netmask address for a local interface.
125****************************************************************************/
126
127static int _get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
128{
129 struct iface_struct *ifaces;
130 struct ifaddrs *iflist = NULL;
131 struct ifaddrs *ifptr = NULL;
132 int count;
133 int total = 0;
134 size_t copy_size;
135
136 if (getifaddrs(&iflist) < 0) {
137 return -1;
138 }
139
140 count = 0;
141 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
142 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
143 continue;
144 }
145 if (!(ifptr->ifa_flags & IFF_UP)) {
146 continue;
147 }
148 count += 1;
149 }
150
151 ifaces = talloc_array(mem_ctx, struct iface_struct, count);
152 if (ifaces == NULL) {
153 errno = ENOMEM;
154 return -1;
155 }
156
157 /* Loop through interfaces, looking for given IP address */
158 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
159
160 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
161 continue;
162 }
163
164 /* Check the interface is up. */
165 if (!(ifptr->ifa_flags & IFF_UP)) {
166 continue;
167 }
168
169 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
170
171 copy_size = sizeof(struct sockaddr_in);
172
173 ifaces[total].flags = ifptr->ifa_flags;
174
175#if defined(HAVE_IPV6)
176 if (ifptr->ifa_addr->sa_family == AF_INET6) {
177 copy_size = sizeof(struct sockaddr_in6);
178 }
179#endif
180
181 memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size);
182 memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size);
183
184 if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) {
185 make_bcast(&ifaces[total].bcast,
186 &ifaces[total].ip,
187 &ifaces[total].netmask);
188 } else if ((ifaces[total].flags & IFF_POINTOPOINT) &&
189 ifptr->ifa_dstaddr ) {
190 memcpy(&ifaces[total].bcast,
191 ifptr->ifa_dstaddr,
192 copy_size);
193 } else {
194 continue;
195 }
196
197 strlcpy(ifaces[total].name, ifptr->ifa_name,
198 sizeof(ifaces[total].name));
199 total++;
200 }
201
202 freeifaddrs(iflist);
203
204 *pifaces = ifaces;
205 return total;
206}
207
208static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
209{
210 int r;
211
212#if defined(HAVE_IPV6)
213 /*
214 * If we have IPv6 - sort these interfaces lower
215 * than any IPv4 ones.
216 */
217 if (i1->ip.ss_family == AF_INET6 &&
218 i2->ip.ss_family == AF_INET) {
219 return -1;
220 } else if (i1->ip.ss_family == AF_INET &&
221 i2->ip.ss_family == AF_INET6) {
222 return 1;
223 }
224
225 if (i1->ip.ss_family == AF_INET6) {
226 struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip;
227 struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip;
228
229 r = memcmp(&s1->sin6_addr,
230 &s2->sin6_addr,
231 sizeof(struct in6_addr));
232 if (r) {
233 return r;
234 }
235
236 s1 = (struct sockaddr_in6 *)&i1->netmask;
237 s2 = (struct sockaddr_in6 *)&i2->netmask;
238
239 r = memcmp(&s1->sin6_addr,
240 &s2->sin6_addr,
241 sizeof(struct in6_addr));
242 if (r) {
243 return r;
244 }
245 }
246#endif
247
248 /* AIX uses __ss_family instead of ss_family inside of
249 sockaddr_storage. Instead of trying to figure out which field to
250 use, we can just cast it to a sockaddr.
251 */
252
253 if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) {
254 struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip;
255 struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip;
256
257 r = ntohl(s1->sin_addr.s_addr) -
258 ntohl(s2->sin_addr.s_addr);
259 if (r) {
260 return r;
261 }
262
263 s1 = (struct sockaddr_in *)&i1->netmask;
264 s2 = (struct sockaddr_in *)&i2->netmask;
265
266 return ntohl(s1->sin_addr.s_addr) -
267 ntohl(s2->sin_addr.s_addr);
268 }
269 return 0;
270}
271
272/* this wrapper is used to remove duplicates from the interface list generated
273 above */
274int get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
275{
276 struct iface_struct *ifaces;
277 int total, i, j;
278
279 total = _get_interfaces(mem_ctx, &ifaces);
280 if (total <= 0) return total;
281
282 /* now we need to remove duplicates */
283 TYPESAFE_QSORT(ifaces, total, iface_comp);
284
285 for (i=1;i<total;) {
286 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
287 for (j=i-1;j<total-1;j++) {
288 ifaces[j] = ifaces[j+1];
289 }
290 total--;
291 } else {
292 i++;
293 }
294 }
295
296 *pifaces = ifaces;
297 return total;
298}
299
Note: See TracBrowser for help on using the repository browser.