interface.c: Fix for AIX4.x finding interfaces.
[samba.git] / source3 / lib / interface.c
index f5dab3caffc5741114a2629bb3e7b1ad927ba9bf..940af1eccf4f8705dc437b91e214df8252fac55b 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    multiple interface handling
-   Copyright (C) Andrew Tridgell 1992-1995
+   Copyright (C) Andrew Tridgell 1992-1997
    
    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
@@ -24,7 +24,7 @@
 extern int DEBUGLEVEL;
 
 struct in_addr ipzero;
-struct in_addr ipgrp;
+struct in_addr wins_ip;
 static struct in_addr default_ip;
 static struct in_addr default_bcast;
 static struct in_addr default_nmask;
@@ -32,12 +32,8 @@ static BOOL got_ip=False;
 static BOOL got_bcast=False;
 static BOOL got_nmask=False;
 
-static struct interface {
-  struct interface *next;
-  struct in_addr ip;
-  struct in_addr bcast;
-  struct in_addr nmask;
-} *interfaces = NULL;
+struct interface *local_interfaces  = NULL;
+
 struct interface *last_iface;
 
 /****************************************************************************
@@ -45,8 +41,8 @@ calculate the default netmask for an address
 ****************************************************************************/
 static void default_netmask(struct in_addr *inm, struct in_addr *iad)
 {
-  unsigned long ad = ntohl(iad->s_addr);
-  unsigned long nm;
+  uint32 ad = ntohl(iad->s_addr);
+  uint32 nm;
   /*
   ** Guess a netmask based on the class of the IP address given.
   */
@@ -158,7 +154,7 @@ static void get_broadcast(struct in_addr *if_ipaddr,
       }
     }
   }
-#elif defined(__FreeBSD__) || defined(NETBSD)
+#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) || defined(_AIX41)
   ifc.ifc_len = sizeof(buff);
   ifc.ifc_buf = buff;
   if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
@@ -231,7 +227,7 @@ static void get_broadcast(struct in_addr *if_ipaddr,
 
   /* sanity check on the netmask */
   {
-    unsigned long nm = ntohl(if_nmask->s_addr);
+    uint32 nm = ntohl(if_nmask->s_addr);
     if ((nm >> 24) != 0xFF) {
       DEBUG(0,("Impossible netmask %s - using defaults\n",inet_ntoa(*if_nmask)));
       default_netmask(if_nmask, if_ipaddr);      
@@ -242,9 +238,9 @@ static void get_broadcast(struct in_addr *if_ipaddr,
      all MS operating systems do, we have to comply even if the unix
      box is setup differently */
   {
-    unsigned long ad = ntohl(if_ipaddr->s_addr);
-    unsigned long nm = ntohl(if_nmask->s_addr);
-    unsigned long bc = (ad & nm) | (0xffffffff & ~nm);
+    uint32 ad = ntohl(if_ipaddr->s_addr);
+    uint32 nm = ntohl(if_nmask->s_addr);
+    uint32 bc = (ad & nm) | (0xffffffff & ~nm);
     if_bcast->s_addr = htonl(bc);
   }
   
@@ -253,20 +249,19 @@ static void get_broadcast(struct in_addr *if_ipaddr,
 
 
 
-
 /****************************************************************************
 load a list of network interfaces
 ****************************************************************************/
-void load_interfaces(void)
+static void interpret_interfaces(char *s, struct interface **interfaces,
+               char *description)
 {
-  char *s = lp_interfaces();
   char *ptr = s;
   fstring token;
   struct interface *iface;
   struct in_addr ip;
 
   ipzero = *interpret_addr2("0.0.0.0");
-  ipgrp = *interpret_addr2("255.255.255.255");
+  wins_ip = *interpret_addr2("255.255.255.255");
 
   while (next_token(&ptr,token,NULL)) {
     /* parse it into an IP address/netmasklength pair */
@@ -278,7 +273,7 @@ void load_interfaces(void)
     /* maybe we already have it listed */
     {
       struct interface *i;
-      for (i=interfaces;i;i=i->next)
+      for (i=(*interfaces);i;i=i->next)
        if (ip_equal(ip,i->ip)) break;
       if (i) continue;
     }
@@ -299,18 +294,18 @@ void load_interfaces(void)
     iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr;
     iface->next = NULL;
 
-    if (!interfaces) {
-      interfaces = iface;
+    if (!(*interfaces)) {
+      (*interfaces) = iface;
     } else {
       last_iface->next = iface;
     }
     last_iface = iface;
-    DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip)));
+    DEBUG(1,("Added %s ip=%s ",description,inet_ntoa(iface->ip)));
     DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast)));
     DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask)));        
   }
 
-  if (interfaces) return;
+  if (*interfaces) return;
 
   /* setup a default interface */
   iface = (struct interface *)malloc(sizeof(*iface));
@@ -339,7 +334,8 @@ void load_interfaces(void)
     DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip)));
   }
 
-  interfaces = last_iface = iface;
+  iface->next = NULL;
+  (*interfaces) = last_iface = iface;
 
   DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip)));
   DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast)));
@@ -347,6 +343,16 @@ void load_interfaces(void)
 }
 
 
+/****************************************************************************
+load the remote and local interfaces
+****************************************************************************/
+void load_interfaces(void)
+{
+  /* add the machine's interfaces to local interface structure*/
+  interpret_interfaces(lp_interfaces(), &local_interfaces,"interface");
+}
+
+
 /****************************************************************************
   override the defaults
   **************************************************************************/
@@ -375,7 +381,7 @@ void iface_set_default(char *ip,char *bcast,char *nmask)
 BOOL ismyip(struct in_addr ip)
 {
   struct interface *i;
-  for (i=interfaces;i;i=i->next)
+  for (i=local_interfaces;i;i=i->next)
     if (ip_equal(i->ip,ip)) return True;
   return False;
 }
@@ -386,7 +392,7 @@ BOOL ismyip(struct in_addr ip)
 BOOL ismybcast(struct in_addr bcast)
 {
   struct interface *i;
-  for (i=interfaces;i;i=i->next)
+  for (i=local_interfaces;i;i=i->next)
     if (ip_equal(i->bcast,bcast)) return True;
   return False;
 }
@@ -399,7 +405,7 @@ int iface_count(void)
   int ret = 0;
   struct interface *i;
 
-  for (i=interfaces;i;i=i->next)
+  for (i=local_interfaces;i;i=i->next)
     ret++;
   return ret;
 }
@@ -411,7 +417,7 @@ struct in_addr *iface_n_ip(int n)
 {
   struct interface *i;
   
-  for (i=interfaces;i && n;i=i->next)
+  for (i=local_interfaces;i && n;i=i->next)
     n--;
 
   if (i) return &i->ip;
@@ -421,15 +427,14 @@ struct in_addr *iface_n_ip(int n)
 static struct interface *iface_find(struct in_addr ip)
 {
   struct interface *i;
-  if (zero_ip(ip)) return interfaces;
+  if (zero_ip(ip)) return local_interfaces;
 
-  for (i=interfaces;i;i=i->next)
+  for (i=local_interfaces;i;i=i->next)
     if (same_net(i->ip,ip,i->nmask)) return i;
 
-  return interfaces;
+  return local_interfaces;
 }
 
-
 /* these 3 functions return the ip/bcast/nmask for the interface
    most appropriate for the given ip address */
 
@@ -449,3 +454,4 @@ struct in_addr *iface_ip(struct in_addr ip)
 }
 
 
+