# &adminguide;

## Overview

The sipcapture module stores incoming/outgoing SIP messages in a database.

Kamailio can capture SIP messages in three modes

- IPIP encapsulation. (ETHHDR+IPHDR+IPHDR+UDPHDR).

- Monitoring/mirroring port.

- Homer encapsulation protocol mode (HEP v1, v2, v3).

The capturing can be turned on/off using rpc commands. Example:

&ctltool; rpc sipcapture.status on

&ctltool; rpc sipcapture.status off

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- database module - mysql, postgres, dbtext, unixodbc...

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- None.

## Parameters

### `db_url` (str)

Database URL.

Default value is &defaultdb;.

#### Set `db_url` parameter

    ...
    modparam("sipcapture", "db_url", "&defaultdb;")
    ...

### `table_name` (str)

Name of the table's name used to store the SIP messages. Can contain multiple tables, separated by "|".

Default value is "sip_capture". Only for Homer 3. For Homer 5, please use an argument for the sip_capture function.

#### Set `sip_capture` parameter

    ...
    modparam("sipcapture", "table_name", "homer_capture")
    ...
    modparam("sipcapture", "table_name", "homer_capture1|homer_capture2");
    ...

### `mt_mode` (str)

Name of the mode used for storing data in multiple tables. Modes can be "rand" (random), "round_robin" (use a round_robin algorithm) or "hash" (use hashing to determine the table to store). These modes are only triggered if there is more than one table specified in table_name parameter, separated by "|".

Default value is "rand".

#### Set `mt_mode` parameter

    ...
    modparam("sipcapture", "mt_mode", "hash")
    ...

### `hash_source` (str)

The field of the SIP message used for hashing, when mt_mode is set to "hash". The value can be "call_id", "to_user" or "from_user".

Default value is "call_id".

#### Set `mt_mode` parameter

    ...
    modparam("sipcapture", "hash_source", "to_user")
    ...

### `db_insert_mode` (integer)

If set to 1, use INSERT DELAYED to store sip message into capture table when the DB driver has support for it. If no INSERT DELAYED support is offered by DB driver, then standard INSERT is used.

If set to 2, use ASYNC INSERT to store sip message into capture table when the DB driver has support for it. If no ASYNC INSERT support is offered by DB driver, then standard INSERT is used.

Default value is 0 (no INSERT DELAYED).

#### db_insert_mode example

    modparam("sipcapture", "db_insert_mode", 1)

### `capture_on` (integer)

Parameter to enable/disable capture globally (on(1)/off(0))

Default value is "0".

#### Set `capture_on` parameter

    ...
    modparam("sipcapture", "capture_on", 1)
    ...

### `capture_mode` (str)

This parameter can be used for defining a capture mode which can be used in the sip_capture calls as a parameter. A capture mode has a name and some parameters. It must be defined in the format: name=>param1=val1;param2=val2;... The parameters are db_url, table_name, mt_mode and hash_source (optional). Multiple capture modes can be defined by using this parameter multiple times. After this, the capture modes can be used like: sip_capture ("", "CAPTURE_MODE");

#### capture_mode example

    modparam("sipcapture", "capture_mode", "mode1=>db_url=mysql://user:passwd@host/dbname1;table_name=homer_capture1|homer_capture2;mt_mode=hash;hash_source=call_id;")
    modparam("sipcapture", "capture_mode", "mode2=>db_url=mysql://user:passwd@host/dbname2;table_name=homer_capture3|homer_capture4;mt_mode=rand;")

### `hep_capture_on` (integer)

Parameter to enable/disable capture of HEP (on(1)/off(0))

Default value is "0".

#### Set `hep_capture_on` parameter

    ...
    modparam("sipcapture", "hep_capture_on", 1)
    ...

### `raw_ipip_capture_on` (integer)

Parameter to enable/disable IPIP capturing (on(1)/off(0))

Default value is "0".

#### Set `raw_ipip_capture_on` parameter

    ...
    modparam("sipcapture", "raw_ipip_capture_on", 1)
    ...

### `raw_moni_capture_on` (integer)

Parameter to enable/disable monitoring/mirroring port capturing (on(1)/off(0)) Only one mode on raw socket can be enabled! Monitoring port capturing currently supported only on Linux.

Default value is "0".

