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