TCP Connection Management(1)

Introduction

Recall that TCP’s service model is a byte stream.

TCP detects and repairs essentially all the data transfer problems that may be introduced by packet loss, duplication, or errors at the IP layer (or below).

UDP is a connectionless protocol that involves no con- nection establishment or termination.

Some options are allowed to be sent only when the connection is established, and others can be sent later.

Recall from Chapter 12 that the TCP header has a limited space for holding options (40 bytes)

TCP Connection Establishment and Termination

A TCP connection is defined to be a 4-tuple consisting of two IP addresses and two port numbers.

More precisely, it is a pair of endpoints or sockets where each end- point is identified by an (IP address, port number) pair.

A connection typically goes through three phases: setup, data transfer (called established), and teardown (closing).

the following events usually take place:

1. The active opener (normally called the client) sends a SYN segment (i.e., a TCP/IP packet with the SYN bit field turned on in the TCP header)

specify- ing the port number of the peer to which it wants to connect

and the client’s initial sequence number or ISN(c) (see Section 13.2.3).

It typically sends one or more options at this point (see Section 13.3). This is segment 1.

2 The server responds with its own SYN segment containing its initial sequence number (ISN(s)).

This is segment 2. The server also acknowledges the client’s SYN by ACKing ISN(c) plus 1.

A SYN consumes one sequence number and is retransmitted if lost.

3 The client must acknowledge this SYN from the server by ACKing ISN(s) plus 1. This is segment 3.

This is often called the three-way handshake.

Its main purposes are to let each end of the connec- tion know that a connection is starting and

the special details that are carried as options, and to exchange the ISNs.

In Section 13.2.2 we describe a supported but unusual simultaneous open when

both sides can do an active open at the same time and become both clients and servers.

Either end can initiate a close operation, and simultaneous closes are also supported but are rare.

Usually a close operation starts with an application indicating its desire to terminate its connection (e.g., using the close() system call).

The closing TCP initiates the close operation by sending a FIN segment (i.e., a TCP segment with the FIN bit field set).

1 The active closer sends a FIN segment specifying the current sequence num- ber the receiver expects to see (K in Figure 13-1).

The FIN also includes an ACK for the last data sent in the other direction (labeled L in Figure 13-1).

2 The passive closer responds by ACKing value K + 1 to indicate its success- ful receipt of the active closer’s FIN.

At this point, the application(server) is noti- fied that the other end of its connection has performed a close.

Typically this results in the application initiating its own close operation.

The passive closer then effectively becomes another active closer and sends its own FIN.

The sequence number is equal to L(server not waste sn yet).

3 To complete the close, the final segment contains an ACK for the last FIN.

Note that if a FIN is lost, it is retransmitted until an ACK for it is received.

This reason is that TCP’s data communica- tions model is bidirectional, meaning it is possible to have only one of the two directions operating.

The half-close operation in TCP closes only a single direction of the data flow.

Two half-close operations together close the entire connection.

There are more abrupt ways to tear down a TCP connection using special reset segments, which we cover later.

When a small amount of data needs to be exchanged, it is now apparent why some applications prefer to use UDP because of its ability to send and receive data with- out establishing connections.

TCP Half-Close

The Berkeley sockets API supports half-close, if the application calls the shutdown() function instead of calling the more typical close() func- tion.

Most applications, however, terminate both directions of the connection by calling close.

Figure 13-2 shows an example of a half-close being used. We show the client on the left side initiating the half-close, but either end can do this.

When the end that received the half-close is done sending data, it closes its end of the connection, causing a FIN to be sent,

and this delivers an end-of-file indication to the application that initiated the half-close.

Simultaneous Open and Close

It is possible, although highly improbable unless specifically arranged, for two applications to perform an active open to each other at the same time.

The only real difference is that the segment sequence is inter- leaved instead of sequential.

Initial Sequence Number (ISN)

This brings up the question of whether it might be possible to have TCP segments being routed through the net- work that could show up later and disrupt a connection.

This concern is addressed by careful selection of the ISN.

Before each end sends its SYN to establish the connection, it chooses an ISN for that connection.

The ISN should change over time, so that each connection has a different one.

[RFC0793] specifies that the ISN should be viewed as a 32-bit counter that increments by 1 every 4μs.

The purpose of doing this is to arrange for the sequence numbers for segments on one connection to not overlap with sequence numbers on a another (new) identical connection.

In particular, new sequence numbers must not be allowed to overlap between different instantiations (or incarnations) of the same connection.

The idea of different instantiations of the same connection becomes clear when we recall that a TCP connection is identified by a pair of endpoints, creat- ing a 4-tuple of two address/port pairs.

If a connection had one of its segments delayed for a long period of time and closed, but then opened again with the same 4-tuple,

it is conceivable that the delayed segment could reenter the new connec- tion’s data stream as valid data.

This would be most troublesome. By taking steps to avoid overlap in sequence numbers between connection instantiations, we can try to minimize this risk.

