Kamailio by example - Getting Started: Pre-Processor Directives

1 minute read

Pre-Processor Directives

:information_source: Note: Read the official docs to get the most updated information.

Pre-processor directives start with a #! or a !!, and are evaluated before the execution of the configuration file.


include_file - include the content of the file in config before parsing. The config files can get huge. We can split our code into different files, and organize our routing logic.

#!include_file extra.cfg
#!import_file extra.cfg // does not throw error if the included file is not found.


define is used to define a keyword ID, with an optional value. These IDs can be used set global settings.

#!define ID       // define an ID
#!define ID VALUE // define an ID with value
#!trydef          // add a define if not already defined
#!redefine        // force redefinition even if already defined

A directive can also be set in as argument, with kamailio -A ID=VALUE


defenv is similar, but with the define an ID to the value of an environment variable with the name of the ENV Variable.

#!defenv ENVVAR
#!defenv ID=ENVVAR   // define an ID with a different name
#!defenvs ENVVAR     // defenv for strings
#!trydefenv ENVVAR   // will not error if the env variable is not set
#!trydefenvns ENVVAR // trydefenv for strings
#!define CODE 200
#!defenv KAMA_LISTEN


loadmodule "sl"
modparam("sl", "bind_tm", 0)

#!include_file "0.5.0-defines.inc.cfg"
request_route {
  sl_send_reply(CODE, ENV_HOSTNAME);

We see that:

  • The CODE ID is set the response code to the request, 200.
  • The ENV_HOSTNAME ID is set with the value of the ENV HOSTNAME. Note the defenvs sets the value to a string, since that’s required for the sl_send_reply.
  • The KAMA_LISTEN is a ENV var that is used by the listen, set by defenv.
  • The routing logic is done a different file, set by include_file

How to test: Check the kamalab to prepare your lab.

# kamalab ❯
# select 0.5.0-defines.cfg
# Listening on
#              udp: []:5060
#              tcp: []:5060
# Aliases:

In another terminal let’s send OPTIONS to the server:

# >
# ...
# OPTIONS sip: SIP/2.0
# ...
# SIP/2.0 200 desktop.bandonga.com
# ...

We can see the response to our request: 200 desktop.bandonga.com



Leave a Comment