<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [

<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
%docentities;

]>
<!-- Module User's Guide -->

<chapter>
	
	<title>&adminguide;</title>


	<section>
		<title>Overview</title>
		<para>
		UAC REDIRECT - User Agent Client redirection - module enhance &kamailio;
		with the functionality of being able to handle (interpret, filter,
		log and follow) redirect responses ( 3xx replies class).
		</para>
		<para>
		UAC REDIRECT module offer stateful processing, gathering the
		contacts from all 3xx branches of a call.
		</para>
		<para>
		The module provide a powerful mechanism for selecting and filtering 
		the contacts to be used for the new redirect:
		</para>
		<itemizedlist>
			<listitem>
			<para><emphasis>number based</emphasis> - limits like the 
			number of total contacts to be used or the maximum number of 
			contacts per branch to be selected.
			</para>
			</listitem>
			<listitem>
			<para><emphasis>Regular Expression based</emphasis> - combinations
			of deny and accept filters allow a strict control of the 
			contacts to be used for redirection.
			</para>
			</listitem>
		</itemizedlist>
		<para>
		When selecting from a 3xx branch the contacts to be used, the contacts
		will be ordered and prioritized based on the <quote>q</quote> value.
		</para>
	</section>

	<section>
		<title>Accounting</title>
		<para>
		UAC REDIRECT module allows to log all the redirection (to be later
		used for CDR aggregation). This functionality may be dynamically
		enabled for each redirection situation.
		</para>
		<para>
		The logging will be done via the accounting module functions (all are
		supported). The information to be logged will be the same as the 
		normal logged information directly via ACC module, but with 
		following differences:
		</para>
		<itemizedlist>
			<listitem>
			<para><emphasis>reason phrase</emphasis> - which will be
			dynamically set by the redirection function;
			</para>
			</listitem>
			<listitem>
			<para><emphasis>outgoing URI</emphasis> - which will be the
			redirect URI.
			</para>
			</listitem>
		</itemizedlist>
		<para>
		For each redirect contact, a separate record will be logged. For
		example, if a call is redirected to three new contacts, the 
		module will log three additional records corresponding to each
		redirect URI.
		</para>
	</section>


	<section>
		<title>Dependencies</title>
		<section>
			<title>&kamailio; Modules</title>
			<para>
			The following modules must be loaded before this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>TM</emphasis> - Transaction Module, for accessing
				replies.
			</para>
			</listitem>
			<listitem>
			<para>
				<emphasis>ACC</emphasis> - Accounting Module, but only if the
				logging feature is used.
			</para>
			</listitem>
			</itemizedlist>
			</para>
		</section>
		<section>
			<title>External Libraries or Applications</title>
			<para>
				The following libraries or applications must be installed 
				before running &kamailio; with this module loaded:
				<itemizedlist>
				<listitem>
				<para>
					<emphasis>None</emphasis>
				</para>
				</listitem>
				</itemizedlist>
			</para>
		</section>
	</section>

	<section>
		<title>Parameters</title>
		<section>
			<title><varname>default_filter</varname> (string)</title>
			<para>
			The default behavior in filtering contacts. It may be 
			<quote>accept</quote> or <quote>deny</quote>.
			</para>
			<para>
				<emphasis>
					The default value is <quote>accept</quote>.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>default_filter</varname> 
					module parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","default_filter","deny")
...
				</programlisting>
			</example>
		</section>
		<section>
			<title><varname>deny_filter</varname> (string)</title>
			<para>
			The regular expression for default deny filtering. It make sens
			to be defined on only if the <varname>default_filter</varname>
			parameter is set to <quote>accept</quote>. All contacts matching
			the <varname>deny_filter</varname> will be rejected; the rest 
			of them will be accepted for redirection.
			</para>
			<para>
			The parameter may be defined only one - multiple definition will
			overwrite the previous definitions. If more regular expression 
			need to be defined, use the 
			<function moreinfo="none">set_deny_filter()</function> scripting
			function.
			</para>
			<para>
				<emphasis>
					This parameter is optional, it's default 
					value being NULL.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>deny_filter</varname> 
					module parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","deny_filter",".*@siphub\.net")
