OSI model and TCP

OSI 7 Layers

lAYER INDEXLAYERProtocol Data Unit(PDU)FunctionsProtocals
7ApplicationDataTFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet, DHCP
6PresentatonDataencoding, decoding, encrypt, decryptSSL, WPA, WEP, Kerberos
5SessionDatamaintaining communication between two devicesPPTP, TSL/SSL, RTP, NetBios, SOCKS, RTP
4TransportSegmentsTCP,UDP
3NetworkPacketsAdd logical address of sender and receiverIP,ICMP,RIP,OSPF,BGP,IGMP
2Data linkFrameError controlSLIP,CSLIP,PPP,ARP,RARP,MTU
1PhysicalBitsSO2110,IEEE802,IEEE802.2

Five Layers

lAYERLAYERValue
5ApplicationData
4TransportSegments
3NetworkPackets
2datalinkFrame
1PhysicalBits

Protocals Relationships

Based on RFC793(RFC, 1981):

       +------+ +-----+ +-----+       +-----+
       |Telnet| | FTP | |Voice|  ...  |     |  Application Level
       +------+ +-----+ +-----+       +-----+
             |   |         |             |
            +-----+     +-----+       +-----+
            | TCP |     | RTP |  ...  |     |  Host Level
            +-----+     +-----+       +-----+
               |           |             |
            +-------------------------------+
            |    Internet Protocol & ICMP   |  Gateway Level
            +-------------------------------+
                           |
              +---------------------------+
              |   Local Network Protocol  |    Network Level
              +---------------------------+

TCP

  • Connection Oriented
  • Reliable
    • ACK, SYN (every message)
    • Sender -> SYN
    • Receiver -> ACK
  • Congestion control

Based on RFC793(RFC, 1981):

TCP Header Format

在这里插入图片描述

  • Sequence Number: 32 bits

    The sequence number of the first data octet in this segment (except
    when SYN is present). If SYN is present, the sequence number is the
    initial sequence number (ISN) and the first data octet is ISN+1.

  • Acknowledgment Number: 32 bits

    If the ACK control bit is set this field contains the value of the
    next sequence number the sender of the segment is expecting to
    receive. Once a connection is established this is always sent.

  • Data Offset: 4 bits

    The number of 32 bit words in the TCP Header. This indicates where
    the data begins. The TCP header (even one including options) is an
    integral number of 32 bits long.

  • Reserved: 6 bits

    Reserved for future use. Must be zero.

  • Control Bits: 6 bits (from left to right):

    NameExplain
    URGUrgent Pointer field significant
    ACKAcknowledgment field significant
    PSHPush Function
    RSTReset the connection
    SYNSynchronize sequence numbers
    FINNo more data from sender
  • Window: 16 bits

    The number of data octets beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.(该段的发送者愿意接受的ACK字段的起始位置)

  • Checksum: 16 bits

    The checksum field is the 16 bit one’s complement of the one’s complement sum of all 16 bit words in the header and text. If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right with zeros to form a 16 bit word for checksum purposes. The pad is not transmitted as part of the segment. While computing the checksum, the checksum field itself is replaced with zeros.

  • Urgent Pointer: 16 bits

    This field communicates the current value of the urgent pointer as a positive offset from the sequence number in this segment(Urgent Pointer代表从sequence number开始的正向偏移值). The urgent pointer points to the sequence number of the octet following the urgent data. This field is only be interpreted in segments with the URG control bit set.

  • Options: variable

术语
  • 发送序列变量(Send Sequence Variables)
termexplain
SND.UNAsend unacknowledged
SND.NXTsend next
SND.WNDsend window
SND.UPsend urgent pointer
SND.WL1segment sequence number used for last window update
SND.WL2segment acknowledgment number used for last window update
ISSinitial send sequence number
  • 接收序列变量 (Receive Sequence Variables)
termexplain
RCV.NXTreceive next
RCV.WNDreceive window
RCV.UPreceive urgent pointer
IRSinitial receive sequence number
  • Current Segment Variables
termexplain
SEG.SEQsegment sequence number
SEG.ACKsegment acknowledgment number
SEG.LENsegment length
SEG.WNDsegment window
SEG.UPsegment urgent pointer
  • ELSE
termexplain
TCBtransmission control block,created and partially filled in with data from the OPEN command parameters.

