netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH nft 1/3] src: move nf_sock into nft_ctx structure
@ 2017-09-01 10:14 Pablo Neira Ayuso
  2017-09-01 10:14 ` [PATCH nft 2/3] netlink: remove nfsock_open() Pablo Neira Ayuso
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Pablo Neira Ayuso @ 2017-09-01 10:14 UTC (permalink / raw)
  To: netfilter-devel; +Cc: phil, eric

The idea is to provide a simplistic API for non-netlink wise people.
Add a field in struct nft_ctx to store the socket.

The advanced API that we're planning will just simply leave this unset,
since netlink IO will be exposed.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
@Eric: Still pending the specific interface to print. As I said, I would prefer
       functions with stricting typing. I think after this you have the simple
       API that you need.

 include/nftables.h |  3 +++
 src/main.c         | 26 +++++++++++---------------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/include/nftables.h b/include/nftables.h
index 256b06ee33fc..5035567a75fd 100644
--- a/include/nftables.h
+++ b/include/nftables.h
@@ -38,7 +38,10 @@ struct nft_cache {
 	uint32_t		seqnum;
 };
 
+struct mnl_socket;
+
 struct nft_ctx {
+	struct mnl_socket	*nf_sock;
 	const char		*include_paths[INCLUDE_PATHS_MAX];
 	unsigned int		num_include_paths;
 	unsigned int		parser_max_errors;
diff --git a/src/main.c b/src/main.c
index eecd430a2e3f..a891832ec5d6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -305,7 +305,6 @@ static void nft_ctx_free(const struct nft_ctx *ctx)
 }
 
 static int nft_run_cmd_from_buffer(struct nft_ctx *nft,
-				   struct mnl_socket *nf_sock,
 				   char *buf, size_t buflen)
 {
 	int rc = NFT_EXIT_SUCCESS;
@@ -313,11 +312,11 @@ static int nft_run_cmd_from_buffer(struct nft_ctx *nft,
 	LIST_HEAD(msgs);
 	void *scanner;
 
-	parser_init(nf_sock, &nft->cache, &state, &msgs, nft->debug_mask);
+	parser_init(nft->nf_sock, &nft->cache, &state, &msgs, nft->debug_mask);
 	scanner = scanner_init(&state);
 	scanner_push_buffer(scanner, &indesc_cmdline, buf);
 
-	if (nft_run(nft, nf_sock, scanner, &state, &msgs) != 0)
+	if (nft_run(nft, nft->nf_sock, scanner, &state, &msgs) != 0)
 		rc = NFT_EXIT_FAILURE;
 
 	erec_print_list(stderr, &msgs, nft->debug_mask);
@@ -326,26 +325,24 @@ static int nft_run_cmd_from_buffer(struct nft_ctx *nft,
 	return rc;
 }
 
-static int nft_run_cmd_from_filename(struct nft_ctx *nft,
-				     struct mnl_socket *nf_sock,
-				     const char *filename)
+static int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 {
 	struct parser_state state;
 	LIST_HEAD(msgs);
 	void *scanner;
 	int rc;
 
-	rc = cache_update(nf_sock, &nft->cache, CMD_INVALID, &msgs,
+	rc = cache_update(nft->nf_sock, &nft->cache, CMD_INVALID, &msgs,
 			  nft->debug_mask);
 	if (rc < 0)
 		return NFT_EXIT_FAILURE;
 
-	parser_init(nf_sock, &nft->cache, &state, &msgs, nft->debug_mask);
+	parser_init(nft->nf_sock, &nft->cache, &state, &msgs, nft->debug_mask);
 	scanner = scanner_init(&state);
 	if (scanner_read_file(scanner, filename, &internal_location) < 0)
 		goto err;
 
-	if (nft_run(nft, nf_sock, scanner, &state, &msgs) != 0)
+	if (nft_run(nft, nft->nf_sock, scanner, &state, &msgs) != 0)
 		rc = NFT_EXIT_FAILURE;
 err:
 	erec_print_list(stderr, &msgs, nft->debug_mask);
@@ -359,13 +356,12 @@ int main(int argc, char * const *argv)
 	char *buf = NULL, *filename = NULL;
 	unsigned int len;
 	bool interactive = false;
-	struct mnl_socket *nf_sock;
 	struct parser_state state;
 	int i, val, rc;
 
 	nft = nft_ctx_new();
 
-	nf_sock = netlink_open_sock();
+	nft->nf_sock = netlink_open_sock();
 	while (1) {
 		val = getopt_long(argc, argv, OPTSTRING, options, NULL);
 		if (val == -1)
@@ -460,11 +456,11 @@ int main(int argc, char * const *argv)
 				strcat(buf, " ");
 		}
 		strcat(buf, "\n");
-		rc = nft_run_cmd_from_buffer(nft, nf_sock, buf, len + 2);
+		rc = nft_run_cmd_from_buffer(nft, buf, len + 2);
 	} else if (filename != NULL) {
-		rc = nft_run_cmd_from_filename(nft, nf_sock, filename);
+		rc = nft_run_cmd_from_filename(nft, filename);
 	} else if (interactive) {
-		if (cli_init(nft, nf_sock, &state) < 0) {
+		if (cli_init(nft, nft->nf_sock, &state) < 0) {
 			fprintf(stderr, "%s: interactive CLI not supported in this build\n",
 				argv[0]);
 			exit(NFT_EXIT_FAILURE);
@@ -476,7 +472,7 @@ int main(int argc, char * const *argv)
 	}
 
 	xfree(buf);
-	netlink_close_sock(nf_sock);
+	netlink_close_sock(nft->nf_sock);
 	nft_ctx_free(nft);
 
 	return rc;
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-09-01 17:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-01 10:14 [PATCH nft 1/3] src: move nf_sock into nft_ctx structure Pablo Neira Ayuso
2017-09-01 10:14 ` [PATCH nft 2/3] netlink: remove nfsock_open() Pablo Neira Ayuso
2017-09-01 10:14 ` [PATCH nft 3/3] src: add nft_ctx_netlink_init() Pablo Neira Ayuso
2017-09-01 10:17   ` Pablo Neira Ayuso
2017-09-01 10:50     ` Phil Sutter
2017-09-01 10:58       ` Pablo Neira Ayuso
2017-09-01 11:20         ` Phil Sutter
2017-09-01 12:28     ` Florian Westphal
2017-09-01 17:50 ` [PATCH nft 1/3] src: move nf_sock into nft_ctx structure Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).