
Before learning SIP, we must first understand what is SIP?
SIP is an application-layer control protocol that can be used to establish, modify, and terminate multimedia sessions (or conferences) such as Internet telephony. SIP supports five aspects in establishing and maintaining the termination multimedia session protocol:
- User Location: Check the location of the end user for communication.
- User Effectiveness: Checks how willing a user is to participate in a session.
- User Capabilities: Check Media and Media Parameters.
- Establish a session: "ringing", establish session parameters between the calling party and the called party.
- Session management: including sending and terminating sessions, modifying session parameters, activating services, and more.
After understanding SIP, we should come to know the format of SIP protocol:
The SIP message body structure is similar to the HTTP protocol structure, and consists of three parts:
- request-line or status-line
- header
- body
request line:
- Format: Method Request-URI SIP-Version
- eg: INVITE sip: 10087@dev.xswitch.cn SIP/2.0
- The main message methods of method:

status line:
- Format: SIP-Version Status-Code
- Such as: SIP/2.0 200 OK
Some common status codes are briefly listed below:

Header:
copyINVITE sip:10087@dev.xswitch.cn SIP/2.0 Record-Route: <sip:192.168.31.188:15060;lr=on;nat=yes> Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.1c537238ae43750e62a423ddf7fe078c.0 Via: SIP/2.0/UDP 172.18.0.1:61789;received=172.18.0.1;rport=61789;branch=z9hG4bKPjTu0TroBVkc2BuxTbvQ4ODaYdk83-ljf9 Max-Forwards: 69 From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: sip:10087@dev.xswitch.cn Contact: <sip:10086@172.18.0.1:61789;ob;alias=172.18.0.1~61789~1> Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 INVITE Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS Supported: replaces, 100rel, norefersub User-Agent: Telephone 1.5.2 Content-Type: application/sdp Content-Length: 471
Introduction to each message header:

