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