【GBT28181開發:SIP協議實踐】之註冊流程

Just4life發表於2014-02-21

由於已經成功搭建好基於eXosip的UAC的開發環境,並且也與SPVMN測試系統互動成功,所以現在開始針對註冊、查詢等業務進行詳細的研究。

今天先研究下裝置註冊的流程,模擬一個IPC向SPVMN系統註冊,記錄下互動的訊息,詳細研究了下:

轉載請註明出處:http://blog.csdn.net/longlong530


一.環境搭建:

環境準備:http://blog.csdn.net/longlong530/article/details/9176989

UAC(模擬IPC):

IP:192.168.10.117

Port:5061


UAS(模擬SIP伺服器):

IP:192.168.10.177

Port:5060


二.業務時序圖:



三.訊息詳解:


第一步:UAC --> UAS

UAC向UAS註冊,下面是具體註冊訊息:

  1. REGISTER sip:192.168.10.177:5060 SIP/2.0 
  2. Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK2509000523;received=192.168.10.117 
  3. From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869 
  4. To: <sip:34020000001320000001@192.168.10.177> 
  5. Call-ID: 939764460 
  6. CSeq: 1 REGISTER 
  7. Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b> 
  8. Max-Forwards: 70 
  9. User-Agent: eXosip/3.6.0 
  10. Expires: 3600 
  11. Authorization: Capability, algorithm="H:MD5" 
  12. Content-Length: 0 
REGISTER sip:192.168.10.177:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK2509000523;received=192.168.10.117
From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869
To: <sip:34020000001320000001@192.168.10.177>
Call-ID: 939764460
CSeq: 1 REGISTER
Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b>
Max-Forwards: 70
User-Agent: eXosip/3.6.0
Expires: 3600
Authorization: Capability, algorithm="H:MD5"
Content-Length: 0

欄位說明:

請求起始行:表示UAC向IP地址為192.168.10.177的伺服器發起註冊,SIP版本號為2.0

From欄位:指明該REGISTER請求訊息由UAS(IP地址:192.168.10.177)控制的UAC發起的。

To欄位:指明REGISTER請求接收方的地址。此時REGISTER請求的接收方為IP地址為192.168.10.177的UAS。(這個值和To頭域的值相同,除非這個請求是第三方發起的註冊請求。)
Call-ID欄位:UAC發出的給某個註冊伺服器(registrar)的所有註冊請求都應該有相同的Call-ID頭域值。如果相同的客戶端用了不同的Call-ID值,註冊伺服器(registrar)就不能檢測是否一個REGISTER請求由於延時的關係導致了故障。
Cseq欄位:Cseq值保證了REGISTER請求的正確順序。一個UA為每一個具備相同的Call-ID的REGISTER請求順序遞增這個Cseq欄位。
Contact欄位:在REGISTER請求中的Contact欄位指明使用者可達位置。
Expires欄位:表示該登記生存期為3600s。
Content-Length欄位:表明此請求訊息訊息體的長度為空,即此訊息不帶會話描述。

第二步,UAS-->UAC

UAS返回401 Unauthorized(無許可權)響應,表明要求對UAC進行使用者認證,並且通過WWW-Authenticate欄位攜帶UAS支援的認證方式,產生本次認證的nonce

  1. SIP/2.0 401 Unauthorized 
  2. To: <sip:34020000001320000001@192.168.10.177>;tag=66081813_53173353_54026835-98f6-4d1e-b562-0d969636b944 
  3. Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK2509000523;received=192.168.10.117 
  4. CSeq: 1 REGISTER 
  5. Call-ID: 939764460 
  6. From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869 
  7. WWW-Authenticate: Digest realm="3402000000",nonce="324cab6e557268e0" 
  8. Content-Length: 0 
SIP/2.0 401 Unauthorized
To: <sip:34020000001320000001@192.168.10.177>;tag=66081813_53173353_54026835-98f6-4d1e-b562-0d969636b944
Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK2509000523;received=192.168.10.117
CSeq: 1 REGISTER
Call-ID: 939764460
From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869
WWW-Authenticate: Digest realm="3402000000",nonce="324cab6e557268e0"
Content-Length: 0

第三步,UAC-->UAS

UAC重新向UAS發起註冊請求,攜帶WWW-Authorization欄位

  1. REGISTER sip:192.168.10.177:5060 SIP/2.0 
  2. Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK3163110852;received=192.168.10.117 
  3. From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869 
  4. To: <sip:34020000001320000001@192.168.10.177> 
  5. Call-ID: 939764460 
  6. CSeq: 2 REGISTER 
  7. Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b> 
  8. Authorization: Digest username="34020000001320000001",realm="3402000000",nonce="324cab6e557268e0",uri="sip:192.168.10.177:5060",response="7f536c8ce38593239f9130e37a72276e",algorithm=MD5 
  9. Max-Forwards: 70 
  10. User-Agent: eXosip/3.6.0 
  11. Expires: 3600 
  12. Content-Length: 0 
REGISTER sip:192.168.10.177:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK3163110852;received=192.168.10.117
From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869
To: <sip:34020000001320000001@192.168.10.177>
Call-ID: 939764460
CSeq: 2 REGISTER
Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b>
Authorization: Digest username="34020000001320000001",realm="3402000000",nonce="324cab6e557268e0",uri="sip:192.168.10.177:5060",response="7f536c8ce38593239f9130e37a72276e",algorithm=MD5
Max-Forwards: 70
User-Agent: eXosip/3.6.0
Expires: 3600
Content-Length: 0

