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
Copyright (C) Jelmer Vernooij 2007
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
23
/* working out the interfaces for a OS is an incredibly non-portable
24
thing. We have several possible implementations below, and autoconf
25
tries each of them to see what works
27
Note that this file does _not_ include includes.h. That is so this code
28
can be called directly from the autoconf tests. That also means
29
this code cannot use any of the normal Samba debug stuff or defines.
30
This is standalone code.
35
#include "system/network.h"
38
/****************************************************************************
39
Try the "standard" getifaddrs/freeifaddrs interfaces.
40
Also gets IPv6 interfaces.
41
****************************************************************************/
43
/****************************************************************************
44
Get the netmask address for a local interface.
45
****************************************************************************/
47
static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
49
struct ifaddrs *iflist = NULL;
50
struct ifaddrs *ifptr = NULL;
53
if (getifaddrs(&iflist) < 0) {
57
/* Loop through interfaces, looking for given IP address */
58
for (ifptr = iflist, total = 0;
59
ifptr != NULL && total < max_interfaces;
60
ifptr = ifptr->ifa_next) {
62
memset(&ifaces[total], '\0', sizeof(ifaces[total]));
64
if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
68
/* Check the interface is up. */
69
if (!(ifptr->ifa_flags & IFF_UP)) {
73
/* We don't support IPv6 *yet* */
74
if (ifptr->ifa_addr->sa_family != AF_INET) {
78
ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr;
79
ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr;
81
strlcpy(ifaces[total].name, ifptr->ifa_name,
82
sizeof(ifaces[total].name));
91
static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
94
r = strcmp(i1->name, i2->name);
96
r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
98
r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
102
/* this wrapper is used to remove duplicates from the interface list generated
104
int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
108
total = _get_interfaces(ifaces, max_interfaces);
109
if (total <= 0) return total;
111
/* now we need to remove duplicates */
112
qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp);
115
if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
116
for (j=i-1;j<total-1;j++) {
117
ifaces[j] = ifaces[j+1];