Manipulating ARP tables in code

Hi All.

I am having trouble manipulating the arp tables in code. SIOCGARP always
returns with “No such device or address”. Following is the code listing.
Can anyone advise what I am doing wrong?

Thanks,
Stuart

#include <stdio.h>
#include <errno.h>
#include <ioctl.h>
#include <net/route.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main (int argc, char **argv)
{
struct arpreq arp_req;
int s;
struct sockaddr_in *sin;

memset (&arp_req, 0, sizeof(arp_req));
sin = (struct sockaddr_in *) &arp_req.arp_pa;

if (argc > 1)
{
if (!inet_aton(argv[1], &sin->sin_addr))
perror(“inet_aton”); // = inet_addr(argv[1]);
}
else
exit(0);

sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);

arp_req.arp_flags = ATF_COM;

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror (“socket”);
}

if (ioctl(s, SIOCGARP, (caddr_t) &arp_req) == -1)
{
perror(“SIOCGARP”);
exit (0);
}

// Print the associated hardware address
printf("%x:%x:%x:%x:%x:%x\n",
arp_req.arp_ha.sa_data[0],arp_req.arp_ha.sa_data[1],
arp_req.arp_ha.sa_data[2],arp_req.arp_ha.sa_data[3],
arp_req.arp_ha.sa_data[4],arp_req.arp_ha.sa_data[5]);

close(s);

}

Stuart Harding <stuart@intellidesign.com.au> wrote:
: Hi All.

: I am having trouble manipulating the arp tables in code. SIOCGARP always
: returns with “No such device or address”. Following is the code listing.
: Can anyone advise what I am doing wrong?

The SIOC[SG]ARP ioctls work on the /dev/io-net/ip_en device:



#include <stdio.h>
#include <errno.h>
#include <ioctl.h>
#include <net/route.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if_dl.h>

int main (int argc, char **argv)
{
struct arpreq arp_req;
int s;
struct sockaddr_in *sin;
struct sockaddr_dl *sdl;
unsigned char *c;

memset (&arp_req, 0, sizeof(arp_req));
sin = (struct sockaddr_in *) &arp_req.arp_pa;

if (argc > 1)
{
if (!inet_aton(argv[1], &sin->sin_addr))
perror(“inet_aton”); // = inet_addr(argv[1]);
}
else
exit(0);

sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);

arp_req.arp_flags = ATF_COM;


if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror (“socket”);
}

s = open("/dev/io-net/ip_en", 0);

if (ioctl(s, SIOCGARP, (caddr_t) &arp_req) == -1)
{
perror(“SIOCGARP”);
exit (0);
}

sdl = (struct sockaddr_dl *)&arp_req.arp_ha;
c = sdl->sdl_data + sdl->sdl_nlen;
// Print the associated hardware address
printf("%x:%x:%x:%x:%x:%x\n",
c[0], c[1], c[2], c[3], c[4], c[5]);

close(s);

}