SIP消息分为请求和响应,格式由一个请求行/状态行、几个标题头、一个空行和一个消息体(可选)组成,之间使用回车换行符表示终结,即使消息中未包含消息体,空行也不能省略:
1、请求行/状态行
<1>请求行包含三部分:方法名,请求URL和协议版本,各部分之间均使用一个空格字符进行分隔,除此之外请求行必须使用回车换行符表示终结;
REGISTER消息,用于发送注册请求信息
INVITE、ACK、CANCEL用于建立会话连接
BYE用于终结会话连接
OPTIONS用于查询服务器能力
注:方法名必须是大写,除此之外,SIP协议在其他文档中还定义了若干方法实现扩展
b、请求URL:
URL中不允许出现空格或其他控制字符,不能包含于“<>”之内
c、协议版本:
用于定义协议版本号
例如:
INVITE sip:liahr@mycompany.com SIP/2.0
<2>响应行包含:协议版本、状态码和状态码相关的文本描述组成,各部分之间由一个空格字符隔开,使用回车换行符表示终结
a、协议版本
用于定义协议版本号
b、状态码
是一个三位的十进制整数,第一位指示响应类型、后两位数字表示具体响应、用于指示请求消息的执行响应结果,状态码有6中:
1)、1XX 临时响应、表示请求消息正在被处理
2)、2XX 成功响应、表示请求已被成功接收完全理解并接收
3)、3XX 重定向响应、表示需采取进一步完成请求
4)、4XX 客户机错误、表示请求消息中包含语法错误信息或服务器无法完成客户机的请求
5)、5xx 服务器错误、表示服务器无法合法完成请求
6)、6XX 全局故障 、表示任何服务器都无法完成该请求
c、状态码相关描述(原因)
是对状态码简单的文本描述,客户机不必检查或显示
2、标题头
标题头提供关于请求/应答的信息和关于这些消息所包含的消息体的信息。一些标题头可以在请求或应答两种消息中使用,而其他的只能单独特定用于请求或应答。标题头由标题头名,后面跟一个冒号,在后面跟着值组成: 头子段:字段值,遵循RFC2822。
注:消息头字段中允许出现多个空格字符,但在实现过程中建议冒号之前,头字段之后应避免出现空格字符;冒号之后,字段值之前加一个空格;不同的标题头顺序任意;
只能存在于请求消息中的字段,为请求字段;只能存在于响应消息中的字段,为响应字段;当消息头字段中出现不匹配的字段的时候,消息接收实体则忽略;
消息头域值可以折叠成多行,但紧接着的折叠行由空格(SP)或水平制表(HT)折叠标记开始。所有的线性空白(LWS)包括折叠行的折叠标记(空格SP或水平制表键HT),具有同SP一样的语义。接收者在解析域值或将消息转送到下游(downstream)之前可能会将任何线性空白(LWS)替换成单个SP(空格)。
例如From标题头:
From: tfg2002 <sip:tfg2002@company.com>
3、空行
空行隔开了sip消息头和消息正文(消息体)
3、消息实体
被SIP消息携带的消息体通常是描述符,但它也可以由任何不透明物体组成(XML,SDP)。对于SIP代理而言,消息体的内容是完全透明的;SIP消息实体可以包含二进制消息实体,如果未明确指出,则为缺省的消息文本类型UTF-8;
Content-type: 消息实体类型;
Content-Encoding:消息的编码方式;
Content-Length:消息实体长度,单位为字节;
--转载:
1、MESSAGE消息
1)头字段填写说明
Call-id: 必选
CSeq: 必选
From: 必选
To: 必选
Max-Forwards: 必选
Via: 必选
常用的可选参数:
指定的消息体
2)消息实例
发送MESSAGE请求消息给192.168.2.48的6010端口,参考消息如下(带了“Hello”的消息体):
Call - ID:8e12c17121ac4121bf927f6fd8013358 @192 . 168.2 . 89
From : < sip: 01052237300 @192 . 168.2 . 89 > ;tag =- 0037 - 708c9a5cba8dd878
To : < sip: 1897778888 @192 . 168.2 . 89 >
CSeq: 1 MESSAGE
Via:SIP / 2.0 / UDP 192.168 . 2.89 : 14010 ;branch = z9hG4bK -- 22bd7222
Max - Forwards: 30
Allow:INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO, UPDATE ,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE
Contact: < sip: 192.168 . 2.89 : 14010 >
Content - Type: text / plain
Content - Length: 5
Hello
Via:SIP / 2.0 / UDP 192.168 . 2.89 : 14010 ;branch = z9hG4bK -- 22bd7222
From : < sip: 01052237300 @192 . 168.2 . 89 > ;tag =- 0037 - 708c9a5cba8dd878
To : < sip: 1897778888 @192 . 168.2 . 89 > ;tag =- 002 - 3c18e810ab17c76f
Call - ID:8e12c17121ac4121bf927f6fd8013358 @192 . 168.2 . 89
CSeq: 1 MESSAGE
Allow:INVITE,MESSAGE
Contact: < sip: 192.168 . 2.48 : 54010 >
Content - Type: text / plain
Content - Length: 11
Helloamigo
2、REGISTER消息
1)头字段填写说明
Call-id: 必选
Cseq: 必选
From: 必选
To: 必选
Max-Forwards: 必选
Via: 必选
Contact: 必选
Authorization: 必选
Expires: 常用可选头
2)非鉴权注册消息实例
在该实例中192.168.2.161机器发送注册消息给192.168.2.89服务器,发送消息实例如下:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 10586
Max - Forwards: 70
From : < sip: 01062237496 @192 . 168.2 . 89 > ;tag = ca04c1391af3429491f2c4dfbe5e1b2e;epid = 4f2e395931
To : < sip: 01062237496 @192 . 168.2 . 89 >
Call - ID:da56b0fab5c54398b16c0d9f9c0ffcf2 @192 . 168.2 . 161
CSeq: 1 REGISTER
Contact: < sip: 192.168 . 2.161 : 10586 > ;methods = "INVITE,MESSAGE,REFER"
User - Agent:RTC / 1.2 . 4949 (BOLSIPPhone 1005 )
Event:registration
Allow - Events:presence
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 10586
From : < sip: 01062237496 @192 . 168.2 . 89 > ;tag = ca04c1391af3429491f2c4dfbe5e1b2e;epid = 4f2e395931
To : < sip: 01062237496 @192 . 168.2 . 89 > ;tag =- 00834 - 14d0805b62bc026d
Call - ID:da56b0fab5c54398b16c0d9f9c0ffcf2 @192 . 168.2 . 161
CSeq: 1 REGISTER
Allow:INVITE,MESSAGE
Contact:sip: 192.168 . 2.161 : 10586
Content - Length: 0
Expires: 3600
当需要鉴权注册时,当请求端192.168.2.161使用BOL或xlite等发送注册消息给192.168.2.89服务器时,服务器对192.168.2.161发送“401 Unauthorized”信息给请求端,提示请求段需要带上鉴权信息重新注册,请求端带上鉴权信息后(带有“Authorization”头字段)重新向服务器注册,服务器验证鉴权头的正确性,如果鉴权成功,给请求端发送200 OK消息。若失败,继续发送401消息。
第一步:请求端(192.168.2.161)发送REGISTER消息,参考消息如下:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 8021
Max - Forwards: 70
From : < sip: 01062237493 @192 . 168.2 . 89 > ;tag = efca469543ce4788a6a6a2c7b66cd01f;epid = de4504430d
To : < sip: 01062237493 @192 . 168.2 . 89 >
Call - ID:c88a247a74b54a8c9e676bdde3bba6c9 @192 . 168.2 . 161
CSeq: 1 REGISTER
Contact: < sip: 192.168 . 2.161 : 8021 > ;methods = "INVITE,REFER"
User - Agent:RTC / 1.2 . 4949 (BOLSIPPhone 1005 )
Event:registration
Allow - Events:presence
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 8021
From : < sip: 01062237493 @192 . 168.2 . 89 > ;tag = efca469543ce4788a6a6a2c7b66cd01f;epid = de4504430d
To : < sip: 01062237493 @192 . 168.2 . 89 > ;tag =- 001893 - 38ba013ba3dde36e
Call - ID:c88a247a74b54a8c9e676bdde3bba6c9 @192 . 168.2 . 161
CSeq: 1 REGISTER
Allow:INVITE,MESSAGE
Contact: < sip: 192.168 . 2.89 : 14010 >
Content - Length: 0
WWW - Authenticate:Digestrealm = " 192.168 . 2.89 ",qop = "auth",nonce = "e17d377c3d2d9c343e26576a7fd04738481dfc10",opaque = "",stale = FALSE,algorithm = MD5
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 8021
Max - Forwards: 70
From : < sip: 01062237493 @192 . 168.2 . 89 > ;tag = efca469543ce4788a6a6a2c7b66cd01f;epid = de4504430d
To : < sip: 01062237493 @192 . 168.2 . 89 >
Call - ID:c88a247a74b54a8c9e676bdde3bba6c9 @192 . 168.2 . 161
CSeq: 2 REGISTER
Contact: < sip: 192.168 . 2.161 : 8021 > ;methods = "INVITE,REFER"
User - Agent:RTC / 1.2 . 4949 (BOLSIPPhone 1005 )
Authorization :Digestusername = " 01062237493 ",realm = " 192.168 . 2.89 ",qop = auth,algorithm = MD5,uri = "sip: 192.168 . 2.89 ",nc = 00000001 ,cnonce = " 12660455546344082314666316435946 ",response = "f57e47ce03162293b9ced07362ce2b79"
Event:registration
Allow - Events:presence
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 8021
From : < sip: 01062237493 @192 . 168.2 . 89 > ;tag = efca469543ce4788a6a6a2c7b66cd01f;epid = de4504430d
To : < sip: 01062237493 @192 . 168.2 . 89 > ;tag =- 001894 - a5eb977c8969aa51
Call - ID:c88a247a74b54a8c9e676bdde3bba6c9 @192 . 168.2 . 161
CSeq: 2 REGISTER
Allow:INVITE,MESSAGE
Contact:sip: 192.168 . 2.161 : 8021
Content - Length: 0
Expires: 3600
3、SUBSCRIBE消息
1)头字段填写说明
Call-id: 必选
CSeq: 必选
From: 必选
To: 必选
Max-Forwards: 必选
Via: 必选
Expires:常用可选头
2)消息实例
请求端(192.168.2.161)给服务器端(192.168.2.89)发送SUBSCRIBE消息,参考消息如下:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 32092 ;branch = z9hG4bK - d87543 - ec03ee3e022acf28 - 1 -- d87543-;rport
Max - Forwards: 70
Contact: < sip: 01062237498 @192 . 168.2 . 161 : 32092 >
To :" 01062237498 " < sip: 01062237498 @192 . 168.2 . 89 : 5060 >
From :" 01062237498 " < sip: 01062237498 @192 . 168.2 . 89 : 5060 > ;tag = e01c2548
Call - ID:NDQzMGE1MDMzYWU0NjRiYzMxMGY3NGUxMjBhMjBkNTQ.
CSeq: 1 SUBSCRIBE
Expires: 300
Allow:INVITE,INFO
User - Agent:eyeBeamrelease1011dstamp 40820
Event:message - summary
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 32092 ;branch = z9hG4bK - d87543 - ec03ee3e022acf28 - 1 -- d87543-;rport
From :" 01062237498 " < sip: 01062237498 @192 . 168.2 . 89 : 5060 > ;tag = e01c2548
To :" 01062237498 " < sip: 01062237498 @192 . 168.2 . 89 : 5060 >
Call - ID:NDQzMGE1MDMzYWU0NjRiYzMxMGY3NGUxMjBhMjBkNTQ.
CSeq: 1 SUBSCRIBE
Content - Length: 0
4、INVITE消息
1)头字段填写说明
必选头域如下:
Call-id
Contact
CSeq
From
To
Max-Forwards
Via
Supported
Allow
常用可选头域:
Accept
Authorization
Content-length
Content-type
Record-Route
Route
Require
Proxy-Authorization
Proxy-require
P-asserted-identity
P-prefered-identity
Privacy
2)非鉴权INVITE消息实例
在这个实例中,笔者在本机(192.168.2.161)上使用BOL注册了17899998888号码,呼叫01062230001号码时发送INVITE消息给服务器(192.168.2.89)的软交换,发送INVITE消息参考如下:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
Max - Forwards: 70
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 >
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 1 INVITE
Contact: < sip: 192.168 . 2.161 : 9545 >
User - Agent:RTC / 1.2
Content - Type:application / sdp
Content - Length: 691
v = 0
o =- 0 0 IN IP4 192.168 . 2.161
s = session
c =IN IP4 192.168 . 2.161
b = CT: 1000
t = 0 0
m = audio 56284 RTP / AVP 97 111 112 6 0 8 4 5 3 101
k = base64:fGmazgf5GXZfJxZ27G9A7rxA4B7KX0pHrjYELKPHPog
a = rtpmap: 97 red / 8000
a = rtpmap: 111 SIREN / 16000
a = fmtp: 111 bitrate = 16000
a = rtpmap: 112 G7221 / 16000
a = fmtp: 112 bitrate = 24000
a = rtpmap: 6 DVI4 / 16000
a = rtpmap: 0 PCMU / 8000
a = rtpmap: 8 PCMA / 8000
a = rtpmap: 4 G723 / 8000
a = rtpmap: 5 DVI4 / 8000
a = rtpmap: 3 GSM / 8000
a = rtpmap: 101 telephone - event / 8000
a = fmtp: 101 0 - 16
a = encryption:optional
m = video 61432 RTP / AVP 34 31
k = base64:W5uTU8sIQjJVPWyRF31GZouVwAHFtE1cAmblK + mvDdI
a = recvonly
a = rtpmap: 34 H263 / 90000
a = rtpmap: 31 H261 / 90000
a = encryption:optional
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 >
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 1 INVITE
Content - Length: 0
SIP / 2.0 180 Ringing
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 > ;tag =- 006773 - 7d10081ef45af150
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 1 INVITE
Allow:INVITE,MESSAGE
Contact: < sip: 192.168 . 2.89 : 14010 >
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.89 : 14010 ;branch = z9hG4bK -- 50c44f35
From : < sip: 17899998888 @192 . 168.2 . 89 > ;tag =- 002221 - d749165cdfed2151
To : < sip: 01062230001 @192 . 168.2 . 89 > ;tag = 6b3e0000bf5c0000
Call - ID:fdfa416f8779a8701af1086b07879a65 @192 . 168.2 . 89
CSeq: 1 INVITE
Contact: < sip: 192.168 . 2.161 : 5060 >
Content - Type:application / sdp
Allow:INVITE,MESSAGE
Content - Length: 118
v = 0
o =- 1 1 IN IP4 192.168 . 2.161
s =-
t = 0 0
m = audio 19194 RTP / AVP 8
c =IN IP4 192.168 . 2.161
a = rtpmap: 8 PCMA / 8000
5、ACK消息
1)头字段填写说明
必选头域如下:
Call-id
Cseq
From
To
Max-Forwords
Via
常用可选头域:
Content-Length
Content-Type
Route
2)消息实例
在“4、INVITE消息”中讲到了主叫方接收到INVITE消息的200 消息后,接着给主叫方发送ACK消息,消息参考如下:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
Max - Forwards: 70
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 > ;tag =- 006773 - 7d10081ef45af150
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 1 ACK
User - Agent:RTC / 1.2
Content - Length: 0
6、BYE消息
1)头字段填写说明
必选头域如下:
Call-id
Cseq
From
To
Max-Forwards
Via
常用可选头域:
Content-Length
Content-Type
Route
Reason
2)消息实例
本实例笔者在本机(192.168.2.161)上注册了17899998888号码,呼叫01062230001号码时发送INVITE消息给服务器(192.168.2.89)的软交换,并与呼叫的号码建立通话后,主叫方主动挂断时,BYE消息如下所示:
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
Max - Forwards: 70
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 > ;tag =- 006773 - 7d10081ef45af150
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 2 BYE
User - Agent:RTC / 1.2
Content - Length: 0
Via:SIP / 2.0 / UDP 192.168 . 2.161 : 9545
From :"Administrator" < sip: 17899998888 @192 . 168.2 . 89 > ;tag = 2dc6e1000081463ba14f7db4e50b8643;epid = d6b5434cef
To : < sip: 01062230001 @192 . 168.2 . 89 > ;tag =- 006773 - 7d10081ef45af150
Call - ID:31e72e80d5b04f52aba6cb8be8f3c0c0 @192 . 168.2 . 161
CSeq: 2 BYE
Content - Length: 0
7、PRACK消息
1)头字段填写说明
必选头域如下:
Call-id
Cseq
From
To
Max-Forwards
Via
RAck
常用可选头域:
Content-Type
Content-Length
2)消息实例
如下是PRACK消息的参考实例:
CSeq: 2 PRACK
Call - ID:0112114b0ca1d73c3fbdc7036e4672c8 @huawei .com
Contact: < sip: 01055554444 @192 . 168.2 . 46 >
Content - Length: 0
From : < sip: 01055554444 @192 . 168.2 . 154 > ;tag = 6c789654
To : < sip: 01062237400 @192 . 168.2 . 154 > ;tag = 0012 - f1939f63004dbd16
Via:SIP / 2.0 / UDP 192.168 . 2.46 ;branch = z9hG4bK04982ba62
User - Agent:Huawei - MC820 / 1.0 . 0
Supported:100rel
RAck: 1 1 INVITE
8、INFO消息
1)头字段填写说明
必选头域如下:
Call-id
Cseq
From
To
Max-Forwards
Via
常用可选头域:
Content-Type
Content-Length
Route
2)消息实例
在笔者开发过程中,媒体服务器与软交换服务器之间的使用的是INFO消息交互,如下消息为软交换服务器向媒体服务器发送的INFO消息(请求开始录音),参考如下:
Call - ID:52f3d02182b72d37c48cea5dd013a401 @192 . 168.2 . 89
From : < sip:msml @minicc : 5090 > ;tag = 013913 - 74fa8415d2902b1c
To : < sip: as @cintel .net.cn > ;tag =- 00708 - b879297623d39362
CSeq: 1 INFO
Via:SIP / 2.0 / UDP 192.168 . 2.89 : 5090 ;branch = z9hG4bKf06b482d
Max - Forwards: 30
Allow:INVITE,MESSAGE
Contact: < sip: 192.168 . 2.89 : 5090 >
Content - Type:application / msml + xml
Content - Length: 164
< ?xmlversion = " 1.0 "encoding = "US - ASCII "? >
< msmlversion = " 1.1 " >
< eventname = "app.startingRecord"id = "conf:002221c6082350d233 / dialog:PRLeg708" >
</ event >
</ msml >
Via:SIP / 2.0 / UDP 192.168 . 2.89 : 5090 ;branch = z9hG4bKf06b482d
From : < sip:msml @minicc : 5090 > ;tag = 013913 - 74fa8415d2902b1c
To : < sip: as @cintel .net.cn > ;tag =- 00708 - b879297623d39362
Call - ID:52f3d02182b72d37c48cea5dd013a401 @192 . 168.2 . 89
CSeq: 1 INFO
Allow:INVITE,MESSAGE
Contact: < sip: 192.168 . 2.89 : 14010 >
Content - Length: 0
9、OPTIONS消息
1)头字段填写说明
必选头域如下:
Call-id
Cseq
From
To
Max-Forwords
Via
常用可选头域:
Accept
Allow
Supported
2)消息实例
参考实例如下所示:
Via:SIP / 2.0 / UDPcavendish.kings.cambridge.edu.uk;branch = z0hG4bK1834
Max - Forwards: 70
To : < sip: user @proxy .carrier.com >
From :J.C.Maxwell < sip:james.maxwell @kings .cambridge.edu.uk > ;tag = 34
Call - ID: 9352812 @cavendish .kings.cambridge.edu.uk
CSeq: 1 OPTIONS
Content - Length: 0
10、NOTIFY消息
参考消息实例如下所示:
ViaSIP / 2.0 / UDPcartouche.rosettastone.org: 5060 ;branch = z9hG4bK3841323
Max - Forwards: 70
To :ThomasYoung < sip:tyoung @elasticity .co.uk > ;tag = 1814
From : < sip:ptolemy @rosettastone .org > ;tag = 5363956k
Call - ID:452k59252058dkfj34924lk34
CSeq: 3 NOTIFY
Contact: < sip:Ptolemy @cartouche .rosettastone.org >
Event:dialog
Subscription - State:active
Allow - Event:dialog
Content - Type:application / xml + dialog
Content - Length:消息体长度
(xml格式的消息体)
11、REFER消息
参考消息实例如下所示:
ViaSIP / 2.0 / UDPlab34.bletchleypark.co.uk: 5060 ;branch = z9hG4bK932039
Max - Forwards: 69
To : < sip:m.rejewski @biuroszyfrow .pl > ;tag = ACEBDC
From :AlanTuring < sip:turing @bletchleypark .co.uk > ;tag = 213424
Call - ID:3419fak3kFD23s1A9dkl
CSeq; 5412 REFER
Refer - To : < sip:info @scherbius - ritter.com >
Content - Length: 0