]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
IP2BIN, IP2HBIN, IP2STR: refactor `interp` call-backs
authorJeremy Sowden <jeremy@azazel.net>
Mon, 26 May 2025 17:19:02 +0000 (18:19 +0100)
committerFlorian Westphal <fw@strlen.de>
Sun, 1 Jun 2025 12:12:47 +0000 (14:12 +0200)
IP2STR and IP2BIN do all family checks inside the for-loop that converts the
address fields, whereas IP2HBIN does the checks once before the loop.  Refactor
the former to do as the latter.

Also, move all the remaining contents of the for-loops, apart from the
`pp_is_valid` checks, into `ip2*` functions.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
filter/ulogd_filter_IP2BIN.c
filter/ulogd_filter_IP2HBIN.c
filter/ulogd_filter_IP2STR.c

index f1ca4eee7d76a134ddd7d488180b783d31af15fb..2667a2a7f717917a1da17cc045c07cf81561deae 100644 (file)
@@ -114,14 +114,42 @@ static struct ulogd_key ip2bin_keys[] = {
 
 static char ipbin_array[MAX_KEY - START_KEY + 1][FORMAT_IPV6_BUFSZ];
 
-static int ip2bin(struct ulogd_key *inp, int index, int oindex)
+static void ip2bin(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+                  uint8_t addr_family)
 {
-       char family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
-       char convfamily = family;
-       struct in6_addr *addr;
-       struct in6_addr ip4_addr;
+       struct in6_addr *addr, ip4_addr;
+
+       switch (addr_family) {
+       case AF_INET6:
+               addr = (struct in6_addr *)ikey_get_u128(&inp[i]);
+               break;
+       case AF_INET:
+               /* Convert IPv4 to IPv4 in IPv6 */
+               addr = &ip4_addr;
+               uint32_to_ipv6(ikey_get_u32(&inp[i]), addr);
+               break;
+       }
+
+       format_ipv6(ipbin_array[o], sizeof(ipbin_array[o]), addr);
+
+       okey_set_ptr(&outp[o], ipbin_array[o]);
+}
+
+static int interp_ip2bin(struct ulogd_pluginstance *pi)
+{
+       struct ulogd_key *outp = pi->output.keys;
+       struct ulogd_key *inp = pi->input.keys;
+       uint8_t proto_family, addr_family;
+       int i, o;
+
+       proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
 
-       if (family == AF_BRIDGE) {
+       switch (proto_family) {
+       case AF_INET6:
+       case AF_INET:
+               addr_family = proto_family;
+               break;
+       case AF_BRIDGE:
                if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
                        ulogd_log(ULOGD_NOTICE,
                                  "No protocol inside AF_BRIDGE packet\n");
@@ -129,56 +157,28 @@ static int ip2bin(struct ulogd_key *inp, int index, int oindex)
                }
                switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
                case ETH_P_IPV6:
-                       convfamily = AF_INET6;
+                       addr_family = AF_INET6;
                        break;
                case ETH_P_IP:
-                       convfamily = AF_INET;
-                       break;
                case ETH_P_ARP:
-                       convfamily = AF_INET;
+                       addr_family = AF_INET;
                        break;
                default:
                        ulogd_log(ULOGD_NOTICE,
                                  "Unknown protocol inside AF_BRIDGE packet\n");
                        return ULOGD_IRET_ERR;
                }
+               break;
+       default:
+               /* TODO handle error */
+               ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
+               return ULOGD_IRET_ERR;
        }
 
-       switch (convfamily) {
-               case AF_INET6:
-                       addr = (struct in6_addr *)ikey_get_u128(&inp[index]);
-                       break;
-               case AF_INET:
-                       /* Convert IPv4 to IPv4 in IPv6 */
-                       addr = &ip4_addr;
-                       uint32_to_ipv6(ikey_get_u32(&inp[index]), addr);
-                       break;
-               default:
-                       /* TODO handle error */
-                       ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
-                       return ULOGD_IRET_ERR;
-       }
-
-       format_ipv6(ipbin_array[oindex], sizeof(ipbin_array[oindex]), addr);
-
-       return ULOGD_IRET_OK;
-}
-
-static int interp_ip2bin(struct ulogd_pluginstance *pi)
-{
-       struct ulogd_key *ret = pi->output.keys;
-       struct ulogd_key *inp = pi->input.keys;
-       int i;
-       int fret;
-
        /* Iter on all addr fields */
-       for(i = START_KEY; i <= MAX_KEY; i++) {
+       for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
                if (pp_is_valid(inp, i)) {
-                       fret = ip2bin(inp, i, i - START_KEY);
-                       if (fret != ULOGD_IRET_OK)
-                               return fret;
-                       okey_set_ptr(&ret[i - START_KEY],
-                                    ipbin_array[i - START_KEY]);
+                       ip2bin(inp, i, outp, o, addr_family);
                }
        }
 
index 48ea6a2cbc14ca2224d489a334a76d171db33874..42ffc94975841dbf651ff3ca4a39cd4a78ed2fb5 100644 (file)
@@ -112,17 +112,32 @@ static struct ulogd_key ip2hbin_keys[] = {
        },
 };
 
