diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-03-29 16:03:31 -0700 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-03-29 16:03:31 -0700 |
commit | eb68f30ca2261fba8388c9774b117c6213eaa4ba (patch) | |
tree | a4adeda490b0c36a327e0664d554fc35a548df01 | |
parent | 4dd1091ef9fae76dd8d038efea5b7daf7661c10c (diff) | |
download | rspamd-eb68f30ca2261fba8388c9774b117c6213eaa4ba.tar.gz rspamd-eb68f30ca2261fba8388c9774b117c6213eaa4ba.zip |
Add utility to convert ip to int.
-rw-r--r-- | src/lua/lua_ip.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/lua/lua_ip.c b/src/lua/lua_ip.c index 014b35493..daf5a03ce 100644 --- a/src/lua/lua_ip.c +++ b/src/lua/lua_ip.c @@ -24,6 +24,8 @@ #include "lua_common.h" LUA_FUNCTION_DEF (ip, to_string); +LUA_FUNCTION_DEF (ip, to_number); +LUA_FUNCTION_DEF (ip, from_number); LUA_FUNCTION_DEF (ip, to_table); LUA_FUNCTION_DEF (ip, str_octets); LUA_FUNCTION_DEF (ip, inversed_str_octets); @@ -34,6 +36,7 @@ LUA_FUNCTION_DEF (ip, get_version); static const struct luaL_reg iplib_m[] = { LUA_INTERFACE_DEF (ip, to_string), LUA_INTERFACE_DEF (ip, to_table), + LUA_INTERFACE_DEF (ip, to_number), LUA_INTERFACE_DEF (ip, str_octets), LUA_INTERFACE_DEF (ip, inversed_str_octets), LUA_INTERFACE_DEF (ip, get_version), @@ -44,6 +47,7 @@ static const struct luaL_reg iplib_m[] = { static const struct luaL_reg iplib_f[] = { LUA_INTERFACE_DEF (ip, from_string), + LUA_INTERFACE_DEF (ip, from_number), {NULL, NULL} }; @@ -213,6 +217,70 @@ lua_ip_from_string (lua_State *L) } static gint +lua_ip_to_number (lua_State *L) +{ + struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + guint32 dst[4], i; + + if (ip != NULL) { + if (ip->af == AF_INET) { + /* One integer in host byte order */ + lua_pushinteger (L, ntohl (ip->data.ip4.s_addr)); + } + else { + /* 4 integers in host byte order */ + G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (dst)); + memcpy (dst, &ip->data, sizeof (dst)); + for (i = 0; i < G_N_ELEMENTS (dst); i ++) { + lua_pushinteger (L, ntohl (dst[i])); + } + return 4; + } + } + else { + lua_pushnil (L); + } + + return 1; +} + +static gint +lua_ip_from_number (lua_State *L) +{ + guint32 src[4], i; + struct rspamd_lua_ip *ip, **pip; + + if (lua_gettop (L) == 1 && lua_isnumber (L, 1)) { + /* Ipv4 version */ + ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); + src[0] = lua_tointeger (L, 1); + ip->af = AF_INET; + ip->data.ip4.s_addr = htonl (src[0]); + pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); + lua_setclass (L, "rspamd{ip}", -1); + *pip = ip; + } + else if (lua_gettop (L) == 4 && lua_isnumber (L, 1)) { + /* Ipv6 version */ + for (i = 0; i < 4; i ++) { + src[i] = htonl (lua_tonumber (L, i + 1)); + } + G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (src)); + ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); + ip->af = AF_INET6; + memcpy (&ip->data, src, sizeof (src)); + pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); + lua_setclass (L, "rspamd{ip}", -1); + *pip = ip; + } + else { + lua_pushnil (L); + } + + return 1; +} + +static gint lua_ip_destroy (lua_State *L) { struct rspamd_lua_ip *ip = lua_check_ip (L, 1); |