<?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>
		The SIPtrace module offer a possibility to store incoming and outgoing SIP
		messages in a database and/or duplicate to the capturing server (using <acronym>HEP</acronym>
		the Homer encapsulation protocol or plain SIP mode)
	</para>
	<para>
		There are two ways of storing information:
		<itemizedlist>
		<listitem>
		<para>
		by calling the sip_trace() method explicitely in the &kamailio; configuration
		file. In this case the original message is processed.
		</para>
		</listitem>
		<listitem>
		<para>
		by setting the flag equal with the value of <quote>trace_flag</quote> (e.g.,
		setflag(__trace_flag__)) parameter of the module. In this case, the
		message sent forward is processed. The logging mechanism is based on
		TM/SL callbacks, so only messages processed with the TM module or sent statelessly
		are logged.
		</para>
		</listitem>
		</itemizedlist>
	</para>

	<para>
	The tracing can be turned on/off using Kamailio <acronym>mi</acronym> or <acronym>RPC</acronym>
	commands.
	</para>
	<para>
	&ctltool; fifo sip_trace on
	</para>
	<para>
	&ctltool; fifo sip_trace off
	</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>A database module</emphasis> - Mysql, Postgres,
				dbtext, unixODBC...
			</para>
			</listitem>
			<listitem>
			<para>
				<emphasis>tm and sl modules</emphasis> - optional, only if
				you want to trace messages forwarded by these modules.
			</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 id="siptrace.p.db_url">
		<title><varname>db_url</varname> (str)</title>
		<para>
		Database URL.
		</para>
		<para>
		<emphasis>
			Default value is "mysql://kamailio:kamailiorw@localhost/okamailio".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>db_url</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "db_url", "mysql://user:passwd@host/dbname")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.table">
		<title><varname>table</varname> (str)</title>
		<para>
		Name of the table where to store the SIP messages.
		</para>
		<para>
		<emphasis>
			Default value is <quote>sip_trace</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>sip_trace</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "table", "strace")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_flag">
		<title><varname>trace_flag</varname> (integer)</title>
		<para>
		Which flag is used to mark messages to trace without
		traced user.
		</para>
		<para>
		<emphasis>
			Default value is "0".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_flag</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_flag", 22)
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_on">
		<title><varname>trace_on</varname> (integer)</title>
		<para>
		Parameter to enable/disable trace (on(1)/off(0))
		</para>
		<para>
		<emphasis>
			Default value is "0".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_on</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_on", 1)
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.traced_user_avp">
		<title><varname>traced_user_avp</varname> (str)</title>
		<para>
		The name of the <acronym>AVP</acronym> storing the SIP URI of the traced user. If
		the AVP is set, messages are stored in database table and
		the <quote>traced_user</quote> column is filled with AVP's value. You can store
		the message many times for many users by having multiple values
		for this AVP.
		</para>
		<para>
		<emphasis>
			Default value is "NULL" (feature disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>traced_user_avp</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "traced_user_avp", "$avp(i:123)")
modparam("siptrace", "traced_user_avp", "$avp(s:user)")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_table_avp">
		<title><varname>trace_table_avp</varname> (str)</title>
		<para>
		The name of the AVP storing the name of the table where to
		store the SIP messages. If it is not set, the value of
		<quote>table</quote> parameter is used. In this way one can select
		dynamically where to store the traced messages. The table
		must exists, and must have the same structure as the <quote>sip_trace</quote>
		table.
		</para>
		<para>
		<emphasis>
			Default value is "NULL" (feature disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_table_avp</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_table_avp", "$avp(i:345)")
modparam("siptrace", "trace_table_avp", "$avp(s:siptrace_table)")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.duplicate_uri">
		<title><varname>duplicate_uri</varname> (str)</title>
		<para>
		The address in form of a SIP URI where to send a duplicate
		of traced message. It uses UDP all the time.
		</para>
		<para>
		<emphasis>
			Default value is "NULL".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>duplicate_uri</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "duplicate_uri", "sip:10.1.1.1:5888")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_to_database">
		<title><varname>trace_to_database</varname> (integer)</title>
		<para>
		Parameter to enable/disable inserts to the database from this
		&kamailio;. 
		</para>
		<para>
		In case we only want to send the SIP messages to the 
		<quote>duplicate_uri</quote> and not store the information to the local 
		database we can set this to "0".  
		</para>
		<para>
		<emphasis>
			Default value is "1".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_to_database</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_to_database", 0)
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_local_ip">
		<title><varname>trace_local_ip</varname> (str)</title>
		<para>
		The address to be used in <quote>fromip</quote> field for
		locally generated messages. If not set, the module sets it to the address
		of the socket that will be used to send the message.
		</para>
		<para>
		<emphasis>
			Default value is "NULL".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_local_ip</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_local_ip", "10.1.1.1:5064")
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.trace_sl_acks">
		<title><varname>trace_sl_acks</varname> (integer)</title>
		<para>
		Parameter to enable/disable tracing of SL-filtered ACKs (on=1
		/ off=0).
		</para>
		<para>
		By default all ACKs to replies generated by SL module are traced. There
		is no way to select among them. The SL module is able to run an event
		route when such an ACK arrives (<emphasis>event_route[sl:filtered-ack]</emphasis>).
		You can set this parameter to 0 and then execute sip_trace() in the event route,
		accompanied by config rules to decide which ACK to trace.
		</para>
		<para>
		<emphasis>
			Default value is "1".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_sl_acks</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "trace_sl_acks", 0)
