使用libnl显示接口信息

来源:互联网 发布:单片机的组成 编辑:程序博客网 时间:2024/05/13 07:55

参考文档:

http://www.carisma.slowglass.com/~tgr/libnl/doc/api/group__link.html

 

http://www.carisma.slowglass.com/~tgr/libnl/doc/route.html

 

#include <stdio.h>#include <netlink/netlink.h>#include <netlink/route/link.h>static void stat_print(struct rtnl_link *link){printf("RTNL_LINK_TX_PACKETS %llu\n",       rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS));printf("RTNL_LINK_RX_PACKETS %llu\n",       rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS));printf("RTNL_LINK_IP6_INPKTS %llu\n",       rtnl_link_get_stat(link, RTNL_LINK_IP6_INPKTS));printf("RTNL_LINK_IP6_OUTPKTS %llu\n",       rtnl_link_get_stat(link, RTNL_LINK_IP6_OUTPKTS));}static void flags_print(struct rtnl_link *link){unsigned int flags = rtnl_link_get_flags(link);printf("flags: 0x%x\n", flags);printf("IFF_UP ");if (flags & IFF_UP) {printf("is set\n");} else {printf("is not set\n");}printf("IFF_RUNNING ");if (flags & IFF_RUNNING) {printf("is set\n");} else {printf("is not set\n");}printf("IFF_PROMISC ");if (flags & IFF_PROMISC) {printf("is set\n");} else {printf("is not set\n");}}static void addr_print(struct rtnl_link *link){char buf[4096] = {[0 ... (sizeof(buf) - 1)] = 0, };struct nl_addr *addr = rtnl_link_get_addr(link);nl_addr2str(addr, buf, sizeof(buf) - 1);printf("%s\n", buf);struct nl_addr *broadcast = rtnl_link_get_broadcast(link);nl_addr2str(broadcast, buf, sizeof(buf) - 1);printf("%s\n", buf);}static void misc_print(struct rtnl_link *link){printf("name: %s\n", rtnl_link_get_name(link));printf("group: %d\n", rtnl_link_get_group(link));printf("family: %d\n", rtnl_link_get_family(link));printf("arptype: %d\n", rtnl_link_get_arptype(link));printf("ifindex: %d\n", rtnl_link_get_ifindex(link));printf("mtu: %d\n", rtnl_link_get_mtu(link));printf("txqlen: %d\n", rtnl_link_get_txqlen(link));printf("master: %d\n", rtnl_link_get_master(link));printf("carrier: %d\n", rtnl_link_get_carrier(link));printf("operstate: %d\n", rtnl_link_get_operstate(link));printf("linkmode: %d\n", rtnl_link_get_linkmode(link));printf("qdisc: %s\n", rtnl_link_get_qdisc(link));printf("type: %s\n", rtnl_link_get_type(link));printf("promiscuity: %d\n", rtnl_link_get_promiscuity(link));printf("num_tx_queues: %d\n", rtnl_link_get_num_tx_queues(link));printf("num_rx_queues: %d\n", rtnl_link_get_num_rx_queues(link));}int main(int argc, char *argv[]){int ret = 0;char *ifname = "lo";struct nl_sock *sk = NULL;struct nl_cache *cache = NULL;struct rtnl_link *link = NULL;if (argc != 2) {printf("Usage %s <interface>\n", argv[0]);return -1;}ifname = argv[1];printf("Interface %s\n", ifname);sk = nl_socket_alloc();if (sk == NULL) {printf("nl_socket_alloc error\n");goto error_socket;}ret = nl_connect(sk, NETLINK_ROUTE);if (ret != 0) {printf("nl_connect error\n");goto error_connect;}ret = rtnl_link_alloc_cache(sk, AF_UNSPEC, &cache);if (ret != 0) {printf("rtnl_link_alloc_cache error\n");goto error_cache;}link = rtnl_link_get_by_name(cache, ifname);if (link == NULL) {printf("rtnl_link_get_by_name error\n");goto error_link;}misc_print(link);addr_print(link);flags_print(link);stat_print(link);rtnl_link_put(link);error_link:nl_cache_free(cache);error_cache:nl_close(sk);error_connect:nl_socket_free(sk);error_socket:return ret;}


 

$ gcc -Wall n1.c $(pkg-config --cflags --libs libnl-3.0 libnl-route-3.0)$ ./a.out loInterface loname: logroup: 0family: 0arptype: 772ifindex: 1mtu: 16436txqlen: 0master: 0carrier: 0operstate: 0linkmode: 0qdisc: noqueuetype: (null)promiscuity: 0num_tx_queues: 0num_rx_queues: 000:00:00:00:00:0000:00:00:00:00:00flags: 0x10049IFF_UP is setIFF_RUNNING is setIFF_PROMISC is not setRTNL_LINK_TX_PACKETS 2574RTNL_LINK_RX_PACKETS 2574RTNL_LINK_IP6_INPKTS 0RTNL_LINK_IP6_OUTPKTS 0


 

原创粉丝点击