s4:libcli/wrepl: make struct wrepl_request private to winsrepl.c
[Samba/cd1.git] / source4 / lib / socket / netif.c
blobe36f268bde1edb70be65effa2c7ac0a8c02673c9
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"
37 #include "lib/util/tsort.h"
39 /****************************************************************************
40 Try the "standard" getifaddrs/freeifaddrs interfaces.
41 Also gets IPv6 interfaces.
42 ****************************************************************************/
44 /****************************************************************************
45 Get the netmask address for a local interface.
46 ****************************************************************************/
48 static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
50 struct ifaddrs *iflist = NULL;
51 struct ifaddrs *ifptr = NULL;
52 int total = 0;
54 if (getifaddrs(&iflist) < 0) {
55 return -1;
58 /* Loop through interfaces, looking for given IP address */
59 for (ifptr = iflist, total = 0;
60 ifptr != NULL && total < max_interfaces;
61 ifptr = ifptr->ifa_next) {
63 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
65 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
66 continue;
69 /* Check the interface is up. */
70 if (!(ifptr->ifa_flags & IFF_UP)) {
71 continue;
74 /* We don't support IPv6 *yet* */
75 if (ifptr->ifa_addr->sa_family != AF_INET) {
76 continue;
79 ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr;
80 ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr;
82 strlcpy(ifaces[total].name, ifptr->ifa_name,
83 sizeof(ifaces[total].name));
84 total++;
87 freeifaddrs(iflist);
89 return total;
92 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
94 int r;
95 r = strcmp(i1->name, i2->name);
96 if (r) return r;
97 r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
98 if (r) return r;
99 r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
100 return r;
103 /* this wrapper is used to remove duplicates from the interface list generated
104 above */
105 int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
107 int total, i, j;
109 total = _get_interfaces(ifaces, max_interfaces);
110 if (total <= 0) return total;
112 /* now we need to remove duplicates */
113 TYPESAFE_QSORT(ifaces, total, iface_comp);
115 for (i=1;i<total;) {
116 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
117 for (j=i-1;j<total-1;j++) {
118 ifaces[j] = ifaces[j+1];
120 total--;
121 } else {
122 i++;
126 return total;