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
Diff: eth/arp.cpp
- Revision:
- 10:f0854784e960
- Child:
- 13:9cd54f7db57a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eth/arp.cpp Sun Apr 16 14:21:55 2017 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "log.h" +#include "net.h" +#include "eth.h" +#include "dhcp.h" + +#define REQUEST 1 +#define REPLY 2 + +__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. +}; +int ArpHandleReceivedPacket(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 (opCode != REQUEST ) return DO_NOTHING; //This is not a request + if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us + + memcpy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress, 6); + pHeader->targetProtocolAddress = pHeader->senderProtocolAddress; + memcpy(pHeader->senderHardwareAddress, EthLocalMac,6); + pHeader->senderProtocolAddress = DhcpLocalIp; + pHeader->opCode = NetToHost16(REPLY); + + memcpy(pDstMac, pSrcMac, 6); + + return UNICAST; +}