Blame SOURCES/autofs-5.0.9-amd-lookup-move-get_proximity-to-parse_subs-c.patch

304803
autofs-5.0.9 - amd lookup move get_proximity() to parse_subs.c
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
Later we'll need to use get_proximity() from outside modules/replicated.c
304803
so move it to the autofs library.
304803
---
304803
 include/parse_subs.h |    8 ++
304803
 lib/parse_subs.c     |  199 ++++++++++++++++++++++++++++++++++++++++++++++++++
304803
 modules/replicated.c |  197 -------------------------------------------------
304803
 3 files changed, 207 insertions(+), 197 deletions(-)
304803
304803
diff --git a/include/parse_subs.h b/include/parse_subs.h
304803
index ecc712d..c0da5ae 100644
304803
--- a/include/parse_subs.h
304803
+++ b/include/parse_subs.h
304803
@@ -18,6 +18,13 @@
304803
 #ifndef PARSE_SUBS_H
304803
 #define PARSE_SUBS_H
304803
 
304803
+#define PROXIMITY_ERROR		0x0000
304803
+#define PROXIMITY_LOCAL         0x0001
304803
+#define PROXIMITY_SUBNET        0x0002
304803
+#define PROXIMITY_NET           0x0004
304803
+#define PROXIMITY_OTHER         0x0008
304803
+#define PROXIMITY_UNSUPPORTED   0x0010
304803
+
304803
 struct mapent;
304803
 
