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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx>
---
 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

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



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux