From: Stephen Hemminger Date: Fri, 23 Dec 2011 18:43:54 +0000 (-0800) Subject: libnetlink: add more attribute functions X-Git-Url: http://rtime.felk.cvut.cz/gitweb/lisovros/iproute2_canprio.git/commitdiff_plain/2aa3dd29a75c494bf969586da5489d4dc7d07839 libnetlink: add more attribute functions New functions to handle u8, u16, u32, u64 and string attribute types. Use common code for all attribute wrappers. --- diff --git a/include/libnetlink.h b/include/libnetlink.h index 057f41d..c54c7f6 100644 --- a/include/libnetlink.h +++ b/include/libnetlink.h @@ -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); diff --git a/lib/libnetlink.c b/lib/libnetlink.c index 7c29985..f7413b6 100644 --- a/lib/libnetlink.c +++ b/lib/libnetlink.c @@ -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,