304803
 struct map_type_info {
304803
@@ -26,6 +33,7 @@ struct map_type_info {
304803
 	char *map;
304803
 };
304803
 
304803
+unsigned int get_proximity(struct sockaddr *);
304803
 const char *skipspace(const char *);
304803
 int check_colon(const char *);
304803
 int chunklen(const char *, int);
304803
diff --git a/lib/parse_subs.c b/lib/parse_subs.c
304803
index dd2a784..b77d890 100644
304803
--- a/lib/parse_subs.c
304803
+++ b/lib/parse_subs.c
304803
@@ -18,8 +18,24 @@
304803
 #include <stdlib.h>
304803
 #include <string.h>
304803
 #include <ctype.h>
304803
+#include <sys/types.h>
304803
+#include <ifaddrs.h>
304803
+#include <net/if.h>
304803
 #include "automount.h"
304803
 
304803
+#define MAX_NETWORK_LEN		255
304803
+
304803
+#define MAX_IFC_BUF		2048
304803
+static int volatile ifc_buf_len = MAX_IFC_BUF;
304803
+static int volatile ifc_last_len = 0;
304803
+
304803
+#define MASK_A  0x7F000000
304803
+#define MASK_B  0xBFFF0000
304803
+#define MASK_C  0xDFFFFF00
304803
+
304803
+/* Get numeric value of the n bits starting at position p */
304803
+#define getbits(x, p, n)	((x >> (p + 1 - n)) & ~(~0 << n))
304803
+
304803
 struct types {
304803
 	char *type;
304803
 	unsigned int len;
304803
@@ -45,6 +61,189 @@ static struct types format_type[] = {
304803
 };
304803
 static unsigned int format_type_count = sizeof(format_type)/sizeof(struct types);
304803
 
304803
+static unsigned int ipv6_mask_cmp(uint32_t *host, uint32_t *iface, uint32_t *mask)
304803
+{
304803
+	unsigned int ret = 1;
304803
+	unsigned int i;
304803
+
304803
+	for (i = 0; i < 4; i++) {
304803
+		if ((host[i] & mask[i]) != (iface[i] & mask[i])) {
304803
+			ret = 0;
304803
+			break;
304803
+		}
304803
+	}
304803
+	return ret;
304803
+}
304803
+
304803
+unsigned int get_proximity(struct sockaddr *host_addr)
304803
+{
304803
+	struct ifaddrs *ifa = NULL;
304803
+	struct ifaddrs *this;
304803
+	struct sockaddr_in *addr, *msk_addr, *if_addr;
304803
+	struct sockaddr_in6 *addr6, *msk6_addr, *if6_addr;
304803
+	struct in_addr *hst_addr;
304803
+	struct in6_addr *hst6_addr;
304803
+	int addr_len;
304803
+	char buf[MAX_ERR_BUF];
304803
+	uint32_t mask, ha, ia, *mask6, *ha6, *ia6;
304803
+	int ret;
304803
+
304803
+	addr = NULL;
304803
+	addr6 = NULL;
304803
+	hst_addr = NULL;
304803
+	hst6_addr = NULL;
304803
+	mask6 = NULL;
304803
+	ha6 = NULL;
304803
+	ia6 = NULL;
304803
+	ha = 0;
304803
+
304803
+	switch (host_addr->sa_family) {
304803
+	case AF_INET:
304803
+		addr = (struct sockaddr_in *) host_addr;
304803
+		hst_addr = (struct in_addr *) &addr->sin_addr;
304803
+		ha = ntohl((uint32_t) hst_addr->s_addr);
304803
+		addr_len = sizeof(*hst_addr);
304803
+		break;
304803
+
304803
+	case AF_INET6:
304803
+#ifndef WITH_LIBTIRPC
304803
+		return PROXIMITY_UNSUPPORTED;
304803
+#else
304803
+		addr6 = (struct sockaddr_in6 *) host_addr;
304803
+		hst6_addr = (struct in6_addr *) &addr6->sin6_addr;
304803
+		ha6 = &hst6_addr->s6_addr32[0];
304803
+		addr_len = sizeof(*hst6_addr);
304803
+		break;
304803
+#endif
304803
+
304803
+	default:
304803
+		return PROXIMITY_ERROR;
304803
+	}
304803
+
304803
+	ret = getifaddrs(&ifa;;
304803
+	if (ret) {
304803
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+		logerr("getifaddrs: %s", estr);
304803
+		return PROXIMITY_ERROR;
304803
+	}
304803
+
304803
+	this = ifa;
304803
+	while (this) {
304803
+		if (!(this->ifa_flags & IFF_UP) ||
304803
+		    this->ifa_flags & IFF_POINTOPOINT ||
304803
+		    this->ifa_addr == NULL) {
304803
+			this = this->ifa_next;
304803
+			continue;
304803
+		}
304803
+
304803
+		switch (this->ifa_addr->sa_family) {
304803
+		case AF_INET:
304803
+			if (host_addr->sa_family == AF_INET6)
304803
+				break;
304803
+			if_addr = (struct sockaddr_in *) this->ifa_addr;
304803
+			ret = memcmp(&if_addr->sin_addr, hst_addr, addr_len);
304803
+			if (!ret) {
304803
+				freeifaddrs(ifa);
304803
+				return PROXIMITY_LOCAL;
304803
+			}
304803
+			break;
304803
+
304803
+		case AF_INET6:
304803
+#ifdef WITH_LIBTIRPC
304803
+			if (host_addr->sa_family == AF_INET)
304803
+				break;
304803
+			if6_addr = (struct sockaddr_in6 *) this->ifa_addr;
304803
+			ret = memcmp(&if6_addr->sin6_addr, hst6_addr, addr_len);
304803
+			if (!ret) {
304803
+				freeifaddrs(ifa);
304803
+				return PROXIMITY_LOCAL;
304803
+			}
304803
+#endif
304803
+		default:
304803
+			break;
304803
+		}
304803
+		this = this->ifa_next;
304803
+	}
304803
+
304803
+	this = ifa;
304803
+	while (this) {
304803
+		if (!(this->ifa_flags & IFF_UP) ||
304803
+		    this->ifa_flags & IFF_POINTOPOINT ||
304803
+		    this->ifa_addr == NULL) {
304803
+			this = this->ifa_next;
304803
+			continue;
304803
+		}
304803
+
304803
+		switch (this->ifa_addr->sa_family) {
304803
+		case AF_INET:
304803
+			if (host_addr->sa_family == AF_INET6)
304803
+				break;
304803
+			if_addr = (struct sockaddr_in *) this->ifa_addr;
304803
+			ia =  ntohl((uint32_t) if_addr->sin_addr.s_addr);
304803
+
304803
+			/* Is the address within a localy attached subnet */
304803
+
304803
+			msk_addr = (struct sockaddr_in *) this->ifa_netmask;
304803
+			mask = ntohl((uint32_t) msk_addr->sin_addr.s_addr);
304803
+
304803
+			if ((ia & mask) == (ha & mask)) {
304803
+				freeifaddrs(ifa);
304803
+				return PROXIMITY_SUBNET;
304803
+			}
304803
+
304803
+			/*
304803
+			 * Is the address within a local ipv4 network.
304803
+			 *
304803
+			 * Bit position 31 == 0 => class A.
304803
+			 * Bit position 30 == 0 => class B.
304803
+			 * Bit position 29 == 0 => class C.
304803
+			 */
304803
+
304803
+			if (!getbits(ia, 31, 1))
304803
+				mask = MASK_A;
304803
+			else if (!getbits(ia, 30, 1))
304803
+				mask = MASK_B;
304803
+			else if (!getbits(ia, 29, 1))
304803
+				mask = MASK_C;
304803
+			else
304803
+				break;
304803
+
304803
+			if ((ia & mask) == (ha & mask)) {
304803
+				freeifaddrs(ifa);
304803
+				return PROXIMITY_NET;
304803
+			}
304803
+			break;
304803
+
304803
+		case AF_INET6:
304803
+#ifdef WITH_LIBTIRPC
304803
+			if (host_addr->sa_family == AF_INET)
304803
+				break;
304803
+			if6_addr = (struct sockaddr_in6 *) this->ifa_addr;
304803
+			ia6 = &if6_addr->sin6_addr.s6_addr32[0];
304803
+
304803
+			/* Is the address within the network of the interface */
304803
+
304803
+			msk6_addr = (struct sockaddr_in6 *) this->ifa_netmask;
304803
+			mask6 = &msk6_addr->sin6_addr.s6_addr32[0];
304803
+
304803
+			if (ipv6_mask_cmp(ha6, ia6, mask6)) {
304803
+				freeifaddrs(ifa);
304803
+				return PROXIMITY_SUBNET;
304803
+			}
304803
+
304803
+			/* How do we define "local network" in ipv6? */
304803
+#endif
304803
+		default:
304803
+			break;
304803
+		}
304803
+		this = this->ifa_next;
304803
+	}
304803
+
304803
+	freeifaddrs(ifa);
304803
+
304803
+	return PROXIMITY_OTHER;
304803
+}
304803
+
304803
 /*
304803
  * Skip whitespace in a string; if we hit a #, consider the rest of the
304803
  * entry a comment.
304803
diff --git a/modules/replicated.c b/modules/replicated.c
304803
index d43f778..0c1a8a7 100644
304803
--- a/modules/replicated.c
304803
+++ b/modules/replicated.c
304803
@@ -48,11 +48,8 @@
304803
 #include <stdint.h>
304803
 #include <sys/ioctl.h>
304803
 #include <sys/socket.h>
304803
-#include <arpa/inet.h>
304803
-#include <net/if.h>
304803
 #include <netinet/in.h>
304803
 #include <netdb.h>
304803
-#include <ifaddrs.h>
304803
 
304803
 #include "rpc_subs.h"
304803
 #include "replicated.h"
304803
@@ -62,34 +59,9 @@
304803
 #define MAX_ERR_BUF		512
304803
 #endif
304803
 
304803
-#define MAX_IFC_BUF		2048
304803
-static int volatile ifc_buf_len = MAX_IFC_BUF;
304803
-static int volatile ifc_last_len = 0;
304803
-
304803
-#define MASK_A  0x7F000000
304803
-#define MASK_B  0xBFFF0000
304803
-#define MASK_C  0xDFFFFF00
304803
-
304803
-/* Get numeric value of the n bits starting at position p */
304803
-#define getbits(x, p, n)	((x >> (p + 1 - n)) & ~(~0 << n))
304803
-
304803
 #define mymax(x, y)	(x >= y ? x : y)
304803
 #define mmax(x, y, z)	(mymax(x, y) == x ? mymax(x, z) : mymax(y, z))
304803
 
304803
-unsigned int ipv6_mask_cmp(uint32_t *host, uint32_t *iface, uint32_t *mask)
304803
-{
304803
-	unsigned int ret = 1;
304803
-	unsigned int i;
304803
-
304803
-	for (i = 0; i < 4; i++) {
304803
-		if ((host[i] & mask[i]) != (iface[i] & mask[i])) {
304803
-			ret = 0;
304803
-			break;
304803
-		}
304803
-	}
304803
-	return ret;
304803
-}
304803
-
304803
 void seed_random(void)
304803
 {
304803
 	int fd;
304803
@@ -111,175 +83,6 @@ void seed_random(void)
304803
 	return;
304803
 }
304803
 
304803
-static unsigned int get_proximity(struct sockaddr *host_addr)
304803
-{
304803
-	struct ifaddrs *ifa = NULL;
304803
-	struct ifaddrs *this;
304803
-	struct sockaddr_in *addr, *msk_addr, *if_addr;
304803
-	struct sockaddr_in6 *addr6, *msk6_addr, *if6_addr;
304803
-	struct in_addr *hst_addr;
304803
-	struct in6_addr *hst6_addr;
304803
-	int addr_len;
304803
-	char buf[MAX_ERR_BUF];
304803
-	uint32_t mask, ha, ia, *mask6, *ha6, *ia6;
304803
-	int ret;
304803
-
304803
-	addr = NULL;
304803
-	addr6 = NULL;
304803
-	hst_addr = NULL;
304803
-	hst6_addr = NULL;
304803
-	mask6 = NULL;
304803
-	ha6 = NULL;
304803
-	ia6 = NULL;
304803
-	ha = 0;
304803
-
304803
-	switch (host_addr->sa_family) {
304803
-	case AF_INET:
304803
-		addr = (struct sockaddr_in *) host_addr;
304803
-		hst_addr = (struct in_addr *) &addr->sin_addr;
304803
-		ha = ntohl((uint32_t) hst_addr->s_addr);
304803
-		addr_len = sizeof(*hst_addr);
304803
-		break;
304803
-
304803
-	case AF_INET6:
304803
-#ifndef WITH_LIBTIRPC
304803
-		return PROXIMITY_UNSUPPORTED;
304803
-#else
304803
-		addr6 = (struct sockaddr_in6 *) host_addr;
304803
-		hst6_addr = (struct in6_addr *) &addr6->sin6_addr;
304803
-		ha6 = &hst6_addr->s6_addr32[0];
304803
-		addr_len = sizeof(*hst6_addr);
304803
-		break;
304803
-#endif
304803
-
304803
-	default:
304803
-		return PROXIMITY_ERROR;
304803
-	}
304803
-
304803
-	ret = getifaddrs(&ifa;;
304803
-	if (ret) {
304803
-		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
-		logerr("getifaddrs: %s", estr);
304803
-		return PROXIMITY_ERROR;
304803
-	}
304803
-
304803
-	this = ifa;
304803
-	while (this) {
304803
-		if (!(this->ifa_flags & IFF_UP) ||
304803
-		    this->ifa_flags & IFF_POINTOPOINT ||
304803
-		    this->ifa_addr == NULL) {
304803
-			this = this->ifa_next;
304803
-			continue;
304803
-		}
304803
-
304803
-		switch (this->ifa_addr->sa_family) {
304803
-		case AF_INET:
304803
-			if (host_addr->sa_family == AF_INET6)
304803
-				break;
304803
-			if_addr = (struct sockaddr_in *) this->ifa_addr;
304803
-			ret = memcmp(&if_addr->sin_addr, hst_addr, addr_len);
304803
-			if (!ret) {
304803
-				freeifaddrs(ifa);
304803
-				return PROXIMITY_LOCAL;
304803
-			}
304803
-			break;
304803
-
304803
-		case AF_INET6:
304803
-#ifdef WITH_LIBTIRPC
304803
-			if (host_addr->sa_family == AF_INET)
304803
-				break;
304803
-			if6_addr = (struct sockaddr_in6 *) this->ifa_addr;
304803
-			ret = memcmp(&if6_addr->sin6_addr, hst6_addr, addr_len);
304803
-			if (!ret) {
304803
-				freeifaddrs(ifa);
304803
-				return PROXIMITY_LOCAL;
304803
-			}
304803
-#endif
304803
-		default:
304803
-			break;
304803
-		}
304803
-		this = this->ifa_next;
304803
-	}
304803
-
304803
-	this = ifa;
304803
-	while (this) {
304803
-		if (!(this->ifa_flags & IFF_UP) ||
304803
-		    this->ifa_flags & IFF_POINTOPOINT ||
304803
-		    this->ifa_addr == NULL) {
304803
-			this = this->ifa_next;
304803
-			continue;
304803
-		}
304803
-
304803
-		switch (this->ifa_addr->sa_family) {
304803
-		case AF_INET:
304803
-			if (host_addr->sa_family == AF_INET6)
304803
-				break;
304803
-			if_addr = (struct sockaddr_in *) this->ifa_addr;
304803
-			ia =  ntohl((uint32_t) if_addr->sin_addr.s_addr);
304803
-
304803
-			/* Is the address within a localy attached subnet */
304803
-
304803
-			msk_addr = (struct sockaddr_in *) this->ifa_netmask;
304803
-			mask = ntohl((uint32_t) msk_addr->sin_addr.s_addr);
304803
-
304803
-			if ((ia & mask) == (ha & mask)) {
304803
-				freeifaddrs(ifa);
304803
-				return PROXIMITY_SUBNET;
304803
-			}
304803
-
304803
-			/*
304803
-			 * Is the address within a local ipv4 network.
304803
-			 *
304803
-			 * Bit position 31 == 0 => class A.
304803
-			 * Bit position 30 == 0 => class B.
304803
-			 * Bit position 29 == 0 => class C.
304803
-			 */
304803
-
304803
-			if (!getbits(ia, 31, 1))
304803
-				mask = MASK_A;
304803
-			else if (!getbits(ia, 30, 1))
304803
-				mask = MASK_B;
304803
-			else if (!getbits(ia, 29, 1))
304803
-				mask = MASK_C;
304803
-			else
304803
-				break;
304803
-
304803
-			if ((ia & mask) == (ha & mask)) {
304803
-				freeifaddrs(ifa);
304803
-				return PROXIMITY_NET;
304803
-			}
304803
-			break;
304803
-
304803
-		case AF_INET6:
304803
-#ifdef WITH_LIBTIRPC
304803
-			if (host_addr->sa_family == AF_INET)
304803
-				break;
304803
-			if6_addr = (struct sockaddr_in6 *) this->ifa_addr;
304803
-			ia6 = &if6_addr->sin6_addr.s6_addr32[0];
304803
-
304803
-			/* Is the address within the network of the interface */
304803
-
304803
-			msk6_addr = (struct sockaddr_in6 *) this->ifa_netmask;
304803
-			mask6 = &msk6_addr->sin6_addr.s6_addr32[0];
304803
-
304803
-			if (ipv6_mask_cmp(ha6, ia6, mask6)) {
304803
-				freeifaddrs(ifa);
304803
-				return PROXIMITY_SUBNET;
304803
-			}
304803
-
304803
-			/* How do we define "local network" in ipv6? */
304803
-#endif
304803
-		default:
304803
-			break;
304803
-		}
304803
-		this = this->ifa_next;
304803
-	}
304803
-
304803
-	freeifaddrs(ifa);
304803
-
304803
-	return PROXIMITY_OTHER;
304803
-}
304803
-
304803
 struct host *new_host(const char *name,
304803
 		      struct sockaddr *addr, size_t addr_len,
304803
 		      unsigned int proximity, unsigned int weight,