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