From: Vsevolod Stakhov Date: Tue, 21 Feb 2017 13:37:14 +0000 (+0000) Subject: [Minor] Add util.get_hostname routine X-Git-Tag: 1.5.0~65 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ff25c4560ed78e6f6bc84ae34532ae9bad80c558;p=rspamd.git [Minor] Add util.get_hostname routine --- diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 80baf8b34..c0f1421e1 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -462,6 +462,13 @@ LUA_FUNCTION_DEF (util, caseless_hash); */ LUA_FUNCTION_DEF (util, caseless_hash_fast); +/*** + * @function util.get_hostname() + * Returns hostname for this machine + * @return {string} hostname + */ +LUA_FUNCTION_DEF (util, get_hostname); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -503,6 +510,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, caseless_hash), LUA_INTERFACE_DEF (util, caseless_hash_fast), LUA_INTERFACE_DEF (util, count_non_ascii), + LUA_INTERFACE_DEF (util, get_hostname), LUA_INTERFACE_DEF (util, pack), LUA_INTERFACE_DEF (util, unpack), LUA_INTERFACE_DEF (util, packsize), @@ -1929,6 +1937,30 @@ lua_util_count_non_ascii (lua_State *L) return 2; } +static gint +lua_util_get_hostname (lua_State *L) +{ + gchar *hostbuf; + gsize hostlen; + + hostlen = sysconf (_SC_HOST_NAME_MAX); + + if (hostlen <= 0) { + hostlen = 256; + } + else { + hostlen ++; + } + + hostbuf = g_alloca (hostlen); + memset (hostbuf, 0, hostlen); + gethostname (hostbuf, hostlen - 1); + + lua_pushstring (L, hostbuf); + + return 1; +} + /* Backport from Lua 5.3 */ /******************************************************************************