From 12a60fe837acdfd67bf352b29ccedfbab141258d Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Fri, 25 Jan 2013 16:32:39 +0400
Subject: [PATCH] Mak accept() and listen() follow luasocket return
 conventions. Do not throw exceptions from coeio_connect_addrinfo() if
 timeout. Rename box.io to box.socket.

---
 include/coio.h                                |   4 +-
 src/CMakeLists.txt                            |   2 +-
 src/coio.m                                    |  55 ++++---
 src/lua/init.m                                |   4 +-
 src/lua/{lua_io.h => lua_socket.h}            |   8 +-
 src/lua/{lua_io.m => lua_socket.m}            | 146 +++++++++---------
 test/box/lua.result                           |  17 +-
 .../box/{lua_sockets.result => socket.result} |  10 +-
 test/box/{lua_sockets.test => socket.test}    |  20 +--
 9 files changed, 129 insertions(+), 137 deletions(-)
 rename src/lua/{lua_io.h => lua_socket.h} (88%)
 rename src/lua/{lua_io.m => lua_socket.m} (88%)
 rename test/box/{lua_sockets.result => socket.result} (98%)
 rename test/box/{lua_sockets.test => socket.test} (97%)

diff --git a/include/coio.h b/include/coio.h
index 2948184f9a..9427ac7cc7 100644
--- a/include/coio.h
+++ b/include/coio.h
@@ -49,11 +49,11 @@ coio_socket(struct ev_io *coio, int domain, int type, int protocol);
 void
 coio_connect(struct ev_io *coio, struct sockaddr_in *addr);
 
-void
+bool
 coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr,
 		     socklen_t len, ev_tstamp timeout);
 
-void
+bool
 coio_connect_addrinfo(struct ev_io *coio, struct addrinfo *ai,
 		      ev_tstamp timeout);
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7979c6c2fb..6dbba7e495 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -96,7 +96,7 @@ set (common_sources
      lua/slab.m
      lua/uuid.m
      lua/lua_ipc.m
-     lua/lua_io.m
+     lua/lua_socket.m
      lua/session.m
 )
 
diff --git a/src/coio.m b/src/coio.m
index 9395880232..38523e0197 100644
--- a/src/coio.m
+++ b/src/coio.m
@@ -72,16 +72,20 @@ coio_connect(struct ev_io *coio, struct sockaddr_in *addr)
 
 /**
  * Connect to a host with a specified timeout.
+ * @retval true timeout
+ * @retval false connected
  */
-void
+bool
 coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr,
 		     socklen_t len, ev_tstamp timeout)
 {
 	if (sio_connect(coio->fd, addr, len) == 0)
-		return;
+		return false;
 	assert(errno == EINPROGRESS);
-	/* Wait until socket is ready for writing or
-	 * timed out. */
+	/*
+	 * Wait until socket is ready for writing or
+	 * timed out.
+	 */
 	ev_io_set(coio, coio->fd, EV_WRITE);
 	ev_io_start(coio);
 	bool is_timedout = fiber_yield_timeout(timeout);
@@ -89,16 +93,17 @@ coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr,
 	fiber_testcancel();
 	if (is_timedout) {
 		errno = ETIMEDOUT;
-		tnt_raise(SocketError, :coio->fd in:"connect");
+		return true;
 	}
 	int error = EINPROGRESS;
 	socklen_t sz = sizeof(error);
 	sio_getsockopt(coio->fd, SOL_SOCKET, SO_ERROR,
 		       &error, &sz);
-	if (error == 0)
-		return;
-	errno = error;
-	tnt_raise(SocketError, :coio->fd in:"connect");
+	if (error != 0) {
+		errno = error;
+		tnt_raise(SocketError, :coio->fd in:"connect");
+	}
+	return false;
 }
 
 /**
@@ -107,39 +112,33 @@ coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr,
  *
  * Coio should not be initialized.
  */
