]> source.dussan.org Git - rspamd.git/commitdiff
Fix ipv6 addresses exporting.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Nov 2013 16:36:57 +0000 (16:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Nov 2013 16:36:57 +0000 (16:36 +0000)
src/lua/lua_ip.c

index 89bb91ed005d8e14c1510695a2fffa0b36bba0b0..1297f0403a892f7710ef4ce8a42e148cfcab182b 100644 (file)
@@ -71,9 +71,8 @@ lua_ip_to_table (lua_State *L)
                }
                ptr = (guint8 *)&ip->data;
                for (i = 1; i <= max; i ++, ptr ++) {
-                       lua_pushnumber (L, i);
                        lua_pushnumber (L, *ptr);
-                       lua_settable (L, -3);
+                       lua_rawseti (L, -2, i);
                }
        }
        else {
@@ -89,7 +88,7 @@ lua_ip_str_octets (lua_State *L)
        struct rspamd_lua_ip *ip = lua_check_ip (L, 1);
        int max, i;
        guint8 *ptr;
-       char numbuf[4];
+       char numbuf[8];
 
        if (ip != NULL) {
                lua_newtable (L);
@@ -101,10 +100,19 @@ lua_ip_str_octets (lua_State *L)
                }
                ptr = (guint8 *)&ip->data;
                for (i = 1; i <= max; i ++, ptr ++) {
-                       rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
-                       lua_pushnumber (L, i);
-                       lua_pushstring (L, numbuf);
-                       lua_settable (L, -3);
+                       if (ip->af == AF_INET) {
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i);
+                       }
+                       else {
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", (*ptr & 0xf0) >> 4);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i*2 - 1);
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", *ptr & 0x0f);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i*2);
+                       }
                }
        }
        else {
@@ -133,10 +141,19 @@ lua_ip_inversed_str_octets (lua_State *L)
                ptr = (guint8 *)&ip->data;
                ptr += max - 1;
                for (i = 1; i <= max; i ++, ptr --) {
-                       rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
-                       lua_pushnumber (L, i);
-                       lua_pushstring (L, numbuf);
-                       lua_settable (L, -3);
+                       if (ip->af == AF_INET) {
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i);
+                       }
+                       else {
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", *ptr & 0x0f);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i*2 - 1);
+                               rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", (*ptr & 0xf0) >> 4);
+                               lua_pushstring (L, numbuf);
+                               lua_rawseti (L, -2, i*2);
+                       }
                }
        }
        else {
@@ -265,7 +282,7 @@ luaopen_ip (lua_State * L)
        lua_rawset (L, -3);
 
        luaL_register (L, NULL, iplib_m);
-       luaL_register (L, "ip", iplib_f);
+       luaL_register (L, "rspamd_ip", iplib_f);
 
        lua_pop (L, 1);                      /* remove metatable from stack */