r13504: add back in a comment noting fred as the contributor of the address
[bbaumbach/samba-autobuild/.git] / source4 / lib / netif / interface.c
index 1db0f5caee5f0d7792f0e2bc49835626e7975e90..2a0e3642fee40ce0c790affdc65105dfbf64b352 100644 (file)
@@ -1,7 +1,9 @@
 /* 
    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 "includes.h"
 #include "system/network.h"
 #include "lib/netif/netif.h"
-
-static struct iface_struct *probed_ifaces;
-static int total_probed;
-
-static struct ipv4_addr allones_ip;
-struct ipv4_addr loopback_ip;
+#include "dlinklist.h"
 
 /* used for network interfaces */
 struct interface {
@@ -34,11 +31,17 @@ struct interface {
        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)
 
@@ -77,12 +80,12 @@ static void add_interface(struct in_addr ip, struct in_addr nmask)
                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 = (struct interface *)malloc(sizeof(*iface));
+       iface = talloc(local_interfaces, struct interface);
        if (!iface) return;
        
        ZERO_STRUCTPN(iface);
@@ -91,11 +94,16 @@ static void add_interface(struct in_addr ip, struct in_addr nmask)
        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));
 }
 
 
@@ -111,7 +119,9 @@ This handles the following different forms:
 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;
@@ -133,10 +143,14 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
        /* 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;
@@ -177,115 +191,57 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
 /****************************************************************************
 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_destroy(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;
 }
 
 /****************************************************************************
@@ -296,6 +252,8 @@ int iface_count(void)
        int ret = 0;
        struct interface *i;
 
+       load_interfaces();
+
        for (i=local_interfaces;i;i=i->next)
                ret++;
        return ret;
@@ -304,52 +262,98 @@ int iface_count(void)
 /****************************************************************************
   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_s;
+       }
+       return NULL;
+}
+
+/****************************************************************************
+  return netmask of the Nth interface
+  **************************************************************************/
+const char *iface_n_netmask(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->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();
 
-/* 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. */
+       ip.s_addr = interpret_addr(dest);
+       iface = iface_find(ip, True);
+       if (iface) {
+               return iface->ip_s;
+       }
+       return iface_n_ip(0);
+}
 
-struct ipv4_addr *iface_ip(struct ipv4_addr ip)
+/*
+  return True if an IP is one one of our local networks
+*/
+BOOL iface_is_local(const char *dest)
 {
-       struct in_addr in;
-       struct interface *i;
-       in.s_addr = ip.addr;
-       i = iface_find(in, True);
-       return(i ? &i->ip : &local_interfaces->ip);
+       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));
 }