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 Jan 02 17:48:38 2019 +0000
Revision:
97:d91f7db00235
Parent:
96:43eb7a110f1a
Child:
98:b977424ec7f7
Added fault points

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 75:603b10404183 3 #include <stdarg.h>
andrewboyson 74:c3756bfa960e 4
andrewboyson 74:c3756bfa960e 5 #include "log.h"
andrewboyson 74:c3756bfa960e 6 #include "net.h"
andrewboyson 74:c3756bfa960e 7 #include "action.h"
andrewboyson 74:c3756bfa960e 8 #include "tcp.h"
andrewboyson 74:c3756bfa960e 9 #include "tcphdr.h"
andrewboyson 74:c3756bfa960e 10 #include "tcpsend.h"
andrewboyson 74:c3756bfa960e 11 #include "tcb.h"
andrewboyson 74:c3756bfa960e 12 #include "ip4.h"
andrewboyson 74:c3756bfa960e 13 #include "dhcp.h"
andrewboyson 74:c3756bfa960e 14 #include "http.h"
andrewboyson 74:c3756bfa960e 15 #include "led.h"
andrewboyson 93:580fc113d9e9 16 #include "mstimer.h"
andrewboyson 97:d91f7db00235 17 #include "fault.h"
andrewboyson 89:9b765a67699b 18
andrewboyson 89:9b765a67699b 19 static void log(void (*traceback)(void), char* fmt, ...)
andrewboyson 74:c3756bfa960e 20 {
andrewboyson 78:9d8fc88df405 21 if (TcpTrace)
andrewboyson 78:9d8fc88df405 22 {
andrewboyson 78:9d8fc88df405 23 if (NetTraceNewLine) Log("\r\n");
andrewboyson 89:9b765a67699b 24 LogTimeF("TCP port %hu - ", TcpHdrSrcPort);
andrewboyson 75:603b10404183 25 va_list argptr;
andrewboyson 75:603b10404183 26 va_start(argptr, fmt);
andrewboyson 75:603b10404183 27 LogV(fmt, argptr);
andrewboyson 75:603b10404183 28 va_end(argptr);
andrewboyson 75:603b10404183 29 Log("\r\n");
andrewboyson 75:603b10404183 30 if (NetTraceStack) traceback();
andrewboyson 75:603b10404183 31 }
andrewboyson 75:603b10404183 32 }
andrewboyson 75:603b10404183 33
andrewboyson 88:1ba13e6062a3 34 static void handleSyn(void *pPacket, int ipType, int remArIndex, int locMss, struct tcb* pTcb)
andrewboyson 74:c3756bfa960e 35 {
andrewboyson 74:c3756bfa960e 36 //Get the MSS to use for sends - it is the lower of the MSS advertised by the remote host and our local MSS
andrewboyson 74:c3756bfa960e 37 int remMss = TcpHdrMssGet();
andrewboyson 74:c3756bfa960e 38 pTcb->remMss = remMss ? remMss : 536; //default MSS for IPv4 [576 - 20(TCP) - 20(IP)];
andrewboyson 74:c3756bfa960e 39 if (pTcb->remMss > locMss) pTcb->remMss = locMss;
andrewboyson 74:c3756bfa960e 40
andrewboyson 93:580fc113d9e9 41 pTcb->timeSendsBeingAcked = MsTimerCount;
andrewboyson 82:20781198d26d 42 pTcb->countSendsNotAcked = 0;
andrewboyson 79:f50e02fb5c94 43 pTcb->rcvdFin = false;
andrewboyson 79:f50e02fb5c94 44 pTcb->sentFin = false;
andrewboyson 79:f50e02fb5c94 45 pTcb->todo = 0;
andrewboyson 79:f50e02fb5c94 46 pTcb->remIsn = TcpHdrSeqNum;
andrewboyson 79:f50e02fb5c94 47 pTcb->locIsn = TcbGetIsn();
andrewboyson 79:f50e02fb5c94 48 pTcb->bytesRcvdFromRem = 0;
andrewboyson 79:f50e02fb5c94 49 pTcb->bytesAckdByRem = 0;
andrewboyson 79:f50e02fb5c94 50 pTcb->bytesAckdToRem = 0;
andrewboyson 79:f50e02fb5c94 51 pTcb->bytesSentToRem = 0;
andrewboyson 74:c3756bfa960e 52 }
andrewboyson 88:1ba13e6062a3 53 static void handleReceivedData(void* pPacket, int dataLength, uint32_t position, struct tcb* pTcb)
andrewboyson 74:c3756bfa960e 54 {
andrewboyson 74:c3756bfa960e 55 pTcb->window = TcpHdrWindow;
andrewboyson 74:c3756bfa960e 56 char* pData = (char*)pPacket + TcpHdrSizeGet();
andrewboyson 74:c3756bfa960e 57 switch (pTcb->locPort)
andrewboyson 74:c3756bfa960e 58 {
andrewboyson 74:c3756bfa960e 59 case 80:
andrewboyson 75:603b10404183 60 HttpHandleRequest(dataLength, pData, position, &pTcb->todo);
andrewboyson 74:c3756bfa960e 61 break;
andrewboyson 74:c3756bfa960e 62 default:
andrewboyson 74:c3756bfa960e 63 break;
andrewboyson 74:c3756bfa960e 64 }
andrewboyson 74:c3756bfa960e 65 }
andrewboyson 90:955f4c6e18a9 66 static int sendResetFromPacket(int* pSizeTx, void* pPacketTx, int ipType, int remArIndex, int locIpScope, int seqLengthRcvd)
andrewboyson 89:9b765a67699b 67 {
andrewboyson 90:955f4c6e18a9 68 /*RFC793 p36 If the connection does not exist (CLOSED) then a reset is sent
andrewboyson 90:955f4c6e18a9 69 in response to any incoming segment except another reset.
andrewboyson 90:955f4c6e18a9 70 If the incoming segment has an ACK field, the reset takes its sequence number from the ACK field of the segment,
andrewboyson 90:955f4c6e18a9 71 otherwise the reset has sequence number zero
andrewboyson 90:955f4c6e18a9 72 and
andrewboyson 90:955f4c6e18a9 73 the ACK field is set to the sum of the sequence number and segment length of the incoming segment.
andrewboyson 90:955f4c6e18a9 74 The connection remains in the CLOSED state.
andrewboyson 90:955f4c6e18a9 75 In TcpSendReset TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
andrewboyson 90:955f4c6e18a9 76 TcpHdrSeqNum = pTcb->bytesSentToRem + pTcb->locIsn; //Set up the start of the message before adding the bytes sent
andrewboyson 90:955f4c6e18a9 77 */
andrewboyson 90:955f4c6e18a9 78
andrewboyson 89:9b765a67699b 79 struct tcb tcb;
andrewboyson 89:9b765a67699b 80 struct tcb* pTcb = &tcb;
andrewboyson 93:580fc113d9e9 81 pTcb->timeLastRcvd = MsTimerCount;
andrewboyson 89:9b765a67699b 82 pTcb->remArIndex = remArIndex;
andrewboyson 89:9b765a67699b 83 pTcb->ipType = ipType;
andrewboyson 89:9b765a67699b 84 pTcb->locIpScope = locIpScope;
andrewboyson 89:9b765a67699b 85 pTcb->remPort = TcpHdrSrcPort;
andrewboyson 89:9b765a67699b 86 pTcb->locPort = TcpHdrDstPort;
andrewboyson 89:9b765a67699b 87 pTcb->window = TcpHdrWindow;
andrewboyson 89:9b765a67699b 88 pTcb->state = TCB_EMPTY;
andrewboyson 89:9b765a67699b 89
andrewboyson 93:580fc113d9e9 90 pTcb->timeSendsBeingAcked = MsTimerCount;
andrewboyson 90:955f4c6e18a9 91 pTcb->countSendsNotAcked = 0;
andrewboyson 90:955f4c6e18a9 92 pTcb->rcvdFin = false;
andrewboyson 90:955f4c6e18a9 93 pTcb->sentFin = false;
andrewboyson 90:955f4c6e18a9 94 pTcb->todo = 0;
andrewboyson 90:955f4c6e18a9 95 pTcb->remIsn = TcpHdrSeqNum + seqLengthRcvd; //Ack number
andrewboyson 90:955f4c6e18a9 96 pTcb->locIsn = TcpHdrACK ? TcpHdrAckNum : 0; //Seq number
andrewboyson 90:955f4c6e18a9 97 pTcb->bytesRcvdFromRem = 0;
andrewboyson 90:955f4c6e18a9 98 pTcb->bytesAckdByRem = 0;
andrewboyson 90:955f4c6e18a9 99 pTcb->bytesAckdToRem = 0;
andrewboyson 90:955f4c6e18a9 100 pTcb->bytesSentToRem = 0;
andrewboyson 90:955f4c6e18a9 101
andrewboyson 89:9b765a67699b 102 return TcpSendReset(pSizeTx, pPacketTx, pTcb);
andrewboyson 89:9b765a67699b 103 }
andrewboyson 74:c3756bfa960e 104
andrewboyson 80:4ef1500fca1d 105 int TcpHandleReceivedPacket(void (*traceback)(void), int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx, int ipType, int remArIndex, int locIpScope)
andrewboyson 74:c3756bfa960e 106 {
andrewboyson 97:d91f7db00235 107 FaultPoint = FAULT_POINT_TcpHandleReceivedPacket;
andrewboyson 97:d91f7db00235 108
andrewboyson 90:955f4c6e18a9 109 int action = DO_NOTHING;
andrewboyson 89:9b765a67699b 110 bool traceRequested = false;
andrewboyson 86:55bc5ddac16c 111
andrewboyson 89:9b765a67699b 112 TcpHdrReadFromPacket(pPacketRx);
andrewboyson 89:9b765a67699b 113
andrewboyson 78:9d8fc88df405 114 if (remArIndex < 0)
andrewboyson 78:9d8fc88df405 115 {
andrewboyson 89:9b765a67699b 116 log(traceback, "invalid remote AR index %d -> ignored packet", remArIndex);
andrewboyson 97:d91f7db00235 117 FaultPoint = 0;
andrewboyson 78:9d8fc88df405 118 return DO_NOTHING;
andrewboyson 78:9d8fc88df405 119 }
andrewboyson 78:9d8fc88df405 120
andrewboyson 74:c3756bfa960e 121 int dataLength = sizeRx - TcpHdrSizeGet();
andrewboyson 74:c3756bfa960e 122 int locMss = *pSizeTx - TcpHdrSizeGet();
andrewboyson 74:c3756bfa960e 123
andrewboyson 90:955f4c6e18a9 124 //Calculate the sequence length of the received packet
andrewboyson 90:955f4c6e18a9 125 int seqLengthRcvd = 0;
andrewboyson 90:955f4c6e18a9 126 if (TcpHdrSYN) seqLengthRcvd += 1; //Add one to acknowledge the SYN
andrewboyson 90:955f4c6e18a9 127 seqLengthRcvd += dataLength; //Add the number of bytes received
andrewboyson 90:955f4c6e18a9 128 if (TcpHdrFIN) seqLengthRcvd += 1; //Add one to acknowledge the FIN
andrewboyson 90:955f4c6e18a9 129
andrewboyson 74:c3756bfa960e 130 //Filter out unwanted links
andrewboyson 74:c3756bfa960e 131 switch (TcpHdrDstPort)
andrewboyson 74:c3756bfa960e 132 {
andrewboyson 74:c3756bfa960e 133 case 80:
andrewboyson 74:c3756bfa960e 134 if (HttpTrace)
andrewboyson 74:c3756bfa960e 135 {
andrewboyson 74:c3756bfa960e 136 if (NetTraceNewLine) Log("\r\n");
andrewboyson 74:c3756bfa960e 137 LogTime("HTTP server request\r\n");
andrewboyson 89:9b765a67699b 138 traceRequested = true;
andrewboyson 74:c3756bfa960e 139 }
andrewboyson 74:c3756bfa960e 140 break;
andrewboyson 74:c3756bfa960e 141
andrewboyson 89:9b765a67699b 142 default: //Send reset if unknown port
andrewboyson 89:9b765a67699b 143 log(traceback, "unhandled local port %hu -> sent reset", TcpHdrDstPort);
andrewboyson 90:955f4c6e18a9 144 action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd);
andrewboyson 97:d91f7db00235 145 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 146 return action;
andrewboyson 74:c3756bfa960e 147 }
andrewboyson 74:c3756bfa960e 148
andrewboyson 76:17534bde28d3 149 //Get the Transmission Control Block
andrewboyson 88:1ba13e6062a3 150 struct tcb* pTcb = TcbGetExisting(ipType, remArIndex, locIpScope, TcpHdrSrcPort, TcpHdrDstPort);
andrewboyson 76:17534bde28d3 151 if (!pTcb) pTcb = TcbGetEmpty();
andrewboyson 89:9b765a67699b 152 if (!pTcb) //send reset if no more tcbs are available
andrewboyson 76:17534bde28d3 153 {
andrewboyson 89:9b765a67699b 154 log(traceback, "no more tcbs available -> sent reset");
andrewboyson 90:955f4c6e18a9 155 action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd);
andrewboyson 97:d91f7db00235 156 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 157 return action;
andrewboyson 76:17534bde28d3 158 }
andrewboyson 93:580fc113d9e9 159 pTcb->timeLastRcvd = MsTimerCount;
andrewboyson 76:17534bde28d3 160 pTcb->remArIndex = remArIndex;
andrewboyson 76:17534bde28d3 161 pTcb->ipType = ipType;
andrewboyson 81:50bfdd512f23 162 pTcb->locIpScope = locIpScope;
andrewboyson 76:17534bde28d3 163 pTcb->remPort = TcpHdrSrcPort;
andrewboyson 76:17534bde28d3 164 pTcb->locPort = TcpHdrDstPort;
andrewboyson 76:17534bde28d3 165 pTcb->window = TcpHdrWindow;
andrewboyson 75:603b10404183 166
andrewboyson 74:c3756bfa960e 167 //Handle request to reset
andrewboyson 74:c3756bfa960e 168 if (TcpHdrRST)
andrewboyson 74:c3756bfa960e 169 {
andrewboyson 76:17534bde28d3 170 if (pTcb->state)
andrewboyson 74:c3756bfa960e 171 {
andrewboyson 89:9b765a67699b 172 log(traceback, "received reset -> reaped TCB");
andrewboyson 75:603b10404183 173 pTcb->state = TCB_EMPTY;
andrewboyson 74:c3756bfa960e 174 }
andrewboyson 97:d91f7db00235 175 FaultPoint = 0;
andrewboyson 74:c3756bfa960e 176 return DO_NOTHING; //Don't reply
andrewboyson 74:c3756bfa960e 177 }
andrewboyson 74:c3756bfa960e 178
andrewboyson 74:c3756bfa960e 179 //Handle request to synchronise
andrewboyson 74:c3756bfa960e 180 if (TcpHdrSYN)
andrewboyson 74:c3756bfa960e 181 {
andrewboyson 76:17534bde28d3 182 if (pTcb->state)
andrewboyson 75:603b10404183 183 {
andrewboyson 89:9b765a67699b 184 log(traceback, "received a SYN on an open connection -> sent reset");
andrewboyson 75:603b10404183 185 pTcb->state = TCB_EMPTY;
andrewboyson 90:955f4c6e18a9 186 action = TcpSendReset(pSizeTx, pPacketTx, pTcb);
andrewboyson 97:d91f7db00235 187 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 188 return action;
andrewboyson 75:603b10404183 189 }
andrewboyson 76:17534bde28d3 190 else
andrewboyson 75:603b10404183 191 {
andrewboyson 88:1ba13e6062a3 192 handleSyn(pPacketRx, ipType, remArIndex, locMss, pTcb);
andrewboyson 75:603b10404183 193 }
andrewboyson 74:c3756bfa960e 194 }
andrewboyson 74:c3756bfa960e 195
andrewboyson 90:955f4c6e18a9 196 //Handle non SYN packet on an empty connection
andrewboyson 76:17534bde28d3 197 if (!TcpHdrSYN && !pTcb->state)
andrewboyson 76:17534bde28d3 198 {
andrewboyson 90:955f4c6e18a9 199
andrewboyson 89:9b765a67699b 200 log(traceback, "non SYN packet received on a closed connection -> sent reset");
andrewboyson 78:9d8fc88df405 201 pTcb->state = TCB_EMPTY;
andrewboyson 90:955f4c6e18a9 202 action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd);
andrewboyson 97:d91f7db00235 203 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 204 return action;
andrewboyson 76:17534bde28d3 205 }
andrewboyson 76:17534bde28d3 206
andrewboyson 79:f50e02fb5c94 207 //Check if the acks of bytes sent has progressed and reset the timer
andrewboyson 91:879545b19260 208 uint32_t ackRcvdFromRem = TcpHdrACK ? TcpHdrAckNum - pTcb->locIsn : 0;
andrewboyson 82:20781198d26d 209 if (ackRcvdFromRem > pTcb->bytesAckdByRem)
andrewboyson 82:20781198d26d 210 {
andrewboyson 93:580fc113d9e9 211 pTcb->timeSendsBeingAcked = MsTimerCount;
andrewboyson 82:20781198d26d 212 pTcb->countSendsNotAcked = 0;
andrewboyson 82:20781198d26d 213 }
andrewboyson 79:f50e02fb5c94 214
andrewboyson 79:f50e02fb5c94 215 //Record the number of bytes acked by the remote host
andrewboyson 79:f50e02fb5c94 216 pTcb->bytesAckdByRem = ackRcvdFromRem;
andrewboyson 79:f50e02fb5c94 217
andrewboyson 76:17534bde28d3 218 /* If the connection is in a synchronized state
andrewboyson 76:17534bde28d3 219 any unacceptable segment (out of window sequence number or
andrewboyson 76:17534bde28d3 220 unacceptible acknowledgment number) must elicit only an empty
andrewboyson 76:17534bde28d3 221 acknowledgment segment containing the current send-sequence number
andrewboyson 76:17534bde28d3 222 and an acknowledgment indicating the next sequence number expected
andrewboyson 76:17534bde28d3 223 to be received, and the connection remains in the same state.*/
andrewboyson 74:c3756bfa960e 224 uint32_t seqRcvdFromRem = TcpHdrSeqNum - pTcb->remIsn;
andrewboyson 78:9d8fc88df405 225 if (seqRcvdFromRem != pTcb->bytesAckdToRem)
andrewboyson 78:9d8fc88df405 226 {
andrewboyson 79:f50e02fb5c94 227 //Only warn non keep-alives
andrewboyson 79:f50e02fb5c94 228 if (seqRcvdFromRem != 0 || pTcb->bytesAckdToRem != 1)
andrewboyson 79:f50e02fb5c94 229 {
andrewboyson 89:9b765a67699b 230 log(traceback, "seq rcvd is %d and last seq ackd was %d -> resent last ACK", seqRcvdFromRem, pTcb->bytesAckdToRem);
andrewboyson 79:f50e02fb5c94 231 }
andrewboyson 90:955f4c6e18a9 232 action = TcpResendLastAck(pSizeTx, pPacketTx, pTcb);
andrewboyson 97:d91f7db00235 233 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 234 return action;
andrewboyson 78:9d8fc88df405 235 }
andrewboyson 90:955f4c6e18a9 236 //Ignore data before established
andrewboyson 90:955f4c6e18a9 237 if (pTcb->state != TCB_ESTABLISHED && dataLength)
andrewboyson 90:955f4c6e18a9 238 {
andrewboyson 90:955f4c6e18a9 239 log(traceback, "data received before connection established -> sent reset");
andrewboyson 90:955f4c6e18a9 240 pTcb->state = TCB_EMPTY;
andrewboyson 90:955f4c6e18a9 241 action = TcpSendReset(pSizeTx, pPacketTx, pTcb);
andrewboyson 97:d91f7db00235 242 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 243 return action;
andrewboyson 90:955f4c6e18a9 244 }
andrewboyson 90:955f4c6e18a9 245
andrewboyson 74:c3756bfa960e 246 //Handle FIN
andrewboyson 74:c3756bfa960e 247 if (TcpHdrFIN) pTcb->rcvdFin = true; //When reply is all sent only a passive close is needed
andrewboyson 90:955f4c6e18a9 248
andrewboyson 90:955f4c6e18a9 249 //From now on there are no errors so display traceback if requested
andrewboyson 90:955f4c6e18a9 250 if (traceRequested && NetTraceStack) traceback();
andrewboyson 74:c3756bfa960e 251
andrewboyson 75:603b10404183 252 //Record the number of bytes received from the remote host
andrewboyson 76:17534bde28d3 253 pTcb->bytesRcvdFromRem += seqLengthRcvd;
andrewboyson 74:c3756bfa960e 254
andrewboyson 76:17534bde28d3 255 switch (pTcb->state) //This is the state of the connection BEFORE this packet arrived
andrewboyson 74:c3756bfa960e 256 {
andrewboyson 74:c3756bfa960e 257 case TCB_EMPTY:
andrewboyson 74:c3756bfa960e 258 pTcb->state = TCB_SYN_RECEIVED;
andrewboyson 74:c3756bfa960e 259 break;
andrewboyson 74:c3756bfa960e 260
andrewboyson 74:c3756bfa960e 261 case TCB_SYN_RECEIVED:
andrewboyson 74:c3756bfa960e 262 pTcb->state = TCB_ESTABLISHED;
andrewboyson 74:c3756bfa960e 263 break;
andrewboyson 74:c3756bfa960e 264
andrewboyson 74:c3756bfa960e 265 case TCB_ESTABLISHED:
andrewboyson 88:1ba13e6062a3 266 if (dataLength) handleReceivedData (pPacketRx, dataLength, seqRcvdFromRem - 1, pTcb);
andrewboyson 74:c3756bfa960e 267 if (pTcb->sentFin)
andrewboyson 74:c3756bfa960e 268 {
andrewboyson 77:6cb7d92c37f3 269 pTcb->state = pTcb->rcvdFin ? TCB_EMPTY : TCB_CLOSE_FIN_WAIT;
andrewboyson 74:c3756bfa960e 270 }
andrewboyson 74:c3756bfa960e 271 break;
andrewboyson 74:c3756bfa960e 272
andrewboyson 74:c3756bfa960e 273 case TCB_CLOSE_FIN_WAIT: //End of active close
andrewboyson 74:c3756bfa960e 274 if (TcpHdrFIN)
andrewboyson 74:c3756bfa960e 275 {
andrewboyson 74:c3756bfa960e 276 pTcb->state = TCB_EMPTY;//Ignore ACK to our FIN. Wait for FIN then close.
andrewboyson 74:c3756bfa960e 277 }
andrewboyson 74:c3756bfa960e 278 break;
andrewboyson 74:c3756bfa960e 279
andrewboyson 74:c3756bfa960e 280 }
andrewboyson 74:c3756bfa960e 281
andrewboyson 90:955f4c6e18a9 282 action = TcpSend(pSizeTx, pPacketTx, pTcb);
andrewboyson 86:55bc5ddac16c 283
andrewboyson 97:d91f7db00235 284 FaultPoint = 0;
andrewboyson 90:955f4c6e18a9 285 return action;
andrewboyson 74:c3756bfa960e 286 }