[SCM] Socket Wrapper Repository - branch master updated

Andreas Schneider asn at samba.org
Mon Dec 8 13:30:19 UTC 2025


The branch, master has been updated
       via  e76fb99 swrap: Fix fd-passing for Python 3.14's ProcessPoolExecutor
       via  64023cd swrap: Add debugging what fd types are being passed
       via  bf12b3b swrap: Add socket_wrapper_debug_level()
       via  3966d40 tests: Add reproducer for python >= 3.14 fd passing problem
       via  bb61fee swrap: Use `struct swrap_address_ip_only` in `struct socket_info`
       via  d23916e swrap: Add `struct swrap_address_ip_only` and helpers
       via  28d7701 swrap: Use bitfields for boolean members in `struct socket_info`
       via  41fece6 swrap: Use uint16_t for pktinfo
       via  2d4a311 swrap: Use uint16_t for the family
       via  555d393 tests: Add helper to calc the size for unix_scm_rights_payload array
       via  13e97ce tests: Reduce runtime from 60 sec to 12 sec
       via  a79b0c0 tests: Removed unused includes from swrap_fake_uid_wrapper.c
       via  b25e2ea tests: Prepare test to compile with cmocka 2.0
       via  3c5f514 Revert "swrap: Allow to pass 8 fds"
      from  89f9c3c swrap: Allow to pass 8 fds

https://git.samba.org/?p=socket_wrapper.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit e76fb993fe424dcd96d54b9623b120c4cc5a789e
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Nov 24 17:49:37 2025 +0100

    swrap: Fix fd-passing for Python 3.14's ProcessPoolExecutor
    
    ./tests/calc_unix_scm_rights_payload:
    
    Current configuration:
      SWRAP_MAX_PASSED_FDS = 16
      SWRAP_MAX_PASSED_SOCKET_INFO = 16
      sizeof(struct socket_info) = 240 bytes
      sizeof(struct swrap_unix_scm_rights) = 3904 bytes
    
    PIPE_BUF = 4096 bytes
    Header size (offsetof payload) = 40 bytes
    Payload base size (offsetof idxs) = 1 bytes
    Available space for arrays = 4055 bytes
    
    Calculated maximum values:
      SWRAP_MAX_PASSED_FDS = 16
      SWRAP_MAX_PASSED_SOCKET_INFO = 16
    
    Verification:
      Calculated total size (struct swrap_unix_scm_rights) = 3897 bytes
      Space used = 95.1% of PIPE_BUF
      Space remaining = 199 bytes
      Fits in PIPE_BUF: YES
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 64023cd4b756ee745a5498619a6066f5e637efe2
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Nov 24 17:53:52 2025 +0100

    swrap: Add debugging what fd types are being passed
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit bf12b3bb899146c311caff35dc9a5f0ebbbd1d03
Author: Andreas Schneider <asn at samba.org>
Date:   Thu Nov 27 09:49:19 2025 +0100

    swrap: Add socket_wrapper_debug_level()
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 3966d40904d4ce26a86d44ff253f6d69eee4e5ea
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Nov 24 17:25:02 2025 +0100

    tests: Add reproducer for python >= 3.14 fd passing problem
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit bb61feedc10205119c31fea9ca0247a9b27be2b2
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Nov 25 13:37:34 2025 +0100

    swrap: Use `struct swrap_address_ip_only` in `struct socket_info`
    
    Before: sizeof(struct socket_info) = 568 bytes
    After:  sizeof(struct socket_info) = 240 bytes
    
    Current configuration:
      SWRAP_MAX_PASSED_FDS = 6
      SWRAP_MAX_PASSED_SOCKET_INFO = 6
      sizeof(struct socket_info) = 240 bytes
      sizeof(struct swrap_unix_scm_rights) = 1488 bytes
    
    PIPE_BUF = 4096 bytes
    Header size (offsetof payload) = 40 bytes
    Payload base size (offsetof idxs) = 1 bytes
    Available space for arrays = 4055 bytes
    
    Calculated maximum values:
      SWRAP_MAX_PASSED_FDS = 16
      SWRAP_MAX_PASSED_SOCKET_INFO = 16
    
    Verification:
      Calculated total size (struct swrap_unix_scm_rights) = 3897 bytes
      Space used = 95.1% of PIPE_BUF
      Space remaining = 199 bytes
      Fits in PIPE_BUF: YES
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit d23916ecd7f81ab47b9818a3d4d42f687afbc1d3
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Nov 25 13:37:02 2025 +0100

    swrap: Add `struct swrap_address_ip_only` and helpers
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 28d77017e9b474ab2c19915c965afe8a10de3af9
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Nov 25 10:14:51 2025 +0100

    swrap: Use bitfields for boolean members in `struct socket_info`
    
    Before: sizeof(struct socket_info) = 596 bytes
    After:  sizeof(struct socket_info) = 568 bytes
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 41fece649cf3b73b062ee78a6dd429e853d1abbf
Author: Andreas Schneider <asn at samba.org>
Date:   Wed Sep 10 11:31:03 2025 +0200

    swrap: Use uint16_t for pktinfo
    
    Before: sizeof(struct socket_info) = 600 bytes
    After:  sizeof(struct socket_info) = 596 bytes
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 2d4a3115607f76dac89765c6e676247892d285eb
Author: Andreas Schneider <asn at samba.org>
Date:   Wed Sep 10 11:16:54 2025 +0200

    swrap: Use uint16_t for the family
    
    Before: sizeof(struct socket_info) = 600 bytes
    After:  sizeof(struct socket_info) = 600 bytes
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 555d393f6e8003cc736afc5e2e6a1daa78f5b139
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Nov 25 11:17:22 2025 +0100

    tests: Add helper to calc the size for unix_scm_rights_payload array
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 13e97cefa9450715fca33f5205fe8eb5014ff56d
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Nov 25 13:53:37 2025 +0100

    tests: Reduce runtime from 60 sec to 12 sec
    
    200msec should be enough.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit a79b0c0c34e033a0c20fd427eff75ffed11bd5bf
