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

Committer:
andrewboyson
Date:
Thu Nov 02 08:10:55 2017 +0000
Revision:
49:1a6336f2b3f9
Parent:
48:952dddb74b8b
Child:
50:492f2d2954e4
Separated the address handling parts of IP4 and IP6 from the protocol parts.

Who changed what in which revision?

UserRevisionLine numberNew 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 48:952dddb74b8b 8 #include "ar4.h"
andrewboyson 37:793b39683406 9 #include "nr.h"
andrewboyson 37:793b39683406 10 #include "io.h"
andrewboyson 49:1a6336f2b3f9 11 #include "ip4addr.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 48:952dddb74b8b 33 static struct header* pHeader;
andrewboyson 43:bc028d5a6424 34
andrewboyson 48:952dddb74b8b 35 static void logHeader()
andrewboyson 48:952dddb74b8b 36 {
andrewboyson 43:bc028d5a6424 37 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 38 {
andrewboyson 43:bc028d5a6424 39 LogTime("ARP header\r\n");
andrewboyson 43:bc028d5a6424 40 if (NetToHost16(pHeader->hardwareType) == ETHERNET) Log (" hardwareType = ETHERNET\r\n");
andrewboyson 43:bc028d5a6424 41 else LogF(" hardwareType = %d\r\n", NetToHost16(pHeader->hardwareType));
andrewboyson 48:952dddb74b8b 42 Log (" protocolType = "); EthProtocolLog(pHeader->protocolType); Log("\r\n");
andrewboyson 48:952dddb74b8b 43 LogF(" hardwareLength = %d\r\n", pHeader->hardwareLength);
andrewboyson 48:952dddb74b8b 44 LogF(" protocolLength = %d\r\n", pHeader->protocolLength);
andrewboyson 48:952dddb74b8b 45 if (NetToHost16(pHeader->opCode) == REQUEST) Log (" opCode = REQUEST\r\n");
andrewboyson 48:952dddb74b8b 46 else if (NetToHost16(pHeader->opCode) == REPLY ) Log (" opCode = REPLY\r\n");
andrewboyson 48:952dddb74b8b 47 else LogF(" opCode = %d\r\n", NetToHost16(pHeader->opCode));
andrewboyson 48:952dddb74b8b 48 Log(" senderHardwareAddress = "); MacLog(pHeader->senderHardwareAddress); Log("\r\n");
andrewboyson 48:952dddb74b8b 49 Log(" senderProtocolAddress = "); Ip4AddressLog(pHeader->senderProtocolAddress); Log("\r\n");
andrewboyson 48:952dddb74b8b 50 Log(" targetHardwareAddress = "); MacLog(pHeader->targetHardwareAddress); Log("\r\n");
andrewboyson 48:952dddb74b8b 51 Log(" targetProtocolAddress = "); Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
andrewboyson 43:bc028d5a6424 52 }
andrewboyson 43:bc028d5a6424 53 else
andrewboyson 43:bc028d5a6424 54 {
andrewboyson 43:bc028d5a6424 55 Log("ARP header ");
andrewboyson 47:73af5c0b0dc2 56 MacLog(pHeader->senderHardwareAddress); Log("==");
andrewboyson 47:73af5c0b0dc2 57 Ip4AddressLog(pHeader->senderProtocolAddress); Log(" >>> ");
andrewboyson 47:73af5c0b0dc2 58 MacLog(pHeader->targetHardwareAddress); Log("==");
andrewboyson 47:73af5c0b0dc2 59 Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
andrewboyson 43:bc028d5a6424 60
andrewboyson 43:bc028d5a6424 61 }
andrewboyson 43:bc028d5a6424 62 }
andrewboyson 48:952dddb74b8b 63 static void (*pTraceBack)(void);
andrewboyson 48:952dddb74b8b 64 static void trace()
andrewboyson 48:952dddb74b8b 65 {
andrewboyson 48:952dddb74b8b 66 pTraceBack();
andrewboyson 48:952dddb74b8b 67 logHeader();
andrewboyson 48:952dddb74b8b 68 }
andrewboyson 43:bc028d5a6424 69
andrewboyson 37:793b39683406 70 int ArpHandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void * pPacket, int* pSize, char* pDstMac)
andrewboyson 10:f0854784e960 71 {
andrewboyson 48:952dddb74b8b 72 pTraceBack = traceback;
andrewboyson 48:952dddb74b8b 73 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 48:952dddb74b8b 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 44:83ce5ace337b 94 if (NetTraceStack) traceback();
andrewboyson 48:952dddb74b8b 95 logHeader();
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 48:952dddb74b8b 103 if (ArpTrace) logHeader();
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 44:83ce5ace337b 111 if (NetTraceStack) traceback();
andrewboyson 48:952dddb74b8b 112 logHeader();
andrewboyson 43:bc028d5a6424 113 }
andrewboyson 48:952dddb74b8b 114 Ar4AddIpRecord(trace, 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 48:952dddb74b8b 126
andrewboyson 48:952dddb74b8b 127 pHeader = (header*)pPacket;
andrewboyson 48:952dddb74b8b 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 48:952dddb74b8b 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 48:952dddb74b8b 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 48:952dddb74b8b 146 logHeader();
andrewboyson 43:bc028d5a6424 147 }
andrewboyson 43:bc028d5a6424 148 return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack);
andrewboyson 35:93c39d260a83 149 }