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