接下来来分析inet_init的源码.
///表示了所有的可能的当前协议簇和套接口类型的组合.
static struct inet_protosw inetsw_array[] =
{
{
.type = SOCK_STREAM,
.protocol = IPPROTO_TCP,
.prot = &tcp_prot,
.ops = &inet_stream_ops,
.capability = -1 ,
.no_check = 0 ,
.flags = INET_PROTOSW_PERMANENT |
INET_PROTOSW_ICSK,
},
{
.type = SOCK_DGRAM,
.protocol = IPPROTO_UDP,
.prot = &udp_prot,
.ops = &inet_dgram_ops,
.capability = -1 ,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_PERMANENT,
},
{
.type = SOCK_RAW,
.protocol = IPPROTO_IP, /* wild card */
.prot = &raw_prot,
.ops = &inet_sockraw_ops,
.capability = CAP_NET_RAW,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_REUSE,
}
};
///协议簇的创建函数.
static struct net_proto_family inet_family_ops = {
.family = PF_INET,
.create = inet_create,
.owner = THIS_MODULE,
};
static int __init inet_init( void )
{
.............................................
///注册相应的proto到全局链表中.
rc = proto_register(&tcp_prot, 1 );
if (rc)
goto out;
rc = proto_register(&udp_prot, 1 );
if (rc)
goto out_unregister_tcp_proto;
rc = proto_register(&raw_prot, 1 );
if (rc)
goto out_unregister_udp_proto;
///注册协议簇的操作函数(后面socket创建的时候会用到).
(void )sock_register(&inet_family_ops);
.............................................
/* Register the socket-side information for inet_create. */
for (r = &inetsw[ 0 ]; r < &inetsw[SOCK_MAX]; ++r)
INIT_LIST_HEAD(r);
///将inetsw_array插入到相应的数组链表.
for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
inet_register_protosw(q);
...........................................
}
///表示了所有的可能的当前协议簇和套接口类型的组合.
static struct inet_protosw inetsw_array[] =
{
{
.type = SOCK_STREAM,
.protocol = IPPROTO_TCP,
.prot = &tcp_prot,
.ops = &inet_stream_ops,
.capability = -1,
.no_check = 0,
.flags = INET_PROTOSW_PERMANENT |
INET_PROTOSW_ICSK,
},
{
.type = SOCK_DGRAM,
.protocol = IPPROTO_UDP,
.prot = &udp_prot,
.ops = &inet_dgram_ops,
.capability = -1,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_PERMANENT,
},
{
.type = SOCK_RAW,
.protocol = IPPROTO_IP, /* wild card */
.prot = &raw_prot,
.ops = &inet_sockraw_ops,
.capability = CAP_NET_RAW,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_REUSE,
}
};
///协议簇的创建函数.
static struct net_proto_family inet_family_ops = {
.family = PF_INET,
.create = inet_create,
.owner = THIS_MODULE,
};
static int __init inet_init(void)
{
.............................................
///注册相应的proto到全局链表中.
rc = proto_register(&tcp_prot, 1);
if (rc)
goto out;
rc = proto_register(&udp_prot, 1);
if (rc)
goto out_unregister_tcp_proto;
rc = proto_register(&raw_prot, 1);
if (rc)
goto out_unregister_udp_proto;
///注册协议簇的操作函数(后面socket创建的时候会用到).
(void)sock_register(&inet_family_ops);
.............................................
/* Register the socket-side information for inet_create. */
for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
INIT_LIST_HEAD(r);
///将inetsw_array插入到相应的数组链表.
for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
inet_register_protosw(q);
...........................................
}
接下来我们来通过分析创建socket的函数sys_socket,来更加好的理解socket的实现.
