linphone之sip分析

static int belle_sip_udp_listening_point_init_socket(belle_sip_udp_listening_point_t *lp){
    int port=belle_sip_uri_get_listening_port(((belle_sip_listening_point_t*)lp)->listening_uri);
    lp->sock=create_udp_socket(belle_sip_uri_get_host(((belle_sip_listening_point_t*)lp)->listening_uri)
                    ,&port,&lp->base.ai_family);
    if (lp->sock==(belle_sip_socket_t)-1){
        return -1;
    }
    belle_sip_uri_set_port(((belle_sip_listening_point_t*)lp)->listening_uri,port);
    if (lp->base.stack->dscp)
        belle_sip_socket_set_dscp(lp->sock,lp->base.ai_family,lp->base.stack->dscp);
    lp->source=belle_sip_socket_source_new((belle_sip_source_func_t)on_udp_data,lp,lp->sock,BELLE_SIP_EVENT_READ,-1);
    belle_sip_main_loop_add_source(((belle_sip_listening_point_t*)lp)->stack->ml,lp->source);
    return 0;
}
/*peek data from the master socket to see where it comes from, and dispatch to matching channel.
 * If the channel does not exist, create it */
static int on_udp_data(belle_sip_udp_listening_point_t *lp, unsigned int events){
    int err;
    unsigned char buf[4096];
    struct sockaddr_storage addr;
    socklen_t addrlen=sizeof(addr);

    if (events & BELLE_SIP_EVENT_READ){
        belle_sip_debug("udp_listening_point: data to read.");
        err=recvfrom(lp->sock,(char*)buf,sizeof(buf),MSG_PEEK,(struct sockaddr*)&addr,&addrlen);
        if (err==-1){
            char *tmp=belle_sip_object_to_string((belle_sip_object_t*) ((belle_sip_listening_point_t*)lp)->listening_uri);
            belle_sip_error("udp_listening_point: recvfrom() failed on [%s], : [%s] reopening server socket"
                    ,tmp
                    ,belle_sip_get_socket_error_string());
            belle_sip_free(tmp);
            belle_sip_udp_listening_point_uninit(lp);
            /*clean all udp channels that are actually sharing the server socket with the listening points*/
            belle_sip_listening_point_clean_channels((belle_sip_listening_point_t*)lp);
            belle_sip_udp_listening_point_init_socket(lp);
        }else{
            belle_sip_channel_t *chan;
            struct addrinfo ai={0};
            /*preserve the V4 mapping*/
            ai.ai_family=addr.ss_family;
            ai.ai_addr=(struct sockaddr*)&addr;
            ai.ai_addrlen=addrlen;
            chan=_belle_sip_listening_point_get_channel((belle_sip_listening_point_t*)lp,NULL,&ai);
            if (chan==NULL){
                /*TODO: should rather create the channel with real local ip and port and not just 0.0.0.0"*/
                chan=belle_sip_channel_new_udp_with_addr(lp->base.stack
                                                        ,lp->sock
                                                        ,belle_sip_uri_get_host(lp->base.listening_uri)
                                                        ,belle_sip_uri_get_port(lp->base.listening_uri)
                                                        ,&ai);
                if (chan!=NULL){
                    belle_sip_message("udp_listening_point: new channel created to %s:%i",chan->peer_name,chan->peer_port);
                    belle_sip_listening_point_add_channel((belle_sip_listening_point_t*)lp,chan);
                }
            }
            if (chan){
                /*notify the channel*/
                /*belle_sip_debug("Notifying udp channel, local [%s:%i]  remote [%s:%i]"
                        ,chan->local_ip
                        ,chan->local_port
                        ,chan->peer_name
                        ,chan->peer_port);*/
                belle_sip_message("Notifying udp channel, local [%s:%i]  remote [%s:%i]"
                        ,chan->local_ip
                        ,chan->local_port
                        ,chan->peer_name
                        ,chan->peer_port);

                belle_sip_message("Knowchat: [1] belle_sip_channel_process_data");
                belle_sip_channel_process_data(chan,events);
            }
        }
    }
    return BELLE_SIP_CONTINUE;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值