[SCM] Socket Wrapper Repository - branch master updated

Andreas Schneider asn at samba.org
Tue May 17 09:44:05 UTC 2016


The branch, master has been updated
       via  0c5a43a tests: Add test_connect_sendto_null_ipv4() test
       via  e443d67 tests: Add a udp test case for sendto() after a connect()
       via  ec18c95 swrap: Fix sendto() with connected sockets
       via  395dc41 swrap: Allow to open RAW sockets when loaded
      from  d731d16 swrap: Add support for accept4()

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


- Log -----------------------------------------------------------------
commit 0c5a43a8a05d74de6c3ffa4f6c42429c2861d75f
Author: Andreas Schneider <asn at samba.org>
Date:   Tue May 17 10:01:38 2016 +0200

    tests: Add test_connect_sendto_null_ipv4() test
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Ralph Boehme <slow at samba.org>

commit e443d67914030e7f2e1126a82003e1bada5cbbac
Author: Richard Sharpe <rsharpe at samba.org>
Date:   Wed May 11 17:10:54 2016 -0700

    tests: Add a udp test case for sendto() after a connect()
    
    Here, we do the same as net ads dns gethostbyname. That is, we
    connect on a UDP socket and then send a sendto with a dest address (the
    same as the one we connected on.) and then a recvfrom etc.
    
    Signed-of-by: Richard Sharpe <rsharpe at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Ralph Boehme <slow at samba.org>

commit ec18c955e13ea2a294339e4c9ee977a769fbe903
Author: Richard Sharpe <rsharpe at samba.org>
Date:   Wed May 11 17:03:46 2016 -0700

    swrap: Fix sendto() with connected sockets
    
    Let the socket wrapper code work with the net ads dns gethostbyname etc
    code (lib/addn/dnssock.c) which uses connect on a UDP socket before then
    using sendto and recvfrom.
    
    Here, we make sure we don't error out in that case.
    
    Tested by creating a test case for this and then observing that:
    
    1. The test case works when the socket wrapper lib is not being used
       ie, run the test directly after defining some SHELL variables.
    
    2. That the test case fails when run with the un modified socket
       wrapper code.
    
    3. Apply this fix and observe that it runs correctly.
    
    Pair-Programmed-With: Andreas Schneider <asn at samba.org>
    Signed-off-by: Richard Sharpe <rsharpe at samba.org>
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Ralph Boehme <slow at samba.org>

commit 395dc418d996d343cfaeb03be5b720c9e52d1d46
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Apr 26 18:10:52 2016 +0200

    swrap: Allow to open RAW sockets when loaded
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Ralph Boehme <slow at samba.org>

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

Summary of changes:
 src/socket_wrapper.c                  |  40 ++++++++++---
 tests/test_echo_udp_sendto_recvfrom.c | 104 ++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 9 deletions(-)


Changeset truncated at 500 lines:

diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c
index 6e65a7e..ba289e6 100644
--- a/src/socket_wrapper.c
+++ b/src/socket_wrapper.c
@@ -2407,6 +2407,9 @@ static int swrap_socket(int family, int type, int protocol)
 #ifdef AF_NETLINK
 	case AF_NETLINK:
 #endif /* AF_NETLINK */
+#ifdef AF_PACKET
+	case AF_PACKET:
+#endif /* AF_PACKET */
 	case AF_UNIX:
 		return libc_socket(family, type, protocol);
 	default:
