]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
expression: use common code for expr_ops/expr_ops_by_type
authorFlorian Westphal <fw@strlen.de>
Sat, 22 Feb 2020 21:12:02 +0000 (22:12 +0100)
committerFlorian Westphal <fw@strlen.de>
Sun, 23 Feb 2020 22:31:12 +0000 (23:31 +0100)
Useless duplication.  Also, this avoids bloating expr_ops_by_type()
when it needs to cope with more expressions.

Signed-off-by: Florian Westphal <fw@strlen.de>
include/expression.h
src/expression.c

index cbf09b59c82bb1c95a6b2892c0bfc57b1b0952d1..62fbbbb5a737209d7835755fc7843f31ec99c8b8 100644 (file)
@@ -72,6 +72,7 @@ enum expr_types {
        EXPR_FIB,
        EXPR_XFRM,
 };
+#define EXPR_MAX EXPR_XFRM
 
 enum ops {
        OP_INVALID,
index cb11cda43792955d02ed676cbb22d3224be7cd0b..a2694f4ab0e4e6193259a8fc122ef867aa011058 100644 (file)
@@ -1185,9 +1185,9 @@ void range_expr_value_high(mpz_t rop, const struct expr *expr)
        }
 }
 
-const struct expr_ops *expr_ops(const struct expr *e)
+static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
 {
-       switch (e->etype) {
+       switch (etype) {
        case EXPR_INVALID:
                BUG("Invalid expression ops requested");
                break;
@@ -1220,26 +1220,21 @@ const struct expr_ops *expr_ops(const struct expr *e)
        case EXPR_XFRM: return &xfrm_expr_ops;
        }
 
-       BUG("Unknown expression type %d\n", e->etype);
+       BUG("Unknown expression type %d\n", etype);
 }
 
-const struct expr_ops *expr_ops_by_type(enum expr_types etype)
+const struct expr_ops *expr_ops(const struct expr *e)
 {
-       switch (etype) {
-       case EXPR_PAYLOAD: return &payload_expr_ops;
-       case EXPR_EXTHDR: return &exthdr_expr_ops;
-       case EXPR_META: return &meta_expr_ops;
-       case EXPR_SOCKET: return &socket_expr_ops;
-       case EXPR_OSF: return &osf_expr_ops;
-       case EXPR_CT: return &ct_expr_ops;
-       case EXPR_NUMGEN: return &numgen_expr_ops;
-       case EXPR_HASH: return &hash_expr_ops;
-       case EXPR_RT: return &rt_expr_ops;
-       case EXPR_FIB: return &fib_expr_ops;
-       case EXPR_XFRM: return &xfrm_expr_ops;
-       default:
-               break;
-       }
+       return __expr_ops_by_type(e->etype);
+}
 
-       BUG("Unknown expression type %d\n", etype);
+const struct expr_ops *expr_ops_by_type(uint32_t value)
+{
+       /* value might come from unreliable source, such as "udata"
+        * annotation of set keys.  Avoid BUG() assertion.
+        */
+       if (value == EXPR_INVALID || value > EXPR_MAX)
+               return NULL;
+
+       return __expr_ops_by_type(value);
 }