]> source.dussan.org Git - rspamd.git/commitdiff
Store IP addresses properly in lua.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 17 Nov 2013 23:58:30 +0000 (23:58 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 17 Nov 2013 23:58:30 +0000 (23:58 +0000)
src/lua/lua_cfg_file.c
src/lua/lua_common.h
src/lua/lua_dns.c
src/lua/lua_ip.c
src/lua/lua_task.c

index 6d50153f46b7f96892ef1016f0886816c7d5270b..47fbd14bb38d68e8602833dd690f2d3ede66332b 100644 (file)
@@ -195,8 +195,6 @@ lua_post_load_config (struct config_file *cfg)
 gboolean
 lua_handle_param (struct worker_task *task, gchar *mname, gchar *optname, enum lua_var_type expected_type, gpointer *res)
 {
-       lua_State                            *L = task->cfg->lua_state;
-
        /* xxx: Adopt this for rcl */
        
        /* Option not found */
index d04ad5c010f32f1d3966f260df7e352c0097738e..ec5215edf152002e23da4907d8615c1b45c1956f 100644 (file)
@@ -118,6 +118,16 @@ gint lua_rcl_obj_push (lua_State *L, ucl_object_t *obj, gboolean allow_array);
  */
 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);
+
+/**
+ * Push ip address from a string (nil is pushed if a string cannot be converted)
+ */
+void lua_ip_push_fromstring (lua_State *L, const gchar *ip_str);
+
 /**
  * Lua IP address structure
  */
index cb011445a0b43f2b4e2a25cb4658c180b8e3d024..fde9da42276297398406705b2ab3d2c05f770fe0 100644 (file)
@@ -67,7 +67,6 @@ lua_dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
 {
        struct lua_dns_cbdata              *cd = arg;
        gint                            i = 0;
-       struct in_addr                  ina;
        struct rspamd_dns_resolver    **presolver;
        union rspamd_reply_element     *elt;
        GList                          *cur;
@@ -86,9 +85,19 @@ lua_dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
                        cur = reply->elements;
                        while (cur) {
                                elt = cur->data;
-                               memcpy (&ina, &elt->a.addr[0], sizeof (struct in_addr));
-                               /* Actually this copy memory, so using of inet_ntoa is valid */
-                               lua_pushstring (cd->L, inet_ntoa (ina));
+                               lua_ip_push (cd->L, AF_INET, &elt->a.addr);
+                               lua_rawseti (cd->L, -2, ++i);
+                               cur = g_list_next (cur);
+                       }
+                       lua_pushnil (cd->L);
+               }
+               if (reply->type == DNS_REQUEST_AAA) {
+
+                       lua_newtable (cd->L);
+                       cur = reply->elements;
+                       while (cur) {
+                               elt = cur->data;
+                               lua_ip_push (cd->L, AF_INET6, &elt->aaa.addr);
                                lua_rawseti (cd->L, -2, ++i);
                                cur = g_list_next (cur);
                        }
index 862e99a05ab845fac173318229c895974b2e8647..b0db30673a94c9a68ff959b09559caf286bd8672 100644 (file)
@@ -73,7 +73,7 @@ lua_ip_to_table (lua_State *L)
                for (i = 1; i <= max; i ++, ptr ++) {
                        lua_pushnumber (L, i);
                        lua_pushnumber (L, *ptr);
-                       lua_settable (L, -2);
+                       lua_settable (L, -3);
                }
        }
        else {
@@ -104,7 +104,7 @@ lua_ip_str_octets (lua_State *L)
                        rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
                        lua_pushnumber (L, i);
                        lua_pushstring (L, numbuf);
-                       lua_settable (L, -2);
+                       lua_settable (L, -3);
                }
        }
        else {
@@ -136,7 +136,7 @@ lua_ip_inversed_str_octets (lua_State *L)
                        rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
                        lua_pushnumber (L, i);
                        lua_pushstring (L, numbuf);
-                       lua_settable (L, -2);
+                       lua_settable (L, -3);
                }
        }
        else {
@@ -205,6 +205,48 @@ lua_ip_destroy (lua_State *L)
        return 0;
 }
 
+void
+lua_ip_push (lua_State *L, int af, gpointer data)
+{
+       struct rspamd_lua_ip *ip, **pip;
+
+       ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
+
+       ip->af = af;
+       if (af == AF_INET6) {
+               memcpy (&ip->data, data, sizeof (struct in6_addr));
+       }
+       else {
+               memcpy (&ip->data, data, sizeof (struct in_addr));
+       }
+       pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
+       lua_setclass (L, "rspamd{ip}", -1);
+       *pip = ip;
+}
+
+void
+lua_ip_push_fromstring (lua_State *L, const gchar *ip_str)
+{
+       struct rspamd_lua_ip *ip, **pip;
+
+       ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
+       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 {
+               g_slice_free1 (sizeof (struct rspamd_lua_ip), ip);
+               lua_pushnil (L);
+               return;
+       }
+
+       pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
+       lua_setclass (L, "rspamd{ip}", -1);
+       *pip = ip;
+}
+
 gint
 luaopen_ip (lua_State * L)
 {
index 86dcd459d4bf05aee615f3c7eaf511a31d988eaa..80fc048f0ba78764a41bdb6c027d587e0c289ce6 100644 (file)
@@ -647,9 +647,13 @@ lua_task_get_received_headers (lua_State * L)
                        }
                        lua_newtable (L);
                        lua_set_table_index (L, "from_hostname", rh->from_hostname);