第四步,UAS-->UAC

UAS收到UAC的註冊請求,首先檢查NONCE的正確性,如果和在401 Unauthorized響應中產生的NONCE相同,則通過。否則,直接返回失敗。然後,UAS會根據NONCE、使用者名稱、密碼(伺服器端可以根據本地使用者資訊獲取使用者的密碼)、URI等採用和終端相同的演算法生成
RESPONSE,並且對此RESPONSE和請求訊息中的RESPONSE進行比較,如果二者一致則使用者認證成功,否則認證失敗。此時,UAS返回200 OK響應訊息,表明終端認證成功。

  1. SIP/2.0 200 OK 
  2. To: <sip:34020000001320000001@192.168.10.177>;tag=31428812_53173353_376bd3d9-cb9d-4b4f-a950-da26d575cff5 
  3. Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK3163110852;received=192.168.10.117 
  4. CSeq: 2 REGISTER 
  5. Call-ID: 939764460 
  6. From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869 
  7. Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b> 
  8. Expires: 3600 
  9. Date: 2013-06-26T16:08:37.164 
  10. Content-Length: 0 
SIP/2.0 200 OK
To: <sip:34020000001320000001@192.168.10.177>;tag=31428812_53173353_376bd3d9-cb9d-4b4f-a950-da26d575cff5
Via: SIP/2.0/UDP 192.168.10.117:5061;rport=5061;branch=z9hG4bK3163110852;received=192.168.10.117
CSeq: 2 REGISTER
Call-ID: 939764460
From: <sip:34020000001320000001@192.168.10.177>;tag=2577708869
Contact: <sip:34020000001320000001@192.168.10.117:5061;line=6513f93347c330b>
Expires: 3600
Date: 2013-06-26T16:08:37.164
Content-Length: 0

附程式碼:

 

int eXosip_register(int expires)/*expires/註冊訊息過期時間,單位為秒*/
{
	int ret = 0;
	eXosip_event_t *je  = NULL;
	osip_message_t *reg = NULL;
	char from[100];/*sip:主叫使用者名稱@被叫IP地址*/
	char proxy[100];/*sip:被叫IP地址:被叫IP埠*/

	memset(from, 0, 100);
	memset(proxy, 0, 100);
	sprintf(from, "sip:%s@%s", device_info.ipc_id, device_info.server_ip);
	sprintf(proxy, "sip:%s:%s", device_info.server_ip, device_info.server_port);

	/*傳送不帶認證資訊的註冊請求*/
retry:
	eXosip_lock();
	g_register_id = eXosip_register_build_initial_register(from, proxy, NULL, expires, ®);
	osip_message_set_authorization(reg, "Capability algorithm=\"H:MD5\"");
	if(0 > g_register_id)
	{
		eXosip_unlock();
		printf("eXosip_register_build_initial_register error!\r\n");
		return -1;
	}
	printf("eXosip_register_build_initial_register success!\r\n");

	ret = eXosip_register_send_register(g_register_id, reg);
	eXosip_unlock();
	if(0 != ret)
	{
		printf("eXosip_register_send_register no authorization error!\r\n");
		return -1;
	}
	printf("eXosip_register_send_register no authorization success!\r\n");

	printf("g_register_id=%d\r\n", g_register_id);

	for(;;)
	{
		je = eXosip_event_wait(0, 50);/*偵聽訊息的到來*/
		if(NULL == je)/*沒有接收到訊息*/
		{
			continue;
		}
		if(EXOSIP_REGISTRATION_FAILURE == je->type)/*註冊失敗*/
		{
			printf("<EXOSIP_REGISTRATION_FAILURE>\r\n");
			printf("je->rid=%d\r\n", je->rid);
			/*收到伺服器返回的註冊失敗/401未認證狀態*/
			if((NULL != je->response)&&(401 == je->response->status_code))
			{
				reg = NULL;
				/*傳送攜帶認證資訊的註冊請求*/
				eXosip_lock();
				eXosip_clear_authentication_info();/*清除認證資訊*/
				eXosip_add_authentication_info(device_info.ipc_id, device_info.ipc_id, device_info.ipc_pwd, "MD5", NULL);/*新增主叫使用者的認證資訊*/
				eXosip_register_build_register(je->rid, expires, ®);
				ret = eXosip_register_send_register(je->rid, reg);
				eXosip_unlock();
				if(0 != ret)
				{
					printf("eXosip_register_send_register authorization error!\r\n");
					return -1;
				}
				printf("eXosip_register_send_register authorization success!\r\n");
			}
			else/*真正的註冊失敗*/
			{
				printf("EXOSIP_REGISTRATION_FAILURE error!\r\n");
				goto retry;/*重新註冊*/
			}
		}
		else if(EXOSIP_REGISTRATION_SUCCESS == je->type)
		{
			/*收到伺服器返回的註冊成功*/
			printf("<EXOSIP_REGISTRATION_SUCCESS>\r\n");
			g_register_id = je->rid;/*儲存註冊成功的註冊ID*/
			printf("g_register_id=%d\r\n", g_register_id);
			break;
		}
	}

	return 0;
}


 

相關文章