...
</programlisting>
		</example>
	</section>
	 <section id="siptrace.p.xheaders_write">
		<title><varname>xheaders_write</varname> (integer)</title>
		<para>
		Parameter to enable/disable writing of x-headers.
		</para>
		<para>
		Stores <quote>fromip</quote>, <quote>toip</quote>, <quote>method</quote> and
		<quote>direction</quote> in <quote>X-Siptrace-*</quote> headers.
		This allows to transmit them to a second &kamailio; server
		using the <quote>duplicate_uri</quote> feature.
		Because the headers are added after the data is written to the database,
		the headers only show up in the packets sent by duplicate_uri.
		</para>
		<para>
		See <varname>xheaders_read</varname>, it should be used on the receiving
		side.
		</para>
		<para>
		<emphasis>Note:</emphasis> The headers are first read, then written. This allows 
		relaying the information over more than two &kamailio; servers by setting both
		<varname>xheaders_write</varname> and <varname>xheaders_read</varname>
		to "1" on the servers in the middle.
		</para>
		<para>
		<emphasis>
			Default value is "0".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>xheaders_write</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "xheaders_write", 0)
...
</programlisting>
		</example>
	</section>
	 <section id="siptrace.p.xheaders_read">
		<title><varname>xheaders_read</varname> (integer)</title>
		<para>
		Parameter to enable/disable reading of x-headers.
		</para>
		<para>
		Reads and removes the <quote>X-Siptrace-*</quote> headers. Packets not containing the
		headers are neither stored to the database nor relayed (duplicate_uri).
		See <varname>xheaders_write</varname> for further information.
		</para>
		<para>
		<emphasis>
			Default value is "0".
		</emphasis>
		</para>
		<example>
		<title>Set <varname>xheaders_read</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("siptrace", "xheaders_read", 0)
...
</programlisting>
		</example>
	</section>
	<section id="siptrace.p.hep_mode_on">
                <title><varname>hep_mode_on</varname> (integer)</title>
                <para>
                Parameter to enable/disable Homer encapsulate mode (on(1)/off(0))
                </para>
                <para>
                <emphasis>
                        Default value is "0".
                </emphasis>
                </para>
                <example>
                <title>Set <varname>hep_mode_on</varname> parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "hep_mode_on", 1)
...
</programlisting>
                </example>
        </section>
        
	<section id="siptrace.p.hep_version">
                <title><varname>hep_version</varname> (integer)</title>
                <para>
                The parameter indicate the version of the HEP protocol.
                Can be <quote>1</quote> or <quote>2</quote>. In HEPv2 the timestamp and capture agent ID
                will be included to HEP header.
                </para>
                <para>
                <emphasis>
                        Default value is "1".
                </emphasis>
                </para>
                <example>
                <title>Set <varname>hep_version</varname> parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "hep_version", 2)
...
</programlisting>
                </example>
        </section>
	<section id="siptrace.p.hep_capture_id">
                <title><varname>hep_capture_id</varname> (integer)</title>
                <para>
                The parameter indicate the capture agent ID for the <acronym>HEPv2</acronym>
                protocol.
                Limitation: 16-bit integer.
                </para>
                <para>
                <emphasis>
                        Default value is "1".
                </emphasis>
                </para>
                <example>
                <title>Set <varname>hep_capture_id</varname>
                parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "hep_capture_id", 234)
...
</programlisting>
                </example>
        </section>
	<section id="siptrace.p.trace_delayed">
                <title><varname>trace_delayed</varname> (integer)</title>
                <para>
		Use <quote>INSERT DELAYED</quote> to store to database when it is available,
		instead of <quote>INSERT</quote>.
                </para>
                <para>
                Default value is <emphasis>0 (off)</emphasis>.
                </para>
                <example>
                <title>Set <varname>trace_delayed</varname>
                parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "trace_delayed", 1)
