• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

rtnl-link-dump.c

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

Generated on Sun Dec 26 2010 22:23:55 for libmnl by  doxygen 1.7.1