Kamailio-kamailio.cfg

From 탱이의 잡동사니
Jump to navigation Jump to search

Overview

kamailio.cfg 파일 내용 정리

Basic

kamailio.cfg 파일은 크게 세가지 영역으로 구분된다.

  • global parameters
  • modules settings
  • routing blocks

Global Parameters Section

Configuration 파일의 첫번째 부분으로, Kamailio 의 core 와 custom global parameter 설정을 한다.

일반적으로 다음과 같이 설정된다.

name=value

name 부분은 core parameter 에 나와있는 항목이어야 한다. 만약 항목에 없는 name 을 설정했다면, kamailio 는 에러를 발생시키고 시작하지 앟는다.

value 부분은 integer, boolean, string 으로 설정한다. 하지만 몇몇 parameter 들은 복합된 형식의 value 로도 설정이 가능하다.

log_facility=LOG_LOCAL0

children=4

disable_tcp=yes

alias="sip.mydomain.com"

listen=udp:10.0.0.10:5060

대부분의 parameter의 설정은 라인의 끝부분까지이지만, 세미콜론(;)을 사용할 수도 있다. 이는 parameter 설정이 한번에 여러개의 값을 설정할 수 있도록 허용한 경우에 사용할 수 있다.

alias="sip.mydomain.com";

만약 reserved config keyword 를 parameter로 사용하고 싶다면, ""로 감싸야 한다. 아래의 예에서 reserved config keyword 는 "dns"이다.

listen=tcp:127.0.0.1:5060 advertise "sip.dns.example.com":5060

Modules Settings Section

Configuration file 의 두번째 부분으로써, module 들을 로드하는 지시어와 설정값들로 이루어져 있다.

설정의 첫부분에는 mpath 지시어로 모듈 디렉토리를 지정한다.

/* set paths to location of modules */
# mpath="/usr/local/lib64/kamailio/modules/"

loadmoudle 과 modparam 지시어가 사용된다.

loadmodule "debugger.so"
...
modparam("debugger", "cfgtrace", 1)

Routing Blocks Section

Configuration 파일의 마지막 부분으로서 SIP 트래픽의 routing logic 을 설정한다.

가장 중요한 routing block 은 request_route 이다. 이 request_route 에서 SIP 트래픽의 경로를 결정한다.

request_route {
 
    # per request initial checks
    route(REQINIT);
 
    ...
}
 
branch_route[MANAGE_BRANCH] {
    xdbg("new branch [$T_branch_idx] to $ru\n");
    route(NATMANAGE);
}

Generic Elements

Comments

  1. 혹은 // 로 시작하는 라인은 해당 라인이 코멘트로 인식된다.

/* 과 */ 에 포함되는 모든 부분도 코멘트로 인식된다.

  # this is a line comment
  
  // this is another line comment
  
  /* this
     is
     a
     block
     comment */

단, #! 로 시작되는 preprocessor 지시어는 코멘트 처리되지 않는다.

Values

세가지 타입의 value 가 있다.

  • integer: Numbers of 32-bit size.
  • boolean: aliases to 1(true, on, yes) or 0(false, off, no).
  • string: tokens enclosed in between double or single quotes.
// next two are strings
 
  "this is a string value"
  'this is another string value"
 
// next is a boolean
 
  yes
 
// next is an integer
 
  64

Identifiers

Identifiers are tokens which are not enclosed in single or double quotes and to match the rules for integer or boolean values.

return

Variables

Variable 은 $로 시작한다.

$var(x) = $rU + "@" + $fd;

Actions

An action is an element used inside routing blocks ended by ;(semicolon). It can be an execution of a function from core or module, a conditional or loop statement, an assignment expression.

  sl_send_reply("404", "Not found");
  exit;

Expressions

An expression is an association group of statements, variables, functions and operatiors.

if(!t_relay())
 
if($var(x)>10)
 
"sip:" + $var(prefix) + $rU + "@" + $rd

Config Pre-Processor Directives

include_file

   include_file "path_to_file"

Include the content of the file in config before parsing.

