Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
eth/arp.cpp
- Committer:
- andrewboyson
- Date:
- 2017-10-19
- Revision:
- 43:bc028d5a6424
- Parent:
- 42:222a4f45f916
- Child:
- 44:83ce5ace337b
File content as of revision 43:bc028d5a6424:
#include "mbed.h" #include "log.h" #include "action.h" #include "net.h" #include "eth.h" #include "mac.h" #include "dhcp.h" #include "ar.h" #include "nr.h" #include "io.h" #include "ip4.h" #define REQUEST 1 #define REPLY 2 bool ArpTrace = false; uint32_t ArpAddressToResolve; bool ArpResolveRequestFlag = false; __packed struct header { int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1. int16_t protocolType; //16.bit: (ar$pro) Protocol address space. For Ethernet hardware, this is from the set of type fields ether_typ$<protocol>. As held in eth.h, eg IPv4 = 0x8000. int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes. int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes. int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first. char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field. uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field. char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known). uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target. }; static void logHeader(void * pPacket) { struct header* pHeader = (header*)pPacket; char text[30]; if (NetTraceVerbose) { LogTime("ARP header\r\n"); if (NetToHost16(pHeader->hardwareType) == ETHERNET) Log (" hardwareType = ETHERNET\r\n"); else LogF(" hardwareType = %d\r\n", NetToHost16(pHeader->hardwareType)); EthProtocolToString(NetToHost16(pHeader->protocolType), sizeof(text), text); LogF(" protocolType = %s\r\n", text); LogF(" hardwareLength = %d\r\n", pHeader->hardwareLength); LogF(" protocolLength = %d\r\n", pHeader->protocolLength); if (NetToHost16(pHeader->opCode) == REQUEST) Log (" opCode = REQUEST\r\n"); else if (NetToHost16(pHeader->opCode) == REPLY ) Log (" opCode = REPLY\r\n"); else LogF(" opCode = %d\r\n", NetToHost16(pHeader->opCode)); MacToString(pHeader->senderHardwareAddress, sizeof(text), text); LogF(" senderHardwareAddress = %s\r\n", text); Ip4AddressToString(pHeader->senderProtocolAddress, sizeof(text), text); LogF(" senderProtocolAddress = %s\r\n", text); MacToString(pHeader->targetHardwareAddress, sizeof(text), text); LogF(" targetHardwareAddress = %s\r\n", text); Ip4AddressToString(pHeader->targetProtocolAddress, sizeof(text), text); LogF(" targetProtocolAddress = %s\r\n", text); } else { Log("ARP header "); MacToString (pHeader->senderHardwareAddress, sizeof(text), text); Log(text); Ip4AddressToString(pHeader->senderProtocolAddress, sizeof(text), text); Log("["); LogF(text); Log("] >>> "); MacToString (pHeader->targetHardwareAddress, sizeof(text), text); LogF(text); Ip4AddressToString(pHeader->targetProtocolAddress, sizeof(text), text); Log("["); LogF(text); Log("]\r\n"); } } int ArpHandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void * pPacket, int* pSize, char* pDstMac) { struct header* pHeader = (header*)pPacket; int16_t hardwareType = NetToHost16(pHeader->hardwareType); int16_t protocolType = NetToHost16(pHeader->protocolType); int8_t hardwareLength = pHeader->hardwareLength; int8_t protocolLength = pHeader->protocolLength; int16_t opCode = NetToHost16(pHeader->opCode); uint32_t targetProtocolAddress = pHeader->targetProtocolAddress; if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4 if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us switch (opCode) { case REQUEST: if (ArpTrace) { if (NetTraceNewLine) Log("\r\n"); LogTime("ARP received request\r\n"); logHeader(pPacket); if (NetTraceStack) traceback(); } MacCopy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress); pHeader->targetProtocolAddress = pHeader->senderProtocolAddress; MacCopy(pHeader->senderHardwareAddress, MacLocal); pHeader->senderProtocolAddress = DhcpLocalIp; pHeader->opCode = NetToHost16(REPLY); MacCopy(pDstMac, pSrcMac); if (ArpTrace) logHeader(pPacket); return ActionMakeFromDestAndTrace(UNICAST, ArpTrace && NetTraceStack); case REPLY: if (ArpTrace) { if (NetTraceNewLine) Log("\r\n"); LogTime("ARP received reply\r\n"); logHeader(pPacket); if (NetTraceStack) traceback(); } ArAddIp4Record(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress); NrMakeRequestForNameFromIp4(pHeader->senderProtocolAddress); return DO_NOTHING; default: return DO_NOTHING; } } int ArpPollForPacketToSend(void* pPacket, int* pSize) { if (!ArpResolveRequestFlag) return DO_NOTHING; ArpResolveRequestFlag = false; struct header* pHeader = (header*)pPacket; pHeader->hardwareType = NetToHost16(ETHERNET); pHeader->protocolType = NetToHost16(IPV4); pHeader->hardwareLength = 6; pHeader->protocolLength = 4; pHeader->opCode = NetToHost16(REQUEST); MacClear(pHeader->targetHardwareAddress); pHeader->targetProtocolAddress = ArpAddressToResolve; MacCopy (pHeader->senderHardwareAddress, MacLocal); pHeader->senderProtocolAddress = DhcpLocalIp; *pSize = sizeof(header); if (ArpTrace) { if (NetTraceNewLine) Log("\r\n"); LogTime("ARP send request\r\n"); logHeader(pPacket); } return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack); }