Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com,
	sergepetrenko@tarantool.org
Subject: [Tarantool-patches] [PATCH 13/16] sio: rework sio_strfaddr()
Date: Sat, 20 Mar 2021 01:42:35 +0100	[thread overview]
Message-ID: <393f1f92a15c7c1ec26f27daf1f6f88bd052da5a.1616200860.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1616200860.git.v.shpilevoy@tarantool.org>

The function was overcomplicated, and made it harder to update it
in the next patches with functional changes.

The main source of the complication was usage of both inet_ntoa()
and getnameinfo(). The latter is more universal, it can cover the
case of the former.

The patch makes it use only getnameinfo() for IP addresses
regardless of v4 or v6.

Needed for #5632
---
 src/lib/core/sio.c | 42 ++++++++++++++++--------------------------
 1 file changed, 16 insertions(+), 26 deletions(-)

diff --git a/src/lib/core/sio.c b/src/lib/core/sio.c
index d008526d5..deffee86c 100644
--- a/src/lib/core/sio.c
+++ b/src/lib/core/sio.c
@@ -329,33 +329,23 @@ sio_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
 const char *
 sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen)
 {
-	switch (addr->sa_family) {
-		case AF_UNIX: {
-			struct sockaddr_un *u = (struct sockaddr_un *) addr;
-			if (addrlen >= sizeof(*u))
-				return tt_sprintf("unix/:%s", u->sun_path);
-			else
-				return tt_sprintf("unix/:(socket)");
-			break;
-		}
-		case AF_INET: {
-			struct sockaddr_in *in = (struct sockaddr_in *) addr;
-			return tt_snprintf(SERVICE_NAME_MAXLEN, "%s:%d",
-					   inet_ntoa(in->sin_addr),
-					   ntohs(in->sin_port));
-		}
-		default: {
-			char host[NI_MAXHOST], serv[NI_MAXSERV];
-			if (getnameinfo(addr, addrlen, host, sizeof(host),
-					serv, sizeof(serv),
-					NI_NUMERICHOST | NI_NUMERICSERV) != 0)
-				return tt_sprintf("(host):(port)");
-
-			return tt_snprintf(NI_MAXHOST + NI_MAXSERV, "[%s]:%s",
-					   host, serv);
-		}
+	if (addr->sa_family == AF_UNIX) {
+		struct sockaddr_un *u = (struct sockaddr_un *)addr;
+		if (addrlen >= sizeof(*u))
+			return tt_sprintf("unix/:%s", u->sun_path);
+		else
+			return tt_sprintf("unix/:(socket)");
+	} else {
+		char host[NI_MAXHOST], serv[NI_MAXSERV];
+		int flags = NI_NUMERICHOST | NI_NUMERICSERV;
+		if (getnameinfo(addr, addrlen, host, sizeof(host), serv,
+				sizeof(serv), flags) != 0)
+			return tt_sprintf("(host):(port)");
+		else if (addr->sa_family == AF_INET)
+			return tt_sprintf("%s:%s", host, serv);
+		else
+			return tt_sprintf("[%s]:%s", host, serv);
 	}
-	unreachable();
 }
 
 int
-- 
2.24.3 (Apple Git-128)


  parent reply	other threads:[~2021-03-20  0:44 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-20  0:42 [Tarantool-patches] [PATCH 00/16] Cord buffer, static alloc, and Lua GC bug Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 01/16] fio: don't use shared buffer in pread() Vladislav Shpilevoy via Tarantool-patches
2021-03-22  7:19   ` Cyrill Gorcunov via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 10/16] uri: replace static_alloc with ffi stash and ibuf Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 11/16] buffer: remove static_alloc() from Lua Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 12/16] lua: use lua_pushfstring() instead of tt_sprintf() Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 14/16] sio: increase SERVICE_NAME_MAXLEN size Vladislav Shpilevoy via Tarantool-patches
2021-03-21 21:58   ` Cyrill Gorcunov via Tarantool-patches
2021-03-22 22:32     ` Vladislav Shpilevoy via Tarantool-patches
2021-03-23  6:56       ` Cyrill Gorcunov via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 15/16] sio: introduce and use sio_snprintf() Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 16/16] buffer: remove Lua registers Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 02/16] test: don't use IBUF_SHARED in the tests Vladislav Shpilevoy via Tarantool-patches
2021-03-22  7:35   ` Cyrill Gorcunov via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 03/16] tuple: pass global ibuf explicitly where possible Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 04/16] iconv: take errno before reseting the context Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 05/16] cord_buf: introduce cord_buf API Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 06/16] cord_buf: introduce ownership management Vladislav Shpilevoy via Tarantool-patches
2021-03-22 16:48   ` Serge Petrenko via Tarantool-patches
2021-03-22 22:32     ` Vladislav Shpilevoy via Tarantool-patches
2021-03-23  7:46       ` Serge Petrenko via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 07/16] buffer: implement ffi stash Vladislav Shpilevoy via Tarantool-patches
2021-03-23  0:29   ` Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 08/16] uuid: replace static_alloc with " Vladislav Shpilevoy via Tarantool-patches
2021-03-20  0:42 ` [Tarantool-patches] [PATCH 09/16] uuid: drop tt_uuid_str() from Lua Vladislav Shpilevoy via Tarantool-patches
2021-03-21 16:38 ` [Tarantool-patches] [PATCH 00/16] Cord buffer, static alloc, and Lua GC bug Vladislav Shpilevoy via Tarantool-patches
2021-03-22  7:52 ` Cyrill Gorcunov via Tarantool-patches
2021-03-22  7:56 ` Konstantin Osipov via Tarantool-patches
2021-03-22 17:17 ` Serge Petrenko via Tarantool-patches
2021-03-23 23:45 ` Vladislav Shpilevoy via Tarantool-patches
2021-03-24 13:28 ` Kirill Yukhin via Tarantool-patches

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=393f1f92a15c7c1ec26f27daf1f6f88bd052da5a.1616200860.git.v.shpilevoy@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=sergepetrenko@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 13/16] sio: rework sio_strfaddr()' \
    /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