1. 代码简介: Chan_sip.c是SIP协议(RFC3261)的实现代码,它没有实现对S/MIME, TCP and TLS的支持,对应的配置文件是sip.conf,代码所在的分组是:通道驱动类(channel_drivers)。 SIP通道处理各种类型的Sip sessions和dialogs(注意:并不是所有的dialogs都是“电话呼叫”),主要包括: * - Incoming calls that will be sent to the PBX core * - Outgoing calls, generated by the PBX * - SIP subscriptions and notifications of states and voicemail messages * - SIP registrations, both inbound and outbound * - SIP peer management (peerpoke, OPTIONS) * - SIP text messages
在SIP通道中,通常会有一列活跃的SIP dialogs,CLI下的命令sip show channels可以显示出大部分dialogs,除了订阅类的(它们可以用命令sip show subscriptions显示出来)。 CLI命令sip show channels的示例: debian120*CLI> sip show channels Peer User/ANR Call ID Seq (Tx/Rx) Form Hold Last Message 211.150.115.116 0132364499 51e8b037316 00102/00000 alaw No Init: INVITE 202.108.12.94 0000123456 76ad6e55-e0 00101/00001 alaw No Rx: ACK 211.150.115.116 0216252766 29df5b95633 00102/00000 alaw No Init: INVITE 202.108.12.94 0000123456 76ad6e55-2c 00101/00001 alaw No Rx: ACK 211.150.115.116 0137587006 720c5ecb32e 00102/00000 alaw No Tx: ACK 202.108.12.94 0000123456 76ad6e55-bf 00101/00001 alaw No Rx: ACK 211.150.115.116 0138797950 6d96c21a580 00102/00000 alaw No Tx: ACK 202.108.12.94 0000123456 76ad6e55-a5 00101/00001 alaw No Rx: ACK 211.150.115.116 0578708822 617679d2699 00102/00000 alaw No Tx: ACK 202.108.12.94 0000123456 76ad6e55-20 00101/00001 alaw No Rx: ACK 211.150.115.116 0512534057 6049a06e77d 00102/00000 alaw No Tx: ACK 202.108.12.94 0000123456 76ad6e55-b7 00101/00001 alaw No Rx: ACK 211.150.115.116 0132684273 4224f333507 00102/00000 alaw No Tx: ACK 202.108.12.94 0000123456 76ad6e55-95 00101/00001 alaw No Rx: ACK
2. 代码剖析:(注意:由于word自动更正某些代码中的首单词的首字母为大写,这儿可能与你在asterisk代码包中看到的代码不一致,请见谅) chan_sip模块注册了load_module()函数作为asterisk在加载本模块时的入口函数。 17818 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)", 17819 .load = load_module, 17820 .unload = unload_module, 17821 .reload = reload, 17822 ); load_module()函数读取配置文件sip.conf,并且注册一个通道驱动类型,即sip,具体见sip_tech中的结构内容。 17696 if(reload_config(sip_reloadreason)) /* Load the configuration from sip.conf */ 17697 return AST_MODULE_LOAD_DECLINE; 17698 17699 /* Make sure we can register our sip channel type */ 17700 if (ast_channel_register(&sip_tech)) { 17701 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'/n"); 17702 io_context_destroy(io); 17703 sched_context_destroy(sched); 17704 return AST_MODULE_LOAD_FAILURE; 17705 } Load_module最后调用restart_monitor()来启动sip监听。restart_monitor另外还有两处会被调用,在sip_request_call()和sip_reload()函数体内。 17735 /* And start the monitor for the first time */ 17736 restart_monitor(); restart_monitor调用pthread接口启动一个独立的监听线程,线程id记录在monitor_thread,线程入口函数是do_monitor()
15399 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) { 15400 ast_mutex_unlock(&monlock); 15401 ast_log(LOG_ERROR, "Unable to start monitor thread./n"); 15402 return -1; 15403 }
do_monitor()给SIP UDP socket添加事件处理器,sipsock_read负责读取socket收到的数据。 15233 /* Add an I/O event to our SIP UDP socket */ 15234 if (sipsock > -1) 15235 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL); do_monitor ()函数然后进入一个for()循环中,这个循环不断检测是否需要reload sip模块,并且遍历sip session列表检查是否有需要kill的session。它是怎么遍历的呢?原来是chan_sip维护了一个sip_pvt结构的列表,头指针保存在全局变量iflist中,通过sip_pvt的next域进行遍历。每个sip_pvt结构记录了一个session的全部信息。 变量t表示现在的时间,sip->lastrtptx表示上次发送rtp包的时间,如果两者之差大于keep alive间隔,则说明需要发送keep alive包了。 15272 if (sip->lastrtptx && 15273 ast_rtp_get_rtpkeepalive(sip->rtp) && 15274 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) { 15275 /* Need to send an empty RTP packet */ 15276 sip->lastrtptx = time(NULL); 15277 ast_rtp_sendcng(sip->rtp, 0); 15278 } 变量t表示现在的时间,sip->lastrtprx表示上次收到rtp包的时间,如果两者之差大于rpt的timeout间隔,则说明已经超时了。 这两个超时参数可以在sip.conf中配置,分别如下: rtptimeout=60 ;rtpholdtimeout=300 15279 if (sip->lastrtprx && 15280 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) && 15281 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) { 15282 /* Might be a timeout now -- see if we're on hold */ 此时再检测holdtimeout,并对channel上锁,ast_channel_trylock(sip->owner)。如果不是bridged channel,则调用soft hangup。 15301 /* Issue a softhangup */ 15302 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
相关的重要数据结构: sip_pvt: PVT structures are used for each SIP dialog, ie. a call, a registration, a subscribe sip_pvt这个结构维护了一个sip session的重要数据信息,关键字段如下: struct sip_pvt* next Next dialog in chain。指向链上的下一个sip_pvt结构 struct ast_channel* owner Who owns us (if we have an owner)。指向了拥有这个结构的通道的指针 struct sip_pkt* packets Packets scheduled for re-transmission。维护待重传的sip packet int pendinginvite Any pending invite ? (seqno of this)。如果有等待的邀请包,则在这里记下这个包序号 struct ast_rtp* rtp RTP Session,指向RTP Session的指针 int rtptimeout RTP timeout time, RTP的超时时间 struct sockaddr_in sa Our peer,对方的地址信息 char tag[11] Our tag for this session,比如:tag=965531f1-52721549 |
|
来自: icecity1306 > 《通信知识》