从 Linux 获取邻居表



libnl 是否允许从 Linux 内核获取邻居表?我尝试使用 API rtnl_neightbl_get但无法获取缓存中的条目。仅获取以"lo"作为其接口的条目。如何转储整个表?
我正在使用 libnl-3.2.25
这是拉米·罗森先生要求的代码

#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 %dn",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;                                                                     
}

这个 shoul 工作。你能把你的代码发布在像pastebin这样的地方,并发送一个链接,以便我们能够审查它吗?

拉米·罗森

最新更新