Author: Andreas Schneider <asn at samba.org>
Date:   Thu Nov 27 10:41:55 2025 +0100

    tests: Removed unused includes from swrap_fake_uid_wrapper.c
    
    Fixes building on FreeBSD.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit b25e2eaf205e52ab65fadc52b03e30b3a3ae75db
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Nov 24 15:57:47 2025 +0100

    tests: Prepare test to compile with cmocka 2.0
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 3c5f514b3069d7a94310f2e67aa766a1f30f5b91
Author: Andreas Schneider <asn at samba.org>
Date:   Wed Sep 10 11:51:30 2025 +0200

    Revert "swrap: Allow to pass 8 fds"
    
    This doesn't fit into PIPE_BUF :-(
    
    This reverts commit 89f9c3c62d17116a782728863d038d6c99d49ab4.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

-----------------------------------------------------------------------

Summary of changes:
 src/socket_wrapper.c                     | 430 +++++++++++++++++++++++++------
 tests/CMakeLists.txt                     |  79 ++++++
 tests/calc_unix_scm_rights_payload.c     |  75 ++++++
 tests/python_concurency_reproducer.py    |  32 +++
 tests/swrap_fake_uid_wrapper.c           |  17 +-
 tests/test_echo_tcp_get_peer_sock_name.c |   4 +
 tests/test_echo_tcp_poll.c               |   2 +-
 7 files changed, 541 insertions(+), 98 deletions(-)
 create mode 100644 tests/calc_unix_scm_rights_payload.c
 create mode 100755 tests/python_concurency_reproducer.py


Changeset truncated at 500 lines:

diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c
index 54eb239..34918ac 100644
--- a/src/socket_wrapper.c
+++ b/src/socket_wrapper.c
@@ -314,6 +314,16 @@ struct swrap_address {
 	} sa;
 };
 
+struct swrap_address_ip_only {
+	union {
+		struct sockaddr s;
+		struct sockaddr_in in;
+#ifdef HAVE_IPV6
+		struct sockaddr_in6 in6;
+#endif
+	} sa;
+};
+
 static int first_free;
 
 struct socket_info
@@ -323,28 +333,30 @@ struct socket_info
 	 * on any change.
 	 */
 
-	int family;
+	uint16_t family;
+	uint16_t pktinfo;
 	int type_flags; /* SOCK_CLOEXEC or SOCK_NONBLOCK */
 	int type;
 	int protocol;
 	int opt_type;
 	int opt_protocol;
