Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Jul 31 15:10:53 2019 +0000
Revision:
154:ba9879b19d9f
Parent:
147:a6093b52e654
Child:
155:22f249751106
Amalgamated Reply into Poll function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 74:c3756bfa960e 1 #include <stdint.h>
andrewboyson 74:c3756bfa960e 2 #include <stdbool.h>
andrewboyson 90:955f4c6e18a9 3 #include <stdarg.h>
andrewboyson 74:c3756bfa960e 4
andrewboyson 93:580fc113d9e9 5 #include "log.h"
andrewboyson 93:580fc113d9e9 6 #include "net.h"
andrewboyson 93:580fc113d9e9 7 #include "action.h"
andrewboyson 93:580fc113d9e9 8 #include "tcp.h"
andrewboyson 144:6bd5c54efc7d 9 #include "tcpbuf.h"
andrewboyson 93:580fc113d9e9 10 #include "tcphdr.h"
andrewboyson 93:580fc113d9e9 11 #include "tcb.h"
andrewboyson 93:580fc113d9e9 12 #include "ip4.h"
andrewboyson 93:580fc113d9e9 13 #include "dhcp.h"
andrewboyson 146:0fc66d610fd6 14 #include "httpshim.h"
andrewboyson 93:580fc113d9e9 15 #include "led.h"
andrewboyson 74:c3756bfa960e 16 #include "tcpsend.h"
andrewboyson 93:580fc113d9e9 17 #include "mstimer.h"
andrewboyson 74:c3756bfa960e 18
andrewboyson 120:05b6d67a0cec 19 #define TIMEOUT_RETRANSMISSION_MS 700
andrewboyson 120:05b6d67a0cec 20 #define MAX_RETRANSMISSIONS 5
andrewboyson 120:05b6d67a0cec 21 #define TIMEOUT_BROKEN_LINK_MS 600000
andrewboyson 74:c3756bfa960e 22
andrewboyson 90:955f4c6e18a9 23 static void log(uint16_t remPort, char* fmt, ...)
andrewboyson 90:955f4c6e18a9 24 {
andrewboyson 90:955f4c6e18a9 25 if (TcpTrace)
andrewboyson 90:955f4c6e18a9 26 {
andrewboyson 90:955f4c6e18a9 27 if (NetTraceNewLine) Log("\r\n");
andrewboyson 90:955f4c6e18a9 28 LogTimeF("TCP port %hu - ", remPort);
andrewboyson 90:955f4c6e18a9 29 va_list argptr;
andrewboyson 90:955f4c6e18a9 30 va_start(argptr, fmt);
andrewboyson 90:955f4c6e18a9 31 LogV(fmt, argptr);
andrewboyson 90:955f4c6e18a9 32 va_end(argptr);
andrewboyson 90:955f4c6e18a9 33 Log("\r\n");
andrewboyson 90:955f4c6e18a9 34 }
andrewboyson 90:955f4c6e18a9 35 }
andrewboyson 90:955f4c6e18a9 36
andrewboyson 88:1ba13e6062a3 37 static bool doTrace(uint16_t port)
andrewboyson 88:1ba13e6062a3 38 {
andrewboyson 88:1ba13e6062a3 39 switch (port)
andrewboyson 88:1ba13e6062a3 40 {
andrewboyson 146:0fc66d610fd6 41 case 80:
andrewboyson 146:0fc66d610fd6 42 case 443: return HttpShimGetTrace();
andrewboyson 146:0fc66d610fd6 43 default: return false;
andrewboyson 88:1ba13e6062a3 44 }
andrewboyson 88:1ba13e6062a3 45 }
andrewboyson 147:a6093b52e654 46
andrewboyson 147:a6093b52e654 47 static bool addAppData(int *pDataLength, void* pPacket, uint16_t port, uint32_t start, int mss, char* pAppState, char* pTlsState, bool clientFinished)
andrewboyson 147:a6093b52e654 48 {
andrewboyson 74:c3756bfa960e 49 char* pData = (char*)pPacket + TcpHdrSizeGet();
andrewboyson 145:206bf0d073c7 50 TcpBufStart(start, mss, pData);
andrewboyson 145:206bf0d073c7 51 bool finished = false;
andrewboyson 75:603b10404183 52 switch (port)
andrewboyson 74:c3756bfa960e 53 {
andrewboyson 154:ba9879b19d9f 54 case 80: finished = HttpShimPoll(clientFinished, pAppState, pTlsState, false); break;
andrewboyson 154:ba9879b19d9f 55 case 443: finished = HttpShimPoll(clientFinished, pAppState, pTlsState, true ); break;
andrewboyson 144:6bd5c54efc7d 56 }
andrewboyson 145:206bf0d073c7 57 *pDataLength = TcpBufLength();
andrewboyson 145:206bf0d073c7 58 return finished;
andrewboyson 75:603b10404183 59 }
andrewboyson 89:9b765a67699b 60 static int preparePacket(void* pPacket, struct tcb* pTcb, int dataLength, int* pSize)
andrewboyson 75:603b10404183 61 {
andrewboyson 75:603b10404183 62 //Set the acknowledge flag
andrewboyson 75:603b10404183 63 TcpHdrACK = true;
andrewboyson 74:c3756bfa960e 64
andrewboyson 75:603b10404183 65 //Swap the ports for the reply
andrewboyson 89:9b765a67699b 66 TcpHdrSrcPort = pTcb->locPort;
andrewboyson 89:9b765a67699b 67 TcpHdrDstPort = pTcb->remPort;
andrewboyson 75:603b10404183 68
andrewboyson 75:603b10404183 69 //Specify the receive window size to not throttle
andrewboyson 75:603b10404183 70 TcpHdrWindow = 4000;
andrewboyson 75:603b10404183 71
andrewboyson 75:603b10404183 72 //Write the header
andrewboyson 75:603b10404183 73 TcpHdrWriteToPacket(pPacket);
andrewboyson 75:603b10404183 74
andrewboyson 75:603b10404183 75 //Calculate the size of the reply
andrewboyson 75:603b10404183 76 *pSize = TcpHdrSizeGet() + dataLength;
andrewboyson 75:603b10404183 77
andrewboyson 89:9b765a67699b 78 return ActionMakeFromDestAndTrace(UNICAST, doTrace(pTcb->locPort) && NetTraceStack);
andrewboyson 74:c3756bfa960e 79 }
andrewboyson 75:603b10404183 80 int TcpSend(int* pSize, void* pPacket, struct tcb* pTcb)
andrewboyson 74:c3756bfa960e 81 {
andrewboyson 74:c3756bfa960e 82 int dataLength = 0;
andrewboyson 74:c3756bfa960e 83 TcpHdrMakeEmpty();
andrewboyson 74:c3756bfa960e 84 int locMss = *pSize - TcpHdrSizeGet();
andrewboyson 74:c3756bfa960e 85 switch (pTcb->state)
andrewboyson 74:c3756bfa960e 86 {
andrewboyson 74:c3756bfa960e 87 case TCB_SYN_RECEIVED:
andrewboyson 74:c3756bfa960e 88 if (pTcb->bytesSentToRem == 0)
andrewboyson 74:c3756bfa960e 89 {
andrewboyson 74:c3756bfa960e 90 TcpHdrMssSet(locMss);
andrewboyson 74:c3756bfa960e 91 TcpHdrSYN = true;
andrewboyson 74:c3756bfa960e 92 }
andrewboyson 74:c3756bfa960e 93 break;
andrewboyson 74:c3756bfa960e 94
andrewboyson 74:c3756bfa960e 95 case TCB_ESTABLISHED:
andrewboyson 74:c3756bfa960e 96 if (!pTcb->sentFin)
andrewboyson 74:c3756bfa960e 97 {
andrewboyson 74:c3756bfa960e 98 if (pTcb->bytesSentToRem - pTcb->bytesAckdByRem < pTcb->window)
andrewboyson 74:c3756bfa960e 99 {
andrewboyson 147:a6093b52e654 100 bool finished = addAppData(&dataLength, pPacket, pTcb->locPort, pTcb->bytesSentToRem - 1, pTcb->remMss, pTcb->appData, pTcb->tlsData, pTcb->rcvdFin);
andrewboyson 145:206bf0d073c7 101 if (finished)
andrewboyson 74:c3756bfa960e 102 {
andrewboyson 144:6bd5c54efc7d 103 TcpHdrFIN = true;
andrewboyson 144:6bd5c54efc7d 104 pTcb->sentFin = true;
andrewboyson 74:c3756bfa960e 105 }
andrewboyson 74:c3756bfa960e 106 }
andrewboyson 74:c3756bfa960e 107 }
andrewboyson 74:c3756bfa960e 108 break;
andrewboyson 74:c3756bfa960e 109 }
andrewboyson 74:c3756bfa960e 110
andrewboyson 79:f50e02fb5c94 111 //Handle the acknowledgement of received bytes
andrewboyson 74:c3756bfa960e 112 bool rcvdSeqHasAdvanced = pTcb->bytesRcvdFromRem > pTcb->bytesAckdToRem;
andrewboyson 74:c3756bfa960e 113 pTcb->bytesAckdToRem = pTcb->bytesRcvdFromRem;
andrewboyson 79:f50e02fb5c94 114 TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 74:c3756bfa960e 115
andrewboyson 79:f50e02fb5c94 116 //Specify the start of the data being sent
andrewboyson 79:f50e02fb5c94 117 uint32_t seqToSend = pTcb->bytesSentToRem;
andrewboyson 79:f50e02fb5c94 118 TcpHdrSeqNum = seqToSend + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 74:c3756bfa960e 119
andrewboyson 74:c3756bfa960e 120 //Record the number of bytes sent
andrewboyson 74:c3756bfa960e 121 uint32_t bytesToSend = 0;
andrewboyson 74:c3756bfa960e 122 if (TcpHdrSYN) bytesToSend += 1; //Add one to acknowledge the SYN
andrewboyson 74:c3756bfa960e 123 bytesToSend += dataLength; //Add the number of bytes received
andrewboyson 74:c3756bfa960e 124 if (TcpHdrFIN) bytesToSend += 1; //Add one to acknowledge the FIN
andrewboyson 74:c3756bfa960e 125
andrewboyson 74:c3756bfa960e 126 pTcb->bytesSentToRem += bytesToSend;
andrewboyson 74:c3756bfa960e 127
andrewboyson 79:f50e02fb5c94 128 //Only send a packet if have bytes or an acknowledgement to send
andrewboyson 75:603b10404183 129 if (!rcvdSeqHasAdvanced && !bytesToSend) return DO_NOTHING;
andrewboyson 79:f50e02fb5c94 130
andrewboyson 89:9b765a67699b 131 return preparePacket(pPacket, pTcb, dataLength, pSize);
andrewboyson 75:603b10404183 132 }
andrewboyson 75:603b10404183 133 int TcpResendLastUnAcked(int* pSize, void *pPacket, struct tcb* pTcb)
andrewboyson 75:603b10404183 134 {
andrewboyson 75:603b10404183 135 int dataLength = 0;
andrewboyson 75:603b10404183 136 TcpHdrMakeEmpty();
andrewboyson 75:603b10404183 137 int locMss = *pSize - TcpHdrSizeGet();
andrewboyson 79:f50e02fb5c94 138 uint32_t seqNum = pTcb->bytesAckdByRem;
andrewboyson 75:603b10404183 139 switch (pTcb->state)
andrewboyson 74:c3756bfa960e 140 {
andrewboyson 75:603b10404183 141 case TCB_SYN_RECEIVED:
andrewboyson 75:603b10404183 142 TcpHdrMssSet(locMss);
andrewboyson 75:603b10404183 143 TcpHdrSYN = true;
andrewboyson 75:603b10404183 144 break;
andrewboyson 74:c3756bfa960e 145
andrewboyson 75:603b10404183 146 case TCB_ESTABLISHED:
andrewboyson 79:f50e02fb5c94 147 case TCB_CLOSE_FIN_WAIT:
andrewboyson 75:603b10404183 148 {
andrewboyson 147:a6093b52e654 149 bool finished = addAppData(&dataLength, pPacket, pTcb->locPort, seqNum - 1, pTcb->remMss, pTcb->appData, pTcb->tlsData, pTcb->rcvdFin);
andrewboyson 145:206bf0d073c7 150 if (finished)
andrewboyson 75:603b10404183 151 {
andrewboyson 75:603b10404183 152 TcpHdrFIN = true;
andrewboyson 75:603b10404183 153 pTcb->sentFin = true;
andrewboyson 75:603b10404183 154 }
andrewboyson 145:206bf0d073c7 155 break;
andrewboyson 75:603b10404183 156 }
andrewboyson 74:c3756bfa960e 157 }
andrewboyson 75:603b10404183 158
andrewboyson 75:603b10404183 159 TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 75:603b10404183 160 TcpHdrSeqNum = seqNum + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 79:f50e02fb5c94 161
andrewboyson 89:9b765a67699b 162 return preparePacket(pPacket, pTcb, dataLength, pSize);
andrewboyson 75:603b10404183 163 }
andrewboyson 75:603b10404183 164 int TcpResendLastAck(int* pSize, void *pPacket, struct tcb* pTcb)
andrewboyson 75:603b10404183 165 {
andrewboyson 75:603b10404183 166 int dataLength = 0;
andrewboyson 75:603b10404183 167 TcpHdrMakeEmpty();
andrewboyson 75:603b10404183 168 TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 75:603b10404183 169 TcpHdrSeqNum = pTcb->bytesSentToRem + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 75:603b10404183 170
andrewboyson 89:9b765a67699b 171 return preparePacket(pPacket, pTcb, dataLength, pSize);
andrewboyson 75:603b10404183 172 }
andrewboyson 75:603b10404183 173 int TcpSendReset(int* pSize, void *pPacket, struct tcb* pTcb)
andrewboyson 75:603b10404183 174 {
andrewboyson 75:603b10404183 175 int dataLength = 0;
andrewboyson 75:603b10404183 176 TcpHdrMakeEmpty();
andrewboyson 75:603b10404183 177 TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 75:603b10404183 178 TcpHdrSeqNum = pTcb->bytesSentToRem + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 75:603b10404183 179
andrewboyson 75:603b10404183 180 TcpHdrRST = true;
andrewboyson 75:603b10404183 181
andrewboyson 89:9b765a67699b 182 return preparePacket(pPacket, pTcb, dataLength, pSize);
andrewboyson 74:c3756bfa960e 183 }
andrewboyson 79:f50e02fb5c94 184 int TcpSendClose(int* pSize, void *pPacket, struct tcb* pTcb)
andrewboyson 79:f50e02fb5c94 185 {
andrewboyson 79:f50e02fb5c94 186 int dataLength = 0;
andrewboyson 79:f50e02fb5c94 187 TcpHdrMakeEmpty();
andrewboyson 79:f50e02fb5c94 188 TcpHdrFIN = true;
andrewboyson 79:f50e02fb5c94 189 pTcb->sentFin = true;
andrewboyson 79:f50e02fb5c94 190 pTcb->bytesSentToRem += 1; //For the FIN
andrewboyson 79:f50e02fb5c94 191 TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 79:f50e02fb5c94 192 TcpHdrSeqNum = pTcb->bytesSentToRem + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 79:f50e02fb5c94 193
andrewboyson 89:9b765a67699b 194 return preparePacket(pPacket, pTcb, dataLength, pSize);
andrewboyson 79:f50e02fb5c94 195 }
andrewboyson 79:f50e02fb5c94 196
andrewboyson 80:4ef1500fca1d 197 int TcpPollForPacketToSend(int* pSize, void* pPacket, int ipType, int* pRemArIndex, int* pLocIpScope)
andrewboyson 74:c3756bfa960e 198 {
andrewboyson 83:08c983006a6e 199 //Loops around the TCBs, moving on if empty but staying if not the right type
andrewboyson 79:f50e02fb5c94 200 static struct tcb* pTcb = NULL; //Passing a pointer containing NULL to TcbGetNext causes it to return the first TCB
andrewboyson 79:f50e02fb5c94 201 static bool stay = false;
andrewboyson 83:08c983006a6e 202 if (!stay) pTcb = TcbGetNext(pTcb);
andrewboyson 79:f50e02fb5c94 203 stay = false;
andrewboyson 78:9d8fc88df405 204 if (!pTcb->state) return DO_NOTHING;
andrewboyson 79:f50e02fb5c94 205 if (pTcb->ipType != ipType)
andrewboyson 79:f50e02fb5c94 206 {
andrewboyson 79:f50e02fb5c94 207 stay = true;
andrewboyson 79:f50e02fb5c94 208 return DO_NOTHING;
andrewboyson 79:f50e02fb5c94 209 }
andrewboyson 79:f50e02fb5c94 210
andrewboyson 79:f50e02fb5c94 211 //Check and make available the remote AR index
andrewboyson 79:f50e02fb5c94 212 if (pTcb->remArIndex < 0)
andrewboyson 79:f50e02fb5c94 213 {
andrewboyson 90:955f4c6e18a9 214 log(pTcb->remPort, "missing remote AR index -> reaping TCB");
andrewboyson 79:f50e02fb5c94 215 pTcb->state = TCB_EMPTY;
andrewboyson 79:f50e02fb5c94 216 return DO_NOTHING;
andrewboyson 79:f50e02fb5c94 217 }
andrewboyson 79:f50e02fb5c94 218 *pRemArIndex = pTcb->remArIndex;
andrewboyson 80:4ef1500fca1d 219
andrewboyson 80:4ef1500fca1d 220 //Return the local IP scope
andrewboyson 80:4ef1500fca1d 221 if (pLocIpScope) *pLocIpScope = pTcb->locIpScope;
andrewboyson 87:40d46a979cdb 222
andrewboyson 79:f50e02fb5c94 223 //Reap old ones
andrewboyson 133:a37eb35a03f1 224 if (MsTimerRelative(pTcb->timeLastRcvd, TIMEOUT_BROKEN_LINK_MS))
andrewboyson 79:f50e02fb5c94 225 {
andrewboyson 90:955f4c6e18a9 226 log(pTcb->remPort, "broken link -> reaping TCB");
andrewboyson 79:f50e02fb5c94 227 pTcb->state = TCB_EMPTY;
andrewboyson 79:f50e02fb5c94 228 return DO_NOTHING;
andrewboyson 79:f50e02fb5c94 229 }
andrewboyson 78:9d8fc88df405 230
andrewboyson 79:f50e02fb5c94 231 //Reset the RTO if all bytes are acknowledged
andrewboyson 79:f50e02fb5c94 232 if (pTcb->bytesSentToRem == pTcb->bytesAckdByRem)
andrewboyson 79:f50e02fb5c94 233 {
andrewboyson 93:580fc113d9e9 234 pTcb->timeSendsBeingAcked = MsTimerCount;
andrewboyson 82:20781198d26d 235 pTcb->countSendsNotAcked = 0;
andrewboyson 79:f50e02fb5c94 236 }
andrewboyson 75:603b10404183 237
andrewboyson 79:f50e02fb5c94 238 //Check if have unacknowledged send bytes after the RTO
andrewboyson 133:a37eb35a03f1 239 if (MsTimerRelative(pTcb->timeSendsBeingAcked, TIMEOUT_RETRANSMISSION_MS))
andrewboyson 75:603b10404183 240 {
andrewboyson 82:20781198d26d 241 pTcb->countSendsNotAcked++;
andrewboyson 82:20781198d26d 242 if (pTcb->countSendsNotAcked > MAX_RETRANSMISSIONS)
andrewboyson 82:20781198d26d 243 {
andrewboyson 90:955f4c6e18a9 244 log(pTcb->remPort, "reached maximum retransmissions -> sending reset");
andrewboyson 90:955f4c6e18a9 245 pTcb->state = TCB_EMPTY;
andrewboyson 90:955f4c6e18a9 246 return TcpSendReset(pSize, pPacket, pTcb);
andrewboyson 82:20781198d26d 247 }
andrewboyson 90:955f4c6e18a9 248 else
andrewboyson 79:f50e02fb5c94 249 {
andrewboyson 90:955f4c6e18a9 250 log(pTcb->remPort, "only had ack of %lu while sent %lu -> resending", pTcb->bytesAckdByRem, pTcb->bytesSentToRem);
andrewboyson 93:580fc113d9e9 251 pTcb->timeSendsBeingAcked = MsTimerCount;
andrewboyson 90:955f4c6e18a9 252 return TcpResendLastUnAcked(pSize, pPacket, pTcb);
andrewboyson 79:f50e02fb5c94 253 }
andrewboyson 75:603b10404183 254 }
andrewboyson 90:955f4c6e18a9 255 else
andrewboyson 90:955f4c6e18a9 256 {
andrewboyson 90:955f4c6e18a9 257 //If haven't had to do anything else then do a normal send
andrewboyson 90:955f4c6e18a9 258 return TcpSend(pSize, pPacket, pTcb);
andrewboyson 90:955f4c6e18a9 259 }
andrewboyson 74:c3756bfa960e 260 }
andrewboyson 74:c3756bfa960e 261