diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-04-21 14:52:18 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-04-21 14:52:18 +0100 |
commit | e1ff872f1a1070ec27ba9c5d011e19044941a3a8 (patch) | |
tree | 57edc4ae1c0711a1043b5f6ebfa1cccb92f3426d /src/lua/lua_ip.c | |
parent | 4eef5ea9ba8af9c57b3985b4671b2c6fa0d081dd (diff) | |
download | rspamd-e1ff872f1a1070ec27ba9c5d011e19044941a3a8.tar.gz rspamd-e1ff872f1a1070ec27ba9c5d011e19044941a3a8.zip |
Fix lua API for new inet addr structure.
Diffstat (limited to 'src/lua/lua_ip.c')
-rw-r--r-- | src/lua/lua_ip.c | 75 |
1 files changed, 24 insertions, 51 deletions
diff --git a/src/lua/lua_ip.c b/src/lua/lua_ip.c index 4706af9ee..105a84120 100644 --- a/src/lua/lua_ip.c +++ b/src/lua/lua_ip.c @@ -71,13 +71,13 @@ lua_ip_to_table (lua_State *L) if (ip != NULL && ip->is_valid) { lua_newtable (L); - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { max = 32 / 8; } else { max = 128 / 8; } - ptr = (guint8 *)&ip->data; + ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr; for (i = 1; i <= max; i ++, ptr ++) { lua_pushnumber (L, *ptr); lua_rawseti (L, -2, i); @@ -100,15 +100,15 @@ lua_ip_str_octets (lua_State *L) if (ip != NULL && ip->is_valid) { lua_newtable (L); - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { max = 32 / 8; } else { max = 128 / 8; } - ptr = (guint8 *)&ip->data; + ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr; for (i = 1; i <= max; i ++, ptr ++) { - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr); lua_pushstring (L, numbuf); lua_rawseti (L, -2, i); @@ -140,16 +140,16 @@ lua_ip_inversed_str_octets (lua_State *L) if (ip != NULL && ip->is_valid) { lua_newtable (L); - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { max = 32 / 8; } else { max = 128 / 8; } - ptr = (guint8 *)&ip->data; + ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr; ptr += max - 1; for (i = 1; i <= max; i ++, ptr --) { - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr); lua_pushstring (L, numbuf); lua_rawseti (L, -2, i); @@ -175,10 +175,9 @@ static gint lua_ip_to_string (lua_State *L) { struct rspamd_lua_ip *ip = lua_check_ip (L, 1); - gchar dst[INET6_ADDRSTRLEN + 1]; if (ip != NULL && ip->is_valid) { - lua_pushstring (L, inet_ntop (ip->af, &ip->data, dst, sizeof (dst))); + lua_pushstring (L, rspamd_inet_address_to_string (&ip->addr)); } else { lua_pushnil (L); @@ -196,17 +195,7 @@ lua_ip_from_string (lua_State *L) ip_str = luaL_checkstring (L, 1); if (ip_str) { ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); - if (inet_pton (AF_INET, ip_str, &ip->data.ip4) == 1) { - ip->af = AF_INET; - ip->is_valid = TRUE; - } - else if (inet_pton (AF_INET6, ip_str, &ip->data.ip6) == 1) { - ip->af = AF_INET6; - ip->is_valid = TRUE; - } - else { - ip->is_valid = FALSE; - } + ip->is_valid = rspamd_parse_inet_address (&ip->addr, ip_str); pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); lua_setclass (L, "rspamd{ip}", -1); *pip = ip; @@ -225,14 +214,14 @@ lua_ip_to_number (lua_State *L) guint32 dst[4], i; if (ip != NULL && ip->is_valid) { - if (ip->af == AF_INET) { + if (ip->addr.af == AF_INET) { /* One integer in host byte order */ - lua_pushinteger (L, ntohl (ip->data.ip4.s_addr)); + lua_pushinteger (L, ntohl (ip->addr.addr.s4.sin_addr.s_addr)); } else { /* 4 integers in host byte order */ - G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (dst)); - memcpy (dst, &ip->data, sizeof (dst)); + G_STATIC_ASSERT (sizeof (ip->addr.addr.s6.sin6_addr) >= sizeof (dst)); + memcpy (dst, &ip->addr.addr.s6.sin6_addr, sizeof (dst)); for (i = 0; i < G_N_ELEMENTS (dst); i ++) { lua_pushinteger (L, ntohl (dst[i])); } @@ -256,9 +245,9 @@ lua_ip_from_number (lua_State *L) /* Ipv4 version */ ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); src[0] = lua_tointeger (L, 1); - ip->af = AF_INET; + ip->addr.af = AF_INET; ip->is_valid = TRUE; - ip->data.ip4.s_addr = htonl (src[0]); + ip->addr.addr.s4.sin_addr.s_addr = htonl (src[0]); pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); lua_setclass (L, "rspamd{ip}", -1); *pip = ip; @@ -268,11 +257,11 @@ lua_ip_from_number (lua_State *L) for (i = 0; i < 4; i ++) { src[i] = htonl (lua_tonumber (L, i + 1)); } - G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (src)); + G_STATIC_ASSERT (sizeof (ip->addr.addr.s6.sin6_addr) >= sizeof (src)); ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); - ip->af = AF_INET6; + ip->addr.af = AF_INET6; ip->is_valid = TRUE; - memcpy (&ip->data, src, sizeof (src)); + memcpy (&ip->addr.addr.s6.sin6_addr, src, sizeof (src)); pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); lua_setclass (L, "rspamd{ip}", -1); *pip = ip; @@ -302,7 +291,7 @@ lua_ip_get_version (lua_State *L) struct rspamd_lua_ip *ip = lua_check_ip (L, 1); if (ip && ip->is_valid) { - lua_pushnumber (L, ip->af == AF_INET6 ? 6 : 4); + lua_pushnumber (L, ip->addr.af == AF_INET6 ? 6 : 4); } else { lua_pushnil (L); @@ -327,25 +316,18 @@ lua_ip_is_valid (lua_State *L) } void -lua_ip_push (lua_State *L, int af, gpointer data) +lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr) { struct rspamd_lua_ip *ip, **pip; ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); - if (!rspamd_ip_is_valid (data, af)) { + if (!rspamd_ip_is_valid (addr)) { ip->is_valid = FALSE; } else { ip->is_valid = TRUE; - ip->af = af; - if (af == AF_INET6) { - memcpy (&ip->data, data, sizeof (struct in6_addr)); - } - else { - memcpy (&ip->data, data, sizeof (struct in_addr)); - } - + memcpy (&ip->addr, addr, sizeof (ip->addr)); } pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); lua_setclass (L, "rspamd{ip}", -1); @@ -362,16 +344,7 @@ lua_ip_push_fromstring (lua_State *L, const gchar *ip_str) } else { ip = g_slice_alloc (sizeof (struct rspamd_lua_ip)); - ip->is_valid = TRUE; - if (inet_pton (AF_INET, ip_str, &ip->data.ip4) == 1) { - ip->af = AF_INET; - } - else if (inet_pton (AF_INET6, ip_str, &ip->data.ip6) == 1) { - ip->af = AF_INET6; - } - else { - ip->is_valid = FALSE; - } + ip->is_valid = rspamd_parse_inet_address (&ip->addr, ip_str); pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); lua_setclass (L, "rspamd{ip}", -1); |