100% found this document useful (1 vote)
2K views

6 - Arp Implementation Using Udp

The document describes an ARP implementation using UDP that takes an IP address as input and returns the corresponding hardware address. It includes necessary header files, creates a socket, declares ARP and sockaddr_in structures, initializes values, pings the client, uses ioctl() to get the ARP cache entry, and prints the hardware address associated with the input IP. The code implements these steps - it takes an IP as a command line argument, retrieves the MAC address from the ARP cache via ioctl(), and prints the result.

Uploaded by

Senthil R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

6 - Arp Implementation Using Udp

The document describes an ARP implementation using UDP that takes an IP address as input and returns the corresponding hardware address. It includes necessary header files, creates a socket, declares ARP and sockaddr_in structures, initializes values, pings the client, uses ioctl() to get the ARP cache entry, and prints the hardware address associated with the input IP. The code implements these steps - it takes an IP as a command line argument, retrieves the MAC address from the ARP cache via ioctl(), and prints the result.

Uploaded by

Senthil R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ARP IMPLEMENTATION USING UDP

GIVEN REQUIREMENTS:

There is a single host. The IP address of any Client in the network is given as input and the corresponding hardware address is got as the output.

TECHNICAL OBJECTIVE:

Address Resolution Protocol (ARP) is implemented through this program. The IP address of any Client is given as the input. The ARP cache is looked up for
the corresponding hardware address. This is returned as the output. Before compiling that Client is pinged.

METHODOLOGY:

 Include the necessary header files.


 Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
 Declare structures arpreq ( as NULL structure, if required) and sockaddr_in.
 Initialize server address to 0 using the bzero function.
 Assign the sin_family to AF_INET and sin_addr using inet_aton().
 Using the object of arpreq structure assign the name of the Network Device to the data member arp_dev like, arp_dev=”eth0”.
 Ping the required Client.
 Using the ioctl() we get the ARP cache entry for the given IP address.
 The output of the ioctl() function is stored in the sa_data[0] datamember of the arp_ha structure which is in turn a data member of
structure arpreq.
 Print the hardware address of the given IP address on the output console.

CODING:
ARP: arp.c

#include<sys/types.h>
#include<sys/socket.h>
#include<net/if_arp.h>
#include<sys/ioctl.h>
#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc,char *argv[])
{
struct sockaddr_in sin={0};
struct arpreq myarp={{0}};
unsigned char *ptr;
int sd;
sin.sin_family=AF_INET;
if(inet_aton(argv[1],&sin.sin_addr)==0)
{
printf("Ip address Entered '%s' is not valid \n",argv[1]);
exit(0);
}
memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa));
strcpy(myarp.arp_dev,"eth0");
sd=socket(AF_INET,SOCK_DGRAM,0);
if(ioctl(sd,SIOCGARP,&myarp)==1)
{
printf("No Entry in ARP Cache for '%s'",argv[1]);
exit(0);
}
ptr=&myarp.arp_ha.sa_data[0];
printf("MAC Address For '%s' : ",argv[1]);
printf("%X:%X:%X:%X:%X:%X\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+5));
return 0;
}

Page | 1
SAMPLE OUTPUT:

Host: arp.c
(Host Name:Root1)

[root@localhost 4ita33]# vi arp.c


[root@localhost 4ita33]# ping 172.16.29.51

PING 172.16.29.51 (172.16.29.51) 56(84) bytes of data.


64 bytes from172.16.29.51: icmp_seq=1 ttl=64 time=1.19 ms
64 bytes from 172.16.29.51: icmp_seq=2 ttl=64 time=0.817 ms

-- 172.16.29.51ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 999ms

rtt min/avg/max/mdev = 0.817/1.005/1.193/0.188 ms

[root@localhost 4ita33]# cc arp.c


[root@localhost 4ita33]# ./a.out 172.16.29.51

Hardware Address is: 172.16.29.51:


The MAC address is:0:8:5C:5D:47:50:

INFERENCE:

Thus the ARP implementation is developed to gets the MAC address of the remote machine’s IP address from ARP cache and
prints it.

Page | 2

You might also like