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:
Wed Dec 05 18:30:37 2018 +0000
Revision:
94:e2973a2c488e
Parent:
93:580fc113d9e9
Child:
111:3600389d1add
Fixed bug - incorrect MSS being sent from a polled sync: expected 1440 but had -60. Traced to buffer datalength in EthPollForPacketToSend being set to zero instead of being calculated from the buffer length - headersize.

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