csce 515: computer network programming ------ tcp details wenyuan xu wyxu/csce515f07.html department...
TRANSCRIPT
CSCE 515:Computer Network Programming
------ TCP DetailsWenyuan Xu
http://www.cse.sc.edu/~wyxu/csce515f07.htmlDepartment of Computer Science and EngineeringUniversity of South Carolina
CSCE515 – Computer Network Programming2007
TCP TCP provides the end-to-end reliable
connection that IP alone cannot support
The TCP protocolFrame formatConnection CreationFlow controlCongestion controlConnection termination
CSCE515 – Computer Network Programming2007
TCP Segment Format
0 1516 31
20 bytes
destination port number
urgent pointerTCP checksum
option (if any)
source port number
window size
sequence number
acknowledgment number
headerlength
reservedURG
ACK
PSH
RST
SYN
FIN
data (if any)
CSCE515 – Computer Network Programming2007
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
TCP Connection Establishment – Three-way handshake
“I want to talk, and I’m starting with byte number X+1”.
“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”
“Got it - you start at byte number Y+1”.
CSCE515 – Computer Network Programming2007
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 ACK segment includes a request number indicating what data has been received. (bytes instead of packets)
CSCE515 – Computer Network Programming2007
Important Information in TCP/IP packet headers
SendN SEQ
RecvACK WIN
Number of bytes in packet (N)
ACK bit set
Sequence number of next expected byte (ACK)
Sequence number of first data byte in packet (SEQ)
Window size at the receiver (WIN)
Contained in IP header Contained in TCP header
CSCE515 – Computer Network Programming2007
Buffering The TCP layer doesn’t know when the
application will ask for any received data.
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
it. outgoing data-> it can be retransmitted should error
occurs
Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment).
CSCE515 – Computer Network Programming2007
Send Buffers The application gives the TCP layer some data t
o send. The data is put in a send buffer, where it stays u
ntil 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.
CSCE515 – Computer Network Programming2007
Recv Buffers The received data is put in the recv buffer, wher
e it stays until the application reads data
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.
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
CSCE515 – Computer Network Programming2007
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.
CSCE515 – Computer Network Programming2007
TCP Flow ControlSender
Application does a 2K write
Application reads 2k
Sender is blocked
2K SEQ=0empty
receiver
0 4K
recv’s buffer
2KACK = 2048 WIN = 2048
Application does a 3K write
2K SEQ=2048
Full
ACK = 4096 WIN = 0
ACK = 4096 WIN = 2048
1k SEQ=4096
Sender may send up to 2k
2K
1K 2K
CSCE515 – Computer Network Programming2007
TCP Segment Order Most TCP implementations will accept out-of-ord
er 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.
CSCE515 – Computer Network Programming2007
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.
CSCE515 – Computer Network Programming2007
FIN Either end of the connection can initiate ter
mination. 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.
CSCE515 – Computer Network Programming2007
App1 App2
FINSN=X
FINSN=X
1
ACK=X+1ACK=X+12
ACK=Y+1ACK=Y+1 4
FINSN=Y
FINSN=Y
3...
“I have no more data for you”
“OK, I understand you
are done sending.”Release connection?
“OK - Now I’m alsodone sending data”.
“Over and Out, Goodbye”Release connection?
TCP Termination
Release connection?
Release connection?
CSCE515 – Computer Network Programming2007
App1 App2
FINSN=X
FINSN=X
1
ACK=X+1ACK=X+12
ACK=Y+1ACK=Y+1 4
FINSN=Y
FINSN=Y
3...
“I have no more data for you”FIN_WAIT_1
“OK, I understand you
are done sending.”CLOSE_WAIT
“OK - Now I’m alsodone sending data”.LAST_ACK
“Over and Out, Goodbye”TIME_WAIT
TCP Termination
FIN_WAIT_2
CLOSED
CSCE515 – Computer Network Programming2007
TCP TIME_WAIT Once a TCP connection has been termi
nated (the last ACK sent) there is some unfinished business:What if the ACK is lost? The last FIN will b
e resent and it must be ACK’d.What if there are lost or duplicated segmen
ts that finally reach the destination after a long delay?
TCP hangs out for a while to handle these situations.
CSCE515 – Computer Network Programming2007
Test Questions
Why is a 3-way handshake necessary? Who sends the first FIN - the server or the
client? Once the connection is established, what i
s the difference between the operation of the server’s TCP layer and the client’s TCP layer?
What happens if a bad guy can guess ISNs?
CSCE515 – Computer Network Programming2007
TCP Sockets Programming Creating a passive mode (server) socket. Establishing an application-level
connection. send/receive data. Terminating a connection.
CSCE515 – Computer Network Programming2007
Client-Server Communication (TCP)socket()
bind()
listen()
accept()
read()
write()
read()
close()
socket()
connect()
write()
read()
close()
TCP Client
TCP Serverwell-known port
blocks until connection from client
process request
connection establishment
data(request)
data(reply)
end-of-file notification
int socket(int family, int type, int protocol);
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
int listen(int sockfd, int backlog);
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
int accept(int sockfd, void *addr, int *addrlen);
int close(int sockfd); int close(int sockfd)
;
int socket(int family, int type, int protocol);
CSCE515 – Computer Network Programming2007
Creating a TCP socket
int socket(int family,int type,int proto);
int sock;
sock = socket( PF_INET, SOCK_STREAM,
0);
if (sock<0) { /* ERROR */ }
CSCE515 – Computer Network Programming2007
Binding to well known addressint mysock;struct sockaddr_in myaddr;
mysock = socket(PF_INET,SOCK_STREAM,0);myaddr.sin_family = AF_INET;myaddr.sin_port = htons( 80 );myaddr.sin_addr = htonl( INADDR_ANY );
bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));
CSCE515 – Computer Network Programming2007
Establishing a passive mode TCP socket
Passive mode:Address already determined.
Tell the kernel to accept incoming connection requests directed at the socket address.
3-way handshake
Tell the kernel to queue incoming connections for us.
CSCE515 – Computer Network Programming2007
listen()int listen( int sockfd, int backlog);
sockfd is the TCP socket (already bound to an address)
backlog is the number of incoming connections the kernel should be able to keep track of (queue for us).
listen() returns -1 on error (otherwise 0).
CSCE515 – Computer Network Programming2007
listen()
Server
TCP
3-way handshake complete
accept
arrivingSYN
Completed connection queue
Incomplete connection queue
Sum of both queues cannot exceed backlog
CSCE515 – Computer Network Programming2007
Accepting an incoming connection. Once we call listen(), the O.S. will
queue incoming connectionsHandles the 3-way handshakeQueues up multiple connections.
When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.
CSCE515 – Computer Network Programming2007
accept()int accept( int sockfd,
struct sockaddr* cliaddr, socklen_t *addrlen);
sockfd is the passive mode TCP socket.cliaddr is a pointer to allocated space.addrlen is a value-result argument
must be set to the size of cliaddron return, will be set to be the number of
used bytes in cliaddr.
CSCE515 – Computer Network Programming2007
accept() return value
accept() returns a new socket descriptor (small positive integer) or -1 on error.
After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls.
read() and write() operate a little differently on sockets (vs. file operation)!
CSCE515 – Computer Network Programming2007
Terminating a TCP connection Either end of the connection can call the close() system call.
If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.
CSCE515 – Computer Network Programming2007
Client Code TCP clients can call connect() which:
takes care of establishing an endpoint address for the client socket.
don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address).
Attempts to establish a connection to the specified server.
3-way handshake
CSCE515 – Computer Network Programming2007
connect()
int connect( int sockfd,
const struct sockaddr *server,
socklen_t addrlen);
sockfd is an already created TCP socket.
server contains the address of the server (IP Address and TCP port number)
connect() returns 0 if OK, -1 on error
CSCE515 – Computer Network Programming2007
Reading from a TCP socketint read( int fd, char *buf, int max);
By default read() will block until data is available.
reading from a TCP socket may return less than max bytes (whatever is available).
You must be prepared to read data 1 byte at a time!
CSCE515 – Computer Network Programming2007
Writing to a TCP socketint write( int fd, char *buf, int num);
write might not be able to write all num bytes (on a nonblocking socket).
The book includes readn(), writen() and readline() function definitions.
CSCE515 – Computer Network Programming2007
fork() In Unix the way to create a new process is the fork()system call.
fork()is called once but it returns twice
Return value: 0: return in the child Non-0: the PID of the newly created process
CSCE515 – Computer Network Programming2007
Client/Server before call to accept return
Client (129.1.1.200) server
connect()
listenfdconnection request
129.1.1.200:1500 65.1.1.200
*:80
listenfd=socket(…)bind(listenfd…)listen(listenfd,LISTENQ);For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) {
close(listendf);doit(connfd);close(connfd);exit(0);
}close(connfd);
CSCE515 – Computer Network Programming2007
Client/Server After call to accept return
Client (129.1.1.200) server
connect()
listenfdconnection request
{129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200
*:80
connfd
listenfd=socket(…)bind(listenfd…)listen(listenfd,LISTENQ);For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) {
close(listendf);doit(connfd);close(connfd);exit(0);
}close(connfd);
CSCE515 – Computer Network Programming2007
Client/Server After call to accept return
Client (129.1.1.200) Server (parent)
connect()
listenfdconnection request
{129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200
*:80
connfd
listenfd=socket(…)bind(listenfd…)listen(listenfd,LISTENQ);For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) {
close(listendf);doit(connfd);close(connfd);exit(0);
}close(connfd);
listenfd
*:80
connfd
Server (child)fork
CSCE515 – Computer Network Programming2007
Client/Server After call to accept return
Client (129.1.1.200) Server (parent)
connect()
listenfd
connection
{129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200
*:80
listenfd=socket(…)bind(listenfd…)listen(listenfd,LISTENQ);For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) {
close(listendf);doit(connfd);close(connfd);exit(0);
}close(connfd);
listenfd
*:80
connfd
Server (child)