ARP
The ARP (Address Resolution Protocol) is a 3rd level protocol, employed to the LAN networks internal, to translate the IP addresses in the physical addresses (MAC) correspondences.
We already analyzed in this page the mechanism which ties the two addresses.
The heading
This protocol isn't only for ethernet networks and IP protocol, so an ARP packet contains different fields which specify what addresses are:
At these follows a variable length dates field (which depends of the heading), that for the operation nominate will contain 4 addresses, like in this scheme:

ARP Request
The MAc address demanding packet contains, beyond the heading, the sender MAC and IP addresses, and like Target IP Address, the address you want to know of the physical address.
That's why, when an ARP packet arrives is sufficient to control the fields Operation and indeed Target IP Address which must correspond to our IP, or else we ignore the demanding.
ARP Reply
The replay packet is similar to the one already saw: it's enough to indicate in the field Operation that it treats about a replay, and refill the fields with the sender and recipient addresses.
Everything is collected in a sole function:
void processARP(){
ARPPacket packet;
IPAddr tmp;
encGetArray((u8*)&packet, sizeof(packet));
packet.operation = htons(packet.operation);
if (packet.operation == ARP_REQUEST){
if (ipMatch(packet.TargetIP,MyIP)){
packet.operation = htons(ARP_REPLY);
tmp = packet.TargetIP;
packet.TargetMAC = packet.SourceMAC;
packet.TargetIP = packet.SourceIP;
packet.SourceIP = tmp;
packet.SourceMAC.b[0] = MY_MAC1;
packet.SourceMAC.b[1] = MY_MAC2;
packet.SourceMAC.b[2] = MY_MAC3;
packet.SourceMAC.b[3] = MY_MAC4;
packet.SourceMAC.b[4] = MY_MAC5;
packet.SourceMAC.b[5] = MY_MAC6;
MACPutHeader(packet.TargetMAC, TYPE_ARP);
encPutArray((u8*)&packet,sizeof(packet));
MACSend();
}
}
}The ARP packet is defined:
typedef struct {
u16 hardware;
u16 protocol;
u8 MacLen;
u8 ProtocolLen;
u16 operation;
MACAddr SourceMAC;
IPAddr SourceIP;
MACAddr TargetMAC;
IPAddr TargetIP;
} ARPPacket;
|