手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Socket的实现(Linux内核部分)

[日期:2010-08-23] 来源:Linux社区  作者:Linux [字体:大 中 小]

接下来来分析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的实现.

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款