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 15 16:55:29 2018 +0000
Revision:
80:4ef1500fca1d
Parent:
74:c3756bfa960e
Child:
86:55bc5ddac16c
Used local ip scope to determine the local address used in TCP.

Who changed what in which revision?

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