diff mbox

[libmnl] nlmsg: introduce mnl_nlmsg_batch_rest to get the rest length

Message ID 20170403061127.GA2329@gmail.com
State Changes Requested
Delegated to: Pablo Neira
Headers show

Commit Message

Ken-ichirou MATSUZAWA April 3, 2017, 6:11 a.m. UTC
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(+)

Comments

Pablo Neira Ayuso April 13, 2017, 10:27 p.m. UTC | #1
On Mon, Apr 03, 2017 at 03:11:27PM +0900, Ken-ichirou MATSUZAWA wrote:
> 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;

Only for this usecase above?

>     nlh = mnl_nlmsg_put_header(nlbuf);

Probably we need mnl_nlmsg_put_header_check()?

>     ...
>     if (!mnl_attr_put_u8_check(nlh, rest, 1, 2))
>             return false;
>     ...
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ken-ichirou MATSUZAWA April 20, 2017, 3:32 a.m. UTC | #2
On Fri, Apr 14, 2017 at 12:27:00AM +0200, Pablo Neira Ayuso wrote:
> On Mon, Apr 03, 2017 at 03:11:27PM +0900, Ken-ichirou MATSUZAWA wrote:
> > 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;
> 
> Only for this usecase above?

So far, yes. Is it worthless to apply?

> >     nlh = mnl_nlmsg_put_header(nlbuf);
> 
> Probably we need mnl_nlmsg_put_header_check()?

And I guess mnl_nlmsg_put_extra_header_check will also be needed.
I am going to resend if you intend to apply those.

Thanks,
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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;
+}
+
+/**
  * @}
  */