r26313: Fix more uses of static loadparm.
[nivanova/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/dlinklist.h"
26 #include "param/param.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 static struct interface *local_interfaces;
39
40 #define ALLONES  ((uint32_t)0xFFFFFFFF)
41 /*
42   address construction based on a patch from fred@datalync.com
43 */
44 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
45 #define MKNETADDR(_IP, _NM) (_IP & _NM)
46
47 /****************************************************************************
48 Try and find an interface that matches an ip. If we cannot, return NULL
49   **************************************************************************/
50 static struct interface *iface_find(struct in_addr ip, bool CheckMask)
51 {
52         struct interface *i;
53         if (is_zero_ip(ip)) return local_interfaces;
54
55         for (i=local_interfaces;i;i=i->next)
56                 if (CheckMask) {
57                         if (same_net(i->ip,ip,i->nmask)) return i;
58                 } else if (i->ip.s_addr == ip.s_addr) return i;
59
60         return NULL;
61 }
62
63
64 /****************************************************************************
65 add an interface to the linked list of interfaces
66 ****************************************************************************/
67 static void add_interface(struct in_addr ip, struct in_addr nmask)
68 {
69         struct interface *iface;
70         struct in_addr bcast;
71
72         if (iface_find(ip, false)) {
73                 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
74                 return;
75         }
76
77         iface = talloc(local_interfaces == NULL ? talloc_autofree_context() : local_interfaces, struct interface);
78         if (!iface) 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(local_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(const char *token, 
114                                 struct iface_struct *probed_ifaces, 
115                                 int total_probed)
116 {
117         struct in_addr ip, nmask;
118         char *p;
119         int i, added=0;
120
121         ip.s_addr = 0;
122         nmask.s_addr = 0;
123         
124         /* first check if it is an interface name */
125         for (i=0;i<total_probed;i++) {
126                 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
127                         add_interface(probed_ifaces[i].ip,
128                                       probed_ifaces[i].netmask);
129                         added = 1;
130                 }
131         }
132         if (added) return;
133
134         /* maybe it is a DNS name */
135         p = strchr_m(token,'/');
136         if (!p) {
137                 /* don't try to do dns lookups on wildcard names */
138                 if (strpbrk(token, "*?") != NULL) {
139                         return;
140                 }
141                 ip.s_addr = interpret_addr2(token).s_addr;
142                 for (i=0;i<total_probed;i++) {
143                         if (ip.s_addr == probed_ifaces[i].ip.s_addr) {
144                                 add_interface(probed_ifaces[i].ip,
145                                               probed_ifaces[i].netmask);
146                                 return;
147                         }
148                 }
149                 DEBUG(2,("can't determine netmask for %s\n", token));
150                 return;
151         }
152
153         /* parse it into an IP address/netmasklength pair */
154         *p++ = 0;
155
156         ip.s_addr = interpret_addr2(token).s_addr;
157
158         if (strlen(p) > 2) {
159                 nmask.s_addr = interpret_addr2(p).s_addr;
160         } else {
161                 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
162         }
163
164         /* maybe the first component was a broadcast address */
165         if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
166             ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
167                 for (i=0;i<total_probed;i++) {
168                         if (same_net(ip, probed_ifaces[i].ip, nmask)) {
169                                 add_interface(probed_ifaces[i].ip, nmask);
170                                 return;
171                         }
172                 }
173                 DEBUG(2,("Can't determine ip for broadcast address %s\n", token));
174                 return;
175         }
176
177         add_interface(ip, nmask);
178 }
179
180
181 /**
182 load the list of network interfaces
183 **/
184 static void load_interfaces(const char **interfaces)
185 {
186         const char **ptr = interfaces;
187         int i;
188         struct iface_struct ifaces[MAX_INTERFACES];
189         struct in_addr loopback_ip;
190         int total_probed;
191
192         if (local_interfaces != NULL) {
193                 return;
194         }
195
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 interfaces
202            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].ip.s_addr != loopback_ip.s_addr) {
209                                 add_interface(ifaces[i].ip, 
210                                               ifaces[i].netmask);
211                         }
212                 }
213         }
214
215         while (ptr && *ptr) {
216                 interpret_interface(*ptr, ifaces, total_probed);
217                 ptr++;
218         }
219
220         if (!local_interfaces) {
221                 DEBUG(0,("WARNING: no network interfaces found\n"));
222         }
223 }
224
225
226 /**
227   unload the interfaces list, so it can be reloaded when needed
228 */
229 void unload_interfaces(void)
230 {
231         talloc_free(local_interfaces);
232         local_interfaces = NULL;
233 }
234
235 /**
236   how many interfaces do we have
237   **/
238 int iface_count(void)
239 {
240         int ret = 0;
241         struct interface *i;
242
243         load_interfaces(lp_interfaces(global_loadparm));
244
245         for (i=local_interfaces;i;i=i->next)
246                 ret++;
247         return ret;
248 }
249
250 /**
251   return IP of the Nth interface
252   **/
253 const char *iface_n_ip(int n)
254 {
255         struct interface *i;
256   
257         load_interfaces(lp_interfaces(global_loadparm));
258
259         for (i=local_interfaces;i && n;i=i->next)
260                 n--;
261
262         if (i) {
263                 return i->ip_s;
264         }
265         return NULL;
266 }
267
268 /**
269   return bcast of the Nth interface
270   **/
271 const char *iface_n_bcast(int n)
272 {
273         struct interface *i;
274   
275         load_interfaces(lp_interfaces(global_loadparm));
276
277         for (i=local_interfaces;i && n;i=i->next)
278                 n--;
279
280         if (i) {
281                 return i->bcast_s;
282         }
283         return NULL;
284 }
285
286 /**
287   return netmask of the Nth interface
288   **/
289 const char *iface_n_netmask(int n)
290 {
291         struct interface *i;
292   
293         load_interfaces(lp_interfaces(global_loadparm));
294
295         for (i=local_interfaces;i && n;i=i->next)
296                 n--;
297
298         if (i) {
299                 return i->nmask_s;
300         }
301         return NULL;
302 }
303
304 /**
305   return the local IP address that best matches a destination IP, or
306   our first interface if none match
307 */
308 const char *iface_best_ip(const char *dest)
309 {
310         struct interface *iface;
311         struct in_addr ip;
312
313         load_interfaces(lp_interfaces(global_loadparm));
314
315         ip.s_addr = interpret_addr(dest);
316         iface = iface_find(ip, true);
317         if (iface) {
318                 return iface->ip_s;
319         }
320         return iface_n_ip(0);
321 }
322
323 /**
324   return true if an IP is one one of our local networks
325 */
326 bool iface_is_local(const char *dest)
327 {
328         struct in_addr ip;
329
330         load_interfaces(lp_interfaces(global_loadparm));
331
332         ip.s_addr = interpret_addr(dest);
333         if (iface_find(ip, true)) {
334                 return true;
335         }
336         return false;
337 }
338
339 /**
340   return true if a IP matches a IP/netmask pair
341 */
342 bool iface_same_net(const char *ip1, const char *ip2, const char *netmask)
343 {
344         return same_net(interpret_addr2(ip1),
345                         interpret_addr2(ip2),
346                         interpret_addr2(netmask));
347 }