libmnl  1.0.3
rtnl-link-dump2.c
00001 /* This example is placed in the public domain. */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <time.h>
00006 
00007 #include <libmnl/libmnl.h>
00008 #include <linux/if.h>
00009 #include <linux/if_link.h>
00010 #include <linux/rtnetlink.h>
00011 
00012 static int data_attr_cb(const struct nlattr *attr, void *data)
00013 {
00014         /* skip unsupported attribute in user-space */
00015         if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
00016                 return MNL_CB_OK;
00017 
00018         switch(mnl_attr_get_type(attr)) {
00019         case IFLA_MTU:
00020                 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
00021                         perror("mnl_attr_validate");
00022                         return MNL_CB_ERROR;
00023                 }
00024                 printf("mtu=%d ", mnl_attr_get_u32(attr));
00025                 break;
00026         case IFLA_IFNAME:
00027                 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
00028                         perror("mnl_attr_validate2");
00029                         return MNL_CB_ERROR;
00030                 }
00031                 printf("name=%s ", mnl_attr_get_str(attr));
00032                 break;
00033         }
00034         return MNL_CB_OK;
00035 }
00036 
00037 static int data_cb(const struct nlmsghdr *nlh, void *data)
00038 {
00039         struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
00040 
00041         printf("index=%d type=%d flags=%d family=%d ", 
00042                 ifm->ifi_index, ifm->ifi_type,
00043                 ifm->ifi_flags, ifm->ifi_family);
00044 
00045         if (ifm->ifi_flags & IFF_RUNNING)
00046                 printf("[RUNNING] ");
00047         else
00048                 printf("[NOT RUNNING] ");
00049 
00050         mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, NULL);
00051         printf("\n");
00052         return MNL_CB_OK;
00053 }
00054 
00055 int main(void)
00056 {
00057         struct mnl_socket *nl;
00058         char buf[MNL_SOCKET_BUFFER_SIZE];
00059         struct nlmsghdr *nlh;
00060         struct rtgenmsg *rt;
00061         int ret;
00062         unsigned int seq, portid;
00063 
00064         nlh = mnl_nlmsg_put_header(buf);
00065         nlh->nlmsg_type = RTM_GETLINK;
00066         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
00067         nlh->nlmsg_seq = seq = time(NULL);
00068         rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
00069         rt->rtgen_family = AF_PACKET;
00070 
00071         nl = mnl_socket_open(NETLINK_ROUTE);
00072         if (nl == NULL) {
00073                 perror("mnl_socket_open");
00074                 exit(EXIT_FAILURE);
00075         }
00076 
00077         if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00078                 perror("mnl_socket_bind");
00079                 exit(EXIT_FAILURE);
00080         }
00081         portid = mnl_socket_get_portid(nl);
00082 
00083         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00084                 perror("mnl_socket_send");
00085                 exit(EXIT_FAILURE);
00086         }
00087 
00088         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00089         while (ret > 0) {
00090                 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
00091                 if (ret <= MNL_CB_STOP)
00092                         break;
00093                 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00094         }
00095         if (ret == -1) {
00096                 perror("error");
00097                 exit(EXIT_FAILURE);
00098         }
00099 
00100         mnl_socket_close(nl);
00101 
00102         return 0;
00103 }