2 Unix SMB/CIFS implementation.
3 return a list of network interfaces
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Jeremy Allison 2007
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.
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.
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/>.
22 #include "interfaces.h"
24 /****************************************************************************
25 Create a struct sockaddr_storage with the netmask bits set to 1.
26 ****************************************************************************/
28 bool make_netmask(struct sockaddr_storage
*pss_out
,
29 const struct sockaddr_storage
*pss_in
,
30 unsigned long masklen
)
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
;
42 for (i
= 0; masklen
>= 8; masklen
-= 8, i
++) {
45 /* Deal with the partial byte. */
46 *p
++ &= (0xff & ~(0xff>>masklen
));
48 for (;i
< sizeof(struct in6_addr
); i
++) {
54 if (pss_in
->ss_family
== AF_INET
) {
58 ((struct sockaddr_in
*)pss_out
)->sin_addr
.s_addr
=
59 htonl(((0xFFFFFFFFL
>> masklen
) ^ 0xFFFFFFFFL
));
65 /****************************************************************************
66 Create a struct sockaddr_storage set to the broadcast or network adress from
67 an incoming sockaddr_storage.
68 ****************************************************************************/
70 static void make_bcast_or_net(struct sockaddr_storage
*pss_out
,
71 const struct sockaddr_storage
*pss_in
,
72 const struct sockaddr_storage
*nmask
,
75 unsigned int i
= 0, len
= 0;
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
;
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
;
94 for (i
= 0; i
< len
; i
++, p
++, pmask
++) {
96 *p
= (*p
& *pmask
) | (*pmask
^ 0xff);
104 void make_bcast(struct sockaddr_storage
*pss_out
,
105 const struct sockaddr_storage
*pss_in
,
106 const struct sockaddr_storage
*nmask
)
108 make_bcast_or_net(pss_out
, pss_in
, nmask
, true);
111 void make_net(struct sockaddr_storage
*pss_out
,
112 const struct sockaddr_storage
*pss_in
,
113 const struct sockaddr_storage
*nmask
)
115 make_bcast_or_net(pss_out
, pss_in
, nmask
, false);
118 /****************************************************************************
119 Try the "standard" getifaddrs/freeifaddrs interfaces.
120 Also gets IPv6 interfaces.
121 ****************************************************************************/
123 /****************************************************************************
124 Get the netmask address for a local interface.
125 ****************************************************************************/
127 static int _get_interfaces(TALLOC_CTX
*mem_ctx
, struct iface_struct
**pifaces
)
129 struct iface_struct
*ifaces
;
130 struct ifaddrs
*iflist
= NULL
;
131 struct ifaddrs
*ifptr
= NULL
;
136 if (getifaddrs(&iflist
) < 0) {
141 for (ifptr
= iflist
; ifptr
!= NULL
; ifptr
= ifptr
->ifa_next
) {
142 if (!ifptr
->ifa_addr
|| !ifptr
->ifa_netmask
) {
145 if (!(ifptr
->ifa_flags
& IFF_UP
)) {
151 ifaces
= talloc_array(mem_ctx
, struct iface_struct
, count
);
152 if (ifaces
== NULL
) {
157 /* Loop through interfaces, looking for given IP address */
158 for (ifptr
= iflist
; ifptr
!= NULL
; ifptr
= ifptr
->ifa_next
) {
160 if (!ifptr
->ifa_addr
|| !ifptr
->ifa_netmask
) {
164 /* Check the interface is up. */
165 if (!(ifptr
->ifa_flags
& IFF_UP
)) {
169 memset(&ifaces
[total
], '\0', sizeof(ifaces
[total
]));
171 copy_size
= sizeof(struct sockaddr_in
);
173 ifaces
[total
].flags
= ifptr
->ifa_flags
;
175 #if defined(HAVE_IPV6)
176 if (ifptr
->ifa_addr
->sa_family
== AF_INET6
) {
177 copy_size
= sizeof(struct sockaddr_in6
);
181 memcpy(&ifaces
[total
].ip
, ifptr
->ifa_addr
, copy_size
);
182 memcpy(&ifaces
[total
].netmask
, ifptr
->ifa_netmask
, copy_size
);
184 if (ifaces
[total
].flags
& (IFF_BROADCAST
|IFF_LOOPBACK
)) {
185 make_bcast(&ifaces
[total
].bcast
,
187 &ifaces
[total
].netmask
);
188 } else if ((ifaces
[total
].flags
& IFF_POINTOPOINT
) &&
189 ifptr
->ifa_dstaddr
) {
190 memcpy(&ifaces
[total
].bcast
,
197 strlcpy(ifaces
[total
].name
, ifptr
->ifa_name
,
198 sizeof(ifaces
[total
].name
));
208 static int iface_comp(struct iface_struct
*i1
, struct iface_struct
*i2
)
212 #if defined(HAVE_IPV6)
214 * If we have IPv6 - sort these interfaces lower
215 * than any IPv4 ones.
217 if (i1
->ip
.ss_family
== AF_INET6
&&
218 i2
->ip
.ss_family
== AF_INET
) {
220 } else if (i1
->ip
.ss_family
== AF_INET
&&
221 i2
->ip
.ss_family
== AF_INET6
) {
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
;
229 r
= memcmp(&s1
->sin6_addr
,
231 sizeof(struct in6_addr
));
236 s1
= (struct sockaddr_in6
*)&i1
->netmask
;
237 s2
= (struct sockaddr_in6
*)&i2
->netmask
;
239 r
= memcmp(&s1
->sin6_addr
,
241 sizeof(struct in6_addr
));
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.
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
;
257 r
= ntohl(s1
->sin_addr
.s_addr
) -
258 ntohl(s2
->sin_addr
.s_addr
);
263 s1
= (struct sockaddr_in
*)&i1
->netmask
;
264 s2
= (struct sockaddr_in
*)&i2
->netmask
;
266 return ntohl(s1
->sin_addr
.s_addr
) -
267 ntohl(s2
->sin_addr
.s_addr
);
272 /* this wrapper is used to remove duplicates from the interface list generated
274 int get_interfaces(TALLOC_CTX
*mem_ctx
, struct iface_struct
**pifaces
)
276 struct iface_struct
*ifaces
;
279 total
= _get_interfaces(mem_ctx
, &ifaces
);
280 if (total
<= 0) return total
;
282 /* now we need to remove duplicates */
283 TYPESAFE_QSORT(ifaces
, total
, iface_comp
);
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];