Description of linuxkernel2.4.54254socket Layer

Source: Internet
Author: User
Article Title: a brief explanation of linuxkernel2.4.54254socket layer. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. Create a socket
Original function:
Static int inet_create (struct socket * sock, int protocol)
In net/ipv4/af_inet.c
Detailed explanation
Static int inet_create (struct socket * sock, int protocol)
{
Struct sock * sk;
Struct proto * prot;
Sock-> state = SS_UNCONNECTED;/* set the status to not connected */
Sk = sk_alloc (PF_INET, GFP_KERNEL, 1);/* apply for memory required by sock */
/* Net/core/sock. c */
If (sk = NULL)
Goto do_oom;
Switch (sock-> type ){
Case SOCK_STREAM:/* TCP protocol */
If (protocol & protocol! = IPPROTO_TCP)
Goto free_and_noproto;
Protocol = IPPROTO_TCP;
Prot = & tcp_prot;/* tcp_prot defined in net/ipv4/tcp_ipv4.c */
Sock-> ops = & inet_stream_ops;/* socket operations on STREAM */
Break;
Case SOCK_SEQPACKET:/* not supported */
Goto free_and_badtype;
Case SOCK_DGRAM:/* UDP protocol */
If (protocol & protocol! = IPPROTO_UDP)
Goto free_and_noproto;
Protocol = IPPROTO_UDP;
Sk-> no_check = UDP_CSUM_DEFAULT;
Prot = & udp_prot;/* udp_prot defined in net/ipv4/udp. c */
Sock-> ops = & inet_dgram_ops;/* socket operation on DGRAM */
Break;
Case SOCK_RAW:/* RAW */
If (! Capable (CAP_NET_RAW)/* determine whether there is a right to establish SOCK_RAW */
Goto free_and_badperm;
If (! Protocol)/* protocol cannot be 0 */
Goto free_and_noproto;
Prot = & raw_prot;/* raw_prot defined in net/ipv4/raw. c */
Sk-> reuse = 1;/* allow address reuse */
Sk-> num = protocol;
Sock-> ops = & inet_dgram_ops;/* some features of raw are the same as those of DGRAM */
If (protocol = IPPROTO_RAW)
Sk-> protinfo. af_inet.hdrincl = 1;
/* Allow custom ip headers */
Break;
Default:
Goto free_and_badtype;
}
If (ipv4_config.no_pmtu_disc)
Sk-> protinfo. af_inet.pmtudisc = IP_PMTUDISC_DONT;
Else
Sk-> protinfo. af_inet.pmtudisc = IP_PMTUDISC_WANT;
Sk-> protinfo. af_inet.id = 0;
Sock_init_data (sock, sk);/* initialize some data */
/* Net/core/sock. c */
Sk-> destruct = inet_sock_destruct;/* Call inet_sock_destruct when the socket is destroyed */
Sk-> zapped = 0;
Sk-> family = PF_INET;
Sk-> protocol = protocol;
Sk-> prot = prot;
Sk-> backlog_rcv = prot-> backlog_rcv;/* prot-> backlog_rcv () see definitions of various types */
Sk-> protinfo. af_inet.ttl = sysctl_ip_default_ttl;/* set the default ttl */
/* Modify/proc/sys/net/ipv4/ip_default_ttl */
Sk-> protinfo. af_inet.mc_loop = 1;
Sk-> protinfo. af_inet.mc_ttl = 1;
Sk-> protinfo. af_inet.mc_index = 0;
Sk-> protinfo. af_inet.mc_list = NULL;
# Ifdef INET_REFCNT_DEBUG
Atomic_inc (& inet_sock_nr );
# Endif
If (sk-> num ){
/* It assumes that any protocol which allows
* The user to assign a number at socket
* Creation time automatically
* Shares.
*/
Sk-> sport = htons (sk-> num);/* set the local port */
/* Add to protocol hash chains .*/
Sk-> prot-> hash (sk );
}
If (sk-> prot-> init ){
Int err = sk-> prot-> init (sk);/* initialize socket */
If (err! = 0 ){
Inet_sock_release (sk );
Return (err );
}
}
Return (0 );
Free_and_badtype:
Sk_free (sk);/* release memory */
Return-ESOCKTNOSUPPORT;
Free_and_badperm:
Sk_free (sk );
Return-EPERM;
Free_and_noproto:
Sk_free (sk );
Return-EPROTONOSUPPORT;
Do_oom:
Return-ENOBUFS;
}
In net/core/sock. c
Void sock_init_data (struct socket * sock, struct sock * sk)
{
Skb_queue_head_init (& sk-> receive_queue);/* initialize three queues for receiving, sending, and Error */
Skb_queue_head_init (& sk-> write_queue );
Skb_queue_head_init (& sk-> error_queue );
Init_timer (& sk-> timer);/* initialize timer */
  
Sk-> allocation = GFP_KERNEL;
Sk-> rcvbuf = sysctl_rmem_default;
Sk-> sndbuf = sysctl_wmem_default;
Sk-> state = TCP_CLOSE;
Sk-> zapped = 1;
Sk-> socket = sock;
If (sock)
{
Sk-> type = sock-> type;
Sk-> sleep = & sock-> wait;
Sock-> sk = sk;
} Else
Sk-> sleep = NULL;
Sk-> dst_lock = RW_LOCK_UNLOCKED;
Sk-> callback_lock = RW_LOCK_UNLOCKED;
/* Sock_def_wakeup (), sock_def_readable (),
Sock_def_write_space (), sock_def_error_report (),
Sock_def_destruct () in net/core/sock. c */
Sk-> state_change = sock_def_wakeup;
Sk-> data_ready = sock_def_readable;
Sk-> write_space = sock_def_write_space;
Sk-> error_report = sock_def_error_report;
Sk-> destruct = sock_def_destruct;
Sk-> peercred. pid = 0;
Sk-> peercred. uid =-1;
Sk-> peercred. gid =-1;
Sk-> rcvlowat = 1;
Sk-> rcvtimeo = MAX_SCHEDULE_TIMEOUT;/* set accept and send timeout */
Sk-> sndtimeo = MAX_SCHEDULE_TIMEOUT;
Atomic_set (& sk-> refcnt, 1 );
}
1.1 SOCK_STREAM Initialization
In net/ipv4/tcp_ipv4.c
Static int tcp_v4_init_sock (struct sock * sk)
{
Struct tcp_opt * tp = & (sk-> tp_pinfo.af_tcp );
Skb_queue_head_init (& tp-> out_of_order_queue );
Tcp_init_xmit_timers (sk );
Tcp_prequeue_init (tp );
Tp-> rto = TCP_TIMEOUT_INIT;
Tp-> mdev = TCP_TIMEOUT_INIT;
     
/* So many TCP implementations out there (incorrectly) count
* Initial SYN frame in their delayed-ACK and congestion control
* Algorithms that we must have the following bandaid to talk
* Efficiently to them.-DaveM
*/
Tp-> snd_cwnd = 2;
/* See draft-stevens-tcpca-spec-01 for discussion of
* Initialization of these values.
*/
Tp-> snd_ssthresh = 0x7fffffff;/* Infinity */
Tp-> snd_cwnd_clamp = ~ 0;
Tp-> mss_cache = 536;
Tp-> reordering = sysctl_tcp_reordering;
Sk-> state = TCP_CLOSE;
Sk-> write_space = tcp_write_space;/* tcp_write_space () in net/ipv4/tcp. c */
Sk-> use_write_queue = 1;
Sk-> tp_pinfo.af_tcp.af_specific = & apos;
/* Ipv4_specific in net/ipv4/tcp_ipv4.c */
Sk-> sndbuf = sysctl_tcp_wmem [1];/* set the buffer size for sending and receiving */
Sk-> rcvbuf = sysctl_tcp_rmem [1];/* sysctl_tcp _ * in net/ipv4/tcp. c */
Atomic_inc (& tcp_sockets_allocated);/* tcp_sockets_allocated indicates the current number of TCP sockets */
Return 0;
}
SOCK_DGRAM is not initialized
1.2 SOCK_RAW Initialization
In net/ipv4/raw. c
Static int raw_init (struct sock * sk)
{
Struct raw_opt * tp = & (sk-> tp_pinfo.tp_raw4 );
If (sk-> num = IPPROTO_ICMP)
Memset (& tp-> filter, 0, sizeof (tp-> filter ));
Return 0;
}
2. Server
2.1 bind
Static int inet_bind (struct socket * sock, struct sockaddr * uaddr, int addr_len)
{
Struct sockaddr_in * addr = (struct sockaddr_in *) uaddr;
Struct sock * sk = sock-> sk;
Unsigned short snum;
Int chk_addr_ret;
Int err;
/* If the socket has its own bind function then use it. (RAW )*/
If (sk-> prot-> bind)
Return sk-> prot-> bind (sk, uaddr, addr_len );
/* Only SOCK_RAW defines its own bind function */
If (addr_len <sizeof (struct sockaddr_in ))
Return-EINVAL;
Chk_addr_ret = inet_addr_type (addr-> sin_addr.s_addr );
/* Inet_addr_type: Type of the returned address */
/* In net/ipv4/fib_frontend.c */
/* Not specified by any standard per-se, however it breaks too
* Applications when removed. It is unfortunate since
* Allowing applications to make a non-local bind solv
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.