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/tcp.txt

Committer:
andrewboyson
Date:
2018-10-22
Revision:
72:19457bba58d0
Parent:
71:736a5747ade1
Child:
75:603b10404183

File content as of revision 72:19457bba58d0:

Names
=====
ISN     == Initial Sequence Number
SEQ num == Sequence Number is the sequence of the first byte of the sent data packet - unless a SYN in which case it is the ISN
ACK num == Acknowledge Number is the next sequence number that the sender of the ACK is expecting
TCB     == Transmission Control Block
RTO     == Retransmission Time Out

Protocol
========
Goes:
0       SYN
1       Data (length == MSS)
1+  MSS Data (length == MSS)
1+2xMSS Data (length <  MSS) + FIN

TcpHandleReceivedPacket
=======================
Called from the network stack
Reads the header to get: the src and dst ports; the ACK and SEQ numbers; SYN, ACK, PSH, RST and FIN flags 
Only handles dst port 80 (HTTP)
Retrieves the TCB for the src port
Handles RST or SYN requests
Subtract the initial sequence numbers to get the position in the query from the SEQ num and the position in the reply from the ACK num.
If the position in the query is not the same as the TCB recd bytes then adjust the TCB recd bytes
If the position in the reply is not the same as the TCB sent bytes then adjust the TCB sent bytes
Increment the TCB recd bytes by the received datalength and by 1 for a SYN or FIN
Handle the packet according to the TCB state, for established connections pass the position in the query and the position in the reply
Set the ACK number to the TCB recd bytes plus the ISN
Set the SEQ number to the TCB sent bytes plus the ISN
Increment the TCB sent bytes by the sent datalength and by 1 for a SYN or FIN
Swap the ports
Return to the network stack for unicast transmission

Todo
====
Separate the work into TcpHandleReceivedPacket and TcpPollForPacketToSend: this would allow RTO to be implemented.
Need to add the IP to the TCB
Change TCB_ACK_WAIT and TCB_FIN_WAIT to TCB_CLOSE_ACK_WAIT and TCB_CLOSE_FIN_WAIT
Add state TCB_SEND_ACK_WAIT
Add ackdBytes to TCB so that can tell if sentBytes has been acked or not; if so then send the next; if not and within timeout then wait; if not and RTO then send again

State
=====
State               Received        Do              New state
TCB_EMPTY           SYN             record ISN      TCB_SYN_RECEIVED
TCB_SYN_RECEIVED    ACK                             TCB_ESTABLISHED
TCB_ESTABLISHED     
TCB_ACK_WAIT
TCB_FIN_WAIT

State
TCB_EMPTY           do nothing
TCB_SYN_RECEIVED    if (TCB.sent == 0) or (TCB.sent == 1 and TCB.ackd == 0 and RTO) then send SYN ACK and ISN.
TCB_ESTABLISHED     if (!TCB.finished) then send from TCB.sent. if RTO and TCB.ackd < TCB.sent then resend TCB.ackd
TCB_ACK_WAIT
TCB_FIN_WAIT