]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/commitdiff
libnetlink: add more attribute functions
authorStephen Hemminger <shemminger@vyatta.com>
Fri, 23 Dec 2011 18:43:54 +0000 (10:43 -0800)
committerStephen Hemminger <shemminger@vyatta.com>
Fri, 23 Dec 2011 18:43:54 +0000 (10:43 -0800)
New functions to handle u8, u16, u32, u64 and string attribute types.
Use common code for all attribute wrappers.

include/libnetlink.h
lib/libnetlink.c

index 057f41d1544c129b19e6d27e16fb52d607e2b8a4..c54c7f6b180aa0326b372daacff244dc4999d375 100644 (file)
@@ -50,7 +50,13 @@ extern int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
 extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int);
 extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int);
 
+extern int addattr(struct nlmsghdr *n, int maxlen, int type);
+extern int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
+extern int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
 extern int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
+extern int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
+extern int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
+
 extern int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen);
 extern int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
 extern struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
index 7c29985c541ad8d4d216644af42c398feda52fb5..f7413b60f2b6a7272dbdafd4ab83d1422f9bb9e5 100644 (file)
@@ -530,20 +530,34 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
        }
 }
 
+int addattr(struct nlmsghdr *n, int maxlen, int type)
+{
+       return addattr_l(n, maxlen, type, NULL, 0);
+}
+
+int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
+{
+       return addattr_l(n, maxlen, type, &data, sizeof(__u8));
+}
+
+int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
+{
+       return addattr_l(n, maxlen, type, &data, sizeof(__u16));
+}
+
 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
 {
-       int len = RTA_LENGTH(4);
-       struct rtattr *rta;
-       if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
-               fprintf(stderr,"addattr32: Error! max allowed bound %d exceeded\n",maxlen);
-               return -1;
-       }
-       rta = NLMSG_TAIL(n);
-       rta->rta_type = type;
-       rta->rta_len = len;
-       memcpy(RTA_DATA(rta), &data, 4);
-       n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
-       return 0;
+       return addattr_l(n, maxlen, type, &data, sizeof(__u32));
+}
+
+int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
+{
+       return addattr_l(n, maxlen, type, &data, sizeof(__u64));
+}
+
+int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
+{
+       return addattr_l(n, maxlen, type, str, strlen(str)+1);
 }
 
 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,