Docstoc

View File

Document Sample
View File Powered By Docstoc
					Network & System Programming
      The Transport Layer
        TCP and UDP
          Lecture 04
               Introduction

• An overview of TCP/IP Protocol
  – Its user from network programming perspective
• Main focus will be on TCP and UDP
  – Used by most client-server application




                                                2
The Big Picture




                  3
Encapsulation




                4
  UDP : User Datagram Protocol
• Application writes a datagram to a UDP socket, which is
  encapsulated as either a IPv4 of a IPv6 datagram, which is
  then sent to its destination
• UDP provides a connectionless service
   – A UDP socket can send a datagram to a given server and then
     immediately send next datagram to another from the same socket
   – A UDP server can receive five datagrams in a row on a single
     UDP socket, each from five different clients
• UDP lacks reliability, sequence numbers, RTT
  estimation, timeouts, or retransmissions
• Length of the datagram is passed to the application when it
  arrives at the destination correctly                       5
            UDP Header

Bit 0                 16                      31

        Source Port        Destination Port


          Length             Checksum




                                                   6
TCP: Transmission Control Protocol
• Provides connections between clients and servers
• Provides reliability
   – Ack, retransmission after RTT ( round-trip-time)
   – Total number of time spent trying to send data typically
     between 4-10 min
• TCP also sequences the data by associating a
  sequence number with every byte that it sends
• TCP provides flow control
   – Sliding window protocol
• A TCP connection is also full-duplex
                                                                7
TCP Header




             8
     TCP Connection Establishment
             Three-Way Handshake
• The server must be prepared to accept connection
  (passive open)
   – socket, bind, listen
• The client calls connect (active open)
• A SYN segment is sent telling the server the initial
  sequence number of client
• The server must ACK the clients SYN and also
  send its own SYN to the client
• The client must ACK the servers SYN
• Each SYN can contain TCP options
                                                     9
Three-Way Handshake




                      10
                 TCP Options
• MSS option
  – With this option the TCP sending the SYN announces
    its maximum segment size, the maximum amount of
    data that it is willing to accept in each TCP segment, on
    this connection
  – Can be set with TCP_MAXSEG socket option
• Timestamp option
  – Needed for high speed connections to avoid possible
    data corruption caused by lost packets that reappear
                                                           11
                  TCP Options
• Window Scale option
  – Maximum window size that either TCP can advertise is
    65535 (because of 16 bit field in TCP header)
  – High speed networks (45 Mbits/sec and faster) or long delay
    path (satellite links) requires a larger window to obtain the
    maximum throughput possible
  – The newer option specifies that advertise window in the TCP
    header must be scaled (left shifted) by 0-14 bits, providing a
    maximum window of almost one gigabyte (65535 x 1014)
  – Both end systems must support this option for the window
    scale to be used on a connection
  – SO_RCVBUF socket option is used to effect this option
                                                                12
      TCP Connection Termination
1.    One application calls close first (active close), this end’s TCP sends a FIN
      segment, which means it is finishes sending the data

2.    The other side that receives FIN performs the passive close
     1.   The receive FIN is acknowledge by TCP

     2.   The receipt of the FIN is also passed to the application as an end-of-file (after any data
          that may be queued for the application to receive)

3.    Some times later the application that received the end-of-file will close its
      socket
     1.   This cause its TCP to send a FIN

4.    The TCP on the system that receives this final FIN (the end that did the
      active close) acknowledges the FIN                                                          13
TCP Connection Termination




                             14
    TCP Connection Termination
•    FIN in step one can be sent with data
•    Normally four segments are required for closing the connection, but
     sometimes it can be lesser
•    Segments in step 2 and 3 are both from the end performing the
     passive close and could be combined into one segment
•    A FIN occupies 1 byte of sequence number space just like SYN,
     therefore, the ACK of each FIN is the sequence number of the FIN
     plus one
•    Between step 2 and 3 it is possible for data to flow from the end
     doing the passive close to the end doing the active close
    –   This is called half-close
•    Often the client performs the active close, but with some protocols
     (HTTP), the server performs the active close
                                                                           15
States for TCP




                 16
TCP State
Transition
 Diagram




             17
State Transition Diagram




                           18
Common scenario




                  19
• MSL is the maximum amount of time
that any given IP datagram can live in
              an internet
•The common value for MSL is between
       30 seconds to 4 minute


                                         20
Connection Termination using Three-way Handshake




                                                   21
Simultaneous Open




                    22
Simultaneous Close




                     23
Denying a Connection




                       24
Aborting a Connection




                        25
Packet Exchange
 for a complete
TCP Connection:
 the connection
 establishment,
