r11710: added function iface_same_net()
[Samba/aatanasov.git] / source / lib / netif / interface.c
blob2abb08e090b4daeefe6eea5f6df212beb0181078
1 /*
2 Unix SMB/CIFS implementation.
4 multiple interface handling
6 Copyright (C) Andrew Tridgell 1992-2005
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.
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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "system/network.h"
25 #include "lib/netif/netif.h"
26 #include "dlinklist.h"
28 static struct iface_struct *probed_ifaces;
29 static int total_probed;
31 static struct ipv4_addr allones_ip;
32 struct ipv4_addr loopback_ip;
34 /* used for network interfaces */
35 struct interface {
36 struct interface *next, *prev;
37 struct ipv4_addr ip;
38 struct ipv4_addr bcast;
39 struct ipv4_addr nmask;
42 static struct interface *local_interfaces;
44 #define ALLONES ((uint32_t)0xFFFFFFFF)
45 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
46 #define MKNETADDR(_IP, _NM) (_IP & _NM)
48 static struct ipv4_addr tov4(struct in_addr in)
50 struct ipv4_addr in2;
51 in2.addr = in.s_addr;
52 return in2;
55 /****************************************************************************
56 Try and find an interface that matches an ip. If we cannot, return NULL
57 **************************************************************************/
58 static struct interface *iface_find(struct in_addr ip, BOOL CheckMask)
60 struct interface *i;
61 if (is_zero_ip(tov4(ip))) return local_interfaces;
63 for (i=local_interfaces;i;i=i->next)
64 if (CheckMask) {
65 if (same_net(i->ip,tov4(ip),i->nmask)) return i;
66 } else if (i->ip.addr == ip.s_addr) return i;
68 return NULL;
72 /****************************************************************************
73 add an interface to the linked list of interfaces
74 ****************************************************************************/
75 static void add_interface(struct in_addr ip, struct in_addr nmask)
77 struct interface *iface;
78 if (iface_find(ip, False)) {
79 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
80 return;
83 if (nmask.s_addr == allones_ip.addr) {
84 DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip)));
85 return;
88 iface = malloc_p(struct interface);
89 if (!iface) return;
91 ZERO_STRUCTPN(iface);
93 iface->ip = tov4(ip);
94 iface->nmask = tov4(nmask);
95 iface->bcast.addr = MKBCADDR(iface->ip.addr, iface->nmask.addr);
97 DLIST_ADD_END(local_interfaces, iface, struct interface *);
99 DEBUG(2,("added interface ip=%s ",sys_inet_ntoa(iface->ip)));
100 DEBUG(2,("bcast=%s ",sys_inet_ntoa(iface->bcast)));
101 DEBUG(2,("nmask=%s\n",sys_inet_ntoa(iface->nmask)));
106 /****************************************************************************
107 interpret a single element from a interfaces= config line
109 This handles the following different forms:
111 1) wildcard interface name
112 2) DNS name
113 3) IP/masklen
114 4) ip/mask
115 5) bcast/mask
116 ****************************************************************************/
117 static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
119 struct in_addr ip, nmask;
120 char *p;
121 int i, added=0;
123 ip.s_addr = 0;
124 nmask.s_addr = 0;
126 /* first check if it is an interface name */
127 for (i=0;i<total_probed;i++) {
128 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
129 add_interface(probed_ifaces[i].ip,
130 probed_ifaces[i].netmask);
131 added = 1;
134 if (added) return;
136 /* maybe it is a DNS name */
137 p = strchr_m(token,'/');
138 if (!p) {
139 /* don't try to do dns lookups on wildcard names */
140 if (strpbrk(token, "*?") != NULL) {
141 return;
143 ip.s_addr = interpret_addr2(token).addr;
144 for (i=0;i<total_probed;i++) {
145 if (ip.s_addr == probed_ifaces[i].ip.s_addr &&
146 allones_ip.addr != probed_ifaces[i].netmask.s_addr) {
147 add_interface(probed_ifaces[i].ip,
148 probed_ifaces[i].netmask);
149 return;
152 DEBUG(2,("can't determine netmask for %s\n", token));
153 return;
156 /* parse it into an IP address/netmasklength pair */
157 *p++ = 0;
159 ip.s_addr = interpret_addr2(token).addr;
161 if (strlen(p) > 2) {
162 nmask.s_addr = interpret_addr2(p).addr;
163 } else {
164 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
167 /* maybe the first component was a broadcast address */
168 if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
169 ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
170 for (i=0;i<total_probed;i++) {
171 if (same_net(tov4(ip), tov4(probed_ifaces[i].ip), tov4(nmask))) {
172 add_interface(probed_ifaces[i].ip, nmask);
173 return;
176 DEBUG(2,("Can't determine ip for broadcast address %s\n", token));
177 return;
180 add_interface(ip, nmask);
184 /****************************************************************************
185 load the list of network interfaces
186 ****************************************************************************/
187 void load_interfaces(void)
189 const char **ptr;
190 int i;
191 struct iface_struct ifaces[MAX_INTERFACES];
192 TALLOC_CTX *mem_ctx;
194 ptr = lp_interfaces();
195 mem_ctx = talloc_init("load_interfaces");
196 if (!mem_ctx) {
197 DEBUG(2,("no memory to load interfaces \n"));
198 return;
201 allones_ip = interpret_addr2("255.255.255.255");
202 loopback_ip = interpret_addr2("127.0.0.1");
204 SAFE_FREE(probed_ifaces);
206 /* dump the current interfaces if any */
207 while (local_interfaces) {
208 struct interface *iface = local_interfaces;
209 DLIST_REMOVE(local_interfaces, local_interfaces);
210 ZERO_STRUCTPN(iface);
211 SAFE_FREE(iface);
214 /* probe the kernel for interfaces */
215 total_probed = get_interfaces(ifaces, MAX_INTERFACES);
217 if (total_probed > 0) {
218 probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed);
221 /* if we don't have a interfaces line then use all broadcast capable
222 interfaces except loopback */
223 if (!ptr || !*ptr || !**ptr) {
224 if (total_probed <= 0) {
225 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
227 for (i=0;i<total_probed;i++) {
228 if (probed_ifaces[i].netmask.s_addr != allones_ip.addr &&
229 probed_ifaces[i].ip.s_addr != loopback_ip.addr) {
230 add_interface(probed_ifaces[i].ip,
231 probed_ifaces[i].netmask);
234 goto exit;
237 if (ptr) {
238 while (*ptr) {
239 interpret_interface(mem_ctx, *ptr);
240 ptr++;
244 if (!local_interfaces) {
245 DEBUG(0,("WARNING: no network interfaces found\n"));
248 exit:
249 talloc_free(mem_ctx);
253 /****************************************************************************
254 return True if the list of probed interfaces has changed
255 ****************************************************************************/
256 BOOL interfaces_changed(void)
258 int n;
259 struct iface_struct ifaces[MAX_INTERFACES];
261 n = get_interfaces(ifaces, MAX_INTERFACES);
263 if ((n > 0 )&& (n != total_probed ||
264 memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) {
265 return True;
268 return False;
272 /****************************************************************************
273 check if an IP is one of mine
274 **************************************************************************/
275 BOOL ismyip(struct ipv4_addr ip)
277 struct interface *i;
278 for (i=local_interfaces;i;i=i->next) {
279 if (i->ip.addr == ip.addr) return True;
281 return False;
284 /****************************************************************************
285 how many interfaces do we have
286 **************************************************************************/
287 int iface_count(void)
289 int ret = 0;
290 struct interface *i;
292 for (i=local_interfaces;i;i=i->next)
293 ret++;
294 return ret;
297 /****************************************************************************
298 return IP of the Nth interface
299 **************************************************************************/
300 const char *iface_n_ip(int n)
302 struct interface *i;
304 for (i=local_interfaces;i && n;i=i->next)
305 n--;
307 if (i) {
308 return sys_inet_ntoa(i->ip);
310 return NULL;
313 /****************************************************************************
314 return bcast of the Nth interface
315 **************************************************************************/
316 const char *iface_n_bcast(int n)
318 struct interface *i;
320 for (i=local_interfaces;i && n;i=i->next)
321 n--;
323 if (i) {
324 return sys_inet_ntoa(i->bcast);
326 return NULL;
329 /****************************************************************************
330 return netmask of the Nth interface
331 **************************************************************************/
332 const char *iface_n_netmask(int n)
334 struct interface *i;
336 for (i=local_interfaces;i && n;i=i->next)
337 n--;
339 if (i) {
340 return sys_inet_ntoa(i->nmask);
342 return NULL;
346 return the local IP address that best matches a destination IP, or
347 our first interface if none match
349 const char *iface_best_ip(const char *dest)
351 struct interface *iface;
352 struct in_addr ip;
353 ip.s_addr = interpret_addr(dest);
354 iface = iface_find(ip, True);
355 if (iface) {
356 return sys_inet_ntoa(iface->ip);
358 return iface_n_ip(0);
362 return True if an IP is one one of our local networks
364 BOOL iface_is_local(const char *dest)
366 struct in_addr ip;
367 ip.s_addr = interpret_addr(dest);
368 if (iface_find(ip, True)) {
369 return True;
371 return False;
375 return True if a IP matches a IP/netmask pair
377 BOOL iface_same_net(const char *ip1, const char *ip2, const char *netmask)
379 return same_net(interpret_addr2(ip1),
380 interpret_addr2(ip2),
381 interpret_addr2(netmask));