All pastes #792806 Raw Edit

Untitled

public text v1 · immutable
·published 2007-11-21 18:41 UTC
rendered paste body
########################################################################
# Request route 'main'
########################################################################
route[0]
{
	xlog("L_INFO", "Processing Route 0");
	force_rport(); # RFC3581 Places the rport parameter in the topmost via header
	if(msg:len > max_len)
	{
		xlog("L_INFO", "Message too big - M=$rm F=$fu T=$tu");
		sl_send_reply("513", "Message Too Big");
		exit;
	}
	if (!mf_process_maxfwd_header("10"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		xlog("L_INFO", "Too many hops - M=$rm F=$fu T=$tu");
		sl_send_reply("483", "Too Many Hops");
		exit;
	}
	# SIP Pings for NAT Traversal
	if (is_method("OPTIONS")) 
	{
		#xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		#xlog("L_INFO", "Sending 200 Response");
		sl_send_reply("200", "Hello from Mark Baker");
		exit;
	}
	# Presence Management
	if( is_method("PUBLISH|SUBSCRIBE"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		route(13);
	}	
	# If the message is not a register, handle the record route (paying attention to NAT)
	if(!is_method("REGISTER"))
	{
		if(nat_uac_test("19"))
		{
			record_route(";nat=yes");
		}
		else
		{
			record_route();
		}
	}
	# On Cancel and Bye messages, drop the audio session
	if(is_method("CANCEL") || is_method("BYE"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		unforce_rtp_proxy();
	}
	# Check for the "lr" parameter in the Route Header
	if(loose_route())
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		# Check if To header field uri contains tag parameter
		if(!has_totag())
		{	
			xlog("L_INFO", "Initial loose-routing rejected - M=$rm F=$fu T=$tu");
			sl_send_reply("403", "Initial Loose-Routing Rejected");
			exit;
		}
		if(nat_uac_test("19") || search("^Route:.*;nat=yes"))
		{
			fix_nated_contact();
			setbflag(6);
		}
		route(3);
	}
	if(is_method("REGISTER"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		route(2);
	}
	if(is_method("INVITE"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		route(4);
	}
	if(is_method("CANCEL") || is_method("ACK"))
	{
		xlog("L_INFO", "New request - M=$rm F=$fu T=$tu");
		route(10);
	}
	
	route(11);
}

########################################################################
# Request route 'stop-rtp-proxy'
########################################################################
route[1]
{
        xlog("L_INFO", "ROUTE 1");
	if(isflagset(22))
	{
		unforce_rtp_proxy();
	}
	
}

########################################################################
# Request route 'base-route-register'
########################################################################
route[2]
{
        xlog("L_INFO", "ROUTE 2");
	sl_send_reply("100", "Trying");
	if(!www_authorize("", "subscriber")) 
	{
		xlog("L_INFO", "Register authentication failed - M=$rm F=$fu T=$tu");
		www_challenge("", "0");
		exit;
	}
	if(!check_to()) 
	{
		xlog("L_INFO", "Spoofed To-URI detected - M=$rm F=$fu T=$tu");
		sl_send_reply("403", "Spoofed To-URI Detected");
		exit;
	}
	consume_credentials();
	if(!search("^Contact:[ ]*\*") && nat_uac_test("19")) 
	{
		fix_nated_register();
		setbflag(6);
	}
	if(!save("location")) 
	{
		xlog("L_ERR", "Saving contact failed - M=$rm F=$fu T=$tu");
		sl_reply_error();
		exit;
	}
	
	xlog("L_INFO", "Registration successful - M=$rm F=$fu T=$tu");
	exit;
	
}

########################################################################
# Request route 'base-outbound'
########################################################################
route[3]
{
	xlog("L_INFO", "ROUTE 3");
	if(isbflagset(6))
	{
		xlog("L_INFO", "B Flag 6 is set");
		if(!isflagset(22) && !search("^Content-Length:[ ]*0"))
		{
			xlog("L_INFO", "Setting Flag 22 and Forcing RTP Proxy");
			setflag(22);
			force_rtp_proxy();
		}		
		t_on_reply("2");
	}
	else
	{
		t_on_reply("1");
	}
	if(!isflagset(21))
	{
		xlog("L_INFO", "B Flag 6 is set");
		t_on_failure("2");
	}
	if(isflagset(29))
	{
		xlog("L_INFO", "Flag 29 is set");
		append_branch();
	}
	if(is_present_hf("Proxy-Authorization"))
	{
		xlog("L_INFO", "Consuming Credentials (Mmmm... Tasty!)");
		consume_credentials();
	}
	
	xlog("L_INFO", "Request leaving server, D-URI='$du' - M=$rm F=$fu");
	# no 100 (we already sent it) and no DNS blacklisting
	xlog("L_INFO", "Sending EDR Record to account for call ID=$ci\n");
	setflag(1); /* set for accounting EDR */
	if(!t_relay())
	{
		xlog("L_INFO", "Could Not t_relay");	
		sl_reply_error();
		if(is_method("INVITE") && isbflagset(6))
		{
			unforce_rtp_proxy();
		}
	}
	exit;
	
}

########################################################################
# Request route 'base-route-invite'
########################################################################
route[4]
{
    xlog("L_INFO", "ROUTE 4");
	sl_send_reply("100", "Trying");
    xlog("L_INFO", "Checking to see if INVITE is from a Trusted Peer...");
	if(allow_trusted())
	{
		xlog("L_INFO", "Call from trusted peer - M=$rm F=$fu T=$tu");
		if(!is_domain_local("$rd"))
		{
			
			xlog("L_INFO", "Rejecting peering attempt with non-local request domain - M=$rm F=$fu T=$tu");
			sl_send_reply("403", "Relaying Denied");
			exit;
		}
		setflag(23);
	}
	else
	{
	    xlog("L_INFO", "Call is NOT from a trusted Peer.");
		if(!proxy_authorize("", "subscriber")) 
		{
			xlog("L_INFO", "Proxy authentication failed - M=$rm F=$fu T=$tu");
			proxy_challenge("", "0");
			exit;
		}
		if(!check_from()) 
		{
			
			xlog("L_INFO", "Spoofed From-URI detected - M=$rm F=$fu T=$tu");
			sl_send_reply("403", "Spoofed From-URI Detected");
			exit;
		}
	}

	if(nat_uac_test("19")) 
	{
		fix_nated_contact();
		setbflag(6);
	}
	
	route(5);
}

########################################################################
# Request route 'invite-find-callee'
########################################################################
route[5]
{
    xlog("L_INFO", "ROUTE 5");
	if(!is_domain_local("$rd"))
	{
		setflag(20);
		route(7);
	}
	if(does_uri_exist())
	{
		xlog("L_INFO", "Callee is local - M=$rm F=$fu T=$tu");
		route(6);
	}
	else
	{
		xlog("L_INFO", "Callee is not local - M=$rm F=$fu T=$tu");
		route(7);
	}
	exit;
	
}

########################################################################
# Request route 'invite-to-internal'
########################################################################
route[6]
{
        xlog("L_INFO", "ROUTE 6");
	if(!lookup("location")) 
	{
		
		xlog("L_INFO", "Local user offline - M=$rm F=$fu T=$tu");
		sl_send_reply("404", "User Offline");
	}
	else
	{
		
		xlog("L_INFO", "Local user online - M=$rm F=$fu T=$tu");
		route(3);
	}
	exit;
	
}

########################################################################
# Request route 'invite-to-external'
########################################################################
route[7]
{
	xlog("L_INFO", "ROUTE 7");
	if(isflagset(20))
	{
		xlog("L_INFO", "Call to foreign domain - M=$rm F=$fu T=$tu");
		route(3);
		exit;
	}
	route(9);
	if(!isflagset(23))
	{
		# don't allow calls relaying from PSTN to PSTN, if not explicitely forwarded
		if(uri =~ "^sip:[0-9]+@")
		{
			xlog("L_INFO", "Executing CarrierRoute F=$fu T=$tu");
			# Use Route Domain 1, Hash based on Call ID
			if(!cr_rewrite_uri("1", "call_id")){
				sl_send_reply("403", "Not allowed");
			} else {
				# In case of failure, re-route the request
				t_on_failure("1");
				# Relay the request to the gateway
				setflag(1); /* set for accounting EDR */
				t_relay();
				exit;
			}						
		}
	}
	xlog("L_INFO", "Call to unknown user - M=$rm F=$fu T=$tu");
	sl_send_reply("404", "User Not Found");
	exit;
}

########################################################################
# Request route 'normalize-e164'
########################################################################
route[8]
{
    xlog("L_INFO", "ROUTE 8");
	# European numbering plans look like this:
	#   CC  = country code (i.e. 43 for Austria)
	#   NDC = national destination code (i.e. 1 for Vienna)
	#   SN  = subscriber number (i.e. 4001234)
	#  
	#       CC + NDC + SN, i.e. 4314001234
	#
	# Within the same CC+NDC, it can be omitted, so if
	# +4314001234 wants to call +4315002345, one can dial
	# just 5002345.
	#
	# Within the same CC, CC can be ommitted and a "0" is prefixed
	# to NDC, so if +4314001234 wants to call +4326003456, 
	# one can dial 026003456.
	#
	# For international calls, either "00" or + is prefixed, like
	# +49123456780 or 0049123456789.
	#
	avp_delete("$avp(s:orig_callee_user)/g");
	$avp(s:orig_callee_user) = $rU;
	
	if(uri =~ "^sip:(\+[1-9])?[0-9]+@")
	{
    	xlog("L_INFO", "looks like a PSTN number");		
		if(uri =~ "^sip:0[1-9][0-9]+@")
		{
	    	#xlog("L_INFO", "we have format 0+NDC+SN");
			strip(1);
			prefix("+1");
		}
		else if(uri =~ "^sip:00[1-9]+@")
		{
	    	#xlog("L_INFO", "we have format 00 + CC + NDC + SN");		
			strip(2);
			prefix("+");
		}
		else if(uri =~ "^sip:[0-9]+@")
		{
	    	#xlog("L_INFO", "Called Party number needs '+' added");		
			prefix("+");
		}
		else if(!uri =~ "^sip:\+[1-9][0-9]+@")
		{
	    	#xlog("L_INFO", "unknown format, maybe NDC wasn't added before? (URI is '$fu')");		
			#xlog("L_INFO", "Not normalized callee '$avp(s:orig_callee_user)' to E.164 format - M=$rm F=$fu T=$tu");
			return(-1);
		}
    	#xlog("L_INFO", "else we have '+' + CC + NDC + SN");		
		xlog("L_INFO", "Normalized callee '$avp(s:orig_callee_user)' to E.164 format '$rU'");
		return(1);
	}
	else
	{
		
		xlog("L_INFO", "Not normalized callee '$avp(s:orig_callee_user)' to E.164 format - M=$rm F=$fu T=$tu");
		return(-1);
	}
	
}

########################################################################
# Request route 'lookup-enum'
########################################################################
route[9]
{
    xlog("L_INFO", "ROUTE 9");
	route(8);
	if($rc == 1)
	{
		if(enum_query("e164.org."))
		{
			# TODO: do GW fallback (load gws, set failure-route)?
			
			xlog("L_INFO", "ENUM query succeeded - M=$rm F=$fu T=$tu");
			route(3);
			exit;
		}
		else
		{
			
			xlog("L_INFO", "ENUM query failed - M=$rm F=$fu T=$tu");
			# ENUM query failed, revert $rU
			avp_pushto("$ru/username", "$avp(s:orig_callee_user)");
		}
	}
	
}

########################################################################
# Request route 'base-route-local'
########################################################################
route[10]
{
        xlog("L_INFO", "ROUTE 10");
	t_on_reply("1");
	if(t_check_trans())
	{
		
		xlog("L_INFO", "Request leaving server - M=$rm F=$fu T=$tu");
		setflag(1); /* set for accounting EDR */
		if(!t_relay())
		{
			sl_reply_error();
		}
	}
	else
	{
		
		xlog("L_INFO", "Dropping mis-routed request - M=$rm F=$fu T=$tu");
	}
	exit;
	
}

########################################################################
# Request route 'base-route-generic'
########################################################################
route[11]
{
	xlog("L_INFO", "ROUTE 11");
	xlog("L_INFO", "Method not supported - M=$rm F=$fu T=$tu");
	sl_send_reply("501", "Method Not Supported Here");
	exit;
	
}

########################################################################
# Request route 'base-filter-failover'
########################################################################
route[12]
{
        xlog("L_INFO", "ROUTE 12");
	if(!t_check_status("408|500|503"))
	{
		
		xlog("L_INFO", "No failover routing needed for this response code - M=$rm F=$fu T=$tu");
		route(1);
		exit;
	}
	
}
########################################################################
# Presence Management
########################################################################
route[13]
{

	exit;	
}
########################################################################
# Reply route 'base-standard-reply'
########################################################################
onreply_route[1]
{
	xlog("L_INFO", "ON REPLY ROUTE 1");
	xlog("L_INFO", "Reply - S=$rs D=$rr F=$fu T=$tu");
	exit;
	
}

########################################################################
# Reply route 'base-nat-reply'
########################################################################
onreply_route[2]
{
	xlog("L_INFO", "ON REPLY ROUTE 2");
	xlog("L_INFO", "NAT-Reply - S=$rs D=$rr F=$fu T=$tu");
	if(nat_uac_test("1"))
	{
		fix_nated_contact();
	}
	if(isbflagset(6) && status=~"(180)|(183)|2[0-9][0-9]") 
	{
		if(!search("^Content-Length:[ ]*0"))
		{
			force_rtp_proxy();
		}
	}
	exit;
	
}

########################################################################
# Failure route 'pstn-failover'
########################################################################
failure_route[1]
{
	xlog("L_INFO", "FAILURE ROUTE 1");
	xlog("L_INFO", "Failure route 1 for PSTN entered - M=$rm F=$fu T=$tu");
	
	#if (t_check_status("408|5[0-9][0-9]|6[0-9][0-9]")) {
		#choose route domain 1 of the default carrier
		# Use Route Domain 2, Hash based on Call ID
		if(!cr_rewrite_uri("2", "call_id")){
			t_reply("403", "Not allowed");
		} else {
			t_on_failure("2");
			setflag(1); /* set for accounting EDR */
			t_relay();
		}
	#}
}

########################################################################
# Failure route 'base-standard-failure'
########################################################################
failure_route[2]
{
	xlog("L_INFO", "FAILURE ROUTE 2");
	xlog("L_INFO", "Failure route 2 for PSTN entered - M=$rm F=$fu T=$tu");
	
	#if (t_check_status("408|5[0-9][0-9]|6[0-9][0-9]")) {
		# Use Route Domain 2, Hash based on Call ID
		if(!cr_rewrite_uri("2", "call_id")){
			t_reply("403", "Not allowed");
		} else {
			t_on_failure("3");
			setflag(1); /* set for accounting EDR */
			t_relay();
		}
	#}
	#route(12);
	#route(1);
}

failure_route[3]
{
	xlog("L_INFO", "FAILURE ROUTE 2");
	xlog("L_INFO", "Failure route 2 for PSTN entered - M=$rm F=$fu T=$tu");
	
	#if (t_check_status("408|5[0-9][0-9]|6[0-9][0-9]")) {
		# Use Route Domain 3, Hash based on Call ID
		if(!cr_rewrite_uri("3", "call_id")){
			t_reply("403", "Not allowed");
		} else {
			t_reply("503", "All carriers tried - Call Failed");
		}
	#}
	#route(12);
	#route(1);
}