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 17:36:08 2018 +0000
Revision:
81:50bfdd512f23
Parent:
80:4ef1500fca1d
Child:
82:20781198d26d
Corrected two bugs in TCP: now working.

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