...
</programlisting>
                </example>
        </section>
	<section id="siptrace.p.force_send_sock">
                <title><varname>force_send_sock</varname> (str)</title>
                <para>
			The local interface in the form of SIP uri from where to send
			the duplicated traffic. In the absence of this parameter
			&kamailio; automatically picks an interface.
                </para>
                <example>
                <title>Set <varname>force_send_sock</varname> parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "force_send_sock", "sip:10.1.1.2:5000")
...
</programlisting>
                </example>
        </section>
	<section id="siptrace.p.trace_mode">
                <title><varname>trace_mode</varname> (integer)</title>
                <para>
				If set to 1, the module uses core events triggered when receiving
				or sending SIP traffic to mirror traffic to a SIP capture server
				using HEP. It will automatically do the mirroring of all traffic,
				no need to set the siptrace flag per request.
                </para>
                <para>
                If set to 0, no automatic mirroring of SIP traffic via HEP.
                </para>
                <para>
                Default value is <emphasis>0</emphasis>.
                </para>
                <example>
                <title>Set <varname>trace_mode</varname>
                parameter</title>
                <programlisting format="linespecific">
...
modparam("siptrace", "trace_mode", 1)
...
</programlisting>
                </example>
        </section>
	</section>
	
	<section>
	<title>Functions</title>
	<section id="siptrace.f.sip_trace">
		<title>
		<function moreinfo="none">sip_trace([address])</function>
		</title>
		<para>
		Store or forward the current processed SIP message in database. It is stored in the
		form prior applying changes made to it.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
		<listitem>
		<para><emphasis>address</emphasis> - The address in form of SIP uri
		where to send a duplicate of traced message. This parameter trumps
		duplicate_uri and allows tracing to more than one server.
		</para>
		</listitem>
		</itemizedlist>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
		ONREPLY_ROUTE, BRANCH_ROUTE.
		</para>
		<emphasis>
			Default value is "NULL".
		</emphasis>
		<example>
		<title><function>sip_trace()</function> usage</title>
		<programlisting format="linespecific">
...
sip_trace();
...
sip_trace("sip:10.1.1.2:5085");
...
</programlisting>
		</example>
	</section>

	</section>

    <section>
	<title>MI Commands</title>
	<section id="siptrace.m.sip_trace">
		<title>
		<function moreinfo="none">sip_trace</function>
		</title>
		<para>

		</para>
		<para>
		Name: <emphasis>sip_trace</emphasis>
		</para>
		<para>Parameters: </para>
		<itemizedlist>
			<listitem><para>trace_mode : turns on/off SIP message tracing.
			Possible values are:</para>
			<itemizedlist>
				<listitem><para> on </para></listitem> 
				<listitem><para> off </para></listitem>
			</itemizedlist>
			<para>The parameter is optional - if missing, the command will
			return the status of the SIP message tracing (as string 
			<quote>on</quote> or <quote>off</quote> ) without changing
			anything.</para>
			</listitem>
		</itemizedlist>

		<para>
		MI FIFO Command Format:
		</para>
		<programlisting  format="linespecific">
		:sip_trace:_reply_fifo_file_
		trace_mode
		_empty_line_
		</programlisting>
	</section>
	</section><!-- MI Commands -->
    	<section>
	<title>RPC Commands</title>
	<section id="siptrace.r.siptrace.status">
		<title>
		<function moreinfo="none">siptrace.status param</function>
		</title>
		<para>

		</para>
		<para>
		Name: <emphasis>siptrace.status</emphasis>
		</para>
		<para>Parameters: </para>
		<itemizedlist>
			<listitem><para>on or off: turns on/off SIP message tracing..
			Possible values are:</para>
			<itemizedlist>
				<listitem><para>on</para></listitem> 
				<listitem><para>off</para></listitem>
			</itemizedlist>
			</listitem>
			<listitem><para><quote>check</quote> does not change 
			siptrace status, just reports the current status.</para>
			</listitem>
		</itemizedlist>

	</section>
	</section><!-- RPC commands -->
	
	<section>
		<title>Database setup</title>
		<para>
		Before running &kamailio; with siptrace, you have to setup the database 
		tables where the module will store the data. For that, if the
		table were not created by the installation script or you choose
		to install everything by yourself you can use the siptrace-create.sql
		<acronym>SQL</acronym> script in the database directories in the
		kamailio/scripts folder as template.
		You can also find the complete database documentation on the
		project webpage, &kamailiodbdocslink;.
		</para>
	</section>

	<section>
	<title>Known issues</title>
	<para>
	Stateless forwarded messages (forward()) are not logged if you set the
	flag, use sip_trace().
	</para>
	</section>
	
</chapter>

