深入浅出asterisk(三):chan_sip代码分析(下)

本文深入分析了Asterisk中处理SIP请求的流程,包括sipsock_read()函数解析SIP包并找到或创建dialog,handle_request()根据请求类型调用不同处理函数,如handle_request_invite()用于处理INVITE请求,涉及通道创建和 dial plan 执行。同时,文章提到了outbound calls的建立,挂断操作以及handle_request_bye()函数的简单介绍。
摘要由CSDN通过智能技术生成

现在回过头来把焦点转移到sipsock_read()函数。所有到来的sip包都在这里开始处理,在处理sip包期间,sipsock_read需要对sip的拥有者channel上锁,sipsock_read成功则返回0,失败则返回1。它解析sip包并且找到所在的dialog,或者创建新的dialog。并且把解析好的包交给handle_request()处理。

    sipsock_read第一步接收socket数据,存到结构sip_requestdata域中。

15062    res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
   
   

解析SIP包,获取sip request method,如INVITE, BYE

15086    parse_request(&req);
   
   
15087    req.method = find_sip_method(req.rlPart1);
   
   

    随后找到对应的sip_pvt结构,或者创建新的sip_pvt结构,结构指针返回到变量p中。

15099       /* Find the active SIP dialog or create a new one */
    
    
15100       p = find_call(&req, &sin, req.method); /* returns p locked */
    
    

   在进一步操作之前,需要对p->owner上锁,这个操作会最多尝试100次直至成功。

15107       /* Go ahead and lock the owner if it has one -- we may need it */
    
    
15108       /* becaues this is deadlock-prone, we need to try and unlock if failed */
    
    
15109       if (!p->owner || !ast_channel_trylock(p->owner))
   
   
15110          break;   /* locking succeeded */
    
    

如果上锁操作失败,将会返回503 sip消息。

15127       if (req.method != SIP_ACK)
   
   
15128          transmit_response(p, "503 Server error", &req); /* We must respond according to RFC 3261 sec 12.2 */
    
    
15129       /* XXX We could add retry-after to make sure they come back */
    
    
15130       append_history(p, "LockFail", "Owner lock failed, transaction failed.");
   
   
15131       return 1;
   
   

更深一步的解析处理操作交给handle_request()函数处理,完了之后就是释放channel的锁。

15134    if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
   
   
15135       /* Request failed */
    
    
15136       if (option_debug)
   
   
15137          ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s/n", p->callid[0] ? p->callid : "<no callid>");
   
   
15138    }
   
   
15139       
   
   
15140    if (p->owner && !nounlock)
   
   
15141       ast_channel_unlock(p->owner);
   
   

  

函数handle_request()视数据包的类型而处理,如果是对外出包的回应,则交给handle_response()处理,如果是一个请求包,则视请求类型(INVITE, OPTIONS, REFER, BYE, CANCEL etc)交给不同的函数处理。如果是一个INVITE包,则交给handle_request_invite()处理,在那里将会创建一个新的channel,这个通道随后会执行一个单独的通道线程。这就是一个来电呼叫。如果这个呼叫被应答,则一个桥接通道或者PBX本身会回调sip_answer()函数。而真正的媒体数据,音频或者视频,则会在RTP子系统中处理,具体见rtp.c

Outbound calls

 Outbound calls are set up by the PBX through the sip_request_call() function. After that, they are activated by sip_call().

Hanging up

 The PBX issues a hangup on both incoming and outgoing calls through the sip_hangup() function

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值