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 Oct 26 14:50:24 2017 +0000
Revision:
47:73af5c0b0dc2
Parent:
44:83ce5ace337b
Child:
57:e0fb648acf48
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 "io.h"
andrewboyson 37:793b39683406 3 #include "log.h"
andrewboyson 37:793b39683406 4 #include "net.h"
andrewboyson 37:793b39683406 5 #include "action.h"
andrewboyson 37:793b39683406 6 #include "arp.h"
andrewboyson 37:793b39683406 7 #include "ip4.h"
andrewboyson 37:793b39683406 8 #include "ip6.h"
andrewboyson 37:793b39683406 9 #include "phy.h"
andrewboyson 37:793b39683406 10 #include "eth.h"
andrewboyson 37:793b39683406 11 #include "mac.h"
andrewboyson 10:f0854784e960 12
andrewboyson 10:f0854784e960 13 #define HEADER_SIZE 14
andrewboyson 13:9cd54f7db57a 14
andrewboyson 42:222a4f45f916 15 //header variables
andrewboyson 10:f0854784e960 16 __packed struct header
andrewboyson 10:f0854784e960 17 {
andrewboyson 10:f0854784e960 18 char dst[6];
andrewboyson 10:f0854784e960 19 char src[6];
andrewboyson 10:f0854784e960 20 uint16_t typ;
andrewboyson 10:f0854784e960 21 };
andrewboyson 42:222a4f45f916 22 static uint16_t protocol;
andrewboyson 47:73af5c0b0dc2 23 void EthProtocolLog(uint16_t prototype)
andrewboyson 10:f0854784e960 24 {
andrewboyson 14:e75a59c1123d 25 switch (prototype)
andrewboyson 10:f0854784e960 26 {
andrewboyson 47:73af5c0b0dc2 27 case ARP: Log("ARP"); break;
andrewboyson 47:73af5c0b0dc2 28 case IPV4: Log("IPV4"); break;
andrewboyson 47:73af5c0b0dc2 29 case IPV6: Log("IPV6"); break;
andrewboyson 47:73af5c0b0dc2 30 default: LogF("%04hX", prototype); break;
andrewboyson 10:f0854784e960 31 }
andrewboyson 10:f0854784e960 32 }
andrewboyson 10:f0854784e960 33
andrewboyson 47:73af5c0b0dc2 34 static void finalisePacket(int dest, int dataLength, void* pPacket, int* pSize)
andrewboyson 10:f0854784e960 35 {
andrewboyson 47:73af5c0b0dc2 36 if (!dest) return;
andrewboyson 14:e75a59c1123d 37
andrewboyson 10:f0854784e960 38 struct header * pHeader = (header*)pPacket;
andrewboyson 14:e75a59c1123d 39
andrewboyson 47:73af5c0b0dc2 40 MacMakeFromDest(dest, protocol, pHeader->dst);
andrewboyson 10:f0854784e960 41
andrewboyson 36:900e24b27bfb 42 MacCopy(pHeader->src, MacLocal); //Put our MAC into the source
andrewboyson 42:222a4f45f916 43 pHeader->typ = NetToHost16(protocol);
andrewboyson 10:f0854784e960 44
andrewboyson 10:f0854784e960 45 *pSize = HEADER_SIZE + dataLength;
andrewboyson 10:f0854784e960 46 }
andrewboyson 37:793b39683406 47 void LogHeader(struct header* pHeader)
andrewboyson 13:9cd54f7db57a 48 {
andrewboyson 43:bc028d5a6424 49 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 50 {
andrewboyson 47:73af5c0b0dc2 51 Log("ETH header\r\n");
andrewboyson 47:73af5c0b0dc2 52 Log(" Destination: "); MacLog(pHeader->dst); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 53 Log(" Source: "); MacLog(pHeader->src); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 54 Log(" EtherType: "); EthProtocolLog(NetToHost16(pHeader->typ)); Log("\r\n");
andrewboyson 43:bc028d5a6424 55 }
andrewboyson 43:bc028d5a6424 56 else
andrewboyson 43:bc028d5a6424 57 {
andrewboyson 44:83ce5ace337b 58 Log("ETH header ");
andrewboyson 47:73af5c0b0dc2 59 EthProtocolLog(NetToHost16(pHeader->typ));
andrewboyson 43:bc028d5a6424 60 Log(" ");
andrewboyson 47:73af5c0b0dc2 61 MacLog(pHeader->src);
andrewboyson 43:bc028d5a6424 62 Log(" >>> ");
andrewboyson 47:73af5c0b0dc2 63 MacLog(pHeader->dst);
andrewboyson 43:bc028d5a6424 64 Log("\r\n");
andrewboyson 43:bc028d5a6424 65 }
andrewboyson 13:9cd54f7db57a 66 }
andrewboyson 37:793b39683406 67 static void (*pTraceBack)(void);
andrewboyson 37:793b39683406 68 static void* tracePacket;
andrewboyson 37:793b39683406 69 static void trace()
andrewboyson 10:f0854784e960 70 {
andrewboyson 37:793b39683406 71 pTraceBack();
andrewboyson 37:793b39683406 72 struct header * pHeader = (header*)tracePacket;
andrewboyson 37:793b39683406 73 LogHeader(pHeader);
andrewboyson 37:793b39683406 74 }
andrewboyson 37:793b39683406 75 int EthHandlePacket(void (*traceback)(void), void* pPacket, int* pSize)
andrewboyson 37:793b39683406 76 {
andrewboyson 37:793b39683406 77 pTraceBack = traceback;
andrewboyson 37:793b39683406 78 tracePacket = pPacket;
andrewboyson 10:f0854784e960 79 struct header * pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 80 int dataLength = *pSize - HEADER_SIZE;
andrewboyson 10:f0854784e960 81 void* pData = (char*)pPacket + HEADER_SIZE;
andrewboyson 14:e75a59c1123d 82
andrewboyson 14:e75a59c1123d 83 if (!MacAccept(pHeader->dst)) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 84
andrewboyson 42:222a4f45f916 85 protocol = NetToHost16(pHeader->typ);
andrewboyson 42:222a4f45f916 86 if (protocol < 1500) return DO_NOTHING; //drop 802.3 messages
andrewboyson 10:f0854784e960 87
andrewboyson 10:f0854784e960 88 int action = DO_NOTHING;
andrewboyson 42:222a4f45f916 89 switch (protocol)
andrewboyson 10:f0854784e960 90 {
andrewboyson 37:793b39683406 91 case ARP: action = ArpHandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break;
andrewboyson 37:793b39683406 92 case IPV4: action = Ip4HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break;
andrewboyson 37:793b39683406 93 case IPV6: action = Ip6HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break;
andrewboyson 28:edc17eeb4142 94 case 0x6970: break; //Drop Sonos group membership packet
andrewboyson 16:f416ef583c89 95 case 0x7374: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 96 case 0x7475: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 97 case 0x7380: break; //Drop Sky Q packet
andrewboyson 23:b641979389b2 98 case 0x8100: break; //Drop Sky Q VLAN 802.1Q packet
andrewboyson 33:714a0345e59b 99 case 0x887b: break; //Drop Sky Q packet
andrewboyson 10:f0854784e960 100 default:
andrewboyson 42:222a4f45f916 101 LogTimeF("ETH protocol %d not handled", protocol);
andrewboyson 10:f0854784e960 102 break;
andrewboyson 10:f0854784e960 103 }
andrewboyson 47:73af5c0b0dc2 104 if (!action) return DO_NOTHING;
andrewboyson 10:f0854784e960 105
andrewboyson 47:73af5c0b0dc2 106 finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize);
andrewboyson 37:793b39683406 107
andrewboyson 37:793b39683406 108 if (ActionGetTracePart(action)) LogHeader(pHeader);
andrewboyson 37:793b39683406 109
andrewboyson 10:f0854784e960 110 return action;
andrewboyson 10:f0854784e960 111 }
andrewboyson 10:f0854784e960 112 int EthPollForPacketToSend(void* pPacket, int* pSize)
andrewboyson 10:f0854784e960 113 {
andrewboyson 10:f0854784e960 114 struct header * pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 115 void* pData = (char*)pPacket + HEADER_SIZE;
andrewboyson 10:f0854784e960 116
andrewboyson 10:f0854784e960 117 int dataLength = 0;
andrewboyson 42:222a4f45f916 118 protocol = 0;
andrewboyson 10:f0854784e960 119 int action = DO_NOTHING;
andrewboyson 10:f0854784e960 120
andrewboyson 47:73af5c0b0dc2 121 if (!action)
andrewboyson 10:f0854784e960 122 {
andrewboyson 22:914b970356f0 123 action = ArpPollForPacketToSend(pData, &dataLength);
andrewboyson 42:222a4f45f916 124 protocol = ARP;
andrewboyson 22:914b970356f0 125 }
andrewboyson 22:914b970356f0 126
andrewboyson 47:73af5c0b0dc2 127 if (!action)
andrewboyson 22:914b970356f0 128 {
andrewboyson 10:f0854784e960 129 action = Ip6PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 130 protocol = IPV6;
andrewboyson 10:f0854784e960 131 }
andrewboyson 10:f0854784e960 132
andrewboyson 47:73af5c0b0dc2 133 if (!action)
andrewboyson 10:f0854784e960 134 {
andrewboyson 10:f0854784e960 135 action = Ip4PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 136 protocol = IPV4;
andrewboyson 10:f0854784e960 137 }
andrewboyson 10:f0854784e960 138
andrewboyson 47:73af5c0b0dc2 139 if (!action) return DO_NOTHING;
andrewboyson 47:73af5c0b0dc2 140
andrewboyson 47:73af5c0b0dc2 141 finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize);
andrewboyson 10:f0854784e960 142
andrewboyson 37:793b39683406 143 if (ActionGetTracePart(action)) LogHeader(pHeader);
andrewboyson 13:9cd54f7db57a 144
andrewboyson 10:f0854784e960 145 return action;
andrewboyson 10:f0854784e960 146 }
andrewboyson 10:f0854784e960 147