-void
+bool
 coio_connect_addrinfo(struct ev_io *coio, struct addrinfo *ai,
 		      ev_tstamp timeout)
 {
-	struct addrinfo *a;
-	int error = 0;
-
 	ev_tstamp start, delay;
 	evio_timeout_init(&start, &delay, timeout);
 
-	for (a = ai; a; a = a->ai_next) {
+	while (ai) {
+		struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
 		@try {
-			coio_socket(coio, a->ai_family, a->ai_socktype, a->ai_protocol);
+			if (! evio_is_active(coio)) {
+				coio_socket(coio, ai->ai_family,
+					    ai->ai_socktype,
+					    ai->ai_protocol);
 
-			evio_setsockopt_tcp(coio->fd);
-
-			coio_connect_timeout(coio, (struct sockaddr_in*)a->ai_addr,
-					     a->ai_addrlen, delay);
-			return;
-		} @catch (FiberCancelException *e) {
-			evio_close(coio);
-			@throw;
+				evio_setsockopt_tcp(coio->fd);
+			}
+			return coio_connect_timeout(coio, addr,
+						    ai->ai_addrlen, delay);
 		} @catch (tnt_Exception *e) {
-			error = errno;
 			ev_now_update();
 			evio_timeout_update(start, &delay);
 			evio_close(coio);
-			continue;
+			ai = ai->ai_next;
 		}
-		return;
 	}
-	errno = error;
-	tnt_raise(SocketError, :coio->fd in:"connect_addrinfo");
+	tnt_raise(SocketError, :coio->fd in:"connect_addrinfo(): exhausted addrinfo list");
 }
 
 /**
diff --git a/src/lua/init.m b/src/lua/init.m
index 4f94747d2d..8eca308a1d 100644
--- a/src/lua/init.m
+++ b/src/lua/init.m
@@ -45,7 +45,7 @@
 #include "pickle.h"
 #include "fiber.h"
 #include "lua_ipc.h"
-#include "lua_io.h"
+#include "lua_socket.h"
 #include "lua/info.h"
 #include "lua/slab.h"
 #include "lua/stat.h"
@@ -1368,7 +1368,7 @@ tarantool_lua_init()
 	tarantool_lua_stat_init(L);
 	tarantool_lua_ipc_init(L);
 	tarantool_lua_uuid_init(L);
-	tarantool_lua_io_init(L);
+	tarantool_lua_socket_init(L);
 	tarantool_lua_session_init(L);
 
 	mod_lua_init(L);
diff --git a/src/lua/lua_io.h b/src/lua/lua_socket.h
similarity index 88%
rename from src/lua/lua_io.h
rename to src/lua/lua_socket.h
index 3879a27797..663d5e3b50 100644
--- a/src/lua/lua_io.h
+++ b/src/lua/lua_socket.h
@@ -1,5 +1,5 @@
-#ifndef TARANTOOL_LUA_IO_H_INCLUDED
-#define TARANTOOL_LUA_IO_H_INCLUDED
+#ifndef TARANTOOL_LUA_SOCKET_H_INCLUDED
+#define TARANTOOL_LUA_SOCKET_H_INCLUDED
 /*
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
@@ -30,6 +30,6 @@
  */
 
 struct lua_State;
-void tarantool_lua_io_init(struct lua_State *L);
+void tarantool_lua_socket_init(struct lua_State *L);
 
-#endif /* TARANTOOL_LUA_IO_H_INCLUDED */
+#endif /* TARANTOOL_LUA_SOCKET_H_INCLUDED */
diff --git a/src/lua/lua_io.m b/src/lua/lua_socket.m
similarity index 88%
rename from src/lua/lua_io.m
rename to src/lua/lua_socket.m
index efa8df4244..c1670dd14a 100644
--- a/src/lua/lua_io.m
+++ b/src/lua/lua_socket.m
@@ -26,7 +26,7 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include "lua_io.h"
+#include "lua_socket.h"
 #include "lua.h"
 #include "lauxlib.h"
 #include "lualib.h"
@@ -48,7 +48,7 @@
 #include <lua/init.h>
 #include <stdlib.h>
 
-static const char iolib_name[] = "box.io";
+static const char socketlib_name[] = "box.socket";
 
 enum bio_error {
 	ERESOLVE = -1
@@ -74,7 +74,7 @@ static int
 bio_pushsocket(struct lua_State *L, int socktype)
 {
 	struct bio_socket *s = lua_newuserdata(L, sizeof(struct bio_socket));
-	luaL_getmetatable(L, iolib_name);
+	luaL_getmetatable(L, socketlib_name);
 	lua_setmetatable(L, -2);
 	evio_clear(&s->coio);
 	s->socktype = socktype;
@@ -90,7 +90,7 @@ bio_checksocket(struct lua_State *L, int narg)
 	/* avoiding unnecessary luajit assert */
 	if (lua_gettop(L) < narg)
 		luaL_error(L, "incorrect method call");
-	return luaL_checkudata(L, narg, iolib_name);
+	return luaL_checkudata(L, narg, socketlib_name);
 }
 
 static inline struct bio_socket *
@@ -192,7 +192,7 @@ bio_resolve(int socktype, const char *host, const char *port,
 }
 
 static int
-lbox_io_tostring(struct lua_State *L)
+lbox_socket_tostring(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, -1);
 	lua_pushfstring(L, "%d", s->coio.fd);
@@ -205,7 +205,7 @@ lbox_io_tostring(struct lua_State *L)
  * Create SOCK_STREAM socket object.
  */
 static int
-lbox_io_tcp(struct lua_State *L)
+lbox_socket_tcp(struct lua_State *L)
 {
 	return bio_pushsocket(L, SOCK_STREAM);
 }
@@ -216,7 +216,7 @@ lbox_io_tcp(struct lua_State *L)
  * Create SOCK_DGRAM socket object.
  */
 static int
-lbox_io_udp(struct lua_State *L)
+lbox_socket_udp(struct lua_State *L)
 {
 	return bio_pushsocket(L, SOCK_DGRAM);
 }
@@ -227,7 +227,7 @@ lbox_io_udp(struct lua_State *L)
  * Close socket and reinitialize readahead buffer.
  */
 static int
-lbox_io_close(struct lua_State *L)
+lbox_socket_close(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, -1);
 	bio_close(s);
@@ -245,7 +245,7 @@ lbox_io_close(struct lua_State *L)
  * 2. error: nil, status = "error", eno, estr
  */
 static int
-lbox_io_shutdown(struct lua_State *L)
+lbox_socket_shutdown(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, -1);
 	int how = luaL_checkint(L, 2);
@@ -263,7 +263,7 @@ lbox_io_shutdown(struct lua_State *L)
  * Return error code and error description.
  */
 static int
-lbox_io_error(struct lua_State *L)
+lbox_socket_error(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, -1);
 	return bio_pusherrorcode(L, s);
@@ -281,7 +281,7 @@ lbox_io_error(struct lua_State *L)
  * 3. timeout: nil, status = "timeout", eno, estr
  */
 static int
-lbox_io_connect(struct lua_State *L)
+lbox_socket_connect(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, 1);
 	const char *host = luaL_checkstring(L, 2);
@@ -302,14 +302,14 @@ lbox_io_connect(struct lua_State *L)
 	evio_timeout_update(start, &delay);
 	@try {
 		/* connect to a first available host */
-		coio_connect_addrinfo(&s->coio, ai, delay);
+		if (coio_connect_addrinfo(&s->coio, ai, delay))
+			return bio_pusherror(L, s, ETIMEDOUT);
 	} @catch (SocketError *e) {
-		/* case #2-3: error or timeout */
 		return bio_pusherror(L, s, errno);
 	} @finally {
 		freeaddrinfo(ai);
 	}
-	/* case #1: Success */
+	/* Success */
 	lua_settop(L, 1);
 	return 1;
 }
@@ -331,7 +331,7 @@ lbox_io_connect(struct lua_State *L)
  *
  */
 static int
-lbox_io_write(struct lua_State *L)
+lbox_socket_write(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	size_t buf_size = 0;
@@ -356,7 +356,7 @@ lbox_io_write(struct lua_State *L)
 }
 
 static inline int
-lbox_io_read_ret(struct lua_State *L, struct bio_socket *s, char *buf,
+lbox_socket_read_ret(struct lua_State *L, struct bio_socket *s, char *buf,
                  size_t size, enum bio_status st)
 {
 	lua_pushlstring(L, buf, size);
@@ -365,7 +365,7 @@ lbox_io_read_ret(struct lua_State *L, struct bio_socket *s, char *buf,
 }
 
 static inline int
-lbox_io_read_eof(struct lua_State *L, struct bio_socket *s,
+lbox_socket_read_eof(struct lua_State *L, struct bio_socket *s,
 		 size_t sz)
 {
 	size_t ret = ibuf_size(&s->iob->in);
@@ -375,7 +375,7 @@ lbox_io_read_eof(struct lua_State *L, struct bio_socket *s,
 	 * read limit: return it with eof status
 	 * set.
 	 */
-	lbox_io_read_ret(L, s, s->iob->in.pos, ret, BIO_EOF);
+	lbox_socket_read_ret(L, s, s->iob->in.pos, ret, BIO_EOF);
 	s->iob->in.pos += ret;
 	return 4;
 }
@@ -397,7 +397,7 @@ lbox_io_read_eof(struct lua_State *L, struct bio_socket *s,
  *
  */
 static int
-lbox_io_read(struct lua_State *L)
+lbox_socket_read(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	int sz = luaL_checkint(L, 2);
@@ -414,7 +414,7 @@ lbox_io_read(struct lua_State *L)
 	 *
 	 * See case #4. */
 	if (s->eof) {
-		int rc = lbox_io_read_eof(L, s, sz);
+		int rc = lbox_socket_read_eof(L, s, sz);
 		if (rc == -1)
 			goto case_1;
 		return rc;
@@ -453,7 +453,7 @@ lbox_io_read(struct lua_State *L)
 			enum bio_status st =
 				(s->error == ETIMEDOUT) ? BIO_TIMEOUT :
 				 BIO_ERROR;
-			return lbox_io_read_ret(L, s, NULL, 0, st);
+			return lbox_socket_read_ret(L, s, NULL, 0, st);
 		}
 
 		/* in case of EOF from a client, return partly read chunk and
@@ -466,7 +466,7 @@ lbox_io_read(struct lua_State *L)
 			/* read chunk could be much bigger than limit, in
 			 * this case, don't return eof flag to a user until
 			 * whole data been read. */
-			int rc = lbox_io_read_eof(L, s, sz);
+			int rc = lbox_socket_read_eof(L, s, sz);
 			if (rc == -1)
 				goto case_1;
 			return rc;
@@ -540,7 +540,7 @@ first_matched:		if (unlikely(rs[i].sep_size == rs[i].pos + 1))
 }
 
 static void
-lbox_io_readline_cr(struct lua_State *L)
+lbox_socket_readline_cr(struct lua_State *L)
 {
 	/* emulate user passed {'\n'} as the separate table */
 	lua_newtable(L);
@@ -550,7 +550,7 @@ lbox_io_readline_cr(struct lua_State *L)
 }
 
 static int
-lbox_io_readline_ret(struct lua_State *L, struct bio_socket *s,
+lbox_socket_readline_ret(struct lua_State *L, struct bio_socket *s,
                      char *buf, size_t size,
                      enum bio_status st, int sid)
 {
@@ -561,21 +561,21 @@ lbox_io_readline_ret(struct lua_State *L, struct bio_socket *s,
 }
 
 static int
-lbox_io_readline_opts(struct lua_State *L, unsigned int *limit,
+lbox_socket_readline_opts(struct lua_State *L, unsigned int *limit,
 		      double *timeout)
 {
 	int seplist = 2;
 	switch (lua_gettop(L)) {
 	case 1:
 		/* readline() */
-		lbox_io_readline_cr(L);
+		lbox_socket_readline_cr(L);
 		break;
 	case 2:
 		 /* readline(limit)
 		    readline({seplist}) */
 		if (lua_isnumber(L, 2)) {
 			*limit = luaL_checkint(L, 2);
-			lbox_io_readline_cr(L);
+			lbox_socket_readline_cr(L);
 			seplist = 3;
 		} else if (! lua_istable(L, 2))
 			luaL_error(L, "box.io.readline: bad argument");
@@ -588,7 +588,7 @@ lbox_io_readline_opts(struct lua_State *L, unsigned int *limit,
 			*limit = luaL_checkint(L, 2);
 			if (lua_isnumber(L, 3)) {
 				*timeout = luaL_checknumber(L, 3);
-				lbox_io_readline_cr(L);
+				lbox_socket_readline_cr(L);
 				seplist = 4;
 				break;
 			} else if (! lua_istable(L, 3))
@@ -641,14 +641,14 @@ lbox_io_readline_opts(struct lua_State *L, unsigned int *limit,
  *
  */
 static int
-lbox_io_readline(struct lua_State *L)
+lbox_socket_readline(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	bio_clearerr(s);
 
 	unsigned int limit = UINT_MAX;
 	double timeout = TIMEOUT_INFINITY;
-	int seplist = lbox_io_readline_opts(L, &limit, &timeout);
+	int seplist = lbox_socket_readline_opts(L, &limit, &timeout);
 
 	int rs_size = lua_objlen(L, seplist);
 	if (rs_size == 0)
@@ -675,7 +675,7 @@ lbox_io_readline(struct lua_State *L)
 
 			/* case #4: user limit reached */
 			if (bottom == limit) {
-				lbox_io_readline_ret(L, s, s->iob->in.pos, bottom,
+				lbox_socket_readline_ret(L, s, s->iob->in.pos, bottom,
 				                     BIO_LIMIT, 0);
 				s->iob->in.pos += bottom;
 				return 5;
@@ -693,7 +693,7 @@ lbox_io_readline(struct lua_State *L)
 				 * is fully traversed.
 				 */
 eof:				if (s->eof) {
-					lbox_io_readline_ret(L, s, s->iob->in.pos, bottom,
+					lbox_socket_readline_ret(L, s, s->iob->in.pos, bottom,
 							     BIO_EOF, 0);
 					s->iob->in.pos += bottom;
 					return 5;
@@ -727,7 +727,7 @@ eof:				if (s->eof) {
 		enum bio_status st =
 			(s->error == ETIMEDOUT) ? BIO_TIMEOUT :
 			 BIO_ERROR;
-		return lbox_io_readline_ret(L, s, NULL, 0, st, 0);
+		return lbox_socket_readline_ret(L, s, NULL, 0, st, 0);
 	}
 
 	/* case #1: success, separator matched */
@@ -750,7 +750,7 @@ eof:				if (s->eof) {
  *
  */
 static int
-lbox_io_bind(struct lua_State *L)
+lbox_socket_bind(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, 1);
 	const char *host = luaL_checkstring(L, 2);
@@ -776,19 +776,17 @@ lbox_io_bind(struct lua_State *L)
 	return 1;
 }
 
-/*
+/**
  * socket:listen()
  *
  * Marks the socket that it will be used to accept incoming
  * connection requests using socket:accept().
  *
- * Return:
- *
- * 1. ok:      socket (self)
- * 2. error:   nil, status = "error",   eno, estr
+ * @retval socket (self) on success
+ * @retval nil, status = "error", errno, error string
  */
 static int
-lbox_io_listen(struct lua_State *L)
+lbox_socket_listen(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	bio_clearerr(s);
@@ -805,11 +803,11 @@ lbox_io_listen(struct lua_State *L)
  * socket.
  *
  * 1. ok:      socket (client), address, port
- * 2. error:   nil, nil, nil, status = "error",   eno, estr
- * 3. timeout: nil, nil, nil, status = "timeout", eno, estr
+ * 2. error:   nil, status = "error", eno, estr
+ * 3. timeout: nil, status = "timeout", eno, estr
  */
 static int
-lbox_io_accept(struct lua_State *L)
+lbox_socket_accept(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	double timeout = TIMEOUT_INFINITY;
@@ -826,9 +824,7 @@ lbox_io_accept(struct lua_State *L)
 		coio_init(&client->coio, fd);
 	} @catch (SocketError *e) {
 		/* case #2: error */
-		lua_pushnil(L);
-		lua_pushnil(L);
-		return 2 + bio_pusherror(L, s, errno);
+		return bio_pusherror(L, s, errno);
 	}
 	/* get user host and port */
 	char hbuf[NI_MAXHOST];
@@ -838,9 +834,7 @@ lbox_io_accept(struct lua_State *L)
 			     sbuf, sizeof(sbuf), NI_NUMERICHOST|NI_NUMERICSERV);
 	if (rc != 0) {
 		/* case #2: error */
-		lua_pushnil(L);
-		lua_pushnil(L);
-		return 2 + bio_pusherror(L, s, ERESOLVE);
+		return bio_pusherror(L, s, ERESOLVE);
 	}
 	/* push host and port */
 	lua_pushstring(L, hbuf);
@@ -860,7 +854,7 @@ lbox_io_accept(struct lua_State *L)
  * 3. timeout: nil, status = "timeout", eno, estr
  */
 static int
-lbox_io_sendto(struct lua_State *L)
+lbox_socket_sendto(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checksocket(L, 1);
 	size_t buf_size = 0;
@@ -907,13 +901,11 @@ lbox_io_sendto(struct lua_State *L)
  * Return:
  *
  * 1. ok:      data,      client_addr,       client_port
- * 2. error:   data = "", client_addr = nil, client_port = nil, status = "error",
- *             eno, estr
- * 3. timeout: data = "", client_addr = nil, client_port = nil, status = "timeout",
- *             eno, estr
+ * 2. error:   data = "", status = "error",  eno, estr
+ * 3. timeout: data = "",  status = "timeout", eno, estr
  */
 static int
-lbox_io_recvfrom(struct lua_State *L)
+lbox_socket_recvfrom(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	int buf_size = luaL_checkint(L, 2);
@@ -962,31 +954,31 @@ lbox_io_recvfrom(struct lua_State *L)
 }
 
 void
-tarantool_lua_io_init(struct lua_State *L)
-{
-	static const struct luaL_reg lbox_io_meta[] = {
-		{"__gc", lbox_io_close},
-		{"__tostring", lbox_io_tostring},
-		{"error", lbox_io_error},
-		{"close", lbox_io_close},
-		{"shutdown", lbox_io_shutdown},
-		{"connect", lbox_io_connect},
-		{"write", lbox_io_write},
-		{"read", lbox_io_read},
-		{"readline", lbox_io_readline},
-		{"bind", lbox_io_bind},
-		{"listen", lbox_io_listen},
-		{"accept", lbox_io_accept},
-		{"sendto", lbox_io_sendto},
-		{"recvfrom", lbox_io_recvfrom},
+tarantool_lua_socket_init(struct lua_State *L)
+{
+	static const struct luaL_reg lbox_socket_meta[] = {
+		{"__gc", lbox_socket_close},
+		{"__tostring", lbox_socket_tostring},
+		{"error", lbox_socket_error},
+		{"close", lbox_socket_close},
+		{"shutdown", lbox_socket_shutdown},
+		{"connect", lbox_socket_connect},
+		{"write", lbox_socket_write},
+		{"read", lbox_socket_read},
+		{"readline", lbox_socket_readline},
+		{"bind", lbox_socket_bind},
+		{"listen", lbox_socket_listen},
+		{"accept", lbox_socket_accept},
+		{"sendto", lbox_socket_sendto},
+		{"recvfrom", lbox_socket_recvfrom},
 		{NULL, NULL}
 	};
-	static const struct luaL_reg iolib[] = {
-		{"tcp", lbox_io_tcp},
-		{"udp", lbox_io_udp},
+	static const struct luaL_reg socketlib[] = {
+		{"tcp", lbox_socket_tcp},
+		{"udp", lbox_socket_udp},
 		{NULL, NULL}
 	};
-	tarantool_lua_register_type(L, iolib_name, lbox_io_meta);
-	luaL_register(L, iolib_name, iolib);
+	tarantool_lua_register_type(L, socketlib_name, lbox_socket_meta);
+	luaL_register(L, socketlib_name, socketlib);
 	lua_pop(L, 1);
 }
diff --git a/test/box/lua.result b/test/box/lua.result
index b6064e5f4e..77fab6ea91 100644
--- a/test/box/lua.result
+++ b/test/box/lua.result
@@ -15,33 +15,34 @@ lua for n in pairs(box) do print('  - box.', n) end
   - box.fiber
   - box.space
   - box.cfg
+  - box.on_reload_configuration
   - box.time64
   - box.uuid
-  - box.select_limit
+  - box.ipc
   - box.delete
-  - box.on_reload_configuration
-  - box.replace
   - box.bless_space
+  - box.replace
   - box.counter
+  - box.auto_increment
   - box.time
   - box.select_range
   - box.insert
-  - box.auto_increment
   - box.update
+  - box.select_reverse_range
   - box.info
   - box.session
   - box.uuid_hex
-  - box.select_reverse_range
+  - box.select
   - box.slab
   - box.process
-  - box.select
   - box.dostring
+  - box.select_limit
   - box.stat
   - box.flags
   - box.unpack
-  - box.ipc
-  - box.index
   - box.pack
+  - box.index
+  - box.socket
 ...
 lua box.pack()
 ---
diff --git a/test/box/lua_sockets.result b/test/box/socket.result
similarity index 98%
rename from test/box/lua_sockets.result
rename to test/box/socket.result
index 784ca229b8..ad6fbdf262 100644
--- a/test/box/lua_sockets.result
+++ b/test/box/socket.result
@@ -1,4 +1,4 @@
-lua s = box.io.udp()
+lua s = box.socket.udp()
 ---
 ...
 lua type(s)
@@ -8,7 +8,7 @@ lua type(s)
 lua s:close()
 ---
 ...
-lua s = box.io.tcp()
+lua s = box.socket.tcp()
 ---
 ...
 lua type(s)
@@ -120,7 +120,7 @@ lua s:error()
  - 110
  - Connection timed out
 ...
-lua s = box.io.tcp()
+lua s = box.socket.tcp()
 ---
 ...
 lua type(s:connect('127.0.0.1', '30303'))
@@ -859,7 +859,7 @@ lua s:close()
 ---
 ...
 ping
-lua s = box.io.udp()
+lua s = box.socket.udp()
 ---
 ...
 lua type(s:sendto('ping', 'localhost', '30302'))
@@ -879,7 +879,7 @@ lua s:read(4)
 lua s:close()
 ---
 ...
-lua s = box.io.udp()
+lua s = box.socket.udp()
 ---
 ...
 lua type(s:bind('localhost', '30301'))
diff --git a/test/box/lua_sockets.test b/test/box/socket.test
similarity index 97%
rename from test/box/lua_sockets.test
rename to test/box/socket.test
index 60d4769890..98e220f3d8 100644
--- a/test/box/lua_sockets.test
+++ b/test/box/socket.test
@@ -2,16 +2,16 @@
 
 import socket
 
-####################
-#                  #
-# box.io.tcp/udp() #
-#                  #
-####################
+########################
+#                      #
+# box.socket.tcp/udp() #
+#                      #
+########################
 
-exec admin "lua s = box.io.udp()"
+exec admin "lua s = box.socket.udp()"
 exec admin "lua type(s)"
 exec admin "lua s:close()"
-exec admin "lua s = box.io.tcp()"
+exec admin "lua s = box.socket.tcp()"
 exec admin "lua type(s)"
 
 ### socket:close()
@@ -76,7 +76,7 @@ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind(('localhost', 30303))
 s.listen(1)
 
-exec admin "lua s = box.io.tcp()"
+exec admin "lua s = box.socket.tcp()"
 exec admin "lua type(s:connect('127.0.0.1', '30303'))"
 exec admin "lua s:write('ping')"
 exec admin "lua s:error()"
@@ -471,7 +471,7 @@ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind(('localhost', 30302))
 
 # SOCK_DGRAM
-exec admin "lua s = box.io.udp()"
+exec admin "lua s = box.socket.udp()"
 exec admin "lua type(s:sendto('ping', 'localhost', '30302'))"
 exec admin "lua s:error()"
 
@@ -489,7 +489,7 @@ exec admin "lua s:close()"
 # connect from test to echo server implemented in
 # stored procedure and do send/recv.
 #
-exec admin "lua s = box.io.udp()"
+exec admin "lua s = box.socket.udp()"
 exec admin "lua type(s:bind('localhost', '30301'))"
 exec admin "lua s:error()"
 
-- 
GitLab