libmnl  1.0.3
rtnl-link-dump3.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_cb(const struct nlmsghdr *nlh, void *data)
00013 {
00014         struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
00015         struct nlattr *attr;
00016 
00017         printf("index=%d type=%d flags=%d family=%d ", 
00018                 ifm->ifi_index, ifm->ifi_type,
00019                 ifm->ifi_flags, ifm->ifi_family);
00020 
00021         if (ifm->ifi_flags & IFF_RUNNING)
00022                 printf("[RUNNING] ");
00023         else
00024                 printf("[NOT RUNNING] ");
00025 
00026         mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
00027                 int type = mnl_attr_get_type(attr);
00028 
00029                 /* skip unsupported attribute in user-space */
00030                 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
00031                         continue;
00032 
00033                 switch(type) {
00034                 case IFLA_MTU:
00035                         if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
00036                                 perror("mnl_attr_validate");
00037                                 return MNL_CB_ERROR;
00038                         }
00039                         printf("mtu=%d ", mnl_attr_get_u32(attr));
00040                         break;
00041                 case IFLA_IFNAME:
00042                         if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
00043                                 perror("mnl_attr_validate");
00044                                 return MNL_CB_ERROR;
00045                         }
00046                         printf("name=%s ", mnl_attr_get_str(attr));
00047                         break;
00048                 }
00049         }
00050         printf("\n");
00051 
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 }