Add a Netlink utility function to add a RTA header and reserve space for data

This commit is contained in:
Martin Willi
2013-03-15 14:32:51 +01:00
parent 53c98f098f
commit 6359ab04f4
2 changed files with 32 additions and 0 deletions
@@ -304,3 +304,24 @@ void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
memcpy(RTA_DATA(rta), data.ptr, data.len);
hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
}
/**
* Described in header.
*/
void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len)
{
struct rtattr *rta;
if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_LENGTH(len) > buflen)
{
DBG1(DBG_KNL, "unable to add attribute, buffer too small");
return NULL;
}
rta = ((void*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len);
rta->rta_type = type;
rta->rta_len = RTA_LENGTH(len);
hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
return RTA_DATA(rta);
}
@@ -76,4 +76,15 @@ netlink_socket_t *netlink_socket_create(int protocol);
void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
size_t buflen);
/**
* Reserve space in a netlink message for given size and type, returning buffer.
*
* @param hdr netlink message
* @param buflen size of full netlink buffer
* @param type RTA type
* @param len length of RTA data
* @return buffer to len bytes of attribute data, NULL on error
*/
void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len);
#endif /* KERNEL_NETLINK_SHARED_H_ */