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@146:0fc66d610fd6, 2019-05-15 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed May 15 15:33:15 2019 +0000
- Revision:
- 146:0fc66d610fd6
- Parent:
- 145:206bf0d073c7
- Child:
- 147:a6093b52e654
Tidied the http shim
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 | 146:0fc66d610fd6 | 14 | #include "httpshim.h" |
andrewboyson | 74:c3756bfa960e | 15 | #include "led.h" |
andrewboyson | 93:580fc113d9e9 | 16 | #include "mstimer.h" |
andrewboyson | 142:a8c0890a58d1 | 17 | #include "restart.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 | 144:6bd5c54efc7d | 45 | pTcb->appStarted = false; |
andrewboyson | 144:6bd5c54efc7d | 46 | for (char* p = pTcb->appData; p < pTcb->appData + TCB_APP_STATE_BYTES; p++) *p = 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 | 146:0fc66d610fd6 | 60 | case 80: HttpShimRequest(dataLength, pData, position, pTcb->appData, false); break; |
andrewboyson | 146:0fc66d610fd6 | 61 | case 443: HttpShimRequest(dataLength, pData, position, pTcb->appData, true ); break; |
andrewboyson | 146:0fc66d610fd6 | 62 | default: break; |
andrewboyson | 74:c3756bfa960e | 63 | } |
andrewboyson | 74:c3756bfa960e | 64 | } |
andrewboyson | 90:955f4c6e18a9 | 65 | static int sendResetFromPacket(int* pSizeTx, void* pPacketTx, int ipType, int remArIndex, int locIpScope, int seqLengthRcvd) |
andrewboyson | 89:9b765a67699b | 66 | { |
andrewboyson | 90:955f4c6e18a9 | 67 | /*RFC793 p36 If the connection does not exist (CLOSED) then a reset is sent |
andrewboyson | 90:955f4c6e18a9 | 68 | in response to any incoming segment except another reset. |
andrewboyson | 90:955f4c6e18a9 | 69 | If the incoming segment has an ACK field, the reset takes its sequence number from the ACK field of the segment, |
andrewboyson | 90:955f4c6e18a9 | 70 | otherwise the reset has sequence number zero |
andrewboyson | 90:955f4c6e18a9 | 71 | and |
andrewboyson | 90:955f4c6e18a9 | 72 | the ACK field is set to the sum of the sequence number and segment length of the incoming segment. |
andrewboyson | 90:955f4c6e18a9 | 73 | The connection remains in the CLOSED state. |
andrewboyson | 90:955f4c6e18a9 | 74 | In TcpSendReset TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send |
andrewboyson | 90:955f4c6e18a9 | 75 | TcpHdrSeqNum = pTcb->bytesSentToRem + pTcb->locIsn; //Set up the start of the message before adding the bytes sent |
andrewboyson | 90:955f4c6e18a9 | 76 | */ |
andrewboyson | 90:955f4c6e18a9 | 77 | |
andrewboyson | 89:9b765a67699b | 78 | struct tcb tcb; |
andrewboyson | 89:9b765a67699b | 79 | struct tcb* pTcb = &tcb; |
andrewboyson | 93:580fc113d9e9 | 80 | pTcb->timeLastRcvd = MsTimerCount; |
andrewboyson | 89:9b765a67699b | 81 | pTcb->remArIndex = remArIndex; |
andrewboyson | 89:9b765a67699b | 82 | pTcb->ipType = ipType; |
andrewboyson | 89:9b765a67699b | 83 | pTcb->locIpScope = locIpScope; |
andrewboyson | 89:9b765a67699b | 84 | pTcb->remPort = TcpHdrSrcPort; |
andrewboyson | 89:9b765a67699b | 85 | pTcb->locPort = TcpHdrDstPort; |
andrewboyson | 89:9b765a67699b | 86 | pTcb->window = TcpHdrWindow; |
andrewboyson | 89:9b765a67699b | 87 | pTcb->state = TCB_EMPTY; |
andrewboyson | 89:9b765a67699b | 88 | |
andrewboyson | 93:580fc113d9e9 | 89 | pTcb->timeSendsBeingAcked = MsTimerCount; |
andrewboyson | 90:955f4c6e18a9 | 90 | pTcb->countSendsNotAcked = 0; |
andrewboyson | 90:955f4c6e18a9 | 91 | pTcb->rcvdFin = false; |
andrewboyson | 90:955f4c6e18a9 | 92 | pTcb->sentFin = false; |
andrewboyson | 144:6bd5c54efc7d | 93 | pTcb->appStarted = false; |
andrewboyson | 144:6bd5c54efc7d | 94 | for (char* p = pTcb->appData; p < pTcb->appData + TCB_APP_STATE_BYTES; p++) *p = 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 | 142:a8c0890a58d1 | 107 | int lastRestartPoint = RestartPoint; |
andrewboyson | 142:a8c0890a58d1 | 108 | RestartPoint = FAULT_POINT_TcpHandleReceivedPacket; |
andrewboyson | 97:d91f7db00235 | 109 | |
andrewboyson | 90:955f4c6e18a9 | 110 | int action = DO_NOTHING; |
andrewboyson | 89:9b765a67699b | 111 | bool traceRequested = false; |
andrewboyson | 86:55bc5ddac16c | 112 | |
andrewboyson | 89:9b765a67699b | 113 | TcpHdrReadFromPacket(pPacketRx); |
andrewboyson | 89:9b765a67699b | 114 | |
andrewboyson | 78:9d8fc88df405 | 115 | if (remArIndex < 0) |
andrewboyson | 78:9d8fc88df405 | 116 | { |
andrewboyson | 89:9b765a67699b | 117 | log(traceback, "invalid remote AR index %d -> ignored packet", remArIndex); |
andrewboyson | 142:a8c0890a58d1 | 118 | RestartPoint = lastRestartPoint; |
andrewboyson | 78:9d8fc88df405 | 119 | return DO_NOTHING; |
andrewboyson | 78:9d8fc88df405 | 120 | } |
andrewboyson | 142:a8c0890a58d1 | 121 | RestartPoint += 100; |
andrewboyson | 78:9d8fc88df405 | 122 | |
andrewboyson | 74:c3756bfa960e | 123 | int dataLength = sizeRx - TcpHdrSizeGet(); |
andrewboyson | 74:c3756bfa960e | 124 | int locMss = *pSizeTx - TcpHdrSizeGet(); |
andrewboyson | 74:c3756bfa960e | 125 | |
andrewboyson | 90:955f4c6e18a9 | 126 | //Calculate the sequence length of the received packet |
andrewboyson | 90:955f4c6e18a9 | 127 | int seqLengthRcvd = 0; |
andrewboyson | 90:955f4c6e18a9 | 128 | if (TcpHdrSYN) seqLengthRcvd += 1; //Add one to acknowledge the SYN |
andrewboyson | 90:955f4c6e18a9 | 129 | seqLengthRcvd += dataLength; //Add the number of bytes received |
andrewboyson | 90:955f4c6e18a9 | 130 | if (TcpHdrFIN) seqLengthRcvd += 1; //Add one to acknowledge the FIN |
andrewboyson | 90:955f4c6e18a9 | 131 | |
andrewboyson | 74:c3756bfa960e | 132 | //Filter out unwanted links |
andrewboyson | 142:a8c0890a58d1 | 133 | RestartPoint++; |
andrewboyson | 74:c3756bfa960e | 134 | switch (TcpHdrDstPort) |
andrewboyson | 74:c3756bfa960e | 135 | { |
andrewboyson | 74:c3756bfa960e | 136 | case 80: |
andrewboyson | 146:0fc66d610fd6 | 137 | if (HttpShimGetTrace()) |
andrewboyson | 74:c3756bfa960e | 138 | { |
andrewboyson | 74:c3756bfa960e | 139 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 74:c3756bfa960e | 140 | LogTime("HTTP server request\r\n"); |
andrewboyson | 89:9b765a67699b | 141 | traceRequested = true; |
andrewboyson | 74:c3756bfa960e | 142 | } |
andrewboyson | 74:c3756bfa960e | 143 | break; |
andrewboyson | 74:c3756bfa960e | 144 | |
andrewboyson | 111:3600389d1add | 145 | case 443: |
andrewboyson | 146:0fc66d610fd6 | 146 | if (HttpShimGetTrace()) |
andrewboyson | 111:3600389d1add | 147 | { |
andrewboyson | 111:3600389d1add | 148 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 111:3600389d1add | 149 | LogTime("HTTPS server request\r\n"); |
andrewboyson | 111:3600389d1add | 150 | traceRequested = true; |
andrewboyson | 111:3600389d1add | 151 | } |
andrewboyson | 111:3600389d1add | 152 | break; |
andrewboyson | 111:3600389d1add | 153 | |
andrewboyson | 89:9b765a67699b | 154 | default: //Send reset if unknown port |
andrewboyson | 89:9b765a67699b | 155 | log(traceback, "unhandled local port %hu -> sent reset", TcpHdrDstPort); |
andrewboyson | 90:955f4c6e18a9 | 156 | action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd); |
andrewboyson | 142:a8c0890a58d1 | 157 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 158 | return action; |
andrewboyson | 74:c3756bfa960e | 159 | } |
andrewboyson | 74:c3756bfa960e | 160 | |
andrewboyson | 76:17534bde28d3 | 161 | //Get the Transmission Control Block |
andrewboyson | 142:a8c0890a58d1 | 162 | RestartPoint++; |
andrewboyson | 88:1ba13e6062a3 | 163 | struct tcb* pTcb = TcbGetExisting(ipType, remArIndex, locIpScope, TcpHdrSrcPort, TcpHdrDstPort); |
andrewboyson | 76:17534bde28d3 | 164 | if (!pTcb) pTcb = TcbGetEmpty(); |
andrewboyson | 89:9b765a67699b | 165 | if (!pTcb) //send reset if no more tcbs are available |
andrewboyson | 76:17534bde28d3 | 166 | { |
andrewboyson | 89:9b765a67699b | 167 | log(traceback, "no more tcbs available -> sent reset"); |
andrewboyson | 90:955f4c6e18a9 | 168 | action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd); |
andrewboyson | 142:a8c0890a58d1 | 169 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 170 | return action; |
andrewboyson | 76:17534bde28d3 | 171 | } |
andrewboyson | 93:580fc113d9e9 | 172 | pTcb->timeLastRcvd = MsTimerCount; |
andrewboyson | 76:17534bde28d3 | 173 | pTcb->remArIndex = remArIndex; |
andrewboyson | 76:17534bde28d3 | 174 | pTcb->ipType = ipType; |
andrewboyson | 81:50bfdd512f23 | 175 | pTcb->locIpScope = locIpScope; |
andrewboyson | 76:17534bde28d3 | 176 | pTcb->remPort = TcpHdrSrcPort; |
andrewboyson | 76:17534bde28d3 | 177 | pTcb->locPort = TcpHdrDstPort; |
andrewboyson | 76:17534bde28d3 | 178 | pTcb->window = TcpHdrWindow; |
andrewboyson | 75:603b10404183 | 179 | |
andrewboyson | 74:c3756bfa960e | 180 | //Handle request to reset |
andrewboyson | 142:a8c0890a58d1 | 181 | RestartPoint++; |
andrewboyson | 74:c3756bfa960e | 182 | if (TcpHdrRST) |
andrewboyson | 74:c3756bfa960e | 183 | { |
andrewboyson | 76:17534bde28d3 | 184 | if (pTcb->state) |
andrewboyson | 74:c3756bfa960e | 185 | { |
andrewboyson | 89:9b765a67699b | 186 | log(traceback, "received reset -> reaped TCB"); |
andrewboyson | 75:603b10404183 | 187 | pTcb->state = TCB_EMPTY; |
andrewboyson | 74:c3756bfa960e | 188 | } |
andrewboyson | 142:a8c0890a58d1 | 189 | RestartPoint = lastRestartPoint; |
andrewboyson | 74:c3756bfa960e | 190 | return DO_NOTHING; //Don't reply |
andrewboyson | 74:c3756bfa960e | 191 | } |
andrewboyson | 74:c3756bfa960e | 192 | |
andrewboyson | 74:c3756bfa960e | 193 | //Handle request to synchronise |
andrewboyson | 142:a8c0890a58d1 | 194 | RestartPoint++; |
andrewboyson | 74:c3756bfa960e | 195 | if (TcpHdrSYN) |
andrewboyson | 74:c3756bfa960e | 196 | { |
andrewboyson | 76:17534bde28d3 | 197 | if (pTcb->state) |
andrewboyson | 75:603b10404183 | 198 | { |
andrewboyson | 89:9b765a67699b | 199 | log(traceback, "received a SYN on an open connection -> sent reset"); |
andrewboyson | 75:603b10404183 | 200 | pTcb->state = TCB_EMPTY; |
andrewboyson | 90:955f4c6e18a9 | 201 | action = TcpSendReset(pSizeTx, pPacketTx, pTcb); |
andrewboyson | 142:a8c0890a58d1 | 202 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 203 | return action; |
andrewboyson | 75:603b10404183 | 204 | } |
andrewboyson | 76:17534bde28d3 | 205 | else |
andrewboyson | 75:603b10404183 | 206 | { |
andrewboyson | 88:1ba13e6062a3 | 207 | handleSyn(pPacketRx, ipType, remArIndex, locMss, pTcb); |
andrewboyson | 75:603b10404183 | 208 | } |
andrewboyson | 74:c3756bfa960e | 209 | } |
andrewboyson | 74:c3756bfa960e | 210 | |
andrewboyson | 90:955f4c6e18a9 | 211 | //Handle non SYN packet on an empty connection |
andrewboyson | 142:a8c0890a58d1 | 212 | RestartPoint++; |
andrewboyson | 76:17534bde28d3 | 213 | if (!TcpHdrSYN && !pTcb->state) |
andrewboyson | 76:17534bde28d3 | 214 | { |
andrewboyson | 90:955f4c6e18a9 | 215 | |
andrewboyson | 89:9b765a67699b | 216 | log(traceback, "non SYN packet received on a closed connection -> sent reset"); |
andrewboyson | 78:9d8fc88df405 | 217 | pTcb->state = TCB_EMPTY; |
andrewboyson | 90:955f4c6e18a9 | 218 | action = sendResetFromPacket(pSizeTx, pPacketTx, ipType, remArIndex, locIpScope, seqLengthRcvd); |
andrewboyson | 142:a8c0890a58d1 | 219 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 220 | return action; |
andrewboyson | 76:17534bde28d3 | 221 | } |
andrewboyson | 76:17534bde28d3 | 222 | |
andrewboyson | 79:f50e02fb5c94 | 223 | //Check if the acks of bytes sent has progressed and reset the timer |
andrewboyson | 142:a8c0890a58d1 | 224 | RestartPoint++; |
andrewboyson | 91:879545b19260 | 225 | uint32_t ackRcvdFromRem = TcpHdrACK ? TcpHdrAckNum - pTcb->locIsn : 0; |
andrewboyson | 82:20781198d26d | 226 | if (ackRcvdFromRem > pTcb->bytesAckdByRem) |
andrewboyson | 82:20781198d26d | 227 | { |
andrewboyson | 93:580fc113d9e9 | 228 | pTcb->timeSendsBeingAcked = MsTimerCount; |
andrewboyson | 82:20781198d26d | 229 | pTcb->countSendsNotAcked = 0; |
andrewboyson | 82:20781198d26d | 230 | } |
andrewboyson | 79:f50e02fb5c94 | 231 | |
andrewboyson | 79:f50e02fb5c94 | 232 | //Record the number of bytes acked by the remote host |
andrewboyson | 142:a8c0890a58d1 | 233 | RestartPoint++; |
andrewboyson | 79:f50e02fb5c94 | 234 | pTcb->bytesAckdByRem = ackRcvdFromRem; |
andrewboyson | 79:f50e02fb5c94 | 235 | |
andrewboyson | 76:17534bde28d3 | 236 | /* If the connection is in a synchronized state |
andrewboyson | 76:17534bde28d3 | 237 | any unacceptable segment (out of window sequence number or |
andrewboyson | 76:17534bde28d3 | 238 | unacceptible acknowledgment number) must elicit only an empty |
andrewboyson | 76:17534bde28d3 | 239 | acknowledgment segment containing the current send-sequence number |
andrewboyson | 76:17534bde28d3 | 240 | and an acknowledgment indicating the next sequence number expected |
andrewboyson | 76:17534bde28d3 | 241 | to be received, and the connection remains in the same state.*/ |
andrewboyson | 142:a8c0890a58d1 | 242 | RestartPoint++; |
andrewboyson | 74:c3756bfa960e | 243 | uint32_t seqRcvdFromRem = TcpHdrSeqNum - pTcb->remIsn; |
andrewboyson | 78:9d8fc88df405 | 244 | if (seqRcvdFromRem != pTcb->bytesAckdToRem) |
andrewboyson | 78:9d8fc88df405 | 245 | { |
andrewboyson | 79:f50e02fb5c94 | 246 | //Only warn non keep-alives |
andrewboyson | 79:f50e02fb5c94 | 247 | if (seqRcvdFromRem != 0 || pTcb->bytesAckdToRem != 1) |
andrewboyson | 79:f50e02fb5c94 | 248 | { |
andrewboyson | 89:9b765a67699b | 249 | log(traceback, "seq rcvd is %d and last seq ackd was %d -> resent last ACK", seqRcvdFromRem, pTcb->bytesAckdToRem); |
andrewboyson | 79:f50e02fb5c94 | 250 | } |
andrewboyson | 90:955f4c6e18a9 | 251 | action = TcpResendLastAck(pSizeTx, pPacketTx, pTcb); |
andrewboyson | 142:a8c0890a58d1 | 252 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 253 | return action; |
andrewboyson | 78:9d8fc88df405 | 254 | } |
andrewboyson | 90:955f4c6e18a9 | 255 | //Ignore data before established |
andrewboyson | 142:a8c0890a58d1 | 256 | RestartPoint++; |
andrewboyson | 90:955f4c6e18a9 | 257 | if (pTcb->state != TCB_ESTABLISHED && dataLength) |
andrewboyson | 90:955f4c6e18a9 | 258 | { |
andrewboyson | 90:955f4c6e18a9 | 259 | log(traceback, "data received before connection established -> sent reset"); |
andrewboyson | 90:955f4c6e18a9 | 260 | pTcb->state = TCB_EMPTY; |
andrewboyson | 90:955f4c6e18a9 | 261 | action = TcpSendReset(pSizeTx, pPacketTx, pTcb); |
andrewboyson | 142:a8c0890a58d1 | 262 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 263 | return action; |
andrewboyson | 90:955f4c6e18a9 | 264 | } |
andrewboyson | 90:955f4c6e18a9 | 265 | |
andrewboyson | 74:c3756bfa960e | 266 | //Handle FIN |
andrewboyson | 142:a8c0890a58d1 | 267 | RestartPoint++; |
andrewboyson | 74:c3756bfa960e | 268 | if (TcpHdrFIN) pTcb->rcvdFin = true; //When reply is all sent only a passive close is needed |
andrewboyson | 90:955f4c6e18a9 | 269 | |
andrewboyson | 90:955f4c6e18a9 | 270 | //From now on there are no errors so display traceback if requested |
andrewboyson | 142:a8c0890a58d1 | 271 | RestartPoint++; |
andrewboyson | 90:955f4c6e18a9 | 272 | if (traceRequested && NetTraceStack) traceback(); |
andrewboyson | 74:c3756bfa960e | 273 | |
andrewboyson | 75:603b10404183 | 274 | //Record the number of bytes received from the remote host |
andrewboyson | 142:a8c0890a58d1 | 275 | RestartPoint++; |
andrewboyson | 76:17534bde28d3 | 276 | pTcb->bytesRcvdFromRem += seqLengthRcvd; |
andrewboyson | 74:c3756bfa960e | 277 | |
andrewboyson | 76:17534bde28d3 | 278 | switch (pTcb->state) //This is the state of the connection BEFORE this packet arrived |
andrewboyson | 74:c3756bfa960e | 279 | { |
andrewboyson | 74:c3756bfa960e | 280 | case TCB_EMPTY: |
andrewboyson | 74:c3756bfa960e | 281 | pTcb->state = TCB_SYN_RECEIVED; |
andrewboyson | 74:c3756bfa960e | 282 | break; |
andrewboyson | 74:c3756bfa960e | 283 | |
andrewboyson | 74:c3756bfa960e | 284 | case TCB_SYN_RECEIVED: |
andrewboyson | 74:c3756bfa960e | 285 | pTcb->state = TCB_ESTABLISHED; |
andrewboyson | 74:c3756bfa960e | 286 | break; |
andrewboyson | 74:c3756bfa960e | 287 | |
andrewboyson | 74:c3756bfa960e | 288 | case TCB_ESTABLISHED: |
andrewboyson | 144:6bd5c54efc7d | 289 | if (dataLength) |
andrewboyson | 144:6bd5c54efc7d | 290 | { |
andrewboyson | 144:6bd5c54efc7d | 291 | handleReceivedData(pPacketRx, dataLength, seqRcvdFromRem - 1, pTcb); |
andrewboyson | 144:6bd5c54efc7d | 292 | pTcb->appStarted = true; |
andrewboyson | 144:6bd5c54efc7d | 293 | } |
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 | 142:a8c0890a58d1 | 309 | RestartPoint++; |
andrewboyson | 90:955f4c6e18a9 | 310 | action = TcpSend(pSizeTx, pPacketTx, pTcb); |
andrewboyson | 86:55bc5ddac16c | 311 | |
andrewboyson | 142:a8c0890a58d1 | 312 | RestartPoint = lastRestartPoint; |
andrewboyson | 90:955f4c6e18a9 | 313 | return action; |
andrewboyson | 74:c3756bfa960e | 314 | } |