@@ -3919,9 +3922,15 @@ static ssize_t swrap_sendmsg_before(int fd,
 	}
 	case SOCK_DGRAM:
 		if (si->connected) {
-			if (msg->msg_name) {
-				errno = EISCONN;
-				return -1;
+			if (msg->msg_name != NULL) {
+				/*
+				 * We are dealing with unix sockets and if we
+				 * are connected, we should only talk to the
+				 * connected unix path. Using the fd to send
+				 * to another server would be hard to achieve.
+				 */
+				msg->msg_name = NULL;
+				msg->msg_namelen = 0;
 			}
 		} else {
 			const struct sockaddr *msg_name;
@@ -4468,12 +4477,25 @@ static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
 		return len;
 	}
 
-	ret = libc_sendto(s,
-			  buf,
-			  len,
-			  flags,
-			  (struct sockaddr *)msg.msg_name,
-			  msg.msg_namelen);
+	/*
+	 * If it is a dgram socket and we are connected, don't include the
+	 * 'to' address.
+	 */
+	if (si->type == SOCK_DGRAM && si->connected) {
+		ret = libc_sendto(s,
+				  buf,
+				  len,
+				  flags,
+				  NULL,
+				  0);
+	} else {
+		ret = libc_sendto(s,
+				  buf,
+				  len,
+				  flags,
+				  (struct sockaddr *)msg.msg_name,
+				  msg.msg_namelen);
+	}
 
 	swrap_sendmsg_after(s, si, &msg, to, ret);
 
diff --git a/tests/test_echo_udp_sendto_recvfrom.c b/tests/test_echo_udp_sendto_recvfrom.c
index 79948ce..bb22371 100644
--- a/tests/test_echo_udp_sendto_recvfrom.c
+++ b/tests/test_echo_udp_sendto_recvfrom.c
@@ -192,6 +192,104 @@ static void test_sendto_recvfrom_ipv6(void **state)
 }
 #endif
 
+static void test_connect_sendto_ipv4(void **state)
+{
+	struct torture_address addr = {
+		.sa_socklen = sizeof(struct sockaddr_in),
+	};
+	char send_buf[] = "packet.0";
+	char recv_buf[64] = {0};
+	ssize_t ret;
+	int rc;
+	int s;
+
+	(void) state; /* unused */
+
+	addr.sa.in.sin_family = AF_INET;
+	addr.sa.in.sin_port = htons(torture_server_port());
+
+	rc = inet_pton(AF_INET,
+		       torture_server_address(AF_INET),
+		       &addr.sa.in.sin_addr);
+	assert_int_equal(rc, 1);
+
+	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	assert_int_not_equal(s, -1);
+
+	/* Now, connect */
+	rc = connect(s, &addr.sa.s, addr.sa_socklen);
+	assert_return_code(rc, errno);
+
+	ret = sendto(s,
+		     send_buf,
+		     sizeof(send_buf),
+		     0,
+		     &addr.sa.s,
+		     addr.sa_socklen);
+	assert_return_code(ret, errno);
+
+	ret = recvfrom(s,
+		       recv_buf,
+		       sizeof(recv_buf),
+		       0,
+		       NULL,
+		       0);
+	assert_return_code(ret, errno);
+
+	assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));
+
+	close(s);
+}
+
+static void test_connect_sendto_null_ipv4(void **state)
+{
+	struct torture_address addr = {
+		.sa_socklen = sizeof(struct sockaddr_in),
+	};
+	char send_buf[] = "packet.0";
+	char recv_buf[64] = {0};
+	ssize_t ret;
+	int rc;
+	int s;
+
+	(void) state; /* unused */
+
+	addr.sa.in.sin_family = AF_INET;
+	addr.sa.in.sin_port = htons(torture_server_port());
+
+	rc = inet_pton(AF_INET,
+		       torture_server_address(AF_INET),
+		       &addr.sa.in.sin_addr);
+	assert_int_equal(rc, 1);
+
+	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	assert_int_not_equal(s, -1);
+
+	/* Now, connect */
+	rc = connect(s, &addr.sa.s, addr.sa_socklen);
+	assert_return_code(rc, errno);
+
+	ret = sendto(s,
+		     send_buf,
+		     sizeof(send_buf),
+		     0,
+		     NULL,
+		     0);
+	assert_return_code(ret, errno);
+
+	ret = recvfrom(s,
+		       recv_buf,
+		       sizeof(recv_buf),
+		       0,
+		       NULL,
+		       0);
+	assert_return_code(ret, errno);
+
+	assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));
+
+	close(s);
+}
+
 int main(void) {
 	int rc;
 
@@ -204,6 +302,12 @@ int main(void) {
 						setup_echo_srv_udp_ipv6,
 						teardown),
 #endif
+		cmocka_unit_test_setup_teardown(test_connect_sendto_ipv4,
+						setup_echo_srv_udp_ipv4,
+						teardown),
+		cmocka_unit_test_setup_teardown(test_connect_sendto_null_ipv4,
+						setup_echo_srv_udp_ipv4,
+						teardown),
 	};
 
 	rc = cmocka_run_group_tests(sendto_tests, NULL, NULL);


-- 
Socket Wrapper Repository



More information about the samba-cvs mailing list