    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 <hadi@cyberus.ca>
Acked-by: Daniel Lezcano <daniel.lezcano@free.fr>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>

---
 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(+)

Index: lxc/src/lxc/nl.c
===================================================================
--- lxc.orig/src/lxc/nl.c
+++ lxc/src/lxc/nl.c
@@ -78,6 +78,11 @@ extern int nla_put_u32(struct nlmsg *nlm
 	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_handle
 	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
 	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;
 	}
 	
Index: lxc/src/lxc/nl.h
===================================================================
--- lxc.orig/src/lxc/nl.h
+++ lxc/src/lxc/nl.h
@@ -161,6 +161,17 @@ int nla_put_buffer(struct nlmsg *nlmsg, 
 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 
  *
  * @nlmsg: the netlink message to be filled
Index: lxc/src/lxc/utils.c
===================================================================
--- lxc.orig/src/lxc/utils.c
+++ lxc/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;
+}
+
Index: lxc/src/lxc/utils.h
===================================================================
--- lxc.orig/src/lxc/utils.h
+++ lxc/src/lxc/utils.h
@@ -54,3 +54,4 @@ extern int lxc_copy_file(const char *src
 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);



