Release alpha15.
[amitay/samba.git] / source4 / lib / socket / netif.c
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
7    
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.
12    
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.
17    
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/>.
20 */
21
22
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
26
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.
31
32 */
33
34 #include "includes.h"
35 #include "system/network.h"
36 #include "netif.h"
37 #include "lib/util/tsort.h"
38
39 /****************************************************************************
40  Try the "standard" getifaddrs/freeifaddrs interfaces.
41  Also gets IPv6 interfaces.
42 ****************************************************************************/
43
44 /****************************************************************************
45  Get the netmask address for a local interface.
46 ****************************************************************************/
47
48 static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
49 {
50         struct ifaddrs *iflist = NULL;
51         struct ifaddrs *ifptr = NULL;
52         int total = 0;
53
54         if (getifaddrs(&iflist) < 0) {
55                 return -1;
56         }
57
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) {
62
63                 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
64
65                 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
66                         continue;
67                 }
68
69                 /* Check the interface is up. */
70                 if (!(ifptr->ifa_flags & IFF_UP)) {
71                         continue;
72                 }
73
74                 /* We don't support IPv6 *yet* */
75                 if (ifptr->ifa_addr->sa_family != AF_INET) {
76                         continue;
77                 }
78
79                 ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr;
80                 ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr;
81
82                 strlcpy(ifaces[total].name, ifptr->ifa_name,
83                         sizeof(ifaces[total].name));
84                 total++;
85         }
86
87         freeifaddrs(iflist);
88
89         return total;
90 }
91
92 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
93 {
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;
101 }
102
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)
106 {
107         int total, i, j;
108
109         total = _get_interfaces(ifaces, max_interfaces);
110         if (total <= 0) return total;
111
112         /* now we need to remove duplicates */
113         TYPESAFE_QSORT(ifaces, total, iface_comp);
114
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];
119                         }
120                         total--;
121                 } else {
122                         i++;
123                 }
124         }
125
126         return total;
127 }