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

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