-	int bound;
-	int bcast;
-	int is_server;
-	int connected;
-	int defer_connect;
-	int pktinfo;
-	int tcp_nodelay;
-	int listening;
 	int fd_passed;
 
+	/* Use bitfields for boolean types */
+	unsigned int bound:1;
+	unsigned int bcast:1;
+	unsigned int is_server:1;
+	unsigned int connected:1;
+	unsigned int defer_connect:1;
+	unsigned int tcp_nodelay:1;
+	unsigned int listening:1;
+
 	/* The unix path so we can unlink it on close() */
 	struct sockaddr_un un_addr;
 
-	struct swrap_address bindname;
-	struct swrap_address myname;
-	struct swrap_address peername;
+	struct swrap_address_ip_only bindname;
+	struct swrap_address_ip_only myname;
+	struct swrap_address_ip_only peername;
 
 	struct {
 		unsigned long pck_snd;
@@ -436,6 +448,59 @@ static const char *getprogname(void)
 }
 #endif /* HAVE_GETPROGNAME */
 
+static int swrap_debug_level = SWRAP_LOG_ERROR;
+
+static void __socket_wrapper_debug_level_once(void)
+{
+	const char *env = NULL;
+	size_t env_len;
+
+	env = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
+	if (env == NULL) {
+		swrap_debug_level = SWRAP_LOG_ERROR;
+		return;
+	}
+
+	env_len = strlen(env);
+	/* Sanity check: must not be empty and not longer than "warning" */
+	if (env_len == 0 || env_len > 7) {
+		swrap_debug_level = SWRAP_LOG_ERROR;
+		return;
+	}
+
+	/* Check for string values (case-insensitive) */
+	if (strcasecmp(env, "error") == 0) {
+		swrap_debug_level = SWRAP_LOG_ERROR;
+	} else if (strcasecmp(env, "warn") == 0 ||
+		   strcasecmp(env, "warning") == 0)
+	{
+		swrap_debug_level = SWRAP_LOG_WARN;
+	} else if (strcasecmp(env, "debug") == 0) {
+		swrap_debug_level = SWRAP_LOG_DEBUG;
+	} else if (strcasecmp(env, "trace") == 0) {
+		swrap_debug_level = SWRAP_LOG_TRACE;
+	} else {
+		/* Numeric value */
+		swrap_debug_level = atoi(env);
+
+		/* Clamp to valid range */
+		if (swrap_debug_level < SWRAP_LOG_ERROR) {
+			swrap_debug_level = SWRAP_LOG_ERROR;
+		} else if (swrap_debug_level > SWRAP_LOG_TRACE) {
+			swrap_debug_level = SWRAP_LOG_TRACE;
+		}
+	}
+}
+
+static unsigned int socket_wrapper_debug_level(void)
+{
+	static pthread_once_t debug_level_once = PTHREAD_ONCE_INIT;
+
+	pthread_once(&debug_level_once, __socket_wrapper_debug_level_once);
+
+	return (unsigned int)swrap_debug_level;
+}
+
 static void swrap_log(enum swrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
 # define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)
 
@@ -445,15 +510,11 @@ static void swrap_log(enum swrap_dbglvl_e dbglvl,
 {
 	char buffer[1024];
 	va_list va;
-	const char *d;
-	unsigned int lvl = 0;
+	unsigned int lvl;
 	const char *prefix = "SWRAP";
 	const char *progname = getprogname();
 
-	d = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
-	if (d != NULL) {
-		lvl = atoi(d);
-	}
+	lvl = socket_wrapper_debug_level();
 
 	if (lvl < dbglvl) {
 		return;
@@ -491,6 +552,146 @@ static void swrap_log(enum swrap_dbglvl_e dbglvl,
 		buffer);
 }
 
+/*********************************************************
+ * HELPER FUNCTIONS
+ **********************************************************/
+
+/*
+ * Get the socklen for an address family
+ */
+static inline socklen_t swrap_address_ip_only_socklen(sa_family_t family)
+{
+	switch (family) {
+	case AF_UNSPEC: /* AF_UNSPEC indicates uninitialized/empty address */
+		return 0;
+	case AF_INET:
+		return sizeof(struct sockaddr_in);
+#ifdef HAVE_IPV6
+	case AF_INET6:
+		return sizeof(struct sockaddr_in6);
+#endif
+	default:
+		SWRAP_LOG(SWRAP_LOG_ERROR,
+			  "BUG: unexpected address family %d",
+			  family);
+		abort();
+	}
+
+	return 0; /* Never reached */
+}
+
+/*
+ * Get the socklen from a swrap_address_ip_only struct
+ */
+static inline socklen_t swrap_address_ip_only_len(
+	const struct swrap_address_ip_only *addr)
+{
+	return swrap_address_ip_only_socklen(addr->sa.s.sa_family);
+}
+
+/*
+ * Copy a sockaddr to swrap_address_ip_only, using the actual address size
+ * based on the address family.
+ */
+static inline void swrap_address_ip_only_from_sockaddr(
+	struct swrap_address_ip_only *dest,
+	const struct sockaddr *src,
+	socklen_t addrlen)
+{
+	socklen_t copy_len = swrap_address_ip_only_socklen(src->sa_family);
+
+	/* AF_UNSPEC means empty/unbind - zero the entire struct */
+	if (copy_len == 0) {
+		memset(&dest->sa, 0, sizeof(dest->sa));
+		return;
+	}
+
+	if (addrlen < copy_len) {
+		copy_len = addrlen;
+	}
+
+	memcpy(&dest->sa.s, src, copy_len);
+
+	/* Zero out any remaining bytes in the union */
+	if (copy_len < sizeof(dest->sa)) {
+		memset((char *)&dest->sa.s + copy_len,
+		       0,
+		       sizeof(dest->sa) - copy_len);
+	}
+}
+
+/*
+ * Copy from swrap_address (with larger union) to swrap_address_ip_only.
+ * Only allows AF_INET and AF_INET6 addresses.
+ */
+static inline void swrap_address_ip_only_from_swrap_address(
+	struct swrap_address_ip_only *dest,
+	const struct swrap_address *src)
+{
+	const struct sockaddr *sa = &src->sa.s;
+	size_t expected_len;
+
+	/* Validate address family and size */
+	switch (sa->sa_family) {
+	case AF_INET:
+		expected_len = sizeof(struct sockaddr_in);
+		if (src->sa_socklen > expected_len) {
+			SWRAP_LOG(SWRAP_LOG_ERROR,
+				  "BUG: IPv4 address size=%u > "
+				  "sizeof(sockaddr_in)=%zu",
+				  src->sa_socklen,
+				  expected_len);
+			abort();
+		}
+		break;
+#ifdef HAVE_IPV6
+	case AF_INET6:
+		expected_len = sizeof(struct sockaddr_in6);
+		if (src->sa_socklen > expected_len) {
+			SWRAP_LOG(SWRAP_LOG_ERROR,
+				  "BUG: IPv6 address size=%u > "
+				  "sizeof(sockaddr_in6)=%zu",
+				  src->sa_socklen,
+				  expected_len);
+			abort();
+		}
+		break;
+#endif
+	default:
+		SWRAP_LOG(SWRAP_LOG_ERROR,
+			  "BUG: unexpected address family %d (expected AF_INET "
+			  "or AF_INET6)",
+			  sa->sa_family);
+		abort();
+	}
+
+	memcpy(&dest->sa.s, &src->sa.s, src->sa_socklen);
+
+	/* Zero out any remaining bytes in the union */
+	if (src->sa_socklen < sizeof(dest->sa)) {
+		memset((char *)&dest->sa.s + src->sa_socklen,
+		       0,
+		       sizeof(dest->sa) - src->sa_socklen);
+	}
+}
+
+/* Copy between two swrap_address_ip_only structs */
+static inline void swrap_address_ip_only_copy(
+	struct swrap_address_ip_only *dest,
+	const struct swrap_address_ip_only *src)
+{
+	socklen_t copy_len = swrap_address_ip_only_socklen(src->sa.s.sa_family);
+
+	memcpy(&dest->sa.s, &src->sa.s, copy_len);
+
+	/* Zero out any remaining bytes in the union */
+	if (copy_len < sizeof(dest->sa)) {
+		memset((char *)&dest->sa.s + copy_len,
+		       0,
+		       sizeof(dest->sa) - copy_len);
+	}
+}
+
 /*********************************************************
  * SWRAP LOADING LIBC FUNCTIONS
  *********************************************************/
@@ -1670,7 +1871,7 @@ static const struct in6_addr *swrap_ipv6(void)
 }
 #endif
 
-static void set_port(int family, int prt, struct swrap_address *addr)
+static void set_port(int family, int prt, struct swrap_address_ip_only *addr)
 {
 	switch (family) {
 	case AF_INET:
@@ -2534,15 +2735,15 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 		}
 
 		/* Store the bind address for connect() */
-		if (si->bindname.sa_socklen == 0) {
+		if (si->bindname.sa.s.sa_family == AF_UNSPEC) {
 			struct sockaddr_in bind_in;
 			socklen_t blen = sizeof(struct sockaddr_in);
 
 			ZERO_STRUCT(bind_in);
 			bind_in.sin_family = in->sin_family;
 			bind_in.sin_port = in->sin_port;
-			bind_in.sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
-			si->bindname.sa_socklen = blen;
+			bind_in.sin_addr.s_addr = htonl(
+				swrap_ipv4_iface(iface));
 			memcpy(&si->bindname.sa.in, &bind_in, blen);
 		}
 
@@ -2584,7 +2785,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 		}
 
 		/* Store the bind address for connect() */
-		if (si->bindname.sa_socklen == 0) {
+		if (si->bindname.sa.s.sa_family == AF_UNSPEC) {
 			struct sockaddr_in6 bind_in;
 			socklen_t blen = sizeof(struct sockaddr_in6);
 
@@ -2596,7 +2797,6 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 			bind_in.sin6_addr.s6_addr[15] = iface;
 
 			memcpy(&si->bindname.sa.in6, &bind_in, blen);
-			si->bindname.sa_socklen = blen;
 		}
 
 		break;
@@ -3820,8 +4020,7 @@ static int swrap_socket(int family, int type, int protocol, int allow_quic)
 			.sin_family = AF_INET,
 		};
 
-		si->myname.sa_socklen = sizeof(struct sockaddr_in);
-		memcpy(&si->myname.sa.in, &sin, si->myname.sa_socklen);
+		memcpy(&si->myname.sa.in, &sin, sizeof(struct sockaddr_in));
 		break;
 	}
 #ifdef HAVE_IPV6
@@ -3830,8 +4029,7 @@ static int swrap_socket(int family, int type, int protocol, int allow_quic)
 			.sin6_family = AF_INET6,
 		};
 
-		si->myname.sa_socklen = sizeof(struct sockaddr_in6);
-		memcpy(&si->myname.sa.in6, &sin6, si->myname.sa_socklen);
+		memcpy(&si->myname.sa.in6, &sin6, sizeof(struct sockaddr_in6));
 		break;
 	}
 #endif
