libmnl: add support for signed types
PATCH 1/1 libmnl: add support for signed types
libmnl has get and put functions for unsigned integer types. It lacks support for the signed variations. On some level this is technically sufficient. A user could use the unsigned variations and then cast to a signed value at use. However, this makes resulting code in the application more difficult to follow. Introduce signed variations of the integer get and put functions.
--- include/libmnl/libmnl.h | 16 ++++ src/attr.c | 194 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+)
include/libmnl/libmnl.h +16 -0
@@ -92,6 +92,10 @@ extern uint8_t mnl_attr_get_u8(const struct nlattr *attr);extern uint16_t mnl_attr_get_u16 (const struct nlattr *attr);extern uint32_t mnl_attr_get_u32 (const struct nlattr *attr);extern uint64_t mnl_attr_get_u64 (const struct nlattr *attr); +extern int8_t mnl_attr_get_s8 (const struct nlattr *attr); +extern int16_t mnl_attr_get_s16 (const struct nlattr *attr); +extern int32_t mnl_attr_get_s32 (const struct nlattr *attr); +extern int64_t mnl_attr_get_s64 (const struct nlattr *attr);extern const char *mnl_attr_get_str (const struct nlattr *attr);/* TLV attribute putters */
@@ -100,6 +104,10 @@ extern void mnl_attr_put_u8(struct nlmsghdr *nlh, uint16_t type, uint8_t data);extern void mnl_attr_put_u16 (struct nlmsghdr *nlh,uint16_t type,uint16_t data);extern void mnl_attr_put_u32 (struct nlmsghdr *nlh,uint16_t type,uint32_t data);extern void mnl_attr_put_u64 (struct nlmsghdr *nlh,uint16_t type,uint64_t data); +extern void mnl_attr_put_s8 (struct nlmsghdr *nlh,uint16_t type,int8_t data); +extern void mnl_attr_put_s16 (struct nlmsghdr *nlh,uint16_t type,int16_t data); +extern void mnl_attr_put_s32 (struct nlmsghdr *nlh,uint16_t type,int32_t data); +extern void mnl_attr_put_s64 (struct nlmsghdr *nlh,uint16_t type,int64_t data);extern void mnl_attr_put_str (struct nlmsghdr *nlh,uint16_t type,const char *data);extern void mnl_attr_put_strz (struct nlmsghdr *nlh,uint16_t type,const char *data);
@@ -109,6 +117,10 @@ extern bool mnl_attr_put_u8_check(struct nlmsghdr *nlh, size_t buflen, uint16_textern bool mnl_attr_put_u16_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,uint16_t data);extern bool mnl_attr_put_u32_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,uint32_t data);extern bool mnl_attr_put_u64_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,uint64_t data); +extern bool mnl_attr_put_s8_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,int8_t data); +extern bool mnl_attr_put_s16_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,int16_t data); +extern bool mnl_attr_put_s32_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,int32_t data); +extern bool mnl_attr_put_s64_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,int64_t data);extern bool mnl_attr_put_str_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,const char *data);extern bool mnl_attr_put_strz_check (struct nlmsghdr *nlh,size_t buflen,uint16_t type,const char *data);
@@ -127,6 +139,10 @@ enum mnl_attr_data_type {MNL_TYPE_U16 ,MNL_TYPE_U32 ,MNL_TYPE_U64 , +MNL_TYPE_S8 , +MNL_TYPE_S16 , +MNL_TYPE_S32 , +MNL_TYPE_S64 ,MNL_TYPE_STRING ,MNL_TYPE_FLAG , MNL_TYPE_MSECS,
src/attr.c +194 -0
218 lines not on this page · raw message
Signature · 1 line
2.37.1.208.ge72d93e88cb2
Quoted 5 lines
+ *
mnl_attr_get_s8- returns 8-bit signed integer attribute payload
+ *
+ * This function returns the 8-bit value of the attribute payload.
+ */
That's kinda redundant - it's logically the same thing, just written ever-so-slightly differently.
Quoted 5 lines
+/**
+ *mnl_attr_get_s64- returns 64-bit signed integer attribute. This
+ * function is align-safe, since accessing 64-bit Netlink attributes is a
+ * common source of alignment issues.
That sentence is self-defeating. If NLA access is a commn source of alignment issues, then, by transitivity, this function too would be potentially affected. Just
"This function reads the 64-bit nlattr in an alignment-safe manner."
Quoted 11 lines
-----Original Message-----
From: Jan Engelhardt <[email protected]>
Sent: Thursday, August 04, 2022 4:36 PM
To: Keller, Jacob E <[email protected]>
Cc: Netfilter Devel <[email protected]>; Duncan Roe <[email protected]>; Pablo Neira Ayuso <[email protected]>
Subject: Re: [PATCH libmnl] libmnl: add support for signed types
Quoted 8 lines
Quoted 4 lines
+ *
mnl_attr_get_s8- returns 8-bit signed integer attribute payload
+ *
+ * This function returns the 8-bit value of the attribute payload.
+ */That's kinda redundant - it's logically the same thing, just written ever-so-slightly differently.
I guess I can fix the unsigned wording as well then. This is just a copy of that but converted to signed.
Quoted 11 lines
Quoted 6 lines
+/**
+ *mnl_attr_get_s64- returns 64-bit signed integer attribute. This
+ * function is align-safe, since accessing 64-bit Netlink attributes is a
+ * common source of alignment issues.That sentence is self-defeating. If NLA access is a commn source of alignment issues, then, by transitivity, this function too would be potentially affected. Just
This is specifically calling out the use of memcpy rather than just dereferencing the pointer address. The default NLA alignment is only 4bytes, not 8 bytes. This is exactly the same as the unsigned implementation, just copied to implement support for signed types.
Thanks,
Jake
Quoted 2 lines
"This function reads the 64-bit nlattr in an alignment-safe manner."
Hi Jacob,
Quoted 16 lines · Jan Engelhardt
Quoted 5 lines
+ *
mnl_attr_get_s8- returns 8-bit signed integer attribute payload
+ *
+ * This function returns the 8-bit value of the attribute payload.
+ */That's kinda redundant - it's logically the same thing, just written ever-so-slightly differently.
Quoted 5 lines
+/**
+ *mnl_attr_get_s64- returns 64-bit signed integer attribute. This
+ * function is align-safe, since accessing 64-bit Netlink attributes is a
+ * common source of alignment issues.
Quoted 7 lines
That sentence is self-defeating. If NLA access is a commn source of alignment issues, then, by transitivity, this function too would be potentially affected. Just
"This function reads the 64-bit nlattr in an alignment-safe manner."
Please use the following documentation style for *all* new functions. The man pages look better that way:
Quoted 4 lines
*
mnl_attr_get_s8- get 8-bit signed integer attribute payload
* \param attr pointer to netlink attribute
*
* \return 8-bit value of the attribute payload
I.e. "get" instead of "returns" in 1-line description; Use \return instead of "This function returns the"; No full stop at end of \return description.
Cheers ... Duncan.