linphone用户注册与sip交互过程分析

linphone用户注册与sip交互过程分析

在linphone_core_init
在linphone_configuring_terminated
在linphone_core_start
在 sip_config_read
在linphone_core_set_sip_transports
在_linphone_core_apply_transports
在sal_listen_port
在sal_add_listen_port
在 belle_sip_stack_create_listening_point
在belle_sip_udp_listening_point_new
在belle_sip_udp_listening_point_init
在belle_sip_udp_listening_point_init_socket
在create_udp_socket
实现随机端口原理是 将udp的端口号设为-1,这样处理的时候会随机赋值,具体处理在create_udp_socket函数中 原理是,bind时使用端口号为0,这样会随机绑定一个未占用的端口,然后通过getsockname 获取具体绑定的端口返回

linphone监听端口是在linphone_core_init函数中就已经处理完成的,在这个函数中会先读取文本配置,然后根据配置再进行处理

使用belle_sip_main_loop_add_source 将lp->source(携带了监听的socket)加入到ml->fd_sources链表中,该结构最终在linphonecore.c文件 linphone_core_iterate函数下sal_iterate(lc->sal)中调用,跟代码可以发现前面监听的socket是在lc->sal->prov->lp 下,
sal_iterate最终执行代码如下:
void belle_sip_main_loop_run(belle_sip_main_loop_t *ml){
if (ml->in_loop){
belle_sip_warning(“belle_sip_main_loop_run(): reentrancy detected, doing nothing”);
return;
}
ml->run = TRUE;
ml->in_loop = TRUE;
while(ml->run){
belle_sip_main_loop_iterate(ml);
}
ml->in_loop = FALSE;
}

其中belle_sip_main_loop_iterate(ml)中对于监听的socket 采用了eventselect模型进行io处理
当有read write事件时,会调用s->notify 指针绑定的方法,udp的话就是udp_listeningpoint.c 文件下的 on_udp_data方法;

->on_udp_data
->belle_sip_channel_process_data (在 make_logbuf 会将接受的sip信息用char*然后输出log)
->belle_sip_channel_recv
在这里如果是udp端口对应处理结构是
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_udp_channel_t)
{
{
BELLE_SIP_VPTR_INIT(belle_sip_udp_channel_t,belle_sip_channel_t,FALSE),
(belle_sip_object_destroy_t)udp_channel_uninit,
NULL,
NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
“UDP”,
0, /is_reliable/
udp_channel_connect,
udp_channel_send,
udp_channel_recv,
NULL /no close method/
}

即udp_channel_recv

接收完数据后转到
->belle_sip_channel_process_stream(obj,FALSE);
->belle_sip_channel_parse_stream 在其中会通过查\r\n\r\n的结尾符号 获取整个sip文本部分

后面处理request
siplistener.c process_request_event
sal_impl.c process_request_event
sal_op_call.c process_request_event

关于监听端口 跟踪

关键函数:
sal_register

_sal_op_send_request_with_contact
方法中 next_hop_uri保存了sip服务器ip和端口
op->base.root->prov 保存了本地使用的ip和端口

belle_sip_client_transaction_send_request_to

关键结构
salop->base.root->prov 保存了端口相关信息

listeningpoint.c文件
int belle_sip_listening_point_get_well_known_port(const char *transport){
if (strcasecmp(transport,”UDP”)==0 || strcasecmp(transport,”TCP”)==0 ) return 5060;
if (strcasecmp(transport,”DTLS”)==0 || strcasecmp(transport,”TLS”)==0 ) return 5061;
belle_sip_error(“No well known port for transport %s”, transport);
return -1;
}

你可能感兴趣的:(linphone)