linux内核代码-注释详解:inet_create

/*linux-5.10.x\net\ipv4\af_inet.c
 * 主要作用是分配和初始化一个新的网络套接字,并将其添加到系统的网络套接字表中。总结:
	套接字创建:					首先会调用 sock_create() 函数创建一个新的套接字实例,该函数返回一个指向 struct socket 结构体的指针,表示创建的套接字
	套接字类型和协议设置:			  	根据指定的协议类型,函数会设置套接字的类型和协议族。常见的协议族包括 IPv4(AF_INET)和 IPv6(AF_INET6)
	套接字表操作:					创建的套接字添加到套接字表中。调用 sk_alloc() 函数为套接字分配一个  sock 结构体,然后将其添加到当前网络命名空间的套接字表中
	协议处理:					调用适当的协议处理函数来处理特定协议类型的操作。
									例如 IPv4 协议,会调用 inet_sock_alloc() 和 inet_hashinfo_init() 来分配套接字并初始化相关的 IP 信息
	返回结果:					返回创建的套接字实例(struct socket 结构体的指针)作为结果
 */
static int inet_create(struct net *net, struct socket *sock, int protocol,
		       int kern)
{
   
	struct sock *sk;
	struct inet_protosw *answer;		//表示IPv4协议的处理函数
	struct inet_sock *inet;				//表示IPv4协议的socket私有数据结构
	struct proto *answer_prot;			//表示IPv4协议的协议控制块
	unsigned char answer_flags;			//表示IPv4协议的标志位
	int try_loading_module = 0;			//表示尝试加载模块的次数
	int err;

	if (protocol < 0 || protocol >= IPPROTO_MAX)					//检查指定的协议是否合法
		return -EINVAL;

	sock->state = SS_UNCONNECTED;									//将socket的状态设置为未连接状态

	/* Look for the requested type/protocol pair. */
lookup_protocol:													/*用于在查找协议时跳转到指定位置*/
	err = -ESOCKTNOSUPPORT;											//表示不支持指定的协议类型
	rcu_read_lock();
	list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
   	//遍历inetsw数组中指定类型的协议处理函数。inetsw数组存储了不同类型的协议处理函数的链表头

		err = 0;
		/* Check the non-wild match. */
		if (protocol == answer->protocol) {
   							//检查指定的协议是否与当前遍历到的协议处理函数的协议相匹配
			if (protocol 

你可能感兴趣的:(linux,网络)