Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Oct 26 14:50:24 2017 +0000
Revision:
47:73af5c0b0dc2
Parent:
44:83ce5ace337b
Child:
48:952dddb74b8b
Replaced a number of temporary buffers with direct writes to the Log or HTTP.

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 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
andrewboyson 43:bc028d5a6424 38 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 39 {
andrewboyson 43:bc028d5a6424 40 LogTime("ARP header\r\n");
andrewboyson 43:bc028d5a6424 41 if (NetToHost16(pHeader->hardwareType) == ETHERNET) Log (" hardwareType = ETHERNET\r\n");
andrewboyson 43:bc028d5a6424 42 else LogF(" hardwareType = %d\r\n", NetToHost16(pHeader->hardwareType));
andrewboyson 47:73af5c0b0dc2 43 Log (" protocolType = "); EthProtocolLog(pHeader->protocolType); Log("\r\n");
andrewboyson 43:bc028d5a6424 44 LogF(" hardwareLength = %d\r\n", pHeader->hardwareLength);
andrewboyson 43:bc028d5a6424 45 LogF(" protocolLength = %d\r\n", pHeader->protocolLength);
andrewboyson 43:bc028d5a6424 46 if (NetToHost16(pHeader->opCode) == REQUEST) Log (" opCode = REQUEST\r\n");
andrewboyson 43:bc028d5a6424 47 else if (NetToHost16(pHeader->opCode) == REPLY ) Log (" opCode = REPLY\r\n");
andrewboyson 43:bc028d5a6424 48 else LogF(" opCode = %d\r\n", NetToHost16(pHeader->opCode));
andrewboyson 47:73af5c0b0dc2 49 Log(" senderHardwareAddress = "); MacLog(pHeader->senderHardwareAddress); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 50 Log(" senderProtocolAddress = "); Ip4AddressLog(pHeader->senderProtocolAddress); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 51 Log(" targetHardwareAddress = "); MacLog(pHeader->targetHardwareAddress); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 52 Log(" targetProtocolAddress = "); Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
andrewboyson 43:bc028d5a6424 53 }
andrewboyson 43:bc028d5a6424 54 else
andrewboyson 43:bc028d5a6424 55 {
andrewboyson 43:bc028d5a6424 56 Log("ARP header ");
andrewboyson 47:73af5c0b0dc2 57 MacLog(pHeader->senderHardwareAddress); Log("==");
andrewboyson 47:73af5c0b0dc2 58 Ip4AddressLog(pHeader->senderProtocolAddress); Log(" >>> ");
andrewboyson 47:73af5c0b0dc2 59 MacLog(pHeader->targetHardwareAddress); Log("==");
andrewboyson 47:73af5c0b0dc2 60 Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
andrewboyson 43:bc028d5a6424 61
andrewboyson 43:bc028d5a6424 62 }
andrewboyson 43:bc028d5a6424 63 }
andrewboyson 43:bc028d5a6424 64
andrewboyson 37:793b39683406 65 int ArpHandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void * pPacket, int* pSize, char* pDstMac)
andrewboyson 10:f0854784e960 66 {
andrewboyson 10:f0854784e960 67 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 68 int16_t hardwareType = NetToHost16(pHeader->hardwareType);
andrewboyson 10:f0854784e960 69 int16_t protocolType = NetToHost16(pHeader->protocolType);
andrewboyson 10:f0854784e960 70 int8_t hardwareLength = pHeader->hardwareLength;
andrewboyson 10:f0854784e960 71 int8_t protocolLength = pHeader->protocolLength;
andrewboyson 10:f0854784e960 72 int16_t opCode = NetToHost16(pHeader->opCode);
andrewboyson 10:f0854784e960 73 uint32_t targetProtocolAddress = pHeader->targetProtocolAddress;
andrewboyson 10:f0854784e960 74
andrewboyson 10:f0854784e960 75 if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet
andrewboyson 10:f0854784e960 76 if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4
andrewboyson 10:f0854784e960 77 if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address
andrewboyson 10:f0854784e960 78 if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address
andrewboyson 10:f0854784e960 79 if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us
andrewboyson 10:f0854784e960 80
andrewboyson 22:914b970356f0 81 switch (opCode)
andrewboyson 22:914b970356f0 82 {
andrewboyson 22:914b970356f0 83 case REQUEST:
andrewboyson 43:bc028d5a6424 84 if (ArpTrace)
andrewboyson 43:bc028d5a6424 85 {
andrewboyson 43:bc028d5a6424 86 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 87 LogTime("ARP received request\r\n");
andrewboyson 44:83ce5ace337b 88 if (NetTraceStack) traceback();
andrewboyson 43:bc028d5a6424 89 logHeader(pPacket);
andrewboyson 43:bc028d5a6424 90 }
andrewboyson 36:900e24b27bfb 91 MacCopy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress);
andrewboyson 43:bc028d5a6424 92 pHeader->targetProtocolAddress = pHeader->senderProtocolAddress;
andrewboyson 36:900e24b27bfb 93 MacCopy(pHeader->senderHardwareAddress, MacLocal);
andrewboyson 43:bc028d5a6424 94 pHeader->senderProtocolAddress = DhcpLocalIp;
andrewboyson 43:bc028d5a6424 95 pHeader->opCode = NetToHost16(REPLY);
andrewboyson 36:900e24b27bfb 96 MacCopy(pDstMac, pSrcMac);
andrewboyson 43:bc028d5a6424 97 if (ArpTrace) logHeader(pPacket);
andrewboyson 43:bc028d5a6424 98 return ActionMakeFromDestAndTrace(UNICAST, ArpTrace && NetTraceStack);
andrewboyson 43:bc028d5a6424 99
andrewboyson 22:914b970356f0 100 case REPLY:
andrewboyson 43:bc028d5a6424 101 if (ArpTrace)
andrewboyson 43:bc028d5a6424 102 {
andrewboyson 43:bc028d5a6424 103 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 104 LogTime("ARP received reply\r\n");
andrewboyson 44:83ce5ace337b 105 if (NetTraceStack) traceback();
andrewboyson 43:bc028d5a6424 106 logHeader(pPacket);
andrewboyson 43:bc028d5a6424 107 }
andrewboyson 35:93c39d260a83 108 ArAddIp4Record(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress);
andrewboyson 35:93c39d260a83 109 NrMakeRequestForNameFromIp4(pHeader->senderProtocolAddress);
andrewboyson 22:914b970356f0 110 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 111
andrewboyson 22:914b970356f0 112 default:
andrewboyson 22:914b970356f0 113 return DO_NOTHING;
andrewboyson 22:914b970356f0 114 }
andrewboyson 22:914b970356f0 115 }
andrewboyson 22:914b970356f0 116 int ArpPollForPacketToSend(void* pPacket, int* pSize)
andrewboyson 22:914b970356f0 117 {
andrewboyson 22:914b970356f0 118 if (!ArpResolveRequestFlag) return DO_NOTHING;
andrewboyson 22:914b970356f0 119 ArpResolveRequestFlag = false;
andrewboyson 43:bc028d5a6424 120
andrewboyson 22:914b970356f0 121 struct header* pHeader = (header*)pPacket;
andrewboyson 22:914b970356f0 122
andrewboyson 22:914b970356f0 123 pHeader->hardwareType = NetToHost16(ETHERNET);
andrewboyson 22:914b970356f0 124 pHeader->protocolType = NetToHost16(IPV4);
andrewboyson 22:914b970356f0 125 pHeader->hardwareLength = 6;
andrewboyson 22:914b970356f0 126 pHeader->protocolLength = 4;
andrewboyson 22:914b970356f0 127 pHeader->opCode = NetToHost16(REQUEST);
andrewboyson 22:914b970356f0 128
andrewboyson 36:900e24b27bfb 129 MacClear(pHeader->targetHardwareAddress);
andrewboyson 36:900e24b27bfb 130 pHeader->targetProtocolAddress = ArpAddressToResolve;
andrewboyson 43:bc028d5a6424 131 MacCopy (pHeader->senderHardwareAddress, MacLocal);
andrewboyson 43:bc028d5a6424 132 pHeader->senderProtocolAddress = DhcpLocalIp;
andrewboyson 22:914b970356f0 133
andrewboyson 22:914b970356f0 134 *pSize = sizeof(header);
andrewboyson 10:f0854784e960 135
andrewboyson 43:bc028d5a6424 136 if (ArpTrace)
andrewboyson 43:bc028d5a6424 137 {
andrewboyson 43:bc028d5a6424 138 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 139 LogTime("ARP send request\r\n");
andrewboyson 43:bc028d5a6424 140 logHeader(pPacket);
andrewboyson 43:bc028d5a6424 141 }
andrewboyson 43:bc028d5a6424 142 return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack);
andrewboyson 35:93c39d260a83 143 }