diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/.gitignore | 5 | ||||
-rw-r--r-- | examples/json-ruleset.nft | 43 | ||||
-rw-r--r-- | examples/nft-buffer.c | 34 | ||||
-rw-r--r-- | examples/nft-json-file.c | 30 |
4 files changed, 112 insertions, 0 deletions
diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..7b1a583c --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,5 @@ +/.deps/ +/.libs/ +/nft-buffer +/nft-json-file +/*.o diff --git a/examples/json-ruleset.nft b/examples/json-ruleset.nft new file mode 100644 index 00000000..acea1786 --- /dev/null +++ b/examples/json-ruleset.nft @@ -0,0 +1,43 @@ +{ + "nftables": [ + { + "flush": { + "ruleset": { + "family": "ip" + } + } + }, + { + "table": { + "family": "ip", + "name": "x" + } + }, + { + "chain": { + "family": "ip", + "table": "x", + "name": "y", + "type": "filter", + "hook": "input", + "prio": 0, + "policy": "accept" + } + }, + { + "rule": { + "family": "ip", + "table": "x", + "chain": "y", + "expr": [ + { + "counter": { + "packets": 0, + "bytes": 0 + } + } + ] + } + } + ] +} diff --git a/examples/nft-buffer.c b/examples/nft-buffer.c new file mode 100644 index 00000000..1c4b1e04 --- /dev/null +++ b/examples/nft-buffer.c @@ -0,0 +1,34 @@ +/* gcc nft-buffer.c -o nft-buffer -lnftables */ +#include <stdlib.h> +#include <nftables/libnftables.h> + +const char ruleset[] = + "flush ruleset;" + "add table x;" + "add chain x y { type filter hook input priority 0; };" + "add rule x y counter;"; + +int main(void) +{ + struct nft_ctx *ctx; + int err; + + ctx = nft_ctx_new(0); + if (!ctx) { + perror("cannot allocate nft context"); + return EXIT_FAILURE; + } + + /* create ruleset: all commands in the buffer are atomically applied */ + err = nft_run_cmd_from_buffer(ctx, ruleset); + if (err < 0) + fprintf(stderr, "failed to run nftables command\n"); + + err = nft_run_cmd_from_buffer(ctx, "list ruleset"); + if (err < 0) + fprintf(stderr, "failed to run nftables command\n"); + + nft_ctx_free(ctx); + + return EXIT_SUCCESS; +} diff --git a/examples/nft-json-file.c b/examples/nft-json-file.c new file mode 100644 index 00000000..0c832f22 --- /dev/null +++ b/examples/nft-json-file.c @@ -0,0 +1,30 @@ +/* gcc nft-json-file.c -o nft-json-file -lnftables */ +#include <stdlib.h> +#include <nftables/libnftables.h> + +int main(void) +{ + struct nft_ctx *ctx; + int err; + + ctx = nft_ctx_new(0); + if (!ctx) { + perror("cannot allocate nft context"); + return EXIT_FAILURE; + } + + nft_ctx_output_set_flags(ctx, NFT_CTX_OUTPUT_JSON); + + /* create ruleset: all commands in the buffer are atomically applied */ + err = nft_run_cmd_from_filename(ctx, "json-ruleset.nft"); + if (err < 0) + fprintf(stderr, "failed to run nftables command\n"); + + err = nft_run_cmd_from_buffer(ctx, "list ruleset"); + if (err < 0) + fprintf(stderr, "failed to run nftables command\n"); + + nft_ctx_free(ctx); + + return EXIT_SUCCESS; +} |