From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 67BE86EC56; Sat, 20 Mar 2021 03:46:27 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 67BE86EC56 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616201187; bh=VwmBaFbC76HYi+Kqzsx9NXJH7T9w3kTYAc7pomPR5Kc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=lE5FKwSbSW+SM/B+X/4F1+++SJ+0/QCsQDkYuHhfABxJPsUghXKnVsz0HUhRCwD3l DDsE0ndwbza7nEqY+WK0nq6qXCDbp3hOsdIT931UwQMaen8nA2q/297F9LnZXKIEK+ 8yshqb780iEJlemO4i5aFUhFT7XgOrPQhfu6OYEs= Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 0E1D46EC57 for ; Sat, 20 Mar 2021 03:42:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0E1D46EC57 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1lNPhg-00076U-9Z; Sat, 20 Mar 2021 03:42:56 +0300 To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com, sergepetrenko@tarantool.org Date: Sat, 20 Mar 2021 01:42:37 +0100 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B1E2E766A3410B623182A05F5380850404789171FF20B3384723DA6D931905325ECC9F46731594F90F78714F85661A653 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A4C4638C9DDF45FCEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B23888C9749F3CAC8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C81A23F326053F827EA553D7B9F97C09A60F58442C8D08C2CA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7328B01A8D746D8839FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E478A468B35FE767117882F4460429728AD0CFFFB425014E868A13BD56FB6657A7F4EDE966BC389F9E8FC8737B5C2249A1DCCEB63E2F10FB089D37D7C0E48F6CCF19DD082D7633A0E7DDDDC251EA7DABAAAE862A0553A39223F8577A6DFFEA7C46CC729E650C927943847C11F186F3C5E7DDDDC251EA7DABCC89B49CDF41148F90DBEB212AEC08F22623479134186CDE6BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2BBE337FB72E923155C0AF1600DCBC20BD3311767B7455A337BEDCB59EFC6E342 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C81A23F326053F827EA553D7B9F97C09A60F58442C8D08C2C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFF532FBFD8162E58C699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3450C5E6D685282BA13379A2B438A3276661C2923B4FEA31A016261821D19B1428936EF6000925E9F81D7E09C32AA3244C7F840A42B821E790B95E2DED249FDFFE63871F383B54D9B3FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj8xHC0Ak4ylZnNJ/r4aCKew== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822F066AA2A7BEB6F26F806B65DE34071EC3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH 15/16] sio: introduce and use sio_snprintf() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" sio_strfaddr() can't be used in the places where static buffer is not acceptable - in any code which wants to push the value to Lua, or the address string must be long living. The patch introduces sio_snprintf(), which does the same, but saves the result into a provided buffer with a limited size. In the Lua C code the patch saves the address string on the stack which makes it safe against Lua GC interruptions. Part of #5632 --- src/box/iproto.cc | 8 +++++--- src/box/iproto.h | 2 +- src/box/lua/info.c | 5 +++-- src/box/lua/session.c | 7 +++++-- src/lib/core/sio.c | 40 ++++++++++++++++++++++++++++------------ src/lib/core/sio.h | 4 ++++ 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/box/iproto.cc b/src/box/iproto.cc index f7330af21..238842e17 100644 --- a/src/box/iproto.cc +++ b/src/box/iproto.cc @@ -138,12 +138,14 @@ static struct sockaddr_storage iproto_bound_address_storage; static socklen_t iproto_bound_address_len; const char * -iproto_bound_address(void) +iproto_bound_address(char *buf) { if (iproto_bound_address_len == 0) return NULL; - return sio_strfaddr((struct sockaddr *) &iproto_bound_address_storage, - iproto_bound_address_len); + sio_addr_snprintf(buf, SERVICE_NAME_MAXLEN, + (struct sockaddr *) &iproto_bound_address_storage, + iproto_bound_address_len); + return buf; } /** diff --git a/src/box/iproto.h b/src/box/iproto.h index 392e4f08e..f6f7101a1 100644 --- a/src/box/iproto.h +++ b/src/box/iproto.h @@ -85,7 +85,7 @@ iproto_reset_stat(void); * iproto. To be shown in box.info. */ const char * -iproto_bound_address(void); +iproto_bound_address(char *buf); #if defined(__cplusplus) } /* extern "C" */ diff --git a/src/box/lua/info.c b/src/box/lua/info.c index c4c9fa0a0..1e0c0148a 100644 --- a/src/box/lua/info.c +++ b/src/box/lua/info.c @@ -52,7 +52,7 @@ #include "box/raft.h" #include "lua/utils.h" #include "fiber.h" -#include "tt_static.h" +#include "sio.h" static void lbox_pushvclock(struct lua_State *L, const struct vclock *vclock) @@ -574,7 +574,8 @@ static int lbox_info_listen(struct lua_State *L) { /* NULL is ok, no need to check. */ - lua_pushstring(L, iproto_bound_address()); + char addrbuf[SERVICE_NAME_MAXLEN]; + lua_pushstring(L, iproto_bound_address(addrbuf)); return 1; } diff --git a/src/box/lua/session.c b/src/box/lua/session.c index 0a20aaad1..ae8c7094b 100644 --- a/src/box/lua/session.c +++ b/src/box/lua/session.c @@ -273,10 +273,13 @@ lbox_session_peer(struct lua_State *L) struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - if (sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0) + struct sockaddr *addr_base = (struct sockaddr *)&addr; + if (sio_getpeername(fd, addr_base, &addrlen) < 0) luaL_error(L, "session.peer(): getpeername() failed"); - lua_pushstring(L, sio_strfaddr((struct sockaddr *)&addr, addrlen)); + char addrbuf[SERVICE_NAME_MAXLEN]; + sio_addr_snprintf(addrbuf, sizeof(addrbuf), addr_base, addrlen); + lua_pushstring(L, addrbuf); return 1; } diff --git a/src/lib/core/sio.c b/src/lib/core/sio.c index 25c34ea59..6d1732332 100644 --- a/src/lib/core/sio.c +++ b/src/lib/core/sio.c @@ -59,16 +59,17 @@ sio_socketname_to_buffer(int fd, char *buf, int size) return 0; struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - int rc = getsockname(fd, (struct sockaddr *) &addr, &addrlen); + struct sockaddr *base_addr = (struct sockaddr *)&addr; + int rc = getsockname(fd, base_addr, &addrlen); if (rc == 0) { - SNPRINT(n, snprintf, buf, size, ", aka %s", - sio_strfaddr((struct sockaddr *)&addr, addrlen)); + SNPRINT(n, snprintf, buf, size, ", aka "); + SNPRINT(n, sio_addr_snprintf, buf, size, base_addr, addrlen); } addrlen = sizeof(addr); rc = getpeername(fd, (struct sockaddr *) &addr, &addrlen); if (rc == 0) { - SNPRINT(n, snprintf, buf, size, ", peer of %s", - sio_strfaddr((struct sockaddr *)&addr, addrlen)); + SNPRINT(n, snprintf, buf, size, ", peer of "); + SNPRINT(n, sio_addr_snprintf, buf, size, base_addr, addrlen); } return 0; } @@ -326,26 +327,41 @@ sio_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen) return 0; } -const char * -sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen) +int +sio_addr_snprintf(char *buf, size_t size, const struct sockaddr *addr, + socklen_t addrlen) { + int res; 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); + res = snprintf(buf, size, "unix/:%s", u->sun_path); else - return tt_sprintf("unix/:(socket)"); + res = snprintf(buf, size, "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)"); + res = snprintf(buf, size, "(host):(port)"); else if (addr->sa_family == AF_INET) - return tt_sprintf("%s:%s", host, serv); + res = snprintf(buf, size, "%s:%s", host, serv); else - return tt_sprintf("[%s]:%s", host, serv); + res = snprintf(buf, size, "[%s]:%s", host, serv); } + assert(res + 1 < SERVICE_NAME_MAXLEN); + assert(res >= 0); + return res; +} + +const char * +sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen) +{ + int size = SERVICE_NAME_MAXLEN; + char *buf = (char *) static_reserve(size); + /* +1 for terminating 0. */ + static_alloc(sio_addr_snprintf(buf, size, addr, addrlen) + 1); + return buf; } int diff --git a/src/lib/core/sio.h b/src/lib/core/sio.h index 4f5a7f6f3..016f1f079 100644 --- a/src/lib/core/sio.h +++ b/src/lib/core/sio.h @@ -73,6 +73,10 @@ sio_wouldblock(int err) return err == EAGAIN || err == EWOULDBLOCK || err == EINTR; } +/** Format the address into the given buffer. Behaves like snprintf(). */ +int +sio_addr_snprintf(char *buf, size_t size, const struct sockaddr *addr, + socklen_t addrlen); /** * Format the address provided in struct sockaddr *addr. -- 2.24.3 (Apple Git-128)