6 #include <libmnl/libmnl.h>
8 #include <linux/if_link.h>
9 #include <linux/rtnetlink.h>
11 static int data_attr_cb(
const struct nlattr *attr,
void *data)
13 const struct nlattr **tb = data;
14 int type = mnl_attr_get_type(attr);
17 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
22 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
23 perror(
"mnl_attr_validate");
28 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
29 perror(
"mnl_attr_validate");
38 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
40 struct nlattr *tb[IFLA_MAX+1] = {};
41 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
43 printf(
"index=%d type=%d flags=%d family=%d ",
44 ifm->ifi_index, ifm->ifi_type,
45 ifm->ifi_flags, ifm->ifi_family);
47 if (ifm->ifi_flags & IFF_RUNNING)
50 printf(
"[NOT RUNNING] ");
52 mnl_attr_parse(nlh,
sizeof(*ifm), data_attr_cb, tb);
54 printf(
"mtu=%d ", mnl_attr_get_u32(tb[IFLA_MTU]));
56 if (tb[IFLA_IFNAME]) {
57 printf(
"name=%s", mnl_attr_get_str(tb[IFLA_IFNAME]));
66 char buf[MNL_SOCKET_BUFFER_SIZE];
69 nl = mnl_socket_open(NETLINK_ROUTE);
71 perror(
"mnl_socket_open");
75 if (mnl_socket_bind(nl, RTMGRP_LINK, MNL_SOCKET_AUTOPID) < 0) {
76 perror(
"mnl_socket_bind");
80 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
82 ret = mnl_cb_run(buf, ret, 0, 0, data_cb, NULL);
85 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));