path: root/examples/rtnl-link-dump.c
diff options
authorPablo Neira Ayuso <>2010-04-03 08:29:16 +0200
committerPablo Neira Ayuso <>2010-04-03 13:26:44 +0200
commit8ce5d4ca70884654988eb86734cb3022e0b71995 (patch)
treea42c6a5b87d5b6c2ff01446f127824e355de3ee8 /examples/rtnl-link-dump.c
parentba57ffc48d3a97421c8358947bc8cf9f2e7ff7c6 (diff)
add validation infrastructure and rework attribute parsing
This patch includes the new validation infrastructure which is decoupled from the attribute parsing. It is composed of: - mnl_attr_type_invalid: that allows to check if the attribute type is valid (ie. the type is not higher than WXYZ_MAX). - mnl_attr_validate: that allows to validate that there's enough room for the attribute data. The patch includes the rework of the attribute parsers. Now, you don't have to use an array of pointer to store the result of the parsing, you can use whatever data structure instead. The prototype as it follows: typedef int (*mnl_attr_cb_t)(const struct nlattr *attr, void *data); extern int mnl_attr_parse(const struct nlmsghdr *nlh, int offset, mnl_attr_cb_t cb, void *data) There are three versions of rtnl-link-dump.c that show how attribute parsing can be done now. Probably that many examples are not good idea, I may remove some of them from the tree in the future. This patch also merges mnl_attr_parse_at_offset into mnl_attr_parse. This patch modifies MNL_ALIGN so that we can use it in static arrays (the use of mnl_align() is not allowed in compilation time to initialize an array field). I have added the mnl_attr_for_each() macro and I have changed mnl_attr_for_each_nested() to declare the length variable internally. Signed-off-by: Pablo Neira Ayuso <>
Diffstat (limited to 'examples/rtnl-link-dump.c')
1 files changed, 30 insertions, 2 deletions
diff --git a/examples/rtnl-link-dump.c b/examples/rtnl-link-dump.c
index 7cf061d..9e3f114 100644
--- a/examples/rtnl-link-dump.c
+++ b/examples/rtnl-link-dump.c
@@ -7,9 +7,37 @@
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
+static int data_attr_cb(const struct nlattr *attr, void *data)
+ const struct nlattr **tb = (const struct nlattr **)data;
+ int type = mnl_attr_get_type(attr);
+ if (mnl_attr_type_invalid(attr, IFLA_MAX) < 0) {
+ perror("mnl_attr_type_invalid");
+ return MNL_CB_ERROR;
+ }
+ switch(type) {
+ case IFLA_MTU:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
+ perror("mnl_attr_validate2");
+ return MNL_CB_ERROR;
+ }
+ break;
+ }
+ tb[type] = attr;
+ return MNL_CB_OK;
static int data_cb(const struct nlmsghdr *nlh, void *data)
- struct nlattr *tb[IFLA_MAX+1];
+ struct nlattr *tb[IFLA_MAX+1] = {};
struct ifinfomsg *ifm = mnl_nlmsg_get_data(nlh);
int len = mnl_nlmsg_get_len(nlh);
struct nlattr *attr;
@@ -23,7 +51,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
printf("[NOT RUNNING] ");
- mnl_attr_parse_at_offset(nlh, sizeof(*ifm), tb, IFLA_MAX);
+ mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
if (tb[IFLA_MTU]) {
printf("mtu=%d ", mnl_attr_get_u32(tb[IFLA_MTU]));