+static void ip2hbin(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+                  uint8_t addr_family)
+{
+       switch (addr_family) {
+       case AF_INET6:
+               okey_set_u128(&outp[o], ikey_get_u128(&inp[i]));
+               break;
+       case AF_INET:
+               okey_set_u32(&outp[o], ntohl(ikey_get_u32(&inp[i])));
+               break;
+       }
+}
+
 static int interp_ip2hbin(struct ulogd_pluginstance *pi)
 {
-       struct ulogd_key *ret = pi->output.keys;
+       struct ulogd_key *outp = pi->output.keys;
        struct ulogd_key *inp = pi->input.keys;
-       uint8_t family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
-       uint8_t convfamily = family;
-       int i;
+       uint8_t proto_family, addr_family;
+       int i, o;
 
-       switch (family) {
-       case AF_INET:
+       proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
+
+       switch (proto_family) {
        case AF_INET6:
+       case AF_INET:
+               addr_family = proto_family;
                break;
        case AF_BRIDGE:
                if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
@@ -132,13 +147,11 @@ static int interp_ip2hbin(struct ulogd_pluginstance *pi)
                }
                switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
                case ETH_P_IPV6:
-                       convfamily = AF_INET6;
+                       addr_family = AF_INET6;
                        break;
                case ETH_P_IP:
-                       convfamily = AF_INET;
-                       break;
                case ETH_P_ARP:
-                       convfamily = AF_INET;
+                       addr_family = AF_INET;
                        break;
                default:
                        ulogd_log(ULOGD_NOTICE,
@@ -147,26 +160,15 @@ static int interp_ip2hbin(struct ulogd_pluginstance *pi)
                }
                break;
        default:
-               ulogd_log(ULOGD_NOTICE,
-                         "Unknown protocol inside packet\n");
+               /* TODO handle error */
+               ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
                return ULOGD_IRET_ERR;
        }
 
        /* Iter on all addr fields */
-       for(i = START_KEY; i <= MAX_KEY; i++) {
+       for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
                if (pp_is_valid(inp, i)) {
-                       switch (convfamily) {
-                       case AF_INET:
-                               okey_set_u32(&ret[i - START_KEY],
-                                            ntohl(ikey_get_u32(&inp[i])));
-                               break;
-                       case AF_INET6:
-                               okey_set_u128(&ret[i - START_KEY],
-                                             ikey_get_u128(&inp[i]));
-                               break;
-                       default:
-                               break;
-                       }
+                       ip2hbin(inp, i, outp, o, addr_family);
                }
        }
 
index fec892a62dac7a500a4bd88c49fde2cbe4ce73ff..194a8b1032721d77372b2c809bd167b82db40816 100644 (file)
@@ -137,12 +137,44 @@ static struct ulogd_key ip2str_keys[] = {
 
 static char ipstr_array[MAX_KEY - START_KEY + 1][INET6_ADDRSTRLEN];
 
-static int ip2str(struct ulogd_key *inp, int index, int oindex)
+static void ip2str(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+                  uint8_t addr_family)
 {
-       char family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
-       char convfamily = family;
+       union {
+               struct in6_addr in6;
+               struct in_addr  in;
+       } addr;
 
-       if (family == AF_BRIDGE) {
+       switch (addr_family) {
+       case AF_INET6:
+               memcpy(addr.in6.s6_addr, ikey_get_u128(&inp[i]),
+                      sizeof(addr.in6.s6_addr));
+               break;
+       case AF_INET:
+               addr.in.s_addr = ikey_get_u32(&inp[i]);
+               break;
+       }
+
+       inet_ntop(addr_family, &addr, ipstr_array[o], sizeof(ipstr_array[o]));
+
+       okey_set_ptr(&outp[o], ipstr_array[o]);
+}
+
+static int interp_ip2str(struct ulogd_pluginstance *pi)
+{
+       struct ulogd_key *outp = pi->output.keys;
+       struct ulogd_key *inp = pi->input.keys;
+       uint8_t proto_family, addr_family;
+       int i, o;
+
+       proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
+
+       switch (proto_family) {
+       case AF_INET6:
+       case AF_INET:
+               addr_family = proto_family;
+               break;
+       case AF_BRIDGE:
                if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
                        ulogd_log(ULOGD_NOTICE,
                                  "No protocol inside AF_BRIDGE packet\n");
@@ -150,56 +182,28 @@ static int ip2str(struct ulogd_key *inp, int index, int oindex)
                }
                switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
                case ETH_P_IPV6:
-                       convfamily = AF_INET6;
+                       addr_family = AF_INET6;
                        break;
                case ETH_P_IP:
-                       convfamily = AF_INET;
-                       break;
                case ETH_P_ARP:
-                       convfamily = AF_INET;
+                       addr_family = AF_INET;
                        break;
                default:
                        ulogd_log(ULOGD_NOTICE,
                                  "Unknown protocol inside AF_BRIDGE packet\n");
                        return ULOGD_IRET_ERR;
                }
-       }
-
-       switch (convfamily) {
-               uint32_t ip;
-       case AF_INET6:
-               inet_ntop(AF_INET6,
-                         ikey_get_u128(&inp[index]),
-                         ipstr_array[oindex], sizeof(ipstr_array[oindex]));
-               break;
-       case AF_INET:
-               ip = ikey_get_u32(&inp[index]);
-               inet_ntop(AF_INET, &ip,
-                         ipstr_array[oindex], sizeof(ipstr_array[oindex]));
                break;
        default:
                /* TODO error handling */
                ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
                return ULOGD_IRET_ERR;
        }
-       return ULOGD_IRET_OK;
-}
-
-static int interp_ip2str(struct ulogd_pluginstance *pi)
-{
-       struct ulogd_key *ret = pi->output.keys;
-       struct ulogd_key *inp = pi->input.keys;
-       int i;
-       int fret;
 
        /* Iter on all addr fields */
-       for (i = START_KEY; i <= MAX_KEY; i++) {
+       for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
                if (pp_is_valid(inp, i)) {
-                       fret = ip2str(inp, i, i - START_KEY);
-                       if (fret != ULOGD_IRET_OK)
-                               return fret;
-                       okey_set_ptr(&ret[i - START_KEY],
-                                    ipstr_array[i-START_KEY]);
+                       ip2str(inp, i, outp, o, addr_family);
                }
        }