Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nla_rewrite() function #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,42 @@ int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
nl_addr_get_binary_addr(addr));
}

/**
* Rewrite an unspecific attribute of netlink message.
* @arg head Head of attributes stream.
* @arg len Length of attributes stream.
* @arg attrtype Attribute type.
* @arg datalen Length of data to be used as payload.
* @arg data Pointer to data to be used as attribute payload.
*
* Rewrites payload of first existing attribute of given type. Returns an error
* if attribute of given type not found of the existing attribute's payload length
* is not equal to new payload length or given null head of attributes stream.
*
* @return 0 on success or a negative error code.
*/
int nla_rewrite(struct nlattr *head, int len, int attrtype, int datalen, const void *data)
{
struct nlattr *nla;

if (!head)
return -NLE_INVAL;

nla = nla_find(head, len, attrtype);

if (!nla)
return -NLE_OBJ_NOTFOUND;

if (datalen > 0 && datalen < nla_len(nla)) {
memcpy(nla_data(nla), data, datalen);
NL_DBG(2, "attr <%p> %d: Wrote %d bytes at offset +%td\n",
nla, nla->nla_type, datalen,
(void *) nla - nlmsg_data(msg->nm_nlh));
}

return 0;
}

/** @} */

/**
Expand Down