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

<chapter>

	<title>&adminguide;</title>

	<section>
	<title>Overview</title>
	<para>
		The <emphasis role='bold'>proto_tcp</emphasis> module is a built-in
		transport module which implements SIP TCP-based communication. It does
		not handle TCP connections management, but only offers higher-level
		primitives to read and write SIP messages over TCP.
	</para>
	</section>
	<para>
		Once loaded, you will be able to define TCP listeners in your script,
		by adding its IP, and optionally the listening port, in your configuration
		file, similar to this example:
	<para>
		<programlisting format="linespecific">
...
socket=tcp:127.0.0.1 		# change the listening IP
socket=tcp:127.0.0.1:5080	# change with the listening IP and port
...
</programlisting>
	</para>
	</para>

	<section>
	<title>Dependencies</title>
	<section>
		<title>&osips; Modules</title>
		<para>
		The following modules must be loaded before this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>None</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>

	<section>
		<title>External Libraries or Applications</title>
		<para>
		The following libraries or applications must be installed before
		running &osips; with this module loaded:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>None</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>
	</section>

	<section>
	<title>Exported Parameters</title>
	<section>
		<title><varname>tcp_port</varname> (integer)</title>
		<para>
		The default port to be used for all TCP related operation. Be careful
		as the default port impacts both the SIP listening part (if no port is
		defined in the TCP listeners) and the SIP sending part (if the 
		destination URI has no explicit port).
		</para>
		<para>
		If you want to change only the listening port for TCP, use the port
		option in the SIP listener defintion.
		</para>
		<para>
		<emphasis>
			Default value is 5060.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_port</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_port", 5065)
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>tcp_send_timeout</varname> (integer)</title>
		<para>
		Time in milliseconds after a TCP connection will be closed if it is
		not available for blocking writing in this interval (and &osips; wants
		to send something on it).
		</para>
		<para>
		<emphasis>
			Default value is 100 ms.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_send_timeout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_send_timeout", 200)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_max_msg_chunks</varname> (integer)</title>
		<para>
			The maximum number of chunks that a SIP message is expected to
			arrive via TCP. If a packet is received more fragmented than this,
			the connection is dropped (either the connection is very
			overloaded and this leads to high fragmentation - or we are the
			victim of an ongoing attack where the attacker is sending the
			traffic very fragmented in order to decrease our performance).
		</para>
		<para>
		<emphasis>
			Default value is 4.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_max_msg_chunks</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_max_msg_chunks", 8)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_crlf_pingpong</varname> (integer)</title>
		<para>
			Send CRLF pong (\r\n) to incoming CRLFCRLF ping messages over TCP.
			By default it is enabled (1).
		</para>
		<para>
		<emphasis>
			Default value is 1 (enabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_crlf_pingpong</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_crlf_pingpong", 0)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_crlf_drop</varname> (integer)</title>
		<para>
			Drop CRLF (\r\n) ping messages. When this parameter is enabled,
			the TCP layer drops packets that contains a single CRLF message.
			If a CRLFCRLF message is received, it is handled according to the
			<emphasis>tcp_crlf_pingpong</emphasis> parameter.
		</para>
		<para>
		<emphasis>
			Default value is 0 (disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_crlf_drop</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_crlf_drop", 1)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_async</varname> (integer)</title>
		<para>
			If the TCP connect and write operations should be done in an
			asynchronous mode (non-blocking connect and
			write). If disabled, OpenSIPS will block and wait for TCP
			operations like connect and write.
		</para>
		<para>
		<emphasis>
			Default value is 1 (enabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_async</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_async", 0)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_async_max_postponed_chunks</varname> (integer)</title>
		<para>
			If <emphasis>tcp_async</emphasis> is enabled, this specifies the
			maximum number of SIP messages that can be stashed for later/async
			writing. If the connection pending writes exceed this number, the
			connection will be marked as broken and dropped.
		</para>
		<para>
		<emphasis>
			Default value is 32.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_async_max_postponed_chunks</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_async_max_postponed_chunks", 16)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_async_local_connect_timeout</varname> (integer)</title>
		<para>
			If <emphasis>tcp_async</emphasis> is enabled, this specifies the
			number of milliseconds that a connect will be tried in blocking
			mode (optimization). If the connect operation lasts more than
			this, the connect will go to async mode and will be passed to TCP
			MAIN for polling.
		</para>
		<para>
		<emphasis>
			Default value is 100 ms.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_async_local_connect_timeout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_async_local_connect_timeout", 200)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_async_local_write_timeout</varname> (integer)</title>
		<para>
			If <emphasis>tcp_async</emphasis> is enabled, this specifies the
			number of milliseconds that a write op will be tried in blocking
			mode (optimization). If the write operation lasts more than this,
			the write will go to async mode and will be passed to TCP MAIN for
			polling.
		</para>
		<para>
		<emphasis>
			Default value is 10 ms.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_async_local_write_timeout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_async_local_write_timeout", 100)
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>tcp_parallel_handling</varname> (integer)</title>
		<para>
		This parameter says if the handling/processing (NOT READING) of the
		SIP messages should be done in parallel (after one SIP msg is read, 
		while processing it, another READ op may be performed).
		</para>
		<para>
		<emphasis>
			Default value is 0 (disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>tcp_parallel_handling</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "tcp_parallel_handling", 1)
...
</programlisting>
		</example>
	</section>

	<section id="trace-destination">
		<title><varname>trace_destination</varname> (string)</title>
		<para>
			Trace destination as defined in the tracing module. Currently
		the only tracing module is <emphasis role="bold">proto_hep</emphasis>.
		Network events such as connect, accept and connection closed events
		shall be traced along with errors that could appear in the process.
		</para>
		<para>
			<emphasis role="bold">WARNING: </emphasis>A tracing module must be
			loaded in order for this parameter to work. (for example
			<emphasis role="bold">proto_hep</emphasis>).
		</para>
		<para>
		<emphasis>
			Default value is none(not defined).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_destination</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_hep", "hep_id", "[hep_dest]10.0.0.2;transport=tcp;version=3")

modparam("proto_tcp", "trace_destination", "hep_dest")
...
</programlisting>
		</example>
	</section>

	<section id="trace-on">
		<title><varname>trace_on</varname> (int)</title>
		<para>
			This controls whether tracing for tcp is on or not. You still need to define
			<xref linkend="trace-destination"/>in order to work, but this value will be
			controlled using mi function <xref linkend="tcp-trace"/>.
		</para>
		<emphasis>
			Default value is 0(tracing inactive).
		</emphasis>
		<example>
		<title>Set <varname>trace_on</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "trace_on", 1)
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>trace_filter_route</varname> (string)</title>
		<para>
			Define the name of a route in which you can filter which connections will
			be trace and which connections won't be. In this route you will have
			information regarding source and destination ips and ports for the current
			connection. To disable tracing for a specific connection the last call in
			this route must be <emphasis role="bold">drop</emphasis>, any other exit
			mode resulting in tracing the current connection ( of course you still
			have to define a <xref linkend="trace-destination"/> and trace must be
			on at the time this connection is opened.
		</para>
		<para>
			<emphasis role="bold">IMPORTANT</emphasis>
			Filtering on ip addresses and ports can be made using <emphasis role="bold">
			$si</emphasis> and <emphasis role="bold">$sp</emphasis> for matching
			either the entity that is connecting to &osips; or the entity to which
			&osips; is connecting. The name might be misleading (<emphasis role="bold">
				$si</emphasis> meaning the source ip if you read the docs) but in reality
			it is simply the socket other than the &osips; socket. In order to match
			&osips; interface (either the one that accepted the connection or the one
			that initiated a connection) <emphasis role="bold">$socket_in(ip)</emphasis> (ip) and
			<emphasis role="bold">$socket_in(port)</emphasis> (port) can be used.
		</para>
		<para>
			<emphasis role="bold">WARNING:</emphasis> IF <xref linkend="trace-on"/> is
			set to 0 or tracing is deactived via the mi command <xref linkend="tcp-trace"/>
			this route won't be called.
		</para>
		<emphasis>
			Default value is none(no route is set).
		</emphasis>
		<example>
		<title>Set <varname>trace_filter_route</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_tcp", "trace_filter_route", "tcp_filter")
...
/* all tcp connections will go through this route if tracing is activated
 * and a trace destination is defined */
route[tcp_filter] {
	...
	/* all connections opened from/by ip 1.1.1.1:8000 will be traced
	   on interface 1.1.1.10:5060(opensips listener)
	   all the other connections won't be */
	 if ( $si == "1.1.1.1" &amp;&amp; $sp == 8000 &amp;&amp;
		$socket_in(ip) == "1.1.1.10"  &amp;&amp; $socket_in(port) == 5060)
		exit;
	else
		drop;
}
...
</programlisting>
		</example>
	</section>

	</section>


	<section>
	<title>Exported MI Functions</title>

		<section id="tcp-trace">
			<title>
			<function moreinfo="none">tcp_trace</function>
			</title>

			<para>
			</para>

			<para>
			Name: <emphasis>tcp_trace</emphasis>
			</para>

			<para>Parameters: </para>
			<itemizedlist>
				<listitem>
					<para>trace_mode(optional): set tcp tracing on and off. This parameter
						can be missing and the command will show the current tracing
						status for this module( on or off );
						Possible values:
						<itemizedlist>
							<listitem><para> on </para></listitem>
							<listitem><para> off </para></listitem>
						</itemizedlist>
					</para>
				</listitem>
			</itemizedlist>

			<para>
			MI FIFO Command Format:
			</para>
			<programlisting  format="linespecific">
			:tcp_trace:_reply_fifo_file_
			trace_mode
			_empty_line_
			</programlisting>
		</section>
	</section>
</chapter>
