]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: hlua: Use net_addr structure internally to parse and compare addresses
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 26 Feb 2021 08:39:05 +0000 (09:39 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 26 Feb 2021 12:53:26 +0000 (13:53 +0100)
hlua_addr structure may be replaced by net_addr structure to parse and
compare addresses. Both structures are similar.

include/haproxy/hlua-t.h
src/hlua_fcn.c

index f843ebdd0bfe155f9897fd42a76503ef140fd5d1..964654c44a3255cd0db55b10afc73893a720ba01 100644 (file)
@@ -176,20 +176,6 @@ struct hlua_concat {
        int len;
 };
 
-struct hlua_addr {
-       union {
-               struct {
-                       struct in_addr ip;
-                       struct in_addr mask;
-               } v4;
-               struct {
-                       struct in6_addr ip;
-                       struct in6_addr mask;
-               } v6;
-       } addr;
-       int type;
-};
-
 #else /* USE_LUA */
 /************************ For use when Lua is disabled ********************/
 
index 0af89b45671cb4f1bca37c49e49899080653c6c5..c2d26817ac3e0f4699c739c28c304f42cbf45e81 100644 (file)
@@ -33,6 +33,7 @@
 #include <haproxy/stats.h>
 #include <haproxy/stick_table.h>
 #include <haproxy/time.h>
+#include <haproxy/tools.h>
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
@@ -1481,24 +1482,24 @@ int hlua_tokenize(lua_State *L)
 
 int hlua_parse_addr(lua_State *L)
 {
-       struct hlua_addr *addr;
+       struct net_addr *addr;
        const char *str = luaL_checkstring(L, 1);
        unsigned char mask;
 
-       addr = lua_newuserdata(L, sizeof(struct hlua_addr));
+       addr = lua_newuserdata(L, sizeof(struct net_addr));
        if (!addr) {
                lua_pushnil(L);
                return 1;
        }
 
        if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
-               addr->type = AF_INET;
+               addr->family = AF_INET;
                return 1;
        }
 
        if (str62net(str, &addr->addr.v6.ip, &mask)) {
                len2mask6(mask, &addr->addr.v6.mask);
-               addr->type = AF_INET6;
+               addr->family = AF_INET6;
                return 1;
        }
 
@@ -1509,8 +1510,8 @@ int hlua_parse_addr(lua_State *L)
 
 int hlua_match_addr(lua_State *L)
 {
-       struct hlua_addr *addr1;
-       struct hlua_addr *addr2;
+       struct net_addr *addr1;
+       struct net_addr *addr2;
 
        if (!lua_isuserdata(L, 1) ||
            !lua_isuserdata(L, 2)) {
@@ -1521,12 +1522,12 @@ int hlua_match_addr(lua_State *L)
        addr1 = lua_touserdata(L, 1);
        addr2 = lua_touserdata(L, 2);
 
-       if (addr1->type != addr2->type) {
+       if (addr1->family != addr2->family) {
                lua_pushboolean(L, 0);
                return 1;
        }
 
-       if (addr1->type == AF_INET) {
+       if (addr1->family == AF_INET) {
                if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
                    (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
                        lua_pushboolean(L, 1);