Login | Register

Documentation

Documentation -> Manuals -> Manual 3.0 -> Script Syntax

Pages for other versions: devel 3.0 2.4 Older versions: 2.3 2.2 2.1 1.11 1.10 1.9 1.8 1.7 1.6 1.5 1.4


Script Syntax v3.0
PrevNext

1.  Script Format

The OpenSIPs configuration script has three main logical parts:

  1. global parameters
  2. modules section
  3. routing logic

1.1  Global parameters

Usually, in the first part, you declare the OpenSIPS global parameters - these global or core parameters are affecting the OpenSIPS core and possible the modules.

Configuring the network listeners, available transport protocols, forking (and number of processes), the logging and other global stuff is provided by these global parameters.

Example:

disable_tcp = yes
listen = udp:192.168.3.00:5060
listen = udp:192.168.3.00:5070
fork = yes
children = 4
log_stderror = no

1.2  Modules section

In regards to the OpenSIPS modules,the modules that are to be loaded (no module is loaded by default) are specified by using the directive loadmodule. Modules are to be specified by name and an optional path (to the .so file). If no path is provided (and just the name of the module), the default path will be assumed for locating the loading the module (default path is /usr/lib/opensips/modules if not other one configured at compile time. For configuring a different path, either the path is pushed directly with the module name (to get control per module) or it can be globally (for all modules) configured via the mpath global parameter.

Once the modules are loaded, the parameters of the modules may be set using the modparam directive - to list of available parameters for each module, the type of parameter value (integer or string) can be found in the documentation of the modules, the Parameters section.

Examples:

loadmodule "modules/mi_datagram/mi_datagram.so"
modparam("mi_datagram", "socket_name", "udp:127.0.0.1:4343")
modparam("mi_datagram", "children_count", 3)

or

mpath="/usr/local/opensips_proxy/lib/modules"
loadmodule "mi_datagram.so"
modparam("mi_datagram", "socket_name", "udp:127.0.0.1:4343")
modparam("mi_datagram", "children_count", 3)
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")

1.3  Routing logic

The routing logic is actually a sum of routes (script routes) that contain the OpenSIPS logic for routing SIP traffic. The description of OpenSIPS behavior in relation to the SIP traffic is done via this routes.

There are different types of routes :

  1. top routes - routes that are directly triggered by OpenSIPs when some events occurs (like SIP request received, SIP reply received, transaction failed, etc)
  2. sub-routes - routes that are triggered / used from other routes in script.


What are the existing top routes, when they are triggered, what kind of SIP messages is handled, what SIP operations are allowed and other are documented in the types of routes section.

The sub-routes have names and they are to be called from any other route (top or sub) in the script via their names. The sub-routes may take parameters (when called) or return a numerical code (avoid returning 0 value as this will terminate your whole script. The sub-routes are similar to functions / procedure in any programing language. See the description of the route directive.

2.  Data Types

The OpenSIPS scripting language supports the following data types:

Basic

  • integer (32-bit, signed).
    • Max value: +2,147,483,647 == 2 ^ 31 - 1
    • Min value: -2,147,483,648 == - 2 ^ 31
  • string (unlimited size)
    • note that some functions which use strings may have internal buffers which limit the maximum size of the strings (e.g. the xlog() function's output buffer is configurable via xlog_buf_size)
  • double (packed as string), through the mathops module

Complex

3.  Function Calling Conventions

A significant portion of opensips.cfg scripting logic consists of functions. Currently, there are two types of functions:

3.1  Core Functions

The core functions are always available at script level, since they are baked into the opensips binary. The calling convention for these functions varies greatly from one function to another, since each function is individually provided by the language parser.

Example core function calls:


cache_raw_query("mongodb", "{ \"op\" : \"count\",\"query\": { \"username\" : $rU} }", "$avp(mongo_count_result)");

... will look up the specified document within MongoDB and return the results in $avp(mongo_count_result).


cache_fetch("redis:cluster1", "my_counter", $var(redis_counter_val));

... will look up "my_counter" within Redis and return the results in $avp(redis_counter_val). Notice how the output parameter is unquoted for this function, while the previous one, cache_raw_query(), had a similar meaning for its equivalent parameter, yet mandated quotes.


force_send_socket(tcp:10.10.10.10:5060);

... will force the egress interface to be tcp:10.10.10.10:5060. Notice how its parameter is not a string -- in fact, it's a unique data type (a socket), as the Yacc grammar interprets it.


Summary: OpenSIPS core functions are very useful, but are often inflexible: their parameters must have the exact type(s) as expected by the language grammar. There are plans to align the core function and module function interfaces (the latter is more powerful, see below) -- this work is scheduled for OpenSIPS 3.1. Script writers are advised to carefully consult the specifics of each core function while keeping in mind that consistency across core functions, if any, is only due to diligent collaboration between OpenSIPS developers, and is not the result of some internal programming interface.

3.2  Module Functions

Module functions become available for OpenSIPS script writers to use once their respective modules are loaded via the loadmodule statement (see the "Script Format" section above). All module functions implement the same internal function interface, such that they benefit from the following, more sophisticated calling convention:


  • any integer or string function parameter may also be passed using a "holder" variable
ds_select_dst(1, 1); 

... is equivalent to:

$var(x) = 1;
ds_select_dst($var(x), $var(x));


  • any string function parameter can also be passed as a format string
set_dlg_profile("caller", "$var(country_code)_$var(area)_$fU");


  • input or output variables passed to functions must not be quoted:
ds_count(1, "a", $var(out_result));


  • integers no longer need to be passed as double-quoted strings:
ds_select_dst("1", "1");
ds_select_dst(1, 1);

Summary: module functions currently benefit from a more powerful calling convention than core functions. This enables a greater flexibility when passing parameters to these functions, while additionally ensuring a greater degree of consistency across all such functions.


Page last modified on August 04, 2019, at 10:58 AM