]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
Introduce boolean datatype and boolean expression
authorPhil Sutter <phil@nwl.cc>
Fri, 10 Mar 2017 17:13:49 +0000 (18:13 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 10 Mar 2017 18:01:21 +0000 (19:01 +0100)
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/datatype.h
src/datatype.c
src/parser_bison.y
src/scanner.l

index b78d76f78f76cc43f562e52c668c85614ab4dd35..e614b96e880bfa712ccb47e97026ad3e1d2a2d44 100644 (file)
@@ -82,6 +82,7 @@ enum datatypes {
        TYPE_DSCP,
        TYPE_ECN,
        TYPE_FIB_ADDR,
+       TYPE_BOOLEAN,
        __TYPE_MAX
 };
 #define TYPE_MAX               (__TYPE_MAX - 1)
@@ -233,6 +234,7 @@ extern const struct datatype icmp_code_type;
 extern const struct datatype icmpv6_code_type;
 extern const struct datatype icmpx_code_type;
 extern const struct datatype time_type;
+extern const struct datatype boolean_type;
 
 extern const struct datatype *concat_type_alloc(uint32_t type);
 extern void concat_type_destroy(const struct datatype *dtype);
index 6b1dd4a09abbc0867ce79a7b0f3cc2aa8d9262a9..c61c4245c386887386b0d8d2b53077bd741e77c7 100644 (file)
@@ -48,6 +48,7 @@ static const struct datatype *datatypes[TYPE_MAX + 1] = {
        [TYPE_ICMP_CODE]        = &icmp_code_type,
        [TYPE_ICMPV6_CODE]      = &icmpv6_code_type,
        [TYPE_ICMPX_CODE]       = &icmpx_code_type,
+       [TYPE_BOOLEAN]          = &boolean_type,
 };
 
 void datatype_register(const struct datatype *dtype)
@@ -1104,3 +1105,21 @@ struct error_record *rate_parse(const struct location *loc, const char *str,
 
        return NULL;
 }
+
+static const struct symbol_table boolean_tbl = {
+       .base           = BASE_DECIMAL,
+       .symbols        = {
+               SYMBOL("exists",        true),
+               SYMBOL("missing",       false),
+               SYMBOL_LIST_END
+       },
+};
+
+const struct datatype boolean_type = {
+       .type           = TYPE_BOOLEAN,
+       .name           = "boolean",
+       .desc           = "boolean type",
+       .size           = 1,
+       .basetype       = &integer_type,
+       .sym_tbl        = &boolean_tbl,
+};
index dff8a5ab90aceb3ece4ff12666883d7f8a35f787..f2ae82f471dd62e08279baf93ca832535fda81dd 100644 (file)
@@ -448,6 +448,9 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 
 %token NOTRACK                 "notrack"
 
+%token EXISTS                  "exists"
+%token MISSING                 "missing"
+
 %type <string>                 identifier type_identifier string comment_spec
 %destructor { xfree($$); }     identifier type_identifier string comment_spec
 
@@ -651,6 +654,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %type <val>                    tcp_hdr_field
 %type <val>                    tcp_hdr_option_type tcp_hdr_option_field
 
+%type <expr>                   boolean_expr
+%destructor { expr_free($$); } boolean_expr
+%type <val>                    boolean_keys
+
 %%
 
 input                  :       /* empty */
@@ -2655,8 +2662,21 @@ concat_rhs_expr          :       basic_rhs_expr
                        }
                        ;
 
+boolean_keys           :       EXISTS          { $$ = true; }
+                       |       MISSING         { $$ = false; }
+                       ;
+
+boolean_expr           :       boolean_keys
+                       {
+                               $$ = constant_expr_alloc(&@$, &boolean_type,
+                                                        BYTEORDER_HOST_ENDIAN,
+                                                        1, &$1);
+                       }
+                       ;
+
 primary_rhs_expr       :       symbol_expr             { $$ = $1; }
                        |       integer_expr            { $$ = $1; }
+                       |       boolean_expr            { $$ = $1; }
                        |       ETHER
                        {
                                $$ = symbol_expr_alloc(&@$, SYMBOL_VALUE,
index 003f15eb1c3c27e1e673a0c862e21e33cbcf4af6..b0d571988650a1ede5ecebcfdc561f04f4c6112c 100644 (file)
@@ -504,6 +504,9 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
 "xml"                  { return XML; }
 "json"                 { return JSON; }
 
+"exists"               { return EXISTS; }
+"missing"              { return MISSING; }
+
 {addrstring}           {
                                yylval->string = xstrdup(yytext);
                                return STRING;