#### Set `raw_moni_capture_on` parameter

    ...
    modparam("sipcapture", "raw_moni_capture_on", 1)
    ...

### `raw_socket_listen` (string)

Parameter indicate a listen IP address of RAW socket for IPIP capturing. You can also define a port/portrange for IPIP/Mirroring mode, to capture SIP messages in specific ports:

"10.0.0.1:5060" - the source/destination port of the SIP message must be equal 5060

"10.0.0.1:5060-5090" - the source/destination port of the SIP message must be equal or be between 5060 and 5090.

The port/portrange must be defined if you are planning to use mirroring capture! In this case, the part with IP address will be ignored, but to make parser happy, use i.e. 10.0.0.0

Default value is "".

#### Set `raw_socket_listen` parameter

    ...
    modparam("sipcapture", "raw_socket_listen", "10.0.0.1:5060-5090")
    ...
    modparam("sipcapture", "raw_socket_listen", "10.0.0.1:5060")
    ...

### `raw_interface` (string)

Name of the interface to bind on the raw socket.

Default value is "".

#### Set `raw_interface` parameter

    ...
    modparam("sipcapture", "raw_interface", "eth0")
    ...

### `raw_sock_children` (integer)

Parameter define how many children that must be created to listen the raw socket.

Default value is "1".

#### Set `raw_sock_children` parameter

    ...
    modparam("sipcapture", "raw_sock_children", 6)
    ...

### `promiscuous_on` (integer)

Parameter to enable/disable promiscuous mode on the raw socket. Linux only.

Default value is "0".

#### Set `promiscuous_on` parameter

    ...
    modparam("sipcapture", "promiscuous_on", 1)
    ...

### `raw_moni_bpf_on` (integer)

Activate Linux Socket Filter (LSF based on BPF) on the mirroring interface. The structure is defined in linux/filter.h. The default LSF accept a port/portrange from the raw_socket_listen param. Currently LSF supported only on Linux.

Default value is "0".

#### Set `raw_moni_bpf_on` parameter

    ...
    modparam("sipcapture", "raw_moni_bpf_on", 1)
    ...

### `capture_node` (str)

Name of the capture node.

Default value is "homer01".

#### Set `capture_node` parameter

    ...
    modparam("sipcapture", "capture_node", "homer03")
    ...

### `insert_retries` (integer)

The number of times Kamailio should retry to write to the Homer database in case the first attempt failed. The retry is also limited timewise by the insert_retry_timeout parameter. Values allowed range from 0 to 500.

Default value is 0 (no retries).

#### Set `insert_retries` parameter

    ...
    modparam("sipcapture", "insert_retries", 5)
    ...

### `insert_retry_timeout` (integer)

The time limit in seconds Kamailio retries to write to the Homer database in case the first attempt failed. This parameter is only used together with the insert_retries parameter. Values allowed range from 0 to 300.

Default value is 60 seconds.

#### Set `insert_retry_timeout` parameter

    ...
    modparam("sipcapture", "insert_retry_timeout", 10)
    ...

### `callid_aleg_header` (str)

Header name used to correlate A-leg with B-leg. It can take a list of headers, separated by semicolon, e.g. "X-CID0;X-CID1". First match wins.

Default value is "X-CID".

#### Set `callid_aleg_header` parameter

    ...
    modparam("sipcapture", "callid_aleg_header", "X-CallIDALeg")
    ...

### `topoh_unmask` (int)

If set to 1, call-id will be unmasked using topoh module api (topoh module must be loaded in this case).

Default value is 0.

#### Set `topoh_unmask` parameter

    ...
    modparam("sipcapture", "topoh_unmask", 1)
    ...

### `nonsip_hook` (int)

If set to 1, event route sipcapture:siprequest is run when HEP message is received.

Default value is 0.

#### Set `nonsip_hook` parameter

    ...
    modparam("sipcapture", "nonsip_hook", 1)
    ...

### `event_callback` (str)

The name of the function in the kemi configuration file (embedded scripting language such as Lua, Python, ...) to be executed instead of event_route[...] blocks.

The function receives a string parameter with the name of the event, the values can be: 'sipcapture:request'.

Default value is 'empty' (no function is executed for events).

