All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: pablo@netfilter.org
Subject: [nft PATCH 2/8] netlink: add netlink_delinearize_set() func
Date: Mon, 14 Apr 2014 12:17:14 +0200	[thread overview]
Message-ID: <20140414101714.5018.89872.stgit@nfdev.cica.es> (raw)
In-Reply-To: <20140414101634.5018.86819.stgit@nfdev.cica.es>

Let's factorize this code, so it can be reused.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 src/netlink.c |  112 +++++++++++++++++++++++++++------------------------------
 1 file changed, 53 insertions(+), 59 deletions(-)

diff --git a/src/netlink.c b/src/netlink.c
index daac64c..e7b3ec1 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -755,6 +755,57 @@ void netlink_dump_set(struct nft_set *nls)
 #endif
 }
 
+static struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
+					   struct nft_set *nls)
+{
+	struct set *set;
+	const struct datatype *keytype, *datatype;
+	uint32_t flags, key, data, data_len;
+
+	key = nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_TYPE);
+	keytype = dtype_map_from_kernel(key);
+	if (keytype == NULL) {
+		netlink_io_error(ctx, NULL, "Unknown data type in set key %u",
+				 key);
+		return NULL;
+	}
+
+	flags = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FLAGS);
+	if (flags & NFT_SET_MAP) {
+		data = nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_TYPE);
+		datatype = dtype_map_from_kernel(data);
+		if (datatype == NULL) {
+			netlink_io_error(ctx, NULL,
+					 "Unknown data type in set key %u",
+					 data);
+			return NULL;
+		}
+	} else
+		datatype = NULL;
+
+	set = set_alloc(&netlink_location);
+	set->handle.family = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FAMILY);
+	set->handle.table  =
+		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_TABLE));
+	set->handle.set    =
+		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_NAME));
+
+	set->keytype = keytype;
+	set->keylen        =
+		nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_LEN) * BITS_PER_BYTE;
+
+	set->flags         = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FLAGS);
+
+	set->datatype = datatype;
+	if (nft_set_attr_is_set(nls, NFT_SET_ATTR_DATA_LEN)) {
+		data_len = nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_LEN);
+		set->datalen = data_len * BITS_PER_BYTE;
+	}
+	list_add_tail(&set->list, &ctx->list);
+
+	return set;
+}
+
 int netlink_add_set(struct netlink_ctx *ctx, const struct handle *h,
 		    struct set *set)
 {
@@ -806,48 +857,9 @@ int netlink_delete_set(struct netlink_ctx *ctx, const struct handle *h,
 static int list_set_cb(struct nft_set *nls, void *arg)
 {
 	struct netlink_ctx *ctx = arg;
-	const struct datatype *keytype, *datatype;
-	uint32_t flags, key, data;
-	struct set *set;
 
 	netlink_dump_set(nls);
-	key = nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_TYPE);
-	keytype = dtype_map_from_kernel(key);
-	if (keytype == NULL) {
-		netlink_io_error(ctx, NULL, "Unknown data type in set key %u",
-				 key);
-		return -1;
-	}
-
-	flags = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FLAGS);
-	if (flags & NFT_SET_MAP) {
-		data = nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_TYPE);
-		datatype = dtype_map_from_kernel(data);
-		if (datatype == NULL) {
-			netlink_io_error(ctx, NULL, "Unknown data type in set key %u",
-					 data);
-			return -1;
-		}
-	} else
-		datatype = NULL;
-
-	set = set_alloc(&netlink_location);
-	set->handle.family = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FAMILY);
-	set->handle.table  =
-		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_TABLE));
-	set->handle.set    =
-		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_NAME));
-	set->keytype       = keytype;
-	set->keylen        =
-		nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_LEN) * BITS_PER_BYTE;
-	set->flags         = flags;
-	set->datatype      = datatype;
-	if (nft_set_attr_is_set(nls, NFT_SET_ATTR_DATA_LEN)) {
-		set->datalen =
-			nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_LEN) * BITS_PER_BYTE;
-	}
-	list_add_tail(&set->list, &ctx->list);
-
+	netlink_delinearize_set(ctx, nls);
 	return 0;
 }
 
@@ -871,7 +883,6 @@ int netlink_get_set(struct netlink_ctx *ctx, const struct handle *h,
 		    const struct location *loc)
 {
 	struct nft_set *nls;
-	struct set *set;
 	int err;
 
 	nls = alloc_nft_set(h);
@@ -882,24 +893,7 @@ int netlink_get_set(struct netlink_ctx *ctx, const struct handle *h,
 					"Could not receive set from kernel: %s",
 					strerror(errno));
 
-	set = set_alloc(&netlink_location);
-	set->handle.family = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FAMILY);
-	set->handle.table  =
-		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_TABLE));
-	set->handle.set    =
-		xstrdup(nft_set_attr_get_str(nls, NFT_SET_ATTR_NAME));
-	set->keytype       =
-		 dtype_map_from_kernel(nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_TYPE));
-	set->keylen        =
-		nft_set_attr_get_u32(nls, NFT_SET_ATTR_KEY_LEN) * BITS_PER_BYTE;
-	set->flags         = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FLAGS);
-	set->datatype      =
-		dtype_map_from_kernel(nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_TYPE));
-	if (nft_set_attr_is_set(nls, NFT_SET_ATTR_DATA_LEN)) {
-		set->datalen =
-			nft_set_attr_get_u32(nls, NFT_SET_ATTR_DATA_LEN) * BITS_PER_BYTE;
-	}
-	list_add_tail(&set->list, &ctx->list);
+	netlink_delinearize_set(ctx, nls);
 	nft_set_free(nls);
 
 	return err;


  parent reply	other threads:[~2014-04-14 10:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-14 10:17 [nft PATCH 0/8] nft event monitor Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 1/8] rule: allow to print sets in plain format Arturo Borrero Gonzalez
2014-04-14 10:17 ` Arturo Borrero Gonzalez [this message]
2014-04-14 10:17 ` [nft PATCH 3/8] rule: generalize chain_print() Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 4/8] netlink: add netlink_delinearize_chain() func Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 5/8] netlink: add netlink_delinearize_table() func Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 6/8] netlink: refactorize set_elem conversion from netlink Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 7/8] netlink: add socket error reporting helper function Arturo Borrero Gonzalez
2014-04-14 10:17 ` [nft PATCH 8/8] src: add events reporting Arturo Borrero Gonzalez
2014-04-14 12:28   ` Pablo Neira Ayuso
2014-04-14 12:32 ` [nft PATCH 0/8] nft event monitor Pablo Neira Ayuso
2014-04-14 12:35   ` Patrick McHardy
2014-04-28 14:28 ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140414101714.5018.89872.stgit@nfdev.cica.es \
    --to=arturo.borrero.glez@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.