* [PATCH 1/1] sio: implement getsockname wrapper
@ 2019-03-05 21:43 Vladislav Shpilevoy
2019-03-06 14:28 ` [tarantool-patches] " Konstantin Osipov
0 siblings, 1 reply; 3+ messages in thread
From: Vladislav Shpilevoy @ 2019-03-05 21:43 UTC (permalink / raw)
To: tarantool-patches; +Cc: vdavydov.dev
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)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [tarantool-patches] [PATCH 1/1] sio: implement getsockname wrapper
2019-03-05 21:43 [PATCH 1/1] sio: implement getsockname wrapper Vladislav Shpilevoy
@ 2019-03-06 14:28 ` Konstantin Osipov
2019-03-06 15:19 ` [tarantool-patches] " Vladislav Shpilevoy
0 siblings, 1 reply; 3+ messages in thread
From: Konstantin Osipov @ 2019-03-06 14:28 UTC (permalink / raw)
To: tarantool-patches; +Cc: vdavydov.dev
* Vladislav Shpilevoy <v.shpilevoy@tarantool.org> [19/03/06 10:25]:
> 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.
>
OK to push.
--
Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
http://tarantool.io - www.twitter.com/kostja_osipov
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [tarantool-patches] Re: [PATCH 1/1] sio: implement getsockname wrapper
2019-03-06 14:28 ` [tarantool-patches] " Konstantin Osipov
@ 2019-03-06 15:19 ` Vladislav Shpilevoy
0 siblings, 0 replies; 3+ messages in thread
From: Vladislav Shpilevoy @ 2019-03-06 15:19 UTC (permalink / raw)
To: tarantool-patches, Konstantin Osipov; +Cc: vdavydov.dev
Pushed to 2.1.
On 06/03/2019 17:28, Konstantin Osipov wrote:
> * Vladislav Shpilevoy <v.shpilevoy@tarantool.org> [19/03/06 10:25]:
>> 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.
>>
>
> OK to push.
>
>
> --
> Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
> http://tarantool.io - www.twitter.com/kostja_osipov
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-03-06 15:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-05 21:43 [PATCH 1/1] sio: implement getsockname wrapper Vladislav Shpilevoy
2019-03-06 14:28 ` [tarantool-patches] " Konstantin Osipov
2019-03-06 15:19 ` [tarantool-patches] " Vladislav Shpilevoy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox