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 |
|
---|
23 | /****************************************************************************
|
---|
24 | Create a struct sockaddr_storage with the netmask bits set to 1.
|
---|
25 | ****************************************************************************/
|
---|
26 |
|
---|
27 | bool make_netmask(struct sockaddr_storage *pss_out,
|
---|
28 | const struct sockaddr_storage *pss_in,
|
---|
29 | unsigned long masklen)
|
---|
30 | {
|
---|
31 | *pss_out = *pss_in;
|
---|
32 | /* Now apply masklen bits of mask. */
|
---|
33 | #if defined(HAVE_IPV6)
|
---|
34 | if (pss_in->ss_family == AF_INET6) {
|
---|
35 | char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
|
---|
36 | unsigned int i;
|
---|
37 |
|
---|
38 | if (masklen > 128) {
|
---|
39 | return false;
|
---|
40 | }
|
---|
41 | for (i = 0; masklen >= 8; masklen -= 8, i++) {
|
---|
42 | *p++ = 0xff;
|
---|
43 | }
|
---|
44 | /* Deal with the partial byte. */
|
---|
45 | *p++ &= (0xff & ~(0xff>>masklen));
|
---|
46 | i++;
|
---|
47 | for (;i < sizeof(struct in6_addr); i++) {
|
---|
48 | *p++ = '\0';
|
---|
49 | }
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 | #endif
|
---|
53 | if (pss_in->ss_family == AF_INET) {
|
---|
54 | if (masklen > 32) {
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | ((struct sockaddr_in *)pss_out)->sin_addr.s_addr =
|
---|
58 | htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL));
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /****************************************************************************
|
---|
65 | Create a struct sockaddr_storage set to the broadcast or network adress from
|
---|
66 | an incoming sockaddr_storage.
|
---|
67 | ****************************************************************************/
|
---|
68 |
|
---|
69 | static void make_bcast_or_net(struct sockaddr_storage *pss_out,
|
---|
70 | const struct sockaddr_storage *pss_in,
|
---|
71 | const struct sockaddr_storage *nmask,
|
---|
72 | bool make_bcast_p)
|
---|
73 | {
|
---|
74 | unsigned int i = 0, len = 0;
|
---|
75 | char *pmask = NULL;
|
---|
76 | char *p = NULL;
|
---|
77 | *pss_out = *pss_in;
|
---|
78 |
|
---|
79 | /* Set all zero netmask bits to 1. */
|
---|
80 | #if defined(HAVE_IPV6)
|
---|
81 | if (pss_in->ss_family == AF_INET6) {
|
---|
82 | p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
|
---|
83 | pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr;
|
---|
84 | len = 16;
|
---|
85 | }
|
---|
86 | #endif
|
---|
87 | if (pss_in->ss_family == AF_INET) {
|
---|
88 | p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr;
|
---|
89 | pmask = (char *)&((struct sockaddr_in *)nmask)->sin_addr;
|
---|
90 | len = 4;
|
---|
91 | }
|
---|
92 |
|
---|
93 | for (i = 0; i < len; i++, p++, pmask++) {
|
---|
94 | if (make_bcast_p) {
|
---|
95 | *p = (*p & *pmask) | (*pmask ^ 0xff);
|
---|
96 | } else {
|
---|
97 | /* make_net */
|
---|
98 | *p = (*p & *pmask);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | void make_bcast(struct sockaddr_storage *pss_out,
|
---|
104 | const struct sockaddr_storage *pss_in,
|
---|
105 | const struct sockaddr_storage *nmask)
|
---|
106 | {
|
---|
107 | make_bcast_or_net(pss_out, pss_in, nmask, true);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void make_net(struct sockaddr_storage *pss_out,
|
---|
111 | const struct sockaddr_storage *pss_in,
|
---|
112 | const struct sockaddr_storage *nmask)
|
---|
113 | {
|
---|
114 | make_bcast_or_net(pss_out, pss_in, nmask, false);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /****************************************************************************
|
---|
118 | Try the "standard" getifaddrs/freeifaddrs interfaces.
|
---|
119 | Also gets IPv6 interfaces.
|
---|
120 | ****************************************************************************/
|
---|
121 |
|
---|
122 | /****************************************************************************
|
---|
123 | Get the netmask address for a local interface.
|
---|
124 | ****************************************************************************/
|
---|
125 |
|
---|
126 | static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
|
---|
127 | {
|
---|
128 | struct ifaddrs *iflist = NULL;
|
---|
129 | struct ifaddrs *ifptr = NULL;
|
---|
130 | int total = 0;
|
---|
131 | size_t copy_size;
|
---|
132 | if (getifaddrs(&iflist) < 0) {
|
---|
133 | return -1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* Loop through interfaces, looking for given IP address */
|
---|
137 | for (ifptr = iflist, total = 0;
|
---|
138 | ifptr != NULL && total < max_interfaces;
|
---|
139 | ifptr = ifptr->ifa_next) {
|
---|
140 |
|
---|
141 | memset(&ifaces[total], '\0', sizeof(ifaces[total]));
|
---|
142 |
|
---|
143 | copy_size = sizeof(struct sockaddr_in);
|
---|
144 |
|
---|
145 | if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 |
|
---|
149 | ifaces[total].flags = ifptr->ifa_flags;
|
---|
150 |
|
---|
151 | /* Check the interface is up. */
|
---|
152 | if (!(ifaces[total].flags & IFF_UP)) {
|
---|
153 | continue;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #if defined(HAVE_IPV6)
|
---|
157 | if (ifptr->ifa_addr->sa_family == AF_INET6) {
|
---|
158 | copy_size = sizeof(struct sockaddr_in6);
|
---|
159 | }
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size);
|
---|
163 | memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size);
|
---|
164 |
|
---|
165 | if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) {
|
---|
166 | make_bcast(&ifaces[total].bcast,
|
---|
167 | &ifaces[total].ip,
|
---|
168 | &ifaces[total].netmask);
|
---|
169 | } else if ((ifaces[total].flags & IFF_POINTOPOINT) &&
|
---|
170 | ifptr->ifa_dstaddr ) {
|
---|
171 | memcpy(&ifaces[total].bcast,
|
---|
172 | ifptr->ifa_dstaddr,
|
---|
173 | copy_size);
|
---|
174 | } else {
|
---|
175 | continue;
|
---|
176 | }
|
---|
177 |
|
---|
178 | strlcpy(ifaces[total].name, ifptr->ifa_name,
|
---|
179 | sizeof(ifaces[total].name));
|
---|
180 | total++;
|
---|
181 | }
|
---|
182 |
|
---|
183 | freeifaddrs(iflist);
|
---|
184 |
|
---|
185 | return total;
|
---|
186 | }
|
---|
187 |
|
---|
188 | static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
|
---|
189 | {
|
---|
190 | int r;
|
---|
191 |
|
---|
192 | #if defined(HAVE_IPV6)
|
---|
193 | /*
|
---|
194 | * If we have IPv6 - sort these interfaces lower
|
---|
195 | * than any IPv4 ones.
|
---|
196 | */
|
---|
197 | if (i1->ip.ss_family == AF_INET6 &&
|
---|
198 | i2->ip.ss_family == AF_INET) {
|
---|
199 | return -1;
|
---|
200 | } else if (i1->ip.ss_family == AF_INET &&
|
---|
201 | i2->ip.ss_family == AF_INET6) {
|
---|
202 | return 1;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (i1->ip.ss_family == AF_INET6) {
|
---|
206 | struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip;
|
---|
207 | struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip;
|
---|
208 |
|
---|
209 | r = memcmp(&s1->sin6_addr,
|
---|
210 | &s2->sin6_addr,
|
---|
211 | sizeof(struct in6_addr));
|
---|
212 | if (r) {
|
---|
213 | return r;
|
---|
214 | }
|
---|
215 |
|
---|
216 | s1 = (struct sockaddr_in6 *)&i1->netmask;
|
---|
217 | s2 = (struct sockaddr_in6 *)&i2->netmask;
|
---|
218 |
|
---|
219 | r = memcmp(&s1->sin6_addr,
|
---|
220 | &s2->sin6_addr,
|
---|
221 | sizeof(struct in6_addr));
|
---|
222 | if (r) {
|
---|
223 | return r;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | #endif
|
---|
227 |
|
---|
228 | /* AIX uses __ss_family instead of ss_family inside of
|
---|
229 | sockaddr_storage. Instead of trying to figure out which field to
|
---|
230 | use, we can just cast it to a sockaddr.
|
---|
231 | */
|
---|
232 |
|
---|
233 | if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) {
|
---|
234 | struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip;
|
---|
235 | struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip;
|
---|
236 |
|
---|
237 | r = ntohl(s1->sin_addr.s_addr) -
|
---|
238 | ntohl(s2->sin_addr.s_addr);
|
---|
239 | if (r) {
|
---|
240 | return r;
|
---|
241 | }
|
---|
242 |
|
---|
243 | s1 = (struct sockaddr_in *)&i1->netmask;
|
---|
244 | s2 = (struct sockaddr_in *)&i2->netmask;
|
---|
245 |
|
---|
246 | return ntohl(s1->sin_addr.s_addr) -
|
---|
247 | ntohl(s2->sin_addr.s_addr);
|
---|
248 | }
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | int get_interfaces(struct iface_struct *ifaces, int max_interfaces);
|
---|
253 | /* this wrapper is used to remove duplicates from the interface list generated
|
---|
254 | above */
|
---|
255 | int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
|
---|
256 | {
|
---|
257 | int total, i, j;
|
---|
258 |
|
---|
259 | total = _get_interfaces(ifaces, max_interfaces);
|
---|
260 | if (total <= 0) return total;
|
---|
261 |
|
---|
262 | /* now we need to remove duplicates */
|
---|
263 | qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp);
|
---|
264 |
|
---|
265 | for (i=1;i<total;) {
|
---|
266 | if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
|
---|
267 | for (j=i-1;j<total-1;j++) {
|
---|
268 | ifaces[j] = ifaces[j+1];
|
---|
269 | }
|
---|
270 | total--;
|
---|
271 | } else {
|
---|
272 | i++;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | return total;
|
---|
277 | }
|
---|
278 |
|
---|