summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Westphal <fw@strlen.de>2018-08-29 11:37:39 +0200
committerFlorian Westphal <fw@strlen.de>2018-08-29 23:53:48 +0200
commit92029c1282958aad13eb8602c67b73caf2a08a09 (patch)
treebff7a62d56ba4fb33d3a44adcbfac1d3b726f95c
parent176d9ad8c351cff9f0c21da5b5f2848e4c77b249 (diff)
src: osf: add json support
Signed-off-by: Florian Westphal <fw@strlen.de>
-rw-r--r--include/json.h2
-rw-r--r--src/json.c5
-rw-r--r--src/osf.c2
-rw-r--r--src/parser_json.c17
-rw-r--r--tests/py/inet/osf.t.json66
5 files changed, 91 insertions, 1 deletions
diff --git a/include/json.h b/include/json.h
index b75512b8..78c026a7 100644
--- a/include/json.h
+++ b/include/json.h
@@ -40,6 +40,7 @@ json_t *hash_expr_json(const struct expr *expr, struct output_ctx *octx);
json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx);
json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx);
json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx);
json_t *integer_type_json(const struct expr *expr, struct output_ctx *octx);
json_t *string_type_json(const struct expr *expr, struct output_ctx *octx);
@@ -118,6 +119,7 @@ EXPR_PRINT_STUB(hash_expr)
EXPR_PRINT_STUB(fib_expr)
EXPR_PRINT_STUB(constant_expr)
EXPR_PRINT_STUB(socket_expr)
+EXPR_PRINT_STUB(osf_expr)
EXPR_PRINT_STUB(integer_type)
EXPR_PRINT_STUB(string_type)
diff --git a/src/json.c b/src/json.c
index 00d24764..eac7a3a0 100644
--- a/src/json.c
+++ b/src/json.c
@@ -811,6 +811,11 @@ json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx)
socket_templates[expr->socket.key].token);
}
+json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx)
+{
+ return json_pack("{s:{s:s}}", "osf", "key", "name");
+}
+
json_t *integer_type_json(const struct expr *expr, struct output_ctx *octx)
{
char buf[1024] = "0x";
diff --git a/src/osf.c b/src/osf.c
index b8457cc4..fc09e157 100644
--- a/src/osf.c
+++ b/src/osf.c
@@ -3,6 +3,7 @@
#include <utils.h>
#include <string.h>
#include <osf.h>
+#include <json.h>
static void osf_expr_print(const struct expr *expr, struct output_ctx *octx)
{
@@ -18,6 +19,7 @@ static const struct expr_ops osf_expr_ops = {
.name = "osf",
.print = osf_expr_print,
.clone = osf_expr_clone,
+ .json = osf_expr_json,
};
struct expr *osf_expr_alloc(const struct location *loc)
diff --git a/src/parser_json.c b/src/parser_json.c
index b51f95e1..6d8cda97 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -346,13 +346,27 @@ static struct expr *json_parse_meta_expr(struct json_ctx *ctx,
return meta_expr_alloc(int_loc, key);
}
+static struct expr *json_parse_osf_expr(struct json_ctx *ctx,
+ const char *type, json_t *root)
+{
+ const char *key;
+
+ if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
+ return NULL;
+
+ if (!strcmp(key, "name"))
+ return osf_expr_alloc(int_loc);
+
+ json_error(ctx, "Invalid osf key value.");
+ return NULL;
+}
+
static struct expr *json_parse_socket_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *key;
int keyval = -1;
-
if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
return NULL;
@@ -1181,6 +1195,7 @@ static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root)
{ "exthdr", json_parse_exthdr_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP },
{ "tcp option", json_parse_tcp_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES },
{ "meta", json_parse_meta_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP },
+ { "osf", json_parse_osf_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_MAP },
{ "socket", json_parse_socket_expr, CTX_F_PRIMARY },
{ "rt", json_parse_rt_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP },
{ "ct", json_parse_ct_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP },
diff --git a/tests/py/inet/osf.t.json b/tests/py/inet/osf.t.json
new file mode 100644
index 00000000..10fb68c1
--- /dev/null
+++ b/tests/py/inet/osf.t.json
@@ -0,0 +1,66 @@
+# osf name "Linux"
+[
+ {
+ "match": {
+ "left": {
+ "osf": {
+ "key": "name"
+ }
+ },
+ "right": "Linux"
+ }
+ }
+]
+
+# osf name { "Windows", "MacOs" }
+[
+ {
+ "match": {
+ "left": {
+ "osf": {
+ "key": "name"
+ }
+ },
+ "right": {
+ "set": [
+ "MacOs",
+ "Windows"
+ ]
+ }
+ }
+ }
+]
+
+# ct mark set osf name map { "Windows" : 0x00000001, "MacOs" : 0x00000002 }
+[
+ {
+ "mangle": {
+ "left": {
+ "ct": {
+ "key": "mark"
+ }
+ },
+ "right": {
+ "map": {
+ "left": {
+ "osf": {
+ "key": "name"
+ }
+ },
+ "right": {
+ "set": [
+ [
+ "MacOs",
+ 2
+ ],
+ [
+ "Windows",
+ 1
+ ]
+ ]
+ }
+ }
+ }
+ }
+ }
+]