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

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