[Olsr-users] olsr.org olsrd and nrlsmf

Roar Bjørgum Rotvik (spam-protected)
Tue Aug 19 09:18:55 CEST 2008


Bernd Petrovitsch wrote:
> On Mon, 2008-08-18 at 17:58 +0200, Henning Rogge wrote:
> [....]
>> The linux stack already "knows" the MAC address, it's only the problem
>> to get the information.
> 
> strace'ing "arp" suggests looking into /proc/net/arp

Or just use ioctl(), here is a short example:

Input is IPv4 address (ip argument), returned MAC is in hw_addr as a 6 byte unsigned char 
array. Flags is for any arp-flags.

bool arp_get (const in_addr_t ip, unsigned char *hw_addr, int *flags)
{
    int                 ret;
    int                 fd;
    struct arpreq       arpreq = {0};
    struct sockaddr_in *sin = NULL;

    fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (fd == -1) {
       return false;
    }
    sin = (struct sockaddr_in *)&arpreq.arp_pa;
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = ip;

    ret = ioctl (fd, SIOCGARP, &arpreq);
    if (ret == -1) {
       return false;
    }
    memcpy (hw_addr, &arpreq.arp_ha.sa_data[0], 6);
    if (flags) {
       *flags = arpreq.arp_flags;
    }
    close (fd);
    return true;
}




More information about the Olsr-users mailing list