A connection progresses through a series of states during its lifetime. The states are: LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT, and the fictional state CLOSED. CLOSED (虚构的)is fictional because it represents the state when there is no TCB, and therefore, no connection. Briefly the meanings of the states are:

  • LISTEN - represents waiting for a connection request from any remote TCP and port. 等待任何远程的 TCP 和端口连接请求

  • SYN-SENT - represents waiting for a matching connection request
    after having sent a connection request.

  • SYN-RECEIVED - represents waiting for a confirming connection
    request acknowledgment after having both received and sent a
    connection request.

  • ESTABLISHED - represents an open connection, data received can be
    delivered to the user. The normal state for the data transfer phase
    of the connection.

  • FIN-WAIT-1 - represents waiting for a connection termination request
    from the remote TCP, or an acknowledgment of the connection
    termination request previously sent.

  • FIN-WAIT-2 - represents waiting for a connection termination request
    from the remote TCP.

  • CLOSE-WAIT - represents waiting for a connection termination request
    from the local user.

  • CLOSING - represents waiting for a connection termination request
    acknowledgment from the remote TCP.

  • LAST-ACK - represents waiting for an acknowledgment of the
    connection termination request previously sent to the remote TCP
    (which includes an acknowledgment of its connection termination
    request).

  • TIME-WAIT - represents waiting for enough time to pass to be sure
    the remote TCP received the acknowledgment of its connection
    termination request.

  • CLOSED - represents no connection state at all.

                              +---------+ ---------\      active OPEN
                              |  CLOSED |            \    -----------
                              +---------+<---------\   \   create TCB
                                |     ^              \   \  snd SYN
                   passive OPEN |     |   CLOSE        \   \
                   ------------ |     | ----------       \   \
                    create TCB  |     | delete TCB         \   \
                                V     |                      \   \
                              +---------+            CLOSE    |    \
                              |  LISTEN |          ---------- |     |
                              +---------+          delete TCB |     |
                   rcv SYN      |     |     SEND              |     |
                  -----------   |     |    -------            |     V
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------
   |                  x         |     |     snd ACK
   |                            V     V
   |  CLOSE                   +---------+
   | -------                  |  ESTAB  |
   | snd FIN                  +---------+
   |                   CLOSE    |     |    rcv FIN
   V                  -------   |     |    -------
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |
   | --------------   snd ACK   |                           ------- |
   V        x                   V                           snd FIN V
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |
   |  -------              x       V    ------------        x       V
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+

                    Figure 6: TCP Connection State Diagram
                               
TCP 3-Way Handshake Process
      TCP P                                                TCP Q	

  1.  CLOSED                                               LISTEN

  2.  SYN-SENT    --> <SEQ=100><CTL=SYN>               --> SYN-RECEIVED

  3.  ESTABLISHED <-- <SEQ=300><ACK=101><CTL=SYN,ACK>  <-- SYN-RECEIVED

  4.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK>       --> ESTABLISHED

  5.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK><DATA> --> ESTABLISHED

          Basic 3-Way Handshake for Connection Synchronization

在这里插入图片描述

  • Step 1 (SYN) : In the first step, client wants to establish a connection with server, so it sends a segment with SYN(Synchronize Sequence Number) which informs server that client is likely to start communication and with what sequence number it starts segments with
  • Step 2 (SYN + ACK): Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of segment it received and SYN signifies with what sequence number it is likely to start the segments with
  • Step 3 (ACK) : In the final part client acknowledges the response of server and they both establish a reliable connection with which they will start the actual data transfer

假定当发生崩溃导致A的TCP内存丢失时,两个用户进程A和B正在相互通信。支持A的TCP的操作系统可能存在某种错误恢复机制。当TCP再次启动时,A可能会从头开始或从恢复点重新开始。结果,A可能会尝试再次打开连接或尝试发送它认为已打开的连接。在后一种情况下,它从本地(A的)TCP收到错误消息“连接未打开”。为了建立连接,A的TCP将发送一个包含SYN的段。这种情况导致出现下图所示的示例。TCP A崩溃后,用户尝试重新打开连接。同时,TCP B认为连接已打开。

Half-Open Connection Discovery
      TCP A                                           TCP B

  1.  (CRASH)                               (send 300,receive 100)

  2.  CLOSED                                           ESTABLISHED

  3.  SYN-SENT --> <SEQ=400><CTL=SYN>              --> (??)

  4.  (!!)     <-- <SEQ=300><ACK=100><CTL=ACK>     <-- ESTABLISHED

  5.  SYN-SENT --> <SEQ=100><CTL=RST>              --> (Abort!!)

  6.  SYN-SENT                                         CLOSED

  7.  SYN-SENT --> <SEQ=400><CTL=SYN>              -->

                     Half-Open Connection Discovery
Why Three-way Handshake

三步握手是为了防止已经建立的连接又再次建立,这会造成混乱。因此,TCP设计了reset控制信息字段。如果接收到的TCP是一个非同步状态 (i.e., SYN-SENT, SYN-RECEIVED),它就会转而去监听reset字段。一旦TCP处在一个同步状态 (ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT),它就会放弃这个连接并通知用户(RFC, 1981)。

References

  1. RFC, Transmission Control Protocal, 1981 , viewed 22 Feb 2020, https://tools.ietf.org/html/rfc793
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值