Basic structure of socket

xiaoxiao2021-03-06  19

The basic structure of the socket ================ (1) socket () is used to create a socket file in the socket file system (SOCKFS). SLR file The operation table is socket_file_ops, which intersects the file socket operation table (Proto_ops) interacts with each other, and the file socket operation table is interspersed with the network socket operation table (PROTO) interaction. (2) file socket with Socket structure description Network socket describes the SOCK structure. The file socket exists in the inode's u.socket_i unit and points to the corresponding network socket. (3) The protocol of the socket determines the creation of the socket Operation, the type of socket determines the operation of the file socket, the protocol of the socket determines the operation of the network socket. For the PF_INET protocol, the Sock_Stream type file socket operation table is INET_STREAM_OPS Network socket operation table is the TCP_PROT.SOCK_DGRAM type file socket operation table is INET_DGRAM_OPS, the network socket operation table is the UDP_PROT.SOCK_RAW type file socket operation table is INET_DGRAM_OPS, the network socket operation table is Raw_prot.

net / ipv4 / af_inet.c: struct net_proto_family inet_family_ops = {PF_INET, inet_create}; static int inet_create (struct socket * sock, int protocol) {struct sock * sk; struct proto * prot; sock-> state = SS_UNCONNECTED; sk = sk_alloc (PF_INET, GFP_KERNEL, 1); if (sk == NULL) goto do_oom; switch (sock-> type) {case SOCK_STREAM: if (! protocol && protocol = IPPROTO_TCP) goto free_and_noproto; protocol = IPPROTO_TCP; prot = & tcp_prot; sock-> ops = & inet_stream_ops; break; case SOCK_SEQPACKET: goto free_and_badtype; case SOCK_DGRAM: if (! protocol && protocol = IPPROTO_UDP) goto free_and_noproto; protocol = IPPROTO_UDP; sk-> no_check = UDP_CSUM_DEFAULT; prot = & udp_prot; sock-> ops = & inet_dgram_ops; break; case SOCK_RAW:; (! protocol) if (! capable (CAP_NET_RAW)) goto free_and_badperm if goto free_and_noproto; prot = & raw_prot; sk-> reuse = 1; sk-> num = protocol; sock-> ops = & inet_dgram_ops ; if (protocol == ipproto_raw) SK-> Protinfo.af_INET.HDRINCL = 1; created IP Baotou Break by the user; 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; sock_init_data (sock, sk); sk-> destruct = inet_sock_destruct; sk-> zapped = 0; SK-> Family = PF_INET; SK-> prot = protocol; sk-> prot = prot; sk-> proG_rcv = prot-> backlog_rcv; sk-> protinfo.af_Inet.ttl = sysctl_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); / * Add to protocol hash chains. * / SK-> prot-> hash (sk);} if (SK-> prot-> init) {int err = SK-> prot-> INIT (SK); if (Err! = 0) {inet_sock_release (SK); Return (Err);}} return (0); free_and_badtype: SK_Free (SK); Return -esockTnosupport; free_and_badperm: SK_Free (SK); Return - Eperm; free_and_noproto: SK_Free (SK); Return -EProtonosupport; do_oom: return.com;}; net / core / sock.c: #define sock_lock_init (__ SK) / do {spin_lock_init (& ((__ SK) -> Lock.slock )); / (__sk) -> Lock.users = 0; / init_waitqueue_head (& (__ SK) -> Lock.wq); /} while (0); struct sock * sk_alloc (int home, int priority, int zero_it) {struct sock * sk = kmem_cache_alloc (sk_cachep, priority); if (sk && zero_it) {memset (sk, 0, sizeof (struct sock)); sk-> family = family; sock_lock_init (sk);} return sk } void sock_ini t_data (struct socket * sock, struct sock * sk) {skb_queue_head_init (& sk-> receive_queue); skb_queue_head_init (& sk-> write_queue); skb_queue_head_init (& sk-> error_queue); init_timer (& sk-> 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; SK->

转载请注明原文地址:https://www.9cbs.com/read-39826.html

New Post(0)