From d7b6a62bcc8e3ffd680a3d2ec51daa2b2ed3ccb2 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 30 Apr 2022 15:26:58 +0800 Subject: check known hosts --- core/src/xmake/engine.c | 2 ++ core/src/xmake/io/socket_peeraddr.c | 67 +++++++++++++++++++++++++++++++++++++ core/src/xmake/makefile | 1 + 3 files changed, 70 insertions(+) create mode 100644 core/src/xmake/io/socket_peeraddr.c (limited to 'core/src') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 78a2f44fa..1a74387d2 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -142,6 +142,7 @@ tb_int_t xm_io_filelock_close(lua_State* lua); // the io/socket functions tb_int_t xm_io_socket_open(lua_State* lua); tb_int_t xm_io_socket_rawfd(lua_State* lua); +tb_int_t xm_io_socket_peeraddr(lua_State* lua); tb_int_t xm_io_socket_wait(lua_State* lua); tb_int_t xm_io_socket_bind(lua_State* lua); tb_int_t xm_io_socket_ctrl(lua_State* lua); @@ -345,6 +346,7 @@ static luaL_Reg const g_io_functions[] = , { "filelock_close", xm_io_filelock_close } , { "socket_open", xm_io_socket_open } , { "socket_rawfd", xm_io_socket_rawfd } +, { "socket_peeraddr", xm_io_socket_peeraddr } , { "socket_wait", xm_io_socket_wait } , { "socket_bind", xm_io_socket_bind } , { "socket_ctrl", xm_io_socket_ctrl } diff --git a/core/src/xmake/io/socket_peeraddr.c b/core/src/xmake/io/socket_peeraddr.c new file mode 100644 index 000000000..bafeb0a7c --- /dev/null +++ b/core/src/xmake/io/socket_peeraddr.c @@ -0,0 +1,67 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this sock except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @sock socket_peeraddr.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_peeraddr" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// socket to fd +#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* io.socket_peeraddr(sock) + */ +tb_int_t xm_io_socket_peeraddr(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + xm_io_return_error(lua, "get peer address for invalid sock!"); + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(sock, 0); + + // get peer address + tb_ipaddr_t addr; + tb_char_t data[256]; + tb_char_t const* cstr = tb_null; + if (tb_socket_peer(sock, &addr) && (cstr = tb_ipaddr_cstr(&addr, data, sizeof(data)))) + lua_pushstring(lua, cstr); + else lua_pushnil(lua); + return 1; +} diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 34f272fce..d9e8025fb 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -85,6 +85,7 @@ xmake_C_FILES += \ io/socket_recv \ io/socket_recvfrom \ io/socket_close \ + io/socket_peeraddr \ io/pipe_open \ io/pipe_openpair \ io/pipe_close \ -- cgit v1.3.1