Network Protocol Stack 11: TCP layer of Connect Function Decomposition

Source: Internet
Author: User

Connect Function Decomposition 1.

First, the connect function obtains the remote IP address from the parameter, assigns this address to the corresponding variable of the corresponding sock struct, and sets some other variables in the sock struct, first, allocate (skb_buff + user space) the memory size. These two parts are allocated by calling kmalloc (sizeof (struct skb_buff) + size, priority, the two data blocks after allocation are consecutive addresses. The allocated memory is as follows:

 

Next, initialize some pointer members of the recently allocated skb_buff struct, and then return the first address of the entire data space to a skb_buff pointer for subsequent operations on the SKB struct, this pointer is used. Then, associate skb_buff with Sock, that is, set skb_buff member SK to the corresponding sock, route to the local route, and then configure the communication data.

 

Step 1: point the pointer to the data space of the skb_buff space, that is, the address (5) pointed by the member data of skb_buff ).

Step 2 (1), set the Ethernet header (6 bytes for the local MAC address + 6 bytes for the remote MAC address + 2 bytes for the data frame type), and copy the local MAC address to the beginning of the data space, the local MAC address is obtained from the local route, while the destination MAC address is generally null. If it is null, you need to set the skb_buff member ARP to 0, it indicates that the remote MAC address (or the next-hop MAC address) is unknown. You need to use ARP to search for the data frame before sending the data. You can select:

# Define eth_p_loop 0x0060/* Ethernet loopback packet */

# Define eth_p_echo 0x0200/* Ethernet ECHO packet */

# Define eth_p_pup 0x0400/* Xerox pup packet */

# Define eth_p_ip 0x0800/* Internet Protocol packet */

# Define eth_p_arp 0x0806/* Address Resolution Packet */

# Define eth_p_rarp 0x8035/* reverse ADDR res packet */

# Define eth_p_x25 0x0805/* CCITT X.25 */

# Define eth_p_atalk 0x809b/* appletalk DDP */

# Define eth_p_ipx 0x8137/* IPX over Dix */

# Define eth_p_802_3 0x0001/* dummy type for 802.3 frames */

# Define eth_p_ax25 0x0002/* dummy protocol id for ax.25 */

# Define eth_p_all 0x0003/* every packet (Be careful !!!) */

# Define eth_p_802_2 0x0004/* 802.2 frames */

# Define eth_p_snap 0x0005/* internal only */

 

Step 3: the pointer skips the Ethernet header 14 bytes and points to the IP header (2), followed by the Data Space initialization IP header. The data of the IP header is basically useful in the sock struct, now we just copied it from the sock struct. After initialization, there is a dedicated pointer in skb_buff to ip_hdr, the member of the IP address of the Data Space header. At this time, we also initialize it.

 

Step 4: The pointer skips the IP header, points to the TCP Header (3), and then initializes the TCP Header of the data space. Some TCP headers are copied from the sock struct, some are assigned for the first time here. Note that the TCP Header does not contain the TCP option section. Therefore, you need to skip the TCP header and point to the TCP option section for initialization, TCP Initialization is complete (assign MSS to option) (4 ).

 

At this point, we have initialized the data space. At the beginning, we allocated a data space of 44 + 18. Now we are using 14 (MAC) + 20 (TCP) + 20 (IP) + 4 (MSS) is used up. From here, we can see that the data sent by the connect function is some local information, packaged to Mac, TCP, in structures such as IP addresses, the purpose is to inform the remote local information about some situations. In future communication, the information will be sent according to the current specifications, but the real data is not.

 

Finally, the network layer for data transmission is queued.

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.