summaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-10-13 14:17:30 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-10-13 14:17:30 +0100
commit91d010ad84b4331eceb24bbfba62cafe44277509 (patch)
tree8a46040d87639e3fa255f7eacd7152976c7c61f2 /src/lua
parentea6b7cea9d8909a7f161ae477b254dbe1a89b6d4 (diff)
downloadrspamd-91d010ad84b4331eceb24bbfba62cafe44277509.tar.gz
rspamd-91d010ad84b4331eceb24bbfba62cafe44277509.zip
[Feature] Lua_dns_resolver: Add idna_convert_utf8 method
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_dns_resolver.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lua/lua_dns_resolver.c b/src/lua/lua_dns_resolver.c
index 75f7a3b6b..7eaa54da6 100644
--- a/src/lua/lua_dns_resolver.c
+++ b/src/lua/lua_dns_resolver.c
@@ -51,6 +51,7 @@ LUA_FUNCTION_DEF (dns_resolver, resolve_txt);
LUA_FUNCTION_DEF (dns_resolver, resolve_mx);
LUA_FUNCTION_DEF (dns_resolver, resolve_ns);
LUA_FUNCTION_DEF (dns_resolver, resolve);
+LUA_FUNCTION_DEF (dns_resolver, idna_convert_utf8);
void lua_push_dns_reply (lua_State *L, const struct rdns_reply *reply);
@@ -66,6 +67,7 @@ static const struct luaL_reg dns_resolverlib_m[] = {
LUA_INTERFACE_DEF (dns_resolver, resolve_mx),
LUA_INTERFACE_DEF (dns_resolver, resolve_ns),
LUA_INTERFACE_DEF (dns_resolver, resolve),
+ LUA_INTERFACE_DEF (dns_resolver, idna_convert_utf8),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}
};
@@ -688,6 +690,50 @@ lua_dns_resolver_resolve (lua_State *L)
return 1;
}
+/***
+ * @method resolver:idna_convert_utf8(hostname[, pool])
+ * Converts domain name from IDN (in utf8 format) to punycode
+ * @return {string} new name converted
+ */
+static int
+lua_dns_resolver_idna_convert_utf8 (lua_State *L)
+{
+ struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1);
+ gsize hlen;
+ guint conv_len = 0;
+ const gchar *hname = luaL_checklstring (L, 2, &hlen);
+ gchar *converted;
+ rspamd_mempool_t *pool = rspamd_lua_check_udata_maybe (L, 3, "rspamd{mempool}");
+
+
+ if (dns_resolver && hname) {
+ if (!rspamd_str_has_8bit (hname, hlen)) {
+ /* No 8 bit, no reasons to call idna */
+ lua_pushlstring (L, hname, hlen);
+ }
+ else {
+ converted = rspamd_dns_resolver_idna_convert_utf8 (dns_resolver, pool,
+ hname, hlen, &conv_len);
+
+ if (converted == NULL) {
+ lua_pushnil (L);
+ }
+ else {
+ lua_pushlstring (L, converted, conv_len);
+
+ if (pool == NULL) {
+ g_free (converted);
+ }
+ }
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
static gint
lua_load_dns_resolver (lua_State *L)
{