# &adminguide;

## Overview

This module was designed to act as a mechanism to limit call duration based on credit information parameters. After getting the credit information of the call being set up, you can instruct the module to start monitoring the consumed credit to shutdown a single call or a group of calls in case of credit exhaustion.

Every call is associated to an unique client/customer identifier. If a credit event occurs, all calls hooked to this identifier are automatically shutdown.

Cnxcc is dialog-aware so there's no need to explicitly allocate/deallocate the monitoring. Only a single function call inside the script is needed upon reception of the INVITE.

The credit discount rate is proportional to the number of calls grouped inside an identifier. Once the setup of the first call is done, the information remains while the call is active. If the customer starts a new call with the same routing criteria, it will land in the same monitoring bag and it will consume the same pool of credit in rates that are equal to the cost per second of both calls.

If your accounting program does not maintain the state of the call in real time, this module can provide you that ability.

Cnxcc can also provide more common means of monitoring, i.e., by time limit or by maximum simultaneous calls.

## Dependencies

### Modules

The following module must be loaded before this module:

- dialog

## Parameters

### `dlg_flag` (integer)

Flag to indicate if the dialog must be monitored or not. Messages are flagged with this value if we call one of the monitoring functions.

#### dlg_flag

    ...
    modparam("cnxcc", "dlg_flag", 29)
    ...

### `credit_check_period` (integer)

Indicates how often the credit checking function should be called. It is directly related to the precison of the module. The maximum precision is 1, which means that every call is checked every one second.

Values greater than 1 leads to precision lost but less CPU consumption.

#### credit_check_period

    ...
    modparam("cnxcc", "credit_check_period", 1)
    ...

## Functions

### `cnxcc_set_max_credit()`

Specifies the initial pulse, final pulse, max credit and cost per second of a call. The discount is calculated in pulses (30/6, 1/1, etc) and sustracted from the pool of credit.

Return code:

- 1 - successful

- -1 - failed, error logged

- -2 - failed, credit value is less than initial pulse value

#### cnxcc_set_max_credit()

    ...
    $var(customer) = "john-doe-123-premium";
    $var(credit) = "100";
    $var(cps)   = "2.00";         # cost per second
    $var(initial_p)   = "030";    # intial pulse
    $var(final_p)   = "006";      # final pulse

    cnxcc_set_max_credit("$var(customer)", "$var(credit)", "$var(cps)", "$var(initial_p)", "$var(final_p)");
    ...

### `cnxcc_set_max_time()`

Specifies the amount of time the call should last at most.

Return code:

- 1 - successful

- -1 - failed, error logged

#### cnxcc_set_max_time()

    ...
    $var(customer) = "john-doe-123-basic";
    $var(max_time) = 120;

    cnxcc_set_max_time("$var(customer)", "$var(max_time)");
    ...

### `cnxcc_update_max_time()`

Updates max-time of an established and monitored call. This can be used to grant minimum values and to update them every short periods on time as a mean to prevent frauds and/or to mimic requested/granted units of time of Credit Control Application behavior.

Return code:

- 1 - successful

- -1 - failed, error logged

#### cnxcc_update_max_time()

    ...
            $var(update_time)  = 5;
            $var(client)       = "john-doe-123-basic";

            if (!cnxcc_update_max_time("$var(client)",
                                      "$var(update_time)")) {
                    xlog("Error updating max-time");
                    return;
            }

    ...

### `cnxcc_set_max_channel()`

Specifies a limit for the number of simultaneous calls

Return code:

- 1 - successful

- -1 - failed, error logged

- -2 - failed, calls established plus calls being established result in more than the limit you specified

- -3 - failed, number of calls established is more than the limit you specified

#### cnxcc_set_max_channels()

    ...
    $var(customer)  = "john-doe-123-basic";
    $var(max_chan)  = 2;
    $var(retcode)   = cnxcc_set_max_channels("$var(customer)", "$var(max_chan)");

    if ($var(retcode) == -1) {
    	xlog("Error setting up credit control");
    	return;
    }

    if ($var(retcode) < -1) {
            xlog("Too many channels for customer");
            sl_send_reply(403, "Forbidden");

            if (!cnxcc_terminate_all("$var(customer)")) {
    		xlog("Error terminating customer's calls");
    	}

    	exit;
    }

    ...

### `cnxcc_terminate_all()`

Terminates all calls of the specified customer/profile

Return code:

- 1 - successful

- -1 - failed, error logged

#### cnxcc_set_max_time()

    ...
    $var(customer)  = "john-doe-123-basic";

    if (!cnxcc_terminate_all("$var(customer)")) {
    	xlog("Error terminating customer's calls");
    }
    ...

## Exported RPC Commands

### `cnxcc.active_clients`

Retrieves all calls grouped by their identifiers.

Parameters: none

Example:

    	    &sercmd; cnxcc.active_clients

### `cnxcc.check_client`

Retrives all calls from a particular identifier.

Parameters: client/customer identifier

Example:

    	    &sercmd; cnxcc.check_client john-doe-123-premium

### `cnxcc.kill_call`

Kills an active call using its call ID.

Parameters: Call-ID

Example:

                &sercmd; cnxcc.kill_call qumojlaahitafih@carlosrdcnx-laptop.site

## Events

When a call is forced to end an event route is automatically invoked. This route is suited with a fake OPTIONS message containing the call ID, ftag and ttag of the original call so it can be located somehow in the accounting database.

Example:

    ...
    event_route[cnxcc:call-shutdown]
    {
    	xlog("L_INFO", "[$ci]: call killed");

            # perform some kind of notification, database update, email sending, etc.
    }
    ...

## Web Interface

The module contains a web management interface completely optional. With it, you can review your calls in real time and hang them up if necessary.

Link: https://github.com/caruizdiaz/cnxcc-web

## Sample

### kamailio-cnxcc.cfg

    ...
    route[CNXCC]
    {
    	$var(client)              = "test-client-0-123-01";
          	$var(credit)              = "50";
          	$var(cost_per_sec)        = "0.5";
          	$var(i_pulse)             = "30";
          	$var(f_pulse)             = "6";

          	if (!cnxcc_set_max_credit("$var(client)",
                              "$var(credit)",
                              "$var(cost_per_sec)",
                              "$var(i_pulse)",
                              "$var(f_pulse)")) {
    		 xlog("Error setting up credit control");
          	}
    }

    event_route[cnxcc:call-shutdown]
    {
    	xlog("L_INFO", "[$ci]: call killed");

    }
    ...