nlmsg: introduce mnl_nlmsg_batch_rest to get the rest length
PATCH 1/1 nlmsg: introduce mnl_nlmsg_batch_rest to get the rest length
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;
...--- include/libmnl/libmnl.h | 1 + src/libmnl.map | 4 ++++ src/nlmsg.c | 12 ++++++++++++ 3 files changed, 17 insertions(+)
include/libmnl/libmnl.h +1 -0
@@ -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
src/libmnl.map +4 -0
@@ -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;
src/nlmsg.c +12 -0
@@ -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; +} + +/** * @} */
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerQuoted 11 lines · Ken-ichirou MATSUZAWA
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?
Quoted 1 line
nlh = mnl_nlmsg_put_header(nlbuf);
Probably we need mnl_nlmsg_put_header_check()?
Quoted 4 lines
...
if (!mnl_attr_put_u8_check(nlh, rest, 1, 2))
return false;
...Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Ken-ichirou MATSUZAWA
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];
Quoted 7 lines
Quoted 5 lines
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?
Quoted 3 lines
Quoted 1 line
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,