Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun May 12 17:17:49 2019 +0000
Revision:
144:6bd5c54efc7d
Parent:
133:a37eb35a03f1
Child:
145:206bf0d073c7
Tidied up tcp.

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