SIP(Session Initiation Protocol,会话初始协议)是由IETF(Internet Engineering Task Force,因特网工程任务组)制定的多媒体通信协议。广泛应用于CS(Circuit Switched,电路交换)、NGN(Next Generation Network,下一代网络)以及IMS(IP Multimedia Subsystem,IP多媒体子系统)的网络中,可以支持并应用于语音、视频、数据等多媒体业务,同时也可以应用于Presence(呈现)、Instant Message(即时消息)等特色业务。所有说,有IP网络的地方就有SIP协议的存在。
SIP协议与HTTP类似,SIP可以减少应用特别是高级应用的开发时间。
在SIP IM通信应用中,一般存在两种会话模式:
会话中,对于消息体内容大于1300字节时,一般采用Session Model。其会话建立过程(整个流程称为一个Dialogue)如下图所示:
主叫方A呼叫被叫方B:
在Sip消息中,对于消息体不大于1300字节时,一般采用Pager Model。Sip消息通信中采用MESSAGE方法,MESSAGE方法本身并不建立Dialog,在多数应用中,每条IM消息都是独立的,颇似分页消息。
MESSAGE方法的由来
RFC3428对Sip协议进行了扩展,在Sip协议中增加了MESSAGE请求方法。采用Pager Model进行通信,传递不超过1300字节的数据。MESSAGE方法详细可参考 “SIP-RFC3428” https://tools.ietf.org/html/rfc3428 。
MESSAGE方法消息体
当User1想给User2发送IM消息时,只需构造一个MESSAGE,发出去即可。
对于其消息体body可以是任何MIME格式。但必须支持plain/text格式,可以选择支持message/cpim、message/sdp格式,可能用message/cpim会好一点,因为已有的IM系统标准是message/cpim格式。
Pager Model请求流程
以User1向User2发送MESSAGE消息为例:
SIP消息体结构与Http协议结构相似,均由三部分组成:
请求行格式:
Method Request-URI SIP-Version CRLF
请求行举例:
INVITE sip:bob@zte.com SIP/2.0 /r/n
Method
以下列出了几种消息Method方法:
Request-URI
指示请求的用户或者服务的地址信息。
SIP-Version
请求和响应消息都需要包含SIP版本信息。
状态行格式:
SIP-Version Status-Code Reason-Phrase CRLF
状态行举例:
SIP/2.0 200 OK /r/n
Status-Code状态码:
状态代码由3位数字组成,表示请求是否被理解或被满足。状态代码的第一个数字定义了响应的类别,后面两位没有具体的分类。第一个数字有五种可能的取值:
常用的状态码举例:
发送MESSAGE消息给user2
MESSAGE sip:user2@domain.com SIP/2.0 Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse Max-Forwards: 70 From: sip:user1@domain.com;tag=49583 To: sip:user2@domain.com Call-ID: asd88asd77a@1.2.3.4 CSeq: 1 MESSAGE Content-Type: text/plain Content-Length: 18 user2, come here.
Header 字段含义说明:
以User1发送MESSAGE消息给user2为例:
步骤1:User1发送MESSAGE请求到代理服务器
MESSAGE sip:user2@domain.com SIP/2.0 Via: SIP/2.0/TCP user1pc.domain.com;branch=z9hG4bK776sgdkse Max-Forwards: 70 From: sip:user1@domain.com;tag=49583 To: sip:user2@domain.com Call-ID: asd88asd77a@1.2.3.4 CSeq: 1 MESSAGE Content-Type: text/plain Content-Length: 18 user2, come here.
步骤2:代理服务器转发User1的MESSAGE请求给USER2,代理服务器收到步骤1请求,到数据库中查找User2(注册过程中生成数据库),随后生成步骤2的数据。
MESSAGE sip:user2@domain.com 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:user1@domain.com;tag=49394 To: sip:user2@domain.com Call-ID: asd88asd77a@1.2.3.4 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:user1@domain.com;tag=49394 To: sip:user2@domain.com;tag=ab8asdasd9 Call-ID: asd88asd77a@1.2.3.4 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:user1@domain.com;;tag=49394 To: sip:user2@domain.com;tag=ab8asdasd9 Call-ID: asd88asd77a@1.2.3.4 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: <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, 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: <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,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: <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, 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: <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,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE Contact: <sip:192.168.2.89:14010> Content-Length: 0 WWW-Authenticate: Digest realm="192.168.2.89", qop="auth", nonce="e17d377c3d2d9c343e26576a7fd04738481dfc10", opaque="", stale=FALSE, algorithm=MD5
注:
这里192.168.2.89服务器带了一个WWW-Authenticate头。里面告知了如下几个重要的信息:
步骤三:
请求端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: <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, 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
注:
这里比第一次REGIETER消息多了Authorization头。其中:
步骤四:
服务器验证鉴权头的正确性,如果鉴权成功,给请求端发送200 OK消息。若失败,继续发送401消息:
SIP/2.0 200 OK 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,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,UPDATE,PRACK,REFER,SUBSCRIBE,NOTIFY,MESSAGE Contact: sip:192.168.2.161:8021 Content-Length: 0 Expires: 3600
原文:https://www.cnblogs.com/zhongqifeng/p/14816873.html