RFC3581——SIP中的rport机制

本文深入探讨了SIP协议在遇到NAT网络地址转换时面临的问题及其解决方案,特别关注了rport机制在保持NAT绑定、实现信令穿越方面的关键作用。通过解析NAT的分类、SIP代理无法穿越NAT的原因,以及NAT穿越的常用解决方案,如ALG、MidCom、STUN等,本文提供了详尽的技术指导和实例演示,旨在帮助开发者理解和解决SIP信令在NAT环境下的传输挑战。

http://my.oschina.net/u/147624/blog/33203


1.    介绍

RFC3581的下载地址:http://www.ietf.org/rfc/rfc3581.txt

该协议比较简短,主要用于描述rportresponse-port)机制。

1.1 NAT分类

NAT:网络地址转换(NAT,Network Address Translation)属接入广域网(WAN)技术,是一种将私有(保留)地址转化为合法IP地址的转换技术,它被广泛应用于各种类型Internet接入方式和各种类型的网络中。原因很简单,NAT不仅完美地解决了lP地址不足的问题,而且还能够有效地避免来自网络外部的攻击,隐藏并保护网络内部的计算机。

NAT常用的分类如下:

Full Cone NAT(完全圆锥型)

Address Restricted Cone NAT(地址限制圆锥型 )

Port Restricted Cone NAT(端口限制圆锥型)

Symmetric NAT(对称型

1.1.1 完全圆锥型NAT

在完全圆锥型NATFull Cone NAT)中,NAT会将客户机地址{X:y}转换成公网地址{A:b}并绑定。任何包都可以通过地址{A:b}送到客户主机的{X:y}地址上。如图所示:
   

1.1.2 地址限制圆锥型NAT

地址限制圆锥型NATAddress Restricted Cone NAT)会将客户机地址{X:y}转换成公网地址{A:b}并绑定,只有来自主机{P}的包才能和主机{X:y}通信。如下图所示:
    

1.1.3 端口限制圆锥型NAT

端口限制圆锥型NAT(Port Restricted Cone NAT)会将客户机地址{X:y}转换成公网地址{A:b}并绑定,只有来自主机{P,q}的包才能和主机{X:y}通信。如下图所示:
    

1.1.4 对称型NAT

对称型NATSymmetric NAT)会将客户机地址{X:y}转换成公网地址{A:b}并绑定为{X:y}|{A:b}<->{P:q}。对称型NAT只接受来自{P:q}incoming packet,将它转给{X:y} ,每次客户机请求一个不同的公网地址和端口,NAT会新分配一个端口号{C,d} 。如下图所示:
     

1.2问题描述

1.2.1 SIP Proxy无法穿过NAT回送SIP信令

因为SIP信令中的FromContact头域记录的是私网地址和端口,NAT无法识别和转换。如图所示:
   

1.2.2 使用UDP Hole Punching的问题

        

这个内网的NAT上打了一个方向为211.136.91.58,(这就是称为UDP Hole Punching的技术)以后211.136.91.58就可以通过这个洞与内网的192.168.1.223联系了,但是其他的IP不能利用这个洞。

在没有活动的时候,这个Hole会过期:

NAT对于地址转换关系是有一定生命期的,某个地址转换后在一段时间内没有被使用将会被清除,当这个业务流再次出现时,将会建立一个新的地址转换关系。

SIP代理无法穿越

SIPUDPTCP上操作。当在UDP中使用的时候,对请求的响应被发送给请求所来自的地址,端口字段带在请求的Via头字段中。一半以上的信息(例如:IP地址)带在 IP包头中,还有一半的信息(例如:端口信息)带在SIP消息头中。SIP这样做的原因是为了监听所有的信息,包括请求消息和响应消息。

但是这种方式在客户端在 NAT中的情况不适用,在NAT的环境中,回应可能发送不过去,因为与在请求中找到的地址不一样,而且此前也没有方法让客户端来得到源端口信息。

2.    NAT的常用解决方案

解决NAT穿越有很多中解决方案,常用的有:

2.1 ALGApplication Level Gateway

可以识别SIP信令,能够适当地修改数据包。ALG可以是单独的连接于外网和内网之间的设备,也可以是内置于防火墙内的插件。

