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

<chapter>

	<title>&adminguide;</title>

	<section id="overview" xreflabel="Overview">
	<title>Overview</title>
	<para>
	The WebSocket protocol (<ulink url='http://tools.ietf.org/html/rfc6455'>RFC 6455</ulink>)
	 provides an end-to-end full-duplex communication channel between two web-based applications.
	This allows WebSocket enabled browsers to connect to a WebSocket server
	and exchange any type of data.
	<ulink url='http://tools.ietf.org/html/rfc7118'>RFC 7118</ulink>
	provides the specifications for transporting SIP messages over the WebSocket protocol.
	</para>
	<para>
	The <emphasis role='bold'>proto_ws</emphasis> module is transport module that provides
	communication over the WebSocket protocol. This module is fully compliant with the
	<ulink url='http://tools.ietf.org/html/rfc7118'>RFC 7118</ulink>, thus allowing browsers
	to act as SIP clients for the &osips; proxy.
	</para>
	<para>
	The current implementation acts both as WebSocket server and client, thus it can
	accept connections from WebSocket clients and can also initiate connections to another
	WebSocket server. After the connection is established, messages can flow  in
	both directions.
	</para>
	<para>&osips; supports the following WebSocket operations:
	<itemizedlist>
	<listitem><para>
	text and binary - can both send and receive WebSocket messages that contain text or binary body
	</para></listitem>
	<listitem><para>
	close - messages used to safely close the WebSocket communication using a 2-messages handshake
	</para></listitem>
	<listitem><para>
	ping - responds with pong messages. There is no mechanism to trigger ping messages.
	</para></listitem>
	<listitem><para>
	pong - sent when a ping message is received. &osips;, absorbes the pong messages received.
	</para></listitem>
	</itemizedlist>
	</para>
	<para>
		Once loaded, you will be able to define WebSocket listeners in your script. To
		add a listener, you have to add its IP, and optionally the listening port,
		<emphasis>after</emphasis> the <varname>mpath</varname> parameter, similar to this
		example:
	<para>
		<programlisting format="linespecific">
...
mpath=/path/to/modules
...
socket=ws:127.0.0.1		# change with the listening IP
socket=ws:127.0.0.1:5060	# change with the listening IP and port
...
</programlisting>
	</para>
	</para>
	</section>

	<section id="dependencies" xreflabel="Dependencies">
	<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 id="exported_parameters" xreflabel="Exported Parameters">
	<title>Exported Parameters</title>
	<section id="param_ws_port" xreflabel="ws_port">
		<title><varname>ws_port</varname> (integer)</title>
		<para>
		The default port to be used for all WS related operation. Be careful
		as the default port impacts both the SIP listening part (if no port is
		defined in the WS listeners) and the SIP sending part (if the 
		destination WS URI has no explicit port).
		</para>
		<para>
		If you want to change only the listening port for WS, use the port
		option in the SIP listener defintion.
		</para>
		<para>
		<emphasis>
			Default value is 80.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>ws_port</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_ws", "ws_port", 8080)
...
</programlisting>
		</example>
	</section>

	<section id="param_ws_send_timeout" xreflabel="ws_send_timeout">
		<title><varname>ws_send_timeout</varname> (integer)</title>
		<para>
		Time in milliseconds after a WebSocket 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>ws_send_timeout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_ws", "ws_send_timeout", 200)
...
</programlisting>
		</example>
	</section>
	<section id="param_ws_max_msg_chunks" xreflabel="ws_max_msg_chunks">
		<title><varname>ws_max_msg_chunks</varname> (integer)</title>
		<para>
			The maximum number of chunks in which a SIP message is expected to
			arrive via WebSocket. If a received packet is 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 very
			fragmented traffic in order to decrease server performance).
		</para>
		<para>
		<emphasis>
			Default value is 4.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>ws_max_msg_chunks</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_ws", "ws_max_msg_chunks", 8)
...
</programlisting>
		</example>
	</section>
	<section id="param_trace_destination" xreflabel="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.
		For each connection that is created an event containing information
		about http request and reply belonging to  web socket protocol
		handshake and network layer information shall be sent.
		</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_ws", "trace_destination", "hep_dest")
...
</programlisting>
		</example>
	</section>

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

	<section id="param_trace_filter_route" xreflabel="trace_filter_route">
		<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="param_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="param_trace_on"/> is
			set to 0 or tracing is deactived via the mi command <xref linkend="mi_ws_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_ws", "trace_filter_route", "ws_filter")
...
/* all ws connections will go through this route if tracing is activated
 * and a trace destination is defined */
route[ws_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 id="param_require_origin" xreflabel="require_origin">
		<title><varname>require_origin</varname> (int)</title>
		<para>
			Controls whether the module should require the Origin header or not.
		</para>
		<emphasis>
			Default value is 1(require Origin header).
		</emphasis>
		<example>
		<title>Set <varname>require_origin</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_ws", "require_origin", no)
...
</programlisting>
		</example>
	</section>

	</section>


	<section id="exported_mi_functions" xreflabel="Exported MI Functions">
	<title>Exported MI Functions</title>

		<section id="mi_ws_trace" xreflabel="ws_trace">
			<title>
			<function moreinfo="none">ws_trace</function>
			</title>

			<para>
			</para>

			<para>
			Name: <emphasis>ws_trace</emphasis>
			</para>

			<para>Parameters: </para>
			<itemizedlist>
				<listitem>
					<para>trace_mode(optional): set ws 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">
			opensips-cli -x mi ws_trace on
			</programlisting>
		</section>
	</section>

</chapter>