#### Set `event_callback` parameter

    ...
    modparam("sipcapture", "event_callback", "ksr_sipcapture_event")
    ...
    -- event callback function implemented in Lua
    function ksr_sipcapture_event(evname)
    	KSR.info("===== sipcapture module triggered event: " .. evname .. "\n");
    	return 1;
    end
    ...

### `capture_bad_msgs` (int)

If set to something different than 0, tries to capture also the broken SIP messages.

Default value is 0.

#### Set `capture_bad_msgs` parameter

    ...
    modparam("sipcapture", "capture_bad_msgs", 1)
    ...

## Functions

### `sip_capture([table], [cmode])`

Store the current processed HEP/IPIP SIP message in a database. It is stored in the form prior applying changes made to it.

Meaning of the parameters is as follows:

- table - The table where HEP SIP message will be stored. Homer 5 use now tables with datestamp. To generate an automatic table's name please use strftime parameters. I.e. $var(table) = "sip_capture_call_%Y%m%d" and set the variable as an argument of the sip_capture function.

- cmode - The reference to the capture_mode parameter.

This function can be used from ANY_ROUTE.

Default value is "NULL".

#### `sip_capture()` usage

    ...
    sip_capture();
    ...
    sip_capture("sip_capture_call_20160124");
    ...
    sip_capture("", "cmode");
    ...

### `report_capture([table], [cid], [data])`

Store the current processed HEP REPORT message in a database.

Meaning of the parameters is as follows:

- table - The table where REPORT message will be stored.

- cid - The correlation id.

- data - The custom report data in JSON format.

This function can be used from ANY_ROUTE.

Default value is "NULL".

#### `report_capture()` usage

    ...
    report_capture();
    ...
    report_capture("report_data", "$ci");
    ...
    report_capture("report_data", "$ci", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
    ...

### `float2int(fval, ival)`

Return the value of atof(fval) * atoi(ival). On case the result is 0, then -1 is returned.

This function can be used from ANY_ROUTE.

#### `report_capture()` usage

    ...
    $var(res) = float2int("10.5", "1");
    ...

### `sip_capture_forward(uri)`

Forward the HEP packet to an address specified by the parameter uri (it has to be a sip uri format). The function should be used inside event_route[sipcapture:request]. After using this function, add a return 0 in order to stop processing further the packet in the local &kamailio; instance.

This function can be used from ANY_ROUTE.

#### `sip_capture_forward()` usage

    ...
        event_route[sipcapture:request] {
            ...
            if(src_ip==1.2.3.4) {
                sip_capture_forward("sip:2.3.4.5:5090");
                return 0;
            }
            ...
        }
    ...

## Event Routes

### event_route[sipcapture:request]

Event route block to be executed when HEP packet is received. It requires module parameter 'nonsip_hook' to be set to 1.

#### event_route[sipcapture:request] usage

    ...
    # new event sipcapture socket
    modparam("sipcapture", "nonsip_hook", 1)
    ...

    event_route[sipcapture:request] {

    	xlog("HEP Request!\n");
    	xlog("received sipcapture request from $si:$sp\n");
    	xlog("HEP VERSION $hep(version) request from $si:$sp\n");
    	xlog("HEP CHUNK Source IP $hep(0x002) request from $si:$sp\n");

    	# is it SIP ?
    	if($hep(0x00b) == 1) {

    		# do parsing internal
    		return 1;
    	} else {
    		# if report lets proceed here with payload
    		xlog("HEP CHUNK PAYLOAD $hep(0x00f) request from $si:$sp\n");
    		return 0;
    	}
    }
    ...

## RPC Commands

### `sipcapture.status param`

Name: sipcapture.status

Parameters:

- on or off: turns on/off SIP message capturing. Possible values are:

  - on

  - off

- check does not change sipcapture status, just reports the current status.

RPC Command Format:

    ...
    &kamctl; rpc sipcapture.status on
    &kamctl; rpc sipcapture.status off
    &kamctl; rpc sipcapture.status check
    ...

## Database setup

Before running &kamailio; with the sipcapture module, you have to set up 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 homer_databases.sql, SQL script in the sql folder of sipcapture module as template. You can also find the complete database documentation on the project webpage, &kamailiodbdocslink;.

## Limitations

- 1. Only one capturing mode on RAW socket is supported: IPIP or monitoring/mirroring port. Don't activate both at the same time. Obsolete. Please use HEP mirroring now.
- 2. Mirroring port capturing works only on Linux.