summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-05-08 13:08:40 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2018-05-11 12:17:45 +0200
commit1688ff9cb1cf73d4b5ff16188cb1728305b9fc3b (patch)
tree133165f56fd7884b883491f790b943947ff3cc9e
parent100d98577043f28bce96405a5fbcceafdc0138b0 (diff)
py: Add JSON support to nftables Class
Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r--py/nftables.py45
1 files changed, 44 insertions, 1 deletions
diff --git a/py/nftables.py b/py/nftables.py
index eb81f5b2..47ff14af 100644
--- a/py/nftables.py
+++ b/py/nftables.py
@@ -68,6 +68,13 @@ class Nftables:
self.nft_ctx_output_set_stateless = lib.nft_ctx_output_set_stateless
self.nft_ctx_output_set_stateless.argtypes = [c_void_p, c_bool]
+ self.nft_ctx_output_get_json = lib.nft_ctx_output_get_json
+ self.nft_ctx_output_get_json.restype = c_bool
+ self.nft_ctx_output_get_json.argtypes = [c_void_p]
+
+ self.nft_ctx_output_set_json = lib.nft_ctx_output_set_json
+ self.nft_ctx_output_set_json.argtypes = [c_void_p, c_bool]
+
self.nft_ctx_output_get_debug = lib.nft_ctx_output_get_debug
self.nft_ctx_output_get_debug.restype = c_int
self.nft_ctx_output_get_debug.argtypes = [c_void_p]
@@ -169,7 +176,7 @@ class Nftables:
return self.nft_ctx_output_get_stateless(self.__ctx)
def set_stateless_output(self, val):
- """Enable or Disable stateless output.
+ """Enable or disable stateless output.
Accepts a boolean turning stateless output either on or off.
@@ -179,6 +186,24 @@ class Nftables:
self.nft_ctx_output_set_stateless(self.__ctx, val)
return old
+ def get_json_output(self):
+ """Get the current state of JSON output.
+
+ Returns a boolean indicating whether JSON output is active or not.
+ """
+ return self.nft_ctx_output_get_json(self.__ctx)
+
+ def set_json_output(self, val):
+ """Enable or disable JSON output.
+
+ Accepts a boolean turning JSON output either on or off.
+
+ Returns the previous value.
+ """
+ old = self.get_json_output()
+ self.nft_ctx_output_set_json(self.__ctx, val)
+ return old
+
def get_debug(self):
"""Get currently active debug flags.
@@ -247,3 +272,21 @@ class Nftables:
error = self.nft_ctx_get_error_buffer(self.__ctx)
return (rc, output, error)
+
+ def json_cmd(self, json_root):
+ """Run an nftables command in JSON syntax via libnftables.
+
+ Accepts a hash object as input.
+
+ Returns a tuple (rc, output, error):
+ rc -- reutrn code as returned by nft_run_cmd_from_buffer() function
+ output -- a hash object containing library standard output
+ error -- a string containing output written to stderr
+ """
+ json_out_old = self.set_json_output(True)
+ rc, output, error = self.cmd(json.dumps(json_root))
+ if not json_out_old:
+ self.set_json_output(json_out_old)
+ if len(output):
+ output = json.loads(output)
+ return (rc, output, error)