libutil: moved the networking defines to util_net.h
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    multiple interface handling
5
6    Copyright (C) Andrew Tridgell 1992-2005
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 #include "includes.h"
23 #include "system/network.h"
24 #include "lib/socket/netif.h"
25 #include "../lib/util/util_net.h"
26 #include "../lib/util/dlinklist.h"
27
28 /** used for network interfaces */
29 struct interface {
30         struct interface *next, *prev;
31         struct in_addr ip;
32         struct in_addr nmask;
33         const char *ip_s;
34         const char *bcast_s;
35         const char *nmask_s;
36 };
37
38 #define ALLONES  ((uint32_t)0xFFFFFFFF)
39 /*
40   address construction based on a patch from fred@datalync.com
41 */
42 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
43 #define MKNETADDR(_IP, _NM) (_IP & _NM)
44
45 /****************************************************************************
46 Try and find an interface that matches an ip. If we cannot, return NULL
47   **************************************************************************/
48 static struct interface *iface_find(struct interface *interfaces, 
49                                     struct in_addr ip, bool CheckMask)
50 {
51         struct interface *i;
52         if (is_zero_ip_v4(ip)) return interfaces;
53
54         for (i=interfaces;i;i=i->next)
55                 if (CheckMask) {
56                         if (same_net_v4(i->ip,ip,i->nmask)) return i;
57                 } else if (i->ip.s_addr == ip.s_addr) return i;
58
59         return NULL;
60 }
61
62
63 /****************************************************************************
64 add an interface to the linked list of interfaces
65 ****************************************************************************/
66 static void add_interface(TALLOC_CTX *mem_ctx, struct in_addr ip, struct in_addr nmask, struct interface **interfaces)
67 {
68         struct interface *iface;
69         struct in_addr bcast;
70
71         if (iface_find(*interfaces, ip, false)) {
72                 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
73                 return;
74         }
75
76         iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
77         if (iface == NULL) 
78                 return;
79         
80         ZERO_STRUCTPN(iface);
81
82         iface->ip = ip;
83         iface->nmask = nmask;
84         bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr);
85
86         /* keep string versions too, to avoid people tripping over the implied
87            static in inet_ntoa() */
88         iface->ip_s = talloc_strdup(iface, inet_ntoa(iface->ip));
89         iface->nmask_s = talloc_strdup(iface, inet_ntoa(iface->nmask));
90         
91         if (nmask.s_addr != ~0) {
92                 iface->bcast_s = talloc_strdup(iface, inet_ntoa(bcast));
93         }
94
95         DLIST_ADD_END(*interfaces, iface, struct interface *);
96
97         DEBUG(2,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s));
98 }
99
100
101
102 /**
103 interpret a single element from a interfaces= config line 
104
105 This handles the following different forms:
106
107 1) wildcard interface name
108 2) DNS name
109 3) IP/masklen
110 4) ip/mask
111 5) bcast/mask
112 **/
113 static void interpret_interface(TALLOC_CTX *mem_ctx, 
114                                 const char *token, 
115                                 struct iface_struct *probed_ifaces, 
116                                 int total_probed,
117                                 struct interface **local_interfaces)
118 {
119         struct in_addr ip, nmask;
120         char *p;
121         char *address;
122         int i, added=0;
123
124         ip.s_addr = 0;
125         nmask.s_addr = 0;
126         
127         /* first check if it is an interface name */
128         for (i=0;i<total_probed;i++) {
129                 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
130                         add_interface(mem_ctx, probed_ifaces[i].ip,
131                                       probed_ifaces[i].netmask,
132                                       local_interfaces);
133                         added = 1;
134                 }
135         }
136         if (added) return;
137
138         /* maybe it is a DNS name */
139         p = strchr_m(token,'/');
140         if (!p) {
141                 /* don't try to do dns lookups on wildcard names */
142                 if (strpbrk(token, "*?") != NULL) {
143                         return;
144                 }
145                 ip.s_addr = interpret_addr2(token).s_addr;
146                 for (i=0;i<total_probed;i++) {
147                         if (ip.s_addr == probed_ifaces[i].ip.s_addr) {
148                                 add_interface(mem_ctx, probed_ifaces[i].ip,
149                                               probed_ifaces[i].netmask,
150                                               local_interfaces);
151                                 return;
152                         }
153                 }
154                 DEBUG(2,("can't determine netmask for %s\n", token));
155                 return;
156         }
157
158         address = talloc_strdup(mem_ctx, token);
159         p = strchr_m(address,'/');
160
161         /* parse it into an IP address/netmasklength pair */
162         *p++ = 0;
163
164         ip.s_addr = interpret_addr2(address).s_addr;
165
166         if (strlen(p) > 2) {
167                 nmask.s_addr = interpret_addr2(p).s_addr;
168         } else {
169                 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
170         }
171
172         /* maybe the first component was a broadcast address */
173         if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
174             ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
175                 for (i=0;i<total_probed;i++) {
176                         if (same_net_v4(ip, probed_ifaces[i].ip, nmask)) {
177                                 add_interface(mem_ctx, probed_ifaces[i].ip, nmask,
178                                               local_interfaces);
179                                 talloc_free(address);
180                                 return;
181                         }
182                 }
183                 DEBUG(2,("Can't determine ip for broadcast address %s\n", address));
184                 talloc_free(address);
185                 return;
186         }
187
188         add_interface(mem_ctx, ip, nmask, local_interfaces);
189         talloc_free(address);
190 }
191
192
193 /**
194 load the list of network interfaces
195 **/
196 void load_interfaces(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces)
197 {
198         const char **ptr = interfaces;
199         int i;
200         struct iface_struct ifaces[MAX_INTERFACES];
201         struct in_addr loopback_ip;
202         int total_probed;
203
204         *local_interfaces = NULL;
205
206         loopback_ip = interpret_addr2("127.0.0.1");
207
208         /* probe the kernel for interfaces */
209         total_probed = get_interfaces(ifaces, MAX_INTERFACES);
210
211         /* if we don't have a interfaces line then use all interfaces
212            except loopback */
213         if (!ptr || !*ptr || !**ptr) {
214                 if (total_probed <= 0) {
215                         DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
216                 }
217                 for (i=0;i<total_probed;i++) {
218                         if (ifaces[i].ip.s_addr != loopback_ip.s_addr) {
219                                 add_interface(mem_ctx, ifaces[i].ip, 
220                                               ifaces[i].netmask, local_interfaces);
221                         }
222                 }
223         }
224
225         while (ptr && *ptr) {
226                 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
227                 ptr++;
228         }
229
230         if (!*local_interfaces) {
231                 DEBUG(0,("WARNING: no network interfaces found\n"));
232         }
233 }
234
235 /**
236   how many interfaces do we have
237   **/
238 int iface_count(struct interface *ifaces)
239 {
240         int ret = 0;
241         struct interface *i;
242
243         for (i=ifaces;i;i=i->next)
244                 ret++;
245         return ret;
246 }
247
248 /**
249   return IP of the Nth interface
250   **/
251 const char *iface_n_ip(struct interface *ifaces, int n)
252 {
253         struct interface *i;
254   
255         for (i=ifaces;i && n;i=i->next)
256                 n--;
257
258         if (i) {
259                 return i->ip_s;
260         }
261         return NULL;
262 }
263
264 /**
265   return bcast of the Nth interface
266   **/
267 const char *iface_n_bcast(struct interface *ifaces, int n)
268 {
269         struct interface *i;
270   
271         for (i=ifaces;i && n;i=i->next)
272                 n--;
273
274         if (i) {
275                 return i->bcast_s;
276         }
277         return NULL;
278 }
279
280 /**
281   return netmask of the Nth interface
282   **/
283 const char *iface_n_netmask(struct interface *ifaces, int n)
284 {
285         struct interface *i;
286   
287         for (i=ifaces;i && n;i=i->next)
288                 n--;
289
290         if (i) {
291                 return i->nmask_s;
292         }
293         return NULL;
294 }
295
296 /**
297   return the local IP address that best matches a destination IP, or
298   our first interface if none match
299 */
300 const char *iface_best_ip(struct interface *ifaces, const char *dest)
301 {
302         struct interface *iface;
303         struct in_addr ip;
304
305         ip.s_addr = interpret_addr(dest);
306         iface = iface_find(ifaces, ip, true);
307         if (iface) {
308                 return iface->ip_s;
309         }
310         return iface_n_ip(ifaces, 0);
311 }
312
313 /**
314   return true if an IP is one one of our local networks
315 */
316 bool iface_is_local(struct interface *ifaces, const char *dest)
317 {
318         struct in_addr ip;
319
320         ip.s_addr = interpret_addr(dest);
321         if (iface_find(ifaces, ip, true)) {
322                 return true;
323         }
324         return false;
325 }
326
327 /**
328   return true if a IP matches a IP/netmask pair
329 */
330 bool iface_same_net(const char *ip1, const char *ip2, const char *netmask)
331 {
332         return same_net_v4(interpret_addr2(ip1),
333                         interpret_addr2(ip2),
334                         interpret_addr2(netmask));
335 }