////////////////////////////////////////////////////////////////////////////////
// Server: the transferee of a blind transfer (RFC 3515)
//
// Counterpart of Call_transfer_client.
//
// It answers a call, is asked by a REFER to call somebody else, makes that call
// (the REFER target answers it), reports the progress with NOTIFY and ends the
// call it made.
//
// 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, call A <-> B
//  <--- INVITE ;retrans
//  ---> 100 Trying
//  ---> 180 Ringing
//  ---> 200 OK ;retrans (SDP)
//  <--- ACK
//  <--- REFER ;retrans   (remember the Refer-To address and the dialog identifiers)
//  ---> 202 Accepted
//  ---> NOTIFY ;retrans  (sipfrag: 100 Trying)
//  <--- 200 OK
//
// SIP Messages, call B <-> C (to the address in Refer-To)
//  ---- cmd_setRemote = Refer-To address
//  ---> INVITE ;retrans  (new Call-ID, Referred-By taken from the REFER)
//  <--- 100 Trying (optional)
//  <--- 180 Ringing (optional)
//  <--- 200 OK ;retrans
//  ---> ACK
//
// SIP Messages, call B <-> C is ended
//  ---- pause, cmd_setRemote = Refer-To address
//  ---> BYE ;retrans
//  <--- 200 OK
//
// SIP Messages, call A <-> B again (the Call-ID of the first call is used last, see below)
//  ---- cmd_setRemote = back to the transferor
//  ---> NOTIFY ;retrans  (sipfrag: 200 OK, subscription terminated)
//  <--- 200 OK
//  <--- BYE ;retrans     (the transferor ends its call)
//  ---> 200 OK
////////////////////////////////////////////////////////////////////////////////

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

---------->
SIP/2.0 100 Trying
[last_Via:]
[last_From:]
[last_To:]
[last_Call-ID:]
[last_CSeq:]
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


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


---------->;retrans
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:];tag=[call_number]
[last_Call-ID:]
[last_CSeq:]
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Type: application/sdp
Content-Length: [len]

v=0
o=111 843670094 843670094 IN [IP_ver] [local_ip_sdp]
s=-
c=IN [IP_ver] [local_ip_sdp]
t=0 0
a=sendrecv
m=audio [media_port] RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=ptime:20

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

<----------;retrans
REFER

// Everything the later messages need is taken from the REFER, before anything is sent.
// Dialog with the transferor: its Call-ID, its From (with tag), our To (with tag), its Contact and address.
----------
set_Variable=aCid "Call-ID:" "*"

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

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

----------
set_Variable=aContact "Contact:" "<*>"

----------
set_Variable=aHost "Contact:" "@*;"

// The transfer target: Refer-To URI and its address; and who asked for the transfer.
----------
set_Variable=xferUri "Refer-To:" "<*>"

----------
set_Variable=xferHost "Refer-To:" "@*>"

----------
set_Variable=referredBy "Referred-By:" "*"

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


---------->;retrans
NOTIFY [$aContact] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: [$aTo]
To: [$aFrom]
Call-ID: [$aCid]
CSeq: [cseq+1] NOTIFY
Max-Forwards: 70
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Event: refer
Subscription-State: active;expires=60
Content-Type: message/sipfrag;version=2.0
User-Agent: SIPInspector_v_[ver]
Content-Length: [len]

SIP/2.0 100 Trying

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

// Call the transfer target
----------
cmd_setRemote=[$xferHost]

---------->;retrans
INVITE [$xferUri] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: <sip:transferee@[local_ip]:[local_port]>;tag=[call_number]x
To: <[$xferUri]>
Call-ID: xfer-[call_number].[run_tag]@[local_ip]
CSeq: [cseq+1] INVITE
Max-Forwards: 70
Contact: <sip:transferee@[local_ip]:[local_port];transport=[transport]>
Referred-By: [$referredBy]
User-Agent: SIPInspector_v_[ver]
Content-Type: application/sdp
Content-Length: [len]

v=0
o=111 843670094 843670094 IN [IP_ver] [local_ip_sdp]
s=-
c=IN [IP_ver] [local_ip_sdp]
t=0 0
a=sendrecv
m=audio [media_port] RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=ptime:20

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

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

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

// The To-tag of the new call; [peer_tag_param] would later point at the other dialog.
----------
set_Variable=xferTag "To:" "tag=*"

---------->
ACK [$xferUri] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: <sip:transferee@[local_ip]:[local_port]>;tag=[call_number]x
To: <[$xferUri]>;tag=[$xferTag]
Call-ID: xfer-[call_number].[run_tag]@[local_ip]
CSeq: [cseq] ACK
Max-Forwards: 70
Contact: <sip:transferee@[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


// End the call to the transfer target
----------
cmd_Pause=500

----------
cmd_setRemote=[$xferHost]

---------->;retrans
BYE [$xferUri] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: <sip:transferee@[local_ip]:[local_port]>;tag=[call_number]x
To: <[$xferUri]>;tag=[$xferTag]
Call-ID: xfer-[call_number].[run_tag]@[local_ip]
CSeq: [cseq+1] BYE
Max-Forwards: 70
Contact: <sip:transferee@[local_ip]:[local_port];transport=[transport]>
User-Agent: SIPInspector_v_[ver]
Content-Length: 0


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

// Tell the transferor that the transfer worked. This has to come last: a scenario call only receives messages
// for the Call-ID it used last, so it goes back to the transferor's dialog (Call-ID) after it is done with the
// call to the target.
----------
cmd_setRemote=[$aHost]

---------->;retrans
NOTIFY [$aContact] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: [$aTo]
To: [$aFrom]
Call-ID: [$aCid]
CSeq: [cseq+1] NOTIFY
Max-Forwards: 70
Contact: <sip:[local_ip]:[local_port];transport=[transport]>
Event: refer
Subscription-State: terminated;reason=noresource
Content-Type: message/sipfrag;version=2.0
User-Agent: SIPInspector_v_[ver]
Content-Length: [len]

SIP/2.0 200 OK

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

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

---------->
SIP/2.0 200 OK
[last_Via:]
[last_From:]
[last_To:]
[last_Call-ID:]
[last_CSeq:]
User-Agent: SIPInspector_v_[ver]
Content-Length: 0

