Getting ARP cache entry

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




I had been trying this for sometime.. but still my problem remains.

I am trying to broadcast an ARP REQUEST to get the MAC address of a remote
machine. The sequence is as follows:
First, i broadcast the ARP request.
Assuming that my ARP cache gets updated with the ARP response, I am using
the ioctl () call to retrieve the MAC address of the destination m/c

I am att.'ing the code for reference.

This solution is giving inconsistant results. Sometimes the ioctl () call
fails saying "no such device or address".

After going thru the problem, i observed that, if no entries are availabe in
cache (no entries displayed with /sbin/arp -n command), then I get the above
error.

if I try pinging the remote machine, so that i get the entry in cache, then
my ioctl() works fine.  (this defeats my original purpose though:) )
It works fine even if i delete the entry in cache with arp -d. (ofcourse,
this just shows 'incomplete' in arp cache for the remote m/c entry)

Wondering if i am doing anything wrong here.
What could be the problem? What is the best way to do this?
Any help would be appreciated.

-Chuks

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/if_ether.h>
#include <sys/ioctl.h>

#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif

void send_arp_req(char *);

int main ()
{ 
  int sd;

  struct arpreq      arpreq;
  struct sockaddr_in *sin;
  struct in_addr     ina; 
  char         ip[] = "10.8.5.39";
  unsigned char      *hw_addr;

  int rc;

  sd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sd < 0)
  {
    perror("socket() error\n");
    exit(1);
  }

  /* Try to find an entry in arp cache for the ip address specified */
  
  printf("Find arp entry for IP : %s\n", ip);
  
  sin = (struct sockaddr_in *) &arpreq.arp_pa;
  memset(sin, 0, sizeof(struct sockaddr_in));
  sin->sin_family = AF_INET;
  ina.s_addr = inet_addr(ip); 
  memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));

  strcpy(arpreq.arp_dev, "eth0"); 
  arpreq.arp_ha.sa_family = AF_UNSPEC;
/*
  rc = ioctl(sd, SIOCGARP, &arpreq);
  
  if (rc < 0) 
  {
    perror("Entry not available in cache. Sending arp req...\n");

    send_arp_req(ip);
    
    rc = ioctl(sd, SIOCGARP, &arpreq);
    if (rc < 0)
    {
      printf("No arp reply. \n");
      exit(1);
    }
  }
*/
    send_arp_req(ip);
    
    rc = ioctl(sd, SIOCGARP, &arpreq);
    if (rc < 0)
    {
      printf("No arp reply. \n");
      exit(1);
    }
 
  hw_addr = (unsigned char *) arpreq.arp_ha.sa_data;
  printf("HWAddr found : %x:%x:%x:%x:%x:%x\n", hw_addr[0],
       hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);
  
  return 1; 
} 

void send_arp_req(char *ip_addr)
{
  /* Sending the arp Request here */
}     



-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux