######################################################################
# Terminating, Initial requests
######################################################################
route[MT] {
    xnotice("PCSCF MT: \n Destination URI: $du\n Request URI: $ru\n");
    xnotice("Source IP and Port: ($si:$sp)\n Route-URI: $route_uri\n");
    xnotice("Received IP and Port: ($Ri:$Rp)\n");
    xnotice("Contact header: $ct\n");
    set_dlg_profile("term");
#!ifdef WITH_IPSEC
    ipsec_forward("location", IPSEC_FORWARD_FLAGS);
#!endif
	t_on_reply("MT_reply");
}

######################################################################
# Replies to Originating Initial Requests
######################################################################
onreply_route[MT_reply] {
    xnotice("PCSCF MT_reply: \n Destination URI: $du\n Request URI: $ru\n");
    xnotice("Source IP and Port: ($si:$sp)\n Route-URI: $route_uri\n");
    xnotice("Received IP and Port: ($Ri:$Rp)\n");
    xnotice("Contact header: $ct\n");
	if (!strempty($(ct{tobody.params}))) {
		append_hf("C-Params: $(ct{tobody.params})\r\n");
	}

	# In case of 1xx and 2xx do NAT
	if(status=~"[12][0-9][0-9]")
		route(NATMANAGE);
#!ifdef WITH_RX
        if (t_check_status("183|200") && has_body("application/sdp")){
        xnotice("PCSCF MT_reply: \n Destination URI: $du\n Request URI: $ru\n");
        xnotice("Source IP and Port: ($si:$sp)\n Route-URI: $route_uri\n");
        xnotice("Received IP and Port: ($Ri:$Rp)\n");
        xnotice("Contact header: $ct\n");
		xlog("L_DBG", "IMS: Received 183 inside term_initial_reply\n");	

		xlog("L_DBG", "About to test if this is a retransmitted reply which is still currently suspended\n");
		if (t_is_retr_async_reply()) {
	                xlog("L_DBG", "Dropping retransmitted reply which is still currently suspended\n");
       		         drop();
       		}

		xlog("L_DBG","Diameter: Term authorizing media via Rx\n");	
		$avp(FTAG_CUSTOM_AVP)=$ft;
		$avp(TTAG_CUSTOM_AVP)=$tt;
		$avp(CALLID_CUSTOM_AVP)=$ci;

        if (Rx_AAR("MT_aar_reply","term","",-1) == 0) {
            exit;
        }
	}
}

route[MT_aar_reply]
{
        xlog("L_DBG", "IMS: TERM_SESSION_AAR_REPLY\n");

        #this is async so to know status we have to check the reply avp        
	switch ($avp(s:aar_return_code)) {
                case 1:
                        xlog("L_DBG", "Diameter: Orig AAR success on media authorization\n"); 
                        break;
                default:
                        xlog("L_ERR", "IMS: AAR failed Orig\n");
                        xlog("L_ERR", "IMS: ttag: "+ "$avp(TTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: ftag: "+ "$avp(FTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: callid: "+ "$avp(CALLID_CUSTOM_AVP)");
                        #comment this if you want to allow even if Rx fails
                        if(dlg_get("$avp(CALLID_CUSTOM_AVP)","$avp(FTAG_CUSTOM_AVP)","$avp(TTAG_CUSTOM_AVP)")){
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;
                        }
        }
#!endif
}


######################################################################
# In-Dialog-MT-Requests
######################################################################
route[MT_indialog] {
    xnotice("PCSCF MT_indialog: \n Destination URI: $du\n Request URI: $ru\n");
    xnotice("Source IP and Port: ($si:$sp)\n Route-URI: $route_uri\n");
    xnotice("Received IP and Port: ($Ri:$Rp)\n");
    xnotice("Contact header: $ct\n");
    #resetflag(FLT_MOBILE_ORIG);
	t_on_reply("MT_indialog_reply");

    # Append rport only if its a request coming from UE
	if (is_request() && ($hdrc(Via) == 1)) {
		force_rport();
	}

#!ifdef WITH_IPSEC
    if ($dd != "" && $rd != "" && $fs != "") {
        if ($rd =~ ".*" + $dd + ".*") {
            ipsec_forward("location", IPSEC_FORWARD_FLAGS);
        }
    }
#!endif
}

onreply_route[MT_indialog_reply] {
    xnotice("PCSCF MT_indialog_reply: \n Destination URI: $du\n Request URI: $ru\n");
    xnotice("Source IP and Port: ($si:$sp)\n Route-URI: $route_uri\n");
    xnotice("Received IP and Port: ($Ri:$Rp)\n");
    xnotice("Contact header: $ct\n");
	# In case of 1xx and 2xx do NAT
	if(status=~"[12][0-9][0-9]")
		route(NATMANAGE);

#!ifdef WITH_RX
	if (t_check_status("183|200") && has_body("application/sdp") && !is_method("PRACK")) {
		if (t_is_retr_async_reply()) {
			xlog("L_DBG", "Dropping retransmitted reply which is still currently suspended\n");
			drop();
		}

		xlog("L_DBG", "IMS: TERM_SUBSEQUENT reply. This is a 200 OK to a re-INVITE\n");
		xlog("L_DBG","Diameter: Term authorizing media via Rx\n");      
		$avp(FTAG_CUSTOM_AVP)=$ft;
		$avp(TTAG_CUSTOM_AVP)=$tt;
		$avp(CALLID_CUSTOM_AVP)=$ci;

		if (Rx_AAR("MT_indialog_aar_reply","term","",-1) == 0) {
            exit;
        }
	}
}

route[MT_indialog_aar_reply]
{
        #this is async so to know status we have to check the reply avp
	switch ($avp(s:aar_return_code)) {
                case 1:
                        xlog("L_DBG", "Diameter: Orig AAR success on media authorization\n"); 
                        break;
                default:
                        xlog("L_ERR", "IMS: AAR failed Orig\n");
                        xlog("L_ERR", "IMS: ttag: "+ "$avp(TTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: ftag: "+ "$avp(FTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: callid: "+ "$avp(CALLID_CUSTOM_AVP)");
                        #comment this if you want to allow even if Rx fails
                        if(dlg_get("$avp(CALLID_CUSTOM_AVP)","$avp(FTAG_CUSTOM_AVP)","$avp(TTAG_CUSTOM_AVP)")){
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;
                        }
        }
#!endif
}
