public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Gabriel Krisman Bertazi <krisman@suse.de>
To: Jens Axboe <axboe@kernel.dk>
Cc: Gabriel Krisman Bertazi <krisman@suse.de>,
	io-uring@vger.kernel.org, csander@purestorage.com
Subject: [PATCH liburing v3 3/4] bind-listen.t: Add tests for getsockname
Date: Wed,  3 Dec 2025 14:52:17 -0500	[thread overview]
Message-ID: <20251203195223.3578559-4-krisman@suse.de> (raw)
In-Reply-To: <20251203195223.3578559-1-krisman@suse.de>

Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
---
 test/bind-listen.c | 99 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 96 insertions(+), 3 deletions(-)

diff --git a/test/bind-listen.c b/test/bind-listen.c
index a468aa94..38200879 100644
--- a/test/bind-listen.c
+++ b/test/bind-listen.c
@@ -202,7 +202,7 @@ static int test_good_server(unsigned int ring_flags)
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
 	struct io_uring ring;
-	int ret;
+	int ret, port;
 	int fds[3];
 	char buf[1024];
 
@@ -235,7 +235,7 @@ static int test_good_server(unsigned int ring_flags)
 		return T_SETUP_SKIP;
 	}
 
-	/* Wait for a request */
+	/* Wait for a connection */
 	sqe = io_uring_get_sqe(&ring);
 	io_uring_prep_accept_direct(sqe, SRV_INDEX, NULL, NULL, 0, CONN_INDEX);
 	sqe->flags |= IOSQE_FIXED_FILE;
@@ -248,6 +248,22 @@ static int test_good_server(unsigned int ring_flags)
 	}
 	io_uring_cqe_seen(&ring, cqe);
 
+	/* Test that getsockname on the peer (getpeername) yields a
+	 * sane result.
+	 */
+	port = saddr.sin_port;
+	saddr.sin_port = 0;
+	if (do_getsockname(&ring, CLI_INDEX, 1,
+			   (struct sockaddr*)&saddr, &saddr_len))
+		return T_EXIT_FAIL;
+
+	if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) ||
+	    saddr.sin_port != port) {
+		fprintf(stderr, "getsockname peer got wrong address: %s:%d\n",
+			inet_ntoa(saddr.sin_addr), saddr.sin_port);
+		return T_EXIT_FAIL;
+	}
+
 	sqe = io_uring_get_sqe(&ring);
 	io_uring_prep_recv(sqe, CONN_INDEX, buf, sizeof(buf), 0);
 	sqe->flags |= IOSQE_FIXED_FILE;
@@ -424,6 +440,77 @@ fail:
 	return ret;
 }
 
+static int test_bad_sockname(void)
+{
+	struct sockaddr_in saddr;
+	socklen_t saddr_len;
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct io_uring ring;
+	int sock = -1, err;
+	int ret = T_EXIT_FAIL;
+
+	memset(&saddr, 0, sizeof(struct sockaddr_in));
+	saddr.sin_family = AF_INET;
+	saddr.sin_port = htons(8001);
+	saddr.sin_addr.s_addr = htons(INADDR_ANY);
+
+	err = t_create_ring(1, &ring, 0);
+	if (err < 0) {
+		fprintf(stderr, "queue_init: %d\n", err);
+		return T_SETUP_SKIP;
+	}
+
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	if (sock < 0) {
+		perror("socket");
+		goto fail;
+	}
+
+	err = t_bind_ephemeral_port(sock, &saddr);
+	if (err) {
+		fprintf(stderr, "bind: %s\n", strerror(-err));
+		goto fail;
+	}
+
+	/* getsockname on a !socket fd.  with getsockname(2), this would
+	 * return -ENOTSOCK, but we can't do it in an io_uring_cmd.
+	 */
+	sqe = io_uring_get_sqe(&ring);
+	saddr_len = sizeof(saddr);
+	io_uring_prep_cmd_getsockname(sqe, 1, (struct sockaddr*)&saddr, &saddr_len, 0);
+	err = io_uring_submit(&ring);
+	if (err < 0)
+		goto fail;
+	err = io_uring_wait_cqe(&ring, &cqe);
+	if (err)
+		goto fail;
+	if (cqe->res != -ENOTSUP)
+		goto fail;
+	io_uring_cqe_seen(&ring, cqe);
+
+	/* getsockname with weird parameters */
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_cmd_getsockname(sqe, sock, (struct sockaddr*)&saddr,
+				      &saddr_len, 3);
+	err = io_uring_submit(&ring);
+	if (err < 0)
+		goto fail;
+	err = io_uring_wait_cqe(&ring, &cqe);
+	if (err)
+		goto fail;
+	if (cqe->res != -EINVAL)
+		goto fail;
+	io_uring_cqe_seen(&ring, cqe);
+
+	ret = T_EXIT_PASS;
+fail:
+	io_uring_queue_exit(&ring);
+	if (sock != -1)
+		close(sock);
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	struct io_uring_probe *probe;
@@ -472,6 +559,12 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "bad listen failed\n");
 		return T_EXIT_FAIL;
 	}
-
+	if (!no_getsockname) {
+		ret = test_bad_sockname();
+		if (ret) {
+			fprintf(stderr, "bad sockname failed\n");
+			return T_EXIT_FAIL;
+		}
+	}
 	return T_EXIT_PASS;
 }
-- 
2.52.0


  parent reply	other threads:[~2025-12-03 19:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-03 19:52 [PATCH liburing v3 0/4] liburing: getsockname support Gabriel Krisman Bertazi
2025-12-03 19:52 ` [PATCH liburing v3 1/4] liburing: Introduce getsockname operation Gabriel Krisman Bertazi
2025-12-03 19:52 ` [PATCH liburing v3 2/4] test/bind-listen.t: Use ephemeral port Gabriel Krisman Bertazi
2025-12-03 19:52 ` Gabriel Krisman Bertazi [this message]
2025-12-03 19:52 ` [PATCH liburing v3 4/4] man/io_uring_prep_getsockname.3: Add man page Gabriel Krisman Bertazi
2025-12-03 20:23 ` [PATCH liburing v3 0/4] liburing: getsockname support Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251203195223.3578559-4-krisman@suse.de \
    --to=krisman@suse.de \
    --cc=axboe@kernel.dk \
    --cc=csander@purestorage.com \
    --cc=io-uring@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox