Skip to contentSkip to conversation

add example folder

PATCH New Pablo Neira Ayuso 1 participant 3 messages Feb 14, 2022 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover add example folder

Add two initial example files:

- use buffer API to load a ruleset
- load JSON ruleset from file

Pablo Neira Ayuso (2):
examples: add libnftables example program
examples: load ruleset from JSON

 configure.ac              |  1 +
 examples/Makefile.am      |  4 ++++
 examples/json-ruleset.nft | 43 +++++++++++++++++++++++++++++++++++++++
 examples/nft-buffer.c     | 34 +++++++++++++++++++++++++++++++
 examples/nft-json-file.c  | 30 +++++++++++++++++++++++++++
 5 files changed, 112 insertions(+)
 create mode 100644 examples/Makefile.am
 create mode 100644 examples/json-ruleset.nft
 create mode 100644 examples/nft-buffer.c
 create mode 100644 examples/nft-json-file.c
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/2 examples: add libnftables example program

Create an example folder to add example source code files to show how to use libnftables. Add first example program using the buffer API.

---
 configure.ac          |  1 +
 examples/Makefile.am  |  3 +++
 examples/nft-buffer.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 examples/Makefile.am
 create mode 100644 examples/nft-buffer.c
configure.ac +1 -0
index 503883f28c66..d321c960562b 100644
@@ -145,6 +145,7 @@ AC_CONFIG_FILES([					\
 		files/osf/Makefile			\
 		doc/Makefile				\
 		py/Makefile				\
+		examples/Makefile			\
 		])
 AC_OUTPUT
 
examples/Makefile.am new file +3 -0
new file mode 100644
index 000000000000..dd637fe31340
@@ -0,0 +1,3 @@
+noinst_PROGRAMS	= nft-buffer
+
+LDADD = $(top_builddir)/src/libnftables.la
examples/nft-buffer.c new file +34 -0
new file mode 100644
index 000000000000..1c4b1e041d75
@@ -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;
+}
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/2 examples: load ruleset from JSON

In reply to Pablo Neira Ayuso

Add an example to load a ruleset file expressed in JSON.

---
 examples/Makefile.am      |  3 ++-
 examples/json-ruleset.nft | 43 +++++++++++++++++++++++++++++++++++++++
 examples/nft-json-file.c  | 30 +++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 examples/json-ruleset.nft
 create mode 100644 examples/nft-json-file.c
examples/Makefile.am +2 -1
index dd637fe31340..c972170d3fdc 100644
@@ -1,3 +1,4 @@
-noinst_PROGRAMS	= nft-buffer
+noinst_PROGRAMS	= nft-buffer		\
+		  nft-json-file
 
 LDADD = $(top_builddir)/src/libnftables.la
examples/json-ruleset.nft new file +43 -0

43 lines not on this page · raw message

examples/nft-json-file.c new file +30 -0
new file mode 100644
index 000000000000..0c832f22e206
@@ -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;
+}
Signature · 1 line
2.30.2