Skip to content

Commit

Permalink
lib/attr: add nla functions for variable-length integers
Browse files Browse the repository at this point in the history
The NLA_{UINT|SINT} attributes are introduced for variable-length
integers, whose length are 32bits or 64bits.

So add appropriate functions to get or put NLA_{UINT|SINT} attributes.

Signed-off-by: Wen Gu <[email protected]>
  • Loading branch information
Wen Gu committed Aug 17, 2024
1 parent 2ae88c4 commit b8d3cfb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/netlink/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ extern int64_t nla_get_s64(const struct nlattr *);
extern int nla_put_s64(struct nl_msg *, int, int64_t);
extern uint64_t nla_get_u64(const struct nlattr *);
extern int nla_put_u64(struct nl_msg *, int, uint64_t);
extern int64_t nla_get_sint(const struct nlattr *);
extern int nla_put_sint(struct nl_msg *, int, int64_t);
extern uint64_t nla_get_uint(const struct nlattr *);
extern int nla_put_uint(struct nl_msg *, int, uint64_t);

/* String attribute */
extern char * nla_get_string(const struct nlattr *);
Expand Down
64 changes: 64 additions & 0 deletions lib/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,70 @@ uint64_t nla_get_u64(const struct nlattr *nla)
return tmp;
}

/**
* Add variable-length signed integer attribute to netlink message.
* @arg msg Netlink message.
* @arg attrtype Attribute type.
* @arg value Numeric value to store as payload.
*
* @see nla_put
* @return 0 on success or a negative error code.
*/
int nla_put_sint(struct nl_msg *msg, int attrtype, int64_t value)
{
int64_t tmp64 = value;
int32_t tmp32 = value;

if (tmp64 == tmp32)
return nla_put_s32(msg, attrtype, tmp32);
return nla_put(msg, attrtype, sizeof(int64_t), &value);
}

/**
* Return payload of variable-length signed integer attribute
* @arg nla variable-length signed integer attribute
*
* @return Payload as 64 bit integer.
*/
int64_t nla_get_sint(const struct nlattr *nla)
{
if (nla && _nla_len(nla) == sizeof(int32_t))
return nla_get_s32(nla);
return nla_get_s64(nla);
}

/**
* Add variable-length unsigned integer attribute to netlink message.
* @arg msg Netlink message.
* @arg attrtype Attribute type.
* @arg value Numeric value to store as payload.
*
* @see nla_put
* @return 0 on success or a negative error code.
*/
int nla_put_uint(struct nl_msg *msg, int attrtype, uint64_t value)
{
uint64_t tmp64 = value;
uint32_t tmp32 = value;

if (tmp64 == tmp32)
return nla_put_u32(msg, attrtype, tmp32);
return nla_put(msg, attrtype, sizeof(uint64_t), &value);
}

/**
* Return payload of variable-length unsigned integer attribute
* @arg nla variable-length unsigned integer attribute
*
* @return Payload as 64 bit integer.
*/
uint64_t nla_get_uint(const struct nlattr *nla)
{
if (nla && _nla_len(nla) == sizeof(uint32_t))
return nla_get_u32(nla);
return nla_get_u64(nla);
}

/** @} */

/**
Expand Down
8 changes: 8 additions & 0 deletions libnl-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,11 @@ libnl_3_10 {
global:
nl_cache_mngr_alloc_ex;
} libnl_3_6;

libnl_3_11 {
global:
nla_get_sint;
nla_put_sint;
nla_get_uint;
nla_put_uint;
} libnl_3_10;

0 comments on commit b8d3cfb

Please sign in to comment.