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

Socket的实现(Linux内核部分)

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

asmlinkage  long  sys_socket( int  family,  int  type,  int  protocol)  
{  
...............................................  
///主要是两个函数,一个是创建socket   
    retval = sock_create(family, type, protocol, &sock);  
    if  (retval <  0 )  
        goto  out;  
///这个是相应的文件系统的操作.   
    retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));  
....................................  
}  

sock_create的具体流程我们就不分析了,我们只需要知道最终他会通过传递进来的family的值,来取得相应的family中注册的 creat函数.然后会调用这个函数来完成socket的创建.而在上面的代码分析中,我们知道在af_inet中,注册的create函数是 inet_create函数,因此我们来看这个函数的实现:

static   int  inet_create(struct net *net, struct socket *sock,  int  protocol)  
{  
    struct sock *sk;  
    struct inet_protosw *answer;  
    struct inet_sock *inet;  
    struct proto *answer_prot;  
    unsigned char  answer_flags;  
    char  answer_no_check;  
    int  try_loading_module =  0 ;  
    int  err;  
...........................................................  
///首先给socket状态赋值.   
  
    sock->state = SS_UNCONNECTED;  
  
    /* Look for the requested type/protocol pair. */   
lookup_protocol:  
    err = -ESOCKTNOSUPPORT;  
    rcu_read_lock();  
///通过type和protocl的值,来查找到相应的inet_protosw结构.   
    list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {  
  
        err = 0 ;  
        /* Check the non-wild match. */   
        if  (protocol == answer->protocol) {  
            if  (protocol != IPPROTO_IP)  
                break ;  
        } else  {  
            /* Check for the two wild cases. */   
            if  (IPPROTO_IP == protocol) {  
                protocol = answer->protocol;  
                break ;  
            }  
            if  (IPPROTO_IP == answer->protocol)  
                break ;  
        }  
        err = -EPROTONOSUPPORT;  
    }  
  
    ..........................................  
  
///开始给socket赋值.这里我们可以看到最终socket的ops域所得到的值就是相应的协议簇的操作集合 (比如inet_stream_ops这些)..   
    sock->ops = answer->ops;  
    answer_prot = answer->prot;  
    answer_no_check = answer->no_check;  
    answer_flags = answer->flags;  
    rcu_read_unlock();  
  
    WARN_ON(answer_prot->slab == NULL);  
  
    err = -ENOBUFS;  
///alloc一个sock结构,其中将刚才取得的inet_protosw中的pro 域赋值给sock的 sk_prot域和sk_prot_creator.以及family域也被相应的赋值.   
    sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);  
    if  (sk == NULL)  
        goto  out;  
....................................................................  
  
///这个函数中会初始化相应的socket中的写队列,读队列以及错误队列.并将sk指针和sock连接起来.而且还将初始化相应的定时器.   
  
    sock_init_data(sock, sk);  
..........................................  
///调用相应的初始化.   
  
    if  (sk->sk_prot->init) {  
///其实也就是相对应4层协议的初始化函数,它会初始化一些协议相关的东西.   
        err = sk->sk_prot->init(sk);  
        if  (err)  
            sk_common_release(sk);  
    }  
out:  
    return  err;  
out_rcu_unlock:  
    rcu_read_unlock();  
    goto  out;  
}  

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

       

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