Network Protocol Stack 6: TCP before the connect () function

Source: Internet
Author: User

Various protocols have been involved since connect. At present, the TCP/IP protocol is clarified first.

First, the structure of TCP (transmit Control Protocol) is matched with the struct:

 

# Define header_size 64/* maximum header size */

 

Struct tcphdr {/* TCP Header */

/*

Sending terminal slogan: 16 bits

Identifies the data sending process at the source of the data packet.

*/

_ 2010source;

/*

Destination terminal slogan: 16 bits

Identifies the target data receiving process of the data packet.

*/

_ 2010dest;

/*

Serial number: 32 bits

The serial number of the first byte of the data contained in the data packet sent from the local end.

*/

_ U32 seq;

/*

Response serial number: 32 bits

This serial number indicates the serial number of the first byte data in the data contained in the next packet to be received by the local end.

Do not confuse the meaning of the two fields: serial number and response serial number. The serial number field is used to compile the data sent locally.

Number, and the response serial number is the number of the data sent to the recipient: that is, the number of the data sent by the recipient received locally.

*/

_ U32 ack_seq;

# If defined (little_endian_bitfield)

_ 2010res1: 4,

Doff: 4,

Fin: 1,

SYN: 1,

RST: 1,

Psh: 1,

Ack: 1,

URG: 1,

RES2: 2;

# Elif defined (big_endian_bitfield)

/*

Data offset: 4 bits

This field indicates the length of the TCP Header in 4 bytes (including the TCP option, if any ). In other words

The starting point of the user data (starting from the first byte of the TCP Header ). It can be seen that, like the IP header, the TCP Header must be

An integer multiple of 4 bytes. Sometimes this requires filling the byte at the end of the header.

*/

_ 2010doff: 4,

/*

Reserved field: 6 bits (4 + 2 = 6)

It must be initialized to 0.

*/

RES1: 4,

RES2: 2,

/*

Control bit: 6 bits

URG: URG = 1 indicates that the value of the emergency pointer field is valid; otherwise, the value is invalid. Urgent Data is to quickly pass the received data

A means of applications.

Ack: ACK = 1 indicates that this is a response packet (note that the response packet can contain data sent from the local end. In fact,

Rarely send only one response packet without data. In other words, the response packet is usually sent along with the data.

Sent by the way.

Psh: When PSH = 1, it means that the data needs to be handed over to the application immediately. In software processing

Use a program to send signals (sigurg ).

RST: reset flag. When rst = 1 in a message, it indicates that the other party requires a local connection to the other party. The

Flags are generally used for non-severe error recovery.

SYN: synchronization flag. When SYN = 1, it indicates that this is a synchronous packet. the synchronous packet is only used to establish a connection.

Fin: end connection packet. When fin = 1, no data is sent to the peer, but you can continue to receive the data from the peer.

. FIN packets are usually used to close connections.

*/

URG: 1,

Ack: 1,

Psh: 1,

RST: 1,

SYN: 1,

Fin: 1;

# Else

# Error "adjust your <ASM/byteorder. h> defines"

# Endif

/*

Window Size: 16 bits

This field is used for traffic control: the maximum amount of data received by the current recipient. This requirement must be noted locally,

The sent data volume must be smaller than the value declared by this field.

*/

_ 2010window;

/*

Checksum: 16 bits

This field calculates the checksum of all user data parts of the TCP header and later.

Note that the calculation of TCP checksum (UDP) also contains a pseudo-header. Figure 1-31 shows the calculation of TCP

Verification and the pseudo-header format to be included.

*/

_ 2010check;

/*

Emergency pointer: 16 bits

This field indicates the emergency data part. Its value is an offset, which is the first Data byte from the data packet.

. Valid only when the URG flag is 1.

*/

_ 2010urg_ptr;

};

 

/* Possible TCP connection status

*/

Enum {

Tcp_established = 1,/* indicates an opened connection */

Tcp_syn_sent,/* Wait for the matching connection request after sending the connection request again */

Tcp_syn_recv,/* wait for the other party to confirm the connection request after receiving and sending a connection request */

Tcp_fin_wait1,/* Wait for the remote TCP connection interruption request or confirmation of the previous connection interruption request */

Tcp_fin_wait2,/* Wait for the connection interruption request from remote tcp */

Tcp_time_wait,/* Wait for enough time to ensure that the remote TCP receives the confirmation of the connection interruption request */

Tcp_close,/* No connection status */

Tcp_close_wait,/* connection interruption requests sent from local users */

Tcp_last_ack,/* wait for confirmation of the original connection interruption request sent to remote tcp */

Tcp_listen,/* listen for connection requests from remote TCP ports */

Tcp_closing/* now a valid state waits for remote TCP to confirm the connection interruption */

};

The TCP Header records some additional information about the data packet, rather than the information itself. It mainly includes the port numbers of both sides of the data packet (at this time, the remote port number is still initialized to 0 ), the number of sent data packets, the expected validation number, and the tightness of the data packets. When sending data on the route, you can analyze these attributes to determine that the data is sent first, or try again later.

TCP header information, mainly to ensure secure transmission of data to the remote end, no confirmation of received data sent from the remote end (that is, when sending data, a member records the first byte order of the sent data, after the remote end receives the data, it must be sent back after the byte order + 1, indicating that the data has been received.) that is, if the confirmation signal is not received, it will wait for a while, re-send the data that has not received the remote confirmation, and the re-sent data has the highest priority until it receives the confirmation signal from the remote end.

When the data is transmitted to a remote machine, the packet is parsed Based on the TCP header information of the packet, and the next step is determined based on the analysis result.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.