netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ken-ichirou MATSUZAWA <chamaken@gmail.com>
To: The netfilter developer mailinglist <netfilter-devel@vger.kernel.org>
Subject: [PATCH libmnl] nlmsg: introduce mnl_nlmsg_batch_rest to get the rest length
Date: Mon, 3 Apr 2017 15:11:27 +0900	[thread overview]
Message-ID: <20170403061127.GA2329@gmail.com> (raw)

Recent languages implements array to hold its length. This patch
enables to help to wrap these. As of this C library, we can use
this like below without double sized buffer.

    char buf[MNL_SOCKET_BUFFER_SIZE];

    b = mnl_nlmsg_batch_start(buf, sizeof(buf));
    nlbuf = mnl_nlmsg_batch_current(b);
    rest = mnl_nlmsg_batch_rest(b);
    if (rest < NLMSG_HDRLEN)
            return false;
    nlh = mnl_nlmsg_put_header(nlbuf);
    ...
    if (!mnl_attr_put_u8_check(nlh, rest, 1, 2))
            return false;
    ...

Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
---
 include/libmnl/libmnl.h |  1 +
 src/libmnl.map          |  4 ++++
 src/nlmsg.c             | 12 ++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h
index 0331da7..8cfe137 100644
--- a/include/libmnl/libmnl.h
+++ b/include/libmnl/libmnl.h
@@ -76,6 +76,7 @@ extern void mnl_nlmsg_batch_reset(struct mnl_nlmsg_batch *b);
 extern void *mnl_nlmsg_batch_head(struct mnl_nlmsg_batch *b);
 extern void *mnl_nlmsg_batch_current(struct mnl_nlmsg_batch *b);
 extern bool mnl_nlmsg_batch_is_empty(struct mnl_nlmsg_batch *b);
+extern size_t mnl_nlmsg_batch_rest(const struct mnl_nlmsg_batch *b);
 
 /*
  * Netlink attributes API
diff --git a/src/libmnl.map b/src/libmnl.map
index e5920e5..97b31d7 100644
--- a/src/libmnl.map
+++ b/src/libmnl.map
@@ -77,3 +77,7 @@ LIBMNL_1.2 {
   mnl_socket_open2;
   mnl_socket_fdopen;
 } LIBMNL_1.1;
+
+LIBMNL_1.3 {
+  mnl_nlmsg_batch_rest;
+} LIBMNL_1.2;
diff --git a/src/nlmsg.c b/src/nlmsg.c
index f9448a5..614e434 100644
--- a/src/nlmsg.c
+++ b/src/nlmsg.c
@@ -562,5 +562,17 @@ bool mnl_nlmsg_batch_is_empty(struct mnl_nlmsg_batch *b)
 }
 
 /**
+ * mnl_nlmsg_batch_rest - get the rest of the batch buffer size
+ * \param b pointer to batch
+ *
+ * This function returns the rest of the batch buffer size
+ */
+EXPORT_SYMBOL(mnl_nlmsg_batch_rest);
+size_t mnl_nlmsg_batch_rest(const struct mnl_nlmsg_batch *b)
+{
+        return b->limit - b->buflen;
+}
+
+/**
  * @}
  */
-- 
2.11.0


             reply	other threads:[~2017-04-03  6:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-03  6:11 Ken-ichirou MATSUZAWA [this message]
2017-04-13 22:27 ` [PATCH libmnl] nlmsg: introduce mnl_nlmsg_batch_rest to get the rest length Pablo Neira Ayuso
2017-04-20  3:32   ` Ken-ichirou MATSUZAWA

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=20170403061127.GA2329@gmail.com \
    --to=chamaken@gmail.com \
    --cc=netfilter-devel@vger.kernel.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 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).