path_to_file 은 반드시 static string 이어야 한다. Including file operation is done at startup. If you change the content of included file, you have to restart the SIP server to become effective.

The path_to_file can be relative or absolute. If it is not absolute path, first attempt is to locate it relative to current directory, and if fails, relative to directory of the file that includes int. There is no restriction where include can be used or what can contain - any part of config file is ok. There is a limit of maximum 10 includes in depth, otherwise you can use as many includes as you want. Reporting of the cfg file syntax errors prints now the file name for easier troubleshooting.

If the included file is not found, the config file parser throws error. You can find this error message at the logging destination, usually in the system logging file.

You can use also the syntax #!include_file or !!include_file.

route {
    ...
    include_file "/sr/checks.cfg"
    ...
}
 
--- /sr/checks.cfg ---
 
   if (!mf_process_maxfwd_header("10")) {
       sl_send_reply("483","Too Many Hops");
       exit;
   }
 
---

import_file

   import_file "path_to_file"

Similar to include_file, but does not throw error if the include file is not found.

define

Control in C-style what parts of the config file are executed. The parts in non-defined-zones are not loaded, ensuring lower memory usage and faster execution.

Available directives:

  • #!define NAME: define a keyword
  • #!define NAME VALUE: define a keyword with value.
  • #!ifdef NAME: check if a keyword is defined
  • #!ifndef: check if a keyword is not defined.
  • #!else: switch to false branch of ifdef/ifndef region.
  • #!endif: end ifdef/ifndef region.
  • #!trydef: add a define if not already define.
  • #!redefine: forse readefinition even if already defined.

Among benefits:

  • easy way to enable/disable features(e.g., see default cfg - controlling support of nat traversal, presence, etc...).
  • switch control for parts where conditional statements were not possible(e.g., global parameters, module settings).
  • faster by not using conditional statements inside routing blocks when switching between running environments.
...
 
#!define TESTBED_MODE
 
#!ifdef TESTBED_MODE
  debug=5
  log_stderror=yes
  listen=192.168.1.1
#!else
  debug=2
  log_stderror=no
  listen=10.0.0.1
#!endif
 
...
 
#!ifdef TESTBED_MODE
modparam("acc|auth_db|usrloc", "db_url",
	"mysql://kamailio:kamailiorw@localhost/kamailio_testbed")
#!else
modparam("acc|auth_db|usrloc", "db_url",
	"mysql://kamailio:kamailiorw@10.0.0.2/kamailio_production")
#!endif
 
...
 
#!ifdef TESTBED_MODE
route[DEBUG] {
  xlog("SCRIPT: SIP $rm from: $fu to: $ru - srcip: $si"\n);
}
#!endif
 
...
 
route {
#!ifdef TESTBED_MODE
  route(DEBUG);
#!endif
 
  ...
}
 
...

You can define values for IDs.

#!define MYINT 123
#!define MYSTR "xyz"

Defined IDs are replaced at startup, during config parsing, e.g.,

$var(x) = 100 + MYINT;

is interpreted as

$var(x) = 100 + 123;

You can have multi-line defined IDs.

#!define IDLOOP $var(i) = 0; \
                while($var(i)<5) { \
                    xlog("++++ $var(i)\n"); \
                    $var(i) = $var(i) + 1; \
                }

Then in routing block

route {
    ...
    IDLOOP
    ...
}

Number of allowed defines is not set to 256.

Note:

  • Multilines defines are reduced to single line, so line counter should be fine.
  • Column counter goes inside the define value, but you have to omit the '\' and CR for the accurate inside-define position.
  • Text on the same line as the directive will cause problem. Keep the directive lines clean and only comment on a line before or after.

subst

  • Perform substitutions inside the strings of config (note that define is replacing only IDs - alphanumeric tokens not enclosed in quotes).
  • #!subst offers an easy way to search and replace inside strings before cfg parsing.
#!subst "/regexp/subst/flags"
  • flag is optinal and can be below.
    • 'i': ignore case.
    • 'g': global replacement.

See also