2017-01-13 16 views
0

Ermöglicht libnl das Abrufen der Nachbartabelle vom Linux-Kernel? Ich habe versucht, die API rtnl_neightbl_get, aber ich kann nicht die Einträge im Cache abrufen. Nur die Einträge mit "lo" als Schnittstelle werden abgerufen. Wie kann ich die gesamte Tabelle löschen?
Ich bin mit libnl-3.2.25
Hier ist der Code wie gewünscht von Herrn Rami RosenFetch-Nachbartabelle von Linux

#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;                  
} 

Antwort

0

Diese shoul Arbeit. Können Sie Ihren Code irgendwo pastebin posten und einen Link senden, damit wir ihn überprüfen können?

Rami Rosen

+0

Ich habe die Frage bearbeitet, um weitere Details hinzuzufügen. – user7408158