/*
Unix SMB/CIFS implementation.
+
multiple interface handling
- Copyright (C) Andrew Tridgell 1992-1998
+
+ Copyright (C) Andrew Tridgell 1992-2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "lib/netif/netif.h"
#include "dlinklist.h"
-static struct iface_struct *probed_ifaces;
-static int total_probed;
-
-static struct ipv4_addr allones_ip;
-struct ipv4_addr loopback_ip;
-
/* used for network interfaces */
struct interface {
struct interface *next, *prev;
struct ipv4_addr ip;
struct ipv4_addr bcast;
struct ipv4_addr nmask;
+ const char *ip_s;
+ const char *bcast_s;
+ const char *nmask_s;
};
static struct interface *local_interfaces;
#define ALLONES ((uint32_t)0xFFFFFFFF)
+/*
+ address construction based on a patch from fred@datalync.com
+*/
#define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
#define MKNETADDR(_IP, _NM) (_IP & _NM)
return;
}
- if (nmask.s_addr == allones_ip.addr) {
+ if (nmask.s_addr == ~0) {
DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip)));
return;
}
- iface = malloc_p(struct interface);
+ iface = talloc(local_interfaces, struct interface);
if (!iface) return;
ZERO_STRUCTPN(iface);
iface->nmask = tov4(nmask);
iface->bcast.addr = MKBCADDR(iface->ip.addr, iface->nmask.addr);
- DLIST_ADD(local_interfaces, iface);
+ /* keep string versions too, to avoid people tripping over the implied
+ static in sys_inet_ntoa() */
+ iface->ip_s = talloc_strdup(iface, sys_inet_ntoa(iface->ip));
+ iface->bcast_s = talloc_strdup(iface, sys_inet_ntoa(iface->bcast));
+ iface->nmask_s = talloc_strdup(iface, sys_inet_ntoa(iface->nmask));
+
+ DLIST_ADD_END(local_interfaces, iface, struct interface *);
- DEBUG(2,("added interface ip=%s ",sys_inet_ntoa(iface->ip)));
- DEBUG(2,("bcast=%s ",sys_inet_ntoa(iface->bcast)));
- DEBUG(2,("nmask=%s\n",sys_inet_ntoa(iface->nmask)));
+ DEBUG(2,("added interface ip=%s bcast=%s nmask=%s\n",
+ iface->ip_s, iface->bcast_s, iface->nmask_s));
}
4) ip/mask
5) bcast/mask
****************************************************************************/
-static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
+static void interpret_interface(const char *token,
+ struct iface_struct *probed_ifaces,
+ int total_probed)
{
struct in_addr ip, nmask;
char *p;
/* maybe it is a DNS name */
p = strchr_m(token,'/');
if (!p) {
+ /* don't try to do dns lookups on wildcard names */
+ if (strpbrk(token, "*?") != NULL) {
+ return;
+ }
ip.s_addr = interpret_addr2(token).addr;
for (i=0;i<total_probed;i++) {
if (ip.s_addr == probed_ifaces[i].ip.s_addr &&
- allones_ip.addr != probed_ifaces[i].netmask.s_addr) {
+ probed_ifaces[i].netmask.s_addr != ~0) {
add_interface(probed_ifaces[i].ip,
probed_ifaces[i].netmask);
return;
/****************************************************************************
load the list of network interfaces
****************************************************************************/
-void load_interfaces(void)
+static void load_interfaces(void)
{
const char **ptr;
int i;
struct iface_struct ifaces[MAX_INTERFACES];
- TALLOC_CTX *mem_ctx;
+ struct ipv4_addr loopback_ip;
+ int total_probed;
- ptr = lp_interfaces();
- mem_ctx = talloc_init("load_interfaces");
- if (!mem_ctx) {
- DEBUG(2,("no memory to load interfaces \n"));
+ if (local_interfaces != NULL) {
return;
}
- allones_ip = interpret_addr2("255.255.255.255");
+ ptr = lp_interfaces();
loopback_ip = interpret_addr2("127.0.0.1");
- SAFE_FREE(probed_ifaces);
-
- /* dump the current interfaces if any */
- while (local_interfaces) {
- struct interface *iface = local_interfaces;
- DLIST_REMOVE(local_interfaces, local_interfaces);
- ZERO_STRUCTPN(iface);
- SAFE_FREE(iface);
- }
-
/* probe the kernel for interfaces */
total_probed = get_interfaces(ifaces, MAX_INTERFACES);
- if (total_probed > 0) {
- probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed);
- }
-
/* if we don't have a interfaces line then use all broadcast capable
interfaces except loopback */
if (!ptr || !*ptr || !**ptr) {
if (total_probed <= 0) {
DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
- exit(1);
}
for (i=0;i<total_probed;i++) {
- if (probed_ifaces[i].netmask.s_addr != allones_ip.addr &&
- probed_ifaces[i].ip.s_addr != loopback_ip.addr) {
- add_interface(probed_ifaces[i].ip,
- probed_ifaces[i].netmask);
+ if (ifaces[i].netmask.s_addr != ~0 &&
+ ifaces[i].ip.s_addr != loopback_ip.addr) {
+ add_interface(ifaces[i].ip,
+ ifaces[i].netmask);
}
}
- goto exit;
}
- if (ptr) {
- while (*ptr) {
- interpret_interface(mem_ctx, *ptr);
- ptr++;
- }
+ while (ptr && *ptr) {
+ interpret_interface(*ptr, ifaces, total_probed);
+ ptr++;
}
if (!local_interfaces) {
DEBUG(0,("WARNING: no network interfaces found\n"));
}
-
-exit:
- talloc_free(mem_ctx);
-}
-
-
-/****************************************************************************
-return True if the list of probed interfaces has changed
-****************************************************************************/
-BOOL interfaces_changed(void)
-{
- int n;
- struct iface_struct ifaces[MAX_INTERFACES];
-
- n = get_interfaces(ifaces, MAX_INTERFACES);
-
- if ((n > 0 )&& (n != total_probed ||
- memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) {
- return True;
- }
-
- return False;
}
-/****************************************************************************
- check if an IP is one of mine
- **************************************************************************/
-BOOL ismyip(struct ipv4_addr ip)
-{
- struct interface *i;
- for (i=local_interfaces;i;i=i->next)
- if (ipv4_equal(i->ip,ip)) return True;
- return False;
-}
-
-/****************************************************************************
- check if a packet is from a local (known) net
- **************************************************************************/
-BOOL is_local_net(struct ipv4_addr from)
+/*
+ unload the interfaces list, so it can be reloaded when needed
+*/
+void unload_interfaces(void)
{
- struct interface *i;
- for (i=local_interfaces;i;i=i->next) {
- if((from.addr & i->nmask.addr) ==
- (i->ip.addr & i->nmask.addr))
- return True;
- }
- return False;
+ talloc_free(local_interfaces);
+ local_interfaces = NULL;
}
/****************************************************************************
int ret = 0;
struct interface *i;
+ load_interfaces();
+
for (i=local_interfaces;i;i=i->next)
ret++;
return ret;
/****************************************************************************
return IP of the Nth interface
**************************************************************************/
-struct ipv4_addr *iface_n_ip(int n)
+const char *iface_n_ip(int n)
{
struct interface *i;
+ load_interfaces();
+
for (i=local_interfaces;i && n;i=i->next)
n--;
- if (i) return &i->ip;
+ if (i) {
+ return i->ip_s;
+ }
return NULL;
}
/****************************************************************************
return bcast of the Nth interface
**************************************************************************/
-struct ipv4_addr *iface_n_bcast(int n)
+const char *iface_n_bcast(int n)
{
struct interface *i;
+ load_interfaces();
+
for (i=local_interfaces;i && n;i=i->next)
n--;
- if (i) return &i->bcast;
+ if (i) {
+ return i->bcast_s;
+ }
return NULL;
}
-/* these 3 functions return the ip/bcast/nmask for the interface
- most appropriate for the given ip address. If they can't find
- an appropriate interface they return the requested field of the
- first known interface. */
-
-struct ipv4_addr *iface_ip(struct ipv4_addr ip)
+/****************************************************************************
+ return netmask of the Nth interface
+ **************************************************************************/
+const char *iface_n_netmask(int n)
{
- struct in_addr in;
struct interface *i;
- in.s_addr = ip.addr;
- i = iface_find(in, True);
- return(i ? &i->ip : &local_interfaces->ip);
+
+ load_interfaces();
+
+ for (i=local_interfaces;i && n;i=i->next)
+ n--;
+
+ if (i) {
+ return i->nmask_s;
+ }
+ return NULL;
+}
+
+/*
+ return the local IP address that best matches a destination IP, or
+ our first interface if none match
+*/
+const char *iface_best_ip(const char *dest)
+{
+ struct interface *iface;
+ struct in_addr ip;
+
+ load_interfaces();
+
+ ip.s_addr = interpret_addr(dest);
+ iface = iface_find(ip, True);
+ if (iface) {
+ return iface->ip_s;
+ }
+ return iface_n_ip(0);
+}
+
+/*
+ return True if an IP is one one of our local networks
+*/
+BOOL iface_is_local(const char *dest)
+{
+ struct in_addr ip;
+
+ load_interfaces();
+
+ ip.s_addr = interpret_addr(dest);
+ if (iface_find(ip, True)) {
+ return True;
+ }
+ return False;
}
/*
- return True if a IP is directly reachable on one of our interfaces
+ return True if a IP matches a IP/netmask pair
*/
-BOOL iface_local(struct ipv4_addr ip)
+BOOL iface_same_net(const char *ip1, const char *ip2, const char *netmask)
{
- struct in_addr in;
- in.s_addr = ip.addr;
- return iface_find(in, True) ? True : False;
+ return same_net(interpret_addr2(ip1),
+ interpret_addr2(ip2),
+ interpret_addr2(netmask));
}