2017-01-13 3 views
0

libnl이 리눅스 커널에서 이웃 테이블을 가져올 수 있습니까? API rtnl_neightbl_get을 사용해 보았지만 캐시의 항목을 가져 오지 못했습니다. 인터페이스로 "lo"가있는 항목 만 가져옵니다. 어떻게 테이블 전체를 버릴 수 있습니까? 씨 라미 로젠리눅스에서 이웃 테이블 가져 오기

#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

이 말아야 작업의 요청에 따라
내가 libnl-3.2.25 여기
을 사용하고하면 코드입니다. pastebin과 같은 곳에 코드를 게시하고 링크를 보내서 검토 할 수 있습니까?

라미 로젠

+0

자세한 내용을 추가하기 위해 질문을 편집했습니다. – user7408158