@@ -4087,10 +4285,7 @@ static int swrap_accept(int s,
 
 	SWRAP_UNLOCK_SI(parent_si);
 
-	child_si->peername = (struct swrap_address) {
-		.sa_socklen = in_addr.sa_socklen,
-	};
-	memcpy(&child_si->peername.sa.ss, &in_addr.sa.ss, in_addr.sa_socklen);
+	swrap_address_ip_only_from_swrap_address(&child_si->peername, &in_addr);
 
 	if (addr != NULL && addrlen != NULL) {
 		size_t copy_len = MIN(*addrlen, in_addr.sa_socklen);
@@ -4117,10 +4312,8 @@ static int swrap_accept(int s,
 		  "accept() path=%s, fd=%d",
 		  un_my_addr.sa.un.sun_path, s);
 
-	child_si->myname = (struct swrap_address) {
-		.sa_socklen = in_my_addr.sa_socklen,
-	};
-	memcpy(&child_si->myname.sa.ss, &in_my_addr.sa.ss, in_my_addr.sa_socklen);
+	swrap_address_ip_only_from_swrap_address(&child_si->myname,
+						 &in_my_addr);
 
 	idx = swrap_create_socket(&new_si, fd);
 	if (idx == -1) {
@@ -4212,10 +4405,7 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 		in.sin_addr.s_addr = htonl(swrap_ipv4_iface(
 					   socket_wrapper_default_iface()));
 
-		si->myname = (struct swrap_address) {
-			.sa_socklen = sizeof(in),
-		};
-		memcpy(&si->myname.sa.in, &in, si->myname.sa_socklen);
+		memcpy(&si->myname.sa.in, &in, sizeof(in));
 		break;
 	}
 #ifdef HAVE_IPV6
@@ -4246,10 +4436,7 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 		in6.sin6_addr = *swrap_ipv6();
 		in6.sin6_addr.s6_addr[15] = socket_wrapper_default_iface();
 
-		si->myname = (struct swrap_address) {
-			.sa_socklen = sizeof(in6),
-		};
-		memcpy(&si->myname.sa.in6, &in6, si->myname.sa_socklen);
+		memcpy(&si->myname.sa.in6, &in6, sizeof(in6));
 		break;
 	}
 #endif
@@ -4387,11 +4574,9 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
 	}
 
 	if (ret == 0) {
-		si->peername = (struct swrap_address) {
-			.sa_socklen = addrlen,
-		};
-
-		memcpy(&si->peername.sa.ss, serv_addr, addrlen);
+		swrap_address_ip_only_from_sockaddr(&si->peername,
+						    serv_addr,
+						    addrlen);
 		si->connected = 1;
 
 		/*
@@ -4401,18 +4586,12 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
 		 * but here we have to update the name so getsockname()
 		 * returns correct information.
 		 */
-		if (si->bindname.sa_socklen > 0) {
-			si->myname = (struct swrap_address) {
-				.sa_socklen = si->bindname.sa_socklen,
-			};
-
-			memcpy(&si->myname.sa.ss,
-			       &si->bindname.sa.ss,
-			       si->bindname.sa_socklen);
+		if (swrap_address_ip_only_len(&si->bindname) > 0) {
+			swrap_address_ip_only_copy(&si->myname, &si->bindname);
 
 			/* Cleanup bindname */
-			si->bindname = (struct swrap_address) {
-				.sa_socklen = 0,
+			si->bindname = (struct swrap_address_ip_only){
+				.sa.s.sa_family = AF_UNSPEC,
 			};
 		}
 
@@ -4446,6 +4625,7 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
 	struct swrap_sockaddr_buf buf = {};
 	int ret_errno = errno;
 	int bind_error = 0;
+	int bcast = 0;
 #if 0 /* FIXME */
 	bool in_use;
 #endif
@@ -4516,20 +4696,21 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
 	}
 #endif
 
-	si->myname.sa_socklen = addrlen;
-	memcpy(&si->myname.sa.ss, myaddr, addrlen);
+	swrap_address_ip_only_from_sockaddr(&si->myname, myaddr, addrlen);
 
 	ret = sockaddr_convert_to_un(si,
 				     myaddr,
 				     addrlen,
 				     &un_addr.sa.un,
 				     1,
-				     &si->bcast);
+				     &bcast);
 	if (ret == -1) {
 		ret_errno = errno;
 		goto out;
 	}
 
+	si->bcast = bcast;
+
 	unlink(un_addr.sa.un.sun_path);
 
 	ret = libc_bind(s, &un_addr.sa.s, un_addr.sa_socklen);
@@ -4883,20 +5064,19 @@ static int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
 
 	SWRAP_LOCK_SI(si);
 
-	if (si->peername.sa_socklen == 0)
-	{
+	if (swrap_address_ip_only_len(&si->peername) == 0) {
 		errno = ENOTCONN;
 		goto out;
 	}
 
-	len = MIN(*addrlen, si->peername.sa_socklen);
+	len = MIN(*addrlen, swrap_address_ip_only_len(&si->peername));
 	if (len == 0) {
 		ret = 0;
 		goto out;
 	}
 
-	memcpy(name, &si->peername.sa.ss, len);
-	*addrlen = si->peername.sa_socklen;
+	memcpy(name, &si->peername.sa.s, len);
+	*addrlen = swrap_address_ip_only_len(&si->peername);
 
 	ret = 0;
 out:
@@ -4930,14 +5110,14 @@ static int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)


-- 
Socket Wrapper Repository



More information about the samba-cvs mailing list