Linux Listen call detailed __linux

Source: Internet
Author: User
Tags function prototype

The listen system allows a TCP socket on one host to be passively listening on a port number, waiting for a connection request from a TCP socket from another host, and the following is the function prototype of the LISTEN system call:

#include int listen (int s, int backlog);

1 backlog is the length of the listening queue, in the kernel function, first check the backlog, if greater than 128, then force it to equal 128.

2) Next check the structure body struct sock member Sk_state, that is, the current socket state, if not tcp_listen, start port listening.

The boot port listens first to the member Icsk_accept_queue allocating memory for the structure body struct inet_connection_sock (which is an extension of Struc sock representing a connection-oriented socket), Icsk_accept_ The type of queue is struct request_sock_queue,

The definition is as follows:

struct Request_sock_queue

{

struct Request_sock *rskq_accept_head;

struct Request_sock *rskq_accept_tail;

rwlock_t Syn_wait_lock;

U8 rskq_defer_accept;

struct Listen_sock *listen_opt;

};

When a TCP socket is listening, a connection request from a TCP socket from another host is accepted (three handshake agreements are completed), and a request_sock is established to establish a TCP connection to the request socket. The Request_sock is placed in a first-in, first-out queue and waits for accept system calls to be processed. But there seems to be no place to store request_sock in the structure above,

The following is the definition of the structure struct Listen_sock:

struct Listen_sock

{

U8 Max_qlen_log;

int Qlen;

int Qlen_young;

int Clock_hand;

U32 hash_rnd;

U32 nr_table_entries;

struct Request_sock *syn_table[0];

};

The newly established request_sock are stored in the syn_table. This is a hash array with a total of nr_table_entries entries. In fact, when allocating memory, the allocated size is the TCP_SYNQ_HSIZE (512) item. The value of the member Nr_table_entries is 512. The member Max_qlen_log represents the maximum value of the Request_sock queue in the form of a logarithm of 2. The hash table has 512 items, but the maximum value for the queue is 1024. That is, the value of Max_qlen_log is 10. Qlen is the current length of the queue. Hash_rnd is a random number that computes the hash value, and Rskq_accept_head and rskq_accept_tail in the struct struct request_sock_queue point to the queue header and the end of the queue for the Request_sock queue, respectively. After allocating memory for the struct inet_connection_sock, continue processing, the structure body struct sock has two members Sk_ack_backlog and Sk_max_ack_backlog. Sk_ack_backlog represents the number of sockets that are currently connected to the socket on the listening socket, but have not completed the three handshake protocol, that is, the number of sockets that are still in the connection process. The initial value is 0,sk_max_ack_backlog to the maximum of the number, that is, the second parameter of the Listen system call, the length of the listening queue, which is the true meaning of the maximum number of concurrent connection requests that the socket can handle, with a maximum value of 128.

Here, change the state of the socket to Tcp_listen and enter the listening state. Then the exclusive port allows the socket to enter the Listening_hash table in the Mytcp_hashinfo hash table set. Listening for establishment completion. Since the listening socket is always listening on the system, at the end of the process, you must also explicitly end the interception and perform cleanup work accordingly.

Related Article

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.