source: trunk/server/source3/lib/interfaces.c@ 745

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

Samba Server: updated trunk 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 */