From b9ae225b28f4707609e6436dad4be7ebdd7e181f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 6 Jun 1996 11:43:09 +0000 Subject: - added interface.c and removed all the references to myip, bcast_ip and Netmask, instead replacing them with calls to routines in interface.c - got rid of old MAXINT define - added code to ensure we only return one entry for each name in the ipc enum routines - added new_only option to add_netbios_entry() to prevent overwriting of important names - minor time handling fixup (This used to be commit 7ed71b73ae745da099072eee36fc2700d1d91407) --- source3/lib/interface.c | 421 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 421 insertions(+) create mode 100644 source3/lib/interface.c (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c new file mode 100644 index 0000000000..a47ef6e47e --- /dev/null +++ b/source3/lib/interface.c @@ -0,0 +1,421 @@ +/* + Unix SMB/Netbios implementation. + Version 1.9. + multiple interface handling + Copyright (C) Andrew Tridgell 1992-1995 + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "loadparm.h" + +extern int DEBUGLEVEL; + +struct in_addr ipzero; +static struct in_addr default_ip; +static struct in_addr default_bcast; +static struct in_addr default_nmask; +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 *last_iface; + +/**************************************************************************** +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; + /* + ** Guess a netmask based on the class of the IP address given. + */ + if ( (ad & 0x80000000) == 0 ) { + /* class A address */ + nm = 0xFF000000; + } else if ( (ad & 0xC0000000) == 0x80000000 ) { + /* class B address */ + nm = 0xFFFF0000; + } else if ( (ad & 0xE0000000) == 0xC0000000 ) { + /* class C address */ + nm = 0xFFFFFF00; + } else { + /* class D or E; netmask doesn't make much sense - guess 4 bits */ + nm = 0xFFFFFFF0; + } + inm->s_addr = htonl(nm); +} + + +/**************************************************************************** + get the broadcast address for our address +(troyer@saifr00.ateng.az.honeywell.com) +****************************************************************************/ +static void get_broadcast(struct in_addr *if_ipaddr, + struct in_addr *if_bcast, + struct in_addr *if_nmask) +{ + BOOL found = False; +#ifndef NO_GET_BROADCAST + int sock = -1; /* AF_INET raw socket desc */ + char buff[1024]; + struct ifreq *ifr=NULL; + int i; + +#if defined(EVEREST) + int n_interfaces; + struct ifconf ifc; + struct ifreq *ifreqs; +#elif defined(USE_IFREQ) + struct ifreq ifreq; + struct strioctl strioctl; + struct ifconf *ifc; +#else + struct ifconf ifc; +#endif +#endif + + /* get a default netmask and broadcast */ + default_netmask(if_nmask, if_ipaddr); + +#ifndef NO_GET_BROADCAST + /* Create a socket to the INET kernel. */ +#if USE_SOCKRAW + if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0) +#else + if ((sock = socket(AF_INET, SOCK_DGRAM, 0 )) < 0) +#endif + { + DEBUG(0,( "Unable to open socket to get broadcast address\n")); + return; + } + + /* Get a list of the configured interfaces */ +#ifdef EVEREST + /* This is part of SCO Openserver 5: The ioctls are no longer part + if the lower level STREAMS interface glue. They are now real + ioctl calls */ + + if (ioctl(sock, SIOCGIFANUM, &n_interfaces) < 0) { + DEBUG(0,( "SIOCGIFANUM: %s\n", strerror(errno))); + } else { + DEBUG(0,( "number of interfaces returned is: %d\n", n_interfaces)); + + ifc.ifc_len = sizeof(struct ifreq) * n_interfaces; + ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len); + + if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) + DEBUG(0, ( "SIOCGIFCONF: %s\n", strerror(errno))); + else { + ifr = ifc.ifc_req; + + for (i = 0; i < n_interfaces; ++i) { + if (if_ipaddr->s_addr == + ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) { + found = True; + break; + } + } + } + } +#elif defined(USE_IFREQ) + ifc = (struct ifconf *)buff; + ifc->ifc_len = BUFSIZ - sizeof(struct ifconf); + strioctl.ic_cmd = SIOCGIFCONF; + strioctl.ic_dp = (char *)ifc; + strioctl.ic_len = sizeof(buff); + if (ioctl(sock, I_STR, &strioctl) < 0) { + DEBUG(0,( "I_STR/SIOCGIFCONF: %s\n", strerror(errno))); + } else { + ifr = (struct ifreq *)ifc->ifc_req; + + /* Loop through interfaces, looking for given IP address */ + for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; + } + } + } +#elif defined(__FreeBSD__) || defined(NETBSD) + ifc.ifc_len = sizeof(buff); + ifc.ifc_buf = buff; + if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { + DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno))); + } else { + ifr = ifc.ifc_req; + /* Loop through interfaces, looking for given IP address */ + i = ifc.ifc_len; + while (i > 0) { + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; + } + i -= ifr->ifr_addr.sa_len + IFNAMSIZ; + ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ); + } + } +#else + ifc.ifc_len = sizeof(buff); + ifc.ifc_buf = buff; + if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { + DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno))); + } else { + ifr = ifc.ifc_req; + + /* Loop through interfaces, looking for given IP address */ + for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { +#ifdef BSDI + if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; +#endif + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; + } + } + } +#endif + + if (!found) { + DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr))); + } else { + /* Get the netmask address from the kernel */ +#ifdef USE_IFREQ + ifreq = *ifr; + + strioctl.ic_cmd = SIOCGIFNETMASK; + strioctl.ic_dp = (char *)&ifreq; + strioctl.ic_len = sizeof(struct ifreq); + if (ioctl(sock, I_STR, &strioctl) < 0) + DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); + else + *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; +#else + if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0) + DEBUG(0,("SIOCGIFNETMASK failed\n")); + else + *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; +#endif + + DEBUG(4,("Netmask for %s = %s\n", ifr->ifr_name, + inet_ntoa(*if_nmask))); + } + + /* Close up shop */ + (void) close(sock); + +#endif + + /* sanity check on the netmask */ + { + unsigned long 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); + } + } + + /* derive the broadcast assuming a 1's broadcast, as this is what + 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); + if_bcast->s_addr = htonl(bc); + } + + DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); +} /* get_broadcast */ + + + + +/**************************************************************************** +load a list of network interfaces +****************************************************************************/ +void load_interfaces(void) +{ + char *s = lp_interfaces(); + char *ptr = s; + fstring token; + struct interface *iface; + struct in_addr ip; + + ipzero = *interpret_addr2("0.0.0.0"); + + while (next_token(&ptr,token,NULL)) { + /* parse it into an IP address/netmasklength pair */ + char *p = strchr(token,'/'); + if (p) *p = 0; + + ip = *interpret_addr2(token); + + /* maybe we already have it listed */ + { + struct interface *i; + for (i=interfaces;i;i=i->next) + if (ip_equal(ip,i->ip)) break; + if (i) continue; + } + + iface = (struct interface *)malloc(sizeof(*iface)); + if (!iface) return; + + iface->ip = ip; + + if (p) { + if (strlen(p+1)>2) + iface->nmask = *interpret_addr2(p+1); + else + iface->nmask.s_addr = htonl(~((1<<(32-atoi(p+1)))-1)); + } else { + default_netmask(&iface->nmask,&iface->ip); + } + iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + iface->next = NULL; + + if (!interfaces) { + interfaces = iface; + } else { + last_iface->next = iface; + } + last_iface = iface; + DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip))); + DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast))); + DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); + } + + if (interfaces) return; + + /* setup a default interface */ + iface = (struct interface *)malloc(sizeof(*iface)); + if (!iface) return; + + if (got_ip) { + iface->ip = default_ip; + } else { + get_myname(NULL,&iface->ip); + } + + if (got_bcast) { + iface->bcast = default_bcast; + } else { + get_broadcast(&iface->ip,&iface->bcast,&iface->nmask); + } + + if (got_nmask) { + iface->nmask = default_nmask; + iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + } + + if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { + DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); + } + + interfaces = last_iface = iface; + + DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip))); + DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast))); + DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); +} + + +/**************************************************************************** + override the defaults + **************************************************************************/ +void iface_set_default(char *ip,char *bcast,char *nmask) +{ + if (ip) { + got_ip = True; + default_ip = *interpret_addr2(ip); + } + + if (bcast) { + got_bcast = True; + default_bcast = *interpret_addr2(bcast); + } + + if (nmask) { + got_nmask = True; + default_nmask = *interpret_addr2(nmask); + } +} + + +/**************************************************************************** + check if an IP is one of mine + **************************************************************************/ +BOOL ismyip(struct in_addr ip) +{ + struct interface *i; + for (i=interfaces;i;i=i->next) + if (ip_equal(i->ip,ip)) return True; + return False; +} + +/**************************************************************************** + check if a bcast is one of mine + **************************************************************************/ +BOOL ismybcast(struct in_addr bcast) +{ + struct interface *i; + for (i=interfaces;i;i=i->next) + if (ip_equal(i->bcast,bcast)) return True; + return False; +} + +static struct interface *iface_find(struct in_addr ip) +{ + struct interface *i; + if (zero_ip(ip)) return interfaces; + + for (i=interfaces;i;i=i->next) + if (same_net(i->ip,ip,i->nmask)) return i; + + return interfaces; +} + + +/* these 3 functions return the ip/bcast/nmask for the interface + most appropriate for the given ip address */ + +struct in_addr *iface_bcast(struct in_addr ip) +{ + return(&iface_find(ip)->bcast); +} + +struct in_addr *iface_nmask(struct in_addr ip) +{ + return(&iface_find(ip)->nmask); +} + +struct in_addr *iface_ip(struct in_addr ip) +{ + return(&iface_find(ip)->ip); +} + + -- cgit From d160d93d8fad563400aa1e1274437df1fbd4ecbf Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 7 Jun 1996 03:34:22 +0000 Subject: - added predict.c, moving the routines from util.c - added iface_count() and iface_n_ip() routines so its easy to loop over the local interface list - made readsize a normal loadparm global - check for null w in add_domain_entry() - set the deathtime to time()-1 for doamin entries with servertype==0 This allows servers that are shutting down to be removed - add the 0x1c name at startup if we are a WINS server. Previously we added it only if we were a master - loop over interfaces in add_my_domains(), so people don't have to have a lmhosts file to get lp_workgroup() on all interfaces - set add to True for find_workgroupstruct() in nmbsync, and check for null return - remove some ugly "errno = EBADF" bits. they just confused things. (This used to be commit 88b191b48836eeb7937f25b37d0bdd4a2276e5a7) --- source3/lib/interface.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index a47ef6e47e..f2a535d80c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -388,6 +388,33 @@ BOOL ismybcast(struct in_addr bcast) return False; } +/**************************************************************************** + how many interfaces do we have + **************************************************************************/ +int iface_count(void) +{ + int ret = 0; + struct interface *i; + + for (i=interfaces;i;i=i->next) + ret++; + return ret; +} + +/**************************************************************************** + return IP of the Nth interface + **************************************************************************/ +struct in_addr *iface_n_ip(int n) +{ + struct interface *i; + + for (i=interfaces;i && n;i=i->next) + n--; + + if (i) return &i->ip; + return NULL; +} + static struct interface *iface_find(struct in_addr ip) { struct interface *i; -- cgit From 4eba893b02334953b0f1bc082dcba6f930f9ff80 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 8 Jun 1996 04:33:37 +0000 Subject: - added comments to byteorder.h explaining how it works. - fixed problem with installscripts if srcdir is not set - fixed ptr init bug in interface.c - changed default lookup type in nmblookup to match nbtstat under NT - new quotas fixes for sunos and solaris (This used to be commit e775576f026d282473256aeac6fef65a85acd98e) --- source3/lib/interface.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index f2a535d80c..e93db5e57e 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -25,6 +25,7 @@ extern int DEBUGLEVEL; struct in_addr ipzero; +struct in_addr ipgrp; static struct in_addr default_ip; static struct in_addr default_bcast; static struct in_addr default_nmask; @@ -266,6 +267,7 @@ void load_interfaces(void) struct in_addr ip; ipzero = *interpret_addr2("0.0.0.0"); + ipgrp = *interpret_addr2("255.255.255.255"); while (next_token(&ptr,token,NULL)) { /* parse it into an IP address/netmasklength pair */ @@ -315,6 +317,8 @@ void load_interfaces(void) iface = (struct interface *)malloc(sizeof(*iface)); if (!iface) return; + iface->next = NULL; + if (got_ip) { iface->ip = default_ip; } else { -- cgit From 7e3b4a1c0df1434eb3d02f93c736ce065f9898d8 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 10 Jun 1996 04:38:24 +0000 Subject: got rid of a lot of redundent header files as we now globally generate prototypes automatically using "make proto". This is much less prone to error than the old method of manually adding prototypes (This used to be commit b551dc98f7cc194a5fc2e67a4ebae7fd67a01bbc) --- source3/lib/interface.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index e93db5e57e..f5dab3caff 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -20,7 +20,6 @@ */ #include "includes.h" -#include "loadparm.h" extern int DEBUGLEVEL; -- cgit From 70d59e977fb828f27c40c2b85bacdd0f94c717b1 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Sat, 29 Jun 1996 19:27:12 +0000 Subject: added local and remote interfaces (didn't get done in first attempt) (This used to be commit c0b0139d1a7816b027b2f1cf107487a4508ed92f) --- source3/lib/interface.c | 57 +++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 23 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index f5dab3caff..40fcdfa6e2 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -32,12 +32,9 @@ 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 *remote_interfaces = NULL; + struct interface *last_iface; /**************************************************************************** @@ -253,13 +250,12 @@ 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; @@ -278,7 +274,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 +295,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 +335,7 @@ void load_interfaces(void) DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); } - interfaces = last_iface = iface; + (*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,21 @@ 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"); + + /* add all subnets to remote interfaces structure */ + interpret_interfaces(lp_remote_interfaces(), &remote_interfaces, + "remote subnet"); +} + + /**************************************************************************** override the defaults **************************************************************************/ @@ -375,7 +386,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 +397,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 +410,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 +422,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 +432,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 +459,4 @@ struct in_addr *iface_ip(struct in_addr ip) } + -- cgit From 234b8c602209d966c5b82148b521d9711e08c4b3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 22 Jul 1996 14:32:38 +0000 Subject: removed the remote interfaces stuff. (This used to be commit 51e5a1a546adf6ba36c7e4c3298c651cff7e15b9) --- source3/lib/interface.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 40fcdfa6e2..3a532140b3 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -33,7 +33,6 @@ static BOOL got_bcast=False; static BOOL got_nmask=False; struct interface *local_interfaces = NULL; -struct interface *remote_interfaces = NULL; struct interface *last_iface; @@ -349,12 +348,7 @@ 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"); - - /* add all subnets to remote interfaces structure */ - interpret_interfaces(lp_remote_interfaces(), &remote_interfaces, - "remote subnet"); + interpret_interfaces(lp_interfaces(), &local_interfaces,"interface"); } -- cgit From 02b98a8965a60a7c3394835ff01074fc88ffbb89 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Fri, 9 Aug 1996 18:05:34 +0000 Subject: applying login updates from jim@oxfordcc.co.uk, sent in by lewis2@server.uwindsor.ca. rest of this patch to follow. bug in interface.c - uninitialised pointer. nmbd has 0x20 as well as 0x0 NetBIOS name when lmhosts entry is added. lkcl (This used to be commit 2b9475cc5fda4b272f19c4f168d3f00363c8042b) --- source3/lib/interface.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 3a532140b3..061ac08c7b 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -334,6 +334,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); } + iface->next = NULL; (*interfaces) = last_iface = iface; DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip))); -- cgit From ebc96b7309dbce110ae63bcb91dd8ad5a2cadbbf Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 14 Aug 1996 15:02:28 +0000 Subject: changed "unsigned long" to "uint32" in several places (for IP addresses) to keep 64 bit machines happy. (This used to be commit b4aaec504ae66dc6a0f05d12529100cb62d47afd) --- source3/lib/interface.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 061ac08c7b..59a542ca0e 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -41,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. */ @@ -227,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); @@ -238,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); } -- cgit From 8bc7d6bebd4fcf8c95cb6d58da14404a5e46de91 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Thu, 9 Jan 1997 18:02:17 +0000 Subject: Makefile: Changes to split Solaris into Solaris2.3 and previous, and 2.4 and after from Paul Eggert. Makefile: Added AMIGA changes from Rask Ingemann Lambertsen . charset.c: Patch for Western European Languages from Josef Hinteregger charset.h: Patch for Western European Languages from Josef Hinteregger clitar.c: Patch to re-sync after read fail from (lost contributor name, sorry). includes.h: Patch for AMIGA from Rask Ingemann Lambertsen includes.h: Patch for SunOS atexit by Jeremy (jra@cygnus.com) interface.c: Patch for AMIGA from Rask Ingemann Lambertsen kanji.h: Patch for Western European Languages from Josef Hinteregger locking.c: Patch to fix file locking from Jeremy (jra@cygnus.com) locking.c: Patch to add granularity of lock files to usec by Jeremy (jra@cygnus.com) pipes.c: Patch to fix file locking from Jeremy (jra@cygnus.com) proto.h: Patch to fix file locking from Jeremy (jra@cygnus.com) reply.c: Patch to fix file locking from Jeremy (jra@cygnus.com) server.c: Patch to fix file locking from Jeremy (jra@cygnus.com) server.c: Patch for FAST_SHARE_MODE fix from (lost contributor name, sorry). smb.h: Patch to fix file locking from Jeremy (jra@cygnus.com) smb.h: Patch to add granularity of lock files to usec by Jeremy (jra@cygnus.com) status.c: Patch to fix file locking from Jeremy (jra@cygnus.com) statuc.c: Patch to add granularity of lock files to usec by Jeremy (jra@cygnus.com) system.c: Patch for Western European Languages from Josef Hinteregger trans2.c: Patch to fix file locking from Jeremy (jra@cygnus.com) trans2.c: Patch to fix volume name reported to Win95 from Jeremy (jra@cygnus.com) util.c: Patch for Western European Languages from Josef Hinteregger util.c: Patch to fix client_name from continuously returning UNKNOWN (from various contributors). version.h: Update to 1.9.16p10. (This used to be commit 03d28fa32eb094affa33133ebe2602fdb70f6361) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 59a542ca0e..1c41293cf7 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -154,7 +154,7 @@ static void get_broadcast(struct in_addr *if_ipaddr, } } } -#elif defined(__FreeBSD__) || defined(NETBSD) +#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { -- cgit From 20b5dea237916902437ce3dcdb7c253fd1ad3585 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Wed, 9 Apr 1997 01:19:25 +0000 Subject: Large changes from jra@cygnus.com. Mainly browser updates. access.c: Fixed crash if yp domain unavailable. includes.h: Moved ifdefs for minor platform. interface.c: Changed name of ipgrp to wins_ip to make it clearer. loadparm.c: Changed default of wins support to 'no'. nameannounce.c: Many changes to fix cross subnet browsing. namebrowse.c: Many changes to fix cross subnet browsing. namedbname.c: Many changes to fix cross subnet browsing. namedbresp.c: Many changes to fix cross subnet browsing. namedbsubnet.c: Many changes to fix cross subnet browsing. namedbwork.c: Many changes to fix cross subnet browsing. nameelect.c: Many changes to fix cross subnet browsing. namelogon.c: Many changes to fix cross subnet browsing. namepacket.c: Many changes to fix cross subnet browsing. nameresp.c: Many changes to fix cross subnet browsing. nameserv.c: Many changes to fix cross subnet browsing. nameserv.h: Many changes to fix cross subnet browsing. nameservreply.c: Many changes to fix cross subnet browsing. nameservresp.c: Many changes to fix cross subnet browsing. namework.c: Many changes to fix cross subnet browsing. nmbd.c: Change to search wins subnet. nmbsync.c: Change to check if we are any master before proceeding. proto.h: Added find_subnet_all() and check_work_servertype(). util.c: Moved 'done' settings on name resolution. (This used to be commit a82476eee2c521e5eed092bc367da0a7cef23de1) --- source3/lib/interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 1c41293cf7..0e2a13e7b9 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -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; @@ -261,7 +261,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, 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 */ -- cgit From 0f1f0ceb9519368188f695e18e2341ccfd1b2d15 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Thu, 8 May 1997 01:14:17 +0000 Subject: 'The mother of all checkins' :-). Jeremy Allison (jallison@whistle.com) Wed May 7 1997: Update for 1.9.17alpha1 release - 'browsefix release' designed to make browsing across subnets work. byteorder.h: Updated copyright to 1997. charcnv.c: Updated copyright to 1997. charset.c Updated copyright to 1997. charset.h Updated copyright to 1997. client.c Updated copyright to 1997. clientutil.c Updated copyright to 1997. dir.c Updated copyright to 1997. fault.c Updated copyright to 1997. includes.h Updated copyright to 1997. interface.c Updated copyright to 1997. ipc.c Updated copyright to 1997. kanji.c Updated copyright to 1997. kanji.h Updated copyright to 1997. loadparm.c Updated copyright to 1997. locking.c Updated copyright to 1997. mangle.c Updated copyright to 1997. message.c Updated copyright to 1997. nameannounce.c Made use of WINS subnet explicit. Added reset_announce_timer() so announcement can be made immediately when we become a master. Expanded code to do sync with dmb. namebrowse.c Removed redundent checks for AM_MASTER in sync code. Made use of WINS subnet explicit. namedbname.c Made use of WINS subnet explicit. namedbresp.c Made use of WINS subnet explicit. namedbserver.c Made use of WINS subnet explicit. namedbsubnet.c Explicitly add workgroup to WINS subnet when we become a dmb. Made use of WINS subnet explicit. namedbwork.c Made use of WINS subnet explicit. Removed redundent check_work_servertype() function. nameelect.c Explicitly add workgroup to WINS subnet when we become a master browser. Made use of WINS subnet explicit. namelogon.c Updated copyright to 1997. namepacket.c Updated copyright to 1997. namequery.c Updated copyright to 1997. nameresp.c Made use of WINS subnet explicit. Made nmbd fail if configured as master browser and one exists already. nameserv.c Made use of WINS subnet explicit. Remove redundent logon server and domain master code. nameserv.h Add emumerate subnet macros. nameservreply.c Made use of WINS subnet explicit. nameservresp.c Updated copyright to 1997. namework.c Made use of WINS subnet explicit. Updated code to add sync browser entries to add subnet parameter. nmbd.c Added sanity check for misconfigured nmbd. nmblib.c Updated copyright to 1997. nmblookup.c Updated copyright to 1997. nmbsync.c Removed redundent AM_ANY_MASTER check. params.c Updated copyright to 1997. password.c Updated copyright to 1997. pipes.c Updated copyright to 1997. predict.c Updated copyright to 1997. printing.c Updated copyright to 1997. proto.h Changed protos for new nmbd code. quotas.c Updated copyright to 1997. replace.c Updated copyright to 1997. reply.c Updated copyright to 1997. server.c Updated copyright to 1997. shmem.c Updated copyright to 1997. smb.h Updated copyright to 1997. smbencrypt.c Updated copyright to 1997. smbpasswd.c Updated copyright to 1997. smbrun.c Updated copyright to 1997. status.c Updated copyright to 1997. system.c Updated copyright to 1997. testparm.c Updated copyright to 1997. testprns.c Updated copyright to 1997. time.c Updated copyright to 1997. trans2.c Updated copyright to 1997. trans2.h Updated copyright to 1997. uid.c Updated copyright to 1997. username.c Updated copyright to 1997. util.c Updated copyright to 1997. version.h Changed to 1.9.17alpha1. (This used to be commit cf23a155a1315f50d488794a2caf88402bf3e3e6) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 0e2a13e7b9..444d511426 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -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 -- cgit From 792771ecc954892e85f7715136a4d70221f90d85 Mon Sep 17 00:00:00 2001 From: Samba Release Account Date: Fri, 11 Jul 1997 00:54:45 +0000 Subject: interface.c: Fix for AIX4.x finding interfaces. server.c: Subtle fix for filenames containing ':'. Jeremy (jallison@whistle.com) (This used to be commit ee9f57bab25e5ee07fb2d384b8c416576401a6ab) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 444d511426..940af1eccf 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -154,7 +154,7 @@ static void get_broadcast(struct in_addr *if_ipaddr, } } } -#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) +#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) || defined(_AIX41) ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { -- cgit From b3c610541b7284cbd59827b13af86a457268ae96 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Tue, 23 Sep 1997 16:09:12 +0000 Subject: added code from Philip A Prindeville which expands the interfaces option to this: "interfaces = le0 le1" or "interfaces = all". it uses SIOCGIFxxxx ioctl calls. (This used to be commit 39071415ba6f8a00e2909e443261d0059fe27e82) --- source3/lib/interface.c | 396 +++++++++++++++++++++++++----------------------- 1 file changed, 210 insertions(+), 186 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 940af1eccf..e03333280a 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -21,62 +21,29 @@ #include "includes.h" +#include /* added by philipp */ + extern int DEBUGLEVEL; struct in_addr ipzero; struct in_addr wins_ip; -static struct in_addr default_ip; -static struct in_addr default_bcast; -static struct in_addr default_nmask; -static BOOL got_ip=False; -static BOOL got_bcast=False; -static BOOL got_nmask=False; struct interface *local_interfaces = NULL; +struct interface *present_interfaces = NULL; struct interface *last_iface; -/**************************************************************************** -calculate the default netmask for an address -****************************************************************************/ -static void default_netmask(struct in_addr *inm, struct in_addr *iad) -{ - uint32 ad = ntohl(iad->s_addr); - uint32 nm; - /* - ** Guess a netmask based on the class of the IP address given. - */ - if ( (ad & 0x80000000) == 0 ) { - /* class A address */ - nm = 0xFF000000; - } else if ( (ad & 0xC0000000) == 0x80000000 ) { - /* class B address */ - nm = 0xFFFF0000; - } else if ( (ad & 0xE0000000) == 0xC0000000 ) { - /* class C address */ - nm = 0xFFFFFF00; - } else { - /* class D or E; netmask doesn't make much sense - guess 4 bits */ - nm = 0xFFFFFFF0; - } - inm->s_addr = htonl(nm); -} - - -/**************************************************************************** - get the broadcast address for our address -(troyer@saifr00.ateng.az.honeywell.com) -****************************************************************************/ -static void get_broadcast(struct in_addr *if_ipaddr, - struct in_addr *if_bcast, - struct in_addr *if_nmask) +static void get_interfaces() { - BOOL found = False; -#ifndef NO_GET_BROADCAST int sock = -1; /* AF_INET raw socket desc */ char buff[1024]; - struct ifreq *ifr=NULL; + struct ifreq *ifr=NULL, ifr2; int i; + struct interface *iface, *last_iface = NULL; + +#ifndef ifr_mtu +#define ifr_mtu ifr_metric +#endif #if defined(EVEREST) int n_interfaces; @@ -88,13 +55,10 @@ static void get_broadcast(struct in_addr *if_ipaddr, struct ifconf *ifc; #else struct ifconf ifc; -#endif #endif - /* get a default netmask and broadcast */ - default_netmask(if_nmask, if_ipaddr); + if (present_interfaces) return; /* already initialized */ -#ifndef NO_GET_BROADCAST /* Create a socket to the INET kernel. */ #if USE_SOCKRAW if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0) @@ -126,11 +90,42 @@ static void get_broadcast(struct in_addr *if_ipaddr, ifr = ifc.ifc_req; for (i = 0; i < n_interfaces; ++i) { - if (if_ipaddr->s_addr == - ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) { - found = True; - break; - } + ifr2 = ifr[i]; + if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) + DEBUG(0,("SIOCGIFFLAGS failed\n")); + + if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) != IFF_RUNNING) + continue; + + iface = (struct interface *)malloc(sizeof(*iface)); + assert(iface != NULL); + iface->next = NULL; + iface->ip = ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr; + iface->name = strdup(ifr[i].ifr_name); + iface->flags = ifr2.ifr_flags; + + /* complete with netmask and b'cast address */ + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) + DEBUG(0,("SIOCGIFNETMASK failed\n")); + else + iface->nmask = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; + if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) + DEBUG(0,("SIOCGIFBRDADDR failed\n")); + else + iface->bcast = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; + if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) + DEBUG(0,("SIOCGIFMTU failed\n")); + else + iface->mtu = ifr2.ifr_mtu; + + DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); + + if (!present_interfaces) + present_interfaces = iface; + else + last_iface->next = iface; + last_iface = iface; } } } @@ -147,10 +142,46 @@ static void get_broadcast(struct in_addr *if_ipaddr, /* Loop through interfaces, looking for given IP address */ for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) + DEBUG(0,("SIOCGIFFLAGS failed\n")); + + if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { + + iface = (struct interface *)malloc(sizeof(*iface)); + assert(iface != NULL); + iface->next = NULL; + iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; + iface->name = strdup(ifr->ifr_name); + iface->flags = ifr2.ifr_flags; + + /* complete with netmask and b'cast address */ + ifr2 = *ifr; + strioctl.ic_cmd = SIOCGIFNETMASK; + strioctl.ic_dp = (char *)&ifr2; + strioctl.ic_len = sizeof(struct ifr2); + if (ioctl(sock, I_STR, &strioctl) < 0) + DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); + else + iface->nmask = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; + strioctl.ic_cmd = SIOCGIFBRDADDR; + if (ioctl(sock, I_STR, &strioctl) < 0) + DEBUG(0,("SIOCGIFBRDADDR failed\n")); + else + iface->bcast = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; + strioctl.ic_cmd = SIOCGIFMTU; + if (ioctl(sock, I_STR, &strioctl) < 0) + DEBUG(0,("SIOCGIFMTU failed\n")); + else + iface->mtu = ifr2.ifr_mtu; + + DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); + + if (!present_interfaces) + present_interfaces = iface; + else + last_iface->next = iface; + last_iface = iface; } } } @@ -164,11 +195,42 @@ static void get_broadcast(struct in_addr *if_ipaddr, /* Loop through interfaces, looking for given IP address */ i = ifc.ifc_len; while (i > 0) { - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) + DEBUG(0,("SIOCGIFFLAGS failed\n")); + + if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { + iface = (struct interface *)malloc(sizeof(*iface)); + assert(iface != NULL); + iface->next = NULL; + iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; + iface->name = strdup(ifr->ifr_name); + iface->flags = ifr2.ifr_flags; + + /* complete with netmask and b'cast address */ + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) + DEBUG(0,("SIOCGIFNETMASK failed\n")); + else + iface->nmask = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; + if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) + DEBUG(0,("SIOCGIFBRDADDR failed\n")); + else + iface->bcast = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; + if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) + DEBUG(0,("SIOCGIFMTU failed\n")); + else + iface->mtu = ifr2.ifr_mtu; + + DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); + + if (!present_interfaces) + present_interfaces = iface; + else + last_iface->next = iface; + last_iface = iface; } + i -= ifr->ifr_addr.sa_len + IFNAMSIZ; ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ); } @@ -183,70 +245,53 @@ static void get_broadcast(struct in_addr *if_ipaddr, /* Loop through interfaces, looking for given IP address */ for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) + DEBUG(0,("SIOCGIFFLAGS failed\n")); + + if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { #ifdef BSDI - if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; + if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; #endif - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; + + iface = (struct interface *)malloc(sizeof(*iface)); + assert(iface != NULL); + iface->next = NULL; + iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; + iface->name = strdup(ifr->ifr_name); + iface->flags = ifr2.ifr_flags; + + /* complete with netmask and b'cast address */ + ifr2 = *ifr; + if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) + DEBUG(0,("SIOCGIFNETMASK failed\n")); + else + iface->nmask = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; + if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) + DEBUG(0,("SIOCGIFBRDADDR failed\n")); + else + iface->bcast = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; + if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) + DEBUG(0,("SIOCGIFMTU failed\n")); + else + iface->mtu = ifr2.ifr_mtu; + + DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); + + if (!present_interfaces) + present_interfaces = iface; + else + last_iface->next = iface; + last_iface = iface; } } } #endif - - if (!found) { - DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr))); - } else { - /* Get the netmask address from the kernel */ -#ifdef USE_IFREQ - ifreq = *ifr; - - strioctl.ic_cmd = SIOCGIFNETMASK; - strioctl.ic_dp = (char *)&ifreq; - strioctl.ic_len = sizeof(struct ifreq); - if (ioctl(sock, I_STR, &strioctl) < 0) - DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); - else - *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; -#else - if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0) - DEBUG(0,("SIOCGIFNETMASK failed\n")); - else - *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; -#endif - - DEBUG(4,("Netmask for %s = %s\n", ifr->ifr_name, - inet_ntoa(*if_nmask))); - } /* Close up shop */ (void) close(sock); - -#endif - - /* sanity check on the netmask */ - { - 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); - } - } - - /* derive the broadcast assuming a 1's broadcast, as this is what - all MS operating systems do, we have to comply even if the unix - box is setup differently */ - { - 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); - } - - DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); -} /* get_broadcast */ +} /* get_interfaces */ /**************************************************************************** @@ -257,42 +302,68 @@ static void interpret_interfaces(char *s, struct interface **interfaces, { char *ptr = s; fstring token; - struct interface *iface; - struct in_addr ip; + struct interface *iface, *i; + BOOL seenALL = False; ipzero = *interpret_addr2("0.0.0.0"); wins_ip = *interpret_addr2("255.255.255.255"); + get_interfaces(); + while (next_token(&ptr,token,NULL)) { - /* parse it into an IP address/netmasklength pair */ - char *p = strchr(token,'/'); - if (p) *p = 0; - ip = *interpret_addr2(token); + if (strcasecmp(token, "ALL")) { + if (*interfaces) { + DEBUG(0, ("Error: interface name \"ALL\" must occur alone\n")); + /* should do something here ... */ + } - /* maybe we already have it listed */ - { - struct interface *i; - for (i=(*interfaces);i;i=i->next) - if (ip_equal(ip,i->ip)) break; - if (i) continue; + /* should we copy the list, or just point at it? */ + for (i = present_interfaces; i; i = i->next) { + iface = (struct interface *)malloc(sizeof(*iface)); + if (!iface) return; + + *iface = *i; + iface->next = NULL; + + if (!(*interfaces)) + (*interfaces) = iface; + else + last_iface->next = iface; + last_iface = iface; + } + + seenALL = True; + continue; + } else if (seenALL) { + DEBUG(0, ("Error: can't mix interface \"ALL\" with other interface namess\n")); + continue; } + /* maybe we already have it listed */ + for (i=(*interfaces);i;i=i->next) + if (strcasecmp(token,i->name)) break; + if (i) continue; + iface = (struct interface *)malloc(sizeof(*iface)); if (!iface) return; - iface->ip = ip; + /* make sure name is known */ + for (i=present_interfaces;i;i=i->next) + if (strcasecmp(token,i->name)) break; - if (p) { - if (strlen(p+1)>2) - iface->nmask = *interpret_addr2(p+1); - else - iface->nmask.s_addr = htonl(~((1<<(32-atoi(p+1)))-1)); - } else { - default_netmask(&iface->nmask,&iface->ip); + if (!i) { + DEBUG(0, ("Warning: unknown interface \"%s\" specified\n", token)); + continue; } - iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + + *iface = *i; iface->next = NULL; + if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { + DEBUG(0, ("Warning: overriding b'cast address %s on interface %s\n", + inet_ntoa(iface->bcast), iface->name)); + iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + } if (!(*interfaces)) { (*interfaces) = iface; @@ -305,41 +376,8 @@ static void interpret_interfaces(char *s, struct interface **interfaces, DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); } - if (*interfaces) return; - - /* setup a default interface */ - iface = (struct interface *)malloc(sizeof(*iface)); - if (!iface) return; - - iface->next = NULL; - - if (got_ip) { - iface->ip = default_ip; - } else { - get_myname(NULL,&iface->ip); - } - - if (got_bcast) { - iface->bcast = default_bcast; - } else { - get_broadcast(&iface->ip,&iface->bcast,&iface->nmask); - } - - if (got_nmask) { - iface->nmask = default_nmask; - iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; - } - - if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { - DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); - } - - 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))); - DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); + if (! *interfaces) + DEBUG(0,("Error: no interfaces specified.\n")); } @@ -358,20 +396,8 @@ void load_interfaces(void) **************************************************************************/ void iface_set_default(char *ip,char *bcast,char *nmask) { - if (ip) { - got_ip = True; - default_ip = *interpret_addr2(ip); - } - - if (bcast) { - got_bcast = True; - default_bcast = *interpret_addr2(bcast); - } - - if (nmask) { - got_nmask = True; - default_nmask = *interpret_addr2(nmask); - } + DEBUG(0, ("iface_set_default: function deprecated.\n")); + exit(1); } @@ -453,5 +479,3 @@ struct in_addr *iface_ip(struct in_addr ip) return(&iface_find(ip)->ip); } - - -- cgit From d939350de407db97422cb22ef885afed3fdf7cea Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 26 Sep 1997 19:23:26 +0000 Subject: Correctly rolled back interface.c (I was working on an older revision). This will now be the current revision. Jeremy (jallison@whistle.com) (This used to be commit eb55ddd5bff1c6f95292215a8c53acb6bfdadeb2) --- source3/lib/interface.c | 396 +++++++++++++++++++++++------------------------- 1 file changed, 186 insertions(+), 210 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index e03333280a..940af1eccf 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -21,29 +21,62 @@ #include "includes.h" -#include /* added by philipp */ - extern int DEBUGLEVEL; struct in_addr ipzero; struct in_addr wins_ip; +static struct in_addr default_ip; +static struct in_addr default_bcast; +static struct in_addr default_nmask; +static BOOL got_ip=False; +static BOOL got_bcast=False; +static BOOL got_nmask=False; struct interface *local_interfaces = NULL; -struct interface *present_interfaces = NULL; struct interface *last_iface; -static void get_interfaces() +/**************************************************************************** +calculate the default netmask for an address +****************************************************************************/ +static void default_netmask(struct in_addr *inm, struct in_addr *iad) +{ + uint32 ad = ntohl(iad->s_addr); + uint32 nm; + /* + ** Guess a netmask based on the class of the IP address given. + */ + if ( (ad & 0x80000000) == 0 ) { + /* class A address */ + nm = 0xFF000000; + } else if ( (ad & 0xC0000000) == 0x80000000 ) { + /* class B address */ + nm = 0xFFFF0000; + } else if ( (ad & 0xE0000000) == 0xC0000000 ) { + /* class C address */ + nm = 0xFFFFFF00; + } else { + /* class D or E; netmask doesn't make much sense - guess 4 bits */ + nm = 0xFFFFFFF0; + } + inm->s_addr = htonl(nm); +} + + +/**************************************************************************** + get the broadcast address for our address +(troyer@saifr00.ateng.az.honeywell.com) +****************************************************************************/ +static void get_broadcast(struct in_addr *if_ipaddr, + struct in_addr *if_bcast, + struct in_addr *if_nmask) { + BOOL found = False; +#ifndef NO_GET_BROADCAST int sock = -1; /* AF_INET raw socket desc */ char buff[1024]; - struct ifreq *ifr=NULL, ifr2; + struct ifreq *ifr=NULL; int i; - struct interface *iface, *last_iface = NULL; - -#ifndef ifr_mtu -#define ifr_mtu ifr_metric -#endif #if defined(EVEREST) int n_interfaces; @@ -55,10 +88,13 @@ static void get_interfaces() struct ifconf *ifc; #else struct ifconf ifc; +#endif #endif - if (present_interfaces) return; /* already initialized */ + /* get a default netmask and broadcast */ + default_netmask(if_nmask, if_ipaddr); +#ifndef NO_GET_BROADCAST /* Create a socket to the INET kernel. */ #if USE_SOCKRAW if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0) @@ -90,42 +126,11 @@ static void get_interfaces() ifr = ifc.ifc_req; for (i = 0; i < n_interfaces; ++i) { - ifr2 = ifr[i]; - if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) - DEBUG(0,("SIOCGIFFLAGS failed\n")); - - if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) != IFF_RUNNING) - continue; - - iface = (struct interface *)malloc(sizeof(*iface)); - assert(iface != NULL); - iface->next = NULL; - iface->ip = ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr; - iface->name = strdup(ifr[i].ifr_name); - iface->flags = ifr2.ifr_flags; - - /* complete with netmask and b'cast address */ - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) - DEBUG(0,("SIOCGIFNETMASK failed\n")); - else - iface->nmask = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; - if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) - DEBUG(0,("SIOCGIFBRDADDR failed\n")); - else - iface->bcast = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; - if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) - DEBUG(0,("SIOCGIFMTU failed\n")); - else - iface->mtu = ifr2.ifr_mtu; - - DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); - - if (!present_interfaces) - present_interfaces = iface; - else - last_iface->next = iface; - last_iface = iface; + if (if_ipaddr->s_addr == + ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) { + found = True; + break; + } } } } @@ -142,46 +147,10 @@ static void get_interfaces() /* Loop through interfaces, looking for given IP address */ for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) - DEBUG(0,("SIOCGIFFLAGS failed\n")); - - if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { - - iface = (struct interface *)malloc(sizeof(*iface)); - assert(iface != NULL); - iface->next = NULL; - iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; - iface->name = strdup(ifr->ifr_name); - iface->flags = ifr2.ifr_flags; - - /* complete with netmask and b'cast address */ - ifr2 = *ifr; - strioctl.ic_cmd = SIOCGIFNETMASK; - strioctl.ic_dp = (char *)&ifr2; - strioctl.ic_len = sizeof(struct ifr2); - if (ioctl(sock, I_STR, &strioctl) < 0) - DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); - else - iface->nmask = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; - strioctl.ic_cmd = SIOCGIFBRDADDR; - if (ioctl(sock, I_STR, &strioctl) < 0) - DEBUG(0,("SIOCGIFBRDADDR failed\n")); - else - iface->bcast = ((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr; - strioctl.ic_cmd = SIOCGIFMTU; - if (ioctl(sock, I_STR, &strioctl) < 0) - DEBUG(0,("SIOCGIFMTU failed\n")); - else - iface->mtu = ifr2.ifr_mtu; - - DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); - - if (!present_interfaces) - present_interfaces = iface; - else - last_iface->next = iface; - last_iface = iface; + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; } } } @@ -195,42 +164,11 @@ static void get_interfaces() /* Loop through interfaces, looking for given IP address */ i = ifc.ifc_len; while (i > 0) { - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) - DEBUG(0,("SIOCGIFFLAGS failed\n")); - - if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { - iface = (struct interface *)malloc(sizeof(*iface)); - assert(iface != NULL); - iface->next = NULL; - iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; - iface->name = strdup(ifr->ifr_name); - iface->flags = ifr2.ifr_flags; - - /* complete with netmask and b'cast address */ - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) - DEBUG(0,("SIOCGIFNETMASK failed\n")); - else - iface->nmask = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; - if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) - DEBUG(0,("SIOCGIFBRDADDR failed\n")); - else - iface->bcast = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; - if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) - DEBUG(0,("SIOCGIFMTU failed\n")); - else - iface->mtu = ifr2.ifr_mtu; - - DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); - - if (!present_interfaces) - present_interfaces = iface; - else - last_iface->next = iface; - last_iface = iface; + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; } - i -= ifr->ifr_addr.sa_len + IFNAMSIZ; ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ); } @@ -245,53 +183,70 @@ static void get_interfaces() /* Loop through interfaces, looking for given IP address */ for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, &ifr2) < 0) - DEBUG(0,("SIOCGIFFLAGS failed\n")); - - if ((ifr2.ifr_flags & (IFF_RUNNING | IFF_LOOPBACK)) == IFF_RUNNING) { #ifdef BSDI - if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; + if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; #endif - - iface = (struct interface *)malloc(sizeof(*iface)); - assert(iface != NULL); - iface->next = NULL; - iface->ip = (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr; - iface->name = strdup(ifr->ifr_name); - iface->flags = ifr2.ifr_flags; - - /* complete with netmask and b'cast address */ - ifr2 = *ifr; - if (ioctl(sock, SIOCGIFNETMASK, &ifr2) < 0) - DEBUG(0,("SIOCGIFNETMASK failed\n")); - else - iface->nmask = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; - if (ioctl(sock, SIOCGIFBRDADDR, &ifr2) < 0) - DEBUG(0,("SIOCGIFBRDADDR failed\n")); - else - iface->bcast = (*(struct sockaddr_in *)&ifr2.ifr_addr).sin_addr; - if (ioctl(sock, SIOCGIFMTU, &ifr2) < 0) - DEBUG(0,("SIOCGIFMTU failed\n")); - else - iface->mtu = ifr2.ifr_mtu; - - DEBUG(4,("Netmask for %s = %s\n", iface->name, inet_ntoa(iface->nmask))); - - if (!present_interfaces) - present_interfaces = iface; - else - last_iface->next = iface; - last_iface = iface; + if (if_ipaddr->s_addr == + (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { + found = True; + break; } } } #endif + + if (!found) { + DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr))); + } else { + /* Get the netmask address from the kernel */ +#ifdef USE_IFREQ + ifreq = *ifr; + + strioctl.ic_cmd = SIOCGIFNETMASK; + strioctl.ic_dp = (char *)&ifreq; + strioctl.ic_len = sizeof(struct ifreq); + if (ioctl(sock, I_STR, &strioctl) < 0) + DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); + else + *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; +#else + if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0) + DEBUG(0,("SIOCGIFNETMASK failed\n")); + else + *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; +#endif + + DEBUG(4,("Netmask for %s = %s\n", ifr->ifr_name, + inet_ntoa(*if_nmask))); + } /* Close up shop */ (void) close(sock); + +#endif + + /* sanity check on the netmask */ + { + 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); + } + } + + /* derive the broadcast assuming a 1's broadcast, as this is what + all MS operating systems do, we have to comply even if the unix + box is setup differently */ + { + 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); + } + + DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); +} /* get_broadcast */ -} /* get_interfaces */ /**************************************************************************** @@ -302,68 +257,42 @@ static void interpret_interfaces(char *s, struct interface **interfaces, { char *ptr = s; fstring token; - struct interface *iface, *i; - BOOL seenALL = False; + struct interface *iface; + struct in_addr ip; ipzero = *interpret_addr2("0.0.0.0"); wins_ip = *interpret_addr2("255.255.255.255"); - get_interfaces(); - while (next_token(&ptr,token,NULL)) { + /* parse it into an IP address/netmasklength pair */ + char *p = strchr(token,'/'); + if (p) *p = 0; - if (strcasecmp(token, "ALL")) { - if (*interfaces) { - DEBUG(0, ("Error: interface name \"ALL\" must occur alone\n")); - /* should do something here ... */ - } - - /* should we copy the list, or just point at it? */ - for (i = present_interfaces; i; i = i->next) { - iface = (struct interface *)malloc(sizeof(*iface)); - if (!iface) return; - - *iface = *i; - iface->next = NULL; - - if (!(*interfaces)) - (*interfaces) = iface; - else - last_iface->next = iface; - last_iface = iface; - } - - seenALL = True; - continue; - } else if (seenALL) { - DEBUG(0, ("Error: can't mix interface \"ALL\" with other interface namess\n")); - continue; - } + ip = *interpret_addr2(token); /* maybe we already have it listed */ - for (i=(*interfaces);i;i=i->next) - if (strcasecmp(token,i->name)) break; - if (i) continue; + { + struct interface *i; + for (i=(*interfaces);i;i=i->next) + if (ip_equal(ip,i->ip)) break; + if (i) continue; + } iface = (struct interface *)malloc(sizeof(*iface)); if (!iface) return; - /* make sure name is known */ - for (i=present_interfaces;i;i=i->next) - if (strcasecmp(token,i->name)) break; + iface->ip = ip; - if (!i) { - DEBUG(0, ("Warning: unknown interface \"%s\" specified\n", token)); - continue; + if (p) { + if (strlen(p+1)>2) + iface->nmask = *interpret_addr2(p+1); + else + iface->nmask.s_addr = htonl(~((1<<(32-atoi(p+1)))-1)); + } else { + default_netmask(&iface->nmask,&iface->ip); } - - *iface = *i; + iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; iface->next = NULL; - if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { - DEBUG(0, ("Warning: overriding b'cast address %s on interface %s\n", - inet_ntoa(iface->bcast), iface->name)); - iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; - } if (!(*interfaces)) { (*interfaces) = iface; @@ -376,8 +305,41 @@ static void interpret_interfaces(char *s, struct interface **interfaces, DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); } - if (! *interfaces) - DEBUG(0,("Error: no interfaces specified.\n")); + if (*interfaces) return; + + /* setup a default interface */ + iface = (struct interface *)malloc(sizeof(*iface)); + if (!iface) return; + + iface->next = NULL; + + if (got_ip) { + iface->ip = default_ip; + } else { + get_myname(NULL,&iface->ip); + } + + if (got_bcast) { + iface->bcast = default_bcast; + } else { + get_broadcast(&iface->ip,&iface->bcast,&iface->nmask); + } + + if (got_nmask) { + iface->nmask = default_nmask; + iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + } + + if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { + DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); + } + + 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))); + DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); } @@ -396,8 +358,20 @@ void load_interfaces(void) **************************************************************************/ void iface_set_default(char *ip,char *bcast,char *nmask) { - DEBUG(0, ("iface_set_default: function deprecated.\n")); - exit(1); + if (ip) { + got_ip = True; + default_ip = *interpret_addr2(ip); + } + + if (bcast) { + got_bcast = True; + default_bcast = *interpret_addr2(bcast); + } + + if (nmask) { + got_nmask = True; + default_nmask = *interpret_addr2(nmask); + } } @@ -479,3 +453,5 @@ struct in_addr *iface_ip(struct in_addr ip) return(&iface_find(ip)->ip); } + + -- cgit From 0235299ea51eb553b157de0cffa117d01adf6a70 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 7 Oct 1997 14:36:22 +0000 Subject: increase the debug level in the "added interface" debug line (This used to be commit b12b3626fd9ccc9cdf2111937b3ee344da361b1f) --- source3/lib/interface.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 940af1eccf..147425d0fc 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -337,9 +337,9 @@ static void interpret_interfaces(char *s, struct interface **interfaces, 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))); - DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask))); + DEBUG(2,("Added interface ip=%s ",inet_ntoa(iface->ip))); + DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); + DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); } -- cgit From cc0f55bd5bce086004c837b4e94b0a0e39b366ed Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 15 Oct 1997 09:16:30 +0000 Subject: added loopback_ip. This is used to detect packets from ourselves (This used to be commit eb76fea411c5c3aa96b7158d02b49ed42ec7ba70) --- source3/lib/interface.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 147425d0fc..1dc605ff2f 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -25,6 +25,7 @@ extern int DEBUGLEVEL; struct in_addr ipzero; struct in_addr wins_ip; +struct in_addr loopback_ip; static struct in_addr default_ip; static struct in_addr default_bcast; static struct in_addr default_nmask; @@ -262,6 +263,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, ipzero = *interpret_addr2("0.0.0.0"); wins_ip = *interpret_addr2("255.255.255.255"); + loopback_ip = *interpret_addr2("127.0.0.1"); while (next_token(&ptr,token,NULL)) { /* parse it into an IP address/netmasklength pair */ -- cgit From c336a2f08183f63031b0a08b2111669bc36a5f30 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 17 Oct 1997 23:08:07 +0000 Subject: .cvsignore: Added make_smbcodepage interface.c: Added is_local_net(). locking.c: Added Fix for zero length share files from Gerald Werner plus a race condition fix for the fix. nameannounce.c: Made function static. namedbresp.c: extern int ClientDGRAM removed - not used. namedbserver.c: extern int ClientDGRAM removed - not used. namedbsubnet.c: Added code to make sockets per subnet. namepacket.c: Added code to read from all sockets & filter. nameresp.c: extern int ClientDGRAM removed - not used. nameserv.c: Indentation tidyup :-). nameserv.h: Added sockets to struct subnet. nameservresp.c: Improved debug message. nmbd.c: Changed to terminte on listen_for_packets exiting. nmbsync.c: extern int ClientDGRAM & ClientNMB removed - not used. proto.h: The usual. util.c: Fixed debug message. Jeremy (jallison@whistle.com) (This used to be commit 6904c2de080b2a9702800e9e4126386ced20569d) --- source3/lib/interface.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 1dc605ff2f..c920cc0cfc 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -399,6 +399,18 @@ BOOL ismybcast(struct in_addr bcast) return False; } +/**************************************************************************** + check if a packet is from a local (known) net + **************************************************************************/ +BOOL is_local_net(struct in_addr from) +{ + struct interface *i; + for (i=local_interfaces;i;i=i->next) + if((from.s_addr & i->nmask.s_addr) == (i->ip.s_addr & i->nmask.s_addr)) + return True; + return False; +} + /**************************************************************************** how many interfaces do we have **************************************************************************/ -- cgit From abb255cfe674a39c6a42f5083af9c5facdbcca05 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 23 Oct 1997 22:30:57 +0000 Subject: Big change to make nmbd code more readable/understandable. Main change is removal of find_name_search() confusion. This has been replaced with find_name_on_subnet() which makes it explicit what is being searched. Also changed wins_subnet to be wins_client_subnet in preparation for splitting the wins subnet into client and server pieces. This is a big nmbd change and I'd appreciate any bug reports. Specific changes follow : asyncdns.c: Removed wins entry from add_netbios_entry(). This is now explicit in the subnet_record parameter. interface.c: iface_bcast(), iface_nmask(), iface_ip() return the default interface if none can be found. Made this behavior explicit - some code in nmbd incorrectly depended upon this (reply_name_status() for instance). nameannounce.c: find_name_search changes to find_name_on_subnet. namebrowse.c: wins_subnet renamed to wins_client_subnet. namedbname.c: find_name_search removed. find_name_on_subnet added. add_netbios_entry - wins parameter removed. namedbsubnet.c: find_req_subnet removed - not explicit enough. nameelect.c: wins_subnet renamed to wins_client_subnet. namepacket.c: listening() simplified. nameresp.c: wins_subnet renamed to wins_client_subnet. nameserv.c: find_name_search moved to find_name_on_subnet. nameserv.h: FIND_XXX -> changed to FIND_SELF_NAME, FIND_ANY_NAME. nameservreply.c: find_name_search moved to find_name_on_subnet. Debug entries changed. nameservresp.c: wins_subnet renamed to wins_client_subnet. namework.c: wins_subnet renamed to wins_client_subnet. nmbd.c: wins parameter removed from add_netbios_entry. nmbsync: wins_subnet renamed to wins_client_subnet. proto.h: The usual. server.c: remove accepted fd from fd_set. Jeremy (jallison@whistle.com) (This used to be commit 2c97b33fc0b5ef181dbf51a50cb61074935165bf) --- source3/lib/interface.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index c920cc0cfc..3b038dcda6 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -438,6 +438,9 @@ struct in_addr *iface_n_ip(int n) return NULL; } +/**************************************************************************** +Try and find an interface that matches an ip. If we cannot, return NULL + **************************************************************************/ static struct interface *iface_find(struct in_addr ip) { struct interface *i; @@ -446,7 +449,7 @@ static struct interface *iface_find(struct in_addr ip) for (i=local_interfaces;i;i=i->next) if (same_net(i->ip,ip,i->nmask)) return i; - return local_interfaces; + return NULL; } /* these 3 functions return the ip/bcast/nmask for the interface @@ -454,17 +457,20 @@ static struct interface *iface_find(struct in_addr ip) struct in_addr *iface_bcast(struct in_addr ip) { - return(&iface_find(ip)->bcast); + struct interface *i = iface_find(ip); + return(i ? &i->bcast : &local_interfaces->bcast); } struct in_addr *iface_nmask(struct in_addr ip) { - return(&iface_find(ip)->nmask); + struct interface *i = iface_find(ip); + return(i ? &i->nmask : &local_interfaces->nmask); } struct in_addr *iface_ip(struct in_addr ip) { - return(&iface_find(ip)->ip); + struct interface *i = iface_find(ip); + return(i ? &i->ip : &local_interfaces->ip); } -- cgit From 00247250052d0f7b76b0478eb08f63844cd13dc5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 1 Nov 1997 08:13:54 +0000 Subject: change a debug level (This used to be commit d8b896a90fb693187a11456e46971c873e9f0d60) --- source3/lib/interface.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 3b038dcda6..8af2696a44 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -302,9 +302,9 @@ static void interpret_interfaces(char *s, struct interface **interfaces, last_iface->next = iface; } last_iface = iface; - 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))); + DEBUG(2,("Added %s ip=%s ",description,inet_ntoa(iface->ip))); + DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); + DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); } if (*interfaces) return; -- cgit From 64f0348a3f994334abe64a4d4896109c3c8c9039 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 13 Dec 1997 14:16:07 +0000 Subject: This is it ! The mega-merge of the JRA_NMBD_REWRITE branch back into the main tree. For the cvs logs of all the files starting nmbd_*.c, look in the JRA_NMBD_REWRITE branch. That branch has now been discontinued. Jeremy. (This used to be commit d80b0cb645f81d16734929a0b27a91c6650499bb) --- source3/lib/interface.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 8af2696a44..0008ad889d 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -24,7 +24,7 @@ extern int DEBUGLEVEL; struct in_addr ipzero; -struct in_addr wins_ip; +struct in_addr allones_ip; struct in_addr loopback_ip; static struct in_addr default_ip; static struct in_addr default_bcast; @@ -33,7 +33,7 @@ static BOOL got_ip=False; static BOOL got_bcast=False; static BOOL got_nmask=False; -struct interface *local_interfaces = NULL; +static struct interface *local_interfaces = NULL; struct interface *last_iface; @@ -262,7 +262,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, struct in_addr ip; ipzero = *interpret_addr2("0.0.0.0"); - wins_ip = *interpret_addr2("255.255.255.255"); + allones_ip = *interpret_addr2("255.255.255.255"); loopback_ip = *interpret_addr2("127.0.0.1"); while (next_token(&ptr,token,NULL)) { @@ -424,6 +424,33 @@ int iface_count(void) return ret; } +/**************************************************************************** + True if we have two or more interfaces. + **************************************************************************/ +BOOL we_are_multihomed() +{ + static int multi = -1; + + if(multi == -1) + multi = (iface_count() > 1 ? True : False); + + return multi; +} + +/**************************************************************************** + return the Nth interface + **************************************************************************/ +struct interface *get_interface(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) + n--; + + if (i) return i; + return NULL; +} + /**************************************************************************** return IP of the Nth interface **************************************************************************/ @@ -453,7 +480,9 @@ static struct interface *iface_find(struct in_addr ip) } /* these 3 functions return the ip/bcast/nmask for the interface - most appropriate for the given ip address */ + 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 in_addr *iface_bcast(struct in_addr ip) { -- cgit From d1e796d8577a666e5ef14f9bb462c080300dca3e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 23 Dec 1997 07:15:59 +0000 Subject: Fixes to compile under OpenBSD from "Todd T. Fries" Jeremy. (This used to be commit 3c9292505914e2119fa7b1973c9fbbe1742262b2) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 0008ad889d..95c0b9d53c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -155,7 +155,7 @@ static void get_broadcast(struct in_addr *if_ipaddr, } } } -#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) || defined(_AIX41) +#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) || defined(_AIX41) || defined(__OpenBSD__) ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { -- cgit From 6e329c6a4b19226dca624bf10eeae23ca856137b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 29 Dec 1997 18:46:20 +0000 Subject: avoid the ~ operator in netmask operations as apparently it causes problems on some systems (eg. freeBSD 2.2.2). I'm surprised by this but the change looks harmless. Patch from fred@datalync.com (This used to be commit ae8151330deafb7abc5aa604e7c8c0b762e3aa20) --- source3/lib/interface.c | 88 ++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 33 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 95c0b9d53c..7ae97e9119 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -37,30 +37,32 @@ static struct interface *local_interfaces = NULL; struct interface *last_iface; +#define ALLONES ((uint32)0xFFFFFFFF) +#define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES)) /**************************************************************************** calculate the default netmask for an address ****************************************************************************/ static void default_netmask(struct in_addr *inm, struct in_addr *iad) { - uint32 ad = ntohl(iad->s_addr); - uint32 nm; - /* - ** Guess a netmask based on the class of the IP address given. - */ - if ( (ad & 0x80000000) == 0 ) { - /* class A address */ - nm = 0xFF000000; - } else if ( (ad & 0xC0000000) == 0x80000000 ) { - /* class B address */ - nm = 0xFFFF0000; - } else if ( (ad & 0xE0000000) == 0xC0000000 ) { - /* class C address */ - nm = 0xFFFFFF00; - } else { - /* class D or E; netmask doesn't make much sense - guess 4 bits */ - nm = 0xFFFFFFF0; - } - inm->s_addr = htonl(nm); + /* + ** Guess a netmask based on the class of the IP address given. + */ + switch((ntohl(iad->s_addr) & 0xE0000000)) { + case 0: /* Class A addr */ + inm->s_addr = htonl(0xFF000000); + break; + + case 0x80000000: /* Class B addr */ + inm->s_addr = htonl(0xFFFF0000); + break; + + case 0xC0000000: /* Class C addr */ + inm->s_addr = htonl(0xFFFFFF00); + break; + + default: /* ??? */ + inm->s_addr = htonl(0xFFFFFFF0); + } } @@ -228,8 +230,30 @@ static void get_broadcast(struct in_addr *if_ipaddr, /* sanity check on the netmask */ { - uint32 nm = ntohl(if_nmask->s_addr); - if ((nm >> 24) != 0xFF) { + uint32 nm; + short onbc; + short offbc; + + nm = ntohl(if_nmask->s_addr); + onbc = 0; + offbc = 0; + while( (onbc + offbc) < 32 ) + { + if( nm & 0x80000000 ) + { + onbc++; + if( offbc ) /* already found an off bit, so mask is wrong */ + { + onbc = 34; + } + } + else + { + offbc++; + } + nm <<= 1; + } + if ((onbc < 8)||(onbc == 34)) { DEBUG(0,("Impossible netmask %s - using defaults\n",inet_ntoa(*if_nmask))); default_netmask(if_nmask, if_ipaddr); } @@ -239,10 +263,7 @@ 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 */ { - 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); + if_bcast->s_addr = MKBCADDR(if_ipaddr->s_addr, if_nmask->s_addr); } DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); @@ -256,11 +277,12 @@ load a list of network interfaces static void interpret_interfaces(char *s, struct interface **interfaces, char *description) { - char *ptr = s; + char *ptr; fstring token; struct interface *iface; struct in_addr ip; + ptr = s; ipzero = *interpret_addr2("0.0.0.0"); allones_ip = *interpret_addr2("255.255.255.255"); loopback_ip = *interpret_addr2("127.0.0.1"); @@ -268,7 +290,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, while (next_token(&ptr,token,NULL)) { /* parse it into an IP address/netmasklength pair */ char *p = strchr(token,'/'); - if (p) *p = 0; + if (p) *p++ = 0; ip = *interpret_addr2(token); @@ -286,14 +308,14 @@ static void interpret_interfaces(char *s, struct interface **interfaces, iface->ip = ip; if (p) { - if (strlen(p+1)>2) - iface->nmask = *interpret_addr2(p+1); + if (strlen(p) > 2) + iface->nmask = *interpret_addr2(p); else - iface->nmask.s_addr = htonl(~((1<<(32-atoi(p+1)))-1)); + iface->nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); } else { default_netmask(&iface->nmask,&iface->ip); } - iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); iface->next = NULL; if (!(*interfaces)) { @@ -329,10 +351,10 @@ static void interpret_interfaces(char *s, struct interface **interfaces, if (got_nmask) { iface->nmask = default_nmask; - iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr; + iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); } - if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) { + if (iface->bcast.s_addr != MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr)) { DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); } -- cgit From c23ed625b22bfc765ba95cb7b8addf55625fea44 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 7 Jan 1998 06:21:56 +0000 Subject: includes.h: Added FreeBSD 3.x fixes. Added HPUX10.x fixes. interface.c: Added netmask fix. nmbd_nameregister.c: Fixed unitialised variable warnings. nmbd_winsproxy.c: Fixed unitialised variable warnings. nmbd_winsserver.c: Fixed DEC warnings. print_svid.c: Fixed DEC warnings. printing.c: Added LPRng fixes. Jeremy. (This used to be commit 28aff043c4a3693a0c20e87c7ce11eb4bf285b78) --- source3/lib/interface.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 7ae97e9119..ff7c6ee816 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -48,11 +48,15 @@ static void default_netmask(struct in_addr *inm, struct in_addr *iad) ** Guess a netmask based on the class of the IP address given. */ switch((ntohl(iad->s_addr) & 0xE0000000)) { - case 0: /* Class A addr */ + case 0x00000000: /* Class A addr */ + case 0x20000000: + case 0x40000000: + case 0x60000000: inm->s_addr = htonl(0xFF000000); break; case 0x80000000: /* Class B addr */ + case 0xA0000000: inm->s_addr = htonl(0xFFFF0000); break; -- cgit From 55f400bd84f26027f5ec9b7fa06b22895de7557c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 22 Jan 1998 13:27:43 +0000 Subject: This is *not* a big change (although it looks like one). This is merely updating the Copyright statements from 1997 to 1998. It's a once a year thing :-). NO OTHER CHANGES WERE MADE. Jeremy. (This used to be commit b9c16977231efb274e08856f7f3f4408dad6d96c) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index ff7c6ee816..9687e1336c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -2,7 +2,7 @@ Unix SMB/Netbios implementation. Version 1.9. multiple interface handling - Copyright (C) Andrew Tridgell 1992-1997 + Copyright (C) Andrew Tridgell 1992-1998 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 -- cgit From cac6a060af598bf94e6414b06e7365ec51ca360e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 13 Apr 1998 19:24:06 +0000 Subject: Changes to allow Samba to be compiled with -Wstrict-prototypes with gcc. (Not a big change although it looks like it :-). Jeremy. (This used to be commit cd2613c57261456485fe4eeecfda209ada70de8e) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 9687e1336c..8c1610e9cb 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -453,7 +453,7 @@ int iface_count(void) /**************************************************************************** True if we have two or more interfaces. **************************************************************************/ -BOOL we_are_multihomed() +BOOL we_are_multihomed(void) { static int multi = -1; -- cgit From 64578c0589a3a741f81fb55c16eeb882128da00b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 29 Jul 1998 03:08:05 +0000 Subject: merge from the autoconf2 branch to the main branch (This used to be commit 3bda7ac417107a7b01d91805ca71c4330657ed21) --- source3/lib/interface.c | 227 ++++++++---------------------------------------- 1 file changed, 36 insertions(+), 191 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 8c1610e9cb..7aae803abf 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -78,200 +78,45 @@ static void get_broadcast(struct in_addr *if_ipaddr, struct in_addr *if_bcast, struct in_addr *if_nmask) { - BOOL found = False; -#ifndef NO_GET_BROADCAST - int sock = -1; /* AF_INET raw socket desc */ - char buff[1024]; - struct ifreq *ifr=NULL; - int i; - -#if defined(EVEREST) - int n_interfaces; - struct ifconf ifc; - struct ifreq *ifreqs; -#elif defined(USE_IFREQ) - struct ifreq ifreq; - struct strioctl strioctl; - struct ifconf *ifc; -#else - struct ifconf ifc; -#endif -#endif - - /* get a default netmask and broadcast */ - default_netmask(if_nmask, if_ipaddr); - -#ifndef NO_GET_BROADCAST - /* Create a socket to the INET kernel. */ -#if USE_SOCKRAW - if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0) -#else - if ((sock = socket(AF_INET, SOCK_DGRAM, 0 )) < 0) -#endif - { - DEBUG(0,( "Unable to open socket to get broadcast address\n")); - return; - } - - /* Get a list of the configured interfaces */ -#ifdef EVEREST - /* This is part of SCO Openserver 5: The ioctls are no longer part - if the lower level STREAMS interface glue. They are now real - ioctl calls */ - - if (ioctl(sock, SIOCGIFANUM, &n_interfaces) < 0) { - DEBUG(0,( "SIOCGIFANUM: %s\n", strerror(errno))); - } else { - DEBUG(0,( "number of interfaces returned is: %d\n", n_interfaces)); - - ifc.ifc_len = sizeof(struct ifreq) * n_interfaces; - ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len); - - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) - DEBUG(0, ( "SIOCGIFCONF: %s\n", strerror(errno))); - else { - ifr = ifc.ifc_req; - - for (i = 0; i < n_interfaces; ++i) { - if (if_ipaddr->s_addr == - ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) { - found = True; - break; + uint32 nm; + short onbc; + short offbc; + + /* get a default netmask and broadcast */ + default_netmask(if_nmask, if_ipaddr); + + get_netmask(if_ipaddr, if_nmask); + + /* sanity check on the netmask */ + nm = ntohl(if_nmask->s_addr); + onbc = 0; + offbc = 0; + while((onbc + offbc) < 32) { + if(nm & 0x80000000) { + onbc++; + if(offbc) { + /* already found an off bit, so mask + is wrong */ + onbc = 34; + } + } else { + offbc++; + } + nm <<= 1; + } + if ((onbc < 8)||(onbc == 34)) { + DEBUG(0,("Impossible netmask %s - using defaults\n", + inet_ntoa(*if_nmask))); + default_netmask(if_nmask, if_ipaddr); } - } - } - } -#elif defined(USE_IFREQ) - ifc = (struct ifconf *)buff; - ifc->ifc_len = BUFSIZ - sizeof(struct ifconf); - strioctl.ic_cmd = SIOCGIFCONF; - strioctl.ic_dp = (char *)ifc; - strioctl.ic_len = sizeof(buff); - if (ioctl(sock, I_STR, &strioctl) < 0) { - DEBUG(0,( "I_STR/SIOCGIFCONF: %s\n", strerror(errno))); - } else { - ifr = (struct ifreq *)ifc->ifc_req; - - /* Loop through interfaces, looking for given IP address */ - for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; - } - } - } -#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA) || defined(_AIX41) || defined(__OpenBSD__) - ifc.ifc_len = sizeof(buff); - ifc.ifc_buf = buff; - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno))); - } else { - ifr = ifc.ifc_req; - /* Loop through interfaces, looking for given IP address */ - i = ifc.ifc_len; - while (i > 0) { - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; - } - i -= ifr->ifr_addr.sa_len + IFNAMSIZ; - ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ); - } - } -#else - ifc.ifc_len = sizeof(buff); - ifc.ifc_buf = buff; - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno))); - } else { - ifr = ifc.ifc_req; - - /* Loop through interfaces, looking for given IP address */ - for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { -#ifdef BSDI - if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break; -#endif - if (if_ipaddr->s_addr == - (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) { - found = True; - break; - } - } - } -#endif - - if (!found) { - DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr))); - } else { - /* Get the netmask address from the kernel */ -#ifdef USE_IFREQ - ifreq = *ifr; - - strioctl.ic_cmd = SIOCGIFNETMASK; - strioctl.ic_dp = (char *)&ifreq; - strioctl.ic_len = sizeof(struct ifreq); - if (ioctl(sock, I_STR, &strioctl) < 0) - DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno))); - else - *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr; -#else - if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0) - DEBUG(0,("SIOCGIFNETMASK failed\n")); - else - *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; -#endif - - DEBUG(4,("Netmask for %s = %s\n", ifr->ifr_name, - inet_ntoa(*if_nmask))); - } - - /* Close up shop */ - (void) close(sock); - -#endif - - /* sanity check on the netmask */ - { - uint32 nm; - short onbc; - short offbc; - - nm = ntohl(if_nmask->s_addr); - onbc = 0; - offbc = 0; - while( (onbc + offbc) < 32 ) - { - if( nm & 0x80000000 ) - { - onbc++; - if( offbc ) /* already found an off bit, so mask is wrong */ - { - onbc = 34; - } - } - else - { - offbc++; - } - nm <<= 1; - } - if ((onbc < 8)||(onbc == 34)) { - DEBUG(0,("Impossible netmask %s - using defaults\n",inet_ntoa(*if_nmask))); - default_netmask(if_nmask, if_ipaddr); - } - } - /* derive the broadcast assuming a 1's broadcast, as this is what - all MS operating systems do, we have to comply even if the unix - box is setup differently */ - { - if_bcast->s_addr = MKBCADDR(if_ipaddr->s_addr, if_nmask->s_addr); - } + /* derive the broadcast assuming a 1's broadcast, as this is what + all MS operating systems do, we have to comply even if the unix + box is setup differently */ + if_bcast->s_addr = MKBCADDR(if_ipaddr->s_addr, if_nmask->s_addr); - DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); -} /* get_broadcast */ + DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); +} -- cgit From 5a44ce9caaa9e3b19ee387b698ac255ec2cb5785 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Aug 1998 05:43:59 +0000 Subject: changed the format of the wins.dat file slightly. It now has a line like this: VERSION 1 251152 the first number is a version #define in nmbd_winsserver.c and will be used if we ever have to change the format again. The second number is a hash of the current interfaces setting. It is used to detect the case where nmbd is restarted on a machine after the IP of the machine has changed (or the interfaces list has changed in any way). When that happens we need to discard the old wins.dat cache or you end up with chaos. This has bitten quite a few people, they find that when they move a machine it continues using the old IP for some things for the next week until the wins entries time out! I've checked, and the old nmbd can handle the new format, although it does spit out a spurious error message about the VERSION line. So users can safely run 2.0alpha then switch back to 1.9.18 without problems. (This used to be commit c4a8cdc60a5b01894ab2456e77b6d89d4c16a088) --- source3/lib/interface.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 7aae803abf..8cc5cfb0b1 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -350,6 +350,28 @@ static struct interface *iface_find(struct in_addr ip) return NULL; } + +/**************************************************************************** +this function provides a simple hash of the configured interfaces. It is +used to detect a change in interfaces to tell us whether to discard +the current wins.dat file. +Note that the result is independent of the order of the interfaces + **************************************************************************/ +unsigned iface_hash(void) +{ + unsigned ret = 0; + struct interface *i; + + for (i=local_interfaces;i;i=i->next) { + unsigned x1 = (unsigned)str_checksum(inet_ntoa(i->ip)); + unsigned x2 = (unsigned)str_checksum(inet_ntoa(i->nmask)); + ret ^= (x1 ^ x2); + } + + return ret; +} + + /* 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 -- cgit From 61b5fd6f32e9ccb612df1354a3e3b3bed5f2b808 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 31 Aug 1998 03:11:42 +0000 Subject: bounds check next_token() to prevent possible buffer overflows (This used to be commit 3eade55dc7c842bdc50205c330802d211fae54d3) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 8cc5cfb0b1..581a2135bd 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -136,7 +136,7 @@ static void interpret_interfaces(char *s, struct interface **interfaces, allones_ip = *interpret_addr2("255.255.255.255"); loopback_ip = *interpret_addr2("127.0.0.1"); - while (next_token(&ptr,token,NULL)) { + while (next_token(&ptr,token,NULL,sizeof(token))) { /* parse it into an IP address/netmasklength pair */ char *p = strchr(token,'/'); if (p) *p++ = 0; -- cgit From e9ea36e4d2270bd7d32da12ef6d6e2299641582d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 5 Sep 1998 05:07:05 +0000 Subject: tridge the destroyer returns! prompted by the interpret_security() dead code that Jean-Francois pointed out I added a make target "finddead" that finds potentially dead (ie. unused) code. It spat out 304 function names ... I went through these are deleted many of them, making others static (finddead also reports functions that are used only in the local file). in doing this I have almost certainly deleted some useful code. I may have even prevented compilation with some compile options. I apologise. I decided it was better to get rid of this code now and add back the one or two functions that are needed than to keep all this baggage. So, if I have done a bit too much "destroying" then let me know. Keep the swearing to a minimum :) One bit I didn't do is the ubibt code. Chris, can you look at that? Heaps of unused functions there. Can they be made static? (This used to be commit 2204475c87f3024ea8fd1fbd7385b2def617a46f) --- source3/lib/interface.c | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 581a2135bd..65d276021c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -259,17 +259,6 @@ BOOL ismyip(struct in_addr ip) return False; } -/**************************************************************************** - check if a bcast is one of mine - **************************************************************************/ -BOOL ismybcast(struct in_addr bcast) -{ - struct interface *i; - for (i=local_interfaces;i;i=i->next) - if (ip_equal(i->bcast,bcast)) return True; - return False; -} - /**************************************************************************** check if a packet is from a local (known) net **************************************************************************/ @@ -383,12 +372,6 @@ struct in_addr *iface_bcast(struct in_addr ip) return(i ? &i->bcast : &local_interfaces->bcast); } -struct in_addr *iface_nmask(struct in_addr ip) -{ - struct interface *i = iface_find(ip); - return(i ? &i->nmask : &local_interfaces->nmask); -} - struct in_addr *iface_ip(struct in_addr ip) { struct interface *i = iface_find(ip); -- cgit From 3db52feb1f3b2c07ce0b06ad4a7099fa6efe3fc7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 Dec 1999 13:27:58 +0000 Subject: first pass at updating head branch to be to be the same as the SAMBA_2_0 branch (This used to be commit 453a822a76780063dff23526c35408866d0c0154) --- source3/lib/interface.c | 452 ++++++++++++++++++++++++------------------------ 1 file changed, 223 insertions(+), 229 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 65d276021c..9266427959 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -21,230 +21,225 @@ #include "includes.h" +#define MAX_INTERFACES 128 + +static struct iface_struct *probed_ifaces; +static int total_probed; + extern int DEBUGLEVEL; struct in_addr ipzero; struct in_addr allones_ip; struct in_addr loopback_ip; -static struct in_addr default_ip; -static struct in_addr default_bcast; -static struct in_addr default_nmask; -static BOOL got_ip=False; -static BOOL got_bcast=False; -static BOOL got_nmask=False; static struct interface *local_interfaces = NULL; -struct interface *last_iface; - #define ALLONES ((uint32)0xFFFFFFFF) #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES)) +#define MKNETADDR(_IP, _NM) (_IP & _NM) + /**************************************************************************** -calculate the default netmask for an address -****************************************************************************/ -static void default_netmask(struct in_addr *inm, struct in_addr *iad) +Try and find an interface that matches an ip. If we cannot, return NULL + **************************************************************************/ +static struct interface *iface_find(struct in_addr ip) { - /* - ** Guess a netmask based on the class of the IP address given. - */ - switch((ntohl(iad->s_addr) & 0xE0000000)) { - case 0x00000000: /* Class A addr */ - case 0x20000000: - case 0x40000000: - case 0x60000000: - inm->s_addr = htonl(0xFF000000); - break; - - case 0x80000000: /* Class B addr */ - case 0xA0000000: - inm->s_addr = htonl(0xFFFF0000); - break; - - case 0xC0000000: /* Class C addr */ - inm->s_addr = htonl(0xFFFFFF00); - break; - - default: /* ??? */ - inm->s_addr = htonl(0xFFFFFFF0); - } + struct interface *i; + if (zero_ip(ip)) return local_interfaces; + + for (i=local_interfaces;i;i=i->next) + if (same_net(i->ip,ip,i->nmask)) return i; + + return NULL; } /**************************************************************************** - get the broadcast address for our address -(troyer@saifr00.ateng.az.honeywell.com) +add an interface to the linked list of interfaces ****************************************************************************/ -static void get_broadcast(struct in_addr *if_ipaddr, - struct in_addr *if_bcast, - struct in_addr *if_nmask) -{ - uint32 nm; - short onbc; - short offbc; - - /* get a default netmask and broadcast */ - default_netmask(if_nmask, if_ipaddr); - - get_netmask(if_ipaddr, if_nmask); - - /* sanity check on the netmask */ - nm = ntohl(if_nmask->s_addr); - onbc = 0; - offbc = 0; - while((onbc + offbc) < 32) { - if(nm & 0x80000000) { - onbc++; - if(offbc) { - /* already found an off bit, so mask - is wrong */ - onbc = 34; - } - } else { - offbc++; - } - nm <<= 1; +static void add_interface(struct in_addr ip, struct in_addr nmask) +{ + struct interface *iface; + if (iface_find(ip)) { + DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); + return; } - if ((onbc < 8)||(onbc == 34)) { - DEBUG(0,("Impossible netmask %s - using defaults\n", - inet_ntoa(*if_nmask))); - default_netmask(if_nmask, if_ipaddr); + + if (ip_equal(nmask, allones_ip)) { + DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip))); + return; } - /* derive the broadcast assuming a 1's broadcast, as this is what - all MS operating systems do, we have to comply even if the unix - box is setup differently */ - if_bcast->s_addr = MKBCADDR(if_ipaddr->s_addr, if_nmask->s_addr); - - DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast))); + iface = (struct interface *)malloc(sizeof(*iface)); + if (!iface) return; + + ZERO_STRUCTPN(iface); + + iface->ip = ip; + iface->nmask = nmask; + iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); + + DLIST_ADD(local_interfaces, iface); + + DEBUG(2,("added interface ip=%s ",inet_ntoa(iface->ip))); + DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); + DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); } /**************************************************************************** -load a list of network interfaces +interpret a single element from a interfaces= config line + +This handles the following different forms: + +1) wildcard interface name +2) DNS name +3) IP/masklen +4) ip/mask +5) bcast/mask ****************************************************************************/ -static void interpret_interfaces(char *s, struct interface **interfaces, - char *description) +static void interpret_interface(char *token) { - char *ptr; - fstring token; - struct interface *iface; - struct in_addr ip; - - ptr = s; - ipzero = *interpret_addr2("0.0.0.0"); - allones_ip = *interpret_addr2("255.255.255.255"); - loopback_ip = *interpret_addr2("127.0.0.1"); - - while (next_token(&ptr,token,NULL,sizeof(token))) { - /* parse it into an IP address/netmasklength pair */ - char *p = strchr(token,'/'); - if (p) *p++ = 0; - - ip = *interpret_addr2(token); - - /* maybe we already have it listed */ - { - struct interface *i; - for (i=(*interfaces);i;i=i->next) - if (ip_equal(ip,i->ip)) break; - if (i) continue; - } - - iface = (struct interface *)malloc(sizeof(*iface)); - if (!iface) return; - - iface->ip = ip; - - if (p) { - if (strlen(p) > 2) - iface->nmask = *interpret_addr2(p); - else - iface->nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); - } else { - default_netmask(&iface->nmask,&iface->ip); - } - iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); - iface->next = NULL; - - if (!(*interfaces)) { - (*interfaces) = iface; - } else { - last_iface->next = iface; - } - last_iface = iface; - DEBUG(2,("Added %s ip=%s ",description,inet_ntoa(iface->ip))); - DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); - DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); - } - - if (*interfaces) return; - - /* setup a default interface */ - iface = (struct interface *)malloc(sizeof(*iface)); - if (!iface) return; - - iface->next = NULL; - - if (got_ip) { - iface->ip = default_ip; - } else { - get_myname(NULL,&iface->ip); - } - - if (got_bcast) { - iface->bcast = default_bcast; - } else { - get_broadcast(&iface->ip,&iface->bcast,&iface->nmask); - } - - if (got_nmask) { - iface->nmask = default_nmask; - iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); - } - - if (iface->bcast.s_addr != MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr)) { - DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip))); - } - - iface->next = NULL; - (*interfaces) = last_iface = iface; - - DEBUG(2,("Added interface ip=%s ",inet_ntoa(iface->ip))); - DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); - DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); + struct in_addr ip, nmask; + char *p; + int i, added=0; + + ip = ipzero; + nmask = ipzero; + + /* first check if it is an interface name */ + for (i=0;i 2) { + nmask = *interpret_addr2(p); + } else { + nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); + } + + /* maybe the first component was a broadcast address */ + if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) || + ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) { + for (i=0;i 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) { + 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;inext) - if (ip_equal(i->ip,ip)) return True; - return False; + struct interface *i; + for (i=local_interfaces;i;i=i->next) + if (ip_equal(i->ip,ip)) return True; + return False; } /**************************************************************************** @@ -264,11 +259,13 @@ BOOL ismyip(struct in_addr ip) **************************************************************************/ BOOL is_local_net(struct in_addr from) { - struct interface *i; - for (i=local_interfaces;i;i=i->next) - if((from.s_addr & i->nmask.s_addr) == (i->ip.s_addr & i->nmask.s_addr)) - return True; - return False; + struct interface *i; + for (i=local_interfaces;i;i=i->next) { + if((from.s_addr & i->nmask.s_addr) == + (i->ip.s_addr & i->nmask.s_addr)) + return True; + } + return False; } /**************************************************************************** @@ -276,12 +273,12 @@ BOOL is_local_net(struct in_addr from) **************************************************************************/ int iface_count(void) { - int ret = 0; - struct interface *i; + int ret = 0; + struct interface *i; - for (i=local_interfaces;i;i=i->next) - ret++; - return ret; + for (i=local_interfaces;i;i=i->next) + ret++; + return ret; } /**************************************************************************** @@ -289,12 +286,12 @@ int iface_count(void) **************************************************************************/ BOOL we_are_multihomed(void) { - static int multi = -1; + static int multi = -1; - if(multi == -1) - multi = (iface_count() > 1 ? True : False); - - return multi; + if(multi == -1) + multi = (iface_count() > 1 ? True : False); + + return multi; } /**************************************************************************** @@ -302,13 +299,13 @@ BOOL we_are_multihomed(void) **************************************************************************/ struct interface *get_interface(int n) { - struct interface *i; + struct interface *i; - for (i=local_interfaces;i && n;i=i->next) - n--; + for (i=local_interfaces;i && n;i=i->next) + n--; - if (i) return i; - return NULL; + if (i) return i; + return NULL; } /**************************************************************************** @@ -316,27 +313,27 @@ struct interface *get_interface(int n) **************************************************************************/ struct in_addr *iface_n_ip(int n) { - struct interface *i; + struct interface *i; - for (i=local_interfaces;i && n;i=i->next) - n--; + for (i=local_interfaces;i && n;i=i->next) + n--; - if (i) return &i->ip; - return NULL; + if (i) return &i->ip; + return NULL; } /**************************************************************************** -Try and find an interface that matches an ip. If we cannot, return NULL + return bcast of the Nth interface **************************************************************************/ -static struct interface *iface_find(struct in_addr ip) +struct in_addr *iface_n_bcast(int n) { - struct interface *i; - if (zero_ip(ip)) return local_interfaces; - - for (i=local_interfaces;i;i=i->next) - if (same_net(i->ip,ip,i->nmask)) return i; + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) + n--; - return NULL; + if (i) return &i->bcast; + return NULL; } @@ -368,15 +365,12 @@ unsigned iface_hash(void) struct in_addr *iface_bcast(struct in_addr ip) { - struct interface *i = iface_find(ip); - return(i ? &i->bcast : &local_interfaces->bcast); + struct interface *i = iface_find(ip); + return(i ? &i->bcast : &local_interfaces->bcast); } struct in_addr *iface_ip(struct in_addr ip) { - struct interface *i = iface_find(ip); - return(i ? &i->ip : &local_interfaces->ip); + struct interface *i = iface_find(ip); + return(i ? &i->ip : &local_interfaces->ip); } - - - -- cgit From 814f37a5e3db3bcbf6478a6c5dd11aa27d6f7c6f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Apr 2000 11:15:27 +0000 Subject: we don't need fnmatch.c any more (This used to be commit e23f43e7d3d2a068f527baa63a31f7fe4e60e79d) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 9266427959..e535251756 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -109,7 +109,7 @@ static void interpret_interface(char *token) /* first check if it is an interface name */ for (i=0;i Date: Fri, 10 Nov 2000 19:02:32 +0000 Subject: Fix from John E. Malmberg for -1 return in interfaces scan. Jeremy. (This used to be commit 4d25a53c36ad2c33cc2ecaf1486e18f1536eff95) --- source3/lib/interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index e535251756..31ec846fdc 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -234,8 +234,8 @@ BOOL interfaces_changed(void) n = get_interfaces(ifaces, MAX_INTERFACES); - if (n != total_probed || - memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n)) { + if ((n > 0 )&& (n != total_probed || + memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) { return True; } -- cgit From a82df9c67367e0828afcc65b0635187c73e2813a Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Thu, 15 Feb 2001 19:50:34 +0000 Subject: samba/source/nmbd/nmbd.c change remote_machine name to nmbd instead of nmb so we write to same log file that was originally created as log.nmbd samba/source/smbd/server.c change remote_machine name to smbd instead of smb so we write to same log file that was originally created as log.smbd samba/source/lib/interface.c allow binding to all interface IP addresses even if on same subnet. This allows you to specify which IP's you want in interfaces line and use bind interfaces only (This used to be commit 01dfd59712f3730498784d7632da8fe0113d55b6) --- source3/lib/interface.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 31ec846fdc..3e45d627d3 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -41,13 +41,15 @@ static struct interface *local_interfaces = NULL; /**************************************************************************** Try and find an interface that matches an ip. If we cannot, return NULL **************************************************************************/ -static struct interface *iface_find(struct in_addr ip) +static struct interface *iface_find(struct in_addr ip, BOOL CheckMask) { struct interface *i; if (zero_ip(ip)) return local_interfaces; for (i=local_interfaces;i;i=i->next) - if (same_net(i->ip,ip,i->nmask)) return i; + if (CheckMask) { + if (same_net(i->ip,ip,i->nmask)) return i; + } else if ((i->ip).s_addr == ip.s_addr) return i; return NULL; } @@ -59,7 +61,7 @@ add an interface to the linked list of interfaces static void add_interface(struct in_addr ip, struct in_addr nmask) { struct interface *iface; - if (iface_find(ip)) { + if (iface_find(ip, False)) { DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); return; } @@ -365,12 +367,12 @@ unsigned iface_hash(void) struct in_addr *iface_bcast(struct in_addr ip) { - struct interface *i = iface_find(ip); + struct interface *i = iface_find(ip, True); return(i ? &i->bcast : &local_interfaces->bcast); } struct in_addr *iface_ip(struct in_addr ip) { - struct interface *i = iface_find(ip); + struct interface *i = iface_find(ip, True); return(i ? &i->ip : &local_interfaces->ip); } -- cgit From ef6c9d7425be907230eb533fbbe2e6ac150a0bbd Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 1 Jul 2001 23:24:08 +0000 Subject: "netbios aliases" and "interfaces" options change from P_STRING to P_LIST (This used to be commit db36ed1d80fcbee16d0a0b5f226e56961f3bf1ec) --- source3/lib/interface.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 3e45d627d3..48070c4446 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -168,8 +168,7 @@ load the list of network interfaces ****************************************************************************/ void load_interfaces(void) { - char *ptr; - fstring token; + char **ptr; int i; struct iface_struct ifaces[MAX_INTERFACES]; @@ -201,7 +200,7 @@ void load_interfaces(void) /* if we don't have a interfaces line then use all broadcast capable interfaces except loopback */ - if (!ptr || !*ptr) { + 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); @@ -216,8 +215,9 @@ void load_interfaces(void) return; } - while (next_token(&ptr,token,NULL,sizeof(token))) { - interpret_interface(token); + while (*ptr) { + interpret_interface(*ptr); + ptr++; } if (!local_interfaces) { -- cgit From 9b70d328cbe3a48cde2821db4293ee6a974ac86d Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 3 Jul 2001 00:54:55 +0000 Subject: - sorry, forgot to check a pointer (This used to be commit 4e0299d4c091bc4a63740f12588675507601e8cd) --- source3/lib/interface.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 48070c4446..e16afa45af 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -215,9 +215,11 @@ void load_interfaces(void) return; } - while (*ptr) { - interpret_interface(*ptr); - ptr++; + if (ptr) { + while (*ptr) { + interpret_interface(*ptr); + ptr++; + } } if (!local_interfaces) { -- cgit From 527e824293ee934ca5da0ef5424efe5ab7757248 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:36:09 +0000 Subject: strchr and strrchr are macros when compiling with optimisation in gcc, so we can't redefine them. damn. (This used to be commit c41fc06376d1a2b83690612304e85010b5e5f3cf) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index e16afa45af..269e0fa85b 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -120,7 +120,7 @@ static void interpret_interface(char *token) if (added) return; /* maybe it is a DNS name */ - p = strchr(token,'/'); + p = strchr_m(token,'/'); if (!p) { ip = *interpret_addr2(token); for (i=0;i Date: Mon, 20 Aug 2001 05:15:26 +0000 Subject: a bunch of fixes from the sflight to seattle in particular: - fixed NT status code for a bunch of ops - fixed handling of protocol levels in ms_fnmatch (This used to be commit 3eba9606f71f90bfd9820af26f8676277ed22390) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 269e0fa85b..c89c22aa08 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -111,7 +111,7 @@ static void interpret_interface(char *token) /* first check if it is an interface name */ for (i=0;i Date: Mon, 17 Sep 2001 02:19:44 +0000 Subject: move to SAFE_FREE() (This used to be commit 60e907b7e8e1c008463a88ed2b076344278986ef) --- source3/lib/interface.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index c89c22aa08..c68b405f7c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -178,17 +178,14 @@ void load_interfaces(void) allones_ip = *interpret_addr2("255.255.255.255"); loopback_ip = *interpret_addr2("127.0.0.1"); - if (probed_ifaces) { - free(probed_ifaces); - probed_ifaces = NULL; - } + 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); - free(iface); + SAFE_FREE(iface); } /* probe the kernel for interfaces */ -- cgit From dc1fc3ee8ec2199bc73bb5d7ec711c6800f61d65 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 2 Oct 2001 04:29:50 +0000 Subject: Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header. (This used to be commit 2d0922b0eabfdc0aaf1d0797482fef47ed7fde8e) --- source3/lib/interface.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index c68b405f7c..70ffcb0e1d 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -26,8 +26,6 @@ static struct iface_struct *probed_ifaces; static int total_probed; -extern int DEBUGLEVEL; - struct in_addr ipzero; struct in_addr allones_ip; struct in_addr loopback_ip; -- cgit From d2b9acd003f0fde8090a80c610280ae79ea7a706 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 23 Nov 2001 01:00:54 +0000 Subject: Don't initialise static pointers to NULL. (This used to be commit 039ea0a0b94be2d70164616f448c0e29fed071cf) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 70ffcb0e1d..d2e0b44fd4 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -30,7 +30,7 @@ struct in_addr ipzero; struct in_addr allones_ip; struct in_addr loopback_ip; -static struct interface *local_interfaces = NULL; +static struct interface *local_interfaces; #define ALLONES ((uint32)0xFFFFFFFF) #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES)) -- cgit From 585d0efbc6428e5876d354fee49c241c1bad809d Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Mon, 26 Nov 2001 03:11:44 +0000 Subject: Got medieval on another pointless extern. Removed extern struct ipzero and replaced with two functions: void zero_ip(struct in_adder *ip); BOOL is_zero_ip(struct in_addr ip); (This used to be commit 778f5f77a66cda76348a7c6f64cd63afe2bfe077) --- source3/lib/interface.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index d2e0b44fd4..a93390e643 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -26,7 +26,6 @@ static struct iface_struct *probed_ifaces; static int total_probed; -struct in_addr ipzero; struct in_addr allones_ip; struct in_addr loopback_ip; @@ -42,7 +41,7 @@ Try and find an interface that matches an ip. If we cannot, return NULL static struct interface *iface_find(struct in_addr ip, BOOL CheckMask) { struct interface *i; - if (zero_ip(ip)) return local_interfaces; + if (is_zero_ip(ip)) return local_interfaces; for (i=local_interfaces;i;i=i->next) if (CheckMask) { @@ -104,8 +103,8 @@ static void interpret_interface(char *token) char *p; int i, added=0; - ip = ipzero; - nmask = ipzero; + zero_ip(&ip); + zero_ip(&nmask); /* first check if it is an interface name */ for (i=0;i Date: Wed, 28 Nov 2001 21:51:11 +0000 Subject: merge from APPLIANCE_HEAD (This used to be commit c60aa6c06f376684b6d6d9a2c14305ca9f4657ef) --- source3/lib/interface.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index a93390e643..08636fa306 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -21,8 +21,6 @@ #include "includes.h" -#define MAX_INTERFACES 128 - static struct iface_struct *probed_ifaces; static int total_probed; -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/interface.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 08636fa306..d43001342e 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -1,6 +1,5 @@ /* - Unix SMB/Netbios implementation. - Version 1.9. + Unix SMB/CIFS implementation. multiple interface handling Copyright (C) Andrew Tridgell 1992-1998 -- cgit From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/interface.c | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index d43001342e..0d751a9c7c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -275,19 +275,6 @@ int iface_count(void) return ret; } -/**************************************************************************** - True if we have two or more interfaces. - **************************************************************************/ -BOOL we_are_multihomed(void) -{ - static int multi = -1; - - if(multi == -1) - multi = (iface_count() > 1 ? True : False); - - return multi; -} - /**************************************************************************** return the Nth interface **************************************************************************/ @@ -331,40 +318,21 @@ struct in_addr *iface_n_bcast(int n) } -/**************************************************************************** -this function provides a simple hash of the configured interfaces. It is -used to detect a change in interfaces to tell us whether to discard -the current wins.dat file. -Note that the result is independent of the order of the interfaces - **************************************************************************/ -unsigned iface_hash(void) -{ - unsigned ret = 0; - struct interface *i; - - for (i=local_interfaces;i;i=i->next) { - unsigned x1 = (unsigned)str_checksum(inet_ntoa(i->ip)); - unsigned x2 = (unsigned)str_checksum(inet_ntoa(i->nmask)); - ret ^= (x1 ^ x2); - } - - return ret; -} - - /* 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 in_addr *iface_bcast(struct in_addr ip) +struct in_addr *iface_ip(struct in_addr ip) { struct interface *i = iface_find(ip, True); - return(i ? &i->bcast : &local_interfaces->bcast); + return(i ? &i->ip : &local_interfaces->ip); } -struct in_addr *iface_ip(struct in_addr ip) +/* + return True if a IP is directly reachable on one of our interfaces +*/ +BOOL iface_local(struct in_addr ip) { - struct interface *i = iface_find(ip, True); - return(i ? &i->ip : &local_interfaces->ip); + return iface_find(ip, True) ? True : False; } -- cgit From 2f194322d419350f35a48dff750066894d68eccf Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 12 Nov 2002 23:20:50 +0000 Subject: Removed global_myworkgroup, global_myname, global_myscope. Added liberal dashes of const. This is a rather large check-in, some things may break. It does compile though :-). Jeremy. (This used to be commit f755711df8f74f9b8e8c1a2b0d07d02a931eeb89) --- source3/lib/interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 0d751a9c7c..4d8010e31b 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -94,7 +94,7 @@ This handles the following different forms: 4) ip/mask 5) bcast/mask ****************************************************************************/ -static void interpret_interface(char *token) +static void interpret_interface(const char *token) { struct in_addr ip, nmask; char *p; @@ -162,7 +162,7 @@ load the list of network interfaces ****************************************************************************/ void load_interfaces(void) { - char **ptr; + const char **ptr; int i; struct iface_struct ifaces[MAX_INTERFACES]; -- cgit From af5750d3ba908225a79f55f2b0de1092e5ad6734 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 24 Jun 2004 23:27:00 +0000 Subject: r1248: Fix from Nick Wellnhofer to prevent lp_interfaces() list from being corrupted. Jeremy. (This used to be commit c892545960a9c3206b5a1f73e98ea924c802c17c) --- source3/lib/interface.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 4d8010e31b..adf9ca3438 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -94,7 +94,7 @@ This handles the following different forms: 4) ip/mask 5) bcast/mask ****************************************************************************/ -static void interpret_interface(const char *token) +static void interpret_interface(char *token) { struct in_addr ip, nmask; char *p; @@ -130,9 +130,9 @@ static void interpret_interface(const char *token) } /* parse it into an IP address/netmasklength pair */ - *p++ = 0; - + *p = 0; ip = *interpret_addr2(token); + *p++ = '/'; if (strlen(p) > 2) { nmask = *interpret_addr2(p); @@ -207,7 +207,11 @@ void load_interfaces(void) if (ptr) { while (*ptr) { - interpret_interface(*ptr); + char *ptr_cpy = strdup(*ptr); + if (ptr_cpy) { + interpret_interface(ptr_cpy); + free(ptr_cpy); + } ptr++; } } -- cgit From acf9d61421faa6c0055d57fdee7db300dc5431aa Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index adf9ca3438..8cf11b8503 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -65,7 +65,7 @@ static void add_interface(struct in_addr ip, struct in_addr nmask) return; } - iface = (struct interface *)malloc(sizeof(*iface)); + iface = SMB_MALLOC_P(struct interface); if (!iface) return; ZERO_STRUCTPN(iface); @@ -207,7 +207,7 @@ void load_interfaces(void) if (ptr) { while (*ptr) { - char *ptr_cpy = strdup(*ptr); + char *ptr_cpy = SMB_STRDUP(*ptr); if (ptr_cpy) { interpret_interface(ptr_cpy); free(ptr_cpy); -- cgit From 92bbd3151d2b72ebeef8b79b86b5eab84fad3898 Mon Sep 17 00:00:00 2001 From: Jim McDonough Date: Tue, 1 Feb 2005 19:32:54 +0000 Subject: r5163: Fix bugzilla 2062: turn off broadcast for all 390 NICs. (This used to be commit d159a5013e96a1188599a3fa0bff108fa6f6679b) --- source3/lib/interface.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 8cf11b8503..2bd7d6ddbe 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -60,10 +60,12 @@ static void add_interface(struct in_addr ip, struct in_addr nmask) return; } +#if !defined(__s390__) if (ip_equal(nmask, allones_ip)) { DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip))); return; } +#endif iface = SMB_MALLOC_P(struct interface); if (!iface) return; @@ -196,7 +198,10 @@ void load_interfaces(void) exit(1); } for (i=0;i Date: Wed, 28 Jun 2006 00:50:14 +0000 Subject: r16582: Fix Klocwork #1997 and all generic class of problems where we don't correctly check the return from memdup. Jeremy. (This used to be commit ce14daf51c7ee2f9c68c77f7f4674e6f0e35c9ca) --- source3/lib/interface.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 2bd7d6ddbe..dea01c6011 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -188,6 +188,10 @@ void load_interfaces(void) if (total_probed > 0) { probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed); + if (!probed_ifaces) { + DEBUG(0,("ERROR: memdup failed\n")); + exit(1); + } } /* if we don't have a interfaces line then use all broadcast capable -- cgit From 1cf1e648feed823244731eef5f56bd34e15cb045 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 31 Jul 2006 04:30:55 +0000 Subject: r17334: Some C++ warnings (This used to be commit 8ae7ed1f3cecbb5285313d17b5f9511e2e622f0b) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index dea01c6011..9d0b966390 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -187,7 +187,7 @@ void load_interfaces(void) total_probed = get_interfaces(ifaces, MAX_INTERFACES); if (total_probed > 0) { - probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed); + probed_ifaces = (struct iface_struct *)memdup(ifaces, sizeof(ifaces[0])*total_probed); if (!probed_ifaces) { DEBUG(0,("ERROR: memdup failed\n")); exit(1); -- cgit From 761f60f83a81103c4c6e39b6460b9d7062a85b5f Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Fri, 16 Mar 2007 14:13:46 +0000 Subject: r21854: Add gfree_interfaces() to gfree_all(). Guenther (This used to be commit eb34ebd9e76061417200a286c2831394be04529b) --- source3/lib/interface.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 9d0b966390..e2c9294b28 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -231,6 +231,18 @@ void load_interfaces(void) } +void gfree_interfaces(void) +{ + while (local_interfaces) { + struct interface *iface = local_interfaces; + DLIST_REMOVE(local_interfaces, local_interfaces); + ZERO_STRUCTPN(iface); + SAFE_FREE(iface); + } + + SAFE_FREE(probed_ifaces); +} + /**************************************************************************** return True if the list of probed interfaces has changed ****************************************************************************/ -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index e2c9294b28..664046cf6f 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -5,7 +5,7 @@ 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 - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/interface.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 664046cf6f..29ed15a0c2 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -14,8 +14,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 3fbd1ae54ced2eb889a8fe0a6ea32dfd8175f941 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 2 Oct 2007 19:27:25 +0000 Subject: r25472: Fix the interfaces code to detect IPv6 interfaces, using the new standard getifaddrs() and freeifaddrs() interfaces. Currently we only return IPv4 af_families. Needs fixing for binds to IPv6 but this has to be careful work. Jeremy. (This used to be commit 327875182c9219aeba687e10aaea93546d9a70ea) --- source3/lib/interface.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 29ed15a0c2..c187583923 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -103,12 +103,12 @@ static void interpret_interface(char *token) zero_ip(&ip); zero_ip(&nmask); - + /* first check if it is an interface name */ for (i=0;i Date: Thu, 4 Oct 2007 18:23:22 +0000 Subject: r25508: Reformatting. Jeremy. (This used to be commit b412ba6fb3e526f5cd83e6cabd952ad7c303c346) --- source3/lib/interface.c | 124 +++++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 55 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index c187583923..5982d82e47 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -1,18 +1,18 @@ -/* +/* Unix SMB/CIFS implementation. multiple interface handling Copyright (C) Andrew Tridgell 1992-1998 - + 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 the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -32,8 +32,9 @@ static struct interface *local_interfaces; #define MKNETADDR(_IP, _NM) (_IP & _NM) /**************************************************************************** -Try and find an interface that matches an ip. If we cannot, return NULL - **************************************************************************/ + Try and find an interface that matches an ip. If we cannot, return NULL. +**************************************************************************/ + static struct interface *iface_find(struct in_addr ip, BOOL CheckMask) { struct interface *i; @@ -47,10 +48,10 @@ static struct interface *iface_find(struct in_addr ip, BOOL CheckMask) return NULL; } - /**************************************************************************** -add an interface to the linked list of interfaces + Add an interface to the linked list of interfaces. ****************************************************************************/ + static void add_interface(struct in_addr ip, struct in_addr nmask) { struct interface *iface; @@ -61,14 +62,15 @@ static void add_interface(struct in_addr ip, struct in_addr nmask) #if !defined(__s390__) if (ip_equal(nmask, allones_ip)) { - DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip))); + DEBUG(3,("not adding non-broadcast interface %s\n", + inet_ntoa(ip))); return; } #endif iface = SMB_MALLOC_P(struct interface); if (!iface) return; - + ZERO_STRUCTPN(iface); iface->ip = ip; @@ -79,22 +81,21 @@ static void add_interface(struct in_addr ip, struct in_addr nmask) DEBUG(2,("added interface ip=%s ",inet_ntoa(iface->ip))); DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); - DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); + DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); } - - /**************************************************************************** -interpret a single element from a interfaces= config line + Interpret a single element from a interfaces= config line. -This handles the following different forms: + This handles the following different forms: -1) wildcard interface name -2) DNS name -3) IP/masklen -4) ip/mask -5) bcast/mask + 1) wildcard interface name + 2) DNS name + 3) IP/masklen + 4) ip/mask + 5) bcast/mask ****************************************************************************/ + static void interpret_interface(char *token) { struct in_addr ip, nmask; @@ -119,11 +120,11 @@ static void interpret_interface(char *token) if (!p) { ip = *interpret_addr2(token); for (i=0;i 0) { - probed_ifaces = (struct iface_struct *)memdup(ifaces, sizeof(ifaces[0])*total_probed); + probed_ifaces = (struct iface_struct *)memdup(ifaces, + sizeof(ifaces[0])*total_probed); if (!probed_ifaces) { DEBUG(0,("ERROR: memdup failed\n")); exit(1); } } - /* if we don't have a interfaces line then use all broadcast capable + /* 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")); + DEBUG(0,("ERROR: Could not determine network " + "interfaces, you must use a interfaces config line\n")); exit(1); } for (i=0;inext) { - if((from.s_addr & i->nmask.s_addr) == + if((from.s_addr & i->nmask.s_addr) == (i->ip.s_addr & i->nmask.s_addr)) return True; } @@ -288,8 +298,9 @@ BOOL is_local_net(struct in_addr from) } /**************************************************************************** - how many interfaces do we have - **************************************************************************/ + How many interfaces do we have +**************************************************************************/ + int iface_count(void) { int ret = 0; @@ -301,12 +312,13 @@ int iface_count(void) } /**************************************************************************** - return the Nth interface - **************************************************************************/ + Return the Nth interface. +**************************************************************************/ + struct interface *get_interface(int n) -{ +{ struct interface *i; - + for (i=local_interfaces;i && n;i=i->next) n--; @@ -315,12 +327,13 @@ struct interface *get_interface(int n) } /**************************************************************************** - return IP of the Nth interface - **************************************************************************/ + Return IP of the Nth interface. +**************************************************************************/ + struct in_addr *iface_n_ip(int n) { struct interface *i; - + for (i=local_interfaces;i && n;i=i->next) n--; @@ -329,12 +342,13 @@ struct in_addr *iface_n_ip(int n) } /**************************************************************************** - return bcast of the Nth interface - **************************************************************************/ + Return bcast of the Nth interface. +**************************************************************************/ + struct in_addr *iface_n_bcast(int n) { struct interface *i; - + for (i=local_interfaces;i && n;i=i->next) n--; @@ -342,7 +356,6 @@ struct in_addr *iface_n_bcast(int n) 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 @@ -357,6 +370,7 @@ struct in_addr *iface_ip(struct in_addr ip) /* return True if a IP is directly reachable on one of our interfaces */ + BOOL iface_local(struct in_addr ip) { return iface_find(ip, True) ? True : False; -- cgit From 8e54530b52fd256137740107e9fdf000f00a7a30 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 10 Oct 2007 18:25:16 -0700 Subject: Add start of IPv6 implementation. Currently most of this is avoiding IPv6 in winbindd, but moves most of the socket functions that were wrongly in lib/util.c into lib/util_sock.c and provides generic IPv4/6 independent versions of most things. Still lots of work to do, but now I can see how I'll fix the access check code. Nasty part that remains is the name resolution code which is used to returning arrays of in_addr structs. Jeremy. (This used to be commit 3f6bd0e1ec5cc6670f3d08f76fc2cd94c9cd1a08) --- source3/lib/interface.c | 644 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 459 insertions(+), 185 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 5982d82e47..1471a06f46 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -2,6 +2,7 @@ Unix SMB/CIFS implementation. multiple interface handling Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Jeremy Allison 2007 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 @@ -22,66 +23,404 @@ static struct iface_struct *probed_ifaces; static int total_probed; -struct in_addr allones_ip; -struct in_addr loopback_ip; - static struct interface *local_interfaces; -#define ALLONES ((uint32)0xFFFFFFFF) -#define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES)) -#define MKNETADDR(_IP, _NM) (_IP & _NM) +/**************************************************************************** + Check if an IP is one of mine. +**************************************************************************/ + +bool ismyaddr(const struct sockaddr_storage *ip) +{ + struct interface *i; + for (i=local_interfaces;i;i=i->next) { + if (addr_equal(&i->ip,ip)) { + return true; + } + } + return false; +} + +bool ismyip_v4(struct in_addr ip) +{ + struct sockaddr_storage ss; + in_addr_to_sockaddr_storage(&ss, ip); + return ismyaddr(&ss); +} /**************************************************************************** Try and find an interface that matches an ip. If we cannot, return NULL. **************************************************************************/ -static struct interface *iface_find(struct in_addr ip, BOOL CheckMask) +static struct interface *iface_find(const struct sockaddr_storage *ip, + bool check_mask) { struct interface *i; - if (is_zero_ip(ip)) return local_interfaces; - for (i=local_interfaces;i;i=i->next) - if (CheckMask) { - if (same_net(i->ip,ip,i->nmask)) return i; - } else if ((i->ip).s_addr == ip.s_addr) return i; + if (is_address_any(ip)) { + return local_interfaces; + } + + for (i=local_interfaces;i;i=i->next) { + if (check_mask) { + if (same_net(ip, &i->ip, &i->netmask)) { + return i; + } + } else if (addr_equal(&i->ip, ip)) { + return i; + } + } return NULL; } +/**************************************************************************** + Check if a packet is from a local (known) net. +**************************************************************************/ + +bool is_local_net(const struct sockaddr_storage *from) +{ + struct interface *i; + for (i=local_interfaces;i;i=i->next) { + if (same_net(from, &i->ip, &i->netmask)) { + return true; + } + } + return false; +} + +/**************************************************************************** + Check if a packet is from a local (known) net. +**************************************************************************/ + +bool is_local_net_v4(struct in_addr from) +{ + struct sockaddr_storage ss; + + in_addr_to_sockaddr_storage(&ss, from); + return is_local_net(&ss); +} + +/**************************************************************************** + How many interfaces do we have ? +**************************************************************************/ + +int iface_count(void) +{ + int ret = 0; + struct interface *i; + + for (i=local_interfaces;i;i=i->next) { + ret++; + } + return ret; +} + +/**************************************************************************** + How many interfaces do we have (v4 only) ? +**************************************************************************/ + +int iface_count_v4(void) +{ + int ret = 0; + struct interface *i; + + for (i=local_interfaces;i;i=i->next) { + if (i->ip.ss_family == AF_INET) { + ret++; + } + } + return ret; +} + +/**************************************************************************** + Return a pointer to the in_addr of the first IPv4 interface. +**************************************************************************/ + +const struct in_addr *first_ipv4_iface(void) +{ + struct interface *i; + + for (i=local_interfaces;i ;i=i->next) { + if (i->ip.ss_family == AF_INET) { + break; + } + } + + if (!i) { + return NULL; + } + return &((const struct sockaddr_in *)&i->ip)->sin_addr; +} + +/**************************************************************************** + Return the Nth interface. +**************************************************************************/ + +struct interface *get_interface(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) { + n--; + } + + if (i) { + return i; + } + return NULL; +} + +/**************************************************************************** + Return IP sockaddr_storage of the Nth interface. +**************************************************************************/ + +const struct sockaddr_storage *iface_n_sockaddr_storage(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) { + n--; + } + + if (i) { + return &i->ip; + } + return NULL; +} + +/**************************************************************************** + Return IPv4 of the Nth interface (if a v4 address). NULL otherwise. +**************************************************************************/ + +const struct in_addr *iface_n_ip_v4(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) { + n--; + } + + if (i && i->ip.ss_family == AF_INET) { + return &((const struct sockaddr_in *)&i->ip)->sin_addr; + } + return NULL; +} + +/**************************************************************************** + Return IPv4 bcast of the Nth interface (if a v4 address). NULL otherwise. +**************************************************************************/ + +const struct in_addr *iface_n_bcast_v4(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) { + n--; + } + + if (i && i->ip.ss_family == AF_INET) { + return &((const struct sockaddr_in *)&i->bcast)->sin_addr; + } + return NULL; +} + +/**************************************************************************** + Return bcast of the Nth interface. +**************************************************************************/ + +const struct sockaddr_storage *iface_n_bcast(int n) +{ + struct interface *i; + + for (i=local_interfaces;i && n;i=i->next) { + n--; + } + + if (i) { + return &i->bcast; + } + 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. */ + +const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip) +{ + struct interface *i = iface_find(ip, true); + if (i) { + return &i->ip; + } + + /* Search for the first interface with + * matching address family. */ + + for (i=local_interfaces;i;i=i->next) { + if (i->ip.ss_family == ip->ss_family) { + return &i->ip; + } + } + return NULL; +} + +/* + return True if a IP is directly reachable on one of our interfaces +*/ + +bool iface_local(struct sockaddr_storage *ip) +{ + return iface_find(ip, True) ? true : false; +} + /**************************************************************************** Add an interface to the linked list of interfaces. ****************************************************************************/ -static void add_interface(struct in_addr ip, struct in_addr nmask) +static void add_interface(const struct iface_struct *ifs) { + char addr[INET6_ADDRSTRLEN]; struct interface *iface; - if (iface_find(ip, False)) { - DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); + + if (iface_find(&ifs->ip, False)) { + DEBUG(3,("add_interface: not adding duplicate interface %s\n", + print_sockaddr(addr, sizeof(addr), + &ifs->ip, sizeof(struct sockaddr_storage)) )); return; } -#if !defined(__s390__) - if (ip_equal(nmask, allones_ip)) { + if (!(ifs->flags & IFF_BROADCAST)) { DEBUG(3,("not adding non-broadcast interface %s\n", - inet_ntoa(ip))); + ifs->name )); return; } -#endif iface = SMB_MALLOC_P(struct interface); - if (!iface) return; + if (!iface) { + return; + } ZERO_STRUCTPN(iface); - iface->ip = ip; - iface->nmask = nmask; - iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); + iface->name = SMB_STRDUP(ifs->name); + if (!iface->name) { + SAFE_FREE(iface); + return; + } + iface->flags = ifs->flags; + iface->ip = ifs->ip; + iface->netmask = ifs->netmask; + iface->bcast = ifs->bcast; DLIST_ADD(local_interfaces, iface); - DEBUG(2,("added interface ip=%s ",inet_ntoa(iface->ip))); - DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); - DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); + DEBUG(2,("added interface %s ip=%s ", + iface->name, + print_sockaddr(addr, sizeof(addr), + &iface->ip, sizeof(struct sockaddr_storage)) )); + DEBUG(2,("bcast=%s ", + print_sockaddr(addr, sizeof(addr), + &iface->bcast, + sizeof(struct sockaddr_storage)) )); + DEBUG(2,("netmask=%s\n", + print_sockaddr(addr, sizeof(addr), + &iface->netmask, + sizeof(struct sockaddr_storage)) )); +} + +/**************************************************************************** + Create a struct sockaddr_storage with the netmask bits set to 1. +****************************************************************************/ + +static bool make_netmask(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + unsigned long masklen) +{ + *pss_out = *pss_in; + /* Now apply masklen bits of mask. */ +#if defined(AF_INET6) + if (pss_in->ss_family == AF_INET6) { + char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; + unsigned int i; + + if (masklen > 128) { + return false; + } + for (i = 0; masklen >= 8; masklen -= 8, i++) { + *p++ = 0xff; + } + /* Deal with the partial byte. */ + *p++ &= (0xff & ~(0xff>>masklen)); + i++; + for (;i < sizeof(struct in6_addr); i++) { + *p++ = '\0'; + } + return true; + } +#endif + if (pss_in->ss_family == AF_INET) { + if (masklen > 32) { + return false; + } + ((struct sockaddr_in *)pss_out)->sin_addr.s_addr = + htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL)); + return true; + } + return false; +} + +/**************************************************************************** + Create a struct sockaddr_storage set to the broadcast or network adress from + an incoming sockaddr_storage. +****************************************************************************/ + +static void make_bcast_or_net(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask, + bool make_bcast) +{ + unsigned int i = 0, len = 0; + char *pmask = NULL; + char *p = NULL; + *pss_out = *pss_in; + + /* Set all zero netmask bits to 1. */ +#if defined(AF_INET6) + if (pss_in->ss_family == AF_INET6) { + p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; + pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr; + len = 16; + } +#endif + if (pss_in->ss_family == AF_INET) { + p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr; + pmask = (char *)&((struct sockaddr_in *)nmask)->sin_addr; + len = 4; + } + + for (i = 0; i < len; i++, p++, pmask++) { + if (make_bcast) { + *p = (*p & *pmask) | (*pmask ^ 0xff); + } else { + /* make_net */ + *p = (*p & *pmask); + } + } +} + +static void make_bcast(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask) +{ + make_bcast_or_net(pss_out, pss_in, nmask, true); +} + +static void make_net(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask) +{ + make_bcast_or_net(pss_out, pss_in, nmask, false); } /**************************************************************************** @@ -98,68 +437,121 @@ static void add_interface(struct in_addr ip, struct in_addr nmask) static void interpret_interface(char *token) { - struct in_addr ip, nmask; + struct sockaddr_storage ss; + struct sockaddr_storage ss_mask; + struct sockaddr_storage ss_net; + struct sockaddr_storage ss_bcast; + struct iface_struct ifs; char *p; - int i, added=0; - - zero_ip(&ip); - zero_ip(&nmask); + int i; + bool added=false; + bool goodaddr = false; /* first check if it is an interface name */ for (i=0;i 2) { - nmask = *interpret_addr2(p); + goodaddr = interpret_string_addr(&ss_mask, p); + if (!goodaddr) { + DEBUG(2,("interpret_interface: " + "can't determine netmask from %s\n", + p)); + return; + } } else { - nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); + char *endp = NULL; + unsigned long val = strtoul(p, &endp, 0); + if (p == endp || (endp && *endp != '\0')) { + DEBUG(2,("interpret_interface: " + "can't determine netmask value from %s\n", + p)); + return; + } + if (!make_netmask(&ss_mask, &ss, val)) { + DEBUG(2,("interpret_interface: " + "can't apply netmask value %lu from %s\n", + val, + p)); + return; + } } - /* maybe the first component was a broadcast address */ - if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) || - ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) { + make_bcast(&ss_bcast, &ss, &ss_mask); + make_net(&ss_net, &ss, &ss_mask); + + /* Maybe the first component was a broadcast address. */ + if (addr_equal(&ss_bcast, &ss) || addr_equal(&ss_net, &ss)) { for (i=0;iname); SAFE_FREE(iface); } - /* probe the kernel for interfaces */ + /* Probe the kernel for interfaces */ total_probed = get_interfaces(ifaces, MAX_INTERFACES); if (total_probed > 0) { @@ -208,15 +595,8 @@ void load_interfaces(void) exit(1); } for (i=0;iname); SAFE_FREE(iface); } @@ -255,7 +635,7 @@ void gfree_interfaces(void) Return True if the list of probed interfaces has changed. ****************************************************************************/ -BOOL interfaces_changed(void) +bool interfaces_changed(void) { int n; struct iface_struct ifaces[MAX_INTERFACES]; @@ -263,115 +643,9 @@ BOOL interfaces_changed(void) 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 in_addr ip) -{ - struct interface *i; - for (i=local_interfaces;i;i=i->next) - if (ip_equal(i->ip,ip)) return True; - return False; -} - -/**************************************************************************** - Check if a packet is from a local (known) net. -**************************************************************************/ - -BOOL is_local_net(struct in_addr from) -{ - struct interface *i; - for (i=local_interfaces;i;i=i->next) { - if((from.s_addr & i->nmask.s_addr) == - (i->ip.s_addr & i->nmask.s_addr)) - return True; + memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) { + return true; } - return False; -} - -/**************************************************************************** - How many interfaces do we have -**************************************************************************/ - -int iface_count(void) -{ - int ret = 0; - struct interface *i; - - for (i=local_interfaces;i;i=i->next) - ret++; - return ret; -} - -/**************************************************************************** - Return the Nth interface. -**************************************************************************/ - -struct interface *get_interface(int n) -{ - struct interface *i; - - for (i=local_interfaces;i && n;i=i->next) - n--; - - if (i) return i; - return NULL; -} - -/**************************************************************************** - Return IP of the Nth interface. -**************************************************************************/ - -struct in_addr *iface_n_ip(int n) -{ - struct interface *i; - - for (i=local_interfaces;i && n;i=i->next) - n--; - - if (i) return &i->ip; - return NULL; -} - -/**************************************************************************** - Return bcast of the Nth interface. -**************************************************************************/ - -struct in_addr *iface_n_bcast(int n) -{ - struct interface *i; - - for (i=local_interfaces;i && n;i=i->next) - n--; - - if (i) return &i->bcast; - 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 in_addr *iface_ip(struct in_addr ip) -{ - struct interface *i = iface_find(ip, True); - return(i ? &i->ip : &local_interfaces->ip); -} - -/* - return True if a IP is directly reachable on one of our interfaces -*/ - -BOOL iface_local(struct in_addr ip) -{ - return iface_find(ip, True) ? True : False; + return false; } -- cgit From 3d89bad41f64906f42616b50170f3471003139f7 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 13 Oct 2007 16:27:52 +0200 Subject: Fix a segfault with an unknown interface "interfaces = foo" with "foo" not being a known interface segfaulted for me. (This used to be commit 556c33702ce6d6c7cde43ddfe965c78ebc2963d3) --- source3/lib/interface.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 1471a06f46..2eaadab0ec 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -460,7 +460,13 @@ static void interpret_interface(char *token) /* maybe it is a DNS name */ p = strchr_m(token,'/'); - if (!p && interpret_string_addr(&ss, token)) { + if (p == NULL) { + if (!interpret_string_addr(&ss, token)) { + DEBUG(2, ("interpret_interface: Can't find address " + "for %s\n", token)); + return; + } + for (i=0;i Date: Sun, 14 Oct 2007 17:17:07 +0200 Subject: Fix one more place for IFF_LOOPBACK (This used to be commit add1294562b76c38d5e471f280ca1167b4d8a93d) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 2eaadab0ec..ded70683e0 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -290,7 +290,7 @@ static void add_interface(const struct iface_struct *ifs) return; } - if (!(ifs->flags & IFF_BROADCAST)) { + if (!(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) { DEBUG(3,("not adding non-broadcast interface %s\n", ifs->name )); return; -- cgit From 666f50b01f282e520c59b94944d4b1583168d46a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 15 Oct 2007 16:11:48 -0700 Subject: Move to protocol independent code in most of lib/util_sock.c We don't use gethostbyname any more except in one case where we're looking for host aliases (I don't know how to do that with getaddrinfo yet). New function should be getaddrinfo(). Next step will be fixing lib/access.c, and then changing libsmb/namequery.c to cope with IPv6 address returns. Jeremy. (This used to be commit 4a56b697b6adcf095e25895c4a9ba3192ed34124) --- source3/lib/interface.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index ded70683e0..d2aa69a289 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -461,7 +461,7 @@ static void interpret_interface(char *token) /* maybe it is a DNS name */ p = strchr_m(token,'/'); if (p == NULL) { - if (!interpret_string_addr(&ss, token)) { + if (!interpret_string_addr(&ss, token, 0)) { DEBUG(2, ("interpret_interface: Can't find address " "for %s\n", token)); return; @@ -481,7 +481,7 @@ static void interpret_interface(char *token) /* parse it into an IP address/netmasklength pair */ *p = 0; - goodaddr = interpret_string_addr(&ss, token); + goodaddr = interpret_string_addr(&ss, token, 0); *p++ = '/'; if (!goodaddr) { @@ -492,7 +492,7 @@ static void interpret_interface(char *token) } if (strlen(p) > 2) { - goodaddr = interpret_string_addr(&ss_mask, p); + goodaddr = interpret_string_addr(&ss_mask, p, 0); if (!goodaddr) { DEBUG(2,("interpret_interface: " "can't determine netmask from %s\n", -- cgit From ce77126e68df65abc063de0ccdaec22cc746203d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 16 Oct 2007 11:33:37 -0700 Subject: Fix access control code to be IPv6/v4 protocol independent. Make unix_wild_match() talloc, not pstring based. Next will be name resolution code, and client code. Jeremy. (This used to be commit f6a01b82c5a47957659df08ea84e335dfbba1826) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index d2aa69a289..4892ec1248 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -333,7 +333,7 @@ static void add_interface(const struct iface_struct *ifs) Create a struct sockaddr_storage with the netmask bits set to 1. ****************************************************************************/ -static bool make_netmask(struct sockaddr_storage *pss_out, +bool make_netmask(struct sockaddr_storage *pss_out, const struct sockaddr_storage *pss_in, unsigned long masklen) { -- cgit From 80b7bcb5fbc98964014b5c8082a626aef24c5feb Mon Sep 17 00:00:00 2001 From: "Gerald (Jerry) Carter" Date: Fri, 19 Oct 2007 08:14:12 -0500 Subject: Add test for "struct in6_addr" to the HAVE_IPV6 configure test. Also make use of "if defined(HAVE_IPV6)" rather than testing for AF_INET6 since this is not sufficient on HP-UX 11.11 to ensure a working IPv6 implementation. (This used to be commit 620785df4e57b72471ff0315e22e0d2f28a2b1a5) --- source3/lib/interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 4892ec1248..0696329fd6 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -339,7 +339,7 @@ bool make_netmask(struct sockaddr_storage *pss_out, { *pss_out = *pss_in; /* Now apply masklen bits of mask. */ -#if defined(AF_INET6) +#if defined(HAVE_IPV6) if (pss_in->ss_family == AF_INET6) { char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; unsigned int i; @@ -386,7 +386,7 @@ static void make_bcast_or_net(struct sockaddr_storage *pss_out, *pss_out = *pss_in; /* Set all zero netmask bits to 1. */ -#if defined(AF_INET6) +#if defined(HAVE_IPV6) if (pss_in->ss_family == AF_INET6) { p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr; -- cgit From f88b7a076be74a29a3bf876b4e2705f4a1ecf42b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 24 Oct 2007 14:16:54 -0700 Subject: This is a large patch (sorry). Migrate from struct in_addr to struct sockaddr_storage in most places that matter (ie. not the nmbd and NetBIOS lookups). This passes make test on an IPv4 box, but I'll have to do more work/testing on IPv6 enabled boxes. This should now give us a framework for testing and finishing the IPv6 migration. It's at the state where someone with a working IPv6 setup should (theorecically) be able to type : smbclient //ipv6-address/share and have it work. Jeremy. (This used to be commit 98e154c3125d5732c37a72d74b0eb5cd7b6155fd) --- source3/lib/interface.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 0696329fd6..49bbceceb7 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -269,7 +269,7 @@ const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip) return True if a IP is directly reachable on one of our interfaces */ -bool iface_local(struct sockaddr_storage *ip) +bool iface_local(const struct sockaddr_storage *ip) { return iface_find(ip, True) ? true : false; } @@ -285,8 +285,7 @@ static void add_interface(const struct iface_struct *ifs) if (iface_find(&ifs->ip, False)) { DEBUG(3,("add_interface: not adding duplicate interface %s\n", - print_sockaddr(addr, sizeof(addr), - &ifs->ip, sizeof(struct sockaddr_storage)) )); + print_sockaddr(addr, sizeof(addr), &ifs->ip) )); return; } @@ -317,16 +316,13 @@ static void add_interface(const struct iface_struct *ifs) DEBUG(2,("added interface %s ip=%s ", iface->name, - print_sockaddr(addr, sizeof(addr), - &iface->ip, sizeof(struct sockaddr_storage)) )); + print_sockaddr(addr, sizeof(addr), &iface->ip) )); DEBUG(2,("bcast=%s ", print_sockaddr(addr, sizeof(addr), - &iface->bcast, - sizeof(struct sockaddr_storage)) )); + &iface->bcast) )); DEBUG(2,("netmask=%s\n", print_sockaddr(addr, sizeof(addr), - &iface->netmask, - sizeof(struct sockaddr_storage)) )); + &iface->netmask) )); } /**************************************************************************** -- cgit From 6128d116b3f09ce0b055d2df89b2f7282185782e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 25 Oct 2007 18:28:36 -0700 Subject: Fix resolve name to resolve IPv6 addresses of link-local%ifaddr Jeremy. (This used to be commit e6609cab732d5cd5cc9a5ae50aee15147f2ec6ec) --- source3/lib/interface.c | 117 ++++++++++-------------------------------------- 1 file changed, 23 insertions(+), 94 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 49bbceceb7..9d073bc08c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -88,6 +88,29 @@ bool is_local_net(const struct sockaddr_storage *from) return false; } +#if defined(HAVE_IPV6) +void setup_linklocal_scope_id(struct sockaddr_storage *pss) +{ + struct interface *i; + for (i=local_interfaces;i;i=i->next) { + if (addr_equal(&i->ip,pss)) { + struct sockaddr_in6 *psa6 = + (struct sockaddr_in6 *)pss; + psa6->sin6_scope_id = if_nametoindex(i->name); + return; + } + } + for (i=local_interfaces;i;i=i->next) { + if (same_net(pss, &i->ip, &i->netmask)) { + struct sockaddr_in6 *psa6 = + (struct sockaddr_in6 *)pss; + psa6->sin6_scope_id = if_nametoindex(i->name); + return; + } + } +} +#endif + /**************************************************************************** Check if a packet is from a local (known) net. **************************************************************************/ @@ -325,100 +348,6 @@ static void add_interface(const struct iface_struct *ifs) &iface->netmask) )); } -/**************************************************************************** - Create a struct sockaddr_storage with the netmask bits set to 1. -****************************************************************************/ - -bool make_netmask(struct sockaddr_storage *pss_out, - const struct sockaddr_storage *pss_in, - unsigned long masklen) -{ - *pss_out = *pss_in; - /* Now apply masklen bits of mask. */ -#if defined(HAVE_IPV6) - if (pss_in->ss_family == AF_INET6) { - char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; - unsigned int i; - - if (masklen > 128) { - return false; - } - for (i = 0; masklen >= 8; masklen -= 8, i++) { - *p++ = 0xff; - } - /* Deal with the partial byte. */ - *p++ &= (0xff & ~(0xff>>masklen)); - i++; - for (;i < sizeof(struct in6_addr); i++) { - *p++ = '\0'; - } - return true; - } -#endif - if (pss_in->ss_family == AF_INET) { - if (masklen > 32) { - return false; - } - ((struct sockaddr_in *)pss_out)->sin_addr.s_addr = - htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL)); - return true; - } - return false; -} - -/**************************************************************************** - Create a struct sockaddr_storage set to the broadcast or network adress from - an incoming sockaddr_storage. -****************************************************************************/ - -static void make_bcast_or_net(struct sockaddr_storage *pss_out, - const struct sockaddr_storage *pss_in, - const struct sockaddr_storage *nmask, - bool make_bcast) -{ - unsigned int i = 0, len = 0; - char *pmask = NULL; - char *p = NULL; - *pss_out = *pss_in; - - /* Set all zero netmask bits to 1. */ -#if defined(HAVE_IPV6) - if (pss_in->ss_family == AF_INET6) { - p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; - pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr; - len = 16; - } -#endif - if (pss_in->ss_family == AF_INET) { - p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr; - pmask = (char *)&((struct sockaddr_in *)nmask)->sin_addr; - len = 4; - } - - for (i = 0; i < len; i++, p++, pmask++) { - if (make_bcast) { - *p = (*p & *pmask) | (*pmask ^ 0xff); - } else { - /* make_net */ - *p = (*p & *pmask); - } - } -} - -static void make_bcast(struct sockaddr_storage *pss_out, - const struct sockaddr_storage *pss_in, - const struct sockaddr_storage *nmask) -{ - make_bcast_or_net(pss_out, pss_in, nmask, true); -} - -static void make_net(struct sockaddr_storage *pss_out, - const struct sockaddr_storage *pss_in, - const struct sockaddr_storage *nmask) -{ - make_bcast_or_net(pss_out, pss_in, nmask, false); -} - /**************************************************************************** Interpret a single element from a interfaces= config line. -- cgit From e054affb7bb3e2aa00663c2d4dfa04c19870ddaf Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 25 Oct 2007 19:07:25 -0700 Subject: Fix bug in writing names into gencache as well as 2 typos where AF_INET6 was mistypes as AF_INET. JERRY YOU NEED THESE FIXES. Fixes smbclient -L localhost -U% Bugs reported by Kukks (thanks kukks). Jeremy. (This used to be commit f109f82622ca30ae2360e8300152e90b9587ffd8) --- source3/lib/interface.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 9d073bc08c..9627bf63dd 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -100,14 +100,6 @@ void setup_linklocal_scope_id(struct sockaddr_storage *pss) return; } } - for (i=local_interfaces;i;i=i->next) { - if (same_net(pss, &i->ip, &i->netmask)) { - struct sockaddr_in6 *psa6 = - (struct sockaddr_in6 *)pss; - psa6->sin6_scope_id = if_nametoindex(i->name); - return; - } - } } #endif -- cgit From 12f0163e26e6c39ba13335468b7a1bef4cf29e73 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 2 Jul 2008 16:31:16 -0700 Subject: Remove worrying warning message when safe_strcpy tries to copy a pseaudo interface name that's too long. Reported by James Kosin . Jeremy. (This used to be commit 3a7542fd495223c3a504571a52e2d00551fea0e2) --- source3/lib/interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 9627bf63dd..eb0af9ef34 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -469,7 +469,7 @@ static void interpret_interface(char *token) token)); ZERO_STRUCT(ifs); - safe_strcpy(ifs.name, token, sizeof(ifs.name)-1); + (void)strlcpy(ifs.name, token, sizeof(ifs.name)); ifs.flags = IFF_BROADCAST; ifs.ip = ss; ifs.netmask = ss_mask; -- cgit From f23a6b7c93a419d70889f06b7b3ab18725399793 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 19 Aug 2008 17:30:30 -0700 Subject: Fix bug 5697 nmbd spins in reload_interfaces when only loopback has an IPv4 address reported by Ted Percival . Jeremy. (This used to be commit ab06efccf31fbc899536d2681a2076e6dfd65b9e) --- source3/lib/interface.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'source3/lib/interface.c') diff --git a/source3/lib/interface.c b/source3/lib/interface.c index eb0af9ef34..2e7c2706a0 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -131,15 +131,18 @@ int iface_count(void) } /**************************************************************************** - How many interfaces do we have (v4 only) ? + How many non-loopback IPv4 interfaces do we have ? **************************************************************************/ -int iface_count_v4(void) +int iface_count_v4_nl(void) { int ret = 0; struct interface *i; for (i=local_interfaces;i;i=i->next) { + if (is_loopback_addr(&i->ip)) { + continue; + } if (i->ip.ss_family == AF_INET) { ret++; } -- cgit