目录
一、SIP协议简介
二、SIP消息的两种会话模式
1、Session Model
2、Pager Model
三、SIP消息体格式
1、请求行(request-line)
2、状态行(status-line)
3、消息头
四、SIP消息举例
1、MESSAGE消息(Pager Model)
2、REGISTER消息
(1)非鉴权注册消息
(2)鉴权注册消息
会话发起协议(SIP,Session Initiation Protocol)是在VoIP技术中使用的最常见的协议之一,用于控制发起、修改、终结交互式多媒体会话的信令协议。它是一种应用层协议,与其他应用层协议协同工作,以控制Internet上的多媒体通信会话。
官方指导文档参考:https://datatracker.ietf.org/doc/html/rfc3428
在SIP IM通信应用过程中,一般存在着两种会话模式:
会话中,对于消息体内容大于1300字节时,一般采用Session Model。其会话建立过程如下图所示:
主叫方A呼叫被叫方B:
注:以上的整个流程称之为一个Dialog
在Sip消息中,对于消息体不大于1300字节时,一般采用Pager Model。Sip消息通信中采用MESSAGE方法,MESSAGE方法本身并不建立Dialog,在多数应用中,每条IM消息都是独立的,颇似分页消息。
当User1想给User2发送IM消息时,只需构造一个MESSAGE,发出去即可。
SIP消息体结构与Http协议结构相似,均由三部分组成:
请求行格式:Method Request-URI SIP-Version CRLF
请求行举例:INVITE sip:[email protected] SIP/2.0 /r/n
常用的Method包括:
Method | 方法说明 |
---|---|
REGISTER | 注册 |
INVITE | 发起会话请求 |
ACK | INVITE 请求的响应的确认 |
CANCEL | 取消请求 |
BYE | 终结会话 |
OPTIONS | 查询服务器能力 |
MESSAGE | RFC3428对Sip协议的扩展,增加了MESSAGE方法。采用Pager Model进行通信,传递不超过1300字节的数据。MESSAGE方法详细可参考 “SIP-RFC3428” RFC 3428 - Session Initiation Protocol (SIP) Extension for Instant Messaging |
Request-URI
指示请求的用户或者服务的地址信息
SIP-Version
请求和响应消息都需要包含SIP版本信息
状态行格式: SIP-Version Status-Code Reason-Phrase CRLF
状态行举例:SIP/2.0 200 OK /r/n
Status-Code状态码:
状态代码由3位数字组成,表示请求是否被理解或被满足。
状态代码的第一个数字定义了响应的类别,后面两位没有具体的分类。
第一个数字有五种可能的取值:
状态码 | 含义 |
---|---|
1xx: | 临时响应、表示请求消息正在被处理 |
2xx | 成功响应、表示请求已被成功接收完全理解并接收 |
3xx | 重定向响应、表示需采取进一步完成请求 |
4xx | 客户机错误、表示请求消息中包含语法错误信息或服务器无法完成客户机的请求 |
5xx | 服务器错误、表示服务器无法合法完成请求 |
6xx | 全局故障 、表示任何服务器都无法完成该请求 |
常用的状态码举例:
状态码 | msg | 含义 |
---|---|---|
100 | Trying | 试呼叫 |
180 | Ringing | 振铃 |
181 | Call is Being Forwarded | 呼叫正在前转 |
200 | OK | 成功响应 |
302 | Moved Temporarily | 临时迁移 |
400 | Bad Request | 错误请求 |
401 | Unauthorized | 未授权 |
403 | Forbidden | 禁止 |
404 | Not Found | 用户不存在 |
408 | Request Timeout | 请求超时 |
480 | Temporarily Unavailable | 暂时无人接听 |
486 | Busy Here | 线路忙 |
504 | Server Time-out | 服务器超时 |
600 | Busy Everywhere | 全忙 |
发送MESSAGE消息给user2
MESSAGE sip:[email protected] SIP/2.0
Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse
Max-Forwards: 70
From: sip:[email protected];tag=49583
To: sip:[email protected]
Call-ID: [email protected]
CSeq: 1 MESSAGE
Content-Type: text/plain
Content-Length: 18
user2, come here.
Header 字段含义说明:
Header | 含义说明 | 举例 |
---|---|---|
Call-ID | 由本地设备(Client)生成,全局唯一,每次呼叫这个值唯一不变 | Call-ID: [email protected] |
From | 表示请求的发起者 | From: sip:[email protected];tag=49583 |
To | 表示请求的接收者 | To: sip:[email protected] |
Via | Via头域是被服务器插入request中,用来检查路由环的,并且可以使response根据via找到返回的路 | Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse |
Max-Forwards | 用于表示这个包最多可以传送多少跳,每经过一跳都会减一当Max-Forwards==0系统会返回483。默认为70 | Max-Forwards: 70 |
Contact | 包含源的URI信息,用来给响应方直接和源建立连接用 | Contact: sip:192.168.100.1:1111 |
Content-Type | 指明消息体的类型 (SDP会话描述协议) | Content-Type: text/plain;Content-Type: application/sdp; Content-Type: application/cpim; |
Content-Length | 指明消息体的字节大小 | Content-Length: 18 |
以User1发送MESSAGE消息给user2为例:
步骤1:User1发送MESSAGE请求到代理服务器
MESSAGE sip:[email protected] SIP/2.0
Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse
Max-Forwards: 70
From: sip:[email protected];tag=49583
To: sip:[email protected]
Call-ID: [email protected]
CSeq: 1 MESSAGE
Content-Type: text/plain
Content-Length: 18
user2, come here.
步骤2:代理服务器转发User1的MESSAGE请求给USER2
代理服务器收到步骤1请求,到数据库中查找User2(注册过程中生成数据库),随后生成步骤2的数据。
MESSAGE sip:[email protected] SIP/2.0
Via: SIP/2.0/TCP proxy.domain.com;branch=z9hG4bK123dsghds
Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse;
received=1.2.3.4
Max-Forwards: 69
From: sip:[email protected];tag=49394
To: sip:[email protected]
Call-ID: [email protected]
CSeq: 1 MESSAGE
Content-Type: text/plain
Content-Length: 18
user2, come here.
步骤3:User2收到User1的消息后,回复200 OK给代理服务器
直接回应(200-OK) 没有Body,也不携带Contact头域
SIP/2.0 200 OK
Via: SIP/2.0/TCP proxy.domain.com;branch=z9hG4bK123dsghds;
received=192.0.2.1
Via: SIP/2.0/TCP user1pc.domain.com;;branch=z9hG4bK776sgdkse;
received=1.2.3.4
From: sip:[email protected];tag=49394
To: sip:[email protected];tag=ab8asdasd9
Call-ID: [email protected]
CSeq: 1 MESSAGE
Content-Length: 0
步骤4:代理服务器转发200 OK回复给User1
代理服务器收到回复后,去掉最顶端的Via,转发如下消息给User1
SIP/2.0 200 OK
Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse;
received=1.2.3.4
From: sip:[email protected];;tag=49394
To: sip:[email protected];tag=ab8asdasd9
Call-ID: [email protected]
CSeq: 1 MESSAGE
Content-Length: 0
192.168.2.161
机器发送注册消息给192.168.2.89
服务器:
REGISTER sip:192.168.2.89 SIP/2.0
Via: SIP/2.0/UDP 192.168.2.161:10586
Max-Forwards: 70
From: ;tag=ca04c1391af3429491f2c4dfbe5e1b2e;epid=4f2e395931
To:
Call-ID: [email protected]
CSeq: 1 REGISTER
Contact: ;methods="INVITE, MESSAGE, INFO, SUBSCRIBE, OPTIONS, BYE, CANCEL, NOTIFY, ACK, REFER"
User-Agent: RTC/1.2.4949 (BOL SIP Phone 1005)
Event: registration
Allow-Events: presence
Content-Length: 0
当注册成功(回送200 OK)时,服务器发送的res消息参考如下:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.2.161:10586
From: ;tag=ca04c1391af3429491f2c4dfbe5e1b2e;epid=4f2e395931
To: ;tag=-00834-14d0805b62bc026d
Call-ID: [email protected]
CSeq: 1 REGISTER
Allow: INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE
Contact: sip:192.168.2.161:10586
Content-Length: 0
Expires: 3600
当需要鉴权注册时:
请求端192.168.2.161发送注册消息给192.168.2.89服务器
REGISTER sip:192.168.2.89 SIP/2.0
Via: SIP/2.0/UDP 192.168.2.161:8021
Max-Forwards: 70
From: ;tag=efca469543ce4788a6a6a2c7b66cd01f;epid=de4504430d
To:
Call-ID: [email protected]
CSeq: 1 REGISTER
Contact: ;methods="INVITE, MESSAGE, INFO, SUBSCRIBE, OPTIONS, BYE, CANCEL, NOTIFY, ACK, REFER"
User-Agent: RTC/1.2.4949 (BOL SIP Phone 1005)
Event: registration
Allow-Events: presence
Content-Length: 0
服务器对192.168.2.161发送401 Unauthorized信息给请求端,提示请求端需要带上鉴权信息重新注册:
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.2.161:8021
From: ;tag=efca469543ce4788a6a6a2c7b66cd01f;epid=de4504430d
To: ;tag=-001893-38ba013ba3dde36e
Call-ID: [email protected]
CSeq: 1 REGISTER
Allow: INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE
Contact:
Content-Length: 0
WWW-Authenticate: Digest realm="192.168.2.89", qop="auth", nonce="e17d377c3d2d9c343e26576a7fd04738481dfc10", opaque="", stale=FALSE, algorithm=MD5
请求端192.168.2.161通过Authorization头字段带上鉴权头信息,发送一个新的REGISTER消息:
REGISTER sip:192.168.2.89 SIP/2.0
Via: SIP/2.0/UDP 192.168.2.161:8021
Max-Forwards: 70
From: ;tag=efca469543ce4788a6a6a2c7b66cd01f;epid=de4504430d
To:
Call-ID: [email protected]
CSeq: 2 REGISTER
Contact: ;methods="INVITE, MESSAGE, INFO, SUBSCRIBE, OPTIONS, BYE, CANCEL, NOTIFY, ACK, REFER"
User-Agent: RTC/1.2.4949 (BOL SIP Phone 1005)
Authorization: Digest username="01062237493", realm="192.168.2.89", qop=auth, algorithm=MD5, uri="sip:192.168.2.89", nonce="e17d377c3d2d9c343e26576a7fd04738481dfc10", nc=00000001, cnonce="12660455546344082314666316435946", response="f57e47ce03162293b9ced07362ce2b79"
Event: registration
Allow-Events: presence
Content-Length: 0
服务器验证鉴权头的正确性,如果鉴权成功,给请求端发送200 OK消息。若失败,继续发送401消息:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.2.161:8021
From: ;tag=efca469543ce4788a6a6a2c7b66cd01f;epid=de4504430d
To: ;tag=-001894-a5eb977c8969aa51
Call-ID: [email protected]
CSeq: 2 REGISTER
Allow: INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE
Contact: sip:192.168.2.161:8021
Content-Length: 0
Expires: 3600