s3-nmbd: Fix bug #7875
[Samba.git] / source4 / lib / socket / netif.c
blobbf410af4417e3f47cfdbcaf0a0607c504f6038ee
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 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.
34 #include "includes.h"
35 #include "system/network.h"
36 #include "netif.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;
51 int total = 0;
53 if (getifaddrs(&iflist) < 0) {
54 return -1;
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) {
65 continue;
68 /* Check the interface is up. */
69 if (!(ifptr->ifa_flags & IFF_UP)) {
70 continue;
73 /* We don't support IPv6 *yet* */
74 if (ifptr->ifa_addr->sa_family != AF_INET) {
75 continue;
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));
83 total++;
86 freeifaddrs(iflist);
88 return total;
91 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
93 int r;
94 r = strcmp(i1->name, i2->name);
95 if (r) return r;
96 r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
97 if (r) return r;
98 r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
99 return r;
102 /* this wrapper is used to remove duplicates from the interface list generated
103 above */
104 int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
106 int total, i, j;
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);
114 for (i=1;i<total;) {
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];
119 total--;
120 } else {
121 i++;
125 return total;