-                       lua_set_table_index (L, "from_ip", rh->from_ip);
+                       lua_pushstring (L, "from_ip");
+                       lua_ip_push_fromstring (L, rh->from_ip);
+                       lua_settable (L, -3);
                        lua_set_table_index (L, "real_hostname", rh->real_hostname);
-                       lua_set_table_index (L, "real_ip", rh->real_ip);
+                       lua_pushstring (L, "real_ip");
+                       lua_ip_push_fromstring (L, rh->real_ip);
+                       lua_settable (L, -3);
                        lua_set_table_index (L, "by_hostname", rh->by_hostname);
                        lua_rawseti (L, -2, i++);
                        cur = g_list_next (cur);
@@ -684,7 +688,6 @@ lua_dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
 {
        struct lua_dns_callback_data   *cd = arg;
        gint                            i = 0;
-       struct in_addr                  ina;
        struct worker_task            **ptask;
        union rspamd_reply_element     *elt;
        GList                          *cur;
@@ -708,9 +711,19 @@ lua_dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
                        cur = reply->elements;
                        while (cur) {
                                elt = cur->data;
-                               memcpy (&ina, &elt->a.addr[0], sizeof (struct in_addr));
-                               /* Actually this copy memory, so using of inet_ntoa is valid */
-                               lua_pushstring (cd->L, inet_ntoa (ina));
+                               lua_ip_push (cd->L, AF_INET, &elt->a.addr);
+                               lua_rawseti (cd->L, -2, ++i);
+                               cur = g_list_next (cur);
+                       }
+                       lua_pushnil (cd->L);
+               }
+               if (reply->type == DNS_REQUEST_AAA) {
+
+                       lua_newtable (cd->L);
+                       cur = reply->elements;
+                       while (cur) {
+                               elt = cur->data;
+                               lua_ip_push (cd->L, AF_INET6, &elt->aaa.addr);
                                lua_rawseti (cd->L, -2, ++i);
                                cur = g_list_next (cur);
                        }
@@ -1205,78 +1218,28 @@ static gint
 lua_task_get_from_ip (lua_State *L)
 {
        struct worker_task             *task = lua_check_task (L);
-#ifdef HAVE_INET_PTON
-       gchar                                                   ipbuf[INET6_ADDRSTRLEN];
-#endif
        
        if (task) {
-#ifdef HAVE_INET_PTON
-               if (task->from_addr.ipv6) {
-                       inet_ntop (AF_INET6, &task->from_addr.d.in6, ipbuf, sizeof (ipbuf));
-               }
-               else {
-                       inet_ntop (AF_INET, &task->from_addr.d.in4, ipbuf, sizeof (ipbuf));
-               }
-               lua_pushstring (L, ipbuf);
-               return 1;
-#else
-               if (task->from_addr.s_addr != INADDR_NONE && task->from_addr.s_addr != INADDR_ANY) {
-                       lua_pushstring (L, inet_ntoa (task->from_addr));
-                       return 1;
-               }
-#endif
+               lua_ip_push (L, task->from_addr.ipv6 ? AF_INET6 : AF_INET, &task->from_addr.d);
+       }
+       else {
+               lua_pushnil (L);
        }
-
-       lua_pushnil (L);
        return 1;
 }
 
 static gint
 lua_task_set_from_ip (lua_State *L)
 {
-       struct worker_task             *task = lua_check_task (L);
-       const gchar                                        *new_ip_str;
-
-       if (task) {
-               new_ip_str = luaL_checkstring (L, 2);
-#ifdef HAVE_INET_PTON
-               if (inet_pton (AF_INET, new_ip_str, &task->from_addr.d.in4) != 1) {
-                       if (inet_pton (AF_INET6, new_ip_str, &task->from_addr.d.in6) != 1) {
-                               msg_warn ("cannot convert %s to ip address", new_ip_str);
-                       }
-               }
-               return 0;
-#else
-               if (inet_aton (new_ip_str, &task->from_addr) != 0) {
-                       msg_warn ("cannot convert %s to ip address", new_ip_str);
-               }
-#endif
-       }
 
+       msg_err ("this function is deprecated and should no longer be used");
        return 0;
 }
 
 static gint
 lua_task_get_from_ip_num (lua_State *L)
 {
-       struct worker_task             *task = lua_check_task (L);
-       
-       if (task) {
-#ifdef HAVE_INET_PTON
-               if (!task->from_addr.ipv6 && task->from_addr.d.in4.s_addr != INADDR_NONE) {
-                       lua_pushinteger (L, ntohl (task->from_addr.d.in4.s_addr));
-                       return 1;
-               }
-               /* TODO: do something with ipv6 numeric representation */
-#else
-               if (task->from_addr.s_addr != INADDR_NONE && task->from_addr.s_addr != INADDR_ANY) {
-                       lua_pushinteger (L, ntohl (task->from_addr.s_addr));
-                       return 1;
-               }
-
-#endif
-       }
-
+       msg_err ("this function is deprecated and should no longer be used");
        lua_pushnil (L);
        return 1;
 }
@@ -1287,13 +1250,12 @@ lua_task_get_client_ip_num (lua_State *L)
        struct worker_task             *task = lua_check_task (L);
        
        if (task) {
-               if (task->client_addr.s_addr != INADDR_NONE && task->client_addr.s_addr != INADDR_ANY) {
-                       lua_pushinteger (L, ntohl (task->client_addr.s_addr));
-                       return 1;
-               }
+               lua_ip_push (L, AF_INET, &task->client_addr);
+       }
+       else {
+               lua_pushnil (L);
        }
 
-       lua_pushnil (L);
        return 1;
 }