netprog: tcp details1 tcp details introduction to networking yan gao ta jan 27, 2005 recital 3

27
Netprog: TCP Details 1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Upload: crystal-brooks

Post on 18-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 1

TCP Details

Introduction to NetworkingYan Gao

TA Jan 27, 2005

Recital 3

Page 2: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 2

The TCP ProjectImportant Handouts:

• Minet– The Minet Technical Report– The Minet Socket Interface

• TCP Guides– RFC 793  (in convenient HTML format), RFC 1122– A very useful summary and picture of the TCP State

diagram– TCP, UDP and IP pocket guide includes header details– Brief overview of TCP contains a nice summary of the

essentials– Here is a page with nice TCP animations. They explain TCP

connection startup, termination, data flow and flow control and cumulative ack concepts. Please see animations 20_1 to 20_5.

Page 3: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 3

TCP Lingo

• When a client requests a connection, it sends a “SYN” segment (a special TCP segment) to the server port.

• SYN stands for synchronize. The SYN message includes the client’s ISN.

• ISN is Initial Sequence Number.

Page 4: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 4

More...

• Every TCP segment includes a Sequence Number that refers to the first byte of data included in the segment.

• Every TCP segment includes a Request Number (Acknowledgement Number) that indicates the byte number of the next data that is expected to be received.

Page 5: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 5

And more...

• There are a bunch of control flags:– URG: urgent data included.– ACK: this segment is (among other things)

an acknowledgement.– RST: error - abort the session.– SYN: synchronize Sequence Numbers

(setup)– FIN: polite connection termination.

Page 6: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 6

And more...

• MSS: Maximum segment size (A TCP option)

• 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).

Page 7: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 7

TCP Connection Creation

• A server accepts a connection.– Must be looking for new connections!

• A client requests a connection.– Must know where the server is!

Page 8: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 8

Client Starts

• 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)

Page 9: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 9

Sever Response• 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)

Page 10: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 10

Finally

• When the Server’s SYN is received, the client sends back an ACK with:– Request Number is Server’s ISN+1

Page 11: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 11

Client Server

SYNISN=X

SYNISN=X

1

SYNISN=Y ACK=X+1

SYNISN=Y ACK=X+1

2

ACK=Y+1ACK=Y+1 3

time

Page 12: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 12

Why 3-Way?

• Why is the third message necessary?

• HINTS: – TCP is a reliable service.– IP delivers each TCP segment.– IP is not reliable.

Page 13: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 13

TCP Data and ACK

• Once the connection is established, data can be sent.

• Each data segment includes a sequence number identifying the first byte in the segment.

• Each segment (data or empty) includes a request number indicating what data has been received.

Page 14: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 14

Buffering

• Keep in mind that TCP is (usually) part of the Operating System. It takes care of all these details asynchronously.

• The TCP layer doesn’t know when the application will ask for any received data.

• TCP buffers incoming data so it’s ready when we ask for it.

Page 15: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 15

TCP Buffers

• Both the client and server allocate buffers to hold incoming and outgoing data– The TCP layer does this.

• Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment).

Page 16: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 16

Send Buffers

• 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 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.

Page 17: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 17

ACKs

• A receiver doesn’t have to ACK every segment (it can ACK many segments with a single ACK segment).

• Each ACK can also contain outgoing data (piggybacking).

• If a sender doesn’t get an ACK after some time limit (MSL) it resends the data.

Page 18: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 18

TCP Segment Order

• Most TCP implementations will accept out-of-order 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 datagram can be lost or arrive out of order.

Page 19: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 19

Termination

• The TCP layer can send a RST segment that terminates a connection if something is wrong.

• Usually the application tells TCP to terminate the connection politely with a FIN segment.

Page 20: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 20

FIN

• Either end of the connection can initiate termination.

• A FIN is sent, which means the application is done sending data.

• The FIN is ACK’d.

• The other end must now send a FIN.

• That FIN must be ACK’d.

Page 21: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 21

App1 App2

FINSN=X

FINSN=X

1

ACK=X+1ACK=X+12

ACK=Y+1ACK=Y+1 4

FINSN=Y

FINSN=Y

3...

Page 22: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 22

Page 23: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 23

Implementing TCP Interactive Data Flow

• DF_listen• DF_SynRcvd• DF_SynSent• DF_Established• DF_SendData• DF_CloseWait• DF_FinWait1• DF_Closing• DF_LastAck• DF_FinWait2• DF_TimeWait

Page 24: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 24

Implementing TCP-Socket Layer interface

• SOCK_connect– Create an active connection– Add new connection to list– Return a STATUS with the same connection, no data, no byte count and the error

code• SOCK_accept

– Perform the passive open– Return a STATUS with only the error code set

• SOCK_write– Push data into the connection’s output queue

• SOCK_forward– Forward matching packets– Message ignored, but Send back result code

• SOCK_close– Received a CLOSE request from Sock Layer– Remove the connection, and create a FIN packet

• SOCK_status– Status update– Byte count actually reflects the number of bytes read from the WRITE– Attempt to send the remaining bytes

Page 25: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 25

Thank you !

Page 26: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 26

Implementing TCP Interactive Data Flow

• DF_Listen{ if(IS_SYN(flags))

//Store remote side sequence number and connection information with a new connection, send SYN/ACK with my sequence number

}• DF_SynRcvd{

if(IS_ACK(flags)) //Send nothing and progress to ESTABLISHED state if valid ACK

else if(IS_SYN(flags)) //Store remote side sequence number and connection information //with a new connection, send SYN/ACK with my sequence number.

//If we get here, we recvd another syn before timing out.

else if(IS_RST(flags)) //close the connection, return to listen state

}

Close

Page 27: Netprog: TCP Details1 TCP Details Introduction to Networking Yan Gao TA Jan 27, 2005 Recital 3

Netprog: TCP Details 27

Samples

//Send back result code

SockRequestResponse repl;

repl.type=STATUS;

repl.connection=req.connection;

repl.bytes=0;

repl.error=EOK;

MinetSend(sock,repl); Close