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@43:bc028d5a6424, 2017-10-19 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Oct 19 20:56:58 2017 +0000
- Revision:
- 43:bc028d5a6424
- Parent:
- 42:222a4f45f916
- Child:
- 44:83ce5ace337b
Added verbose option to trace
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 37:793b39683406 | 1 | #include "mbed.h" |
andrewboyson | 37:793b39683406 | 2 | #include "log.h" |
andrewboyson | 37:793b39683406 | 3 | #include "action.h" |
andrewboyson | 37:793b39683406 | 4 | #include "net.h" |
andrewboyson | 37:793b39683406 | 5 | #include "eth.h" |
andrewboyson | 37:793b39683406 | 6 | #include "mac.h" |
andrewboyson | 37:793b39683406 | 7 | #include "dhcp.h" |
andrewboyson | 37:793b39683406 | 8 | #include "ar.h" |
andrewboyson | 37:793b39683406 | 9 | #include "nr.h" |
andrewboyson | 37:793b39683406 | 10 | #include "io.h" |
andrewboyson | 43:bc028d5a6424 | 11 | #include "ip4.h" |
andrewboyson | 10:f0854784e960 | 12 | |
andrewboyson | 10:f0854784e960 | 13 | #define REQUEST 1 |
andrewboyson | 10:f0854784e960 | 14 | #define REPLY 2 |
andrewboyson | 10:f0854784e960 | 15 | |
andrewboyson | 43:bc028d5a6424 | 16 | bool ArpTrace = false; |
andrewboyson | 43:bc028d5a6424 | 17 | |
andrewboyson | 22:914b970356f0 | 18 | uint32_t ArpAddressToResolve; |
andrewboyson | 22:914b970356f0 | 19 | bool ArpResolveRequestFlag = false; |
andrewboyson | 22:914b970356f0 | 20 | |
andrewboyson | 10:f0854784e960 | 21 | __packed struct header |
andrewboyson | 10:f0854784e960 | 22 | { |
andrewboyson | 10:f0854784e960 | 23 | int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1. |
andrewboyson | 10:f0854784e960 | 24 | 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 | 25 | int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes. |
andrewboyson | 10:f0854784e960 | 26 | int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes. |
andrewboyson | 10:f0854784e960 | 27 | int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first. |
andrewboyson | 10:f0854784e960 | 28 | char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field. |
andrewboyson | 10:f0854784e960 | 29 | uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field. |
andrewboyson | 10:f0854784e960 | 30 | char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known). |
andrewboyson | 10:f0854784e960 | 31 | uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target. |
andrewboyson | 10:f0854784e960 | 32 | }; |
andrewboyson | 43:bc028d5a6424 | 33 | |
andrewboyson | 43:bc028d5a6424 | 34 | static void logHeader(void * pPacket) |
andrewboyson | 43:bc028d5a6424 | 35 | { |
andrewboyson | 43:bc028d5a6424 | 36 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 43:bc028d5a6424 | 37 | char text[30]; |
andrewboyson | 43:bc028d5a6424 | 38 | |
andrewboyson | 43:bc028d5a6424 | 39 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 40 | { |
andrewboyson | 43:bc028d5a6424 | 41 | LogTime("ARP header\r\n"); |
andrewboyson | 43:bc028d5a6424 | 42 | if (NetToHost16(pHeader->hardwareType) == ETHERNET) Log (" hardwareType = ETHERNET\r\n"); |
andrewboyson | 43:bc028d5a6424 | 43 | else LogF(" hardwareType = %d\r\n", NetToHost16(pHeader->hardwareType)); |
andrewboyson | 43:bc028d5a6424 | 44 | EthProtocolToString(NetToHost16(pHeader->protocolType), sizeof(text), text); |
andrewboyson | 43:bc028d5a6424 | 45 | LogF(" protocolType = %s\r\n", text); |
andrewboyson | 43:bc028d5a6424 | 46 | LogF(" hardwareLength = %d\r\n", pHeader->hardwareLength); |
andrewboyson | 43:bc028d5a6424 | 47 | LogF(" protocolLength = %d\r\n", pHeader->protocolLength); |
andrewboyson | 43:bc028d5a6424 | 48 | if (NetToHost16(pHeader->opCode) == REQUEST) Log (" opCode = REQUEST\r\n"); |
andrewboyson | 43:bc028d5a6424 | 49 | else if (NetToHost16(pHeader->opCode) == REPLY ) Log (" opCode = REPLY\r\n"); |
andrewboyson | 43:bc028d5a6424 | 50 | else LogF(" opCode = %d\r\n", NetToHost16(pHeader->opCode)); |
andrewboyson | 43:bc028d5a6424 | 51 | MacToString(pHeader->senderHardwareAddress, sizeof(text), text); |
andrewboyson | 43:bc028d5a6424 | 52 | LogF(" senderHardwareAddress = %s\r\n", text); |
andrewboyson | 43:bc028d5a6424 | 53 | Ip4AddressToString(pHeader->senderProtocolAddress, sizeof(text), text); |
andrewboyson | 43:bc028d5a6424 | 54 | LogF(" senderProtocolAddress = %s\r\n", text); |
andrewboyson | 43:bc028d5a6424 | 55 | MacToString(pHeader->targetHardwareAddress, sizeof(text), text); |
andrewboyson | 43:bc028d5a6424 | 56 | LogF(" targetHardwareAddress = %s\r\n", text); |
andrewboyson | 43:bc028d5a6424 | 57 | Ip4AddressToString(pHeader->targetProtocolAddress, sizeof(text), text); |
andrewboyson | 43:bc028d5a6424 | 58 | LogF(" targetProtocolAddress = %s\r\n", text); |
andrewboyson | 43:bc028d5a6424 | 59 | } |
andrewboyson | 43:bc028d5a6424 | 60 | else |
andrewboyson | 43:bc028d5a6424 | 61 | { |
andrewboyson | 43:bc028d5a6424 | 62 | Log("ARP header "); |
andrewboyson | 43:bc028d5a6424 | 63 | MacToString (pHeader->senderHardwareAddress, sizeof(text), text); Log(text); |
andrewboyson | 43:bc028d5a6424 | 64 | Ip4AddressToString(pHeader->senderProtocolAddress, sizeof(text), text); Log("["); LogF(text); Log("] >>> "); |
andrewboyson | 43:bc028d5a6424 | 65 | MacToString (pHeader->targetHardwareAddress, sizeof(text), text); LogF(text); |
andrewboyson | 43:bc028d5a6424 | 66 | Ip4AddressToString(pHeader->targetProtocolAddress, sizeof(text), text); Log("["); LogF(text); Log("]\r\n"); |
andrewboyson | 43:bc028d5a6424 | 67 | |
andrewboyson | 43:bc028d5a6424 | 68 | } |
andrewboyson | 43:bc028d5a6424 | 69 | } |
andrewboyson | 43:bc028d5a6424 | 70 | |
andrewboyson | 37:793b39683406 | 71 | int ArpHandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void * pPacket, int* pSize, char* pDstMac) |
andrewboyson | 10:f0854784e960 | 72 | { |
andrewboyson | 10:f0854784e960 | 73 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 74 | int16_t hardwareType = NetToHost16(pHeader->hardwareType); |
andrewboyson | 10:f0854784e960 | 75 | int16_t protocolType = NetToHost16(pHeader->protocolType); |
andrewboyson | 10:f0854784e960 | 76 | int8_t hardwareLength = pHeader->hardwareLength; |
andrewboyson | 10:f0854784e960 | 77 | int8_t protocolLength = pHeader->protocolLength; |
andrewboyson | 10:f0854784e960 | 78 | int16_t opCode = NetToHost16(pHeader->opCode); |
andrewboyson | 10:f0854784e960 | 79 | uint32_t targetProtocolAddress = pHeader->targetProtocolAddress; |
andrewboyson | 10:f0854784e960 | 80 | |
andrewboyson | 10:f0854784e960 | 81 | if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet |
andrewboyson | 10:f0854784e960 | 82 | if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4 |
andrewboyson | 10:f0854784e960 | 83 | if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address |
andrewboyson | 10:f0854784e960 | 84 | if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address |
andrewboyson | 10:f0854784e960 | 85 | if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us |
andrewboyson | 10:f0854784e960 | 86 | |
andrewboyson | 22:914b970356f0 | 87 | switch (opCode) |
andrewboyson | 22:914b970356f0 | 88 | { |
andrewboyson | 22:914b970356f0 | 89 | case REQUEST: |
andrewboyson | 43:bc028d5a6424 | 90 | if (ArpTrace) |
andrewboyson | 43:bc028d5a6424 | 91 | { |
andrewboyson | 43:bc028d5a6424 | 92 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 93 | LogTime("ARP received request\r\n"); |
andrewboyson | 43:bc028d5a6424 | 94 | logHeader(pPacket); |
andrewboyson | 43:bc028d5a6424 | 95 | if (NetTraceStack) traceback(); |
andrewboyson | 43:bc028d5a6424 | 96 | } |
andrewboyson | 36:900e24b27bfb | 97 | MacCopy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress); |
andrewboyson | 43:bc028d5a6424 | 98 | pHeader->targetProtocolAddress = pHeader->senderProtocolAddress; |
andrewboyson | 36:900e24b27bfb | 99 | MacCopy(pHeader->senderHardwareAddress, MacLocal); |
andrewboyson | 43:bc028d5a6424 | 100 | pHeader->senderProtocolAddress = DhcpLocalIp; |
andrewboyson | 43:bc028d5a6424 | 101 | pHeader->opCode = NetToHost16(REPLY); |
andrewboyson | 36:900e24b27bfb | 102 | MacCopy(pDstMac, pSrcMac); |
andrewboyson | 43:bc028d5a6424 | 103 | if (ArpTrace) logHeader(pPacket); |
andrewboyson | 43:bc028d5a6424 | 104 | return ActionMakeFromDestAndTrace(UNICAST, ArpTrace && NetTraceStack); |
andrewboyson | 43:bc028d5a6424 | 105 | |
andrewboyson | 22:914b970356f0 | 106 | case REPLY: |
andrewboyson | 43:bc028d5a6424 | 107 | if (ArpTrace) |
andrewboyson | 43:bc028d5a6424 | 108 | { |
andrewboyson | 43:bc028d5a6424 | 109 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 110 | LogTime("ARP received reply\r\n"); |
andrewboyson | 43:bc028d5a6424 | 111 | logHeader(pPacket); |
andrewboyson | 43:bc028d5a6424 | 112 | if (NetTraceStack) traceback(); |
andrewboyson | 43:bc028d5a6424 | 113 | } |
andrewboyson | 35:93c39d260a83 | 114 | ArAddIp4Record(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress); |
andrewboyson | 35:93c39d260a83 | 115 | NrMakeRequestForNameFromIp4(pHeader->senderProtocolAddress); |
andrewboyson | 22:914b970356f0 | 116 | return DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 117 | |
andrewboyson | 22:914b970356f0 | 118 | default: |
andrewboyson | 22:914b970356f0 | 119 | return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 120 | } |
andrewboyson | 22:914b970356f0 | 121 | } |
andrewboyson | 22:914b970356f0 | 122 | int ArpPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 22:914b970356f0 | 123 | { |
andrewboyson | 22:914b970356f0 | 124 | if (!ArpResolveRequestFlag) return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 125 | ArpResolveRequestFlag = false; |
andrewboyson | 43:bc028d5a6424 | 126 | |
andrewboyson | 22:914b970356f0 | 127 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 22:914b970356f0 | 128 | |
andrewboyson | 22:914b970356f0 | 129 | pHeader->hardwareType = NetToHost16(ETHERNET); |
andrewboyson | 22:914b970356f0 | 130 | pHeader->protocolType = NetToHost16(IPV4); |
andrewboyson | 22:914b970356f0 | 131 | pHeader->hardwareLength = 6; |
andrewboyson | 22:914b970356f0 | 132 | pHeader->protocolLength = 4; |
andrewboyson | 22:914b970356f0 | 133 | pHeader->opCode = NetToHost16(REQUEST); |
andrewboyson | 22:914b970356f0 | 134 | |
andrewboyson | 36:900e24b27bfb | 135 | MacClear(pHeader->targetHardwareAddress); |
andrewboyson | 36:900e24b27bfb | 136 | pHeader->targetProtocolAddress = ArpAddressToResolve; |
andrewboyson | 43:bc028d5a6424 | 137 | MacCopy (pHeader->senderHardwareAddress, MacLocal); |
andrewboyson | 43:bc028d5a6424 | 138 | pHeader->senderProtocolAddress = DhcpLocalIp; |
andrewboyson | 22:914b970356f0 | 139 | |
andrewboyson | 22:914b970356f0 | 140 | *pSize = sizeof(header); |
andrewboyson | 10:f0854784e960 | 141 | |
andrewboyson | 43:bc028d5a6424 | 142 | if (ArpTrace) |
andrewboyson | 43:bc028d5a6424 | 143 | { |
andrewboyson | 43:bc028d5a6424 | 144 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 145 | LogTime("ARP send request\r\n"); |
andrewboyson | 43:bc028d5a6424 | 146 | logHeader(pPacket); |
andrewboyson | 43:bc028d5a6424 | 147 | } |
andrewboyson | 43:bc028d5a6424 | 148 | return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack); |
andrewboyson | 35:93c39d260a83 | 149 | } |