Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 4/6] swim: wrap sio_strfaddr()
Date: Wed, 24 Apr 2019 17:36:18 +0300	[thread overview]
Message-ID: <73ba32c413ce9371b845ac161a0d5ea698fac575.1556116199.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1556116199.git.v.shpilevoy@tarantool.org>

SIO provides a function sio_strfaddr() to obtain string
representation of arbitrary struct sockaddr. And it uses
singleton buffer to store results, because some of them can't
fit into tt_static_buf() according to POSIX name limits.

SWIM uses only AF_INET addresses, they are short enough to fit
into tt_static_buf(). Also SWIM is going to call sio_strfaddr()
on more than 1 address in a row in subsequent patches, and
singleton buffer does not work here - each call will overwrite
result of the previous. Besides, SWIM never uses struct sockaddr
type. All these reasons are sufficient to wrap sio_strfaddr()
with encapsulated cast to struct sockaddr *, and copying on
tt_static_buf().

Part of #3234
---
 src/lib/swim/swim.c       | 10 +++-------
 src/lib/swim/swim_io.c    |  5 ++---
 src/lib/swim/swim_proto.c |  8 ++++++++
 src/lib/swim/swim_proto.h | 10 ++++++++++
 4 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/lib/swim/swim.c b/src/lib/swim/swim.c
index 230ec52d4..bb1ded713 100644
--- a/src/lib/swim/swim.c
+++ b/src/lib/swim/swim.c
@@ -1086,8 +1086,7 @@ swim_send_fd_msg(struct swim *swim, struct swim_task *task,
 	assert(map_size == 2);
 	mp_encode_map(header, map_size);
 	say_verbose("SWIM %d: schedule %s to %s", swim_fd(swim),
-		    swim_fd_msg_type_strs[type],
-		    sio_strfaddr((struct sockaddr *) dst, sizeof(*dst)));
+		    swim_fd_msg_type_strs[type], swim_inaddr_str(dst));
 	swim_task_send(task, dst, &swim->scheduler);
 }
 
@@ -1732,9 +1731,7 @@ swim_info(struct swim *swim, struct info_handler *info)
 	     node = mh_next(swim->members, node)) {
 		struct swim_member *m =
 			*mh_swim_table_node(swim->members, node);
-		info_table_begin(info,
-				 sio_strfaddr((struct sockaddr *) &m->addr,
-					      sizeof(m->addr)));
+		info_table_begin(info, swim_inaddr_str(&m->addr));
 		info_append_str(info, "status",
 				swim_member_status_strs[m->status]);
 		info_append_str(info, "uuid", swim_uuid_str(&m->uuid));
@@ -1888,8 +1885,7 @@ swim_iterator_close(struct swim_iterator *iterator)
 const char *
 swim_member_uri(const struct swim_member *member)
 {
-	return sio_strfaddr((const struct sockaddr *) &member->addr,
-			    sizeof(member->addr));
+	return swim_inaddr_str(&member->addr);
 }
 
 const struct tt_uuid *
diff --git a/src/lib/swim/swim_io.c b/src/lib/swim/swim_io.c
index 7d6addf02..a55c15f30 100644
--- a/src/lib/swim/swim_io.c
+++ b/src/lib/swim/swim_io.c
@@ -321,8 +321,7 @@ swim_scheduler_on_output(struct ev_loop *loop, struct ev_io *io, int events)
 		rlist_shift_entry(&scheduler->queue_output, struct swim_task,
 				  in_queue_output);
 	say_verbose("SWIM %d: send %s to %s", swim_scheduler_fd(scheduler),
-		    task->desc, sio_strfaddr((struct sockaddr *) &task->dst,
-					     sizeof(task->dst)));
+		    task->desc, swim_inaddr_str(&task->dst));
 	swim_packet_build_meta(&task->packet, &scheduler->transport.addr);
 	int rc = swim_transport_send(&scheduler->transport, task->packet.buf,
 				     task->packet.pos - task->packet.buf,
@@ -353,7 +352,7 @@ swim_scheduler_on_input(struct ev_loop *loop, struct ev_io *io, int events)
 		return;
 	}
 	say_verbose("SWIM %d: received from %s", swim_scheduler_fd(scheduler),
-		    sio_strfaddr((struct sockaddr *) &src, len));
+		    swim_inaddr_str(&src));
 	struct swim_meta_def meta;
 	const char *pos = buf, *end = pos + size;
 	if (swim_meta_def_decode(&meta, &pos, end) < 0)
