////////////////////////////////////////////////////////////////////////////////
// Server: registrar and call broker (back-to-back user agent)
//
// No counterpart in this directory: it needs a caller that registers and calls
// it, and a callee. The callee's address is the variable "calleeAddr", set
// right after the INVITE arrives (192.168.1.154:5108 in the file); change it
// for your network.
//
// It accepts the registration, and for an incoming call it saves the caller's
// SDP, address, CSeq, Via, From, To and Call-ID, answers 100 and 180 at once and calls the callee
// with the caller's SDP. When the callee answers, the 200 OK is passed back to
// the caller with the callee's SDP (in the caller's dialog: its From, To and
// Call-ID), and the caller's BYE is passed on with the callee's tagged To.
//
// Messages with ;retrans are sent again over UDP until they are answered, and a copy
// of one that comes again is answered as before (RFC 3261; -t1, -t2).
//
// SIP Messages
//  <--- REGISTER ;retrans
//  ---> 200
//  (new dialog)
//  <--- INVITE ;retrans (from the caller)
//  ---> 100 Trying, 180 Ringing (to the caller)
//  ---> INVITE ;retrans (to the callee, with the caller's SDP)
//  <--- 100, 180, 200 OK (from the callee)
//  ---> ACK (to the callee)
//  ---> 200 OK ;retrans (to the caller, with the callee's SDP)
//  <--- ACK
//  <--- BYE ;retrans (from the caller)
//  ---> 200 OK
//  ---> BYE ;retrans (to the callee)
//  <--- 200 OK
////////////////////////////////////////////////////////////////////////////////

<----------;retrans
REGISTER

---------->
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:];tag=[call_number]
[last_Call-ID:]
[last_CSeq:]
[last_Contact:];expires=3600
Expires: 3600
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


----------
cmd_NewDialog

<----------;retrans
INVITE

----------
sdp1=save(SDP)

----------
rem1=save(REMOTE_IP_PORT)

// Setting: where the callee is
----------
set_Variable=calleeAddr "-" "=192.168.1.154:5108"

----------
set_Variable=cseqVal "CSeq:" "* "

----------
set_Variable=viaVal "Via:" "*"

----------
set_Variable=aFrom "From:" "*"

----------
set_Variable=aTo "To:" "*"

----------
set_Variable=aCid "Call-ID:" "*"

---------->
SIP/2.0 100 Trying
[last_Via:]
[last_From:]
[last_To:]
[last_Call-ID:]
[last_CSeq:]
User-Agent: SIPInspector_v_[ver]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Length: 0


---------->
SIP/2.0 180 Ringing
[last_Via:]
[last_From:]
[last_To:];tag=[call_number]
[last_Call-ID:]
[last_CSeq:]
User-Agent: SIPInspector_v_[ver]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Length: 0


----------
cmd_setRemote=[$calleeAddr]

---------->;retrans
INVITE sip:[toUsername]@[remote_ip]:[remote_port];transport=[transport] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
[last_From:]
[last_To:]
Call-ID: [call_number].[run_tag]
CSeq: [$cseqVal] INVITE
Max-Forwards: 70
Contact: <sip:[fromUsername]@[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Type: application/sdp
Content-Length: [len]

[$sdp1]

<----------
100 Trying

<----------
180 Ringing

<----------;retrans
200 OK

----------
sdp2=save(SDP)

----------
set_Variable=bTo "To:" "*"

---------->
ACK sip:[toUsername]@[remote_ip]:[remote_port];transport=[transport] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]abc
[last_From:]
[last_To:]
Call-ID: [call_number].[run_tag]
CSeq: [$cseqVal] ACK
Max-Forwards: 70
Contact: <sip:[fromUsername]@[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


----------
cmd_setRemote=[$rem1]

---------->;retrans
SIP/2.0 200 OK
Via: [$viaVal]
From: [$aFrom]
To: [$aTo];tag=[call_number]
Call-ID: [$aCid]
CSeq: [$cseqVal] INVITE
User-Agent: SIPInspector_v_[ver]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Type: application/sdp
Content-Length: [len]

[$sdp2]

<----------
ACK

<----------;retrans
BYE

---------->
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:]
[last_Call-ID:]
[last_CSeq:]
User-Agent: SIPInspector_v_[ver]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Content-Length: 0


----------
cmd_setRemote=[$calleeAddr]

---------->;retrans
BYE sip:[toUsername]@[remote_ip]:[remote_port];transport=[transport] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: [$aFrom]
To: [$bTo]
Call-ID: [call_number].[run_tag]
CSeq: [cseq+1] BYE
Max-Forwards: 70
Contact: <sip:[fromUsername]@[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


<----------
200 OK
