Commit 9ddaf3bf by Jamal Hadi Salim Committed by Daniel Lezcano

Add utility u16 get/put

Add utility functions to parse a u16 and put a u16 on a netlink message Signed-off-by: 's avatarJamal Hadi Salim <hadi@cyberus.ca> Acked-by: 's avatarDaniel Lezcano <daniel.lezcano@free.fr> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 108ed092
...@@ -78,6 +78,11 @@ extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value) ...@@ -78,6 +78,11 @@ extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value)
return nla_put(nlmsg, attr, &value, sizeof(value)); return nla_put(nlmsg, attr, &value, sizeof(value));
} }
extern int nla_put_u16(struct nlmsg *nlmsg, int attr, ushort value)
{
return nla_put(nlmsg, attr, &value, 2);
}
extern int nla_put_attr(struct nlmsg *nlmsg, int attr) extern int nla_put_attr(struct nlmsg *nlmsg, int attr)
{ {
return nla_put(nlmsg, attr, NULL, 0); return nla_put(nlmsg, attr, NULL, 0);
...@@ -184,6 +189,9 @@ extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg) ...@@ -184,6 +189,9 @@ extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg)
return ret; return ret;
} }
#ifndef NLMSG_ERROR
#define NLMSG_ERROR 0x2
#endif
extern int netlink_transaction(struct nl_handler *handler, extern int netlink_transaction(struct nl_handler *handler,
struct nlmsg *request, struct nlmsg *answer) struct nlmsg *request, struct nlmsg *answer)
{ {
...@@ -201,6 +209,8 @@ extern int netlink_transaction(struct nl_handler *handler, ...@@ -201,6 +209,8 @@ extern int netlink_transaction(struct nl_handler *handler,
if (answer->nlmsghdr.nlmsg_type == NLMSG_ERROR) { if (answer->nlmsghdr.nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer); struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer);
errno = -err->error; errno = -err->error;
if (errno)
perror("Error configuring kernel");
return -errno; return -errno;
} }
......
...@@ -161,6 +161,17 @@ int nla_put_buffer(struct nlmsg *nlmsg, int attr, ...@@ -161,6 +161,17 @@ int nla_put_buffer(struct nlmsg *nlmsg, int attr,
int nla_put_u32(struct nlmsg *nlmsg, int attr, int value); int nla_put_u32(struct nlmsg *nlmsg, int attr, int value);
/* /*
* nla_put_u16: copy an integer to a netlink message attribute
*
* @nlmsg: the netlink message to be filled
* @attr: the attribute name of the unsigned 16-bit value
* @value: 16-bit attribute data value to be copied to the netlink message
*
* Returns 0 on success, < 0 otherwise
*/
int nla_put_u16(struct nlmsg *nlmsg, int attr, ushort value);
/*
* nla_put_attr: add an attribute name to a netlink * nla_put_attr: add an attribute name to a netlink
* *
* @nlmsg: the netlink message to be filled * @nlmsg: the netlink message to be filled
......
...@@ -270,3 +270,22 @@ extern int lxc_setup_fs(void) ...@@ -270,3 +270,22 @@ extern int lxc_setup_fs(void)
return 0; return 0;
} }
/* borrowed from iproute2 */
extern int get_u16(ushort *val, const char *arg, int base)
{
unsigned long res;
char *ptr;
if (!arg || !*arg)
return -1;
res = strtoul(arg, &ptr, base);
if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
return -1;
*val = res;
return 0;
}
...@@ -54,3 +54,4 @@ extern int lxc_copy_file(const char *src, const char *dst); ...@@ -54,3 +54,4 @@ extern int lxc_copy_file(const char *src, const char *dst);
extern int lxc_close_inherited_fd(int fd); extern int lxc_close_inherited_fd(int fd);
extern int lxc_close_all_inherited_fd(void); extern int lxc_close_all_inherited_fd(void);
extern int lxc_setup_fs(void); extern int lxc_setup_fs(void);
extern int get_u16(ushort *val, const char *arg, int base);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment