CSCE 515 Computer Network Programming TCP Details Wenyuan

  • Slides: 27
Download presentation
CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu http: //www. cse. sc.

CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu http: //www. cse. sc. edu/~wyxu/csce 515 f 07. html Department of Computer Science and Engineering University of South Carolina

TCP n TCP provides the end-to-end reliable connection that IP alone cannot support n

TCP n TCP provides the end-to-end reliable connection that IP alone cannot support n The TCP protocol ¨ Frame format ¨ Connection Creation ¨ Flow control ¨ Congestion control ¨ Connection termination 2007 CSCE 515 – Computer Network Programming

TCP Segment Format 0 15 16 source port number 31 destination port number sequence

TCP Segment Format 0 15 16 source port number 31 destination port number sequence number acknowledgment number header length reserved U A P R S F R C S S Y I G K H T N N TCP checksum window size urgent pointer option (if any) data (if any) 2007 CSCE 515 – Computer Network Programming 20 bytes

More. . . n Every TCP segment includes a Sequence Number that refers to

More. . . n Every TCP segment includes a Sequence Number that refers to the first byte of data included in the segment. n Every TCP segment includes a Request Number (Acknowledgement Number) that indicates the byte number of the next data that is expected to be received. ¨ All bytes up through this number have already been received. 2007 CSCE 515 – Computer Network Programming

And more. . . n MSS: Maximum segment size (A TCP option) n Window:

And more. . . n MSS: Maximum segment size (A TCP option) n Window: Every ACK includes a Window field that tells the sender how many bytes it can send before the receiver will have to toss it away (due to fixed buffer size). 2007 CSCE 515 – Computer Network Programming

And more… n There a bunch of control flags: URG: urgent data included. n

And more… n There a bunch of control flags: URG: urgent data included. n ACK: this segment is (among other things) an acknowledgement. n RST: error - abort the session. n SYN: Used to establish connection; synchronize Sequence Numbers (setup) n FIN: polite connection termination. n 2007 CSCE 515 – Computer Network Programming

TCP Lingo When a client requests a connection, it sends a “SYN” segment (a

TCP Lingo When a client requests a connection, it sends a “SYN” segment (a special TCP segment) to the server port. n SYN stands for synchronize. The SYN message includes the client’s ISN. n ISN is Initial Sequence Number. n 2007 CSCE 515 – Computer Network Programming

TCP Connection Establishment – Three-way handshake Client “I want to talk, and I’m starting

TCP Connection Establishment – Three-way handshake Client “I want to talk, and I’m starting with byte number X+1”. time “Got it - you start at byte number Y+1”. 2007 Server SYN ISN=X 1 SYN 2 ISN=Y ACK=X+1 ACK=Y+1 3 CSCE 515 – Computer Network Programming “OK, I’m here and I’ll talk. My first byte will be called number Y+1, and I know your first byte will be number X+1”

TCP Connection Creation n A server accepts a connection. ¨ Must n A client

TCP Connection Creation n A server accepts a connection. ¨ Must n A client requests a connection. ¨ Must 2007 be looking for new connections! know where the server is! CSCE 515 – Computer Network Programming

Client Starts n A client starts by sending a SYN segment with the following

Client Starts n A client starts by sending a SYN segment with the following information: ¨ Client’s ISN (generated pseudo-randomly) ¨ Maximum Receive Window for client. ¨ Optionally (but usually) MSS (largest datagram accepted). ¨ No payload! (Only TCP headers) 2007 CSCE 515 – Computer Network Programming

Sever Response n When a waiting server sees a new connection request, the server

Sever Response n When a waiting server sees a new connection request, the server sends back a SYN segment with: ¨ Server’s ISN (generated pseudo-randomly) ¨ Request Number is Client ISN+1 ¨ Maximum Receive Window for server. ¨ Optionally (but usually) MSS ¨ No payload! (Only TCP headers) 2007 CSCE 515 – Computer Network Programming

Finally n When the Server’s SYN is received, the client sends back an ACK

Finally n When the Server’s SYN is received, the client sends back an ACK with: ¨ Request 2007 Number is Server’s ISN+1 CSCE 515 – Computer Network Programming

Why 3 -Way? n Why is the third message necessary? n HINTS: ¨ TCP

Why 3 -Way? n Why is the third message necessary? n HINTS: ¨ TCP is a reliable service. ¨ IP delivers each TCP segment. ¨ IP is not reliable. 2007 CSCE 515 – Computer Network Programming

TCP Data and ACK Once the connection is established, data can be sent. n

TCP Data and ACK Once the connection is established, data can be sent. n Each data segment includes a sequence number identifying the first byte in the segment. n Each ACK segment includes a request number indicating what data has been received. (bytes instead of packets) n 2007 CSCE 515 – Computer Network Programming

Important Information in TCP/IP packet headers N SEQ Send ACK WIN Recv Contained in

Important Information in TCP/IP packet headers N SEQ Send ACK WIN Recv Contained in IP header 2007 Sequence number of first data byte in packet (SEQ) Number of bytes in packet (N) ACK bit set Sequence number of next expected byte (ACK) Window size at the receiver (WIN) Contained in TCP header CSCE 515 – Computer Network Programming

Buffering n The TCP layer doesn’t know when the application will ask for any

Buffering n The TCP layer doesn’t know when the application will ask for any received data. n Both the client and server allocate buffers to hold incoming and outgoing data (TCP does this) ¨ incoming data -> it’s ready when application asks for ¨ outgoing data-> it can be retransmitted should error occurs n 2007 Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment). CSCE 515 – Computer Network Programming it.

