Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: vdavydov.dev@gmail.com
Subject: [PATCH 1/1] sio: implement getsockname wrapper
Date: Wed,  6 Mar 2019 00:43:02 +0300	[thread overview]
Message-ID: <7405ac9cec20bd309712418674694a66ea31590b.1551821931.git.v.shpilevoy@tarantool.org> (raw)

SWIM wants to allow to bind to zero ports so as the kernel could
choose any free port automatically. It is needed mainly for
tests.

Zero port means that a real port is known only after bind() has
called, and getsockname() should be used to get it. SWIM uses sio
library for such lowlevel API. This is why that function is added
to sio.

Needed for #3234
---
Branch: https://github.com/tarantool/tarantool/tree/gerold103/sio-getsockname

 src/lib/core/sio.c   | 10 ++++++++++
 src/lib/core/sio.h   | 15 +++++++++++++++
 test/unit/sio.c      | 23 ++++++++++++++++++++++-
 test/unit/sio.result |  9 ++++++++-
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/src/lib/core/sio.c b/src/lib/core/sio.c
index bb8e508d9..8f25b8159 100644
--- a/src/lib/core/sio.c
+++ b/src/lib/core/sio.c
@@ -293,6 +293,16 @@ sio_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
 	return 0;
 }
 
+int
+sio_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
+{
+	if (getsockname(fd, addr, addrlen) < 0) {
+		diag_set(SocketError, sio_socketname(fd), "getsockname");
+		return -1;
+	}
+	return 0;
+}
+
 const char *
 sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen)
 {
diff --git a/src/lib/core/sio.h b/src/lib/core/sio.h
index f0998e2f3..093b5f5ba 100644
--- a/src/lib/core/sio.h
+++ b/src/lib/core/sio.h
@@ -86,6 +86,21 @@ sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen);
 int
 sio_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen);
 
+/**
+ * Fill @a addr with a real address currently bound to @a fd
+ * socket.
+ *
+ * @param fd Socket.
+ * @param[out] addr An address structure to fill.
+ * @param[in][out] addlen On input it is a size of @a addr as a
+ *                 buffer. On output it becomes a size of a new
+ *                 content of @a addr.
+ * @retval 0 Success.
+ * @retval -1 Error.
+ */
+int
+sio_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen);
+
 /**
  * Advance write position in the iovec array
  * based on its current value and the number of
diff --git a/test/unit/sio.c b/test/unit/sio.c
index 84a86aac3..dffdd5592 100644
--- a/test/unit/sio.c
+++ b/test/unit/sio.c
@@ -78,6 +78,26 @@ check_uri_to_addr(void)
 	footer();
 }
 
+static void
+check_auto_bind(void)
+{
+	header();
+	plan(3);
+
+	struct sockaddr_in addr;
+	socklen_t addrlen = sizeof(addr);
+	sio_uri_to_addr("127.0.0.1:0", (struct sockaddr *) &addr);
+	int fd = sio_socket(AF_INET, SOCK_STREAM, 0);
+	is(sio_bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 0,
+	   "bind to 0 works");
+	is(sio_getsockname(fd, (struct sockaddr *) &addr, &addrlen), 0,
+	   "getsockname works on 0 bind");
+	isnt(addr.sin_port, 0, "a real port is returned");
+
+	check_plan();
+	footer();
+}
+
 int
 main(void)
 {
@@ -85,8 +105,9 @@ main(void)
 	fiber_init(fiber_c_invoke);
 
 	header();
-	plan(1);
+	plan(2);
 	check_uri_to_addr();
+	check_auto_bind();
 	int rc = check_plan();
 	footer();
 
diff --git a/test/unit/sio.result b/test/unit/sio.result
index 32a5babae..e5712ad3c 100644
--- a/test/unit/sio.result
+++ b/test/unit/sio.result
@@ -1,5 +1,5 @@
 	*** main ***
-1..1
+1..2
 	*** check_uri_to_addr ***
     1..18
     ok 1 - invalid uri is detected
@@ -22,4 +22,11 @@
     ok 18 - invalid IP
 ok 1 - subtests
 	*** check_uri_to_addr: done ***
+	*** check_auto_bind ***
+    1..3
+    ok 1 - bind to 0 works
+    ok 2 - getsockname works on 0 bind
+    ok 3 - a real port is returned
+ok 2 - subtests
+	*** check_auto_bind: done ***
 	*** main: done ***
-- 
2.17.2 (Apple Git-113)

             reply	other threads:[~2019-03-05 21:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-05 21:43 Vladislav Shpilevoy [this message]
2019-03-06 14:28 ` [tarantool-patches] " Konstantin Osipov
2019-03-06 15:19   ` [tarantool-patches] " Vladislav Shpilevoy

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=7405ac9cec20bd309712418674694a66ea31590b.1551821931.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH 1/1] sio: implement getsockname wrapper' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox