这一节将通过一个简单的例子来介绍一些基本的 SIP 操作。先让我们来诊视下图展示的两个用户代理之间的消息顺序。关于SIP协议详细流程,可以参考RFC3261-SIP协议,当然如果不习惯的话可以参考中文翻译版RFC3261-SIP协议中文版
收到180响应时建立dialog叫做早期对话(early dialog),收到2XX的应答开始才是真正的dialog建立。
当然,这个代理可以有多个代理,其实也就就是多了代理服务器之间的转发过程,如下图:
发起呼叫流程
//发出 INVITE请求
Request: INVITE sip:100@172.20.16.107 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bK5DF007802335421F9A6DAE3DC9B49E54..
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>
Contact: <sip:300@172.20.16.107:5060>
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22243 INVITE
Content-Type: application/sdp..
//返回响应 100 Trying
Response: SIP/2.0 100 Trying
Via: SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bK5DF007802335421F9A6DAE3DC9B49E54
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22243 INVITE
Contact: <sip:100@172.20.16.146>
//如果被邀请方收到INVITE 请求,在应答呼叫之前接收到响应180 Ringing
Response: SIP/2.0 180 Ringing
Via: SIP/2.0/UDP
172.20.16.107:5060;branch=z9hG4bK5DF007802335421F9A6DAE3DC9B49E54
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22243 INVITE
Contact: <sip:100@172.20.16.146>
//收到被邀请方应答呼叫的响应 200 OK
Response: SIP/2.0 200 OK
Via: SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bK5DF007802335421F9A6DAE3DC9B49E54
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22243 INVITE
Contact: <sip:100@172.20.16.146>
//呼叫发起方收到200 OK消息,直接发送一个ACK确认消息给被邀请方
Request: ACK sip:100@172.20.16.146 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bK30F7F7B47E45499BAC441059EFA2DEA2
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Contact: <sip:300@172.20.16.107:5060>
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22243 ACK
接受呼叫过程
//接收到 INVITE请求
Request: INVITE sip:300@172.20.16.107 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.146:5060;branch=z9hG4bK5490f4d8
From: "ppp" <sip:100@172.20.16.146>;tag=as45eb9e71
To: <sip:300@172.20.16.107>
Contact: <sip:100@172.20.16.146>
Call-ID: 0ee9bea806059b0f2770ce5c060d5251@172.20.16.146
CSeq: 102 INVITE
Date: Tue, 15 Mar 2005 05:41:21 GMT
//发送回应100 Trying
Response: SIP/2.0 100 Trying
Via: SIP/2.0/UDP 172.20.16.146:5060;f=z9hG4bK5490f4d8
From: "ppp" <sip:100@172.20.16.146>;tag=as45eb9e71
To: <sip:300@172.20.16.107>;tag=3363667257
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 0ee9bea806059b0f2770ce5c060d5251@172.20.16.146
CSeq: 102 INVITE
//如果接受邀请,则在接受之前发送回应180 Ringing
Response: SIP/2.0 180 Ringing
Via: SIP/2.0/UDP 172.20.16.146:5060;branch=z9hG4bK5490f4d8
From: "ppp" <sip:100@172.20.16.146>;tag=as45eb9e71
To: <sip:300@172.20.16.107>;tag=3363667257
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 0ee9bea806059b0f2770ce5c060d5251@172.20.16.146
CSeq: 102 INVITE
如果决定应答呼叫,则发送200 Ok消息
Response: SIP/2.0 200 Ok
Via: SIP/2.0/UDP 172.20.16.146:5060;branch=z9hG4bK5490f4d8
From: "ppp" <sip:100@172.20.16.146>;tag=as45eb9e71
To: <sip:300@172.20.16.107>;tag=3363667257
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 0ee9bea806059b0f2770ce5c060d5251@172.20.16.146
CSeq: 102 INVITE
//接收到邀请方发来的ACK确认消息
Request: ACK sip:300@172.20.16.107:5060 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.146:5060;branch=z9hG4bK74cf8e58
From: "ppp" <sip:100@172.20.16.146>;tag=as45eb9e71
To: <sip:300@172.20.16.107>;tag=3363667257
Contact: <sip:100@172.20.16.146>
Call-ID: 0ee9bea806059b0f2770ce5c060d5251@172.20.16.146
CSeq: 102 ACK
终止呼叫或拒绝接受邀请
//发送BYE消息
Request: BYE sip:100@172.20.16.146 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bK2CF3B0C22620465D988E1CC2C8A71C56
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Contact: <sip:300@172.20.16.107:5060>
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22244 BYE
返回200 OK消息
Response: SIP/2.0 200 OK
Via: SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bK2CF3B0C22620465D988E1CC2C8A71C56
From: 300 <sip:300@172.20.16.107>;tag=2549473886
To: <sip:100@172.20.16.107>;tag=as30112a7b
Call-ID: B2ADB3A5-CCB1-485D-AB6C-17D70D82D76E@172.20.16.107
CSeq: 22244 BYE
Contact: <sip:100@172.20.16.146>
取消邀请过程
//发出 INVITE请求
Request: INVITE sip:100@172.20.16.107 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 INVITE
//返回响应 100 Trying
Response: SIP/2.0 100 Trying
Via: SIP/2.0/UDP
172.20.16.107:5060;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>;tag=as3324adcc
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 INVITE
Contact: <sip:100@172.20.16.146>
//返回响应180 Ringing
Response: SIP/2.0 180 Ringing
Via:SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>;tag=as3324adcc
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 INVITE
Contact: <sip:100@172.20.16.146>
取消INVITE请求
Request: CANCEL sip:100@172.20.16.107 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 CANCEL
返回487请求终止应答
Response: SIP/2.0 487 Request Terminated
Via: SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>;tag=as3324adcc
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 INVITE
Contact: <sip:100@172.20.16.146>
//返回应答200 OK
Response: SIP/2.0 200 OK
Via: SIP/2.0/UDP 172.20.16.107:5060;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>;tag=as3324adcc
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 CANCEL
Contact: <sip:100@172.20.16.146>
//发送ACK确认消息
Request: ACK sip:100@172.20.16.107 SIP/2.0
Via: SIP/2.0/UDP 172.20.16.107:5060;rport;branch=z9hG4bKE7C2E749AA8B49C693EA90BE1BB367D6
From: 300 <sip:300@172.20.16.107>;tag=1829163469
To: <sip:100@172.20.16.107>;tag=as3324adcc
Contact: <sip:300@172.20.16.107:5060>
Call-ID: 7C09DBD4-85DE-4DA7-8881-A9B309F8E672@172.20.16.107
CSeq: 41305 ACK