Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Fri May 17 15:01:32 2019 +0000
Revision:
147:a6093b52e654
Parent:
146:0fc66d610fd6
Child:
154:ba9879b19d9f
Split HttpPollReply into HttpPoll and HttpReply to allow TSL to work

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