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

Linux内核--网络协议栈深入分析(四)--套接字内核初始化和创建过程

[日期:2012-09-17] 来源:csdn  作者:闫明 [字体:大 中 小]
上面函数中的inetsw_array的定义中有四个元素:
  1. static struct inet_protosw inetsw_array[] =  
  2. {  
  3.     {  
  4.         .type =       SOCK_STREAM,  
  5.         .protocol =   IPPROTO_TCP,  
  6.         .prot =       &tcp_prot,  
  7.         .ops =        &inet_stream_ops,  
  8.         .no_check =   0,  
  9.         .flags =      INET_PROTOSW_PERMANENT |  
  10.                   INET_PROTOSW_ICSK,  
  11.     },  
  12.   
  13.     {  
  14.         .type =       SOCK_DGRAM,  
  15.         .protocol =   IPPROTO_UDP,  
  16.         .prot =       &udp_prot,  
  17.         .ops =        &inet_dgram_ops,  
  18.         .no_check =   UDP_CSUM_DEFAULT,  
  19.         .flags =      INET_PROTOSW_PERMANENT,  
  20.        },  
  21.   
  22.        {  
  23.         .type =       SOCK_DGRAM,  
  24.         .protocol =   IPPROTO_ICMP,  
  25.         .prot =       &ping_prot,  
  26.         .ops =        &inet_dgram_ops,  
  27.         .no_check =   UDP_CSUM_DEFAULT,  
  28.         .flags =      INET_PROTOSW_REUSE,  
  29.        },  
  30.   
  31.        {  
  32.            .type =       SOCK_RAW,  
  33.            .protocol =   IPPROTO_IP,    /* wild card */  
  34.            .prot =       &raw_prot,  
  35.            .ops =        &inet_sockraw_ops,  
  36.            .no_check =   UDP_CSUM_DEFAULT,  
  37.            .flags =      INET_PROTOSW_REUSE,  
  38.        }  
  39. };  

上面的函数会将这个数组中的元素按照type为索引注册到inetsw指针数组中。

函数2中调用的sock_register函数就是想协议族数组net_families中添加inet协议族的net_proto_family的数据定义,主要是协议族的创建方法inet_create下面是它的实现

  1. int sock_register(const struct net_proto_family *ops)  
  2. {  
  3.     int err;  
  4.   
  5.     if (ops->family >= NPROTO) {  
  6.         printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,  
  7.                NPROTO);  
  8.         return -ENOBUFS;  
  9.     }  
  10.   
  11.     spin_lock(&net_family_lock);  
  12.     if (rcu_dereference_protected(net_families[ops->family],  
  13.                       lockdep_is_held(&net_family_lock)))  
  14.         err = -EEXIST;  
  15.     else {  
  16.         RCU_INIT_POINTER(net_families[ops->family], ops);//这里就相当于将ops赋予net_families[ops->families]   
  17.         err = 0;  
  18.     }  
  19.     spin_unlock(&net_family_lock);  
  20.   
  21.     printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);  
  22.     return err;  
  23. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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