深入浅出asterisk(二):chan_sip代码分析(上)

简介:

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




本文转自einyboy博客园博客,原文链接:http://www.cnblogs.com/einyboy/archive/2012/10/23/2735253.html,如需转载请自行联系原作者。

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
监控 安全 前端开发
Onvif开发笔记(二): 使用gSOAP编译Onvif协议之编译Onvif代码框架
Onvif开发笔记(二): 使用gSOAP编译Onvif协议之编译Onvif代码框架
Onvif开发笔记(二): 使用gSOAP编译Onvif协议之编译Onvif代码框架
|
2月前
|
网络协议 编译器 调度
【Qt 面试题】深入剖析QT TCP通讯流程及应用实例
【Qt 面试题】深入剖析QT TCP通讯流程及应用实例
33 0
|
4月前
|
API 数据安全/隐私保护
jrtplib开源库系列之三:jrtplib发送接收数据流程
前面2篇文章主要说明了如何安装jrtplib库,以及对example1进行了说明,这篇文章主要说下jrtplib库数据的收发流程。
86 0
|
Python
x64dbg 实现插件Socket反向通信
编写一个带有socket通信功能的插件,x64dbg运行后,用户点击链接按钮可直接连接到外部的python中,python作为服务端,当x64dbg内部出现某个事件后,自动将消息推送到外部python脚本上,实现反向传参的目的。
141 0
x64dbg 实现插件Socket反向通信
|
JavaScript 前端开发 Java
Objective-C协议(protocol)和委托(delegate)的基本概念(★firecat推荐★)
Objective-C协议(protocol)和委托(delegate)的基本概念(★firecat推荐★)
177 0

热门文章

最新文章