It does suggest, however, that an application with a very great need for data integrity should employ its own CRCs or checksums at the application layer to ensure that its own data has been transferred without error.

This is generally good practice in any case, and it is commonly done for large files.

As we shall see, knowing the connection 4-tuple as well as the currently active window of sequence numbers is all that is required to form a TCP segment that is considered valid to a communicating TCP endpoint.

This represents a form of vul- nerability for TCP: anyone can forge a TCP segment and, if the sequence numbers, IP addresses, and port numbers are chosen appropriately, can interrupt a TCP connection [RFC5961].

One way of repelling this is to make the initial sequence number (or ephemeral port number [RFC6056]) relatively hard to guess. Another is encryption (see Chapter 18).

Example

second segment:

...

This segment also includes a window advertisement indicating that the server is willing to accept up to 64,240 bytes.

...

finish third segment:
...

This segment also (redundantly) ACKs the client’s FIN once again.

...

Note that the PSH bit field is on. This has no real effect on the closing of the connection but usually indicates that the server has no additional data to send.

...

One thing we can see in Figure 13-5 is that the SYN segments contain one or more options.

These take up additional space in the TCP header. For example, the length of the first TCP header is 44 bytes, 24 bytes greater than the minimum size.

...

Timeout of Connection Establishment

There are several circumstances in which a connection cannot be established. One obvious case is when the server host is down.

If, however, we place an ARP entry for a nonexistent host in the ARP table first, the ARP request is not sent,

and the system immediately attempts to contact the nonexistent host with TCP/IP.

The interesting point in this output is how frequently the client’s TCP sends a SYN to try to establish the connection.

The second segment is sent 3s after the first, the third is sent 6s after the second, the fourth is sent 12s after the third, and so on.

This behavior is called exponential backoff .

The number of times to retry an initial SYN can be configured on some sys- tems and usually has a fairly small value such as 5.

In Linux, the system configura- tion variable net.ipv4.tcp_syn_retries gives the maximum number of times to attempt to resend a SYN segment during an active open.

A corresponding value called net.ipv4.tcp_synack_retries gives the maximum number of times to attempt to resend a SYN + ACK segment when responding to a peer’s active open request.

It can also be used on an individual connection basis by setting the Linux-specific TCP_SYNCNT socket option.

Its default value is five retries, as we see here.

The exponential backoff timing between these retransmissions is part of TCP’s congestion management response.

We shall examine it in detail when we discuss Karn’s algorithm (see Chapter 16).

原文地址:https://www.cnblogs.com/geeklove01/p/9733369.html

时间: 2024-10-10 10:18:00

TCP Connection Management(1)的相关文章

TCP Connection Management(4)

Reset Segments A segment hav- ing this bit set to "on" is called a "reset segment" or simply a "reset." In general, a reset is sent by TCP whenever a segment arrives that does not appear to be correct for the referenced conne

TCP Connection Management(2)

Connections and Translators NAT TCP Options Every option begins with a 1-byte kind that specifies the type of option. Options that are not understood are simply ignored, according to [RFC1122]. The options with a kind value of 0 and 1 occupy a single

idea启动tomcat服务失败 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild:

我的工程是从eclipse生成的,个人习惯用idea开发.重复了一遍以往正常的不能再正常了的导入配置,结果遇到了如下问题: SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start comp

A TCP connection is distinguished by four values

4个值唯一地定义一条TCP连接. HTTP The Definitive Guide A computer might have several TCP connections open at any one time. TCP keeps all these connections straight through port numbers. Port numbers are like employees' phone extensions. Just as a company's main

HttpWebRequest's Timeout and ReadWriteTimeout — What do these mean for the underlying TCP connection?

http://stackoverflow.com/questions/7250983/httpwebrequests-timeout-and-readwritetimeout-what-do-these-mean-for-the-unde https://support.microsoft.com/en-us/kb/904262 https://msdn.microsoft.com/library/e1ax3cw0.aspx?f=255&MSPPError=-2147217396 https:/

TCPIP------TCP Connection Management

TCP Connection State Diagram +---------+ ---------\ active OPEN | CLOSED | \ ----------- +---------+<---------\ \ create TCB | ^ \ \ snd SYN passive OPEN | | CLOSE \ \ ------------ | | ---------- \ \ create TCB | | delete TCB \ \ V | \ \ +---------+

ASA TCP Connection Flags

Have you ever wondered what the flags meant when you issued the show conn or show connections command? This post will demystify that for you. Viewing the Connections Use the command show conn to view the connections currently going through the firewa

Exception in thread &quot;RMI TCP Connection(idle)&quot; java.lang.OutOfMemoryError: PermGen space

在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space 在google半天后,找到解决方法: Tomcat直接运行 Catalina.sh/Catalina.bat 设置: windows: set JAVA_OPTS="-Xms1024m -Xmx10246m -X

RT: TCP connection close

CLOSE is an operation meaning "I have no more data to send." The notion of closing a full-duplex connection is subject to ambiguous interpretation, of course, since it may not be obvious how to treat the receiving side of the connection. We have