Send Buffers n n The application gives the TCP layer some data to send.

Send Buffers n n The application gives the TCP layer some data to send. The data is put in a send buffer, where it stays until the data is ACK’d. ¨ it n 2007 has to stay, as it might need to be sent again! The TCP layer won’t accept data from the application unless (or until) there is buffer space. CSCE 515 – Computer Network Programming

Recv Buffers n The received data is put in the recv buffer, where it

Recv Buffers n The received data is put in the recv buffer, where it stays until the application reads data n The Recv Buffer won’t accept data from network unless (or until) there is buffer space, and the sequence number is within its sliding windows. n The recv buffer can store out of order data as long as there is buffer space and the sequence number is within its sliding windows 2007 CSCE 515 – Computer Network Programming

ACKs A receiver doesn’t have to ACK every segment (it can ACK many segments

ACKs A receiver doesn’t have to ACK every segment (it can ACK many segments with a single ACK segment). n Each ACK can also contain outgoing data (piggybacking). n If a sender doesn’t get an ACK after some time limit (MSL) it resends the data. n 2007 CSCE 515 – Computer Network Programming

TCP Flow Control Sender Application does a 2 K write 2 K ACK =

TCP Flow Control Sender Application does a 2 K write 2 K ACK = SEQ=0 receiver recv’s buffer 4 K 0 empty 48 2048 WIN = 20 2 K Application does a 3 K write 2 K Sender is blocked SEQ=2048 Full ACK = 4096 Sender may send up to 2 k 1 k WIN = 0 Application reads 2 k WIN = 2048 2 K SEQ=4096 1 K 2007 CSCE 515 – Computer Network Programming 2 K

TCP Segment Order n n n 2007 Most TCP implementations will accept out-oforder segments

TCP Segment Order n n n 2007 Most TCP implementations will accept out-oforder segments (if there is room in the buffer). Once the missing segments arrive, a single ACK can be sent for the whole thing. Remember: IP delivers TCP segments, and IP in not reliable - IP datagrams can be lost or arrive out of order. CSCE 515 – Computer Network Programming

Termination The TCP layer can send a RST segment that terminates a connection if

Termination The TCP layer can send a RST segment that terminates a connection if something is wrong. n Usually the application tells TCP to terminate the connection politely with a FIN segment. n 2007 CSCE 515 – Computer Network Programming

FIN Either end of the connection can initiate termination. n A FIN is sent,

FIN Either end of the connection can initiate termination. n A FIN is sent, which means the application is done sending data. n The FIN is ACK’d. n The other end must now send a FIN. n That FIN must be ACK’d. n 2007 CSCE 515 – Computer Network Programming

TCP Termination App 2 App 1 “I have no more data for you” FIN

TCP Termination App 2 App 1 “I have no more data for you” FIN SN=X 1 2 ACK=X+1 “OK, I understand you are done sending. ” Release connection . . . Release connection FIN SN=Y “Over and Out, Goodbye” Release connection 2007 ACK=Y+1 3 “OK - Now I’m also done sending data”. 4 CSCE 515 – Computer Network Programming Release connection

TCP Termination App 2 App 1 “I have no more data for you” FIN_WAIT_1

TCP Termination App 2 App 1 “I have no more data for you” FIN_WAIT_1 FIN SN=X 1 2 ACK=X+1 CLOSE_WAIT . . . FIN_WAIT_2 “OK, I understand you are done sending. ” FIN SN=Y “Over and Out, Goodbye” TIME_WAIT 2007 ACK=Y+1 3 “OK - Now I’m also done sending data”. LAST_ACK 4 CSCE 515 – Computer Network Programming CLOSED

TCP TIME_WAIT n Once a TCP connection has been terminated (the last ACK sent)

TCP TIME_WAIT n Once a TCP connection has been terminated (the last ACK sent) there is some unfinished business: ¨ What if the ACK is lost? The last FIN will be resent and it must be ACK’d. ¨ What if there are lost or duplicated segments that finally reach the destination after a long delay? n 2007 TCP hangs out for a while to handle these situations. CSCE 515 – Computer Network Programming

Test Questions Why is a 3 -way handshake necessary? n Who sends the first

Test Questions Why is a 3 -way handshake necessary? n Who sends the first FIN - the server or the client? n Once the connection is established, what is the difference between the operation of the server’s TCP layer and the client’s TCP layer? n What happens if a bad guy can guess ISNs? n 2007 CSCE 515 – Computer Network Programming