A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
eth/arp.cpp@13:9cd54f7db57a, 2017-05-01 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon May 01 18:20:55 2017 +0000
- Revision:
- 13:9cd54f7db57a
- Parent:
- 10:f0854784e960
- Child:
- 14:e75a59c1123d
Added ability to read DNS queries with encoded IP addresses
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 10:f0854784e960 | 1 | #include "mbed.h" |
andrewboyson | 10:f0854784e960 | 2 | #include "log.h" |
andrewboyson | 10:f0854784e960 | 3 | #include "net.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "mac.h" |
andrewboyson | 10:f0854784e960 | 5 | #include "dhcp.h" |
andrewboyson | 10:f0854784e960 | 6 | |
andrewboyson | 10:f0854784e960 | 7 | #define REQUEST 1 |
andrewboyson | 10:f0854784e960 | 8 | #define REPLY 2 |
andrewboyson | 10:f0854784e960 | 9 | |
andrewboyson | 10:f0854784e960 | 10 | __packed struct header |
andrewboyson | 10:f0854784e960 | 11 | { |
andrewboyson | 10:f0854784e960 | 12 | int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1. |
andrewboyson | 10:f0854784e960 | 13 | 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. |
andrewboyson | 10:f0854784e960 | 14 | int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes. |
andrewboyson | 10:f0854784e960 | 15 | int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes. |
andrewboyson | 10:f0854784e960 | 16 | int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first. |
andrewboyson | 10:f0854784e960 | 17 | char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field. |
andrewboyson | 10:f0854784e960 | 18 | uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field. |
andrewboyson | 10:f0854784e960 | 19 | char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known). |
andrewboyson | 10:f0854784e960 | 20 | uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target. |
andrewboyson | 10:f0854784e960 | 21 | }; |
andrewboyson | 10:f0854784e960 | 22 | int ArpHandleReceivedPacket(char* pSrcMac, void * pPacket, int* pSize, char* pDstMac) |
andrewboyson | 10:f0854784e960 | 23 | { |
andrewboyson | 10:f0854784e960 | 24 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 25 | int16_t hardwareType = NetToHost16(pHeader->hardwareType); |
andrewboyson | 10:f0854784e960 | 26 | int16_t protocolType = NetToHost16(pHeader->protocolType); |
andrewboyson | 10:f0854784e960 | 27 | int8_t hardwareLength = pHeader->hardwareLength; |
andrewboyson | 10:f0854784e960 | 28 | int8_t protocolLength = pHeader->protocolLength; |
andrewboyson | 10:f0854784e960 | 29 | int16_t opCode = NetToHost16(pHeader->opCode); |
andrewboyson | 10:f0854784e960 | 30 | uint32_t targetProtocolAddress = pHeader->targetProtocolAddress; |
andrewboyson | 10:f0854784e960 | 31 | |
andrewboyson | 10:f0854784e960 | 32 | if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet |
andrewboyson | 10:f0854784e960 | 33 | if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4 |
andrewboyson | 10:f0854784e960 | 34 | if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address |
andrewboyson | 10:f0854784e960 | 35 | if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address |
andrewboyson | 10:f0854784e960 | 36 | if (opCode != REQUEST ) return DO_NOTHING; //This is not a request |
andrewboyson | 10:f0854784e960 | 37 | if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us |
andrewboyson | 10:f0854784e960 | 38 | |
andrewboyson | 10:f0854784e960 | 39 | memcpy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress, 6); |
andrewboyson | 10:f0854784e960 | 40 | pHeader->targetProtocolAddress = pHeader->senderProtocolAddress; |
andrewboyson | 13:9cd54f7db57a | 41 | memcpy(pHeader->senderHardwareAddress, MacLocal,6); |
andrewboyson | 10:f0854784e960 | 42 | pHeader->senderProtocolAddress = DhcpLocalIp; |
andrewboyson | 10:f0854784e960 | 43 | pHeader->opCode = NetToHost16(REPLY); |
andrewboyson | 10:f0854784e960 | 44 | |
andrewboyson | 10:f0854784e960 | 45 | memcpy(pDstMac, pSrcMac, 6); |
andrewboyson | 10:f0854784e960 | 46 | |
andrewboyson | 10:f0854784e960 | 47 | return UNICAST; |
andrewboyson | 10:f0854784e960 | 48 | } |