message body:
Message encoding protocol header:
copyv=0 o=- 3874185567 3874185567 IN IP4 172.18.0.1 s=pjmedia b=AS:117 t=0 0 a=X-nat:0 m=audio 4000 RTP/AVP 96 9 8 0 101 102 c=IN IP4 172.18.0.1 b=TIAS:96000 a=rtpmap:96 opus/48000/2 a=fmtp:96 useinbandfec=1 a=rtpmap:9 G722/8000 a=rtpmap:8 PCMA/8000 a=rtpmap:0 PCMU/8000 a=rtpmap:101 telephone-event/48000 a=fmtp:101 0-16 a=rtpmap:102 telephone-event/8000 a=fmtp:102 0-16 a=rtcp:4001 IN IP4 172.18.0.1 a=ssrc:2122978608 cname:0d505733158cdc19
Complete process Demo:
Environment description:
- Environment: docker + kamailio + freeswitch
- Local en0 NIC address: 192.168.31.188
- Kamailio IP: 172.18.0.188 Proxy port: 15060
- FreeSWITCH IP: 172.18.0.14
INVITE message call source 192.168.31.188 extension 10086 sends a session request to call extension 10087, note the call-id at this time: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I, save it for comparison later.
copyINVITE sip:10087@dev.xswitch.cn SIP/2.0 Record-Route: <sip:192.168.31.188:15060;lr=on;nat=yes> Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.1c537238ae43750e62a423ddf7fe078c.0 Via: SIP/2.0/UDP 172.18.0.1:61789;received=172.18.0.1;rport=61789;branch=z9hG4bKPjTu0TroBVkc2BuxTbvQ4ODaYdk83-ljf9 Max-Forwards: 69 From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: sip:10087@dev.xswitch.cn Contact: <sip:10086@172.18.0.1:61789;ob;alias=172.18.0.1~61789~1> Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 INVITE Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS Supported: replaces, 100rel, norefersub User-Agent: Telephone 1.5.2 Content-Type: application/sdp Content-Length: 471 P-SRC-IP: 172.18.0.1
100 Trying message:
freeswitch tells the client that it has received the request and is preparing to call the target 10087
Note that the 172.18.0.188 in the via header field here is the ip address of kam, because kam is responsible for the distribution of the dispatch agent here.
So far, the communication between 10086 and fs has been established. For the b2bua mode of fs, a channel has been generated. Next, freeswitch will generate another channel through the sip protocol.
copySIP/2.0 100 Trying Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.1c537238ae43750e62a423ddf7fe078c.0;received=172.18.0.188 From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: sip:10087@dev.xswitch.cn Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 INVITE User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Content-Length: 0
Generate another channel:
An invite message header appeared again, but the call-id changed to 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e. The ip address in the Via header field has also changed to the address 172.18.0.14 of the freeswtich container, indicating that fs is starting to call the target number.
copyINVITE sip:10087@dev.xswitch.cn;dest=extension SIP/2.0 Via: SIP/2.0/UDP 172.18.0.14:17060;rport;branch=z9hG4bK9FNpKQ6v9aSea Route: <sip:172.18.0.188:15060> Max-Forwards: 68 From: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ To: <sip:10087@dev.xswitch.cn;dest=extension> Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e CSeq: 58044591 INVITE Contact: <sip:mod_sofia@172.18.0.14:17060> User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY Supported: timer, path, replaces Allow-Events: talk, hold, conference, refer Content-Type: application/sdp Content-Disposition: session Content-Length: 248 P-SRC-IP: 172.18.0.1 X-FS-Session: c540ce3b-ef41-4cd8-b1ef-4081f678baa0 X-User-Fallback-Proxy: sip:192.168.7.8:5080;transport=tcp X-FS-Support: update_display,send_info Remote-Party-ID: "10086USER" <sip:10086@172.18.0.14>;party=calling;screen=yes;privacy=off
100 trying again:
Reply 10087 The target number is ready. Note that the reply at this time is the reply from the target end 10087 to freeswitch.
copySIP/2.0 100 trying -- your call is important to us Via: SIP/2.0/UDP 172.18.0.14:17060;rport=17060;branch=z9hG4bK9FNpKQ6v9aSea;received=172.18.0.14 From: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ To: <sip:10087@dev.xswitch.cn;dest=extension> Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e CSeq: 58044591 INVITE Server: kamailio (5.5.2 (x86_64/linux)) Content-Length: 0
183 progres s message:
This is the reply from freeswiitch to the 10086 client. At this time, 10087 rings and 10086 plays the ringback tone.
copySIP/2.0 183 Session Progress Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.1c537238ae43750e62a423ddf7fe078c.0;received=172.18.0.188 Via: SIP/2.0/UDP 172.18.0.1:61789;received=172.18.0.1;rport=61789;branch=z9hG4bKPjTu0TroBVkc2BuxTbvQ4ODaYdk83-ljf9 Record-Route: <sip:192.168.31.188:15060;lr=on;nat=yes> From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: <sip:10087@dev.xswitch.cn>;tag=5K0jvDUcF5QtB Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 INVITE Contact: <sip:10087@172.18.0.14:17060;transport=udp> User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Accept: application/sdp Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY Supported: timer, path, replaces Allow-Events: talk, hold, conference, refer Content-Type: application/sdp Content-Disposition: session Content-Length: 260 Remote-Party-ID: "10087" <sip:10087@dev.xswitch.cn>;party=calling;privacy=off;screen=no
200 OK message:
When 10087 picks up the phone to answer, reply 200 to freeswitch.
copyIP/2.0 200 OK Via: SIP/2.0/UDP 172.18.0.14:17060;rport=17060;received=172.18.0.14;branch=z9hG4bK9FNpKQ6v9aSea Record-Route: <sip:192.168.31.188:15060;transport=tcp;lr;r2=on;nat=yes> Record-Route: <sip:192.168.31.188:15060;lr;r2=on;nat=yes> Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e From: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ To: <sip:10087@dev.xswitch.cn;dest=extension>;tag=B5e4EsqcqeTGk30gdo1X0way6hjX8t04 CSeq: 58044591 INVITE Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS Contact: <sip:10087@172.18.0.1:57950;transport=TCP;ob;alias=172.18.0.1~57950~2> Supported: replaces, 100rel, norefersub Content-Type: application/sdp Content-Length: 311
ACK message:
After freeswitch receives 200, it replies to 10087 with an ack, indicating that it has been received, and then returns 200 to 10086.
copyACK sip:10087@172.18.0.1:57950;transport=TCP;ob;alias=172.18.0.1~57950~2 SIP/2.0 Via: SIP/2.0/UDP 172.18.0.14:17060;rport;branch=z9hG4bKaSeFNjQ06KF1N Route: <sip:192.168.31.188:15060;lr;r2=on;nat=yes> Route: <sip:192.168.31.188:15060;transport=tcp;lr;r2=on;nat=yes> Max-Forwards: 70 From: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ To: <sip:10087@dev.xswitch.cn;dest=extension>;tag=B5e4EsqcqeTGk30gdo1X0way6hjX8t04 Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e CSeq: 58044591 ACK Contact: <sip:mod_sofia@172.18.0.14:17060> Content-Length: 0
200 ok message sent back to the client:
copySIP/2.0 200 OK Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.1c537238ae43750e62a423ddf7fe078c.0;received=172.18.0.188 Via: SIP/2.0/UDP 172.18.0.1:61789;received=172.18.0.1;rport=61789;branch=z9hG4bKPjTu0TroBVkc2BuxTbvQ4ODaYdk83-ljf9 Record-Route: <sip:192.168.31.188:15060;lr=on;nat=yes> From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: <sip:10087@dev.xswitch.cn>;tag=5K0jvDUcF5QtB Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 INVITE Contact: <sip:10087@172.18.0.14:17060;transport=udp> User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY Supported: timer, path, replaces Allow-Events: talk, hold, conference, refer Session-Expires: 120;refresher=uas Content-Type: application/sdp Content-Disposition: session Content-Length: 260 Remote-Party-ID: "Outbound Call" <sip:10087@dev.xswitch.cn>;party=calling;privacy=off;screen=no
The client returns an ack message to freeswitch:
copyACK sip:10087@172.18.0.14:17060;transport=udp SIP/2.0 Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK986d.10b2bc120531367a8df6e82cbba0ef11.0 Via: SIP/2.0/UDP 172.18.0.1:61789;received=172.18.0.1;rport=61789;branch=z9hG4bKPjqRtG0BNHXz3lieovgGbagVMf1bgveYpB Max-Forwards: 69 From: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl To: sip:10087@dev.xswitch.cn;tag=5K0jvDUcF5QtB Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 10584 ACK Content-Length: 0
BYE message:
At this time, from is 10087. It can be seen that 10087, the called party, actively sends an on-hook request to freeswitch.
copyBYE sip:mod_sofia@172.18.0.14:17060 SIP/2.0 Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK71b.ab931f7decc319df14337c8822b5278b.0;i=3 Via: SIP/2.0/TCP 172.18.0.1:57950;received=172.18.0.1;rport=57950;branch=z9hG4bKPje2pt4x0eBwbYGx1ki8iKkZiCUhRD8oCD;alias Max-Forwards: 69 From: <sip:10087@dev.xswitch.cn;dest=extension>;tag=B5e4EsqcqeTGk30gdo1X0way6hjX8t04 To: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e CSeq: 16442 BYE User-Agent: Telephone 1.5.2 Content-Length: 0
After freeswitch receives the bye message, it replies with 200 and deletes the called channel.
copySIP/2.0 200 OK Via: SIP/2.0/UDP 192.168.31.188:15060;branch=z9hG4bK71b.ab931f7decc319df14337c8822b5278b.0;i=3;received=172.18.0.188 Via: SIP/2.0/TCP 172.18.0.1:57950;received=172.18.0.1;rport=57950;branch=z9hG4bKPje2pt4x0eBwbYGx1ki8iKkZiCUhRD8oCD;alias From: <sip:10087@dev.xswitch.cn;dest=extension>;tag=B5e4EsqcqeTGk30gdo1X0way6hjX8t04 To: "10086USER" <sip:10086@172.18.0.14>;tag=6vSBy8BgceeDQ Call-ID: 6e0d4d6e-1d00-4326-b82d-f2eb3bac236e CSeq: 16442 BYE User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY Supported: timer, path, replaces Content-Length: 0
At the same time, freeswitch will also return the bye to the original call, and at the same time, it will carry the reason for hanging up Reason:Q.850;cause=16;text="NORMAL_CLEARING"
copyBYE sip:10086@172.18.0.1:61789;ob;alias=172.18.0.1~61789~1 SIP/2.0 Via: SIP/2.0/UDP 172.18.0.14:17060;rport;branch=z9hG4bKB277pD833v5KH Route: <sip:192.168.31.188:15060;lr=on;nat=yes> Max-Forwards: 70 From: <sip:10087@dev.xswitch.cn>;tag=5K0jvDUcF5QtB To: "10086USER" <sip:10086@dev.xswitch.cn>;tag=xav30sssdGzvtGLUJV7uwjtvKVZra6nl Call-ID: 01YPOzp4pT.DDQs5VapOAu9EEy7kss3I CSeq: 58044593 BYE User-Agent: FreeSWITCH-mod_sofia/1.10.8-dev+git~20211222T004816Z~c7280c7e93~64bit Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY Supported: timer, path, replaces Reason: Q.850;cause=16;text="NORMAL_CLEARING" Content-Length: 0
After receiving the bye, the client replies 200 to freeswitch, and freeswitch hangs up the original channel.
So far, the entire SIP protocol process is completed.
Below, organize the whole process through a simple diagram:
