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:
Tue Nov 14 17:43:08 2017 +0000
Revision:
56:35117a8b5c65
Parent:
55:e64b8b47a2b6
Child:
59:e0e556c8bd46
Corrected closing FIN requirement

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 "net.h"
andrewboyson 37:793b39683406 4 #include "action.h"
andrewboyson 37:793b39683406 5 #include "dhcp.h"
andrewboyson 37:793b39683406 6 #include "eth.h"
andrewboyson 37:793b39683406 7 #include "ip.h"
andrewboyson 49:1a6336f2b3f9 8 #include "ip4addr.h"
andrewboyson 37:793b39683406 9 #include "tcp.h"
andrewboyson 37:793b39683406 10 #include "udp.h"
andrewboyson 10:f0854784e960 11
andrewboyson 10:f0854784e960 12 static uint16_t calculateChecksum(uint8_t pro, uint32_t srcIp, uint32_t dstIp, int size, void* pPacket)
andrewboyson 10:f0854784e960 13 {
andrewboyson 10:f0854784e960 14 __packed struct pseudo
andrewboyson 10:f0854784e960 15 {
andrewboyson 10:f0854784e960 16 uint32_t src;
andrewboyson 10:f0854784e960 17 uint32_t dst;
andrewboyson 10:f0854784e960 18 uint8_t zer;
andrewboyson 10:f0854784e960 19 uint8_t pro;
andrewboyson 10:f0854784e960 20 uint16_t len;
andrewboyson 10:f0854784e960 21 } pseudo;
andrewboyson 10:f0854784e960 22
andrewboyson 10:f0854784e960 23 pseudo.src = srcIp;
andrewboyson 10:f0854784e960 24 pseudo.dst = dstIp;
andrewboyson 10:f0854784e960 25 pseudo.zer = 0;
andrewboyson 10:f0854784e960 26 pseudo.pro = pro;
andrewboyson 10:f0854784e960 27 pseudo.len = NetToHost16(size);
andrewboyson 10:f0854784e960 28
andrewboyson 10:f0854784e960 29 return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket);
andrewboyson 10:f0854784e960 30 }
andrewboyson 10:f0854784e960 31 static void finalisePacket(uint8_t pro, int action, void* pPacket, int size, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 32 {
andrewboyson 10:f0854784e960 33 if (!action) return;
andrewboyson 10:f0854784e960 34
andrewboyson 49:1a6336f2b3f9 35 Ip4AddressFromDest(ActionGetDestPart(action), pDstIp);
andrewboyson 10:f0854784e960 36
andrewboyson 10:f0854784e960 37 *pSrcIp = DhcpLocalIp;
andrewboyson 10:f0854784e960 38
andrewboyson 10:f0854784e960 39 switch (pro)
andrewboyson 10:f0854784e960 40 {
andrewboyson 10:f0854784e960 41 case TCP: TcpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 42 case UDP: UdpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 43 }
andrewboyson 10:f0854784e960 44
andrewboyson 10:f0854784e960 45 uint16_t checksum = calculateChecksum(pro, *pSrcIp, *pDstIp, size, pPacket);
andrewboyson 10:f0854784e960 46
andrewboyson 10:f0854784e960 47 switch (pro)
andrewboyson 10:f0854784e960 48 {
andrewboyson 10:f0854784e960 49 case TCP: TcpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 50 case UDP: UdpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 51 }
andrewboyson 10:f0854784e960 52
andrewboyson 37:793b39683406 53 if (ActionGetTracePart(action))
andrewboyson 11:c051adb70c5a 54 {
andrewboyson 37:793b39683406 55 switch (pro)
andrewboyson 37:793b39683406 56 {
andrewboyson 56:35117a8b5c65 57 case TCP: TcpLogHeader(0); break;
andrewboyson 56:35117a8b5c65 58 case UDP: UdpLogHeader(0); break;
andrewboyson 37:793b39683406 59 }
andrewboyson 11:c051adb70c5a 60 }
andrewboyson 10:f0854784e960 61 }
andrewboyson 10:f0854784e960 62
andrewboyson 37:793b39683406 63 static void (*pTraceBack)(void);
andrewboyson 37:793b39683406 64 static int tracePacketProtocol;
andrewboyson 37:793b39683406 65 static uint16_t calculatedChecksum;
andrewboyson 37:793b39683406 66 static void trace()
andrewboyson 37:793b39683406 67 {
andrewboyson 37:793b39683406 68 pTraceBack();
andrewboyson 37:793b39683406 69 switch(tracePacketProtocol)
andrewboyson 37:793b39683406 70 {
andrewboyson 56:35117a8b5c65 71 case UDP: UdpLogHeader(calculatedChecksum); break;
andrewboyson 56:35117a8b5c65 72 case TCP: TcpLogHeader(calculatedChecksum); break;
andrewboyson 37:793b39683406 73 default: LogTimeF("UdpTcp4 - traceback unrecognised protocol %d\r\n", tracePacketProtocol); break;
andrewboyson 37:793b39683406 74 }
andrewboyson 37:793b39683406 75 }
andrewboyson 37:793b39683406 76 int Tcp4HandleReceivedPacket(void (*traceback)(void), uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 10:f0854784e960 77 {
andrewboyson 37:793b39683406 78 pTraceBack = traceback;
andrewboyson 37:793b39683406 79 tracePacketProtocol = TCP;
andrewboyson 37:793b39683406 80 calculatedChecksum = calculateChecksum(TCP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 81
andrewboyson 10:f0854784e960 82 TcpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 83
andrewboyson 37:793b39683406 84 int action = TcpHandleReceivedPacket(trace, pSize, pPacket);
andrewboyson 10:f0854784e960 85
andrewboyson 10:f0854784e960 86 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 87
andrewboyson 10:f0854784e960 88 finalisePacket(TCP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 89
andrewboyson 10:f0854784e960 90 return action;
andrewboyson 10:f0854784e960 91 }
andrewboyson 10:f0854784e960 92
andrewboyson 37:793b39683406 93 int Udp4HandleReceivedPacket(void (*traceback)(void), uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void* pPacket)
andrewboyson 10:f0854784e960 94 {
andrewboyson 37:793b39683406 95 pTraceBack = traceback;
andrewboyson 37:793b39683406 96 tracePacketProtocol = UDP;
andrewboyson 37:793b39683406 97 calculatedChecksum = calculateChecksum(UDP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 98
andrewboyson 10:f0854784e960 99 UdpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 100
andrewboyson 37:793b39683406 101 int action = UdpHandleReceivedPacket(trace, pSize, pPacket);
andrewboyson 10:f0854784e960 102
andrewboyson 10:f0854784e960 103 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 104
andrewboyson 10:f0854784e960 105 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); //Note that the ports are reversed here
andrewboyson 10:f0854784e960 106
andrewboyson 10:f0854784e960 107 return action;
andrewboyson 10:f0854784e960 108 }
andrewboyson 10:f0854784e960 109 int Udp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 110 {
andrewboyson 10:f0854784e960 111 int action = UdpPollForPacketToSend(IPV4, pSize, pPacket);
andrewboyson 10:f0854784e960 112
andrewboyson 10:f0854784e960 113 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 114
andrewboyson 10:f0854784e960 115 return action;
andrewboyson 10:f0854784e960 116 }