[PATCH 1/2] Fix sin_len/sin6_len handling

James Clarke jrtc27 at jrtc27.com
Sun Jul 1 19:16:53 UTC 2018


Signed-off-by: James Clarke <jrtc27 at jrtc27.com>
---
 ctdb/common/ctdb_util.c        |  8 ++++----
 ctdb/common/system_util.c      | 22 +++++++++++-----------
 ctdb/protocol/protocol_util.c  |  2 +-
 lib/replace/wscript            |  4 ++++
 source4/lib/socket/socket_ip.c | 12 ++++++++++++
 5 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/ctdb/common/ctdb_util.c b/ctdb/common/ctdb_util.c
index cbab3672a67..13d138dae12 100644
--- a/ctdb/common/ctdb_util.c
+++ b/ctdb/common/ctdb_util.c
@@ -351,7 +351,7 @@ void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
 			/* Copy IPv4-mapped IPv6 addresses as IPv4 */
 			cip->ip.sin_family = AF_INET;
 #ifdef HAVE_SOCK_SIN_LEN
-			cip->ip.sin_len = sizeof(ctdb_sock_addr);
+			cip->ip.sin_len = sizeof(cip->ip);
 #endif
 			cip->ip.sin_port   = ip->ip6.sin6_port;
 			memcpy(&cip->ip.sin_addr,
@@ -359,8 +359,8 @@ void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
 			       sizeof(cip->ip.sin_addr));
 		} else {
 			cip->ip6.sin6_family = AF_INET6;
-#ifdef HAVE_SOCK_SIN_LEN
-			cip->ip6.sin_len = sizeof(ctdb_sock_addr);
+#ifdef HAVE_SOCK_SIN6_LEN
+			cip->ip6.sin6_len = sizeof(cip->ip6);
 #endif
 			cip->ip6.sin6_port   = ip->ip6.sin6_port;
 			memcpy(&cip->ip6.sin6_addr,
@@ -374,7 +374,7 @@ void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
 	if (ip->sa.sa_family == AF_INET) {
 		cip->ip.sin_family = AF_INET;
 #ifdef HAVE_SOCK_SIN_LEN
-		cip->ip.sin_len = sizeof(ctdb_sock_addr);
+		cip->ip.sin_len = sizeof(cip->ip);
 #endif
 		cip->ip.sin_port = ip->ip.sin_port;
 		memcpy(&cip->ip.sin_addr,
diff --git a/ctdb/common/system_util.c b/ctdb/common/system_util.c
index f27eed7038d..473d4a6ea64 100644
--- a/ctdb/common/system_util.c
+++ b/ctdb/common/system_util.c
@@ -132,30 +132,30 @@ static bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
 	return true;
 }
 
-static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
+static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, struct sockaddr_in6 *sin6)
 {
-	saddr->ip6.sin6_family   = AF_INET6;
-	saddr->ip6.sin6_port     = htons(port);
-	saddr->ip6.sin6_flowinfo = 0;
-	saddr->ip6.sin6_scope_id = 0;
+	sin6->sin6_family   = AF_INET6;
+	sin6->sin6_port     = htons(port);
+	sin6->sin6_flowinfo = 0;
+	sin6->sin6_scope_id = 0;
 
-	if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
+	if (inet_pton(AF_INET6, s, &sin6->sin6_addr) != 1) {
 		DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
 		return false;
 	}
 
-	if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
+	if (ifaces && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
 		if (strchr(ifaces, ',')) {
 			DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
 					  "is specified for multiple ifaces %s\n",
 					  s, ifaces));
 			return false;
 		}
-		saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
+		sin6->sin6_scope_id = if_nametoindex(ifaces);
 	}
 
-#ifdef HAVE_SOCK_SIN_LEN
-	saddr->ip6.sin6_len = sizeof(*saddr);
+#ifdef HAVE_SOCK_SIN6_LEN
+	sin6->sin6_len = sizeof(*sin6);
 #endif
 	return true;
 }
@@ -184,7 +184,7 @@ static bool parse_ip(const char *addr, const char *ifaces, unsigned port,
 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
 		};
 
