libnl是否允许从linux内核获取邻居表?
我尝试使用API rtnl_neightbl_get但我无法获取缓存中的条目。只有" lo"因为他们的界面被提取。我该如何倾倒整张桌子?
我正在使用 libnl-3.2.25
以下是Rami Rosen先生要求的代码
#include <stdio.h>
#include <errno.h>
#include <netlink/types.h>
#include <netlink/utils.h>
#include <netlink/route/link.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/data.h>
#include <netlink/addr.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
struct nl_sock *sock;
int main(int argc, char **argv)
{
int err;
struct nl_cache *neightbl_cache = NULL;
struct rtnl_neightbl *neightbl = NULL;
struct rtnl_neightbl *modified_neightbl = NULL;
int ret_code;
int stale_time = 50;
// Allocate a new netlink socket
sock = nl_socket_alloc();
if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0)
{
nl_perror(err, "Unable to connect socket");
return err;
goto EXIT_LABEL;
}
/************************************************************************//**
* Fetch the neighbor table cache from OS.
***************************************************************************/
ret_code = rtnl_neightbl_alloc_cache(sock,
&neightbl_cache);
if (ret_code < 0)
{
printf("Failed to fetch the neighbor table cache.");
goto EXIT_LABEL;
}
printf("Fetched neighbor table cache.");
/************************************************************************//**
* Allocate a neighbor table object for setting stale time
***************************************************************************/
modified_neightbl = rtnl_neightbl_alloc();
if (modified_neightbl == NULL)
{
printf("Failed to allocate a neighbor table object.");
goto EXIT_LABEL;
}
/************************************************************************//**
* Fetched neighbor table object from the neighbor table cache.
***************************************************************************/
char *table;
table = (char *)malloc(sizeof(char) * 16);
strcpy(table, "arp_cache");
neightbl = rtnl_neightbl_get(neightbl_cache, table, 0);
if (neightbl == NULL)
{
printf("Failed to fetch the neigihbor table object.");
goto EXIT_LABEL;
}
memcpy(modified_neightbl, neightbl, sizeof(struct rtnl_neightbl));
/************************************************************************//**
* Update the stale time in the neighbor table
***************************************************************************/
rtnl_neightbl_set_gc_stale_time(modified_neightbl, stale_time);
ret_code = rtnl_neightbl_change(sock, neightbl,
modified_neightbl);
if (ret_code < 0)
{
printf("\n %d\n",errno);
printf("Failed to update the stale time.");
goto EXIT_LABEL;
}
printf("Updated stale time = %lu.", stale_time);
nl_close(sock);
EXIT_LABEL:
return 0;
}
答案 0 :(得分:0)
这是有效的。你可以在像pastebin这样的地方发布你的代码,并发送一个链接,以便我们能够查看它吗?
拉米罗森