FW/NAT发现外网呼叫信令为SIP时,将其转发到ALG(应用层网关),通过ALG建立起内网伪地址终端与外网终端的通信连接。

使用ALG需要对现有设备升级改造。例如思科的路由器都支持配置ALG

2.2 MidComIETF MIDCOM(Middlebox Communications

允许第三方(MIDCOM Agent )成为受FW/NAT信任的实体,然后代表FW/NAT做出决定,强迫其开放端口传送媒体流或数据流。这些受信任的实体通过“MidCom”定义的新协议与FW/NAT进行通信。

协议的识别不由Middlebox完成,而是由外部的MIDCOM Agent完成。

使用MidCom需要对现有设备升级改造。

2.3 STUN(Simple Traversalof UDP Through Network)

 
      IETF RFC 3489
定义了如何确定由NAT分配的公网地址和端口,不需要改造现有NAT

主要特色

能够让客户端发现NAT的存在以及类型;

能够让客户端发现NAT的绑定生命周期;

可以工作在多NAT串联环境下;

非常简单的协议,易于实现,负载低;

STUN服务器可以位于公网任何地方。

适用范围

不适用于Symmetric NAT

对于Non- Symmetric NAT都适用;

如果双方都位于同一个NAT之后,就不适用。

2.4 SBC(Session Border Controller)

             
    
Signaling Solution

Ø SBC可以帮助SIP信令穿越已经存在的FW/NAT,而不需要对现有的FW/NAT设备做任何改变;

Ø 对于SIP终端,SIP终端设备会周期性发送注册消息到SBC

Media Traversal Solution

SBC可以把相应的媒体流发送到防火墙上的相关IP地址和端口,然后正确地使媒体流到达防火墙后的用户侧。

3     rport机制讲解

3.1 方案描述

获得IP地址是在Via头中带上received参数。为了得到端口信息,也参考了这种方式,即在Via头中带上rport属性来指明端口信息。

当在客户端和服务器之间是NAT的时候,请求可能会在NAT中创建(或刷新)一个绑定,为了让客户端收到响应信息,在事务处理的过程中这个绑定必须保持存在。大多数的NAT绑定有超过1分钟的超时时间,这超过了non-INVITE事务的持续时间,因而对non-INVITE事务的请求的响应只能在绑定存在的时候存在。INVITE事务倒是不存在这个问题。

为了保持这个绑定,客户端应该在每隔20s左右重发INVITE请求,这种重发机制需要发生在收到一个临时的响应后。

当然刚才所说的大概1分钟的超时时间也不是确定的,有时候会比这长,此时重发机制可以发慢一点,否则,可以发快一点。这些问题可参考RFC3489

如果是支持rport机制的服务器,它需要在接收到的请求中检查Via头是否包含一个没有值的rport参数。如果有,它需要在回应中带上rport的值,这与received的处理类似。

为了穿越对称性的对称性的NAT,响应需要发送到相同的IP地址和端口。当服务器在多端口或接口的请求上监听请求时,它必须记住请求是从何处发的。对一个稳定的Proxy,在一个传输的持续时间中,记住这些东西是没有问题的。但是对于不稳定的Proxy,它不存储请求和响应中的状态信息,为了达到本规范的要求,它需要将地址和端口信息加密到Via头字段中,在响应信息到达的时候,它能提取加密的信息并将它放到响应中。

rport机制需要终端支持该种机制,因此应用情况比较受限。但是在笔者的应用场景(呼叫中心)中,主要要解决的问题是坐席能在NAT环境中穿越,给服务器发送信息。因为坐席所使用的SIP软电话是本公司开发的,所以可以保证是支持rportreceived的。

3.2 实例

下面举一个发送REGISTER信息的实例,在请求信息的Via头中包含了没有值的rport参数,如下所示:

  REGISTER sip:  124.40  .  120.188  :  5060   SIP  /  2.0
Via: SIP/
  2.0  /  UDP   124.42  .  4.203  :  15500  ;branch  =  z9hG4bK  -  d8754z  -  1049ed261d2e643d  -  1  ---  d8754z -;rport
Max
  -  Forwards:   70
Contact: <
  sip:  19988888888  @  192.168  .  2.65  :  12344  ;rinstance  =  7cd1c532e92fdb0e  >  ;expires  = 
To: "
  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  >
From: "
  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  >  ;tag  =203ba359
Call
  -ID: Yzc4N2IwMzY5OWU4MTdkMzY0NWY4OWU3NjMzNmJiM2U.
CSeq: 
  1 REGISTER
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO
User
  -  Agent: eyeBeam release 1105a stamp   56793
Content-
  Length: 

    发送到的服务器支持rport机制,它看到请求中的rport后,将通过分析UDP包信息得到的的NAT的公网地址(124.42.4.203)和端口信息(15500)分别作为receivedrport属性带给客户端:

  SIP  /  2.0     200   OK
Via: SIP
  /  2.0  /  UDP   124.42  .  4.203  :  15500  ;branch  =  z9hG4bK  -  d8754z  -  1049ed261d2e643d  -  1  ---  d8754z -  ;rport  =  15500  ;received  =  124.42  .  4.203 
From: 
  "  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  >  ;tag  =  203ba359
To: 
  "  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  >  ;tag  =  0005  -  058  -  7d6dc90516ae2e21
Call
  -  ID: Yzc4N2IwMzY5OWU4MTdkMzY0NWY4OWU3NjMzNmJiM2U.
CSeq: 
  4   REGISTER
Allow: INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE
Contact: 
  <  sip:  124.40  .  120.188  :  5060  > 
Content
  -  Length: 

   客户端在得到响应信息后,知道了所使用的公网地址和端口,在而后定期重发的REGISTER信息中,Contact变换成124.42.4.203: 15500,例如新发的REGISTER信息变为:

  REGISTER sip:  124.40  .  120.188  :  5060   SIP  /  2.0 
Via: SIP
  /  2.0  /  UDP   124.42  .  4.203  :  15500  ;branch  =  z9hG4bK  -  d8754z  -  1049ed261d2e643d  -  1  ---  d8754z -  ;rport
Max
  -  Forwards:   70 
Contact: 
  <  sip:  19988888888  @  124.42  .  4.203    15500  ;rinstance  =  7cd1c532e92fdb0e  >  ;expires  = 
To: 
  "  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  > 
From: 
  "  19988888888  "  <  sip:  19988888888  @  124.40  .  120.188  :  5060  >  ;tag  =  203ba359
Call
  -  ID: Yzc4N2IwMzY5OWU4MTdkMzY0NWY4OWU3NjMzNmJiM2U.
CSeq: 
  2   REGISTER
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO
User
  -  Agent: eyeBeam release 1105a stamp   56793 

Content - Length: 


rport方式主要是对sip信令中Via字头的扩展,不过同时也要求SIP Proxy支持该功能。
NAT之后的sip client在发送请求的时候在via字头中添加rport字段,该消息经发出后路由到SIPProxy,SIP Proxy通过检查消息的源地址和Via字段中的地址,得知该client处于NAT之后,并且基于已有的rport,将消息的真实地址即公网上的地址通过received和rport字段返回给client端,这样client就知道自己真实的公网地址,可以解决信令穿越的问题。

 4.    参考文档

神州泰岳应用开发事业部郑昀《SIP穿越NAT
   
RFC3581http://www.ietf.org/rfc/rfc3581.txt


pj_status_t pjsua_call_make_call( pjsua_acc_id acc_id, pj_str_t *dest_uri, int seq, unsigned options, void *user_data, const pjsua_msg_data *msg_data, const pjsua_call_other *other, pjsua_call_id *p_call_id) { pj_pool_t *tmp_pool = NULL; pjsip_dialog *dlg = NULL; pjmedia_sdp_session *offer = NULL; pjsip_inv_session *inv = NULL; pjsua_acc *acc = NULL; pjsua_call *call = NULL; int call_id = -1; pj_str_t contact = {NULL, 0}; pjsip_tx_data *tdata = NULL; pj_status_t status; # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 pj_str_t local_uri = {NULL, 0}; # endif # if PJ_FAKE_CALL_SUPPORT pj_bool_t bfakeCall = (0 == dest_uri->slen) ? PJ_TRUE : PJ_FALSE; # endif pj_bool_t bCreateFwRule = PJ_TRUE; # if PJ_FAKE_CALL_SUPPORT bCreateFwRule = !bfakeCall; # endif #ifdef INCLUDE_RFC3325 pjsip_generic_string_hdr privacyHdr; pjsip_p_prefered_identity_hdr *preferedIdHdr; pj_str_t preferedId_Url_Str = {NULL, 0}; pjsip_uri *preferedIdUri = NULL; pj_str_t hname = {"", 0}; pj_str_t hvalue = {"", 0}; #if PJ_RFC3960_SUPPORT char localIp[40]; pj_uint16_t rtpPort; pj_uint16_t rtcpPort; int family; #endif #endif /* INCLUDE_RFC3325 */ #if PJ_FAKE_CALL_SUPPORT if (bfakeCall) { dest_uri->slen = sprintf(dest_uri->ptr, "%s", "sip:192.168.1.100"); } #endif #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Ready to make a call!"); #if PJ_FAKE_CALL_SUPPORT } #endif /* Check that account is valid */ PJ_ASSERT_RETURN( # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 -1==acc_id || # endif ( acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc)), PJ_EINVAL); /* Check arguments */ PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "argument:accId %d, destUri(%.*s)", acc_id, dest_uri->slen, pj_strnull(dest_uri->ptr)); #if PJ_FAKE_CALL_SUPPORT } #endif PJSUA_LOCK(); /* Create sound port if none is instantiated, to check if sound device * can be used. But only do this with the conference bridge, as with * audio switchboard (i.e. APS-Direct), we can only open the sound * device once the correct format has been known */ /*ycw-pjsip-delete conference*/ #if 0 if (!pjsua_var.is_mswitch #if 0 /*ycw-pjsip-20110610-delete sound device*/ && pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && !pjsua_var.no_snd #endif ) { /*ycw-pjsip-20110610-delete sound device*/ #if 0 pj_status_t status; status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); if (status != PJ_SUCCESS) { PJSUA_UNLOCK(); return status; } #endif } #endif # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (acc_id >= 0) { # endif acc = &pjsua_var.acc[acc_id]; if (!acc->valid # if defined(SUPPORT_ACCOUNT_RTT) && SUPPORT_ACCOUNT_RTT!=0 || !acc->regDuration /*ycw-pjsip-regDuration*/ # elif !defined(INCLUDE_EMERGENCY_CALL) || !acc->regOK # endif ) { pjsua_perror(THIS_FILE, "Unable to make call because account " "is not valid or not active", PJ_EINVALIDOP); cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Unable to make call because account " "is not valid or not active"); PJSUA_UNLOCK(); return PJ_EINVALIDOP; } # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 } # endif /* Find free call slot. */ call_id = alloc_call_id(); if (call_id == PJSUA_INVALID_ID) { pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Alloc call id error"); #if PJ_FAKE_CALL_SUPPORT } #endif PJSUA_UNLOCK(); return PJ_ETOOMANY; } call = &pjsua_var.calls[call_id]; /* Associate session with account */ call->acc_id = acc_id; # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (acc) { # endif call->call_hold_type = acc->cfg.call_hold_type; # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 } else { call->call_hold_type = PJSUA_CALL_HOLD_TYPE_DEFAULT; } # endif call->seq = seq; /* Create temporary pool */ tmp_pool = pjsua_pool_create("tmpcall10", 512, 256); if (NULL == tmp_pool) { pjsua_perror(THIS_FILE, "Error create pool", PJ_ENOMEM); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Create pool error!"); #if PJ_FAKE_CALL_SUPPORT } #endif PJSUA_UNLOCK(); return PJ_ENOMEM; } /* Verify that destination URI is valid before calling * pjsua_acc_create_uac_contact, or otherwise there * a misleading "Invalid Contact URI" error will be printed * when pjsua_acc_create_uac_contact() fails. */ if (1) { pjsip_uri *uri; pj_str_t dup; pj_strdup_with_null(tmp_pool, &dup, dest_uri); //printf("going to parse the string: %.*s\n", (int)dup.slen, pj_strnull(dup.ptr)); uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0); if (uri == NULL) { pjsua_perror(THIS_FILE, "Unable to make call", PJSIP_EINVALIDREQURI); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Parse uri error!"); #if PJ_FAKE_CALL_SUPPORT } #endif pj_pool_release(tmp_pool); PJSUA_UNLOCK(); return PJSIP_EINVALIDREQURI; } } PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id, (int)dest_uri->slen, pj_strnull(dest_uri->ptr))); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Making call with acc #%d to %.*s", acc_id, (int)dest_uri->slen, pj_strnull(dest_uri->ptr)); #if PJ_FAKE_CALL_SUPPORT } #endif /* Mark call start time. */ pj_gettimeofday(&call->start_time); /* Reset first response time */ call->res_time.sec = 0; /* Create suitable Contact header unless a Contact header has been * set in the account. */ if ( # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 acc && # endif acc->contact.slen ) { contact = acc->contact; } else { status = pjsua_acc_create_uac_contact(tmp_pool, &contact, acc_id, dest_uri); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Unable to generate Contact header"); #if PJ_FAKE_CALL_SUPPORT } #endif pj_pool_release(tmp_pool); PJSUA_UNLOCK(); return status; } } /* Create outgoing dialog: */ #if 0 status = pjsip_dlg_create_uac( pjsip_ua_instance(), &acc->cfg.id, &contact, dest_uri, dest_uri, &dlg); #else #ifdef INCLUDE_RFC3325 if (acc && (PJ_TRUE == acc->cfg.supportNAI)) { local_uri.ptr = (char*)pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); local_uri.slen = pj_ansi_snprintf(local_uri.ptr, PJSIP_MAX_URL_SIZE, "\"Anonymous\"<sip:%s@%.*s>", acc->cfg.preferId.ptr, (int)acc->srv_domain.slen, pj_strnull(acc->srv_domain.ptr)); PJ_LOG(1,(THIS_FILE, "Making Anonymous call with acc #%d to %.*s", acc_id, (int)local_uri.slen, pj_strnull(local_uri.ptr))); status = pjsip_dlg_create_uac( pjsip_ua_instance(), &local_uri, &contact, dest_uri, dest_uri, &dlg); options = 0; // reset } else #endif /* INCLUDE_RFC3325 */ if (options == pj_call_anonymous) { pj_str_t anonymous_uri = {"<sip:anonymous@anonymous.invalid>", 33}; status = pjsip_dlg_create_uac( pjsip_ua_instance(), &anonymous_uri, &contact, dest_uri, dest_uri, &dlg); options = 0; // reset } # if (defined(NUM_VOICEAPP_CHANNELS) && 0!=NUM_VOICEAPP_CHANNELS) else if (options == pj_call_voiceapp_anonymous) { pj_str_t anonymous_uri = {"<sip:anonymous@anonymous.invalid>", 33}; status = pjsip_dlg_create_uac( pjsip_ua_instance(), &anonymous_uri, &contact, dest_uri, dest_uri, &dlg); call->isVoiceapp = PJ_TRUE; options = 0; // reset } else if(options == pj_call_voiceapp) { # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (NULL == acc && pjsua_var.BoundIp.slen > 0) { local_uri.ptr = (char*)pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); local_uri.slen = pj_ansi_snprintf(local_uri.ptr, PJSIP_MAX_URL_SIZE, "<sip:%.*s>", (int)pjsua_var.BoundIp.slen, pjsua_var.BoundIp.ptr); status = pjsip_dlg_create_uac( pjsip_ua_instance(), &local_uri, &contact, dest_uri, dest_uri, &dlg); } else # endif { pj_str_t *local_str = &acc->cfg.id; if (acc->cfg.lineId.slen > 0 ) { CMSIP_PRINT("=========the acc has lineid,replace it!!!============\n"); local_str = &acc->cfg.lineId; } status = pjsip_dlg_create_uac( pjsip_ua_instance(), local_str, &contact, dest_uri, dest_uri, &dlg); } /*call->isVoiceapp = PJ_TRUE; options = 0;*/ } #endif else { # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (NULL == acc && pjsua_var.BoundIp.slen > 0) { local_uri.ptr = (char*)pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); local_uri.slen = pj_ansi_snprintf(local_uri.ptr, PJSIP_MAX_URL_SIZE, "<sip:%.*s>", (int)pjsua_var.BoundIp.slen, pjsua_var.BoundIp.ptr); status = pjsip_dlg_create_uac( pjsip_ua_instance(), &local_uri, &contact, dest_uri, dest_uri, &dlg); } else # endif { pj_str_t *local_str = &acc->cfg.id; if (acc->cfg.lineId.slen > 0 ) { CMSIP_PRINT("=========the acc has lineid,replace it!!!============\n"); local_str = &acc->cfg.lineId; } status = pjsip_dlg_create_uac( pjsip_ua_instance(), local_str, &contact, dest_uri, dest_uri, &dlg); } } #endif if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Dialog creation failed", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Dialog creation failed"); #if PJ_FAKE_CALL_SUPPORT } #endif pj_pool_release(tmp_pool); PJSUA_UNLOCK(); return status; } /* Increment the dialog's lock otherwise when invite session creation * fails the dialog will be destroyed prematurely. */ pjsip_dlg_inc_lock(dlg); /* Calculate call's secure level */ call->secure_level = get_secure_level(acc_id, dest_uri); /* Init media channel */ status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, call->secure_level, dlg->pool, NULL, NULL); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Error initializing media channel", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Error initializing media channel"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } /* Create offer */ /*ycw-pjsip. t38*/ #if 0 status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, &offer, NULL); #else /*ycw-pjsip-codec*/ /*We must set the sdp's codec list according to the order of the codec list of the account.*/ if (options == pj_call_t38) { status = pjsua_media_channel_create_sdp_t38(call->index, dlg->pool, NULL, &offer, NULL, bCreateFwRule); options = 0; } else { status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, &offer, NULL, bCreateFwRule); } if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Error creating sdp", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Create sdp error!"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } # if defined(INCLUDE_PSTN_GATEWAY) if (pj_call_pstn == options) { call->isPstn = PJ_TRUE; # if (defined(PJ_MEDIA_TRANSIT_BY_PJSIP) && 0==PJ_MEDIA_TRANSIT_BY_PJSIP) && \ (defined(INCLUDE_PSTN_GATEWAY) || defined(INCLUDE_USB_VOICEMAIL)) if (PJ_FALSE == call->medTpReady) { status = pjsua_media_transport_create_for_single_call(call_id); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "create media transport for single call error", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Create media transport error!"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } call->medTpReady = PJ_TRUE; } # endif } else { call->isPstn = PJ_FALSE; } # endif #endif /*ycw-pjsip. t38*/ #if defined(PJMEDIA_HAS_SRTP) && PJMEDIA_HAS_SRTP!= 0 int audio_index; # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (NULL == acc) { audio_index = find_audio_index(offer, pjsua_var.ua_cfg.use_srtp); } else { # endif audio_index = find_audio_index(offer, pjsua_var.acc[call->acc_id].cfg.use_srtp); # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 } # endif #else int audio_index = find_audio_index(offer, PJ_FALSE); #endif if (audio_index < 0) audio_index = 0; call->request_channel_mode = pjsua_call_acquire_channel_mode(offer, audio_index); /* Create the INVITE session: */ options |= PJSIP_INV_SUPPORT_100REL; if (NULL == acc) { if (pjsua_var.ua_cfg.require_100rel) { options |= PJSIP_INV_REQUIRE_100REL; } if (pjsua_var.ua_cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) { options |= PJSIP_INV_SUPPORT_TIMER; if (pjsua_var.ua_cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) { options |= PJSIP_INV_REQUIRE_TIMER; } else if (pjsua_var.ua_cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) { options |= PJSIP_INV_ALWAYS_USE_TIMER; } } } else { if (acc->cfg.require_100rel) { options |= PJSIP_INV_REQUIRE_100REL; } if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) { options |= PJSIP_INV_SUPPORT_TIMER; if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) { options |= PJSIP_INV_REQUIRE_TIMER; } else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) { options |= PJSIP_INV_ALWAYS_USE_TIMER; } } } /*ycw-pjsip*/ # if defined(SUPPORT_FAX_T38) && SUPPORT_FAX_T38!=0 if (pjsua_var.enableT38) { options |= PJSIP_INV_ENABLE_T38; } # endif status = pjsip_inv_create_uac( dlg, offer, options, &inv); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Invite session creation failed", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Create Invite session error!"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } /* Init Session Timers */ # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (NULL == acc) { status = pjsip_timer_init_session(inv, &pjsua_var.ua_cfg.timer_setting #ifdef INCLUDE_TFC_ES , TR_UAC #endif ); } else { # endif status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting #ifdef INCLUDE_TFC_ES , TR_UAC #endif ); # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 } # endif if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Session Timer init failed", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Init session timer error!"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } /* Create and associate our data in the session. */ call->inv = inv; dlg->mod_data[pjsua_var.mod.id] = call; inv->mod_data[pjsua_var.mod.id] = call; /*ycw-pjsip*/ dlg->callIndex = call_id; dlg->seq = seq; /* Attach user data */ call->user_data = user_data; /* If account is locked to specific transport, then lock dialog * to this transport too. */ if ( # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 acc && # endif acc->cfg.transport_id != PJSUA_INVALID_ID) { pjsip_tpselector tp_sel; pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); pjsip_dlg_set_transport(dlg, &tp_sel); } /* Set dialog Route-Set: */ #if !defined(INCLUDE_TFC_ES) /*By YuChuwei, For Telefonica*/ if ( # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 acc && # endif !pj_list_empty(&acc->route_set)) { pjsip_dlg_set_route_set(dlg, &acc->route_set); } #endif /* Set credentials: */ if ( # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 acc && # endif acc->cred_cnt) { pjsip_auth_clt_set_credentials( &dlg->auth_sess, acc->cred_cnt, acc->cred); } /* Set authentication preference */ # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 if (acc) { # endif pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); # if defined(SUPPORT_IPCALL_NO_ACCOUNT) && SUPPORT_IPCALL_NO_ACCOUNT!=0 } # endif #if defined(INCLUDE_SIP_INVITE_PRIORITY) if (other) { inv->isWarmCall = other->isWarmCall; } else { inv->isWarmCall = 0; } #endif /* INCLUDE_SIP_INVITE_PRIORITY */ /* Create initial INVITE: */ status = pjsip_inv_invite(inv, &tdata); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to create initial INVITE request", status); #if PJ_FAKE_CALL_SUPPORT if (!bfakeCall) { #endif cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "Unable to create initial INVITE request"); #if PJ_FAKE_CALL_SUPPORT } #endif goto on_error; } #ifdef INCLUDE_RFC3325 if (acc) { if (acc->cfg.preferId.slen > 0) { preferedIdHdr = pjsip_p_asserted_identity_hdr_create(tmp_pool); if (NULL == preferedIdHdr) { pjsua_perror(THIS_FILE, "No memory!", PJ_ENOMEM); goto on_error; } if (PJ_TRUE == acc->cfg.supportNAI) { preferedId_Url_Str.ptr = (char*)pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); preferedId_Url_Str.slen = pj_ansi_snprintf(preferedId_Url_Str.ptr, PJSIP_MAX_URL_SIZE, "\"Anonymous\"<sip:%s@%.*s>", acc->cfg.preferId.ptr, (int)acc->srv_domain.slen, pj_strnull(acc->srv_domain.ptr)); } else { preferedId_Url_Str.ptr = (char*)pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); preferedId_Url_Str.slen = pj_ansi_snprintf(preferedId_Url_Str.ptr, PJSIP_MAX_URL_SIZE, "\"%s\"<sip:%s@%.*s>", acc->cfg.preferId.ptr, acc->cfg.preferId.ptr, (int)acc->srv_domain.slen, pj_strnull(acc->srv_domain.ptr)); } CMSIP_PRINT("==parse prefered identity=="); preferedIdUri = (pjsip_uri*)pjsip_parse_uri(tmp_pool, preferedId_Url_Str.ptr, preferedId_Url_Str.slen, 0); if (NULL == preferedIdUri) { CMSIP_PRINT("==parse prefered identity error=="); pjsua_perror(THIS_FILE, "parse value of Prefered Identity error ", PJ_EINVAL); } else { preferedIdHdr->uri = preferedIdUri; pj_list_push_back(&msg_data->hdr_list, preferedIdHdr); } } hname = pj_str("Privacy"); if(acc->cfg.supportNAI) { hvalue = pj_str("id"); } else { hvalue = pj_str("none"); } pjsip_generic_string_hdr_init2(&privacyHdr, &hname, &hvalue); pj_list_push_back(&msg_data->hdr_list, &privacyHdr); } #endif /* INCLUDE_RFC3325 */ #if defined(INCLUDE_ROBIN_DNS) || defined(INCLUDE_VOIP_ROBIN_DNS) if (acc && acc->regc && acc->regc->srv_ipinfo.server_name.slen) { CMSIP_PRINT("==copy server address from regc to dlg=="); pj_strdup(dlg->pool, &dlg->server_name, &acc->regc->srv_ipinfo.server_name); memcpy(&dlg->server_addr, &acc->regc->srv_ipinfo.server_addr, sizeof(dlg->server_addr) ); dlg->cur_addr = acc->regc->srv_ipinfo.cur_addr; } #endif /* INCLUDE_ROBIN_DNS || INCLUDE_VOIP_ROBIN_DNS */ /* Add additional headers etc */ pjsua_process_msg_data( tdata, msg_data); # if PJ_FAKE_CALL_SUPPORT if (bfakeCall) { char contact[1][MAX_URI_LEN] = {{0}}; cmsip_send_callStatus(CMSIP_CALL_GENERIC, call_id, seq, 0, contact, 1); status = PJ_EINVAL; goto on_error; } # endif /* Send initial INVITE: */ status = pjsip_inv_send_msg(inv, tdata); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to send initial INVITE request", status); cmsip_send_systemlog(CMSIP_SYSTEMLOG_WARN, "Unable to send initial INVITE request for [accIndex(%d),callIndex(%d) dest(%.*s)]", acc_id, call_id, dest_uri->slen, pj_strnull(dest_uri->ptr)); /* Upon failure to send first request, the invite * session would have been cleared. */ inv = NULL; goto on_error; } cmsip_send_systemlog(CMSIP_SYSTEMLOG_DEBUG, "the initial INVITE request for [accIndex(%d),callIndex(%d) dest(%.*s)] is sent", acc_id, call_id, dest_uri->slen, pj_strnull(dest_uri->ptr)); /* Must increment call counter now */ ++pjsua_var.call_cnt; CMSIP_PRINT("~~~~now, call count is (%d)~~~~~", pjsua_var.call_cnt); #if defined(INCLUDE_TFC_ES) && PJ_RFC3960_SUPPORT #if PJ_MEDIA_TRANSIT_BY_PJSIP==0 pjmedia_transport_udp_local_info(call->med_orig, localIp, &rtpPort, &rtcpPort, &family); cmsip_send_rtpcreate(call->index, (options==pj_call_t38)?CMSIP_MEDIA_TYPE_IMAGE:CMSIP_MEDIA_TYPE_AUDIO, (options==pj_call_t38)?CMSIP_MEDIA_TPPROTO_UDPTL:CMSIP_MEDIA_TPPROTO_RTP_AVP, CMSIP_MEDIA_DIR_NONE, NULL, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, localIp, strlen(localIp), rtpPort, NULL, 0, 0, 0, family); #else /*BTD*/ #endif #endif /* Done. */ if (p_call_id) { *p_call_id = call_id; } pjsip_dlg_dec_lock(dlg); pj_pool_release(tmp_pool); PJSUA_UNLOCK(); return PJ_SUCCESS; on_error: /*If tdata has been created, must release it, or , this will cause memory leak. By Yuchuwei*/ if (tdata) { pjsip_tx_data_dec_ref(tdata); tdata = NULL; } if (dlg) { /* This may destroy the dialog */ pjsip_dlg_dec_lock(dlg); } if (inv != NULL) { pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE); } if (call_id != -1) { pjsua_media_channel_deinit(call_id); reset_call(call_id); # if PJ_FAKE_CALL_SUPPORT if (bfakeCall) { call->busy = PJ_FALSE; } # endif } CMSIP_PRINT("==release pool=="); pj_pool_release(tmp_pool); PJSUA_UNLOCK(); return status; }
最新发布
10-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值