diff --git a/src/lib/swim/swim_proto.c b/src/lib/swim/swim_proto.c
index 91500518d..796559e8e 100644
--- a/src/lib/swim/swim_proto.c
+++ b/src/lib/swim/swim_proto.c
@@ -33,8 +33,16 @@
 #include "say.h"
 #include "version.h"
 #include "diag.h"
+#include "sio.h"
 #include <sys/socket.h> /* AF_INET for FreeBSD. */
 
+const char *
+swim_inaddr_str(const struct sockaddr_in *addr)
+{
+	return tt_sprintf("%s", sio_strfaddr((struct sockaddr *) addr,
+					     sizeof(*addr)));
+}
+
 const char *swim_member_status_strs[] = {
 	"alive",
 	"dead",
diff --git a/src/lib/swim/swim_proto.h b/src/lib/swim/swim_proto.h
index 59a4d2086..ddf9e28db 100644
--- a/src/lib/swim/swim_proto.h
+++ b/src/lib/swim/swim_proto.h
@@ -540,4 +540,14 @@ swim_inaddr_eq(const struct sockaddr_in *a1, const struct sockaddr_in *a2)
 	       a1->sin_addr.s_addr == a2->sin_addr.s_addr;
 }
 
+/**
+ * A wrapper around sio_strfaddr() so as to 1) do not clog SWIM
+ * code with huge casts to 'struct sockaddr *' and passes of
+ * sizeof(struct sockaddr_in); 2) copy on tt_static_buf instead of
+ * internal singleton sio buffer - otherwise non-first invocation
+ * of sio_strfaddr() rewrites the previous one.
+ */
+const char *
+swim_inaddr_str(const struct sockaddr_in *addr);
+
 #endif /* TARANTOOL_SWIM_PROTO_H_INCLUDED */
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-04-24 14:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-24 14:36 [tarantool-patches] [PATCH 0/6] swim suspicion Vladislav Shpilevoy
2019-04-24 14:36 ` [tarantool-patches] [PATCH 1/6] test: rename swim_cluster_node to swim_cluster_member Vladislav Shpilevoy
2019-04-24 16:37   ` [tarantool-patches] " Konstantin Osipov
2019-04-24 14:36 ` [tarantool-patches] [PATCH 2/6] test: remove swim packet filter destructors Vladislav Shpilevoy
2019-04-24 16:37   ` [tarantool-patches] " Konstantin Osipov
2019-04-24 14:36 ` [tarantool-patches] [PATCH 3/6] test: introduce swim packet filter by destination address Vladislav Shpilevoy
2019-04-24 16:38   ` [tarantool-patches] " Konstantin Osipov
2019-04-24 14:36 ` Vladislav Shpilevoy [this message]
2019-04-24 16:40   ` [tarantool-patches] Re: [PATCH 4/6] swim: wrap sio_strfaddr() Konstantin Osipov
2019-04-24 20:23     ` Vladislav Shpilevoy
2019-04-25 10:34       ` Konstantin Osipov
2019-04-25 13:50         ` Vladislav Shpilevoy
2019-04-24 14:36 ` [tarantool-patches] [PATCH 5/6] swim: introduce routing Vladislav Shpilevoy
2019-04-24 16:46   ` [tarantool-patches] " Konstantin Osipov
2019-04-24 20:25     ` Vladislav Shpilevoy
2019-04-25 10:39       ` Konstantin Osipov
2019-04-25 13:50         ` Vladislav Shpilevoy
2019-04-25 13:57           ` Konstantin Osipov
2019-04-24 14:36 ` [tarantool-patches] [PATCH 6/6] swim: introduce suspicion Vladislav Shpilevoy
2019-04-24 17:01   ` [tarantool-patches] " Konstantin Osipov
2019-04-24 20:28     ` Vladislav Shpilevoy
2019-04-25 10:42       ` Konstantin Osipov

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=73ba32c413ce9371b845ac161a0d5ea698fac575.1556116199.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 4/6] swim: wrap 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