...
				</programlisting>
			</example>
		</section>
		<section>
			<title><varname>accept_filter</varname> (string)</title>
			<para>
			The regular expression for default accept filtering. It make sens
			to be defined on only if the <varname>default_filter</varname>
			parameter is set to <quote>deny</quote>. All contacts matching
			the <varname>accept_filter</varname> will be accepted; the rest 
			of them will be rejected for redirection.
			</para>
			<para>
			The parameter may be defined only one - multiple definition will
			overwrite the previous definitions. If more regular expression 
			need to be defined, use the 
			<function moreinfo="none">set_accept_filter()</function> scripting
			function.
			</para>
			<para>
				<emphasis>
					This parameter is optional, it's default 
					value being NULL.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>accept_filter</varname> 
					module parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","accept_filter",".*@siphub\.net")
...
				</programlisting>
			</example>
		</section>
		<section>
			<title><varname>acc_function</varname> (string)</title>
			<para>
			Specifies the accounting function to be used. Just be defining 
			this parameter, the accounting support will not be enabled. 
			Accounting may only be enabled via two parameters 
			<function moreinfo="none">set_accept_filter()</function> 
			scripting function.
			</para>
			<para>
			Its values my be:
			</para>
			<itemizedlist>
				<listitem>
				<para><emphasis>acc_log_request</emphasis></para>
				</listitem>
				<listitem>
				<para><emphasis>acc_db_request</emphasis></para>
				</listitem>
				<listitem>
				<para><emphasis>acc_rad_request</emphasis></para>
				</listitem>
				<listitem>
				<para><emphasis>acc_diam_request</emphasis></para>
				</listitem>
			</itemizedlist>
			<para>
				<emphasis>
					The default value is <quote>acc_log_request</quote>.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>acc_function</varname> parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","acc_function","acc_db_request")
...
				</programlisting>
			</example>
		</section>
		<section>
			<title><varname>acc_db_table</varname> (string)</title>
			<para>
			Specifies the accounting table to be used if DB accounting was 
			chosen (<varname>acc_function</varname> was set to 
			<quote>acc_db_request</quote>). Just be defining 
			this parameter, the accounting support will not be enabled. 
			Accounting may only be enabled via two parameters 
			<function moreinfo="none">set_accept_filter()</function> 
			scripting function.
			</para>
			<para>
				<emphasis>
					The default value is <quote>acc</quote>.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>acc_db_table</varname> parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","acc_db_table","acc_redirect")
...
				</programlisting>
			</example>
		</section>
		<section>
			<title><varname>bflags</varname> (int)</title>
			<para>
			This parameter defines the branch-flags to be set for new, added branch.
			</para>
			<para>
				<emphasis>
					This parameter is optional, it's default 
					value being 0.
				</emphasis>
			</para>
			<example>
				<title>Set <varname>bflags</varname> 
					module parameter</title>
				<programlisting format="linespecific">
...
modparam("uac_redirect","bflags", 1)
...

branch_route[1] {
	if (isbflagset(1)) {
		log(1, "This branch comes from a 302 Forward Request.\n");
	} else {
		log(1, "This is a regular branch.\n");
	}
}
				</programlisting>
			</example>
		</section>
	</section>

	<section>
		<title>Functions</title>
		<section>
			<title>
				<function moreinfo="none">set_deny_filter(filter,flags)
					</function>
			</title>
			<para>
			Sets additional deny filters. Maximum 6 may be combined. This
			additional filter will apply only to the current message - it
			will not have a global effect.
			</para>
			<para>
			Default or previous added deny filter may be reset depending of
			the <emphasis>flag</emphasis> parameter value:
			</para>
			<itemizedlist>
				<listitem>
				<para><emphasis>reset_all</emphasis> - reset both default
				and previous added deny filters;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>reset_default</emphasis> - reset only the
				default deny filter;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>reset_added</emphasis> - reset only the 
				previous added deny filters;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>empty</emphasis> - no reset, just add the
				filter.
				</para>
				</listitem>
			</itemizedlist>
			<para>
			This function can be used from FAILURE_ROUTE.
			</para>
			<example>
				<title><function>set_deny_filter</function> usage</title>
				<programlisting format="linespecific">
