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

Socket的实现(Linux内核部分)

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

还有两个一个是fs_struct,一个是namespace也都是进程相关的.这里就不一一介绍了.

我这里vfs介绍只是个大概,需要详细了解的,可以去看ulk的vfs相关章节和linux内核设计与实现的相关章节.

因此下面的图表示了进程和socket的关系:


还有两个一个是fs_struct,一个是namespace也都是进程相关的.这里就不一一介绍了.

我这里vfs介绍只是个大概,需要详细了解的,可以去看ulk的vfs相关章节和linux内核设计与实现的相关章节.

因此下面的图表示了进程和socket的关系:

   1. /**
   2.  *  struct socket - general BSD socket
   3.  *  @state: socket state (%SS_CONNECTED, etc)
   4.  *  @type: socket type (%SOCK_STREAM, etc)
   5.  *  @flags: socket flags (%SOCK_ASYNC_NOSPACE, etc)
   6.  *  @ops: protocol specific socket operations
   7.  *  @fasync_list: Asynchronous wake up list
   8.  *  @file: File back pointer for gc
   9.  *  @sk: internal networking protocol agnostic socket representation
  10.  *  @wait: wait queue for several uses
  11.  */ 
  12. struct socket { 
  13.     socket_state        state; 
  14.     short           type; 
  15.     unsigned long       flags; 
  16.     const struct proto_ops  *ops; 
  17.     struct fasync_struct    *fasync_list; 
  18.     struct file     *file; 
  19.     struct sock     *sk; 
  20.     wait_queue_head_t   wait; 
  21. }; 
  22.  
  23. struct sock_common { 
  24.     unsigned short      skc_family; 
  25.     volatile unsigned char  skc_state; 
  26.     unsigned char       skc_reuse; 
  27.     int         skc_bound_dev_if; 
  28.     struct hlist_node   skc_node; 
  29.     struct hlist_node   skc_bind_node; 
  30.     atomic_t        skc_refcnt; 
  31.     unsigned int        skc_hash; 
  32.     struct proto        *skc_prot; 
  33. #ifdef CONFIG_NET_NS 
  34.     struct net      *skc_net; 
  35. #endif 
  36. }; 
  37.  
  38. struct proto_ops { 
  39.     int     family; 
  40.     struct module   *owner; 
  41.     int     (*release)   (struct socket *sock); 
  42.     int     (*bind)      (struct socket *sock, 
  43.                       struct sockaddr *myaddr, 
  44.                       int sockaddr_len); 
  45.     int     (*connect)   (struct socket *sock, 
  46.                       struct sockaddr *vaddr, 
  47.                       int sockaddr_len, int flags); 
  48. ................................................... 
  49. }; 

然后我们来看sock_init的实现,在这个函数中,将socket注册为一个伪文件系统,并安装相应的mount点:

   1. ///相应的mount对象 
   2. static struct vfsmount *sock_mnt __read_mostly; 
   3. ///文件系统对象. 
   4. static struct file_system_type sock_fs_type = { 
   5.     .name =     "sockfs", 
   6.     .get_sb =   sockfs_get_sb, 
   7.     .kill_sb =  kill_anon_super, 
   8. }; 
   9.  
  10. static int __init sock_init(void) 
  11. { 
  12.     /*
  13.      *      Initialize sock SLAB cache.
  14.      */ 
  15.  
  16.     sk_init(); 
  17.  
  18.     /*
  19.      *      Initialize skbuff SLAB cache
  20.      */ 
  21.     skb_init(); 
  22.  
  23. ///初始化一个inodecache. 
  24.     init_inodecache(); 
  25. ///注册文件系统到内核. 
  26.     register_filesystem(&sock_fs_type); 
  27. ///安装mount点. 
  28.     sock_mnt = kern_mount(&sock_fs_type); 
  29.  
  30. #ifdef CONFIG_NETFILTER 
  31.     netfilter_init(); 
  32. #endif 
  33.     return 0; 
  34. } 

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

       

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