7 #include <libmnl/libmnl.h>
9 #include <linux/if_link.h>
10 #include <linux/rtnetlink.h>
12 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
14 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
17 printf(
"index=%d type=%d flags=%d family=%d ",
18 ifm->ifi_index, ifm->ifi_type,
19 ifm->ifi_flags, ifm->ifi_family);
21 if (ifm->ifi_flags & IFF_RUNNING)
24 printf(
"[NOT RUNNING] ");
26 mnl_attr_for_each(attr, nlh,
sizeof(*ifm)) {
27 int type = mnl_attr_get_type(attr);
30 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
35 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
36 perror(
"mnl_attr_validate");
39 printf(
"mtu=%d ", mnl_attr_get_u32(attr));
42 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
43 perror(
"mnl_attr_validate");
46 printf(
"name=%s ", mnl_attr_get_str(attr));
58 char buf[MNL_SOCKET_BUFFER_SIZE];
62 unsigned int seq, portid;
64 nlh = mnl_nlmsg_put_header(buf);
65 nlh->nlmsg_type = RTM_GETLINK;
66 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
67 nlh->nlmsg_seq = seq = time(NULL);
68 rt = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct rtgenmsg));
69 rt->rtgen_family = AF_PACKET;
71 nl = mnl_socket_open(NETLINK_ROUTE);
73 perror(
"mnl_socket_open");
77 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
78 perror(
"mnl_socket_bind");
81 portid = mnl_socket_get_portid(nl);
83 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
84 perror(
"mnl_socket_sendto");
88 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
90 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
91 if (ret <= MNL_CB_STOP)
93 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
100 mnl_socket_close(nl);