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