...
set_deny_filter(".*@domain2.net","reset_all");
set_deny_filter(".*@domain1.net","");
...
				</programlisting>
			</example>
		</section>

		<section>
			<title>
				<function moreinfo="none">set_accept_filter(filter,flags)
					</function>
			</title>
			<para>
			Sets additional accept filters. Maximum 6 may be combined. This
			additional filter will apply only to the current message - it
			will not have a global effect.
			</para>
			<para>
			Default or previous added deny filter may be reset depending of
			the <emphasis>flag</emphasis> parameter value:
			</para>
			<itemizedlist>
				<listitem>
				<para><emphasis>reset_all</emphasis> - reset both default
				and previous added accept filters;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>reset_default</emphasis> - reset only the
				default accept filter;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>reset_added</emphasis> - reset only the 
				previous added accept filters;
				</para>
				</listitem>
				<listitem>
				<para><emphasis>empty</emphasis> - no reset, just add
				the filter.
				</para>
				</listitem>
			</itemizedlist>
			<para>
			This function can be used from FAILURE_ROUTE.
			</para>
			<example>
				<title><function>set_accept_filter</function> usage</title>
				<programlisting format="linespecific">
...
set_accept_filter(".*@domain2.net","reset_added");
set_accept_filter(".*@domain1.net","");
...
				</programlisting>
			</example>
		</section>

		<section>
			<title>
				<function moreinfo="none">get_redirects(max)</function>
			</title>
			<para>
				The function may be called only from failure routes. It will
				extract the contacts from all 3xx branches and append them
				as new branches. Note that the function will not forward the
				new branches, this must be done explicitly from script.
			</para>
			<para>
				How many contacts (in total and per branch) are selected 
				depends of the <emphasis>max</emphasis> parameter values.
				Its syntax is:
			</para>
			<itemizedlist>
				<listitem><para>
				max = max_total [":" max_branch]
				</para></listitem>
				<listitem><para>
				max_total = number of total contacts to be selected
				</para></listitem>
				<listitem><para>
				max_branch = number of contacts per branch to be selected
				</para></listitem>
			</itemizedlist>
			<para>
				Both <quote>max_total</quote> and <quote>max_branch</quote>
				are positive integer. To specify unlimited values, use 0 value
				or "*" character.
			</para>
			<para>
				NOTE that during the selection process, each set of contacts 
				from a specific branch are ordered based on <quote>q</quote> 
				value.
			</para>
			<para>
				This function will produce no accounting records.
			</para>
			<para>
				This function can be used from FAILURE_ROUTE.
			</para>
			<example>
				<title><function>get_redirects</function> usage</title>
				<programlisting format="linespecific">
...
# max 2 contacts per branch, but no overall limit
get_redirects("*:2");
...
# no limits per branch, but not more than 6 overall contacts
get_redirects("6:*");
...
# no restrictions
get_redirects("*");
...
				</programlisting>
			</example>
		</section>

		<section>
			<title>
				<function moreinfo="none">get_redirects(max,reason)</function>
			</title>
			<para>
				The function has same functionality as 
				<function moreinfo="none">get_redirects(max)</function>
				function, but it will produce accounting records.
			</para>
			<para>
				The accounting records will be mark by the 
				<emphasis>reason</emphasis> phrase.
			</para>
			<para>
				If this function appears in the script, at startup, the module
				will import the accounting function. Otherwise not.
			</para>
			<para>
			This function can be used from FAILURE_ROUTE.
			</para>
			<example>
				<title><function>get_redirects</function> usage</title>
				<programlisting format="linespecific">
...
get_redirects("4:1","Redirected");
...
				</programlisting>
			</example>
		</section>
	</section>

	<section>
		<title>Script Example</title>
		<example>
			<title>Redirection script example</title>
				<programlisting format="linespecific">
loadmodule "modules/sl/sl.so"
loadmodule "modules/usrloc/usrloc.so"
loadmodule "modules/registrar/registrar.so"
loadmodule "modules/tm/tm.so"
loadmodule "modules/acc/acc.so"
loadmodule "modules/uac_redirect/uac_redirect.so"

modparam("usrloc", "db_mode",   0)

route{
	if (uri==myself) {

		if (method=="REGISTER") {
			save("location");
			exit;
		};

		if (!lookup("location")) {
			sl_send_reply("404", "Not Found");
			exit;
		};
		# do redirect with accounting
		t_on_failure("2");
	} else {
		# just do redirect
		t_on_failure("1");
	}

	if (!t_relay()) {
		sl_reply_error();
	};
}

failure_route[1] {
	get_redirects("3:1");
	t_relay();
}

failure_route[2] {
	get_redirects("6:2","redirect");
	t_relay();
}
				</programlisting>
			</example>

	</section>



</chapter>

