In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to deeply understand the socket implementation of TCP/IP protocol, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Socket is known to be used for network communication, and it is also known that it is a combination of ip and port. But many students may not be very clear about the principle and implementation of socket. Let's take a closer look at what socket really is.
When we recall the steps of socket programming, the first function to call is socket, both on the client and on the server. Let's start with the implementation of this function and see what a socket really is.
/ / create a new socket structure and create a lower-level sock structure that is associated with each other
Static int sock_socket (int family, int type, int protocol)
{
Int i, fd
Struct socket * sock
Struct proto_ops * ops
/ / find the corresponding protocol family, such as Unix domain and ipv4
For (I = 0; I)
< NPROTO; ++i) { // 从props数组中找到family协议对应的操作函数集,props由系统初始化时sock_register进行操作 if (pops[i] == NULL) continue; if (pops[i]->Family = = family)
Break
}
If (I = = NPROTO)
{
Return-EINVAL
}
/ / function set
Ops = pops [I]
/ / check the type
If ((type! = SOCK_STREAM & & type! = SOCK_DGRAM & &
Type! = SOCK_SEQPACKET & & type! = SOCK_RAW & &
Type! = SOCK_PACKET) | | protocol
< 0) return(-EINVAL); // 分配一个新的socket结构体 if (!(sock = sock_alloc())) { ... } // 设置类型和操作函数集 sock->Type = type
Sock- > ops = ops
If ((I = sock- > ops- > create (sock, protocol))
< 0) { sock_release(sock); return(i); } // 返回一个新的文件描述符 if ((fd = get_fd(SOCK_INODE(sock))) < 0) { sock_release(sock); return(-EINVAL); } return(fd); } 我们从上到下,逐步分析这个过程。 1 根据传的协议类型,找到对应的函数集,因为不同的协议族他的底层操作是不一样的。 2 分配一个socket结构体。定义如下。我们大概了解一下字段就行。 struct socket { short type; /* SOCK_STREAM, ... */ socket_state state; long flags; struct proto_ops *ops; // 这个字段要记一下 void *data; struct socket *conn; struct socket *iconn; struct socket *next; struct wait_queue **wait; struct inode *inode; struct fasync_struct *fasync_list; }; struct socket *sock_alloc(void) { struct inode * inode; struct socket * sock; // 获取一个可用的inode节点 inode = get_empty_inode(); if (!inode) return NULL; // 初始化某些字段 inode->I_mode = S_IFSOCK
Inode- > i_sock = 1 socket file
Inode- > i_uid = current- > uid
Inode- > i_gid = current- > gid
/ / point to the socket structure of inode, and initialize the socket structure of inode structure
Sock = & inode- > u.socket_i
Sock- > state = SS_UNCONNECTED
Sock- > flags = 0
Sock- > ops = NULL
Sock- > data = NULL
Sock- > conn = NULL
Sock- > iconn = NULL
Sock- > next = NULL
Sock- > wait = & inode- > i_wait
/ / quote each other
Sock- > inode = inode; / * "backlink": we could use pointer arithmetic instead * /
Sock- > fasync_list = NULL
/ / socket number plus one
Sockets_in_use++
/ / returns a new socket structure, which is mounted in inode
Return sock
}
Sock_alloc first allocates an inode,inode node with a socket structure, then initializes some fields of the socket structure and returns his address.
3 this is when we get a socket structure. Then call the create function (omitting some of the code).
/ / create a sock structure, which is associated with the socket structure
Static int inet_create (struct socket * sock, int protocol)
{
Struct sock * sk
Struct proto * prot
Int err
/ / assign a sock structure
Sk = (struct sock *) kmalloc (sizeof (* sk), GFP_KERNEL)
Switch (sock- > type)
{
Case SOCK_STREAM:
Protocol = IPPROTO_TCP
/ / function set
Prot = & tcp_prot
Break
Case SOCK_DGRAM:
Protocol = IPPROTO_UDP
Prot=&udp_prot
Break
}
/ / the socket field of the sock structure points to the upper socket structure.
Sk- > socket = sock
/ / omit a bunch of initialization codes for sock structures
}
We found that when we created a socket, we applied for a socket structure as well as a sock structure. Why do you need two structures, and these two structures are related to each other? This is about the complexity of network protocols, and this design is linux's solution to this complexity. Let's look back at the parameters of the socket function.
Socket (int family, int type, int protocol)
Family is a protocol suite, such as Unix domain, ipv4, and ipv6,type are subcategories based on the first parameter. For example, there are tcp, udp, raw and packet under ipv4. Protocol is not useful for tcp and udp, but for raw and packet, it marks the upper layer protocol type. This is like a tree, starting from the root node, there are many branches. Socket structure is the top structure of the whole network protocol, and it is the first layer of abstraction. According to the different protocol suite, there are different implementation functions, and there are different subcategories under the same protocol suite, such as tcp, udp and so on under ipv4. The specific logic of different subcategories is also different. That is, the data structure and algorithm are different. So the socket structure has a data field, which is customized, and for the implementation of ipv4, it points to a sock structure, and for the implementation of the Unix domain, the unix_proto_data structure. This solves the problem of different implementations of different protocols (family). How to implement different subtypes under the same protocol suite? For example, tcp and udp under ipv4. The solution given by linux is to define a field in the sock structure that points to a different set of underlying protocol functions according to the value of the subtype type.
Insert a picture description here
After applying for the sock structure and associating it with the socket structure. At this point we get an inode, a socket structure, a sock structure. Then get a file and fd file descriptor based on inode. Finally, fd is returned to the user. The content structure is as follows.
Insert a picture description here
This is the memory structure returned by the socket function. Later, we call bind,listen and other functions, and pass in fd, and the system will find the tcp function set and execute the corresponding function according to the above figure. The same is true for udp, except that the tcp function set becomes the udp function set. On how to in-depth understanding of the TCP/IP protocol socket implementation to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.