From 9ddaf3bf1cc264af0eee60b37cd7c7a3ddcb6825 Mon Sep 17 00:00:00 2001 From: Jamal Hadi Salim Date: Tue, 15 Dec 2009 10:14:26 +0100 Subject: [PATCH] Add utility u16 get/put Add utility functions to parse a u16 and put a u16 on a netlink message Signed-off-by: Jamal Hadi Salim Acked-by: Daniel Lezcano Signed-off-by: Daniel Lezcano --- src/lxc/nl.c | 10 ++++++++++ src/lxc/nl.h | 11 +++++++++++ src/lxc/utils.c | 19 +++++++++++++++++++ src/lxc/utils.h | 1 + 4 files changed, 41 insertions(+) diff --git a/src/lxc/nl.c b/src/lxc/nl.c index 261ffb527..b5d3dd0f4 100644 --- a/src/lxc/nl.c +++ b/src/lxc/nl.c @@ -78,6 +78,11 @@ extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int 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) { return nla_put(nlmsg, attr, NULL, 0); @@ -184,6 +189,9 @@ extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg) return ret; } +#ifndef NLMSG_ERROR +#define NLMSG_ERROR 0x2 +#endif extern int netlink_transaction(struct nl_handler *handler, struct nlmsg *request, struct nlmsg *answer) { @@ -201,6 +209,8 @@ extern int netlink_transaction(struct nl_handler *handler, if (answer->nlmsghdr.nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer); errno = -err->error; + if (errno) + perror("Error configuring kernel"); return -errno; } diff --git a/src/lxc/nl.h b/src/lxc/nl.h index 2f760de2c..864780a5a 100644 --- a/src/lxc/nl.h +++ b/src/lxc/nl.h @@ -160,6 +160,17 @@ int nla_put_buffer(struct nlmsg *nlmsg, int attr, */ 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 * diff --git a/src/lxc/utils.c b/src/lxc/utils.c index f9477a347..1869c0cb7 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -270,3 +270,22 @@ extern int lxc_setup_fs(void) 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; +} + diff --git a/src/lxc/utils.h b/src/lxc/utils.h index cb4e6a0bb..c49c4ffa5 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -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_all_inherited_fd(void); extern int lxc_setup_fs(void); +extern int get_u16(ushort *val, const char *arg, int base); -- 2.39.5