From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 66A9F2BAD0 for ; Wed, 24 Apr 2019 10:36:30 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kVFK2HExWPjy for ; Wed, 24 Apr 2019 10:36:30 -0400 (EDT) Received: from smtp45.i.mail.ru (smtp45.i.mail.ru [94.100.177.105]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 1D72B2BB12 for ; Wed, 24 Apr 2019 10:36:30 -0400 (EDT) From: Vladislav Shpilevoy Subject: [tarantool-patches] [PATCH 4/6] swim: wrap sio_strfaddr() Date: Wed, 24 Apr 2019 17:36:18 +0300 Message-Id: <73ba32c413ce9371b845ac161a0d5ea698fac575.1556116199.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: kostja@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 /* 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)