In computer networking, the Address Resolution Protocol (ARP) is the method for finding a host's hardware address when only its Network Layer address is known.ARP is not an IP-only or Ethernet-only protocol; it can be used to resolve many different network-layer protocol addresses to hardware addresses, although, due to the overwhelming prevalence of IPv4 and Ethernet, ARP is primarily used to translate IP addresses to Ethernet MAC addresses. It is also used for IP over other LAN technologies, such as Token Ring, FDDI, or IEEE 802.11, and for IP over ATM.
ARP is used in four cases of two hosts communicating:
1. When two hosts are on the same network and one desires to send a packet to the other
2. When two hosts are on different networks and must use a gateway/router to reach the other host
3. When a router needs to forward a packet for one host through another router
4. When a router needs to forward a packet from one host to the destination host on the same network
In the first case, you would have two hosts, A and B, on the same LAN segment. If, for example, Host A wants to send an IPv4 packet to Host B, Host A must already have an IPv4 (Network Layer) address for Host B. However, in order to be able to send the packet on the LAN to Host B, Host A must also have a Link Layer address, e.g. a MAC address, for Host B. If it doesn't already know that MAC address, it would send an ARP request to ask for that MAC address, in hope of getting a reply from Host B, or another host on the network, returning the required MAC address.
In the second case, for the same example, hosts A and B would be on different network segments, but there would be a router on the same LAN segment as Host A, which is either on the same network segment as Host B, or on the same network segment as another router that is on the same network segment as Host B, or on the same network segment as another router that is on the same network segment as yet another router that is on the same segment as Host B, and so on. Host A would send the IPv4 packet not to Host B, but to the first of those routers; it would look up Host B in its routing table to determine the IPv4 address of the appropriate router. It would then, if it doesn't already know the MAC address of that router, use ARP to determine that MAC address.
The third case is similar to the second case; the router would look up Host B in its routing table to determine the IPv4 address of the next router to which it should send the packet and, if it doesn't already know the MAC address for the router, use ARP to determine that MAC address. The fourth case is similar to the first case; the router has determined that Host B is on the same LAN segment, and, if it doesn't already know Host B's MAC address, will use ARP to determine that MAC address.
ARP Packet structure

The following is the packet structure used for ARP requests and replies. On Ethernet networks, these packets use an EtherType of 0x0806, and are sent to the broadcast MAC address of FF:FF:FF:FF:FF:FF. Note that the EtherType (0x0806) is used in the Ethernet header, and should not be used as the PTYPE of the ARP packet. The ARP type (0x0806) should never be used in the PTYPE field of an ARP packet, since we never want to link a hardware ptotocol address to the ARP protocol.
Hardware type (HTYPE)
Each data link layer protocol is assigned a number used in this field. For example, Ethernet is 1.
Protocol type (PTYPE)
Each protocol is assigned a number used in this field. For example, IP is 0x0800.
Hardware length (HLEN)
Length in bytes of a hardware address. Ethernet addresses are 6 bytes long.
Protocol length (PLEN)
Length in bytes of a logical address. IPv4 address are 4 bytes long.
Operation
Specifies the operation the sender is performing: 1 for request, and 2 for reply.
Sender hardware address (SHA)
Hardware address of the sender.
Sender protocol address (SPA)
Protocol address of the sender.
Target hardware address (THA)
Hardware address of the intended receiver. This field is ignored in requests.
Target protocol address (TPA)
Protocol address of the intended receiver.
ARP is used in four cases of two hosts communicating:
1. When two hosts are on the same network and one desires to send a packet to the other
2. When two hosts are on different networks and must use a gateway/router to reach the other host
3. When a router needs to forward a packet for one host through another router
4. When a router needs to forward a packet from one host to the destination host on the same network
In the first case, you would have two hosts, A and B, on the same LAN segment. If, for example, Host A wants to send an IPv4 packet to Host B, Host A must already have an IPv4 (Network Layer) address for Host B. However, in order to be able to send the packet on the LAN to Host B, Host A must also have a Link Layer address, e.g. a MAC address, for Host B. If it doesn't already know that MAC address, it would send an ARP request to ask for that MAC address, in hope of getting a reply from Host B, or another host on the network, returning the required MAC address.
In the second case, for the same example, hosts A and B would be on different network segments, but there would be a router on the same LAN segment as Host A, which is either on the same network segment as Host B, or on the same network segment as another router that is on the same network segment as Host B, or on the same network segment as another router that is on the same network segment as yet another router that is on the same segment as Host B, and so on. Host A would send the IPv4 packet not to Host B, but to the first of those routers; it would look up Host B in its routing table to determine the IPv4 address of the appropriate router. It would then, if it doesn't already know the MAC address of that router, use ARP to determine that MAC address.
The third case is similar to the second case; the router would look up Host B in its routing table to determine the IPv4 address of the next router to which it should send the packet and, if it doesn't already know the MAC address for the router, use ARP to determine that MAC address. The fourth case is similar to the first case; the router has determined that Host B is on the same LAN segment, and, if it doesn't already know Host B's MAC address, will use ARP to determine that MAC address.
ARP Packet structure

The following is the packet structure used for ARP requests and replies. On Ethernet networks, these packets use an EtherType of 0x0806, and are sent to the broadcast MAC address of FF:FF:FF:FF:FF:FF. Note that the EtherType (0x0806) is used in the Ethernet header, and should not be used as the PTYPE of the ARP packet. The ARP type (0x0806) should never be used in the PTYPE field of an ARP packet, since we never want to link a hardware ptotocol address to the ARP protocol.
Hardware type (HTYPE)
Each data link layer protocol is assigned a number used in this field. For example, Ethernet is 1.
Protocol type (PTYPE)
Each protocol is assigned a number used in this field. For example, IP is 0x0800.
Hardware length (HLEN)
Length in bytes of a hardware address. Ethernet addresses are 6 bytes long.
Protocol length (PLEN)
Length in bytes of a logical address. IPv4 address are 4 bytes long.
Operation
Specifies the operation the sender is performing: 1 for request, and 2 for reply.
Sender hardware address (SHA)
Hardware address of the sender.
Sender protocol address (SPA)
Protocol address of the sender.
Target hardware address (THA)
Hardware address of the intended receiver. This field is ignored in requests.
Target protocol address (TPA)
Protocol address of the intended receiver.