From 71317d85025dcbb348d262a42ae60f4b086d0943 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 May 2021 11:16:24 +0800 Subject: [PATCH] Add socket.resolve, see #1398 --- lualib-src/lua-socket.c | 31 +++++++++++++++++++++++++++++++ lualib/skynet/socket.lua | 1 + 2 files changed, 32 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 0f735300..1754eb44 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -11,8 +11,10 @@ #include #include +#include #include #include +#include #include "skynet.h" #include "skynet_socket.h" @@ -786,6 +788,34 @@ linfo(lua_State *L) { return 1; } +static int +lresolve(lua_State *L) { + const char * host = luaL_checkstring(L, 1); + int status; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + struct addrinfo *ai_ptr = NULL; + memset( &ai_hints, 0, sizeof( ai_hints ) ); + status = getaddrinfo( host, NULL, &ai_hints, &ai_list); + if ( status != 0 ) { + return luaL_error(L, gai_strerror(status)); + } + lua_newtable(L); + int idx = 1; + char tmp[128]; + for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { + struct sockaddr * addr = ai_ptr->ai_addr; + void * sin_addr = (ai_ptr->ai_family == AF_INET) ? (void*)&((struct sockaddr_in *)addr)->sin_addr : (void*)&((struct sockaddr_in6 *)addr)->sin6_addr; + if (inet_ntop(ai_ptr->ai_family, sin_addr, tmp, sizeof(tmp))) { + lua_pushstring(L, tmp); + lua_rawseti(L, -2, idx++); + } + } + + freeaddrinfo(ai_list); + return 1; +} + LUAMOD_API int luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); @@ -820,6 +850,7 @@ luaopen_skynet_socketdriver(lua_State *L) { { "udp_connect", ludp_connect }, { "udp_send", ludp_send }, { "udp_address", ludp_address }, + { "resolve", lresolve }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index ae9b3724..2ba2a32e 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -457,6 +457,7 @@ end socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) socket.netstat = assert(driver.info) +socket.resolve = assert(driver.resolve) function socket.warning(id, callback) local obj = socket_pool[id]