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