data transfer, and
   connection
   termination
 • Piggybacking
 normally happens
 when the time it
 takes the server to
 process the request
 and generates the
 reply is less than
 200 ms
 • If the server takes
 longer then ack
 followed later by
                         26
 the reply
                TIME_WAIT State
• Most misunderstood concept of TCP
• The end that performs the active close goes through
  TIME_WAIT state
• The duration of TIME_WAIT state is twice the MSL (maximum
  segment life time), some times called 2MSL
• Varies from 30 sec to 4 minutes
• MSL is the maximum amount of time that any given IP datagram
  can live in an internet
• This time is bounded because every datagram contains an 8-bit
  hop limit with a maximum value of 255
   – This is the hop limit not the true time limit
• The assumption is made that a packet with the maximum hop
  limit of 255 cannot exist in an internet for more than MSL
  seconds
                                                               27
                    TIME_WAIT State
• Packet lost in an internet is usually the result of routing anomalies
   – Router crashes
   – Link between two routers goes down and it takes the routing protocols seconds or
     minutes to stabilize and find an alternate path
   – During this time routing loops can occur (router A sends packets to router B, and
     router B sends back to A)
• In the meantime, the sending TCP times out and retransmits the
  packet, that gets to the final destination through some alternate
  path
• Sometimes later, the routing loop is corrected and the packet that
  was lost in the loop is sent to the final destination
   – This original packet is called a lost duplicate or a wandering duplicate
• TCP must handles these duplicate                                                28
                  TIME_WAIT State
• There are two reason for TIME_WAIT state
   1.   To implement TCP’s full-duplex connection termination reliably
        •   Assume the final ACK is lost
        •   The server will resend the final FIN so that the client must maintain state
            information allowing it to resend the final ACK
        •   If the client goes to Closed state before the 2MSL timer expires, it will
            never receive this FIN segment, consequently server never receive final
            Ack and server can not close the connection
        •   Client in Closed state would respond with a RST, which would be
            interpreted by the server as an error
        •   If during the TIME_WAIT state, a new FIN arrives, the client sends a new
            AcK and restarts the 2MSL timer
        •   2MSL timer makes the client wait for a duration that is enough time for an
            Ack to be lost (one MSL) and a FIN to arrive (another MSL)

                                                                                 29
Common scenario




                  30
          TIME_WAIT State

2. To allow old duplicate segments to expire in
   the network
  •   During the incarnation of the previous connection, old
      duplicates can reappear and being misinterpreted as belonging
      to the new incarnation of the same connection
  •   TCP will not initiate a new incarnation of a connection that is
      currently in TIME_WAIT state
  •   Since the duration of the TIME_WAIT state is twice the MSL,
      this allows MSL seconds for a packet in one direction to be
      lost, and another MSL seconds for the reply to be lost



                                                                   31
                            Port Numbers




•   Multiple processes can use either TCP or UDP
•   Both TCP and UDP use 16-bit integer port numbers to differentiate between these
    processes
•   Internet Assigned Numbers Authority (IANA)
     –   Well-known Ports (0-1023)
     –   Registered Ports (1024-49151)
     –   Ephemeral Ports (49152-65535)                                          32
                    Socket Pair
• The socket pair for a TCP connection is the 4- tuple
  that defines the two endpoints of the connection
   –   Local IP address
   –   Local TCP Port
   –   Remote IP address
   –   Remote TCP Port
• A socket pair uniquely identifies every TCP
  connection on an internet
• The two values that identify each endpoint, an IP
  address and a port number, are often called a socket
                                                     33
    TCP Port Numbers and Concurrent Servers
•   With a concurrent server, where the main server loop spawns a child to
    handle each new connection, What happens if the child continues to
    use the well-known port number while servicing a long request?
•   Server waiting for a connection request on any local interface (the first
    asterisk) on port 21
•   There is one-or-any choice for the server




                                                                           34
TCP Port Numbers and Concurrent Servers




                                     35
TCP Port Numbers and Concurrent Servers




                                          36
TCP Port Numbers and Concurrent Servers

                           206.62.226.35
                           206.62.226.66                                      198.69.10.2


                                  server                                         client1
 listening socket            (*.21, *.*)                                   {198.69.10.2.1500,
                                                                  ion
                                                              ect          206.62.226.35.21}
                                                          nn
               fork                  fork            co


                               server
                                                                                 client2
                              (child1)
   connected
     socket             {206.62.226.35.21,                                 {198.69.10.2.1501,
                                                                io     n
                        198.69.10.2.1500}                 n ect            206.62.226.35.21}
                                                        n
                                                     co


                               server
                              (child2)
   connected
     socket             {206.62.226.35.21,
                        198.69.10.2.1501}




                    Figure 2.10     Second client connection with same server
                                                                                                37

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:8/1/2013
language:English
pages:37