aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lua/lua_common.h8
-rw-r--r--src/lua/lua_dns.c13
-rw-r--r--src/lua/lua_ip.c75
-rw-r--r--src/lua/lua_task.c4
4 files changed, 39 insertions, 61 deletions
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index 9902fd845..f9d977244 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -126,7 +126,7 @@ ucl_object_t * lua_rcl_obj_get (lua_State *L, gint idx);
/**
* Push lua ip address
*/
-void lua_ip_push (lua_State *L, int af, gpointer data);
+void lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr);
/**
* Push ip address from a string (nil is pushed if a string cannot be converted)
@@ -142,11 +142,7 @@ int rspamd_lua_typerror (lua_State *L, int narg, const char *tname);
* Lua IP address structure
*/
struct rspamd_lua_ip {
- union {
- struct in6_addr ip6;
- struct in_addr ip4;
- } data;
- int af;
+ rspamd_inet_addr_t addr;
gboolean is_valid;
};
diff --git a/src/lua/lua_dns.c b/src/lua/lua_dns.c
index 21599220a..5e926ca2d 100644
--- a/src/lua/lua_dns.c
+++ b/src/lua/lua_dns.c
@@ -90,6 +90,7 @@ lua_dns_callback (struct rdns_reply *reply, gpointer arg)
gint i = 0;
struct rspamd_dns_resolver **presolver;
struct rdns_reply_entry *elt;
+ rspamd_inet_addr_t addr;
lua_rawgeti (cd->L, LUA_REGISTRYINDEX, cd->cbref);
presolver = lua_newuserdata (cd->L, sizeof (gpointer));
@@ -106,11 +107,19 @@ lua_dns_callback (struct rdns_reply *reply, gpointer arg)
LL_FOREACH (reply->entries, elt) {
switch (elt->type) {
case RDNS_REQUEST_A:
- lua_ip_push (cd->L, AF_INET, &elt->content.a.addr);
+ addr.af = AF_INET;
+ addr.slen = sizeof (addr.addr.s4);
+ memcpy (&addr.addr.s4.sin_addr, &elt->content.a.addr,
+ sizeof (addr.addr.s4.sin_addr));
+ lua_ip_push (cd->L, &addr);
lua_rawseti (cd->L, -2, ++i);
break;
case RDNS_REQUEST_AAAA:
- lua_ip_push (cd->L, AF_INET6, &elt->content.aaa.addr);
+ addr.af = AF_INET6;
+ addr.slen = sizeof (addr.addr.s6);
+ memcpy (&addr.addr.s6.sin6_addr, &elt->content.aaa.addr,
+ sizeof (addr.addr.s6.sin6_addr));
+ lua_ip_push (cd->L, &addr);
lua_rawseti (cd->L, -2, ++i);
break;
case RDNS_REQUEST_PTR:
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);
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index de42989da..4ef56b3d4 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -993,7 +993,7 @@ lua_task_get_from_ip (lua_State *L)
struct rspamd_task *task = lua_check_task (L);
if (task) {
- lua_ip_push (L, task->from_addr.ipv6 ? AF_INET6 : AF_INET, &task->from_addr.d);
+ lua_ip_push (L, &task->from_addr);
}
else {
lua_pushnil (L);
@@ -1023,7 +1023,7 @@ lua_task_get_client_ip_num (lua_State *L)
struct rspamd_task *task = lua_check_task (L);
if (task) {
- lua_ip_push (L, AF_INET, &task->client_addr);
+ lua_ip_push (L, &task->client_addr);
}
else {
lua_pushnil (L);