We have better json support in libnftables these days. Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> --- configure.ac | 14 - examples/Makefile.am | 20 -- examples/nft-chain-get.c | 5 +- examples/nft-chain-parse-add.c | 160 ----------- examples/nft-ct-helper-get.c | 14 +- examples/nft-ct-timeout-get.c | 15 +- examples/nft-events.c | 12 +- examples/nft-flowtable-get.c | 5 +- examples/nft-obj-get.c | 15 +- examples/nft-rule-get.c | 8 +- examples/nft-rule-parse-add.c | 159 ----------- examples/nft-ruleset-get.c | 12 +- examples/nft-ruleset-parse-file.c | 467 ------------------------------- examples/nft-set-elem-get.c | 6 +- examples/nft-set-get.c | 5 +- examples/nft-set-parse-add.c | 162 ----------- examples/nft-table-get.c | 11 +- examples/nft-table-parse-add.c | 157 ----------- include/Makefile.am | 1 - include/common.h | 11 - include/expr_ops.h | 2 - include/internal.h | 1 - include/json.h | 58 ---- include/obj.h | 2 - src/Makefile.am | 4 +- src/buffer.c | 220 --------------- src/chain.c | 193 +------------ src/common.c | 74 ----- src/expr/bitwise.c | 59 ---- src/expr/byteorder.c | 59 ---- src/expr/cmp.c | 51 ---- src/expr/connlimit.c | 38 --- src/expr/counter.c | 38 --- src/expr/ct.c | 66 ----- src/expr/data_reg.c | 137 --------- src/expr/dup.c | 37 --- src/expr/dynset.c | 55 ---- src/expr/exthdr.c | 89 ------ src/expr/fib.c | 44 --- src/expr/flow_offload.c | 32 --- src/expr/fwd.c | 42 --- src/expr/hash.c | 70 ----- src/expr/immediate.c | 57 ---- src/expr/limit.c | 48 ---- src/expr/log.c | 63 ----- src/expr/lookup.c | 50 ---- src/expr/masq.c | 42 --- src/expr/match.c | 32 --- src/expr/meta.c | 52 ---- src/expr/nat.c | 81 ------ src/expr/numgen.c | 52 ---- src/expr/objref.c | 47 ---- src/expr/osf.c | 13 - src/expr/payload.c | 55 ---- src/expr/queue.c | 49 ---- src/expr/quota.c | 43 --- src/expr/range.c | 59 +--- src/expr/redir.c | 44 --- src/expr/reject.c | 37 --- src/expr/rt.c | 44 --- src/expr/socket.c | 15 - src/expr/target.c | 32 --- src/expr/tunnel.c | 15 - src/expr/xfrm.c | 62 ---- src/flowtable.c | 146 +--------- src/gen.c | 16 +- src/jansson.c | 279 ------------------ src/obj/counter.c | 38 --- src/obj/ct_helper.c | 43 --- src/obj/ct_timeout.c | 16 -- src/obj/limit.c | 52 ---- src/obj/quota.c | 43 --- src/obj/tunnel.c | 13 - src/object.c | 92 ------ src/rule.c | 175 +----------- src/ruleset.c | 412 +-------------------------- src/set.c | 260 +---------------- src/set_elem.c | 104 +------ src/table.c | 93 +----- tests/Makefile.am | 9 +- tests/jsonfiles/01-table.json | 1 - tests/jsonfiles/02-table.json | 1 - tests/jsonfiles/11-chain.json | 1 - tests/jsonfiles/12-chain.json | 1 - tests/jsonfiles/13-chain.json | 1 - tests/jsonfiles/14-chain.json | 1 - tests/jsonfiles/20-rule-bitwise.json | 1 - tests/jsonfiles/21-rule-byteorder.json | 1 - tests/jsonfiles/22-rule-cmp.json | 1 - tests/jsonfiles/23-rule-counter.json | 1 - tests/jsonfiles/24-rule-ct.json | 1 - tests/jsonfiles/25-rule-exthdr.json | 1 - tests/jsonfiles/26-rule-immediate.json | 1 - tests/jsonfiles/27-rule-limit.json | 1 - tests/jsonfiles/28-rule-log.json | 1 - tests/jsonfiles/29-rule-match.json | 1 - tests/jsonfiles/30-rule-lookup.json | 1 - tests/jsonfiles/31-rule-meta.json | 1 - tests/jsonfiles/32-rule-nat4.json | 1 - tests/jsonfiles/33-rule-nat6.json | 1 - tests/jsonfiles/34-rule-payload.json | 1 - tests/jsonfiles/35-rule-target.json | 1 - tests/jsonfiles/36-rule-real.json | 1 - tests/jsonfiles/37-rule-real.json | 1 - tests/jsonfiles/38-rule-real.json | 1 - tests/jsonfiles/39-rule-real.json | 1 - tests/jsonfiles/40-rule-real.json | 1 - tests/jsonfiles/41-rule-real.json | 1 - tests/jsonfiles/42-rule-real.json | 1 - tests/jsonfiles/43-rule-real.json | 1 - tests/jsonfiles/44-rule-real.json | 1 - tests/jsonfiles/45-rule-real.json | 1 - tests/jsonfiles/46-rule-real.json | 1 - tests/jsonfiles/47-rule-real.json | 1 - tests/jsonfiles/48-rule-real.json | 1 - tests/jsonfiles/49-rule-real.json | 1 - tests/jsonfiles/50-rule-real.json | 1 - tests/jsonfiles/51-rule-real.json | 1 - tests/jsonfiles/52-rule-real.json | 1 - tests/jsonfiles/53-rule-real.json | 1 - tests/jsonfiles/54-rule-real.json | 1 - tests/jsonfiles/55-rule-real.json | 1 - tests/jsonfiles/56-rule-real.json | 1 - tests/jsonfiles/57-rule-real.json | 1 - tests/jsonfiles/58-rule-real.json | 1 - tests/jsonfiles/59-rule-real.json | 1 - tests/jsonfiles/60-rule-real.json | 1 - tests/jsonfiles/61-rule-real.json | 1 - tests/jsonfiles/62-set.json | 1 - tests/jsonfiles/63-set.json | 1 - tests/jsonfiles/64-ruleset.json | 1 - tests/jsonfiles/65-rule-meta-target.json | 1 - tests/jsonfiles/66-rule-queue.json | 1 - tests/jsonfiles/67-rule-queue.json | 1 - tests/jsonfiles/68-rule-masq.json | 1 - tests/jsonfiles/69-rule-redir.json | 1 - tests/jsonfiles/70-rule-real.json | 1 - tests/nft-parsing-test.c | 283 ------------------- tests/test-script.sh | 1 - 139 files changed, 38 insertions(+), 5716 deletions(-) delete mode 100644 examples/nft-chain-parse-add.c delete mode 100644 examples/nft-rule-parse-add.c delete mode 100644 examples/nft-ruleset-parse-file.c delete mode 100644 examples/nft-set-parse-add.c delete mode 100644 examples/nft-table-parse-add.c delete mode 100644 include/json.h delete mode 100644 src/buffer.c delete mode 100644 src/jansson.c delete mode 100644 tests/jsonfiles/01-table.json delete mode 100644 tests/jsonfiles/02-table.json delete mode 100644 tests/jsonfiles/11-chain.json delete mode 100644 tests/jsonfiles/12-chain.json delete mode 100644 tests/jsonfiles/13-chain.json delete mode 100644 tests/jsonfiles/14-chain.json delete mode 100644 tests/jsonfiles/20-rule-bitwise.json delete mode 100644 tests/jsonfiles/21-rule-byteorder.json delete mode 100644 tests/jsonfiles/22-rule-cmp.json delete mode 100644 tests/jsonfiles/23-rule-counter.json delete mode 100644 tests/jsonfiles/24-rule-ct.json delete mode 100644 tests/jsonfiles/25-rule-exthdr.json delete mode 100644 tests/jsonfiles/26-rule-immediate.json delete mode 100644 tests/jsonfiles/27-rule-limit.json delete mode 100644 tests/jsonfiles/28-rule-log.json delete mode 100644 tests/jsonfiles/29-rule-match.json delete mode 100644 tests/jsonfiles/30-rule-lookup.json delete mode 100644 tests/jsonfiles/31-rule-meta.json delete mode 100644 tests/jsonfiles/32-rule-nat4.json delete mode 100644 tests/jsonfiles/33-rule-nat6.json delete mode 100644 tests/jsonfiles/34-rule-payload.json delete mode 100644 tests/jsonfiles/35-rule-target.json delete mode 100644 tests/jsonfiles/36-rule-real.json delete mode 100644 tests/jsonfiles/37-rule-real.json delete mode 100644 tests/jsonfiles/38-rule-real.json delete mode 100644 tests/jsonfiles/39-rule-real.json delete mode 100644 tests/jsonfiles/40-rule-real.json delete mode 100644 tests/jsonfiles/41-rule-real.json delete mode 100644 tests/jsonfiles/42-rule-real.json delete mode 100644 tests/jsonfiles/43-rule-real.json delete mode 100644 tests/jsonfiles/44-rule-real.json delete mode 100644 tests/jsonfiles/45-rule-real.json delete mode 100644 tests/jsonfiles/46-rule-real.json delete mode 100644 tests/jsonfiles/47-rule-real.json delete mode 100644 tests/jsonfiles/48-rule-real.json delete mode 100644 tests/jsonfiles/49-rule-real.json delete mode 100644 tests/jsonfiles/50-rule-real.json delete mode 100644 tests/jsonfiles/51-rule-real.json delete mode 100644 tests/jsonfiles/52-rule-real.json delete mode 100644 tests/jsonfiles/53-rule-real.json delete mode 100644 tests/jsonfiles/54-rule-real.json delete mode 100644 tests/jsonfiles/55-rule-real.json delete mode 100644 tests/jsonfiles/56-rule-real.json delete mode 100644 tests/jsonfiles/57-rule-real.json delete mode 100644 tests/jsonfiles/58-rule-real.json delete mode 100644 tests/jsonfiles/59-rule-real.json delete mode 100644 tests/jsonfiles/60-rule-real.json delete mode 100644 tests/jsonfiles/61-rule-real.json delete mode 100644 tests/jsonfiles/62-set.json delete mode 100644 tests/jsonfiles/63-set.json delete mode 100644 tests/jsonfiles/64-ruleset.json delete mode 100644 tests/jsonfiles/65-rule-meta-target.json delete mode 100644 tests/jsonfiles/66-rule-queue.json delete mode 100644 tests/jsonfiles/67-rule-queue.json delete mode 100644 tests/jsonfiles/68-rule-masq.json delete mode 100644 tests/jsonfiles/69-rule-redir.json delete mode 100644 tests/jsonfiles/70-rule-real.json delete mode 100644 tests/nft-parsing-test.c diff --git a/configure.ac b/configure.ac index dbb723d8eb06..d05532d1a20e 100644 --- a/configure.ac +++ b/configure.ac @@ -15,13 +15,6 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) dnl Dependencies PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3]) -AC_ARG_WITH([json-parsing], - AS_HELP_STRING([--with-json-parsing], [JSON parsing support])) - -AS_IF([test "x$with_json_parsing" = "xyes"], - [PKG_CHECK_MODULES([LIBJSON], [jansson >= 2.3])], - [with_json_parsing="no"] -) AC_PROG_CC AM_PROG_CC_C_O AC_EXEEXT @@ -35,9 +28,6 @@ esac regular_CPPFLAGS="-D_FILE_OFFSET_BITS=64 -D_REENTRANT" -AS_IF([test "x$with_json_parsing" = "xyes"], [ - regular_CPPFLAGS="$regular_CPPFLAGS -DJSON_PARSING" -]) regular_CFLAGS="-Wall -Waggregate-return -Wmissing-declarations \ -Wmissing-prototypes -Wshadow -Wstrict-prototypes \ -Wformat=2 -Wwrite-strings -pipe" @@ -45,7 +35,3 @@ AC_SUBST([regular_CPPFLAGS]) AC_SUBST([regular_CFLAGS]) AC_CONFIG_FILES([Makefile src/Makefile include/Makefile include/libnftnl/Makefile include/linux/Makefile include/linux/netfilter/Makefile examples/Makefile tests/Makefile libnftnl.pc doxygen.cfg]) AC_OUTPUT - -echo " -libnftnl configuration: - JSON support: ${with_json_parsing}" diff --git a/examples/Makefile.am b/examples/Makefile.am index 67f01560ef59..d044b90738b9 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,21 +1,17 @@ include $(top_srcdir)/Make_global.am check_PROGRAMS = nft-table-add \ - nft-table-parse-add \ nft-table-upd \ nft-table-del \ nft-table-get \ nft-chain-add \ - nft-chain-parse-add \ nft-chain-del \ nft-chain-get \ nft-rule-add \ - nft-rule-parse-add \ nft-rule-del \ nft-rule-get \ nft-events \ nft-set-add \ - nft-set-parse-add \ nft-set-get \ nft-set-del \ nft-map-add \ @@ -32,7 +28,6 @@ check_PROGRAMS = nft-table-add \ nft-flowtable-del \ nft-flowtable-get \ nft-ruleset-get \ - nft-ruleset-parse-file \ nft-compat-get \ nft-ct-helper-add \ nft-ct-helper-get \ @@ -43,9 +38,6 @@ check_PROGRAMS = nft-table-add \ nft_table_add_SOURCES = nft-table-add.c nft_table_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_table_parse_add_SOURCES = nft-table-parse-add.c -nft_table_parse_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - nft_table_upd_SOURCES = nft-table-upd.c nft_table_upd_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} @@ -58,9 +50,6 @@ nft_table_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_chain_add_SOURCES = nft-chain-add.c nft_chain_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_chain_parse_add_SOURCES = nft-chain-parse-add.c -nft_chain_parse_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - nft_chain_del_SOURCES = nft-chain-del.c nft_chain_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} @@ -70,9 +59,6 @@ nft_chain_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_rule_add_SOURCES = nft-rule-add.c nft_rule_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_rule_parse_add_SOURCES = nft-rule-parse-add.c -nft_rule_parse_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - nft_rule_del_SOURCES = nft-rule-del.c nft_rule_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} @@ -85,9 +71,6 @@ nft_events_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_set_add_SOURCES = nft-set-add.c nft_set_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_set_parse_add_SOURCES = nft-set-parse-add.c -nft_set_parse_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - nft_set_del_SOURCES = nft-set-del.c nft_set_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} @@ -136,9 +119,6 @@ nft_flowtable_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_ruleset_get_SOURCES = nft-ruleset-get.c nft_ruleset_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_ruleset_parse_file_SOURCES = nft-ruleset-parse-file.c -nft_ruleset_parse_file_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - nft_compat_get_SOURCES = nft-compat-get.c nft_compat_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} diff --git a/examples/nft-chain-get.c b/examples/nft-chain-get.c index 6fda2d8124d0..fcccbf718fd7 100644 --- a/examples/nft-chain-get.c +++ b/examples/nft-chain-get.c @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) seq = time(NULL); if (argc < 2 || argc > 5) { - fprintf(stderr, "Usage: %s <family> [<table> <chain>] [json]\n", + fprintf(stderr, "Usage: %s <family> [<table> <chain>]\n", argv[0]); exit(EXIT_FAILURE); } @@ -95,9 +95,6 @@ int main(int argc, char *argv[]) NLM_F_DUMP, seq); } - if (strcmp(argv[argc-1], "json") == 0) - type = NFTNL_OUTPUT_JSON; - nl = mnl_socket_open(NETLINK_NETFILTER); if (nl == NULL) { perror("mnl_socket_open"); diff --git a/examples/nft-chain-parse-add.c b/examples/nft-chain-parse-add.c deleted file mode 100644 index 9198615d9a5f..000000000000 --- a/examples/nft-chain-parse-add.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * (C) 2013 by Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> - * (C) 2014 by Arturo Borrero Gonzalez <arturo@xxxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This code has been sponsored by Sophos Astaro <http://www.sophos.com> - */ - -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <netinet/in.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> - -#include <linux/netfilter.h> -#include <linux/netfilter/nf_tables.h> - -#include <libmnl/libmnl.h> -#include <libnftnl/chain.h> -#include <libnftnl/rule.h> - -static struct nftnl_chain *chain_parse_file(const char *file, uint16_t format) -{ - int fd; - struct nftnl_chain *c; - struct nftnl_parse_err *err; - char data[4096]; - - c = nftnl_chain_alloc(); - if (c == NULL) { - perror("OOM"); - return NULL; - } - - fd = open(file, O_RDONLY); - if (fd < 0) { - perror("open"); - return NULL; - } - - if (read(fd, data, sizeof(data)) < 0) { - perror("read"); - close(fd); - return NULL; - } - - close(fd); - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("OOM"); - return NULL; - } - - if (nftnl_chain_parse(c, format, data, err) < 0) { - nftnl_parse_perror("Unable to parse file", err); - nftnl_parse_err_free(err); - return NULL; - } - - nftnl_parse_err_free(err); - return c; -} - -int main(int argc, char *argv[]) -{ - struct mnl_socket *nl; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - uint32_t portid, seq, chain_seq; - struct nftnl_chain *c; - uint16_t family, format, outformat; - struct mnl_nlmsg_batch *batch; - int ret; - - if (argc < 3) { - printf("Usage: %s {json} <file>\n", argv[0]); - exit(EXIT_FAILURE); - } - - if (strcmp(argv[1], "json") == 0) { - format = NFTNL_PARSE_JSON; - outformat = NFTNL_OUTPUT_JSON; - } else { - printf("Unknown format: only json is supported\n"); - exit(EXIT_FAILURE); - } - - c = chain_parse_file(argv[2], format); - if (c == NULL) - exit(EXIT_FAILURE); - - nftnl_chain_fprintf(stdout, c, outformat, 0); - fprintf(stdout, "\n"); - - nftnl_chain_unset(c, NFTNL_CHAIN_HANDLE); - family = nftnl_chain_get_u32(c, NFTNL_CHAIN_FAMILY); - - seq = time(NULL); - - batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); - - nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - chain_seq = seq; - nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - NFT_MSG_NEWCHAIN, family, - NLM_F_ACK, seq++); - nftnl_chain_nlmsg_build_payload(nlh, c); - nftnl_chain_free(c); - mnl_nlmsg_batch_next(batch); - - nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - nl = mnl_socket_open(NETLINK_NETFILTER); - if (nl == NULL) { - perror("mnl_socket_open"); - exit(EXIT_FAILURE); - } - - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { - perror("mnl_socket_bind"); - exit(EXIT_FAILURE); - } - - portid = mnl_socket_get_portid(nl); - - if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), - mnl_nlmsg_batch_size(batch)) < 0) { - perror("mnl_socket_send"); - exit(EXIT_FAILURE); - } - - mnl_nlmsg_batch_stop(batch); - - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - while (ret > 0) { - ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL); - if (ret <= 0) - break; - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - } - if (ret == -1) { - perror("error"); - exit(EXIT_FAILURE); - } - - - mnl_socket_close(nl); - return EXIT_SUCCESS; -} diff --git a/examples/nft-ct-helper-get.c b/examples/nft-ct-helper-get.c index 94bd0cd8fbf7..eb0313322ca7 100644 --- a/examples/nft-ct-helper-get.c +++ b/examples/nft-ct-helper-get.c @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) uint32_t type = NFTNL_OUTPUT_DEFAULT; if (argc < 3 || argc > 5) { - fprintf(stderr, "%s <family> <table> [<obj>] [<default|xml|json>]\n", + fprintf(stderr, "%s <family> <table> [<obj>]\n", argv[0]); return EXIT_FAILURE; } @@ -76,18 +76,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (strcmp(argv[argc-1], "xml") == 0) { - type = NFTNL_OUTPUT_XML; - argv[argc-1] = NULL; - argc--; - }else if (strcmp(argv[argc-1], "json") == 0) { - type = NFTNL_OUTPUT_JSON; - argv[argc-1] = NULL; - argc--; - } else if (strcmp(argv[argc - 1], "default") == 0) { - argc--; - } - if (argc == 3 || argc == 4) { t = nftnl_obj_alloc(); if (t == NULL) { diff --git a/examples/nft-ct-timeout-get.c b/examples/nft-ct-timeout-get.c index 503c48833853..badcd234529b 100644 --- a/examples/nft-ct-timeout-get.c +++ b/examples/nft-ct-timeout-get.c @@ -58,8 +58,7 @@ int main(int argc, char *argv[]) uint32_t type = NFTNL_OUTPUT_DEFAULT; if (argc < 3 || argc > 5) { - fprintf(stderr, "%s <family> <table> [<obj>] [<default|xml|json>]\n", - argv[0]); + fprintf(stderr, "%s <family> <table> [<obj>]\n", argv[0]); return EXIT_FAILURE; } @@ -76,18 +75,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (strcmp(argv[argc-1], "xml") == 0) { - type = NFTNL_OUTPUT_XML; - argv[argc-1] = NULL; - argc--; - } else if (strcmp(argv[argc-1], "json") == 0) { - type = NFTNL_OUTPUT_JSON; - argv[argc-1] = NULL; - argc--; - } else if (strcmp(argv[argc - 1], "default") == 0) { - argc--; - } - if (argc == 3 || argc == 4) { t = nftnl_obj_alloc(); if (t == NULL) { diff --git a/examples/nft-events.c b/examples/nft-events.c index bd8c752e5668..8aab90a11824 100644 --- a/examples/nft-events.c +++ b/examples/nft-events.c @@ -235,18 +235,8 @@ int main(int argc, char *argv[]) case 1: type = NFTNL_OUTPUT_DEFAULT; break; - case 2: - if (strcmp(argv[1], "json") == 0) { - type = NFTNL_OUTPUT_JSON; - } else if (strcmp(argv[1], "default") == 0) { - type = NFTNL_OUTPUT_DEFAULT; - } else { - fprintf(stderr, "unknown format type `%s'\n", argv[1]); - return EXIT_FAILURE; - } - break; default: - fprintf(stderr, "%s [<default|json>]\n", argv[0]); + fprintf(stderr, "%s\n", argv[0]); return EXIT_FAILURE; } diff --git a/examples/nft-flowtable-get.c b/examples/nft-flowtable-get.c index 37cfadf0b1cd..1a034ce60579 100644 --- a/examples/nft-flowtable-get.c +++ b/examples/nft-flowtable-get.c @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) seq = time(NULL); if (argc < 2 || argc > 5) { - fprintf(stderr, "Usage: %s <family> [<table> <flowtable>] [json]\n", + fprintf(stderr, "Usage: %s <family> [<table> <flowtable>]\n", argv[0]); exit(EXIT_FAILURE); } @@ -84,9 +84,6 @@ int main(int argc, char *argv[]) NLM_F_DUMP, seq); } - if (strcmp(argv[argc-1], "json") == 0) - type = NFTNL_OUTPUT_JSON; - nl = mnl_socket_open(NETLINK_NETFILTER); if (nl == NULL) { perror("mnl_socket_open"); diff --git a/examples/nft-obj-get.c b/examples/nft-obj-get.c index bec33b693731..e6a19fe3baf8 100644 --- a/examples/nft-obj-get.c +++ b/examples/nft-obj-get.c @@ -57,8 +57,7 @@ int main(int argc, char *argv[]) uint32_t type = NFTNL_OUTPUT_DEFAULT; if (argc < 2 || argc > 5) { - fprintf(stderr, "%s <family> <table> [<obj>] [<default|xml|json>]\n", - argv[0]); + fprintf(stderr, "%s <family> <table> [<obj>]\n", argv[0]); return EXIT_FAILURE; } @@ -77,18 +76,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (strcmp(argv[argc-1], "xml") == 0) { - type = NFTNL_OUTPUT_XML; - argv[argc-1] = NULL; - argc--; - }else if (strcmp(argv[argc-1], "json") == 0) { - type = NFTNL_OUTPUT_JSON; - argv[argc-1] = NULL; - argc--; - } else if (strcmp(argv[argc - 1], "default") == 0) { - argc--; - } - if (argc == 3 || argc == 4) { t = nftnl_obj_alloc(); if (t == NULL) { diff --git a/examples/nft-rule-get.c b/examples/nft-rule-get.c index aa496125570b..8a980ef180b0 100644 --- a/examples/nft-rule-get.c +++ b/examples/nft-rule-get.c @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) int ret, family; if (argc < 2 || argc > 5) { - fprintf(stderr, "Usage: %s <family> [<table> <chain>] [json]\n", + fprintf(stderr, "Usage: %s <family> [<table> <chain>]\n", argv[0]); exit(EXIT_FAILURE); } @@ -102,12 +102,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - /* json specified */ - if (argc == 3 || argc == 5) { - if (strcmp(argv[argc - 1], "json") == 0) - type = NFTNL_OUTPUT_JSON; - } - /* at least [<table> <chain>] specified */ if (argc >= 4) { table = argv[2]; diff --git a/examples/nft-rule-parse-add.c b/examples/nft-rule-parse-add.c deleted file mode 100644 index 3fcf2691295b..000000000000 --- a/examples/nft-rule-parse-add.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * (C) 2013 by Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> - * (C) 2013 by Arturo Borrero Gonzalez <arturo@xxxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This code has been sponsored by Sophos Astaro <http://www.sophos.com> - */ - -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <stddef.h> /* for offsetof */ -#include <netinet/in.h> -#include <arpa/inet.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> - -#include <linux/netfilter.h> -#include <linux/netfilter/nf_tables.h> - -#include <libmnl/libmnl.h> -#include <libnftnl/rule.h> - -static struct nftnl_rule *rule_parse_file(const char *file, uint16_t format) -{ - int fd; - struct nftnl_rule *r; - struct nftnl_parse_err *err; - char data[4096]; - - fd = open(file, O_RDONLY); - if (fd < 0) { - perror("open"); - return NULL; - } - - if (read(fd, data, sizeof(data)) < 0) { - perror("read"); - close(fd); - return NULL; - } - close(fd); - - r = nftnl_rule_alloc(); - if (r == NULL) { - perror("OOM"); - exit(EXIT_FAILURE); - } - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - exit(EXIT_FAILURE); - } - - if (nftnl_rule_parse(r, format, data, err) < 0) { - nftnl_parse_perror("Unable to parse file", err); - nftnl_parse_err_free(err); - nftnl_rule_free(r); - return NULL; - } - - nftnl_rule_unset(r, NFTNL_RULE_HANDLE); - - nftnl_parse_err_free(err); - return r; -} - -int main(int argc, char *argv[]) -{ - struct mnl_socket *nl; - struct mnl_nlmsg_batch *batch; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - uint32_t portid, seq, rule_seq; - struct nftnl_rule *r; - int ret; - uint16_t family, format, outformat; - - if (argc < 3) { - printf("Usage: %s {json} <file>\n", argv[0]); - exit(EXIT_FAILURE); - } - - if (strcmp(argv[1], "json") == 0) { - format = NFTNL_PARSE_JSON; - outformat = NFTNL_OUTPUT_JSON; - } else { - printf("Unknown format: json\n"); - exit(EXIT_FAILURE); - } - - r = rule_parse_file(argv[2], format); - if (r == NULL) - exit(EXIT_FAILURE); - - nftnl_rule_fprintf(stdout, r, outformat, 0); - fprintf(stdout, "\n"); - - seq = time(NULL); - batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); - - nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - rule_seq = seq; - family = nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY); - nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - NFT_MSG_NEWRULE, family, - NLM_F_CREATE|NLM_F_APPEND|NLM_F_ACK, - seq++); - nftnl_rule_nlmsg_build_payload(nlh, r); - nftnl_rule_free(r); - mnl_nlmsg_batch_next(batch); - - nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - nl = mnl_socket_open(NETLINK_NETFILTER); - if (nl == NULL) { - perror("mnl_socket_open"); - exit(EXIT_FAILURE); - } - - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { - perror("mnl_socket_bind"); - exit(EXIT_FAILURE); - } - portid = mnl_socket_get_portid(nl); - - if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), - mnl_nlmsg_batch_size(batch)) < 0) { - perror("mnl_socket_send"); - exit(EXIT_FAILURE); - } - - mnl_nlmsg_batch_stop(batch); - - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - while (ret > 0) { - ret = mnl_cb_run(buf, ret, rule_seq, portid, NULL, NULL); - if (ret <= 0) - break; - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - } - if (ret == -1) { - perror("error"); - exit(EXIT_FAILURE); - } - mnl_socket_close(nl); - - return EXIT_SUCCESS; -} diff --git a/examples/nft-ruleset-get.c b/examples/nft-ruleset-get.c index 9e80bb6f8338..70984370bc4d 100644 --- a/examples/nft-ruleset-get.c +++ b/examples/nft-ruleset-get.c @@ -344,20 +344,10 @@ int main(int argc, char *argv[]) int ret; if (argc > 2) { - fprintf(stderr, "%s {json}\n", - argv[0]); + fprintf(stderr, "%s\n", argv[0]); exit(EXIT_FAILURE); } - if (argc == 2) { - if (strcmp(argv[1], "json") == 0) - type = NFTNL_OUTPUT_JSON; - else { - fprintf(stderr, "Unknown type: only json is supported\n"); - exit(EXIT_FAILURE); - } - } - nl = mnl_socket_open(NETLINK_NETFILTER); if (nl == NULL) { perror("mnl_socket_open"); diff --git a/examples/nft-ruleset-parse-file.c b/examples/nft-ruleset-parse-file.c deleted file mode 100644 index be0994fd84f5..000000000000 --- a/examples/nft-ruleset-parse-file.c +++ /dev/null @@ -1,467 +0,0 @@ -/* - * (C) 2014 by Alvaro Neira Ayuso <alvaroneay@xxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - */ - -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <stddef.h> /* for offsetof */ -#include <netinet/in.h> -#include <netinet/ip.h> -#include <netinet/tcp.h> -#include <arpa/inet.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <errno.h> - -#include <linux/netfilter.h> -#include <linux/netfilter/nfnetlink.h> -#include <linux/netfilter/nf_tables.h> - -#include <libmnl/libmnl.h> -#include <libnftnl/ruleset.h> -#include <libnftnl/table.h> -#include <libnftnl/chain.h> -#include <libnftnl/rule.h> -#include <libnftnl/set.h> - -struct mnl_nlmsg_batch *batch; -uint32_t seq; - -static int nftnl_ruleset_set_elems(const struct nftnl_parse_ctx *ctx) -{ - struct nftnl_set_elems_iter *iter_elems; - uint16_t nl_type, nl_flags; - uint32_t cmd; - struct nlmsghdr *nlh; - struct nftnl_set *set; - - cmd = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_CMD); - - set = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_SET); - if (set == NULL) - return -1; - - switch (cmd) { - case NFTNL_CMD_ADD: - nl_type = NFT_MSG_NEWSETELEM; - nl_flags = NLM_F_CREATE|NLM_F_EXCL|NLM_F_ACK; - break; - case NFTNL_CMD_DELETE: - nl_type = NFT_MSG_DELSETELEM; - /* This will generate an ACK message for each request. When - * removing NLM_F_ACK, the kernel will only report when things - * go wrong - */ - nl_flags = NLM_F_ACK; - break; - default: - goto err; - } - - iter_elems = nftnl_set_elems_iter_create(set); - if (iter_elems == NULL) - goto err; - - nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), nl_type, - nftnl_set_get_u32(set, - NFTNL_SET_FAMILY), - nl_flags, seq++); - - nftnl_set_elems_nlmsg_build_payload_iter(nlh, iter_elems); - mnl_nlmsg_batch_next(batch); - - nftnl_set_elems_iter_destroy(iter_elems); - return 0; -err: - return -1; -} - -static int nftnl_ruleset_parse_set(const struct nftnl_parse_ctx *ctx) -{ - - struct nlmsghdr *nlh; - uint16_t nl_type, nl_flags; - struct nftnl_set *set; - uint32_t cmd; - int ret; - - cmd = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_CMD); - - set = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_SET); - if (set == NULL) - return -1; - - switch (cmd) { - case NFTNL_CMD_ADD: - nl_type = NFT_MSG_NEWSET; - nl_flags = NLM_F_CREATE|NLM_F_ACK; - break; - case NFTNL_CMD_DELETE: - nl_type = NFT_MSG_DELSET; - nl_flags = NLM_F_ACK; - break; - default: - goto err; - } - - nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - nl_type, - nftnl_set_get_u32(set, - NFTNL_SET_FAMILY), - nl_flags, - seq++); - - nftnl_set_nlmsg_build_payload(nlh, set); - mnl_nlmsg_batch_next(batch); - - ret = nftnl_ruleset_set_elems(ctx); - return ret; -err: - return -1; -} - -static int nftnl_ruleset_rule_build_msg(const struct nftnl_parse_ctx *ctx, - uint32_t cmd, struct nftnl_rule *rule) -{ - struct nlmsghdr *nlh; - uint16_t nl_type, nl_flags; - - switch (cmd) { - case NFTNL_CMD_ADD: - nl_type = NFT_MSG_NEWRULE; - nl_flags = NLM_F_APPEND|NLM_F_CREATE|NLM_F_ACK; - nftnl_rule_unset(rule, NFTNL_RULE_HANDLE); - break; - case NFTNL_CMD_DELETE: - nl_type = NFT_MSG_DELRULE; - nl_flags = NLM_F_ACK; - break; - case NFTNL_CMD_REPLACE: - nl_type = NFT_MSG_NEWRULE; - nl_flags = NLM_F_REPLACE|NLM_F_ACK; - break; - case NFTNL_CMD_INSERT: - nl_type = NFT_MSG_NEWRULE; - nl_flags = NLM_F_CREATE|NLM_F_ACK; - nftnl_rule_unset(rule, NFTNL_RULE_HANDLE); - break; - default: - return -1; - } - - nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - nl_type, - nftnl_rule_get_u32(rule, - NFTNL_RULE_FAMILY), - nl_flags, - seq++); - - nftnl_rule_nlmsg_build_payload(nlh, rule); - mnl_nlmsg_batch_next(batch); - - return 0; -} - -static int nftnl_ruleset_rule(const struct nftnl_parse_ctx *ctx) -{ - struct nftnl_rule *rule; - int ret; - uint32_t cmd; - - cmd = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_CMD); - - rule = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_RULE); - if (rule == NULL) - return -1; - - ret = nftnl_ruleset_rule_build_msg(ctx, cmd, rule); - - return ret; -} - -static int nftnl_ruleset_flush_rules(const struct nftnl_parse_ctx *ctx) -{ - struct nftnl_rule *nlr; - struct nftnl_table *nlt; - struct nftnl_chain *nlc; - uint32_t type; - int ret; - - nlr = nftnl_rule_alloc(); - if (nlr == NULL) - return -1; - - type = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_TYPE); - switch (type) { - case NFTNL_RULESET_TABLE: - nlt = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_TABLE); - nftnl_rule_set(nlr, NFTNL_RULE_TABLE, - nftnl_table_get(nlt, NFTNL_TABLE_NAME)); - nftnl_rule_set(nlr, NFTNL_RULE_FAMILY, - nftnl_table_get(nlt, NFTNL_TABLE_FAMILY)); - break; - case NFTNL_RULESET_CHAIN: - nlc = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_CHAIN); - nftnl_rule_set(nlr, NFTNL_RULE_TABLE, - nftnl_chain_get(nlc, - NFTNL_CHAIN_TABLE)); - nftnl_rule_set(nlr, NFTNL_RULE_CHAIN, - nftnl_chain_get(nlc, - NFTNL_CHAIN_NAME)); - nftnl_rule_set(nlr, NFTNL_RULE_FAMILY, - nftnl_chain_get(nlc, NFTNL_TABLE_FAMILY)); - break; - default: - goto err; - } - - ret = nftnl_ruleset_rule_build_msg(ctx, NFTNL_CMD_DELETE, nlr); - nftnl_rule_free(nlr); - - return ret; -err: - nftnl_rule_free(nlr); - return -1; -} - -static int nftnl_ruleset_chain(const struct nftnl_parse_ctx *ctx) -{ - struct nlmsghdr *nlh; - uint16_t nl_type, nl_flags; - uint32_t cmd; - struct nftnl_chain *chain; - - cmd = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_CMD); - - chain = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_CHAIN); - if (chain == NULL) - return -1; - - switch (cmd) { - case NFTNL_CMD_ADD: - nl_type = NFT_MSG_NEWCHAIN; - nl_flags = NLM_F_CREATE|NLM_F_ACK; - break; - case NFTNL_CMD_DELETE: - nl_type = NFT_MSG_DELCHAIN; - nl_flags = NLM_F_ACK; - break; - case NFTNL_CMD_FLUSH: - return nftnl_ruleset_flush_rules(ctx); - default: - goto err; - } - - nftnl_chain_unset(chain, NFTNL_CHAIN_HANDLE); - nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - nl_type, - nftnl_chain_get_u32(chain, - NFTNL_CHAIN_FAMILY), - nl_flags, - seq++); - - nftnl_chain_nlmsg_build_payload(nlh, chain); - mnl_nlmsg_batch_next(batch); - - return 0; -err: - return -1; -} - -static int nftnl_ruleset_table_build_msg(const struct nftnl_parse_ctx *ctx, - uint32_t cmd, struct nftnl_table *table) -{ - struct nlmsghdr *nlh; - uint16_t nl_type, nl_flags; - - switch (cmd) { - case NFTNL_CMD_ADD: - nl_type = NFT_MSG_NEWTABLE; - nl_flags = NLM_F_CREATE|NLM_F_ACK; - break; - case NFTNL_CMD_DELETE: - nl_type = NFT_MSG_DELTABLE; - nl_flags = NLM_F_ACK; - break; - case NFTNL_CMD_FLUSH: - return nftnl_ruleset_flush_rules(ctx); - default: - return -1; - } - - nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - nl_type, - nftnl_table_get_u32(table, - NFTNL_TABLE_FAMILY), - nl_flags, - seq++); - - nftnl_table_nlmsg_build_payload(nlh, table); - mnl_nlmsg_batch_next(batch); - - return 0; -} - -static int nftnl_ruleset_table(const struct nftnl_parse_ctx *ctx) -{ - struct nftnl_table *table; - uint32_t cmd; - int ret; - - cmd = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_CMD); - - table = nftnl_ruleset_ctx_get(ctx, NFTNL_RULESET_CTX_TABLE); - if (table == NULL) - return -1; - - ret = nftnl_ruleset_table_build_msg(ctx, cmd, table); - - return ret; -} - -static int nftnl_ruleset_flush_ruleset(const struct nftnl_parse_ctx *ctx) -{ - struct nftnl_table *table; - int ret; - - table = nftnl_table_alloc(); - if (table == NULL) - return -1; - - ret = nftnl_ruleset_table_build_msg(ctx, NFTNL_CMD_DELETE, table); - nftnl_table_free(table); - - return ret; -} - -static int ruleset_elems_cb(const struct nftnl_parse_ctx *ctx) -{ - uint32_t type; - int ret; - - type = nftnl_ruleset_ctx_get_u32(ctx, NFTNL_RULESET_CTX_TYPE); - - switch (type) { - case NFTNL_RULESET_TABLE: - ret = nftnl_ruleset_table(ctx); - break; - case NFTNL_RULESET_CHAIN: - ret = nftnl_ruleset_chain(ctx); - break; - case NFTNL_RULESET_RULE: - ret = nftnl_ruleset_rule(ctx); - break; - case NFTNL_RULESET_SET: - ret = nftnl_ruleset_parse_set(ctx); - break; - case NFTNL_RULESET_SET_ELEMS: - ret = nftnl_ruleset_set_elems(ctx); - break; - case NFTNL_RULESET_RULESET: - ret = nftnl_ruleset_flush_ruleset(ctx); - break; - default: - return -1; - } - - nftnl_ruleset_ctx_free(ctx); - return ret; -} - -int main(int argc, char *argv[]) -{ - struct nftnl_parse_err *err; - const char *filename; - FILE *fp; - int ret = -1, len, portid; - uint32_t ruleset_seq; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct mnl_socket *nl; - - if (argc < 2) { - printf("Usage: %s <file>\n", argv[0]); - exit(EXIT_FAILURE); - } - - fp = fopen(argv[1], "r"); - if (fp == NULL) { - printf("unable to open file %s: %s\n", argv[1], - strerror(errno)); - exit(EXIT_FAILURE); - } - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - exit(EXIT_FAILURE); - } - - seq = time(NULL); - batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); - - nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - ruleset_seq = seq; - - filename = argv[1]; - len = strlen(filename); - if (len >= 5 && strcmp(&filename[len - 5], ".json") == 0) - ret = nftnl_ruleset_parse_file_cb(NFTNL_PARSE_JSON, fp, err, NULL, - &ruleset_elems_cb); - else { - printf("the filename %s does not have a trailing .json\n", - filename); - exit(EXIT_FAILURE); - } - - if (ret < 0) { - nftnl_parse_perror("fail", err); - exit(EXIT_FAILURE); - } - - fclose(fp); - - nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - nl = mnl_socket_open(NETLINK_NETFILTER); - if (nl == NULL) { - perror("mnl_socket_open"); - exit(EXIT_FAILURE); - } - - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { - perror("mnl_socket_bind"); - exit(EXIT_FAILURE); - } - portid = mnl_socket_get_portid(nl); - - if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), - mnl_nlmsg_batch_size(batch)) < 0) { - perror("mnl_socket_send"); - exit(EXIT_FAILURE); - } - - mnl_nlmsg_batch_stop(batch); - - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - while (ret > 0) { - ret = mnl_cb_run(buf, ret, ruleset_seq, portid, NULL, NULL); - if (ret <= 0) - break; - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - } - if (ret == -1) { - perror("error"); - exit(EXIT_FAILURE); - } - - mnl_socket_close(nl); - return EXIT_SUCCESS; -} diff --git a/examples/nft-set-elem-get.c b/examples/nft-set-elem-get.c index 9a3ccd7cf2d4..1bc9abca3711 100644 --- a/examples/nft-set-elem-get.c +++ b/examples/nft-set-elem-get.c @@ -57,8 +57,7 @@ int main(int argc, char *argv[]) int ret; if (argc < 4 || argc > 5) { - fprintf(stderr, "%s <family> <table> <set> [<json>]\n", - argv[0]); + fprintf(stderr, "%s <family> <table> <set>\n", argv[0]); return EXIT_FAILURE; } t = nftnl_set_alloc(); @@ -80,9 +79,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (argc == 5 && strcmp(argv[4], "json") == 0 ) - type = NFTNL_OUTPUT_JSON; - nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family, NLM_F_DUMP|NLM_F_ACK, seq); nftnl_set_set(t, NFTNL_SET_NAME, argv[3]); diff --git a/examples/nft-set-get.c b/examples/nft-set-get.c index c4ad8ee3b4dd..bb33674c0df7 100644 --- a/examples/nft-set-get.c +++ b/examples/nft-set-get.c @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) int ret; if (argc < 2 || argc > 3) { - fprintf(stderr, "%s <family> [<json>]\n", argv[0]); + fprintf(stderr, "%s <family>\n", argv[0]); return EXIT_FAILURE; } t = nftnl_set_alloc(); @@ -81,9 +81,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (argc == 3 && strcmp(argv[2], "json") == 0) - type = NFTNL_OUTPUT_JSON; - nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family, NLM_F_DUMP|NLM_F_ACK, seq); /* Use this below if you want to obtain sets per table */ diff --git a/examples/nft-set-parse-add.c b/examples/nft-set-parse-add.c deleted file mode 100644 index 5197dc30aafe..000000000000 --- a/examples/nft-set-parse-add.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@xxxxxxxxx> - * - * Based on nft-set-xml-add from: - * - * (C) 2013 by Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> - * (C) 2013 by Arturo Borrero Gonzalez <arturo@xxxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - */ - -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <netinet/in.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> - -#include <linux/netfilter.h> -#include <linux/netfilter/nf_tables.h> -#include <linux/netfilter/nfnetlink.h> - -#include <libmnl/libmnl.h> -#include <libnftnl/set.h> - -static struct nftnl_set *set_parse_file(const char *file, uint16_t format) -{ - int fd; - struct nftnl_set *s; - struct nftnl_parse_err *err; - char data[4096]; - - s = nftnl_set_alloc(); - if (s == NULL) { - perror("OOM"); - return NULL; - } - - fd = open(file, O_RDONLY); - if (fd < 0) { - perror("open"); - return NULL; - } - - if (read(fd, data, sizeof(data)) < 0) { - perror("read"); - close(fd); - return NULL; - } - close(fd); - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - return NULL; - } - - if (nftnl_set_parse(s, format, data, err) < 0) { - nftnl_parse_perror("Unable to parse file", err); - nftnl_parse_err_free(err); - return NULL; - } - - nftnl_parse_err_free(err); - - nftnl_set_set_u32(s, NFTNL_SET_ID, 1); - return s; - -} - -int main(int argc, char *argv[]) -{ - struct mnl_socket *nl; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - uint32_t portid, seq, set_seq; - struct nftnl_set *s; - uint16_t family, format, outformat; - struct mnl_nlmsg_batch *batch; - int ret; - - if (argc < 2) { - printf("Usage: %s {json} <file>\n", argv[0]); - exit(EXIT_FAILURE); - } - - if (strcmp(argv[1], "json") == 0) { - format = NFTNL_PARSE_JSON; - outformat = NFTNL_OUTPUT_JSON; - } else { - printf("Unknown format: only json is supported\n"); - exit(EXIT_FAILURE); - } - - s = set_parse_file(argv[2], format); - if (s == NULL) - exit(EXIT_FAILURE); - - nftnl_set_fprintf(stdout, s, outformat, 0); - fprintf(stdout, "\n"); - - seq = time(NULL); - - batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); - - nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - family = nftnl_set_get_u32(s, NFTNL_SET_FAMILY); - - set_seq = seq; - nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - NFT_MSG_NEWSET, family, - NLM_F_CREATE|NLM_F_ACK, seq++); - nftnl_set_nlmsg_build_payload(nlh, s); - nftnl_set_free(s); - mnl_nlmsg_batch_next(batch); - - nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - nl = mnl_socket_open(NETLINK_NETFILTER); - if (nl == NULL) { - perror("mnl_socket_open"); - exit(EXIT_FAILURE); - } - - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { - perror("mnl_socket_bind"); - exit(EXIT_FAILURE); - } - portid = mnl_socket_get_portid(nl); - - if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), - mnl_nlmsg_batch_size(batch)) < 0) { - perror("mnl_socket_send"); - exit(EXIT_FAILURE); - } - - mnl_nlmsg_batch_stop(batch); - - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - while (ret > 0) { - ret = mnl_cb_run(buf, ret, set_seq, portid, NULL, NULL); - if (ret <= 0) - break; - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - } - if (ret == -1) { - perror("error"); - exit(EXIT_FAILURE); - } - - mnl_socket_close(nl); - - return EXIT_SUCCESS; -} diff --git a/examples/nft-table-get.c b/examples/nft-table-get.c index 193db7724a9e..eac2f2f6dfc8 100644 --- a/examples/nft-table-get.c +++ b/examples/nft-table-get.c @@ -57,8 +57,7 @@ int main(int argc, char *argv[]) uint32_t type = NFTNL_OUTPUT_DEFAULT; if (argc < 2 || argc > 4) { - fprintf(stderr, "%s <family> [<table>] [<default|json>]\n", - argv[0]); + fprintf(stderr, "%s <family> [<table>]\n", argv[0]); return EXIT_FAILURE; } @@ -77,14 +76,6 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (strcmp(argv[argc-1], "json") == 0) { - type = NFTNL_OUTPUT_JSON; - argv[argc-1] = NULL; - argc--; - } else if (strcmp(argv[argc - 1], "default") == 0) { - argc--; - } - if (argc == 3) { t = nftnl_table_alloc(); if (t == NULL) { diff --git a/examples/nft-table-parse-add.c b/examples/nft-table-parse-add.c deleted file mode 100644 index 55dbc132ecea..000000000000 --- a/examples/nft-table-parse-add.c +++ /dev/null @@ -1,157 +0,0 @@ -/* - * (C) 2013 by Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> - * (C) 2013 by Arturo Borrero Gonzalez <arturo@xxxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This code has been sponsored by Sophos Astaro <http://www.sophos.com> - */ - -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <netinet/in.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> - -#include <linux/netfilter/nf_tables.h> - -#include <libmnl/libmnl.h> -#include <libnftnl/table.h> -#include <libnftnl/common.h> - -static struct nftnl_table *table_parse_file(const char *file, uint16_t format) -{ - int fd; - struct nftnl_table *t; - struct nftnl_parse_err *err; - char data[4096]; - - t = nftnl_table_alloc(); - if (t == NULL) { - perror("OOM"); - return NULL; - } - - fd = open(file, O_RDONLY); - if (fd < 0) { - perror("open"); - return NULL; - } - - if (read(fd, data, sizeof(data)) < 0) { - perror("read"); - close(fd); - return NULL; - } - close(fd); - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - return NULL; - } - - if (nftnl_table_parse(t, format, data, err) < 0) { - nftnl_parse_perror("Unable to parse file", err); - nftnl_parse_err_free(err); - return NULL; - } - - nftnl_parse_err_free(err); - return t; - -} - -int main(int argc, char *argv[]) -{ - struct mnl_socket *nl; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - uint32_t portid, seq, table_seq; - struct nftnl_table *t = NULL; - uint16_t family, format, outformat; - struct mnl_nlmsg_batch *batch; - int ret; - - if (argc < 3) { - printf("Usage: %s {json} <file>\n", argv[0]); - exit(EXIT_FAILURE); - } - - if (strcmp(argv[1], "json") == 0) { - format = NFTNL_PARSE_JSON; - outformat = NFTNL_OUTPUT_JSON; - } else { - printf("Unknown format: only json is supported\n"); - exit(EXIT_FAILURE); - } - - t = table_parse_file(argv[2], format); - if (t == NULL) - exit(EXIT_FAILURE); - - nftnl_table_fprintf(stdout, t, outformat, 0); - fprintf(stdout, "\n"); - - seq = time(NULL); - - batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); - - nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - family = nftnl_table_get_u32(t, NFTNL_TABLE_FAMILY); - - table_seq = seq; - nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), - NFT_MSG_NEWTABLE, family, - NLM_F_CREATE|NLM_F_ACK, seq++); - nftnl_table_nlmsg_build_payload(nlh, t); - nftnl_table_free(t); - mnl_nlmsg_batch_next(batch); - - nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); - mnl_nlmsg_batch_next(batch); - - nl = mnl_socket_open(NETLINK_NETFILTER); - if (nl == NULL) { - perror("mnl_socket_open"); - exit(EXIT_FAILURE); - } - - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { - perror("mnl_socket_bind"); - exit(EXIT_FAILURE); - } - portid = mnl_socket_get_portid(nl); - - if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), - mnl_nlmsg_batch_size(batch)) < 0) { - perror("mnl_socket_send"); - exit(EXIT_FAILURE); - } - - mnl_nlmsg_batch_stop(batch); - - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - while (ret > 0) { - ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL); - if (ret <= 0) - break; - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); - } - if (ret == -1) { - perror("error"); - exit(EXIT_FAILURE); - } - - mnl_socket_close(nl); - - return EXIT_SUCCESS; -} diff --git a/include/Makefile.am b/include/Makefile.am index fd4cb408a44a..01667f57a7b1 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -10,7 +10,6 @@ noinst_HEADERS = internal.h \ set.h \ common.h \ expr.h \ - json.h \ set_elem.h \ udata.h \ utils.h diff --git a/include/common.h b/include/common.h index 425bbf5f8dd3..d05a81ad542c 100644 --- a/include/common.h +++ b/include/common.h @@ -18,15 +18,4 @@ enum nftnl_parse_input { NFTNL_PARSE_FILE, }; -#include <stdio.h> - -int nftnl_cmd_header_snprintf(char *buf, size_t bufsize, uint32_t cmd, - uint32_t format, uint32_t flags); -int nftnl_cmd_header_fprintf(FILE *fp, uint32_t cmd, uint32_t format, - uint32_t flags); -int nftnl_cmd_footer_snprintf(char *buf, size_t bufsize, uint32_t cmd, - uint32_t format, uint32_t flags); -int nftnl_cmd_footer_fprintf(FILE *fp, uint32_t cmd, uint32_t format, - uint32_t flags); - #endif diff --git a/include/expr_ops.h b/include/expr_ops.h index e63939039c38..00548b355841 100644 --- a/include/expr_ops.h +++ b/include/expr_ops.h @@ -19,8 +19,6 @@ struct expr_ops { int (*parse)(struct nftnl_expr *e, struct nlattr *attr); void (*build)(struct nlmsghdr *nlh, const struct nftnl_expr *e); int (*snprintf)(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e); - int (*json_parse)(struct nftnl_expr *e, json_t *data, - struct nftnl_parse_err *err); }; struct expr_ops *nftnl_expr_ops_lookup(const char *name); diff --git a/include/internal.h b/include/internal.h index 7e97c4a8a2e4..b762a4a54261 100644 --- a/include/internal.h +++ b/include/internal.h @@ -6,7 +6,6 @@ #include "linux_list.h" #include "utils.h" #include "common.h" -#include "json.h" #include "linux_list.h" #include "set.h" #include "set_elem.h" diff --git a/include/json.h b/include/json.h deleted file mode 100644 index bd70cecd6658..000000000000 --- a/include/json.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef LIBNFTNL_JSON_INTERNAL_H -#define LIBNFTNL_JSON_INTERNAL_H - -#ifdef JSON_PARSING -#include <jansson.h> -#include <stdbool.h> -#include "common.h" - -struct nftnl_table; -struct nftnl_chain; -struct nftnl_rule; -struct nftnl_set; -struct nftnl_set_elem; -struct nftnl_set_list; -union nftnl_data_reg; - -int nftnl_jansson_parse_val(json_t *root, const char *node_name, int type, - void *out, struct nftnl_parse_err *err); -const char *nftnl_jansson_parse_str(json_t *root, const char *node_name, - struct nftnl_parse_err *err); -bool nftnl_jansson_node_exist(json_t *root, const char *node_name); -json_t *nftnl_jansson_create_root(const void *json, json_error_t *error, - struct nftnl_parse_err *err, enum nftnl_parse_input input); -json_t *nftnl_jansson_get_node(json_t *root, const char *node_name, - struct nftnl_parse_err *err); -void nftnl_jansson_free_root(json_t *root); -int nftnl_jansson_parse_family(json_t *root, void *out, struct nftnl_parse_err *err); -int nftnl_jansson_str2num(json_t *root, const char *node_name, int base, void *out, - enum nftnl_type type, struct nftnl_parse_err *err); -int nftnl_jansson_parse_reg(json_t *root, const char *node_name, int type, - void *out, struct nftnl_parse_err *err); -struct nftnl_expr *nftnl_jansson_expr_parse(json_t *root, - struct nftnl_parse_err *err, - struct nftnl_set_list *set_list); -int nftnl_jansson_data_reg_parse(json_t *root, const char *node_name, - union nftnl_data_reg *data_reg, - struct nftnl_parse_err *err); -int nftnl_jansson_set_elem_parse(struct nftnl_set_elem *e, json_t *root, - struct nftnl_parse_err *err); -int nftnl_jansson_parse_table(struct nftnl_table *t, json_t *tree, - struct nftnl_parse_err *err); -int nftnl_jansson_parse_chain(struct nftnl_chain *c, json_t *tree, - struct nftnl_parse_err *err); -int nftnl_jansson_parse_rule(struct nftnl_rule *r, json_t *tree, - struct nftnl_parse_err *err, - struct nftnl_set_list *set_list); -int nftnl_jansson_parse_set(struct nftnl_set *s, json_t *tree, - struct nftnl_parse_err *err); -int nftnl_jansson_parse_elem(struct nftnl_set *s, json_t *tree, - struct nftnl_parse_err *err); - -int nftnl_data_reg_json_parse(union nftnl_data_reg *reg, json_t *data, - struct nftnl_parse_err *err); -#else -#define json_t void -#endif - -#endif /* LIBNFTNL_JSON_INTERNAL_H */ diff --git a/include/obj.h b/include/obj.h index 26184da9f46f..36395c719f81 100644 --- a/include/obj.h +++ b/include/obj.h @@ -90,8 +90,6 @@ struct obj_ops { int (*parse)(struct nftnl_obj *e, struct nlattr *attr); void (*build)(struct nlmsghdr *nlh, const struct nftnl_obj *e); int (*snprintf)(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_obj *e); - int (*json_parse)(struct nftnl_obj *e, json_t *data, - struct nftnl_parse_err *err); }; extern struct obj_ops obj_ops_counter; diff --git a/src/Makefile.am b/src/Makefile.am index 05dbeaa4ed92..d5db59633154 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,13 +1,12 @@ include $(top_srcdir)/Make_global.am lib_LTLIBRARIES = libnftnl.la -libnftnl_la_LIBADD = ${LIBMNL_LIBS} ${LIBJSON_LIBS} +libnftnl_la_LIBADD = ${LIBMNL_LIBS} libnftnl_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnftnl.map \ -version-info $(LIBVERSION) libnftnl_la_SOURCES = utils.c \ batch.c \ - buffer.c \ flowtable.c \ common.c \ gen.c \ @@ -19,7 +18,6 @@ libnftnl_la_SOURCES = utils.c \ set.c \ set_elem.c \ ruleset.c \ - jansson.c \ udata.c \ expr.c \ expr_ops.c \ diff --git a/src/buffer.c b/src/buffer.c deleted file mode 100644 index 9ec86af121c9..000000000000 --- a/src/buffer.c +++ /dev/null @@ -1,220 +0,0 @@ -/* - * (C) 2012-2014 by Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - */ - -#include <stdio.h> -#include <stdint.h> -#include <stdarg.h> -#include <inttypes.h> -#include <string.h> -#include <buffer.h> -#include <libnftnl/common.h> -#include <libnftnl/expr.h> -#include "internal.h" - -int nftnl_buf_update(struct nftnl_buf *b, int ret) -{ - if (ret < 0) { - b->fail = true; - } else { - b->off += ret; - if (ret > b->len) - ret = b->len; - b->size += ret; - b->len -= ret; - } - - return ret; -} - -int nftnl_buf_done(struct nftnl_buf *b) -{ - if (b->fail) - return -1; - - /* Remove trailing comma in json */ - if (b->size > 0 && b->buf[b->size - 1] == ',') { - b->off--; - b->size--; - b->len++; - } - - return b->off; -} - -static int nftnl_buf_put(struct nftnl_buf *b, const char *fmt, ...) -{ - int ret; - va_list ap; - - va_start(ap, fmt); - ret = vsnprintf(b->buf + b->off, b->len, fmt, ap); - ret = nftnl_buf_update(b, ret); - va_end(ap); - - return ret; -} - -int nftnl_buf_open(struct nftnl_buf *b, int type, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "{\"%s\":{", tag); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_close(struct nftnl_buf *b, int type, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - /* Remove trailing comma in json */ - if (b->size > 0 && b->buf[b->size - 1] == ',') { - b->off--; - b->size--; - b->len++; - } - - return nftnl_buf_put(b, "}}"); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_open_array(struct nftnl_buf *b, int type, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "{\"%s\":[", tag); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_close_array(struct nftnl_buf *b, int type, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "]}"); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_u32(struct nftnl_buf *b, int type, uint32_t value, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "\"%s\":%u,", tag, value); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_s32(struct nftnl_buf *b, int type, uint32_t value, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "\"%s\":%d,", tag, value); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_u64(struct nftnl_buf *b, int type, uint64_t value, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "\"%s\":%"PRIu64",", tag, value); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_str(struct nftnl_buf *b, int type, const char *str, const char *tag) -{ - switch (type) { - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "\"%s\":\"%s\",", tag, str); - case NFTNL_OUTPUT_XML: - default: - return 0; - } -} - -int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg, - int reg_type, const char *tag) -{ - int ret; - - switch (type) { - case NFTNL_OUTPUT_XML: - return 0; - case NFTNL_OUTPUT_JSON: - nftnl_buf_put(b, "\"%s\":{", tag); - ret = nftnl_data_reg_snprintf(b->buf + b->off, b->len, reg, - NFTNL_OUTPUT_JSON, 0, reg_type); - nftnl_buf_update(b, ret); - return nftnl_buf_put(b, "},"); - } - return 0; -} - -int nftnl_buf_expr_open(struct nftnl_buf *b, int type) -{ - switch (type) { - case NFTNL_OUTPUT_XML: - return 0; - case NFTNL_OUTPUT_JSON: - return nftnl_buf_put(b, "\"expr\":["); - } - return 0; -} - -int nftnl_buf_expr_close(struct nftnl_buf *b, int type) -{ - switch (type) { - case NFTNL_OUTPUT_XML: - return 0; - case NFTNL_OUTPUT_JSON: - nftnl_buf_done(b); - return nftnl_buf_put(b, "]"); - } - return 0; -} - -int nftnl_buf_expr(struct nftnl_buf *b, int type, uint32_t flags, - struct nftnl_expr *expr) -{ - int ret; - - switch (type) { - case NFTNL_OUTPUT_XML: - return 0; - case NFTNL_OUTPUT_JSON: - nftnl_buf_put(b, "{"); - nftnl_buf_str(b, type, expr->ops->name, TYPE); - ret = nftnl_expr_snprintf(b->buf + b->off, b->len, expr, type, - flags); - if (ret > 0) - nftnl_buf_update(b, ret); - else - nftnl_buf_done(b); - - return nftnl_buf_put(b, "},"); - } - return 0; -} diff --git a/src/chain.c b/src/chain.c index eff518680873..06252b66a018 100644 --- a/src/chain.c +++ b/src/chain.c @@ -603,188 +603,6 @@ static inline int nftnl_str2hooknum(int family, const char *hook) return -1; } -#ifdef JSON_PARSING -int nftnl_jansson_parse_chain(struct nftnl_chain *c, json_t *tree, - struct nftnl_parse_err *err) -{ - json_t *root; - uint64_t handle, bytes, packets; - int policy_num; - int32_t family, prio, hooknum, use; - const char *name, *table, *type, *hooknum_str, *policy, *dev; - - root = nftnl_jansson_get_node(tree, "chain", err); - if (root == NULL) - return -1; - - name = nftnl_jansson_parse_str(root, "name", err); - if (name != NULL) - nftnl_chain_set_str(c, NFTNL_CHAIN_NAME, name); - - if (nftnl_jansson_parse_val(root, "handle", NFTNL_TYPE_U64, &handle, - err) == 0) - nftnl_chain_set_u64(c,NFTNL_CHAIN_HANDLE, handle); - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_chain_set_u64(c, NFTNL_CHAIN_BYTES, bytes); - - if (nftnl_jansson_parse_val(root, "packets", NFTNL_TYPE_U64, &packets, - err) == 0) - nftnl_chain_set_u64(c, NFTNL_CHAIN_PACKETS, packets); - - if (nftnl_jansson_parse_family(root, &family, err) == 0) - nftnl_chain_set_u32(c, NFTNL_CHAIN_FAMILY, family); - - table = nftnl_jansson_parse_str(root, "table", err); - - if (table != NULL) - nftnl_chain_set_str(c, NFTNL_CHAIN_TABLE, table); - - if (nftnl_jansson_parse_val(root, "use", NFTNL_TYPE_U32, &use, err) == 0) - nftnl_chain_set_u32(c, NFTNL_CHAIN_USE, use); - - if (nftnl_jansson_node_exist(root, "hooknum")) { - type = nftnl_jansson_parse_str(root, "type", err); - - if (type != NULL) - nftnl_chain_set_str(c, NFTNL_CHAIN_TYPE, type); - - if (nftnl_jansson_parse_val(root, "prio", NFTNL_TYPE_S32, - &prio, err) == 0) - nftnl_chain_set_s32(c, NFTNL_CHAIN_PRIO, prio); - - hooknum_str = nftnl_jansson_parse_str(root, "hooknum", err); - if (hooknum_str != NULL) { - hooknum = nftnl_str2hooknum(c->family, hooknum_str); - if (hooknum == -1) - return -1; - nftnl_chain_set_u32(c, NFTNL_CHAIN_HOOKNUM, - hooknum); - } - - policy = nftnl_jansson_parse_str(root, "policy", err); - if (policy != NULL) { - if (nftnl_str2verdict(policy, &policy_num) != 0) { - errno = EINVAL; - err->node_name = "policy"; - err->error = NFTNL_PARSE_EBADTYPE; - return -1; - } - nftnl_chain_set_u32(c, NFTNL_CHAIN_POLICY, - policy_num); - } - - dev = nftnl_jansson_parse_str(root, "device", err); - if (dev != NULL) - nftnl_chain_set_str(c, NFTNL_CHAIN_DEV, dev); - } - - return 0; -} -#endif - -static int nftnl_chain_json_parse(struct nftnl_chain *c, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_chain(c, tree, err); - - nftnl_jansson_free_root(tree); - - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_chain_do_parse(struct nftnl_chain *c, enum nftnl_parse_type type, - const void *data, struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ - int ret; - struct nftnl_parse_err perr = {}; - - switch (type) { - case NFTNL_PARSE_JSON: - ret = nftnl_chain_json_parse(c, data, &perr, input); - break; - case NFTNL_PARSE_XML: - default: - ret = -1; - errno = EOPNOTSUPP; - break; - } - - if (err != NULL) - *err = perr; - - return ret; -} - -EXPORT_SYMBOL(nftnl_chain_parse); -int nftnl_chain_parse(struct nftnl_chain *c, enum nftnl_parse_type type, - const char *data, struct nftnl_parse_err *err) -{ - return nftnl_chain_do_parse(c, type, data, err, NFTNL_PARSE_BUFFER); -} - -EXPORT_SYMBOL(nftnl_chain_parse_file); -int nftnl_chain_parse_file(struct nftnl_chain *c, enum nftnl_parse_type type, - FILE *fp, struct nftnl_parse_err *err) -{ - return nftnl_chain_do_parse(c, type, fp, err, NFTNL_PARSE_FILE); -} - -static int nftnl_chain_export(char *buf, size_t size, - const struct nftnl_chain *c, int type) -{ - NFTNL_BUF_INIT(b, buf, size); - - nftnl_buf_open(&b, type, CHAIN); - if (c->flags & (1 << NFTNL_CHAIN_NAME)) - nftnl_buf_str(&b, type, c->name, NAME); - if (c->flags & (1 << NFTNL_CHAIN_HANDLE)) - nftnl_buf_u64(&b, type, c->handle, HANDLE); - if (c->flags & (1 << NFTNL_CHAIN_BYTES)) - nftnl_buf_u64(&b, type, c->bytes, BYTES); - if (c->flags & (1 << NFTNL_CHAIN_PACKETS)) - nftnl_buf_u64(&b, type, c->packets, PACKETS); - if (c->flags & (1 << NFTNL_CHAIN_TABLE)) - nftnl_buf_str(&b, type, c->table, TABLE); - if (c->flags & (1 << NFTNL_CHAIN_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(c->family), FAMILY); - if (c->flags & (1 << NFTNL_CHAIN_USE)) - nftnl_buf_u32(&b, type, c->use, USE); - if (c->flags & (1 << NFTNL_CHAIN_HOOKNUM)) { - if (c->flags & (1 << NFTNL_CHAIN_TYPE)) - nftnl_buf_str(&b, type, c->type, TYPE); - if (c->flags & (1 << NFTNL_CHAIN_HOOKNUM)) - nftnl_buf_str(&b, type, nftnl_hooknum2str(c->family, - c->hooknum), HOOKNUM); - if (c->flags & (1 << NFTNL_CHAIN_PRIO)) - nftnl_buf_s32(&b, type, c->prio, PRIO); - if (c->flags & (1 << NFTNL_CHAIN_POLICY)) - nftnl_buf_str(&b, type, nftnl_verdict2str(c->policy), POLICY); - if (c->flags & (1 << NFTNL_CHAIN_DEV)) - nftnl_buf_str(&b, type, c->dev, DEVICE); - } - - nftnl_buf_close(&b, type, CHAIN); - - return nftnl_buf_done(&b); -} - static int nftnl_chain_snprintf_default(char *buf, size_t size, const struct nftnl_chain *c) { @@ -827,26 +645,17 @@ static int nftnl_chain_cmd_snprintf(char *buf, size_t size, { int ret, remain = size, offset = 0; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch (type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_chain_snprintf_default(buf + offset, remain, c); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - ret = nftnl_chain_export(buf + offset, remain, c, type); - break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/common.c b/src/common.c index 561c95439114..feb13b2aa836 100644 --- a/src/common.c +++ b/src/common.c @@ -91,80 +91,6 @@ int nftnl_parse_perror(const char *msg, struct nftnl_parse_err *err) } } -int nftnl_cmd_header_snprintf(char *buf, size_t size, uint32_t cmd, uint32_t type, - uint32_t flags) -{ - NFTNL_BUF_INIT(b, buf, size); - - if (cmd == NFTNL_CMD_UNSPEC) - return 0; - - switch (type) { - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - nftnl_buf_open_array(&b, type, nftnl_cmd2tag(cmd)); - break; - default: - switch (cmd) { - case NFTNL_CMD_ADD: - return snprintf(buf, size, "%9s", "[ADD] "); - case NFTNL_CMD_DELETE: - return snprintf(buf, size, "%9s", "[DELETE] "); - default: - return snprintf(buf, size, "%9s", "[unknown] "); - } - break; - } - return nftnl_buf_done(&b); -} - -static int nftnl_cmd_header_fprintf_cb(char *buf, size_t size, const void *obj, - uint32_t cmd, uint32_t type, - uint32_t flags) -{ - return nftnl_cmd_header_snprintf(buf, size, cmd, type, flags); -} - -int nftnl_cmd_header_fprintf(FILE *fp, uint32_t cmd, uint32_t type, - uint32_t flags) -{ - return nftnl_fprintf(fp, NULL, cmd, type, flags, - nftnl_cmd_header_fprintf_cb); -} - -int nftnl_cmd_footer_snprintf(char *buf, size_t size, uint32_t cmd, uint32_t type, - uint32_t flags) -{ - NFTNL_BUF_INIT(b, buf, size); - - if (cmd == NFTNL_CMD_UNSPEC) - return 0; - - switch (type) { - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - nftnl_buf_close_array(&b, type, nftnl_cmd2tag(cmd)); - break; - default: - return 0; - } - return nftnl_buf_done(&b); -} - -static int nftnl_cmd_footer_fprintf_cb(char *buf, size_t size, const void *obj, - uint32_t cmd, uint32_t type, - uint32_t flags) -{ - return nftnl_cmd_footer_snprintf(buf, size, cmd, type, flags); -} - -int nftnl_cmd_footer_fprintf(FILE *fp, uint32_t cmd, uint32_t type, - uint32_t flags) -{ - return nftnl_fprintf(fp, NULL, cmd, type, flags, - nftnl_cmd_footer_fprintf_cb); -} - EXPORT_SYMBOL(nftnl_batch_begin); struct nlmsghdr *nftnl_batch_begin(char *buf, uint32_t seq) { diff --git a/src/expr/bitwise.c b/src/expr/bitwise.c index a89734b5f2d4..ebfbcd077237 100644 --- a/src/expr/bitwise.c +++ b/src/expr/bitwise.c @@ -174,61 +174,6 @@ nftnl_expr_bitwise_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int -nftnl_expr_bitwise_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e); - uint32_t reg, len; - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_SREG, reg); - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_DREG, reg); - - if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, &len, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_LEN, len); - - if (nftnl_jansson_data_reg_parse(root, "mask", &bitwise->mask, - err) == DATA_VALUE) - e->flags |= (1 << NFTNL_EXPR_BITWISE_MASK); - - if (nftnl_jansson_data_reg_parse(root, "xor", &bitwise->xor, - err) == DATA_VALUE) - e->flags |= (1 << NFTNL_EXPR_BITWISE_XOR); - - if (bitwise->mask.len != bitwise->xor.len) - return -1; - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_bitwise_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_BITWISE_SREG)) - nftnl_buf_u32(&b, type, bitwise->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_BITWISE_DREG)) - nftnl_buf_u32(&b, type, bitwise->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_BITWISE_LEN)) - nftnl_buf_u32(&b, type, bitwise->len, LEN); - if (e->flags & (1 << NFTNL_EXPR_BITWISE_MASK)) - nftnl_buf_reg(&b, type, &bitwise->mask, DATA_VALUE, MASK); - if (e->flags & (1 << NFTNL_EXPR_BITWISE_XOR)) - nftnl_buf_reg(&b, type, &bitwise->xor, DATA_VALUE, XOR); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -260,9 +205,6 @@ nftnl_expr_bitwise_snprintf(char *buf, size_t size, uint32_t type, switch (type) { case NFTNL_OUTPUT_DEFAULT: return nftnl_expr_bitwise_snprintf_default(buf, size, e); - case NFTNL_OUTPUT_XML: - case NFTNL_OUTPUT_JSON: - return nftnl_expr_bitwise_export(buf, size, e, type); default: break; } @@ -300,5 +242,4 @@ struct expr_ops expr_ops_bitwise = { .parse = nftnl_expr_bitwise_parse, .build = nftnl_expr_bitwise_build, .snprintf = nftnl_expr_bitwise_snprintf, - .json_parse = nftnl_expr_bitwise_json_parse, }; diff --git a/src/expr/byteorder.c b/src/expr/byteorder.c index 47c04cfea69f..4cddd4a052c2 100644 --- a/src/expr/byteorder.c +++ b/src/expr/byteorder.c @@ -197,63 +197,6 @@ static inline int nftnl_str2ntoh(const char *op) } } -static int -nftnl_expr_byteorder_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *op; - uint32_t sreg, dreg, len, size; - int ntoh; - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &sreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BYTEORDER_SREG, sreg); - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &dreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BYTEORDER_DREG, dreg); - - op = nftnl_jansson_parse_str(root, "op", err); - if (op != NULL) { - ntoh = nftnl_str2ntoh(op); - if (ntoh < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_BYTEORDER_OP, ntoh); - } - - if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, &len, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BYTEORDER_LEN, len); - - if (nftnl_jansson_parse_val(root, "size", NFTNL_TYPE_U32, &size, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_BYTEORDER_SIZE, size); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_byteorder_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_SREG)) - nftnl_buf_u32(&b, type, byteorder->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_DREG)) - nftnl_buf_u32(&b, type, byteorder->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_OP)) - nftnl_buf_str(&b, type, bo2str(byteorder->op), OP); - if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_LEN)) - nftnl_buf_u32(&b, type, byteorder->len, LEN); - if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_SIZE)) - nftnl_buf_u32(&b, type, byteorder->size, SIZE); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_byteorder_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -277,7 +220,6 @@ nftnl_expr_byteorder_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_byteorder_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_byteorder_export(buf, size, e, type); default: break; } @@ -315,5 +257,4 @@ struct expr_ops expr_ops_byteorder = { .parse = nftnl_expr_byteorder_parse, .build = nftnl_expr_byteorder_build, .snprintf = nftnl_expr_byteorder_snprintf, - .json_parse = nftnl_expr_byteorder_json_parse, }; diff --git a/src/expr/cmp.c b/src/expr/cmp.c index b26d0eb08bb2..86708a48410f 100644 --- a/src/expr/cmp.c +++ b/src/expr/cmp.c @@ -176,55 +176,6 @@ static inline int nftnl_str2cmp(const char *op) } } -static int nftnl_expr_cmp_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - struct nftnl_expr_cmp *cmp = nftnl_expr_data(e); - const char *op; - uint32_t uval32; - int base; - - if (nftnl_jansson_parse_val(root, "sreg", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_CMP_SREG, uval32); - - op = nftnl_jansson_parse_str(root, "op", err); - if (op != NULL) { - base = nftnl_str2cmp(op); - if (base < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_CMP_OP, base); - } - - if (nftnl_jansson_data_reg_parse(root, "data", - &cmp->data, err) == DATA_VALUE) - e->flags |= (1 << NFTNL_EXPR_CMP_DATA); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_cmp_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_cmp *cmp = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_CMP_SREG)) - nftnl_buf_u32(&b, type, cmp->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_CMP_OP)) - nftnl_buf_str(&b, type, cmp2str(cmp->op), OP); - if (e->flags & (1 << NFTNL_EXPR_CMP_DATA)) - nftnl_buf_reg(&b, type, &cmp->data, DATA_VALUE, DATA); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_cmp_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -251,7 +202,6 @@ nftnl_expr_cmp_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_cmp_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_cmp_export(buf, size, e, type); default: break; } @@ -285,5 +235,4 @@ struct expr_ops expr_ops_cmp = { .parse = nftnl_expr_cmp_parse, .build = nftnl_expr_cmp_build, .snprintf = nftnl_expr_cmp_snprintf, - .json_parse = nftnl_expr_cmp_json_parse, }; diff --git a/src/expr/connlimit.c b/src/expr/connlimit.c index 60965b5d8329..61fd299e6e31 100644 --- a/src/expr/connlimit.c +++ b/src/expr/connlimit.c @@ -117,42 +117,6 @@ nftnl_expr_connlimit_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_connlimit_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t uval32; - - if (nftnl_jansson_parse_val(root, "count", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_CONNLIMIT_COUNT, uval32); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_CONNLIMIT_FLAGS, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_connlimit_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_connlimit *connlimit = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_CONNLIMIT_COUNT)) - nftnl_buf_u32(&b, type, connlimit->count, COUNT); - if (e->flags & (1 << NFTNL_EXPR_CONNLIMIT_FLAGS)) - nftnl_buf_u32(&b, type, connlimit->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_connlimit_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -171,7 +135,6 @@ static int nftnl_expr_connlimit_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_connlimit_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_connlimit_export(buf, len, e, type); default: break; } @@ -203,5 +166,4 @@ struct expr_ops expr_ops_connlimit = { .parse = nftnl_expr_connlimit_parse, .build = nftnl_expr_connlimit_build, .snprintf = nftnl_expr_connlimit_snprintf, - .json_parse = nftnl_expr_connlimit_json_parse, }; diff --git a/src/expr/counter.c b/src/expr/counter.c index 21901e892471..2036cb0c1502 100644 --- a/src/expr/counter.c +++ b/src/expr/counter.c @@ -115,42 +115,6 @@ nftnl_expr_counter_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_counter_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t uval64; - - if (nftnl_jansson_parse_val(root, "pkts", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_CTR_PACKETS, uval64); - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_CTR_BYTES, uval64); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_counter_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_counter *ctr = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_CTR_PACKETS)) - nftnl_buf_u64(&b, type, ctr->pkts, PKTS); - if (e->flags & (1 << NFTNL_EXPR_CTR_BYTES)) - nftnl_buf_u64(&b, type, ctr->bytes, BYTES); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_counter_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -169,7 +133,6 @@ static int nftnl_expr_counter_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_counter_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_counter_export(buf, len, e, type); default: break; } @@ -201,5 +164,4 @@ struct expr_ops expr_ops_counter = { .parse = nftnl_expr_counter_parse, .build = nftnl_expr_counter_build, .snprintf = nftnl_expr_counter_snprintf, - .json_parse = nftnl_expr_counter_json_parse, }; diff --git a/src/expr/ct.c b/src/expr/ct.c index 39e9be6c5da2..bb7fe8a4bac2 100644 --- a/src/expr/ct.c +++ b/src/expr/ct.c @@ -221,70 +221,6 @@ static inline int str2ctdir(const char *str, uint8_t *ctdir) return -1; } -static int nftnl_expr_ct_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *key_str, *dir_str; - uint32_t reg; - uint8_t dir; - int key; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_CT_DREG, reg); - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_CT_SREG, reg); - - key_str = nftnl_jansson_parse_str(root, "key", err); - if (key_str != NULL) { - key = str2ctkey(key_str); - if (key < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_CT_KEY, key); - } - - dir_str = nftnl_jansson_parse_str(root, "dir", err); - if (dir_str != NULL) { - if (str2ctdir(dir_str, &dir) != 0) { - err->node_name = "dir"; - err->error = NFTNL_PARSE_EBADTYPE; - goto err; - } - nftnl_expr_set_u8(e, NFTNL_EXPR_CT_DIR, dir); - } - - return 0; -err: - errno = EINVAL; - return -1; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - - -static int -nftnl_expr_ct_export(char *buf, size_t size, const struct nftnl_expr *e, - int type) -{ - struct nftnl_expr_ct *ct = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_CT_SREG)) - nftnl_buf_u32(&b, type, ct->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_CT_DREG)) - nftnl_buf_u32(&b, type, ct->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_CT_KEY)) - nftnl_buf_str(&b, type, ctkey2str(ct->key), KEY); - if (e->flags & (1 << NFTNL_EXPR_CT_DIR)) - nftnl_buf_str(&b, type, ctdir2str(ct->dir), DIR); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_ct_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) @@ -322,7 +258,6 @@ nftnl_expr_ct_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_ct_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_ct_export(buf, len, e, type); default: break; } @@ -358,5 +293,4 @@ struct expr_ops expr_ops_ct = { .parse = nftnl_expr_ct_parse, .build = nftnl_expr_ct_build, .snprintf = nftnl_expr_ct_snprintf, - .json_parse = nftnl_expr_ct_json_parse, }; diff --git a/src/expr/data_reg.c b/src/expr/data_reg.c index ad7f4cb28ebd..48a012bb6cbf 100644 --- a/src/expr/data_reg.c +++ b/src/expr/data_reg.c @@ -24,116 +24,6 @@ #include <libnftnl/rule.h> #include "internal.h" -#ifdef JSON_PARSING -static int nftnl_data_reg_verdict_json_parse(union nftnl_data_reg *reg, json_t *data, - struct nftnl_parse_err *err) -{ - int verdict; - const char *verdict_str; - const char *chain; - - verdict_str = nftnl_jansson_parse_str(data, "verdict", err); - if (verdict_str == NULL) - return DATA_NONE; - - if (nftnl_str2verdict(verdict_str, &verdict) != 0) { - err->node_name = "verdict"; - err->error = NFTNL_PARSE_EBADTYPE; - errno = EINVAL; - return -1; - } - - reg->verdict = (uint32_t)verdict; - - if (nftnl_jansson_node_exist(data, "chain")) { - chain = nftnl_jansson_parse_str(data, "chain", err); - if (chain == NULL) - return DATA_NONE; - - reg->chain = strdup(chain); - } - - return DATA_VERDICT; -} - -static int nftnl_data_reg_value_json_parse(union nftnl_data_reg *reg, json_t *data, - struct nftnl_parse_err *err) -{ - char node_name[32] = {}; - int ret, remain = sizeof(node_name), offset = 0, i; - - if (nftnl_jansson_parse_val(data, "len", NFTNL_TYPE_U32, ®->len, err) < 0) - return DATA_NONE; - - for (i = 0; i < div_round_up(reg->len, sizeof(uint32_t)); i++) { - ret = snprintf(node_name, sizeof(node_name), "data%u", i); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - if (nftnl_jansson_str2num(data, node_name, BASE_HEX, - ®->val[i], NFTNL_TYPE_U32, err) != 0) - return DATA_NONE; - } - - return DATA_VALUE; -} - -int nftnl_data_reg_json_parse(union nftnl_data_reg *reg, json_t *data, - struct nftnl_parse_err *err) -{ - - const char *type; - - type = nftnl_jansson_parse_str(data, "type", err); - if (type == NULL) - return -1; - - /* Select what type of parsing is needed */ - if (strcmp(type, "value") == 0) - return nftnl_data_reg_value_json_parse(reg, data, err); - else if (strcmp(type, "verdict") == 0) - return nftnl_data_reg_verdict_json_parse(reg, data, err); - - return DATA_NONE; -} -#endif - -static int -nftnl_data_reg_value_snprintf_json(char *buf, size_t size, - const union nftnl_data_reg *reg, - uint32_t flags) -{ - int remain = size, offset = 0, ret, i, j; - uint32_t utemp; - uint8_t *tmp; - - ret = snprintf(buf, remain, "\"reg\":{\"type\":\"value\","); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = snprintf(buf + offset, remain, "\"len\":%u,", reg->len); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - for (i = 0; i < div_round_up(reg->len, sizeof(uint32_t)); i++) { - ret = snprintf(buf + offset, remain, "\"data%d\":\"0x", i); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - utemp = htonl(reg->val[i]); - tmp = (uint8_t *)&utemp; - - for (j = 0; j<sizeof(uint32_t); j++) { - ret = snprintf(buf + offset, remain, "%.02x", tmp[j]); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - ret = snprintf(buf + offset, remain, "\","); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - offset--; - ret = snprintf(buf + offset, remain, "}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - return offset; -} - static int nftnl_data_reg_value_snprintf_default(char *buf, size_t size, const union nftnl_data_reg *reg, @@ -167,29 +57,6 @@ nftnl_data_reg_verdict_snprintf_def(char *buf, size_t size, return offset; } -static int -nftnl_data_reg_verdict_snprintf_json(char *buf, size_t size, - const union nftnl_data_reg *reg, - uint32_t flags) -{ - int remain = size, offset = 0, ret = 0; - - ret = snprintf(buf, size, "\"reg\":{\"type\":\"verdict\"," - "\"verdict\":\"%s\"", nftnl_verdict2str(reg->verdict)); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - if (reg->chain != NULL) { - ret = snprintf(buf + offset, remain, ",\"chain\":\"%s\"", - reg->chain); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - ret = snprintf(buf + offset, remain, "}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - return offset; -} - int nftnl_data_reg_snprintf(char *buf, size_t size, const union nftnl_data_reg *reg, uint32_t output_format, uint32_t flags, @@ -202,8 +69,6 @@ int nftnl_data_reg_snprintf(char *buf, size_t size, return nftnl_data_reg_value_snprintf_default(buf, size, reg, flags); case NFTNL_OUTPUT_JSON: - return nftnl_data_reg_value_snprintf_json(buf, size, - reg, flags); case NFTNL_OUTPUT_XML: default: break; @@ -216,8 +81,6 @@ int nftnl_data_reg_snprintf(char *buf, size_t size, return nftnl_data_reg_verdict_snprintf_def(buf, size, reg, flags); case NFTNL_OUTPUT_JSON: - return nftnl_data_reg_verdict_snprintf_json(buf, size, - reg, flags); case NFTNL_OUTPUT_XML: default: break; diff --git a/src/expr/dup.c b/src/expr/dup.c index ed8e6208cfab..6fa65988c835 100644 --- a/src/expr/dup.c +++ b/src/expr/dup.c @@ -112,41 +112,6 @@ static int nftnl_expr_dup_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int nftnl_expr_dup_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t sreg_addr, sreg_dev; - int ret; - - ret = nftnl_jansson_parse_val(root, "sreg_addr", NFTNL_TYPE_U32, &sreg_addr, err); - if (ret >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_DUP_SREG_DEV, sreg_addr); - ret = nftnl_jansson_parse_val(root, "sreg_dev", NFTNL_TYPE_U32, &sreg_dev, err); - if (ret >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_DUP_SREG_DEV, sreg_dev); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_dup_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_dup *dup = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_DUP_SREG_ADDR)) - nftnl_buf_u32(&b, type, dup->sreg_addr, "sreg_addr"); - if (e->flags & (1 << NFTNL_EXPR_DUP_SREG_DEV)) - nftnl_buf_u32(&b, type, dup->sreg_addr, "sreg_dev"); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_dup_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e, uint32_t flags) @@ -175,7 +140,6 @@ static int nftnl_expr_dup_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_dup_snprintf_default(buf, len, e, flags); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_dup_export(buf, len, e, type); default: break; } @@ -207,5 +171,4 @@ struct expr_ops expr_ops_dup = { .parse = nftnl_expr_dup_parse, .build = nftnl_expr_dup_build, .snprintf = nftnl_expr_dup_snprintf, - .json_parse = nftnl_expr_dup_json_parse, }; diff --git a/src/expr/dynset.c b/src/expr/dynset.c index 160d0e15c151..3d24ef8da0f8 100644 --- a/src/expr/dynset.c +++ b/src/expr/dynset.c @@ -205,59 +205,6 @@ nftnl_expr_dynset_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int -nftnl_expr_dynset_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *set_name; - uint32_t uval32; - uint64_t uval64; - - set_name = nftnl_jansson_parse_str(root, "set", err); - if (set_name != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_DYNSET_SET_NAME, set_name); - - if (nftnl_jansson_parse_reg(root, "sreg_key", - NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_DYNSET_SREG_KEY, uval32); - - if (nftnl_jansson_parse_reg(root, "sreg_data", - NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_DYNSET_SREG_DATA, uval32); - - if (nftnl_jansson_parse_val(root, "op", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_DYNSET_OP, uval32); - - if (nftnl_jansson_parse_val(root, "timeout", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_DYNSET_TIMEOUT, uval64); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_expr_dynset_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_dynset *dynset = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_DYNSET_SET_NAME)) - nftnl_buf_str(&b, type, dynset->set_name, SET_NAME); - if (e->flags & (1 << NFTNL_EXPR_DYNSET_SREG_KEY)) - nftnl_buf_u32(&b, type, dynset->sreg_key, SREG_KEY); - if (e->flags & (1 << NFTNL_EXPR_DYNSET_SREG_DATA)) - nftnl_buf_u32(&b, type, dynset->sreg_data, SREG_DATA); - - return nftnl_buf_done(&b); -} - static const char *op2str_array[] = { [NFT_DYNSET_OP_ADD] = "add", [NFT_DYNSET_OP_UPDATE] = "update", @@ -319,7 +266,6 @@ nftnl_expr_dynset_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_dynset_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_dynset_export(buf, size, e, type); default: break; } @@ -369,5 +315,4 @@ struct expr_ops expr_ops_dynset = { .parse = nftnl_expr_dynset_parse, .build = nftnl_expr_dynset_build, .snprintf = nftnl_expr_dynset_snprintf, - .json_parse = nftnl_expr_dynset_json_parse, }; diff --git a/src/expr/exthdr.c b/src/expr/exthdr.c index a3518353be09..ec6a855b3f8e 100644 --- a/src/expr/exthdr.c +++ b/src/expr/exthdr.c @@ -205,24 +205,6 @@ static const char *op2str(uint8_t op) } } -static const char *type2str(uint32_t type) -{ - switch (type) { - case IPPROTO_HOPOPTS: - return "hopopts"; - case IPPROTO_ROUTING: - return "routing"; - case IPPROTO_FRAGMENT: - return "fragment"; - case IPPROTO_DSTOPTS: - return "dstopts"; - case IPPROTO_MH: - return "mh"; - default: - return "unknown"; - } -} - static inline int str2exthdr_op(const char* str) { if (!strcmp(str, "tcpopt")) @@ -248,75 +230,6 @@ static inline int str2exthdr_type(const char *str) return -1; } -static int -nftnl_expr_exthdr_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *exthdr_type; - uint32_t uval32; - int type; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_DREG, uval32); - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_SREG, uval32); - - exthdr_type = nftnl_jansson_parse_str(root, "exthdr_type", err); - if (exthdr_type != NULL) { - type = str2exthdr_type(exthdr_type); - if (type < 0) - return -1; - nftnl_expr_set_u8(e, NFTNL_EXPR_EXTHDR_TYPE, type); - } - - if (nftnl_jansson_parse_val(root, "offset", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_OFFSET, uval32); - - if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_LEN, uval32); - - if (nftnl_jansson_parse_val(root, "op", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_OP, uval32); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_EXTHDR_FLAGS, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_exthdr_export(char *buf, size_t len, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_exthdr *exthdr = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, len); - - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_DREG)) - nftnl_buf_u32(&b, type, exthdr->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_SREG)) - nftnl_buf_u32(&b, type, exthdr->dreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_TYPE)) - nftnl_buf_str(&b, type, type2str(exthdr->type), EXTHDR_TYPE); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_OFFSET)) - nftnl_buf_u32(&b, type, exthdr->offset, OFFSET); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_LEN)) - nftnl_buf_u32(&b, type, exthdr->len, LEN); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_OP)) - nftnl_buf_u32(&b, type, exthdr->op, OP); - if (e->flags & (1 << NFTNL_EXPR_EXTHDR_FLAGS)) - nftnl_buf_u32(&b, type, exthdr->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_exthdr_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -344,7 +257,6 @@ nftnl_expr_exthdr_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_exthdr_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_exthdr_export(buf, len, e, type); default: break; } @@ -386,5 +298,4 @@ struct expr_ops expr_ops_exthdr = { .parse = nftnl_expr_exthdr_parse, .build = nftnl_expr_exthdr_build, .snprintf = nftnl_expr_exthdr_snprintf, - .json_parse = nftnl_expr_exthdr_json_parse, }; diff --git a/src/expr/fib.c b/src/expr/fib.c index b922b26681e7..f1eec73a2927 100644 --- a/src/expr/fib.c +++ b/src/expr/fib.c @@ -128,31 +128,6 @@ nftnl_expr_fib_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int nftnl_expr_fib_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t result, flags, dreg; - - if (nftnl_jansson_parse_reg(root, "result", NFTNL_TYPE_U32, - &result, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_RESULT, result); - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, - &dreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_DREG, dreg); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, - &flags, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static const char *fib_type[NFT_FIB_RESULT_MAX + 1] = { [NFT_FIB_RESULT_OIF] = "oif", [NFT_FIB_RESULT_OIFNAME] = "oifname", @@ -215,23 +190,6 @@ nftnl_expr_fib_snprintf_default(char *buf, size_t size, return offset; } -static int nftnl_expr_fib_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_fib *fib = nftnl_expr_data(e); - - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_FIB_RESULT)) - nftnl_buf_u32(&b, type, fib->result, OP); - if (e->flags & (1 << NFTNL_EXPR_FIB_DREG)) - nftnl_buf_u32(&b, type, fib->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_FIB_FLAGS)) - nftnl_buf_u32(&b, type, fib->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_fib_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -241,7 +199,6 @@ nftnl_expr_fib_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_fib_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_fib_export(buf, len, e, type); default: break; } @@ -275,5 +232,4 @@ struct expr_ops expr_ops_fib = { .parse = nftnl_expr_fib_parse, .build = nftnl_expr_fib_build, .snprintf = nftnl_expr_fib_snprintf, - .json_parse = nftnl_expr_fib_json_parse, }; diff --git a/src/expr/flow_offload.c b/src/expr/flow_offload.c index a2001c9ae9db..648099a2d6af 100644 --- a/src/expr/flow_offload.c +++ b/src/expr/flow_offload.c @@ -92,36 +92,6 @@ static int nftnl_expr_flow_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int -nftnl_expr_flow_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *table_name; - - table_name = nftnl_jansson_parse_str(root, "flowtable", err); - if (table_name != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_FLOW_TABLE_NAME, table_name); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_flow_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_flow *l = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME)) - nftnl_buf_str(&b, type, l->table_name, SET); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_flow_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -142,7 +112,6 @@ static int nftnl_expr_flow_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_flow_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_flow_export(buf, size, e, type); default: break; } @@ -180,5 +149,4 @@ struct expr_ops expr_ops_flow = { .parse = nftnl_expr_flow_parse, .build = nftnl_expr_flow_build, .snprintf = nftnl_expr_flow_snprintf, - .json_parse = nftnl_expr_flow_json_parse, }; diff --git a/src/expr/fwd.c b/src/expr/fwd.c index 90216066cb07..9890e9aca546 100644 --- a/src/expr/fwd.c +++ b/src/expr/fwd.c @@ -126,46 +126,6 @@ static int nftnl_expr_fwd_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int nftnl_expr_fwd_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t u32val; - int ret; - - ret = nftnl_jansson_parse_val(root, "sreg_dev", NFTNL_TYPE_U32, &u32val, err); - if (ret >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FWD_SREG_DEV, u32val); - ret = nftnl_jansson_parse_val(root, "sreg_addr", NFTNL_TYPE_U32, &u32val, err); - if (ret >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FWD_SREG_ADDR, u32val); - ret = nftnl_jansson_parse_val(root, "nfproto", NFTNL_TYPE_U32, &u32val, err); - if (ret >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_FWD_NFPROTO, u32val); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_fwd_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV)) - nftnl_buf_u32(&b, type, fwd->sreg_dev, "sreg_dev"); - if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_ADDR)) - nftnl_buf_u32(&b, type, fwd->sreg_dev, "sreg_addr"); - if (e->flags & (1 << NFTNL_EXPR_FWD_NFPROTO)) - nftnl_buf_u32(&b, type, fwd->nfproto, "nfproto"); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_fwd_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e, uint32_t flags) @@ -200,7 +160,6 @@ static int nftnl_expr_fwd_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_fwd_snprintf_default(buf, len, e, flags); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_fwd_export(buf, len, e, type); default: break; } @@ -234,5 +193,4 @@ struct expr_ops expr_ops_fwd = { .parse = nftnl_expr_fwd_parse, .build = nftnl_expr_fwd_build, .snprintf = nftnl_expr_fwd_snprintf, - .json_parse = nftnl_expr_fwd_json_parse, }; diff --git a/src/expr/hash.c b/src/expr/hash.c index 415537ea721b..280ea0c41e75 100644 --- a/src/expr/hash.c +++ b/src/expr/hash.c @@ -220,47 +220,6 @@ nftnl_expr_hash_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int nftnl_expr_hash_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t sreg, dreg, len, modulus, seed, offset, type; - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, - &sreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_SREG, sreg); - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, - &dreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_DREG, dreg); - - if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, - &len, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_LEN, len); - - if (nftnl_jansson_parse_val(root, "modulus", NFTNL_TYPE_U32, - &modulus, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_MODULUS, modulus); - - if (nftnl_jansson_parse_val(root, "seed", NFTNL_TYPE_U32, - &seed, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_SEED, seed); - - if (nftnl_jansson_parse_val(root, "offset", NFTNL_TYPE_U32, - &offset, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_OFFSET, offset); - - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, - &type, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_TYPE, type); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_hash_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) @@ -302,33 +261,6 @@ nftnl_expr_hash_snprintf_default(char *buf, size_t size, return offset; } -static int nftnl_expr_hash_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_hash *hash = nftnl_expr_data(e); - - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_HASH_SREG)) - nftnl_buf_u32(&b, type, hash->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_HASH_DREG)) - nftnl_buf_u32(&b, type, hash->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_HASH_LEN)) - nftnl_buf_u32(&b, type, hash->len, LEN); - if (e->flags & (1 << NFTNL_EXPR_HASH_MODULUS)) - nftnl_buf_u32(&b, type, hash->modulus, MODULUS); - if (e->flags & (1 << NFTNL_EXPR_HASH_SEED)) - nftnl_buf_u32(&b, type, hash->seed, SEED); - if (e->flags & (1 << NFTNL_EXPR_HASH_OFFSET)) - nftnl_buf_u32(&b, type, hash->offset, OFFSET); - if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE)) - nftnl_buf_u32(&b, type, hash->type, TYPE); - if (e->flags & (1 << NFTNL_EXPR_HASH_SET_NAME)) - nftnl_buf_str(&b, type, hash->map.name, SET); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_hash_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -338,7 +270,6 @@ nftnl_expr_hash_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_hash_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_hash_export(buf, len, e, type); default: break; } @@ -384,5 +315,4 @@ struct expr_ops expr_ops_hash = { .parse = nftnl_expr_hash_parse, .build = nftnl_expr_hash_build, .snprintf = nftnl_expr_hash_snprintf, - .json_parse = nftnl_expr_hash_json_parse, }; diff --git a/src/expr/immediate.c b/src/expr/immediate.c index b0570bd539a4..22edbc80b7f4 100644 --- a/src/expr/immediate.c +++ b/src/expr/immediate.c @@ -174,61 +174,6 @@ nftnl_expr_immediate_parse(struct nftnl_expr *e, struct nlattr *attr) } static int -nftnl_expr_immediate_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - struct nftnl_expr_immediate *imm = nftnl_expr_data(e); - int datareg_type; - uint32_t reg; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_IMM_DREG, reg); - - datareg_type = nftnl_jansson_data_reg_parse(root, "data", - &imm->data, err); - if (datareg_type >= 0) { - switch (datareg_type) { - case DATA_VALUE: - e->flags |= (1 << NFTNL_EXPR_IMM_DATA); - break; - case DATA_VERDICT: - e->flags |= (1 << NFTNL_EXPR_IMM_VERDICT); - break; - case DATA_CHAIN: - e->flags |= (1 << NFTNL_EXPR_IMM_CHAIN); - break; - default: - return -1; - } - } - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_expr_immediate_export(char *buf, size_t size, const struct nftnl_expr *e, - int type) -{ - struct nftnl_expr_immediate *imm = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_IMM_DREG)) - nftnl_buf_u32(&b, type, imm->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_IMM_DATA)) - nftnl_buf_reg(&b, type, &imm->data, DATA_VALUE, DATA); - if (e->flags & (1 << NFTNL_EXPR_IMM_VERDICT)) - nftnl_buf_reg(&b, type, &imm->data, DATA_VERDICT, DATA); - if (e->flags & (1 << NFTNL_EXPR_IMM_CHAIN)) - nftnl_buf_reg(&b, type, &imm->data, DATA_CHAIN, DATA); - - return nftnl_buf_done(&b); -} - -static int nftnl_expr_immediate_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e, uint32_t flags) @@ -267,7 +212,6 @@ nftnl_expr_immediate_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_immediate_snprintf_default(buf, len, e, flags); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_immediate_export(buf, len, e, type); default: break; } @@ -317,5 +261,4 @@ struct expr_ops expr_ops_immediate = { .parse = nftnl_expr_immediate_parse, .build = nftnl_expr_immediate_build, .snprintf = nftnl_expr_immediate_snprintf, - .json_parse = nftnl_expr_immediate_json_parse, }; diff --git a/src/expr/limit.c b/src/expr/limit.c index 856ab1839b0a..4831abd4bdfc 100644 --- a/src/expr/limit.c +++ b/src/expr/limit.c @@ -160,32 +160,6 @@ nftnl_expr_limit_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int nftnl_expr_limit_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t uval64; - uint32_t uval32; - - if (nftnl_jansson_parse_val(root, "rate", NFTNL_TYPE_U64, &uval64, err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_RATE, uval64); - - if (nftnl_jansson_parse_val(root, "unit", NFTNL_TYPE_U64, &uval64, err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_UNIT, uval64); - if (nftnl_jansson_parse_val(root, "burst", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LIMIT_BURST, uval32); - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LIMIT_TYPE, uval32); - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LIMIT_FLAGS, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static const char *get_unit(uint64_t u) { switch (u) { @@ -198,26 +172,6 @@ static const char *get_unit(uint64_t u) return "error"; } -static int nftnl_expr_limit_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_limit *limit = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE)) - nftnl_buf_u64(&b, type, limit->rate, RATE); - if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT)) - nftnl_buf_u64(&b, type, limit->unit, UNIT); - if (e->flags & (1 << NFTNL_EXPR_LIMIT_BURST)) - nftnl_buf_u32(&b, type, limit->burst, BURST); - if (e->flags & (1 << NFTNL_EXPR_LIMIT_TYPE)) - nftnl_buf_u32(&b, type, limit->type, TYPE); - if (e->flags & (1 << NFTNL_EXPR_LIMIT_FLAGS)) - nftnl_buf_u32(&b, type, limit->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static const char *limit_to_type(enum nft_limit_type type) { switch (type) { @@ -248,7 +202,6 @@ nftnl_expr_limit_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_limit_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_limit_export(buf, len, e, type); default: break; } @@ -286,5 +239,4 @@ struct expr_ops expr_ops_limit = { .parse = nftnl_expr_limit_parse, .build = nftnl_expr_limit_build, .snprintf = nftnl_expr_limit_snprintf, - .json_parse = nftnl_expr_limit_json_parse, }; diff --git a/src/expr/log.c b/src/expr/log.c index 86d965136cd4..e4bf0516e966 100644 --- a/src/expr/log.c +++ b/src/expr/log.c @@ -186,45 +186,6 @@ nftnl_expr_log_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int nftnl_expr_log_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *prefix; - uint32_t snaplen, level, flags; - uint16_t group, qthreshold; - - prefix = nftnl_jansson_parse_str(root, "prefix", err); - if (prefix != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_LOG_PREFIX, prefix); - - if (nftnl_jansson_parse_val(root, "group", NFTNL_TYPE_U16, &group, - err) == 0) - nftnl_expr_set_u16(e, NFTNL_EXPR_LOG_GROUP, group); - - if (nftnl_jansson_parse_val(root, "snaplen", NFTNL_TYPE_U32, &snaplen, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOG_SNAPLEN, snaplen); - - if (nftnl_jansson_parse_val(root, "qthreshold", NFTNL_TYPE_U16, - &qthreshold, err) == 0) - nftnl_expr_set_u16(e, NFTNL_EXPR_LOG_QTHRESHOLD, qthreshold); - - if (nftnl_jansson_parse_val(root, "level", NFTNL_TYPE_U32, &level, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOG_LEVEL, level); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOG_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_log_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -275,28 +236,6 @@ static int nftnl_expr_log_snprintf_default(char *buf, size_t size, return offset; } -static int nftnl_expr_log_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_log *log = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_LOG_PREFIX)) - nftnl_buf_str(&b, type, log->prefix, PREFIX); - if (e->flags & (1 << NFTNL_EXPR_LOG_GROUP)) - nftnl_buf_u32(&b, type, log->group, GROUP); - if (e->flags & (1 << NFTNL_EXPR_LOG_SNAPLEN)) - nftnl_buf_u32(&b, type, log->snaplen, SNAPLEN); - if (e->flags & (1 << NFTNL_EXPR_LOG_QTHRESHOLD)) - nftnl_buf_u32(&b, type, log->qthreshold, QTHRESH); - if (e->flags & (1 << NFTNL_EXPR_LOG_LEVEL)) - nftnl_buf_u32(&b, type, log->level, LEVEL); - if (e->flags & (1 << NFTNL_EXPR_LOG_FLAGS)) - nftnl_buf_u32(&b, type, log->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_log_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -306,7 +245,6 @@ nftnl_expr_log_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_log_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_log_export(buf, len, e, type); default: break; } @@ -354,5 +292,4 @@ struct expr_ops expr_ops_log = { .parse = nftnl_expr_log_parse, .build = nftnl_expr_log_build, .snprintf = nftnl_expr_log_snprintf, - .json_parse = nftnl_expr_log_json_parse, }; diff --git a/src/expr/lookup.c b/src/expr/lookup.c index 5fcb81f3a7a2..132dc4e66dc6 100644 --- a/src/expr/lookup.c +++ b/src/expr/lookup.c @@ -168,54 +168,6 @@ nftnl_expr_lookup_parse(struct nftnl_expr *e, struct nlattr *attr) } static int -nftnl_expr_lookup_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *set_name; - uint32_t sreg, dreg, flags; - - set_name = nftnl_jansson_parse_str(root, "set", err); - if (set_name != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_LOOKUP_SET, set_name); - - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &sreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_SREG, sreg); - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &dreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_DREG, dreg); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, - &flags, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_expr_lookup_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_lookup *l = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_LOOKUP_SET)) - nftnl_buf_str(&b, type, l->set_name, SET); - if (e->flags & (1 << NFTNL_EXPR_LOOKUP_SREG)) - nftnl_buf_u32(&b, type, l->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_LOOKUP_DREG)) - nftnl_buf_u32(&b, type, l->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_LOOKUP_FLAGS)) - nftnl_buf_u32(&b, type, l->flags, FLAGS); - - return nftnl_buf_done(&b); -} - -static int nftnl_expr_lookup_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -247,7 +199,6 @@ nftnl_expr_lookup_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_lookup_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_lookup_export(buf, size, e, type); default: break; } @@ -293,5 +244,4 @@ struct expr_ops expr_ops_lookup = { .parse = nftnl_expr_lookup_parse, .build = nftnl_expr_lookup_build, .snprintf = nftnl_expr_lookup_snprintf, - .json_parse = nftnl_expr_lookup_json_parse, }; diff --git a/src/expr/masq.c b/src/expr/masq.c index 7c235d3b0bdc..78dd76b1a3b3 100644 --- a/src/expr/masq.c +++ b/src/expr/masq.c @@ -131,46 +131,6 @@ nftnl_expr_masq_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_masq_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t reg, flags; - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_FLAGS, flags); - if (nftnl_jansson_parse_reg(root, "sreg_proto_min", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MIN, reg); - if (nftnl_jansson_parse_reg(root, "sreg_proto_max", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MAX, reg); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_masq_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_masq *masq = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS)) - nftnl_buf_u32(&b, type, masq->flags, FLAGS); - if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) - nftnl_buf_u32(&b, type, masq->sreg_proto_min, SREG_PROTO_MIN); - if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX)) - nftnl_buf_u32(&b, type, masq->sreg_proto_max, SREG_PROTO_MAX); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_masq_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -195,7 +155,6 @@ static int nftnl_expr_masq_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_masq_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_masq_export(buf, len, e, type); default: break; } @@ -229,5 +188,4 @@ struct expr_ops expr_ops_masq = { .parse = nftnl_expr_masq_parse, .build = nftnl_expr_masq_build, .snprintf = nftnl_expr_masq_snprintf, - .json_parse = nftnl_expr_masq_json_parse, }; diff --git a/src/expr/match.c b/src/expr/match.c index dd09e1e85192..85dd79c6a63d 100644 --- a/src/expr/match.c +++ b/src/expr/match.c @@ -164,36 +164,6 @@ static int nftnl_expr_match_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int nftnl_expr_match_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *name; - - name = nftnl_jansson_parse_str(root, "name", err); - if (name != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_MT_NAME, name); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - - -static int nftnl_expr_match_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_match *mt = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_MT_NAME)) - nftnl_buf_str(&b, type, mt->name, NAME); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_match_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -206,7 +176,6 @@ nftnl_expr_match_snprintf(char *buf, size_t len, uint32_t type, match->name, match->rev); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_match_export(buf, len, e, type); default: break; } @@ -250,5 +219,4 @@ struct expr_ops expr_ops_match = { .parse = nftnl_expr_match_parse, .build = nftnl_expr_match_build, .snprintf = nftnl_expr_match_snprintf, - .json_parse = nftnl_expr_match_json_parse, }; diff --git a/src/expr/meta.c b/src/expr/meta.c index de82105de44f..b66b9b702ca2 100644 --- a/src/expr/meta.c +++ b/src/expr/meta.c @@ -182,40 +182,6 @@ static inline int str2meta_key(const char *str) return -1; } -static int nftnl_expr_meta_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *key_str; - uint32_t reg; - int key; - - key_str = nftnl_jansson_parse_str(root, "key", err); - if (key_str != NULL) { - key = str2meta_key(key_str); - if (key >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key); - } - - if (nftnl_jansson_node_exist(root, "dreg")) { - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, reg); - } - - if (nftnl_jansson_node_exist(root, "sreg")) { - if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, ®, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, reg); - } - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_meta_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) @@ -233,22 +199,6 @@ nftnl_expr_meta_snprintf_default(char *buf, size_t len, return 0; } -static int nftnl_expr_meta_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_meta *meta = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_META_DREG)) - nftnl_buf_u32(&b, type, meta->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_META_KEY)) - nftnl_buf_str(&b, type, meta_key2str(meta->key), KEY); - if (e->flags & (1 << NFTNL_EXPR_META_SREG)) - nftnl_buf_u32(&b, type, meta->sreg, SREG); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -258,7 +208,6 @@ nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_meta_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_meta_export(buf, len, e, type); default: break; } @@ -292,5 +241,4 @@ struct expr_ops expr_ops_meta = { .parse = nftnl_expr_meta_parse, .build = nftnl_expr_meta_build, .snprintf = nftnl_expr_meta_snprintf, - .json_parse = nftnl_expr_meta_json_parse, }; diff --git a/src/expr/nat.c b/src/expr/nat.c index 92713033a6e7..9be78f39c31d 100644 --- a/src/expr/nat.c +++ b/src/expr/nat.c @@ -220,85 +220,6 @@ static inline int nftnl_str2nat(const char *nat) } } -static int nftnl_expr_nat_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *nat_type, *family_str; - uint32_t reg, flags; - int val32; - - nat_type = nftnl_jansson_parse_str(root, "nat_type", err); - if (nat_type == NULL) - return -1; - - val32 = nftnl_str2nat(nat_type); - if (val32 < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_TYPE, val32); - - family_str = nftnl_jansson_parse_str(root, "family", err); - if (family_str == NULL) - return -1; - - val32 = nftnl_str2family(family_str); - if (val32 < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_FAMILY, val32); - - if (nftnl_jansson_parse_reg(root, "sreg_addr_min", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_REG_ADDR_MIN, reg); - - if (nftnl_jansson_parse_reg(root, "sreg_addr_max", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_REG_ADDR_MAX, reg); - - if (nftnl_jansson_parse_reg(root, "sreg_proto_min", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_REG_PROTO_MIN, reg); - - if (nftnl_jansson_parse_reg(root, "sreg_proto_max", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_REG_PROTO_MAX, reg); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, - &flags, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NAT_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_nat_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_nat *nat = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_NAT_TYPE)) - nftnl_buf_str(&b, type, nat2str(nat->type), NAT_TYPE); - if (e->flags & (1 << NFTNL_EXPR_NAT_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(nat->family), FAMILY); - if (e->flags & (1 << NFTNL_EXPR_NAT_REG_ADDR_MIN)) - nftnl_buf_u32(&b, type, nat->sreg_addr_min, SREG_ADDR_MIN); - if (e->flags & (1 << NFTNL_EXPR_NAT_REG_ADDR_MAX)) - nftnl_buf_u32(&b, type, nat->sreg_addr_max, SREG_ADDR_MAX); - if (e->flags & (1 << NFTNL_EXPR_NAT_REG_PROTO_MIN)) - nftnl_buf_u32(&b, type, nat->sreg_proto_min, SREG_PROTO_MIN); - if (e->flags & (1 << NFTNL_EXPR_NAT_REG_PROTO_MAX)) - nftnl_buf_u32(&b, type, nat->sreg_proto_max, SREG_PROTO_MAX); - if (e->flags & (1 << NFTNL_EXPR_NAT_FLAGS)) - nftnl_buf_u32(&b, type, nat->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_nat_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) @@ -344,7 +265,6 @@ nftnl_expr_nat_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_nat_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_nat_export(buf, size, e, type); default: break; } @@ -385,5 +305,4 @@ struct expr_ops expr_ops_nat = { .parse = nftnl_expr_nat_parse, .build = nftnl_expr_nat_build, .snprintf = nftnl_expr_nat_snprintf, - .json_parse = nftnl_expr_nat_json_parse, }; diff --git a/src/expr/numgen.c b/src/expr/numgen.c index 5336fdec1d69..d46cf947464d 100644 --- a/src/expr/numgen.c +++ b/src/expr/numgen.c @@ -179,35 +179,6 @@ nftnl_expr_ng_parse(struct nftnl_expr *e, struct nlattr *attr) return ret; } -static int nftnl_expr_ng_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t dreg, modulus, type, offset; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, - &dreg, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NG_DREG, dreg); - - if (nftnl_jansson_parse_val(root, "modulus", NFTNL_TYPE_U32, - &modulus, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NG_MODULUS, modulus); - - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, - &type, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NG_TYPE, type); - - if (nftnl_jansson_parse_val(root, "offset", NFTNL_TYPE_U32, - &offset, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_NG_OFFSET, offset); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_ng_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) @@ -244,27 +215,6 @@ nftnl_expr_ng_snprintf_default(char *buf, size_t size, return offset; } -static int nftnl_expr_ng_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_ng *ng = nftnl_expr_data(e); - - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_NG_DREG)) - nftnl_buf_u32(&b, type, ng->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_NG_MODULUS)) - nftnl_buf_u32(&b, type, ng->modulus, MODULUS); - if (e->flags & (1 << NFTNL_EXPR_NG_TYPE)) - nftnl_buf_u32(&b, type, ng->type, TYPE); - if (e->flags & (1 << NFTNL_EXPR_NG_OFFSET)) - nftnl_buf_u32(&b, type, ng->type, OFFSET); - if (e->flags & (1 << NFTNL_EXPR_NG_SET_NAME)) - nftnl_buf_str(&b, type, ng->map.name, SET); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_ng_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -274,7 +224,6 @@ nftnl_expr_ng_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_ng_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_ng_export(buf, len, e, type); default: break; } @@ -314,5 +263,4 @@ struct expr_ops expr_ops_ng = { .parse = nftnl_expr_ng_parse, .build = nftnl_expr_ng_build, .snprintf = nftnl_expr_ng_snprintf, - .json_parse = nftnl_expr_ng_json_parse, }; diff --git a/src/expr/objref.c b/src/expr/objref.c index 64ee8638027c..67fd74bd8992 100644 --- a/src/expr/objref.c +++ b/src/expr/objref.c @@ -174,51 +174,6 @@ static int nftnl_expr_objref_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_objref_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t uval32; - const char *str; - - if (nftnl_jansson_node_exist(root, "name")) { - str = nftnl_jansson_parse_str(root, "name", err); - if (str == NULL) - return -1; - - nftnl_expr_set_str(e, NFTNL_EXPR_OBJREF_IMM_NAME, str); - } - - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_OBJREF_IMM_TYPE, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_objref_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_objref *objref = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_OBJREF_IMM_TYPE)) - nftnl_buf_u32(&b, type, objref->imm.type, BYTES); - if (e->flags & (1 << NFTNL_EXPR_OBJREF_IMM_NAME)) - nftnl_buf_str(&b, type, objref->imm.name, NAME); - if (e->flags & (1 << NFTNL_EXPR_OBJREF_SET_SREG)) - nftnl_buf_u32(&b, type, objref->set.sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_OBJREF_SET_NAME)) - nftnl_buf_str(&b, type, objref->set.name, SET); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_objref_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -241,7 +196,6 @@ static int nftnl_expr_objref_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_objref_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_objref_export(buf, len, e, type); default: break; } @@ -279,5 +233,4 @@ struct expr_ops expr_ops_objref = { .parse = nftnl_expr_objref_parse, .build = nftnl_expr_objref_build, .snprintf = nftnl_expr_objref_snprintf, - .json_parse = nftnl_expr_objref_json_parse, }; diff --git a/src/expr/osf.c b/src/expr/osf.c index 6fd62e582332..372ebe1a8588 100644 --- a/src/expr/osf.c +++ b/src/expr/osf.c @@ -102,18 +102,6 @@ static int nftnl_expr_osf_snprintf_default(char *buf, size_t size, return offset; } -static int nftnl_expr_osf_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_osf *osf = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_OSF_DREG)) - nftnl_buf_u32(&b, type, osf->dreg, "dreg"); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_osf_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -123,7 +111,6 @@ nftnl_expr_osf_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_osf_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_osf_export(buf, len, e, type); default: break; } diff --git a/src/expr/payload.c b/src/expr/payload.c index 91e1587ff239..4b2f32213c29 100644 --- a/src/expr/payload.c +++ b/src/expr/payload.c @@ -232,59 +232,6 @@ static inline int nftnl_str2base(const char *base) } static int -nftnl_expr_payload_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *base_str; - uint32_t reg, uval32; - int base; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_DREG, reg); - - base_str = nftnl_jansson_parse_str(root, "base", err); - if (base_str != NULL) { - base = nftnl_str2base(base_str); - if (base < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_BASE, base); - } - - if (nftnl_jansson_parse_val(root, "offset", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_OFFSET, uval32); - - if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, &uval32, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_LEN, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_payload_export(char *buf, size_t size, uint32_t flags, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_payload *payload = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_PAYLOAD_DREG)) - nftnl_buf_u32(&b, type, payload->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_PAYLOAD_OFFSET)) - nftnl_buf_u32(&b, type, payload->offset, OFFSET); - if (e->flags & (1 << NFTNL_EXPR_PAYLOAD_LEN)) - nftnl_buf_u32(&b, type, payload->len, LEN); - if (e->flags & (1 << NFTNL_EXPR_PAYLOAD_BASE)) - nftnl_buf_str(&b, type, base2str(payload->base), BASE); - - return nftnl_buf_done(&b); -} - -static int nftnl_expr_payload_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { @@ -305,7 +252,6 @@ nftnl_expr_payload_snprintf(char *buf, size_t len, uint32_t type, payload->offset, payload->dreg); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_payload_export(buf, len, flags, e, type); default: break; } @@ -349,5 +295,4 @@ struct expr_ops expr_ops_payload = { .parse = nftnl_expr_payload_parse, .build = nftnl_expr_payload_build, .snprintf = nftnl_expr_payload_snprintf, - .json_parse = nftnl_expr_payload_json_parse, }; diff --git a/src/expr/queue.c b/src/expr/queue.c index a392a271a6fb..e71df7dafd03 100644 --- a/src/expr/queue.c +++ b/src/expr/queue.c @@ -143,35 +143,6 @@ nftnl_expr_queue_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_queue_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t sreg_qnum; - uint16_t type; - uint16_t code; - - if (nftnl_jansson_parse_val(root, "num", NFTNL_TYPE_U16, &type, err) == 0) - nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_NUM, type); - - if (nftnl_jansson_parse_val(root, "total", NFTNL_TYPE_U16, &code, err) == 0) - nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_TOTAL, code); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U16, &code, err) == 0) - nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_FLAGS, code); - - if (nftnl_jansson_parse_val(root, "sreg_qnum", NFTNL_TYPE_U32, &sreg_qnum, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_QUEUE_SREG_QNUM, sreg_qnum); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_queue_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -213,24 +184,6 @@ static int nftnl_expr_queue_snprintf_default(char *buf, size_t len, return offset; } -static int nftnl_expr_queue_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_queue *queue = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_QUEUE_NUM)) - nftnl_buf_u32(&b, type, queue->queuenum, NUM); - if (e->flags & (1 << NFTNL_EXPR_QUEUE_TOTAL)) - nftnl_buf_u32(&b, type, queue->queues_total, TOTAL); - if (e->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS)) - nftnl_buf_u32(&b, type, queue->flags, FLAGS); - if (e->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM)) - nftnl_buf_u32(&b, type, queue->sreg_qnum, SREG_QNUM); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_queue_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -240,7 +193,6 @@ nftnl_expr_queue_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_queue_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_queue_export(buf, len, e, type); default: break; } @@ -276,5 +228,4 @@ struct expr_ops expr_ops_queue = { .parse = nftnl_expr_queue_parse, .build = nftnl_expr_queue_build, .snprintf = nftnl_expr_queue_snprintf, - .json_parse = nftnl_expr_queue_json_parse, }; diff --git a/src/expr/quota.c b/src/expr/quota.c index 667e6e17c28c..b2808529442a 100644 --- a/src/expr/quota.c +++ b/src/expr/quota.c @@ -128,47 +128,6 @@ nftnl_expr_quota_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_quota_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t bytes, consumed; - uint32_t flags; - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_QUOTA_BYTES, bytes); - if (nftnl_jansson_parse_val(root, "consumed", NFTNL_TYPE_U64, &consumed, - err) == 0) - nftnl_expr_set_u64(e, NFTNL_EXPR_QUOTA_CONSUMED, consumed); - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_QUOTA_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_quota_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_quota *quota = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_QUOTA_BYTES)) - nftnl_buf_u64(&b, type, quota->bytes, BYTES); - if (e->flags & (1 << NFTNL_EXPR_QUOTA_CONSUMED)) - nftnl_buf_u64(&b, type, quota->consumed, CONSUMED); - if (e->flags & (1 << NFTNL_EXPR_QUOTA_FLAGS)) - nftnl_buf_u32(&b, type, quota->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_quota_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -188,7 +147,6 @@ static int nftnl_expr_quota_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_quota_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_quota_export(buf, len, e, type); default: break; } @@ -204,5 +162,4 @@ struct expr_ops expr_ops_quota = { .parse = nftnl_expr_quota_parse, .build = nftnl_expr_quota_build, .snprintf = nftnl_expr_quota_snprintf, - .json_parse = nftnl_expr_quota_json_parse, }; diff --git a/src/expr/range.c b/src/expr/range.c index b2789ffaada3..f5a41acd12e0 100644 --- a/src/expr/range.c +++ b/src/expr/range.c @@ -184,61 +184,6 @@ static inline int nftnl_str2range(const char *op) } } -static int nftnl_expr_range_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - struct nftnl_expr_range *range = nftnl_expr_data(e); - const char *op; - uint32_t uval32; - int base; - - if (nftnl_jansson_parse_val(root, "sreg", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_RANGE_SREG, uval32); - - op = nftnl_jansson_parse_str(root, "op", err); - if (op != NULL) { - base = nftnl_str2range(op); - if (base < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_RANGE_OP, base); - } - - if (nftnl_jansson_data_reg_parse(root, "data_from", - &range->data_from, err) == DATA_VALUE) - e->flags |= (1 << NFTNL_EXPR_RANGE_FROM_DATA); - - if (nftnl_jansson_data_reg_parse(root, "data_to", - &range->data_to, err) == DATA_VALUE) - e->flags |= (1 << NFTNL_EXPR_RANGE_TO_DATA); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_range_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_range *range = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_RANGE_SREG)) - nftnl_buf_u32(&b, type, range->sreg, SREG); - if (e->flags & (1 << NFTNL_EXPR_RANGE_OP)) - nftnl_buf_str(&b, type, range2str(range->op), OP); - if (e->flags & (1 << NFTNL_EXPR_RANGE_FROM_DATA)) - nftnl_buf_reg(&b, type, &range->data_from, DATA_VALUE, DATA); - if (e->flags & (1 << NFTNL_EXPR_RANGE_TO_DATA)) - nftnl_buf_reg(&b, type, &range->data_to, DATA_VALUE, DATA); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_range_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) { @@ -246,7 +191,7 @@ static int nftnl_expr_range_snprintf_default(char *buf, size_t size, int remain = size, offset = 0, ret; ret = snprintf(buf, remain, "%s reg %u ", - expr_range_str[range->op], range->sreg); + range2str(range->op), range->sreg); SNPRINTF_BUFFER_SIZE(ret, remain, offset); ret = nftnl_data_reg_snprintf(buf + offset, remain, &range->data_from, @@ -268,7 +213,6 @@ static int nftnl_expr_range_snprintf(char *buf, size_t size, uint32_t type, return nftnl_expr_range_snprintf_default(buf, size, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_range_export(buf, size, e, type); default: break; } @@ -284,5 +228,4 @@ struct expr_ops expr_ops_range = { .parse = nftnl_expr_range_parse, .build = nftnl_expr_range_build, .snprintf = nftnl_expr_range_snprintf, - .json_parse = nftnl_expr_range_json_parse, }; diff --git a/src/expr/redir.c b/src/expr/redir.c index b2aa3453870a..1ed48969b154 100644 --- a/src/expr/redir.c +++ b/src/expr/redir.c @@ -131,48 +131,6 @@ nftnl_expr_redir_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_redir_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t reg, flags; - - if (nftnl_jansson_parse_reg(root, "sreg_proto_min", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_REDIR_REG_PROTO_MIN, reg); - - if (nftnl_jansson_parse_reg(root, "sreg_proto_max", NFTNL_TYPE_U32, - ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_REDIR_REG_PROTO_MAX, reg); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_REDIR_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_expr_redir_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_redir *redir = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_REDIR_REG_PROTO_MIN)) - nftnl_buf_u32(&b, type, redir->sreg_proto_min, SREG_PROTO_MIN); - if (e->flags & (1 << NFTNL_EXPR_REDIR_REG_PROTO_MAX)) - nftnl_buf_u32(&b, type, redir->sreg_proto_max, SREG_PROTO_MAX); - if (e->flags & (1 << NFTNL_EXPR_REDIR_FLAGS)) - nftnl_buf_u32(&b, type, redir->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_redir_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -209,7 +167,6 @@ nftnl_expr_redir_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_redir_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_redir_export(buf, len, e, type); default: break; } @@ -243,5 +200,4 @@ struct expr_ops expr_ops_redir = { .parse = nftnl_expr_redir_parse, .build = nftnl_expr_redir_build, .snprintf = nftnl_expr_redir_snprintf, - .json_parse = nftnl_expr_redir_json_parse, }; diff --git a/src/expr/reject.c b/src/expr/reject.c index 11d8b20096ae..b26b0cc5711a 100644 --- a/src/expr/reject.c +++ b/src/expr/reject.c @@ -116,27 +116,6 @@ nftnl_expr_reject_parse(struct nftnl_expr *e, struct nlattr *attr) return 0; } -static int -nftnl_expr_reject_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint32_t type; - uint8_t code; - - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &type, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_REJECT_TYPE, type); - - if (nftnl_jansson_parse_val(root, "code", NFTNL_TYPE_U8, &code, err) == 0) - nftnl_expr_set_u8(e, NFTNL_EXPR_REJECT_CODE, code); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_reject_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) { @@ -146,20 +125,6 @@ static int nftnl_expr_reject_snprintf_default(char *buf, size_t len, reject->type, reject->icmp_code); } -static int nftnl_expr_reject_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_reject *reject = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_REJECT_TYPE)) - nftnl_buf_u32(&b, type, reject->type, TYPE); - if (e->flags & (1 << NFTNL_EXPR_REJECT_CODE)) - nftnl_buf_u32(&b, type, reject->icmp_code, CODE); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_reject_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -169,7 +134,6 @@ nftnl_expr_reject_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_reject_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_reject_export(buf, len, e, type); default: break; } @@ -201,5 +165,4 @@ struct expr_ops expr_ops_reject = { .parse = nftnl_expr_reject_parse, .build = nftnl_expr_reject_build, .snprintf = nftnl_expr_reject_snprintf, - .json_parse = nftnl_expr_reject_json_parse, }; diff --git a/src/expr/rt.c b/src/expr/rt.c index 18c3945d45ee..2c0c1cf00b56 100644 --- a/src/expr/rt.c +++ b/src/expr/rt.c @@ -141,34 +141,6 @@ static inline int str2rt_key(const char *str) return -1; } -static int nftnl_expr_rt_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *val_str; - uint32_t reg; - int val32; - - val_str = nftnl_jansson_parse_str(root, "key", err); - if (val_str != NULL) { - val32 = str2rt_key(val_str); - if (val32 >= 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_RT_KEY, val32); - } - - if (nftnl_jansson_node_exist(root, "dreg")) { - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, - err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_RT_DREG, reg); - } - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_expr_rt_snprintf_default(char *buf, size_t len, const struct nftnl_expr *e) @@ -182,20 +154,6 @@ nftnl_expr_rt_snprintf_default(char *buf, size_t len, return 0; } -static int nftnl_expr_rt_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_rt *rt = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_RT_DREG)) - nftnl_buf_u32(&b, type, rt->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_RT_KEY)) - nftnl_buf_str(&b, type, rt_key2str(rt->key), KEY); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_rt_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -205,7 +163,6 @@ nftnl_expr_rt_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_rt_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_rt_export(buf, len, e, type); default: break; } @@ -237,5 +194,4 @@ struct expr_ops expr_ops_rt = { .parse = nftnl_expr_rt_parse, .build = nftnl_expr_rt_build, .snprintf = nftnl_expr_rt_snprintf, - .json_parse = nftnl_expr_rt_json_parse, }; diff --git a/src/expr/socket.c b/src/expr/socket.c index 5fa2ef2ec2f3..704b1d943d24 100644 --- a/src/expr/socket.c +++ b/src/expr/socket.c @@ -151,20 +151,6 @@ nftnl_expr_socket_snprintf_default(char *buf, size_t len, return 0; } -static int nftnl_expr_socket_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_socket *socket = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_SOCKET_DREG)) - nftnl_buf_u32(&b, type, socket->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_SOCKET_KEY)) - nftnl_buf_str(&b, type, socket_key2str(socket->key), KEY); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_socket_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -174,7 +160,6 @@ nftnl_expr_socket_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_socket_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_socket_export(buf, len, e, type); default: break; } diff --git a/src/expr/target.c b/src/expr/target.c index ed4bf7df6328..6ae6c819d2f4 100644 --- a/src/expr/target.c +++ b/src/expr/target.c @@ -165,36 +165,6 @@ static int nftnl_expr_target_parse(struct nftnl_expr *e, struct nlattr *attr) } static int -nftnl_expr_target_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *name; - - name = nftnl_jansson_parse_str(root, "name", err); - if (name != NULL) - nftnl_expr_set_str(e, NFTNL_EXPR_TG_NAME, name); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_rule_exp_target_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_target *target = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_TG_NAME)) - nftnl_buf_str(&b, type, target->name, NAME); - - return nftnl_buf_done(&b); -} - -static int nftnl_expr_target_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { @@ -206,7 +176,6 @@ nftnl_expr_target_snprintf(char *buf, size_t len, uint32_t type, target->name, target->rev); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_rule_exp_target_export(buf, len, e, type); default: break; } @@ -250,5 +219,4 @@ struct expr_ops expr_ops_target = { .parse = nftnl_expr_target_parse, .build = nftnl_expr_target_build, .snprintf = nftnl_expr_target_snprintf, - .json_parse = nftnl_expr_target_json_parse, }; diff --git a/src/expr/tunnel.c b/src/expr/tunnel.c index 2067c35d79d5..677e9685b8ad 100644 --- a/src/expr/tunnel.c +++ b/src/expr/tunnel.c @@ -150,20 +150,6 @@ nftnl_expr_tunnel_snprintf_default(char *buf, size_t len, return 0; } -static int nftnl_expr_tunnel_export(char *buf, size_t size, - const struct nftnl_expr *e, int type) -{ - struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) - nftnl_buf_u32(&b, type, tunnel->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY)) - nftnl_buf_str(&b, type, tunnel_key2str(tunnel->key), KEY); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_tunnel_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) @@ -173,7 +159,6 @@ nftnl_expr_tunnel_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_tunnel_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_tunnel_export(buf, len, e, type); default: break; } diff --git a/src/expr/xfrm.c b/src/expr/xfrm.c index b159d60d1e3f..dd3062c43a74 100644 --- a/src/expr/xfrm.c +++ b/src/expr/xfrm.c @@ -199,66 +199,6 @@ static int str2xfmrdir(const char *s) } #endif -static int nftnl_expr_xfrm_json_parse(struct nftnl_expr *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - const char *key_str, *dir_str; - uint32_t reg, key, spnum; - uint8_t dir; - - if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, ®, err) == 0) - nftnl_expr_set_u32(e, NFTNL_EXPR_XFRM_DREG, reg); - - key_str = nftnl_jansson_parse_str(root, "key", err); - if (key_str != NULL) { - key = str2xfrmkey(key_str); - if (key < 0) - return -1; - - nftnl_expr_set_u32(e, NFTNL_EXPR_XFRM_KEY, key); - } - - dir_str = nftnl_jansson_parse_str(root, "dir", err); - if (dir_str != NULL) { - dir = str2xfmrdir(dir_str); - if (dir == -1) { - err->node_name = "dir"; - err->error = NFTNL_PARSE_EBADTYPE; - goto err; - } - nftnl_expr_set_u8(e, NFTNL_EXPR_XFRM_DIR, dir); - } - - return 0; -err: - errno = EINVAL; - return -1; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_expr_xfrm_export(char *buf, size_t size, const struct nftnl_expr *e, - int type) -{ - struct nftnl_expr_xfrm *x = nftnl_expr_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_EXPR_XFRM_DREG)) - nftnl_buf_u32(&b, type, x->dreg, DREG); - if (e->flags & (1 << NFTNL_EXPR_XFRM_KEY)) - nftnl_buf_str(&b, type, xfrmkey2str(x->key), KEY); - if (e->flags & (1 << NFTNL_EXPR_XFRM_DIR)) - nftnl_buf_str(&b, type, xfrmdir2str(x->dir), DIR); - if (e->flags & (1 << NFTNL_EXPR_XFRM_SPNUM)) - nftnl_buf_u32(&b, type, x->spnum, NUM); - - return nftnl_buf_done(&b); -} - static int nftnl_expr_xfrm_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) @@ -285,7 +225,6 @@ nftnl_expr_xfrm_snprintf(char *buf, size_t len, uint32_t type, return nftnl_expr_xfrm_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_expr_xfrm_export(buf, len, e, type); default: break; } @@ -321,5 +260,4 @@ struct expr_ops expr_ops_xfrm = { .parse = nftnl_expr_xfrm_parse, .build = nftnl_expr_xfrm_build, .snprintf = nftnl_expr_xfrm_snprintf, - .json_parse = nftnl_expr_xfrm_json_parse, }; diff --git a/src/flowtable.c b/src/flowtable.c index c1ddae4ad65e..c939306b56c4 100644 --- a/src/flowtable.c +++ b/src/flowtable.c @@ -513,109 +513,11 @@ static inline int nftnl_str2hooknum(int family, const char *hook) return -1; } -#ifdef JSON_PARSING -static int nftnl_jansson_parse_flowtable(struct nftnl_flowtable *c, - json_t *tree, - struct nftnl_parse_err *err) -{ - const char *name, *table, *hooknum_str; - int32_t family, prio, hooknum; - uint32_t size; - json_t *root; - - root = nftnl_jansson_get_node(tree, "flowtable", err); - if (root == NULL) - return -1; - - name = nftnl_jansson_parse_str(root, "name", err); - if (name != NULL) - nftnl_flowtable_set_str(c, NFTNL_FLOWTABLE_NAME, name); - - if (nftnl_jansson_parse_family(root, &family, err) == 0) - nftnl_flowtable_set_u32(c, NFTNL_FLOWTABLE_FAMILY, family); - - table = nftnl_jansson_parse_str(root, "table", err); - - if (table != NULL) - nftnl_flowtable_set_str(c, NFTNL_FLOWTABLE_TABLE, table); - - if (nftnl_jansson_node_exist(root, "hooknum")) { - if (nftnl_jansson_parse_val(root, "prio", NFTNL_TYPE_S32, - &prio, err) == 0) - nftnl_flowtable_set_s32(c, NFTNL_FLOWTABLE_PRIO, prio); - - hooknum_str = nftnl_jansson_parse_str(root, "hooknum", err); - if (hooknum_str != NULL) { - hooknum = nftnl_str2hooknum(c->family, hooknum_str); - if (hooknum == -1) - return -1; - nftnl_flowtable_set_u32(c, NFTNL_FLOWTABLE_HOOKNUM, - hooknum); - } - } - if (nftnl_jansson_parse_val(root, "size", NFTNL_TYPE_U32, - &size, err) == 0) - nftnl_flowtable_set_u32(c, NFTNL_FLOWTABLE_SIZE, size); - - return 0; -} -#endif - -static int nftnl_flowtable_json_parse(struct nftnl_flowtable *c, - const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_flowtable(c, tree, err); - - nftnl_jansson_free_root(tree); - - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_flowtable_do_parse(struct nftnl_flowtable *c, - enum nftnl_parse_type type, - const void *data, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ - int ret; - struct nftnl_parse_err perr = {}; - - switch (type) { - case NFTNL_PARSE_JSON: - ret = nftnl_flowtable_json_parse(c, data, &perr, input); - break; - case NFTNL_PARSE_XML: - default: - ret = -1; - errno = EOPNOTSUPP; - break; - } - - if (err != NULL) - *err = perr; - - return ret; -} - int nftnl_flowtable_parse(struct nftnl_flowtable *c, enum nftnl_parse_type type, const char *data, struct nftnl_parse_err *err) { - return nftnl_flowtable_do_parse(c, type, data, err, NFTNL_PARSE_BUFFER); + errno = EOPNOTSUPP; + return -1; } EXPORT_SYMBOL(nftnl_flowtable_parse); @@ -623,41 +525,11 @@ int nftnl_flowtable_parse_file(struct nftnl_flowtable *c, enum nftnl_parse_type type, FILE *fp, struct nftnl_parse_err *err) { - return nftnl_flowtable_do_parse(c, type, fp, err, NFTNL_PARSE_FILE); + errno = EOPNOTSUPP; + return -1; } EXPORT_SYMBOL(nftnl_flowtable_parse_file); -static int nftnl_flowtable_export(char *buf, size_t size, - const struct nftnl_flowtable *c, int type) -{ - NFTNL_BUF_INIT(b, buf, size); - - nftnl_buf_open(&b, type, CHAIN); - if (c->flags & (1 << NFTNL_FLOWTABLE_NAME)) - nftnl_buf_str(&b, type, c->name, NAME); - if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE)) - nftnl_buf_str(&b, type, c->table, TABLE); - if (c->flags & (1 << NFTNL_FLOWTABLE_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(c->family), FAMILY); - if (c->flags & (1 << NFTNL_FLOWTABLE_USE)) - nftnl_buf_u32(&b, type, c->use, USE); - if (c->flags & (1 << NFTNL_FLOWTABLE_HOOKNUM)) { - if (c->flags & (1 << NFTNL_FLOWTABLE_HOOKNUM)) - nftnl_buf_str(&b, type, nftnl_hooknum2str(c->family, - c->hooknum), HOOKNUM); - if (c->flags & (1 << NFTNL_FLOWTABLE_PRIO)) - nftnl_buf_s32(&b, type, c->prio, PRIO); - } - if (c->flags & (1 << NFTNL_FLOWTABLE_SIZE)) - nftnl_buf_u32(&b, type, c->size, SIZE); - if (c->flags & (1 << NFTNL_FLOWTABLE_FLAGS)) - nftnl_buf_u32(&b, type, c->ft_flags, FLAGS); - - nftnl_buf_close(&b, type, CHAIN); - - return nftnl_buf_done(&b); -} - static int nftnl_flowtable_snprintf_default(char *buf, size_t size, const struct nftnl_flowtable *c) { @@ -697,26 +569,18 @@ static int nftnl_flowtable_cmd_snprintf(char *buf, size_t size, { int ret, remain = size, offset = 0; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch (type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_flowtable_snprintf_default(buf + offset, remain, c); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - ret = nftnl_flowtable_export(buf + offset, remain, c, type); break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/gen.c b/src/gen.c index f92180c7baf6..8e1a37a3097f 100644 --- a/src/gen.c +++ b/src/gen.c @@ -156,32 +156,20 @@ int nftnl_gen_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_gen *gen) return 0; } -static int nftnl_gen_snprintf_default(char *buf, size_t size, - const struct nftnl_gen *gen) -{ - return snprintf(buf, size, "ruleset generation ID %u", gen->id); -} - static int nftnl_gen_cmd_snprintf(char *buf, size_t size, const struct nftnl_gen *gen, uint32_t cmd, uint32_t type, uint32_t flags) { int ret, remain = size, offset = 0; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch(type) { case NFTNL_OUTPUT_DEFAULT: - ret = nftnl_gen_snprintf_default(buf + offset, remain, gen); + ret = snprintf(buf, size, "ruleset generation ID %u", gen->id); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/src/jansson.c b/src/jansson.c deleted file mode 100644 index 3476ed264dfa..000000000000 --- a/src/jansson.c +++ /dev/null @@ -1,279 +0,0 @@ -/* - * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@xxxxxxxxx> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - */ - -#include <internal.h> -#include <stdlib.h> -#include <limits.h> -#include <stdint.h> -#include <stdbool.h> -#include <arpa/inet.h> -#include <errno.h> -#include <string.h> -#include "expr_ops.h" -#include <libnftnl/set.h> - -#include <libnftnl/expr.h> -#include <linux/netfilter/nf_tables.h> - -#ifdef JSON_PARSING - -static int nftnl_jansson_load_int_node(json_t *root, const char *node_name, - json_int_t *val, struct nftnl_parse_err *err) -{ - json_t *node; - - node = json_object_get(root, node_name); - if (node == NULL) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = node_name; - errno = EINVAL; - return -1; - } - - if (!json_is_integer(node)) { - err->error = NFTNL_PARSE_EBADTYPE; - err->node_name = node_name; - errno = ERANGE; - return -1; - } - *val = json_integer_value(node); - - return 0; -} - -const char *nftnl_jansson_parse_str(json_t *root, const char *node_name, - struct nftnl_parse_err *err) -{ - json_t *node; - const char *val; - - node = json_object_get(root, node_name); - if (node == NULL) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = node_name; - errno = EINVAL; - return NULL; - } - - val = json_string_value(node); - if (val == NULL) { - err->error = NFTNL_PARSE_EBADTYPE; - err->node_name = node_name; - } - - return val; -} - -int nftnl_jansson_parse_val(json_t *root, const char *node_name, int type, - void *out, struct nftnl_parse_err *err) -{ - json_int_t val; - - if (nftnl_jansson_load_int_node(root, node_name, &val, err) == -1) - return -1; - - if (nftnl_get_value(type, &val, out) == -1) - return -1; - - return 0; -} - -bool nftnl_jansson_node_exist(json_t *root, const char *node_name) -{ - return json_object_get(root, node_name) != NULL; -} - -json_t *nftnl_jansson_create_root(const void *json, json_error_t *error, - struct nftnl_parse_err *err, enum nftnl_parse_input input) -{ - json_t *root; - - switch (input) { - case NFTNL_PARSE_BUFFER: - root = json_loadb(json, strlen(json), 0, error); - break; - case NFTNL_PARSE_FILE: - root = json_loadf((FILE *)json, 0, error); - break; - default: - goto err; - } - - if (root == NULL) { - err->error = NFTNL_PARSE_EBADINPUT; - err->line = error->line; - err->column = error->column; - err->node_name = error->source; - goto err; - } - - return root; -err: - errno = EINVAL; - return NULL; -} - -json_t *nftnl_jansson_get_node(json_t *root, const char *node_name, - struct nftnl_parse_err *err) -{ - json_t *node; - - node = json_object_get(root, node_name); - if (node == NULL) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = node_name; - errno = EINVAL; - return NULL; - } - - return node; -} - -void nftnl_jansson_free_root(json_t *root) -{ - json_decref(root); -} - -int nftnl_jansson_parse_family(json_t *root, void *out, struct nftnl_parse_err *err) -{ - const char *str; - int family; - - str = nftnl_jansson_parse_str(root, "family", err); - if (str == NULL) - return -1; - - family = nftnl_str2family(str); - if (family < 0) { - err->node_name = "family"; - errno = EINVAL; - return -1; - } - - memcpy(out, &family, sizeof(family)); - return 0; -} - -int nftnl_jansson_parse_reg(json_t *root, const char *node_name, int type, - void *out, struct nftnl_parse_err *err) -{ - if (nftnl_jansson_parse_val(root, node_name, type, out, err) < 0) - return -1; - - if (*((uint32_t *)out) > NFT_REG_MAX){ - errno = ERANGE; - return -1; - } - - return 0; -} - -int nftnl_jansson_str2num(json_t *root, const char *node_name, int base, - void *out, enum nftnl_type type, struct nftnl_parse_err *err) -{ - const char *str; - - str = nftnl_jansson_parse_str(root, node_name, err); - if (str == NULL) - return -1; - - return nftnl_strtoi(str, base, out, type); -} - -struct nftnl_expr *nftnl_jansson_expr_parse(json_t *root, - struct nftnl_parse_err *err, - struct nftnl_set_list *set_list) -{ - struct nftnl_expr *e; - const char *type; - uint32_t set_id; - int ret; - - type = nftnl_jansson_parse_str(root, "type", err); - if (type == NULL) - return NULL; - - e = nftnl_expr_alloc(type); - if (e == NULL) { - err->node_name = "type"; - return NULL; - } - - ret = e->ops->json_parse(e, root, err); - - if (set_list != NULL && - strcmp(type, "lookup") == 0 && - nftnl_set_lookup_id(e, set_list, &set_id)) - nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_SET_ID, set_id); - - return ret < 0 ? NULL : e; -} - -int nftnl_jansson_data_reg_parse(json_t *root, const char *node_name, - union nftnl_data_reg *data_reg, - struct nftnl_parse_err *err) -{ - json_t *data; - int ret; - - /* It is necessary for the compatibility with cmpdata label. */ - data = json_object_get(root, node_name); - if (data == NULL) - data = root; - - data = json_object_get(data, "reg"); - if (data == NULL) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = "reg"; - errno = EINVAL; - return -1; - } - - ret = nftnl_data_reg_json_parse(data_reg, data, err); - if (ret == DATA_NONE) { - errno = EINVAL; - return -1; - } - - return ret; -} - -int nftnl_jansson_set_elem_parse(struct nftnl_set_elem *e, json_t *root, - struct nftnl_parse_err *err) -{ - int set_elem_data; - uint32_t flags; - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, err) == 0) - nftnl_set_elem_set_u32(e, NFTNL_SET_ELEM_FLAGS, flags); - - if (nftnl_jansson_data_reg_parse(root, "key", &e->key, err) == DATA_VALUE) - e->flags |= (1 << NFTNL_SET_ELEM_KEY); - - if (nftnl_jansson_node_exist(root, "data")) { - set_elem_data = nftnl_jansson_data_reg_parse(root, "data", - &e->data, err); - switch (set_elem_data) { - case DATA_VALUE: - e->flags |= (1 << NFTNL_SET_ELEM_DATA); - break; - case DATA_VERDICT: - e->flags |= (1 << NFTNL_SET_ELEM_VERDICT); - if (e->data.chain != NULL) - e->flags |= (1 << NFTNL_SET_ELEM_CHAIN); - break; - case DATA_NONE: - default: - return -1; - } - } - - return 0; -} -#endif diff --git a/src/obj/counter.c b/src/obj/counter.c index 332bb2b35fa2..23b60c56638a 100644 --- a/src/obj/counter.c +++ b/src/obj/counter.c @@ -109,42 +109,6 @@ nftnl_obj_counter_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int -nftnl_obj_counter_json_parse(struct nftnl_obj *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t uval64; - - if (nftnl_jansson_parse_val(root, "pkts", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_CTR_PKTS, uval64); - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_CTR_BYTES, uval64); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_obj_counter_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_counter *ctr = nftnl_obj_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS)) - nftnl_buf_u64(&b, type, ctr->pkts, PKTS); - if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES)) - nftnl_buf_u64(&b, type, ctr->bytes, BYTES); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_counter_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -166,7 +130,6 @@ static int nftnl_obj_counter_snprintf(char *buf, size_t len, uint32_t type, return nftnl_obj_counter_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_obj_counter_export(buf, len, e, type); default: break; } @@ -183,5 +146,4 @@ struct obj_ops obj_ops_counter = { .parse = nftnl_obj_counter_parse, .build = nftnl_obj_counter_build, .snprintf = nftnl_obj_counter_snprintf, - .json_parse = nftnl_obj_counter_json_parse, }; diff --git a/src/obj/ct_helper.c b/src/obj/ct_helper.c index 62569fefe26f..5a689b1405d8 100644 --- a/src/obj/ct_helper.c +++ b/src/obj/ct_helper.c @@ -131,47 +131,6 @@ nftnl_obj_ct_helper_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int -nftnl_obj_quota_json_parse(struct nftnl_obj *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t bytes; - uint32_t flags; - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_BYTES, bytes); - if (nftnl_jansson_parse_val(root, "consumed", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_CONSUMED, bytes); - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_obj_set_u32(e, NFTNL_OBJ_QUOTA_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_obj_ct_helper_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_NAME)) - nftnl_buf_str(&b, type, helper->name, NAME); - if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_L3PROTO)) - nftnl_buf_u32(&b, type, helper->l3proto, FAMILY); - if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_L4PROTO)) - nftnl_buf_u32(&b, type, helper->l4proto, "service"); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_ct_helper_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -192,7 +151,6 @@ static int nftnl_obj_ct_helper_snprintf(char *buf, size_t len, uint32_t type, case NFTNL_OUTPUT_DEFAULT: return nftnl_obj_ct_helper_snprintf_default(buf, len, e); case NFTNL_OUTPUT_JSON: - return nftnl_obj_ct_helper_export(buf, len, e, type); default: break; } @@ -209,5 +167,4 @@ struct obj_ops obj_ops_ct_helper = { .parse = nftnl_obj_ct_helper_parse, .build = nftnl_obj_ct_helper_build, .snprintf = nftnl_obj_ct_helper_snprintf, - .json_parse = nftnl_obj_quota_json_parse, }; diff --git a/src/obj/ct_timeout.c b/src/obj/ct_timeout.c index 7c2ea97e7821..12c8f5f9b4df 100644 --- a/src/obj/ct_timeout.c +++ b/src/obj/ct_timeout.c @@ -254,21 +254,6 @@ nftnl_obj_ct_timeout_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int nftnl_obj_ct_timeout_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e); - - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L3PROTO)) - nftnl_buf_u32(&b, type, timeout->l3proto, FAMILY); - if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L4PROTO)) - nftnl_buf_u32(&b, type, timeout->l4proto, "service"); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_ct_timeout_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -330,7 +315,6 @@ static int nftnl_obj_ct_timeout_snprintf(char *buf, size_t len, uint32_t type, case NFTNL_OUTPUT_DEFAULT: return nftnl_obj_ct_timeout_snprintf_default(buf, len, e); case NFTNL_OUTPUT_JSON: - return nftnl_obj_ct_timeout_export(buf, len, e, type); default: break; } diff --git a/src/obj/limit.c b/src/obj/limit.c index 7f8bcf732b18..b0ed4c8b84b2 100644 --- a/src/obj/limit.c +++ b/src/obj/limit.c @@ -148,56 +148,6 @@ static int nftnl_obj_limit_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int nftnl_obj_limit_json_parse(struct nftnl_obj *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t uval64; - uint32_t uval32; - - if (nftnl_jansson_parse_val(root, "rate", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_LIMIT_RATE, uval64); - if (nftnl_jansson_parse_val(root, "unit", NFTNL_TYPE_U64, &uval64, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_LIMIT_UNIT, uval64); - if (nftnl_jansson_parse_val(root, "burst", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_obj_set_u32(e, NFTNL_OBJ_LIMIT_BURST, uval32); - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_obj_set_u32(e, NFTNL_OBJ_LIMIT_TYPE, uval32); - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &uval32, - err) == 0) - nftnl_obj_set_u32(e, NFTNL_OBJ_LIMIT_FLAGS, uval32); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_obj_limit_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_limit *limit = nftnl_obj_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_LIMIT_RATE)) - nftnl_buf_u64(&b, type, limit->rate, RATE); - if (e->flags & (1 << NFTNL_OBJ_LIMIT_UNIT)) - nftnl_buf_u64(&b, type, limit->unit, UNIT); - if (e->flags & (1 << NFTNL_OBJ_LIMIT_BURST)) - nftnl_buf_u32(&b, type, limit->burst, BURST); - if (e->flags & (1 << NFTNL_OBJ_LIMIT_TYPE)) - nftnl_buf_u32(&b, type, limit->type, TYPE); - if (e->flags & (1 << NFTNL_OBJ_LIMIT_FLAGS)) - nftnl_buf_u32(&b, type, limit->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_limit_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -220,7 +170,6 @@ static int nftnl_obj_limit_snprintf(char *buf, size_t len, uint32_t type, return nftnl_obj_limit_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_obj_limit_export(buf, len, e, type); default: break; } @@ -237,5 +186,4 @@ struct obj_ops obj_ops_limit = { .parse = nftnl_obj_limit_parse, .build = nftnl_obj_limit_build, .snprintf = nftnl_obj_limit_snprintf, - .json_parse = nftnl_obj_limit_json_parse, }; diff --git a/src/obj/quota.c b/src/obj/quota.c index 6d3678465d40..b1a3f7e4b754 100644 --- a/src/obj/quota.c +++ b/src/obj/quota.c @@ -125,47 +125,6 @@ nftnl_obj_quota_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int -nftnl_obj_quota_json_parse(struct nftnl_obj *e, json_t *root, - struct nftnl_parse_err *err) -{ -#ifdef JSON_PARSING - uint64_t bytes; - uint32_t flags; - - if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_BYTES, bytes); - if (nftnl_jansson_parse_val(root, "consumed", NFTNL_TYPE_U64, &bytes, - err) == 0) - nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_CONSUMED, bytes); - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_obj_set_u32(e, NFTNL_OBJ_QUOTA_FLAGS, flags); - - return 0; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int nftnl_obj_quota_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_quota *quota = nftnl_obj_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_QUOTA_BYTES)) - nftnl_buf_u64(&b, type, quota->bytes, BYTES); - if (e->flags & (1 << NFTNL_OBJ_QUOTA_CONSUMED)) - nftnl_buf_u64(&b, type, quota->consumed, CONSUMED); - if (e->flags & (1 << NFTNL_OBJ_QUOTA_FLAGS)) - nftnl_buf_u32(&b, type, quota->flags, FLAGS); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_quota_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -187,7 +146,6 @@ static int nftnl_obj_quota_snprintf(char *buf, size_t len, uint32_t type, return nftnl_obj_quota_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_obj_quota_export(buf, len, e, type); default: break; } @@ -204,5 +162,4 @@ struct obj_ops obj_ops_quota = { .parse = nftnl_obj_quota_parse, .build = nftnl_obj_quota_build, .snprintf = nftnl_obj_quota_snprintf, - .json_parse = nftnl_obj_quota_json_parse, }; diff --git a/src/obj/tunnel.c b/src/obj/tunnel.c index 32ca0fd59717..5b2d9de74438 100644 --- a/src/obj/tunnel.c +++ b/src/obj/tunnel.c @@ -530,18 +530,6 @@ nftnl_obj_tunnel_parse(struct nftnl_obj *e, struct nlattr *attr) return 0; } -static int nftnl_obj_tunnel_export(char *buf, size_t size, - const struct nftnl_obj *e, int type) -{ - struct nftnl_obj_tunnel *tun = nftnl_obj_data(e); - NFTNL_BUF_INIT(b, buf, size); - - if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ID)) - nftnl_buf_u64(&b, type, tun->id, ID); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_tunnel_snprintf_default(char *buf, size_t len, const struct nftnl_obj *e) { @@ -561,7 +549,6 @@ static int nftnl_obj_tunnel_snprintf(char *buf, size_t len, uint32_t type, return nftnl_obj_tunnel_snprintf_default(buf, len, e); case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - return nftnl_obj_tunnel_export(buf, len, e, type); default: break; } diff --git a/src/object.c b/src/object.c index 434f999d7414..09199ee48ca9 100644 --- a/src/object.c +++ b/src/object.c @@ -320,58 +320,6 @@ int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *obj) return 0; } -#ifdef JSON_PARSING -static int nftnl_jansson_parse_obj(struct nftnl_obj *t, json_t *tree, - struct nftnl_parse_err *err) -{ - const char *str; - uint32_t type; - json_t *root; - - root = nftnl_jansson_get_node(tree, "obj", err); - if (root == NULL) - return -1; - - str = nftnl_jansson_parse_str(root, "table", err); - if (str != NULL) - nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, str); - - str = nftnl_jansson_parse_str(root, "name", err); - if (str != NULL) - nftnl_obj_set_str(t, NFTNL_OBJ_NAME, str); - - if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &type, - err) < 0) - nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, type); - - return 0; -} -#endif - -static int nftnl_obj_json_parse(struct nftnl_obj *t, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_obj(t, tree, err); - - nftnl_jansson_free_root(tree); - - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type, const void *data, struct nftnl_parse_err *err, enum nftnl_parse_input input) @@ -381,8 +329,6 @@ static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type, switch (type) { case NFTNL_PARSE_JSON: - ret = nftnl_obj_json_parse(obj, data, &perr, input); - break; case NFTNL_PARSE_XML: default: ret = -1; @@ -410,36 +356,6 @@ int nftnl_obj_parse_file(struct nftnl_obj *obj, enum nftnl_parse_type type, return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE); } -static int nftnl_obj_export(char *buf, size_t size, - const struct nftnl_obj *obj, - uint32_t type, uint32_t flags) -{ - int ret = 0; - - NFTNL_BUF_INIT(b, buf, size); - - nftnl_buf_open(&b, type, TABLE); - if (obj->flags & (1 << NFTNL_OBJ_TABLE)) - nftnl_buf_str(&b, type, obj->name, NAME); - if (obj->flags & (1 << NFTNL_OBJ_NAME)) - nftnl_buf_str(&b, type, obj->name, NAME); - if (obj->flags & (1 << NFTNL_OBJ_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(obj->family), FAMILY); - if (obj->flags & (1 << NFTNL_OBJ_USE)) - nftnl_buf_u32(&b, type, obj->use, USE); - if (obj->flags & (1 << NFTNL_OBJ_HANDLE)) - nftnl_buf_u64(&b, type, obj->handle, HANDLE); - - if (obj->ops) - ret = obj->ops->snprintf(buf + b.len, size - b.len, type, - flags, obj); - - b.len += ret; - nftnl_buf_close(&b, type, TABLE); - - return nftnl_buf_done(&b); -} - static int nftnl_obj_snprintf_dflt(char *buf, size_t size, const struct nftnl_obj *obj, uint32_t type, uint32_t flags) @@ -468,26 +384,18 @@ static int nftnl_obj_cmd_snprintf(char *buf, size_t size, { int ret, remain = size, offset = 0; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch (type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, flags); break; case NFTNL_OUTPUT_JSON: - ret = nftnl_obj_export(buf + offset, remain, obj, type, flags); - break; case NFTNL_OUTPUT_XML: default: return -1; } SNPRINTF_BUFFER_SIZE(ret, remain, offset); - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/rule.c b/src/rule.c index 146b06ca91ef..a42d087c95c5 100644 --- a/src/rule.c +++ b/src/rule.c @@ -512,130 +512,6 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) return 0; } -#ifdef JSON_PARSING -int nftnl_jansson_parse_rule(struct nftnl_rule *r, json_t *tree, - struct nftnl_parse_err *err, - struct nftnl_set_list *set_list) -{ - json_t *root, *array; - struct nftnl_expr *e; - const char *str = NULL; - uint64_t uval64; - uint32_t uval32; - int i, family; - - root = nftnl_jansson_get_node(tree, "rule", err); - if (root == NULL) - return -1; - - if (nftnl_jansson_node_exist(root, "family")) { - if (nftnl_jansson_parse_family(root, &family, err) != 0) - goto err; - - nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family); - } - - if (nftnl_jansson_node_exist(root, "table")) { - str = nftnl_jansson_parse_str(root, "table", err); - if (str == NULL) - goto err; - - nftnl_rule_set_str(r, NFTNL_RULE_TABLE, str); - } - - if (nftnl_jansson_node_exist(root, "chain")) { - str = nftnl_jansson_parse_str(root, "chain", err); - if (str == NULL) - goto err; - - nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, str); - } - - if (nftnl_jansson_node_exist(root, "handle")) { - if (nftnl_jansson_parse_val(root, "handle", NFTNL_TYPE_U64, &uval64, - err) < 0) - goto err; - - nftnl_rule_set_u64(r, NFTNL_RULE_HANDLE, uval64); - } - - if (nftnl_jansson_node_exist(root, "compat_proto") || - nftnl_jansson_node_exist(root, "compat_flags")) { - if (nftnl_jansson_parse_val(root, "compat_proto", NFTNL_TYPE_U32, - &uval32, err) < 0) - goto err; - - nftnl_rule_set_u32(r, NFTNL_RULE_COMPAT_PROTO, uval32); - - if (nftnl_jansson_parse_val(root, "compat_flags", NFTNL_TYPE_U32, - &uval32, err) < 0) - goto err; - - nftnl_rule_set_u32(r, NFTNL_RULE_COMPAT_FLAGS, uval32); - } - - if (nftnl_jansson_node_exist(root, "position")) { - if (nftnl_jansson_parse_val(root, "position", NFTNL_TYPE_U64, - &uval64, err) < 0) - goto err; - - nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, uval64); - } - - if (nftnl_jansson_node_exist(root, "id")) { - if (nftnl_jansson_parse_val(root, "id", NFTNL_TYPE_U32, - &uval32, err) < 0) - goto err; - nftnl_rule_set_u32(r, NFTNL_RULE_COMPAT_PROTO, uval32); - } - - array = json_object_get(root, "expr"); - if (array == NULL) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = "expr"; - goto err; - } - - for (i = 0; i < json_array_size(array); ++i) { - - e = nftnl_jansson_expr_parse(json_array_get(array, i), err, - set_list); - if (e == NULL) - goto err; - - nftnl_rule_add_expr(r, e); - } - - return 0; -err: - return -1; -} -#endif - -static int nftnl_rule_json_parse(struct nftnl_rule *r, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input, - struct nftnl_set_list *set_list) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_rule(r, tree, err, set_list); - - nftnl_jansson_free_root(tree); - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_rule_do_parse(struct nftnl_rule *r, enum nftnl_parse_type type, const void *data, struct nftnl_parse_err *err, enum nftnl_parse_input input) @@ -645,8 +521,6 @@ static int nftnl_rule_do_parse(struct nftnl_rule *r, enum nftnl_parse_type type, switch (type) { case NFTNL_PARSE_JSON: - ret = nftnl_rule_json_parse(r, data, &perr, input, NULL); - break; case NFTNL_PARSE_XML: default: ret = -1; @@ -673,43 +547,6 @@ int nftnl_rule_parse_file(struct nftnl_rule *r, enum nftnl_parse_type type, return nftnl_rule_do_parse(r, type, fp, err, NFTNL_PARSE_FILE); } -static int nftnl_rule_export(char *buf, size_t size, - const struct nftnl_rule *r, - uint32_t type, uint32_t flags) -{ - struct nftnl_expr *expr; - - NFTNL_BUF_INIT(b, buf, size); - - nftnl_buf_open(&b, type, RULE); - - if (r->flags & (1 << NFTNL_RULE_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(r->family), FAMILY); - if (r->flags & (1 << NFTNL_RULE_TABLE)) - nftnl_buf_str(&b, type, r->table, TABLE); - if (r->flags & (1 << NFTNL_RULE_CHAIN)) - nftnl_buf_str(&b, type, r->chain, CHAIN); - if (r->flags & (1 << NFTNL_RULE_HANDLE)) - nftnl_buf_u64(&b, type, r->handle, HANDLE); - if (r->flags & (1 << NFTNL_RULE_COMPAT_PROTO)) - nftnl_buf_u32(&b, type, r->compat.proto, COMPAT_PROTO); - if (r->flags & (1 << NFTNL_RULE_COMPAT_FLAGS)) - nftnl_buf_u32(&b, type, r->compat.flags, COMPAT_FLAGS); - if (r->flags & (1 << NFTNL_RULE_POSITION)) - nftnl_buf_u64(&b, type, r->position, POSITION); - if (r->flags & (1 << NFTNL_RULE_ID)) - nftnl_buf_u32(&b, type, r->id, ID); - - nftnl_buf_expr_open(&b, type); - list_for_each_entry(expr, &r->expr_list, head) - nftnl_buf_expr(&b, type, flags, expr); - nftnl_buf_expr_close(&b, type); - - nftnl_buf_close(&b, type, RULE); - - return nftnl_buf_done(&b); -} - static int nftnl_rule_snprintf_default(char *buf, size_t size, const struct nftnl_rule *r, uint32_t type, uint32_t flags) @@ -795,28 +632,18 @@ static int nftnl_rule_cmd_snprintf(char *buf, size_t size, inner_flags &= ~NFTNL_OF_EVENT_ANY; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch(type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_rule_snprintf_default(buf + offset, remain, r, type, inner_flags); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; case NFTNL_OUTPUT_JSON: - ret = nftnl_rule_export(buf + offset, remain, r, type, - inner_flags); - break; case NFTNL_OUTPUT_XML: default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/ruleset.c b/src/ruleset.c index eb49fde582c2..2468bd46cd5d 100644 --- a/src/ruleset.c +++ b/src/ruleset.c @@ -45,8 +45,6 @@ struct nftnl_parse_ctx { void *data; /* These fields below are not exposed to the user */ - json_t *json; - uint32_t format; uint32_t set_id; struct nftnl_set_list *set_list; @@ -212,393 +210,14 @@ uint32_t nftnl_ruleset_ctx_get_u32(const struct nftnl_parse_ctx *ctx, uint16_t a return ret == NULL ? 0 : *((uint32_t *)ret); } -#if defined(JSON_PARSING) -static void nftnl_ruleset_ctx_set(struct nftnl_parse_ctx *ctx, uint16_t attr, - void *data) -{ - switch (attr) { - case NFTNL_RULESET_CTX_CMD: - ctx->cmd = *((uint32_t *)data); - break; - case NFTNL_RULESET_CTX_TYPE: - ctx->type = *((uint32_t *)data); - break; - case NFTNL_RULESET_CTX_TABLE: - ctx->table = data; - break; - case NFTNL_RULESET_CTX_CHAIN: - ctx->chain = data; - break; - case NFTNL_RULESET_CTX_RULE: - ctx->rule = data; - break; - case NFTNL_RULESET_CTX_SET: - ctx->set = data; - break; - case NFTNL_RULESET_CTX_DATA: - ctx->data = data; - break; - } - ctx->flags |= (1 << attr); -} - -static void nftnl_ruleset_ctx_set_u32(struct nftnl_parse_ctx *ctx, uint16_t attr, - uint32_t val) -{ - nftnl_ruleset_ctx_set(ctx, attr, &val); -} - -static int nftnl_ruleset_parse_tables(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - struct nftnl_table *table; - - table = nftnl_table_alloc(); - if (table == NULL) - return -1; - - switch (ctx->format) { - case NFTNL_OUTPUT_JSON: -#ifdef JSON_PARSING - if (nftnl_jansson_parse_table(table, ctx->json, err) < 0) - goto err; -#endif - break; - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - goto err; - } - - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_TYPE, NFTNL_RULESET_TABLE); - nftnl_ruleset_ctx_set(ctx, NFTNL_RULESET_CTX_TABLE, table); - if (ctx->cb(ctx) < 0) - goto err; - - return 0; -err: - nftnl_table_free(table); - return -1; -} - -static int nftnl_ruleset_parse_chains(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - struct nftnl_chain *chain; - - chain = nftnl_chain_alloc(); - if (chain == NULL) - return -1; - - switch (ctx->format) { - case NFTNL_OUTPUT_JSON: -#ifdef JSON_PARSING - if (nftnl_jansson_parse_chain(chain, ctx->json, err) < 0) - goto err; -#endif - break; - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - goto err; - } - - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_TYPE, NFTNL_RULESET_CHAIN); - nftnl_ruleset_ctx_set(ctx, NFTNL_RULESET_CTX_CHAIN, chain); - if (ctx->cb(ctx) < 0) - goto err; - - return 0; -err: - nftnl_chain_free(chain); - return -1; -} - -static int nftnl_ruleset_parse_set(struct nftnl_parse_ctx *ctx, - struct nftnl_set *set, uint32_t type, - struct nftnl_parse_err *err) -{ - struct nftnl_set *newset; - - nftnl_set_set_u32(set, NFTNL_SET_ID, ctx->set_id++); - - newset = nftnl_set_clone(set); - if (newset == NULL) - goto err; - - nftnl_set_list_add_tail(newset, ctx->set_list); - - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_TYPE, type); - nftnl_ruleset_ctx_set(ctx, NFTNL_RULESET_CTX_SET, set); - if (ctx->cb(ctx) < 0) - goto err; - - return 0; -err: - return -1; -} - -static int nftnl_ruleset_parse_set_elems(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - struct nftnl_set *set; - - set = nftnl_set_alloc(); - if (set == NULL) - return -1; - - switch (ctx->format) { - case NFTNL_OUTPUT_JSON: -#ifdef JSON_PARSING - if (nftnl_jansson_parse_elem(set, ctx->json, err) < 0) - goto err; -#endif - break; - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - goto err; - } - - if (nftnl_ruleset_parse_set(ctx, set, NFTNL_RULESET_SET_ELEMS, err) < 0) - goto err; - - return 0; -err: - nftnl_set_free(set); - return -1; -} - -static int nftnl_ruleset_parse_sets(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - struct nftnl_set *set; - - set = nftnl_set_alloc(); - if (set == NULL) - return -1; - - switch (ctx->format) { - case NFTNL_OUTPUT_JSON: -#ifdef JSON_PARSING - if (nftnl_jansson_parse_set(set, ctx->json, err) < 0) - goto err; -#endif - break; - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - goto err; - } - - if (nftnl_ruleset_parse_set(ctx, set, NFTNL_RULESET_SET, err) < 0) - goto err; - - return 0; -err: - nftnl_set_free(set); - return -1; -} - -static int nftnl_ruleset_parse_rules(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - struct nftnl_rule *rule; - - rule = nftnl_rule_alloc(); - if (rule == NULL) - return -1; - - switch (ctx->format) { - case NFTNL_OUTPUT_JSON: -#ifdef JSON_PARSING - if (nftnl_jansson_parse_rule(rule, ctx->json, err, - ctx->set_list) < 0) - goto err; -#endif - break; - case NFTNL_OUTPUT_XML: - default: - errno = EOPNOTSUPP; - goto err; - } - - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_TYPE, NFTNL_RULESET_RULE); - nftnl_ruleset_ctx_set(ctx, NFTNL_RULESET_CTX_RULE, rule); - if (ctx->cb(ctx) < 0) - goto err; - - return 0; -err: - nftnl_rule_free(rule); - return -1; -} -#endif - -#ifdef JSON_PARSING -static int nftnl_ruleset_json_parse_ruleset(struct nftnl_parse_ctx *ctx, - struct nftnl_parse_err *err) -{ - json_t *node, *array = ctx->json; - int len, i, ret; - - len = json_array_size(array); - for (i = 0; i < len; i++) { - node = json_array_get(array, i); - if (node == NULL) { - errno = EINVAL; - return -1; - } - - ctx->json = node; - if (nftnl_jansson_node_exist(node, "table")) - ret = nftnl_ruleset_parse_tables(ctx, err); - else if (nftnl_jansson_node_exist(node, "chain")) - ret = nftnl_ruleset_parse_chains(ctx, err); - else if (nftnl_jansson_node_exist(node, "set")) - ret = nftnl_ruleset_parse_sets(ctx, err); - else if (nftnl_jansson_node_exist(node, "rule")) - ret = nftnl_ruleset_parse_rules(ctx, err); - else if (nftnl_jansson_node_exist(node, "element")) - ret = nftnl_ruleset_parse_set_elems(ctx, err); - else - return -1; - - if (ret < 0) - return ret; - } - - if (len == 0 && ctx->cmd == NFTNL_CMD_FLUSH) { - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_TYPE, - NFTNL_RULESET_RULESET); - if (ctx->cb(ctx) < 0) - return -1; - } - - return 0; -} - -static int nftnl_ruleset_json_parse_cmd(const char *cmd, - struct nftnl_parse_err *err, - struct nftnl_parse_ctx *ctx) -{ - uint32_t cmdnum; - json_t *nodecmd; - - cmdnum = nftnl_str2cmd(cmd); - if (cmdnum == NFTNL_CMD_UNSPEC) { - err->error = NFTNL_PARSE_EMISSINGNODE; - err->node_name = strdup(cmd); - return -1; - } - - nftnl_ruleset_ctx_set_u32(ctx, NFTNL_RULESET_CTX_CMD, cmdnum); - - nodecmd = json_object_get(ctx->json, cmd); - if (nodecmd == NULL) - return 0; - - ctx->json = nodecmd; - if (nftnl_ruleset_json_parse_ruleset(ctx, err) != 0) - goto err; - - return 0; -err: - return -1; -} -#endif - -static int nftnl_ruleset_json_parse(const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input, - enum nftnl_parse_type type, void *arg, - int (*cb)(const struct nftnl_parse_ctx *ctx)) -{ -#ifdef JSON_PARSING - json_t *root, *array, *node; - json_error_t error; - int i, len; - const char *key; - struct nftnl_parse_ctx ctx = { - .cb = cb, - .format = type, - .flags = 0, - }; - - ctx.set_list = nftnl_set_list_alloc(); - if (ctx.set_list == NULL) - return -1; - - if (arg != NULL) - nftnl_ruleset_ctx_set(&ctx, NFTNL_RULESET_CTX_DATA, arg); - - root = nftnl_jansson_create_root(json, &error, err, input); - if (root == NULL) - goto err1; - - array = json_object_get(root, "nftables"); - if (array == NULL) { - errno = EINVAL; - goto err2; - } - - len = json_array_size(array); - for (i = 0; i < len; i++) { - node = json_array_get(array, i); - if (node == NULL) { - errno = EINVAL; - goto err2; - } - ctx.json = node; - key = json_object_iter_key(json_object_iter(node)); - if (key == NULL) - goto err2; - - if (nftnl_ruleset_json_parse_cmd(key, err, &ctx) < 0) - goto err2; - } - - nftnl_set_list_free(ctx.set_list); - nftnl_jansson_free_root(root); - return 0; -err2: - nftnl_jansson_free_root(root); -err1: - nftnl_set_list_free(ctx.set_list); - return -1; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_ruleset_do_parse(enum nftnl_parse_type type, const void *data, - struct nftnl_parse_err *err, enum nftnl_parse_input input, - void *arg, int (*cb)(const struct nftnl_parse_ctx *ctx)) -{ - int ret; - - switch (type) { - case NFTNL_PARSE_JSON: - ret = nftnl_ruleset_json_parse(data, err, input, type, arg, cb); - break; - case NFTNL_PARSE_XML: - default: - ret = -1; - errno = EOPNOTSUPP; - break; - } - - return ret; -} EXPORT_SYMBOL(nftnl_ruleset_parse_file_cb); int nftnl_ruleset_parse_file_cb(enum nftnl_parse_type type, FILE *fp, struct nftnl_parse_err *err, void *data, int (*cb)(const struct nftnl_parse_ctx *ctx)) { - return nftnl_ruleset_do_parse(type, fp, err, NFTNL_PARSE_FILE, data, cb); + errno = EOPNOTSUPP; + return -1; } EXPORT_SYMBOL(nftnl_ruleset_parse_buffer_cb); @@ -606,8 +225,8 @@ int nftnl_ruleset_parse_buffer_cb(enum nftnl_parse_type type, const char *buffer struct nftnl_parse_err *err, void *data, int (*cb)(const struct nftnl_parse_ctx *ctx)) { - return nftnl_ruleset_do_parse(type, buffer, err, NFTNL_PARSE_BUFFER, data, - cb); + errno = EOPNOTSUPP; + return -1; } static int nftnl_ruleset_cb(const struct nftnl_parse_ctx *ctx) @@ -675,7 +294,8 @@ EXPORT_SYMBOL(nftnl_ruleset_parse); int nftnl_ruleset_parse(struct nftnl_ruleset *r, enum nftnl_parse_type type, const char *data, struct nftnl_parse_err *err) { - return nftnl_ruleset_parse_buffer_cb(type, data, err, r, nftnl_ruleset_cb); + errno = EOPNOTSUPP; + return -1; } EXPORT_SYMBOL(nftnl_ruleset_parse_file); @@ -849,13 +469,6 @@ nftnl_ruleset_do_snprintf(char *buf, size_t size, const struct nftnl_ruleset *rs /* dont pass events flags to child calls of _snprintf() */ inner_flags &= ~NFTNL_OF_EVENT_ANY; - ret = snprintf(buf + offset, remain, "%s", - nftnl_ruleset_o_opentag(type)); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - if (nftnl_ruleset_is_set(rs, NFTNL_RULESET_TABLELIST) && (!nftnl_table_list_is_empty(rs->table_list))) { ret = nftnl_ruleset_snprintf_table(buf + offset, remain, rs, @@ -905,13 +518,6 @@ nftnl_ruleset_do_snprintf(char *buf, size_t size, const struct nftnl_ruleset *rs SNPRINTF_BUFFER_SIZE(ret, remain, offset); } - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = snprintf(buf + offset, remain, "%s", - nftnl_ruleset_o_closetag(type)); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } @@ -1108,9 +714,6 @@ static int nftnl_ruleset_cmd_fprintf(FILE *fp, const struct nftnl_ruleset *rs, ret = fprintf(fp, "%s", nftnl_ruleset_o_opentag(type)); NFTNL_FPRINTF_RETURN_OR_FIXLEN(ret, len); - ret = nftnl_cmd_header_fprintf(fp, cmd, type, flags); - NFTNL_FPRINTF_RETURN_OR_FIXLEN(ret, len); - if ((nftnl_ruleset_is_set(rs, NFTNL_RULESET_TABLELIST)) && (!nftnl_table_list_is_empty(rs->table_list))) { ret = nftnl_ruleset_fprintf_tables(fp, rs, type, inner_flags); @@ -1153,9 +756,6 @@ static int nftnl_ruleset_cmd_fprintf(FILE *fp, const struct nftnl_ruleset *rs, NFTNL_FPRINTF_RETURN_OR_FIXLEN(ret, len); } - ret = nftnl_cmd_footer_fprintf(fp, cmd, type, flags); - NFTNL_FPRINTF_RETURN_OR_FIXLEN(ret, len); - ret = fprintf(fp, "%s", nftnl_ruleset_o_closetag(type)); NFTNL_FPRINTF_RETURN_OR_FIXLEN(ret, len); diff --git a/src/set.c b/src/set.c index d2a75893ef70..74724224b683 100644 --- a/src/set.c +++ b/src/set.c @@ -569,153 +569,6 @@ int nftnl_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s) return 0; } -#ifdef JSON_PARSING -static int nftnl_jansson_parse_set_info(struct nftnl_set *s, json_t *tree, - struct nftnl_parse_err *err) -{ - json_t *root = tree, *array, *json_elem; - uint32_t flags, key_type, key_len, data_type, data_len, policy, size; - int family, i; - const char *name, *table; - struct nftnl_set_elem *elem; - - name = nftnl_jansson_parse_str(root, "name", err); - if (name == NULL) - return -1; - - nftnl_set_set_str(s, NFTNL_SET_NAME, name); - - table = nftnl_jansson_parse_str(root, "table", err); - if (table == NULL) - return -1; - - nftnl_set_set_str(s, NFTNL_SET_TABLE, table); - - if (nftnl_jansson_parse_family(root, &family, err) == 0) - nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, err) == 0) - nftnl_set_set_u32(s, NFTNL_SET_FLAGS, flags); - - if (nftnl_jansson_parse_val(root, "key_type", NFTNL_TYPE_U32, &key_type, - err) == 0) - nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, key_type); - - if (nftnl_jansson_parse_val(root, "key_len", NFTNL_TYPE_U32, &key_len, - err) == 0) - nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, key_len); - - if (nftnl_jansson_node_exist(root, "data_type")) { - if (nftnl_jansson_parse_val(root, "data_type", NFTNL_TYPE_U32, - &data_type, err) < 0) - return -1; - - nftnl_set_set_u32(s, NFTNL_SET_DATA_TYPE, data_type); - } - - if (nftnl_jansson_node_exist(root, "data_len")) { - if (nftnl_jansson_parse_val(root, "data_len", NFTNL_TYPE_U32, - &data_len, err) < 0) - return -1; - - nftnl_set_set_u32(s, NFTNL_SET_DATA_LEN, data_len); - } - - if (nftnl_jansson_node_exist(root, "obj_type")) { - if (nftnl_jansson_parse_val(root, "obj_type", NFTNL_TYPE_U32, - &data_type, err) < 0) - return -1; - - nftnl_set_set_u32(s, NFTNL_SET_OBJ_TYPE, data_type); - } - - if (nftnl_jansson_node_exist(root, "policy")) { - if (nftnl_jansson_parse_val(root, "policy", NFTNL_TYPE_U32, - &policy, err) < 0) - return -1; - - nftnl_set_set_u32(s, NFTNL_SET_POLICY, policy); - } - - if (nftnl_jansson_node_exist(root, "desc_size")) { - if (nftnl_jansson_parse_val(root, "desc_size", NFTNL_TYPE_U32, - &size, err) < 0) - return -1; - - nftnl_set_set_u32(s, NFTNL_SET_DESC_SIZE, size); - } - - if (nftnl_jansson_node_exist(root, "set_elem")) { - array = json_object_get(root, "set_elem"); - for (i = 0; i < json_array_size(array); i++) { - elem = nftnl_set_elem_alloc(); - if (elem == NULL) - return -1; - - json_elem = json_array_get(array, i); - if (json_elem == NULL || - nftnl_jansson_set_elem_parse(elem, - json_elem, err) < 0) { - free(elem); - return -1; - } - - list_add_tail(&elem->head, &s->element_list); - } - - } - - return 0; -} - -int nftnl_jansson_parse_set(struct nftnl_set *s, json_t *tree, - struct nftnl_parse_err *err) -{ - json_t *root; - - root = nftnl_jansson_get_node(tree, "set", err); - if (root == NULL) - return -1; - - return nftnl_jansson_parse_set_info(s, root, err); -} - -int nftnl_jansson_parse_elem(struct nftnl_set *s, json_t *tree, - struct nftnl_parse_err *err) -{ - json_t *root; - - root = nftnl_jansson_get_node(tree, "element", err); - if (root == NULL) - return -1; - - return nftnl_jansson_parse_set_info(s, root, err); -} -#endif - -static int nftnl_set_json_parse(struct nftnl_set *s, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_set(s, tree, err); - nftnl_jansson_free_root(tree); - - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_set_do_parse(struct nftnl_set *s, enum nftnl_parse_type type, const void *data, struct nftnl_parse_err *err, enum nftnl_parse_input input) @@ -725,8 +578,6 @@ static int nftnl_set_do_parse(struct nftnl_set *s, enum nftnl_parse_type type, switch (type) { case NFTNL_PARSE_JSON: - ret = nftnl_set_json_parse(s, data, &perr, input); - break; case NFTNL_PARSE_XML: default: ret = -1; @@ -754,104 +605,6 @@ int nftnl_set_parse_file(struct nftnl_set *s, enum nftnl_parse_type type, return nftnl_set_do_parse(s, type, fp, err, NFTNL_PARSE_FILE); } -static int nftnl_set_snprintf_json(char *buf, size_t size, - const struct nftnl_set *s, - uint32_t type, uint32_t flags) -{ - int remain = size, offset = 0, ret; - struct nftnl_set_elem *elem; - - ret = snprintf(buf, remain, "{\"set\":{"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - if (s->flags & (1 << NFTNL_SET_NAME)) { - ret = snprintf(buf + offset, remain, "\"name\":\"%s\"", - s->name); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_TABLE)) { - ret = snprintf(buf + offset, remain, ",\"table\":\"%s\"", - s->table); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_FLAGS)) { - ret = snprintf(buf + offset, remain, ",\"flags\":%u", - s->set_flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_FAMILY)) { - ret = snprintf(buf + offset, remain, ",\"family\":\"%s\"", - nftnl_family2str(s->family)); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_KEY_TYPE)) { - ret = snprintf(buf + offset, remain, ",\"key_type\":%u", - s->key_type); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_KEY_LEN)) { - ret = snprintf(buf + offset, remain, ",\"key_len\":%u", - s->key_len); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if(s->flags & (1 << NFTNL_SET_DATA_TYPE)) { - ret = snprintf(buf + offset, remain, - ",\"data_type\":%u", s->data_type); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if(s->flags & (1 << NFTNL_SET_DATA_LEN)) { - ret = snprintf(buf + offset, remain, ",\"data_len\":%u", - s->data_len); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - if (s->flags & (1 << NFTNL_SET_OBJ_TYPE)) { - ret = snprintf(buf + offset, remain, - ",\"obj_type\":%u", s->obj_type); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - if (s->flags & (1 << NFTNL_SET_POLICY)) { - ret = snprintf(buf + offset, remain, ",\"policy\":%u", - s->policy); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - if (s->flags & (1 << NFTNL_SET_DESC_SIZE)) { - ret = snprintf(buf + offset, remain, ",\"desc_size\":%u", - s->desc.size); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - /* Empty set? Skip printinf of elements */ - if (list_empty(&s->element_list)){ - ret = snprintf(buf + offset, remain, "}}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; - } - - ret = snprintf(buf + offset, remain, ",\"set_elem\":["); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - list_for_each_entry(elem, &s->element_list, head) { - ret = snprintf(buf + offset, remain, "{"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_set_elem_snprintf(buf + offset, remain, elem, type, - flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = snprintf(buf + offset, remain, "},"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - /* Overwrite trailing ", " from last set element */ - offset --; - - ret = snprintf(buf + offset, remain, "]}}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - return offset; -} - static int nftnl_set_snprintf_default(char *buf, size_t size, const struct nftnl_set *s, uint32_t type, uint32_t flags) @@ -918,27 +671,16 @@ static int nftnl_set_cmd_snprintf(char *buf, size_t size, /* prevent set_elems to print as events */ inner_flags &= ~NFTNL_OF_EVENT_ANY; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch(type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_set_snprintf_default(buf + offset, remain, s, type, inner_flags); - break; - case NFTNL_OUTPUT_JSON: - ret = nftnl_set_snprintf_json(buf + offset, remain, s, type, - inner_flags); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/set_elem.c b/src/set_elem.c index 1ac53dadbb75..bb8d0464087b 100644 --- a/src/set_elem.c +++ b/src/set_elem.c @@ -548,101 +548,20 @@ int nftnl_set_elems_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s) return 0; } -static int nftnl_set_elem_json_parse(struct nftnl_set_elem *e, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - return nftnl_jansson_set_elem_parse(e, tree, err); -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - -static int -nftnl_set_elem_do_parse(struct nftnl_set_elem *e, enum nftnl_parse_type type, - const void *data, struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ - int ret; - - switch (type) { - case NFTNL_PARSE_JSON: - ret = nftnl_set_elem_json_parse(e, data, err, input); - break; - case NFTNL_PARSE_XML: - default: - errno = EOPNOTSUPP; - ret = -1; - break; - } - - return ret; -} - EXPORT_SYMBOL(nftnl_set_elem_parse); int nftnl_set_elem_parse(struct nftnl_set_elem *e, enum nftnl_parse_type type, const char *data, struct nftnl_parse_err *err) { - return nftnl_set_elem_do_parse(e, type, data, err, NFTNL_PARSE_BUFFER); + errno = EOPNOTSUPP; + return -1; } EXPORT_SYMBOL(nftnl_set_elem_parse_file); int nftnl_set_elem_parse_file(struct nftnl_set_elem *e, enum nftnl_parse_type type, FILE *fp, struct nftnl_parse_err *err) { - return nftnl_set_elem_do_parse(e, type, fp, err, NFTNL_PARSE_FILE); -} - -static int nftnl_set_elem_snprintf_json(char *buf, size_t size, - const struct nftnl_set_elem *e, - uint32_t flags) -{ - int ret, remain = size, offset = 0, type = -1; - - if (e->flags & (1 << NFTNL_SET_ELEM_FLAGS)) { - ret = snprintf(buf, remain, "\"flags\":%u,", e->set_elem_flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - ret = snprintf(buf + offset, remain, "\"key\":{"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->key, - NFTNL_OUTPUT_JSON, flags, DATA_VALUE); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = snprintf(buf + offset, remain, "}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - if (e->flags & (1 << NFTNL_SET_ELEM_DATA)) - type = DATA_VALUE; - else if (e->flags & (1 << NFTNL_SET_ELEM_CHAIN)) - type = DATA_CHAIN; - else if (e->flags & (1 << NFTNL_SET_ELEM_VERDICT)) - type = DATA_VERDICT; - - if (type != -1) { - ret = snprintf(buf + offset, remain, ",\"data\":{"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->data, - NFTNL_OUTPUT_JSON, flags, type); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = snprintf(buf + offset, remain, "}"); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - } - - return offset; + errno = EOPNOTSUPP; + return -1; } static int nftnl_set_elem_snprintf_default(char *buf, size_t size, @@ -695,29 +614,18 @@ static int nftnl_set_elem_cmd_snprintf(char *buf, size_t size, { int ret, remain = size, offset = 0; - if (type == NFTNL_OUTPUT_XML) - return 0; - - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch(type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_set_elem_snprintf_default(buf + offset, remain, e); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; + case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - ret = nftnl_set_elem_snprintf_json(buf + offset, remain, e, - flags); break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - return offset; } diff --git a/src/table.c b/src/table.c index e7616f6dddbc..c987c5e7552a 100644 --- a/src/table.c +++ b/src/table.c @@ -283,72 +283,14 @@ int nftnl_table_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_table *t) return 0; } -#ifdef JSON_PARSING -int nftnl_jansson_parse_table(struct nftnl_table *t, json_t *tree, - struct nftnl_parse_err *err) -{ - json_t *root; - uint32_t flags, use; - const char *str; - int family; - - root = nftnl_jansson_get_node(tree, "table", err); - if (root == NULL) - return -1; - - str = nftnl_jansson_parse_str(root, "name", err); - if (str != NULL) - nftnl_table_set_str(t, NFTNL_TABLE_NAME, str); - - if (nftnl_jansson_parse_family(root, &family, err) == 0) - nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family); - - if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags, - err) == 0) - nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags); - - if (nftnl_jansson_parse_val(root, "use", NFTNL_TYPE_U32, &use, err) == 0) - nftnl_table_set_u32(t, NFTNL_TABLE_USE, use); - - return 0; -} -#endif - -static int nftnl_table_json_parse(struct nftnl_table *t, const void *json, - struct nftnl_parse_err *err, - enum nftnl_parse_input input) -{ -#ifdef JSON_PARSING - json_t *tree; - json_error_t error; - int ret; - - tree = nftnl_jansson_create_root(json, &error, err, input); - if (tree == NULL) - return -1; - - ret = nftnl_jansson_parse_table(t, tree, err); - - nftnl_jansson_free_root(tree); - - return ret; -#else - errno = EOPNOTSUPP; - return -1; -#endif -} - static int nftnl_table_do_parse(struct nftnl_table *t, enum nftnl_parse_type type, const void *data, struct nftnl_parse_err *err, enum nftnl_parse_input input) { int ret; - struct nftnl_parse_err perr = {}; switch (type) { case NFTNL_PARSE_JSON: - ret = nftnl_table_json_parse(t, data, &perr, input); - break; case NFTNL_PARSE_XML: default: ret = -1; @@ -356,9 +298,6 @@ static int nftnl_table_do_parse(struct nftnl_table *t, enum nftnl_parse_type typ break; } - if (err != NULL) - *err = perr; - return ret; } @@ -376,28 +315,6 @@ int nftnl_table_parse_file(struct nftnl_table *t, enum nftnl_parse_type type, return nftnl_table_do_parse(t, type, fp, err, NFTNL_PARSE_FILE); } -static int nftnl_table_export(char *buf, size_t size, - const struct nftnl_table *t, int type) -{ - NFTNL_BUF_INIT(b, buf, size); - - nftnl_buf_open(&b, type, TABLE); - if (t->flags & (1 << NFTNL_TABLE_NAME)) - nftnl_buf_str(&b, type, t->name, NAME); - if (t->flags & (1 << NFTNL_TABLE_FAMILY)) - nftnl_buf_str(&b, type, nftnl_family2str(t->family), FAMILY); - if (t->flags & (1 << NFTNL_TABLE_FLAGS)) - nftnl_buf_u32(&b, type, t->table_flags, FLAGS); - if (t->flags & (1 << NFTNL_TABLE_USE)) - nftnl_buf_u32(&b, type, t->use, USE); - if (t->flags & (1 << NFTNL_TABLE_HANDLE)) - nftnl_buf_u64(&b, type, t->handle, HANDLE); - - nftnl_buf_close(&b, type, TABLE); - - return nftnl_buf_done(&b); -} - static int nftnl_table_snprintf_default(char *buf, size_t size, const struct nftnl_table *t) { @@ -412,24 +329,16 @@ static int nftnl_table_cmd_snprintf(char *buf, size_t size, { int ret, remain = size, offset = 0; - ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - switch (type) { case NFTNL_OUTPUT_DEFAULT: ret = nftnl_table_snprintf_default(buf + offset, remain, t); + SNPRINTF_BUFFER_SIZE(ret, remain, offset); break; case NFTNL_OUTPUT_XML: case NFTNL_OUTPUT_JSON: - ret = nftnl_table_export(buf + offset, remain, t, type); - break; default: return -1; } - SNPRINTF_BUFFER_SIZE(ret, remain, offset); - - ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags); - SNPRINTF_BUFFER_SIZE(ret, remain, offset); return offset; } diff --git a/tests/Makefile.am b/tests/Makefile.am index 5872bb13990f..556575fcce4d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,10 +1,8 @@ include $(top_srcdir)/Make_global.am -EXTRA_DIST = test-script.sh \ - jsonfiles +EXTRA_DIST = test-script.sh -check_PROGRAMS = nft-parsing-test \ - nft-table-test \ +check_PROGRAMS = nft-table-test \ nft-chain-test \ nft-object-test \ nft-rule-test \ @@ -37,9 +35,6 @@ check_PROGRAMS = nft-parsing-test \ nft-expr_target-test \ nft-expr_hash-test -nft_parsing_test_SOURCES = nft-parsing-test.c -nft_parsing_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} ${LIBJSON_LIBS} - nft_table_test_SOURCES = nft-table-test.c nft_table_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c deleted file mode 100644 index d25a73ebd205..000000000000 --- a/tests/nft-parsing-test.c +++ /dev/null @@ -1,283 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <dirent.h> -#include <limits.h> -#include <errno.h> -#include <getopt.h> - -#include <libmnl/libmnl.h> /*nlmsghdr*/ -#include <libnftnl/ruleset.h> -#include <libnftnl/table.h> -#include <libnftnl/chain.h> -#include <libnftnl/rule.h> -#include <libnftnl/set.h> - -enum { - TEST_XML_RULESET, - TEST_JSON_RULESET, -}; - -static bool update = false; - -static void print_detail_error(char *a, char *b) -{ - int i; - int from = -1; - - for (i = 0; i < strlen(b); i++) { - if (from == -1 && a[i] != b[i]) { - from = i; - break; - - } - } - - if (from != -1) { - int k = from - 10; - - if (k < 0) - k = 0; - - fprintf(stderr, "from file: "); - for (i = k; i < from + 10; i++) - fprintf(stderr, "%c", a[i]); - - fprintf(stderr, "\nfrom snprintf: "); - for (i = k; i < from + 10; i++) - fprintf(stderr, "%c", b[i]); - - /* Don't look twice below this comment ;-) */ - fprintf(stderr, "\n "); - for (i = k; i < from + 10; i++) { - if (i == from) - fprintf(stderr, "^"); - else - fprintf(stderr, " "); - } - fprintf(stderr, "\n"); - } -} - -static int compare_test(uint32_t type, struct nftnl_ruleset *rs, - const char *filename, FILE *fp) -{ - char orig[4096]; - char out[4096]; - - switch (type) { - case TEST_XML_RULESET: - nftnl_ruleset_snprintf(out, sizeof(out), rs, - NFTNL_OUTPUT_XML, NFTNL_OF_EVENT_NEW); - break; - case TEST_JSON_RULESET: - nftnl_ruleset_snprintf(out, sizeof(out), rs, - NFTNL_OUTPUT_JSON, NFTNL_OF_EVENT_NEW); - break; - default: - errno = EINVAL; - return -1; - } - - rewind(fp); - fgets(orig, sizeof(orig), fp); - - if (strncmp(orig, out, strlen(out)) == 0) { - if (update) - printf("%s: No changes to update\n", filename); - return 0; - } - if (update) { - FILE *fout; - printf("%s: Updating test file\n", filename); - fout = fopen(filename, "w"); - if (fout == NULL) { - printf("unable to open file %s: %s\n", filename, - strerror(errno)); - return -1; - } - fprintf(fout, "%s\n", out); - fclose(fout); - return 0; - } - - printf("validating %s: ", filename); - printf("\033[31mFAILED\e[0m\n"); - print_detail_error(orig, out); - return -1; -} - -static int test_json(const char *filename, struct nftnl_parse_err *err) -{ - int ret = -1; - struct nftnl_ruleset *rs; - FILE *fp; - - fp = fopen(filename, "r"); - if (fp == NULL) { - printf("unable to open file %s: %s\n", filename, - strerror(errno)); - return -1; - } - - rs = nftnl_ruleset_alloc(); - if (rs == NULL) { - perror("nftnl_ruleset_alloc"); - return -1; - } - - if (nftnl_ruleset_parse_file(rs, NFTNL_PARSE_JSON, fp, err) == 0) - ret = compare_test(TEST_JSON_RULESET, rs, filename, fp); - else - goto failparsing; - - nftnl_ruleset_free(rs); - fclose(fp); - - return ret; - -failparsing: - fclose(fp); - printf("parsing %s: ", filename); - printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno)); - nftnl_parse_perror("Reason", err); - return -1; -} - -static int execute_test(const char *dir_name) -{ - DIR *d; - struct dirent *dent; - char path[PATH_MAX]; - int ret = 0, exit_code = 0; - struct nftnl_parse_err *err; - - d = opendir(dir_name); - if (d == NULL) { - perror("opendir"); - exit(EXIT_FAILURE); - } - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - exit(EXIT_FAILURE); - } - - while ((dent = readdir(d)) != NULL) { - int len = strlen(dent->d_name); - - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - - snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); - - if (strcmp(&dent->d_name[len-5], ".json") == 0) { - if ((ret = test_json(path, err)) == 0) { - if (!update) { - printf("parsing and validating %s: ", - path); - printf("\033[32mOK\e[0m\n"); - } - } - exit_code += ret; - } - } - - closedir(d); - nftnl_parse_err_free(err); - - if (exit_code != 0) - exit(EXIT_FAILURE); - - return 0; -} - -static int execute_test_file(const char *filename) -{ - char path[PATH_MAX]; - struct nftnl_parse_err *err; - int ret = 0, len; - - err = nftnl_parse_err_alloc(); - if (err == NULL) { - perror("error"); - exit(EXIT_FAILURE); - } - - snprintf(path, sizeof(path), "%s", filename); - - len = strlen(filename); - if (strcmp(&filename[len-5], ".json") == 0) { - if ((ret = test_json(path, err)) == 0) { - if (!update) { - printf("parsing and validating %s: ", - path); - printf("\033[32mOK\e[0m\n"); - } - } - nftnl_parse_err_free(err); - exit(EXIT_FAILURE); - } - - nftnl_parse_err_free(err); - - return 0; -} - -static void show_help(const char *name) -{ - printf( -"Usage: %s [option]\n" -"\n" -"Options:\n" -" -d/--dir <directory> Check test files from <directory>.\n" -" -u/--update <directory> Update test files from <directory>.\n" -" -f/--file <file> Check test file <file>\n" -"\n", - name); -} - -int main(int argc, char *argv[]) -{ - int val; - int ret = 0; - int option_index = 0; - static struct option long_options[] = { - { "dir", required_argument, 0, 'd' }, - { "update", required_argument, 0, 'u' }, - { "file", required_argument, 0, 'f' }, - { 0 } - }; - - if (argc != 3) { - show_help(argv[0]); - exit(EXIT_FAILURE); - } - - while (1) { - val = getopt_long(argc, argv, "d:u:f:", long_options, - &option_index); - - if (val == -1) - break; - - switch (val) { - case 'd': - ret = execute_test(optarg); - break; - case 'u': - update = true; - ret = execute_test(optarg); - break; - case 'f': - ret = execute_test_file(optarg); - break; - default: - show_help(argv[0]); - break; - } - } - return ret; -} diff --git a/tests/test-script.sh b/tests/test-script.sh index 2bd7ef0e6716..83dbda228abc 100755 --- a/tests/test-script.sh +++ b/tests/test-script.sh @@ -29,4 +29,3 @@ ./nft-set-test ./nft-table-test ./nft-object-test -./nft-parsing-test -d jsonfiles -- 2.11.0