-		ret = parse_ipv6(addr, ifaces, port, saddr);
+		ret = parse_ipv6(addr, ifaces, port, &saddr->ip6);
 		if (! ret) {
 			return ret;
 		}
diff --git a/ctdb/protocol/protocol_util.c b/ctdb/protocol/protocol_util.c
index 73652e7f060..c75555fa734 100644
--- a/ctdb/protocol/protocol_util.c
+++ b/ctdb/protocol/protocol_util.c
@@ -206,7 +206,7 @@ static int ipv6_from_string(const char *str, struct sockaddr_in6 *ip6)
 		return EINVAL;
 	}
 
-#ifdef HAVE_SOCK_SIN_LEN
+#ifdef HAVE_SOCK_SIN6_LEN
 	ip6->sin6_len = sizeof(*ip6);
 #endif
 	return 0;
diff --git a/lib/replace/wscript b/lib/replace/wscript
index 541573babf2..3df86b1b5bc 100644
--- a/lib/replace/wscript
+++ b/lib/replace/wscript
@@ -670,6 +670,10 @@ removeea setea
                                 headers='sys/socket.h netinet/in.h',
                                 define='HAVE_SOCK_SIN_LEN')
 
+    conf.CHECK_STRUCTURE_MEMBER('struct sockaddr_in6', 'sin6_len',
+                                headers='sys/socket.h netinet/in.h',
+                                define='HAVE_SOCK_SIN6_LEN')
+
     conf.CHECK_CODE('struct sockaddr_un sunaddr; sunaddr.sun_family = AF_UNIX;',
                     define='HAVE_UNIXSOCKET', headers='sys/socket.h sys/un.h')
 
diff --git a/source4/lib/socket/socket_ip.c b/source4/lib/socket/socket_ip.c
index e0aa5da07cb..7eb6a6033d0 100644
--- a/source4/lib/socket/socket_ip.c
+++ b/source4/lib/socket/socket_ip.c
@@ -642,6 +642,9 @@ static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
 		if (memcmp(&my_ip, &in6addr_any, sizeof(my_ip)) || my_address->port != 0) {
 			struct sockaddr_in6 my_addr;
 			ZERO_STRUCT(my_addr);
+#ifdef HAVE_SOCK_SIN6_LEN
+			my_addr.sin6_len	= sizeof(my_addr);
+#endif
 			my_addr.sin6_addr	= my_ip;
 			my_addr.sin6_port	= htons(my_address->port);
 			my_addr.sin6_family	= PF_INET6;
@@ -664,6 +667,9 @@ static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
 		}
 		
 		ZERO_STRUCT(srv_addr);
+#ifdef HAVE_SOCK_SIN6_LEN
+		srv_addr.sin6_len	= sizeof(srv_addr);
+#endif
 		srv_addr.sin6_addr	= srv_ip;
 		srv_addr.sin6_port	= htons(srv_address->port);
 		srv_addr.sin6_family	= PF_INET6;
@@ -707,6 +713,9 @@ static NTSTATUS ipv6_listen(struct socket_context *sock,
 		ip_addr = interpret_addr6(my_address->addr);
 		
 		ZERO_STRUCT(my_addr);
+#ifdef HAVE_SOCK_SIN6_LEN
+		my_addr.sin6_len	= sizeof(my_addr);
+#endif
 		my_addr.sin6_addr	= ip_addr;
 		my_addr.sin6_port	= htons(my_address->port);
 		my_addr.sin6_family	= PF_INET6;
@@ -864,6 +873,9 @@ static NTSTATUS ipv6_sendto(struct socket_context *sock,
 		struct in6_addr addr;
 		
 		ZERO_STRUCT(srv_addr);
+#ifdef HAVE_SOCK_SIN6_LEN
+		srv_addr.sin6_len        = sizeof(srv_addr);
+#endif
 		addr                     = interpret_addr6(dest_addr->addr);
 		if (memcmp(&addr.s6_addr, &in6addr_any,
 			   sizeof(addr.s6_addr)) == 0) {
-- 
2.18.0




More information about the samba-technical mailing list