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@35:93c39d260a83, 2017-09-22 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Sep 22 13:55:56 2017 +0000
- Revision:
- 35:93c39d260a83
- Parent:
- 30:e34173b7585c
- Child:
- 36:900e24b27bfb
Added name resolution and address resolution
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 | 14:e75a59c1123d | 4 | #include "eth.h" |
andrewboyson | 13:9cd54f7db57a | 5 | #include "mac.h" |
andrewboyson | 10:f0854784e960 | 6 | #include "dhcp.h" |
andrewboyson | 22:914b970356f0 | 7 | #include "ar.h" |
andrewboyson | 35:93c39d260a83 | 8 | #include "nr.h" |
andrewboyson | 22:914b970356f0 | 9 | #include "io.h" |
andrewboyson | 10:f0854784e960 | 10 | |
andrewboyson | 10:f0854784e960 | 11 | #define REQUEST 1 |
andrewboyson | 10:f0854784e960 | 12 | #define REPLY 2 |
andrewboyson | 10:f0854784e960 | 13 | |
andrewboyson | 35:93c39d260a83 | 14 | #define DEBUG false |
andrewboyson | 35:93c39d260a83 | 15 | |
andrewboyson | 22:914b970356f0 | 16 | uint32_t ArpAddressToResolve; |
andrewboyson | 22:914b970356f0 | 17 | bool ArpResolveRequestFlag = false; |
andrewboyson | 22:914b970356f0 | 18 | |
andrewboyson | 10:f0854784e960 | 19 | __packed struct header |
andrewboyson | 10:f0854784e960 | 20 | { |
andrewboyson | 10:f0854784e960 | 21 | int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1. |
andrewboyson | 10:f0854784e960 | 22 | 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 | 23 | int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes. |
andrewboyson | 10:f0854784e960 | 24 | int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes. |
andrewboyson | 10:f0854784e960 | 25 | int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first. |
andrewboyson | 10:f0854784e960 | 26 | char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field. |
andrewboyson | 10:f0854784e960 | 27 | uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field. |
andrewboyson | 10:f0854784e960 | 28 | char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known). |
andrewboyson | 10:f0854784e960 | 29 | uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target. |
andrewboyson | 10:f0854784e960 | 30 | }; |
andrewboyson | 10:f0854784e960 | 31 | int ArpHandleReceivedPacket(char* pSrcMac, void * pPacket, int* pSize, char* pDstMac) |
andrewboyson | 10:f0854784e960 | 32 | { |
andrewboyson | 10:f0854784e960 | 33 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 34 | int16_t hardwareType = NetToHost16(pHeader->hardwareType); |
andrewboyson | 10:f0854784e960 | 35 | int16_t protocolType = NetToHost16(pHeader->protocolType); |
andrewboyson | 10:f0854784e960 | 36 | int8_t hardwareLength = pHeader->hardwareLength; |
andrewboyson | 10:f0854784e960 | 37 | int8_t protocolLength = pHeader->protocolLength; |
andrewboyson | 10:f0854784e960 | 38 | int16_t opCode = NetToHost16(pHeader->opCode); |
andrewboyson | 10:f0854784e960 | 39 | uint32_t targetProtocolAddress = pHeader->targetProtocolAddress; |
andrewboyson | 10:f0854784e960 | 40 | |
andrewboyson | 10:f0854784e960 | 41 | if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet |
andrewboyson | 10:f0854784e960 | 42 | if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4 |
andrewboyson | 10:f0854784e960 | 43 | if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address |
andrewboyson | 10:f0854784e960 | 44 | if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address |
andrewboyson | 10:f0854784e960 | 45 | if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us |
andrewboyson | 10:f0854784e960 | 46 | |
andrewboyson | 22:914b970356f0 | 47 | switch (opCode) |
andrewboyson | 22:914b970356f0 | 48 | { |
andrewboyson | 22:914b970356f0 | 49 | case REQUEST: |
andrewboyson | 22:914b970356f0 | 50 | memcpy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress, 6); |
andrewboyson | 22:914b970356f0 | 51 | pHeader->targetProtocolAddress = pHeader->senderProtocolAddress; |
andrewboyson | 22:914b970356f0 | 52 | memcpy(pHeader->senderHardwareAddress, MacLocal,6); |
andrewboyson | 22:914b970356f0 | 53 | pHeader->senderProtocolAddress = DhcpLocalIp; |
andrewboyson | 22:914b970356f0 | 54 | pHeader->opCode = NetToHost16(REPLY); |
andrewboyson | 22:914b970356f0 | 55 | memcpy(pDstMac, pSrcMac, 6); |
andrewboyson | 22:914b970356f0 | 56 | return UNICAST; |
andrewboyson | 22:914b970356f0 | 57 | case REPLY: |
andrewboyson | 35:93c39d260a83 | 58 | ArAddIp4Record(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress); |
andrewboyson | 35:93c39d260a83 | 59 | NrMakeRequestForNameFromIp4(pHeader->senderProtocolAddress); |
andrewboyson | 22:914b970356f0 | 60 | return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 61 | default: |
andrewboyson | 22:914b970356f0 | 62 | return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 63 | } |
andrewboyson | 22:914b970356f0 | 64 | } |
andrewboyson | 22:914b970356f0 | 65 | int ArpPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 22:914b970356f0 | 66 | { |
andrewboyson | 22:914b970356f0 | 67 | if (!ArpResolveRequestFlag) return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 68 | ArpResolveRequestFlag = false; |
andrewboyson | 22:914b970356f0 | 69 | |
andrewboyson | 22:914b970356f0 | 70 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 22:914b970356f0 | 71 | |
andrewboyson | 22:914b970356f0 | 72 | pHeader->hardwareType = NetToHost16(ETHERNET); |
andrewboyson | 22:914b970356f0 | 73 | pHeader->protocolType = NetToHost16(IPV4); |
andrewboyson | 22:914b970356f0 | 74 | pHeader->hardwareLength = 6; |
andrewboyson | 22:914b970356f0 | 75 | pHeader->protocolLength = 4; |
andrewboyson | 22:914b970356f0 | 76 | pHeader->opCode = NetToHost16(REQUEST); |
andrewboyson | 22:914b970356f0 | 77 | |
andrewboyson | 22:914b970356f0 | 78 | memset(pHeader->targetHardwareAddress, 0, 6); |
andrewboyson | 22:914b970356f0 | 79 | pHeader->targetProtocolAddress = ArpAddressToResolve; |
andrewboyson | 13:9cd54f7db57a | 80 | memcpy(pHeader->senderHardwareAddress, MacLocal,6); |
andrewboyson | 10:f0854784e960 | 81 | pHeader->senderProtocolAddress = DhcpLocalIp; |
andrewboyson | 22:914b970356f0 | 82 | |
andrewboyson | 22:914b970356f0 | 83 | *pSize = sizeof(header); |
andrewboyson | 10:f0854784e960 | 84 | |
andrewboyson | 22:914b970356f0 | 85 | return BROADCAST; |
andrewboyson | 35:93c39d260a83 | 86 | } |