1.16 路由块 - 请求路由、响应路由、分支路由、失败路由等

1. Routing Blocks The routing blocks are the parts of the configuration file executed by kamailio at runtime. They can be seen as blocks of actions similar to functions (or procedures) from common programming languages. A routing block is identified by a specific token, followed by a name in between square brackets and actions in between curly braces. route_block_id[NAME] { ACTIONS } The name can be any alphanumeric string, with specific routing blocks enforcing a particular format. ...

8 min · Eddie Wang

1.17 脚本语法 - if、else、switch、while、赋值、比较、算数等

1. if IF-ELSE statement Prototype: if(expr) { actions; } else { actions; } The expr should be a valid logical expression. The logical operators that can be used in expr: ==: equal !=: not equal =~: case-insensitive regular expression matching: Note: Posix regular expressions will be used, e.g. use [[:digit:]]{3} instead of \d\d\d !~: regular expression not-matching (NOT PORTED from Kamailio 1.x, use !(x =~ y)) >: greater >=: greater or equal <: less <=: less or equal &&: logical AND ||: logical OR !: logical NOT Example of usage: ...

5 min · Eddie Wang

1.18 命令行参数

Command Line Parameters Kamailio can be started with a set of command line parameters, providing more flexibility to control what is doing at runtime. Some of them can be quite useful when running on containerised environments. To see the the available command line parameters, run kamailio -h: # kamailio -h version: kamailio 5.4.0-dev4 (x86_64/darwin) 8c1864 Usage: kamailio [options] Options: -a mode Auto aliases mode: enable with yes or on, disable with no or off --alias=val Add an alias, the value has to be '[proto:]hostname[:port]' (like for 'alias' global parameter) -A define Add config pre-processor define (e.g., -A WITH_AUTH, -A 'FLT_ACC=1', -A 'DEFVAL="str-val"') -b nr Maximum receive buffer size which will not be exceeded by auto-probing procedure even if OS allows -c Check configuration file for syntax errors -d Debugging mode (multiple -d increase the level) -D Control how daemonize is done: -D..do not fork (almost) anyway; -DD..do not daemonize creator; -DDD..daemonize (default) -e Log messages printed in terminal colors (requires -E) -E Log to stderr -f file Configuration file (default: /usr/local/etc/kamailio/kamailio.cfg) -g gid Change gid (group id) -G file Create a pgid file -h This help message --help Long option for `-h` -I Print more internal compile flags and options -K Turn on "via:" host checking when forwarding replies -l address Listen on the specified address/interface (multiple -l mean listening on more addresses). The address format is [proto:]addr_lst[:port][/advaddr], where proto=udp|tcp|tls|sctp, addr_lst= addr|(addr, addr_lst), addr=host|ip_address|interface_name and advaddr=addr[:port] (advertised address). E.g: -l localhost, -l udp:127.0.0.1:5080, -l eth0:5062, -l udp:127.0.0.1:5080/1.2.3.4:5060, -l "sctp:(eth0)", -l "(eth0, eth1, 127.0.0.1):5065". The default behaviour is to listen on all the interfaces. --loadmodule=name load the module specified by name --log-engine=log engine name and data -L path Modules search path (default: /usr/local/lib64/kamailio/modules) -m nr Size of shared memory allocated in Megabytes --modparam=modname:paramname:type:value set the module parameter type has to be 's' for string value and 'i' for int value, example: --modparam=corex:alias_subdomains:s:kamailio.org -M nr Size of private memory allocated, in Megabytes -n processes Number of child processes to fork per interface (default: 8) -N Number of tcp child processes (default: equal to `-n') -O nr Script optimization level (debugging option) -P file Create a pid file -Q Number of sctp child processes (default: equal to `-n') -r Use dns to check if is necessary to add a "received=" field to a via -R Same as `-r` but use reverse dns; (to use both use `-rR`) --server-id=num set the value for server_id --subst=exp set a subst preprocessor directive --substdef=exp set a substdef preprocessor directive --substdefs=exp set a substdefs preprocessor directive -S disable sctp -t dir Chroot to "dir" -T Disable tcp -u uid Change uid (user id) -v Version number --version Long option for `-v` -V Alternative for `-v` -x name Specify internal manager for shared memory (shm) - can be: fm, qm or tlsf -X name Specify internal manager for private memory (pkg) - if omitted, the one for shm is used -Y dir Runtime dir path -w dir Change the working directory to "dir" (default: "/") -W type poll method (depending on support in OS, it can be: poll, epoll_lt, epoll_et, sigio_rt, select, kqueue, /dev/poll)

3 min · Eddie Wang

1.19 日志引擎

Log Engine CLI Parameter The –log-engine parameter allows to specify what logging engine to be used, which is practically about the format of the log messages. If not set at all, then Kamailio does the classic style of line-based plain text log messages. The value of this parameter can be –log-engine=name or –log-engine=name:data. The name of the log engine can be: json - write logs in structured JSON format the data for json log engine can be a set of character flags: a - add log prefix as a special field A - do not add log prefix c - add Call-ID (when available) as a dedicated JSON attribute j - the log prefix and message fields are printed in JSON structure format, detecting if they are enclosed in between { } or adding them as a text field M - strip EOL (’\n’) from the value of the log message field N - do not add EOL at the end of JSON document p - the log prefix is printed as it is in the root json document, it has to start with comma (,) and be a valid set of json fields U - CEE (Common Event Expression) schema format - https://cee.mitre.org/language/1.0-beta1/core-profile.html Example of JSON logs when running Kamailio with “–log-engine=json:M” : ...

2 min · Eddie Wang

1.2 通用元素 - 注释、标识符、变量、值、表达式

1. Comments Line comments start with # (hash/pound character - like in shell) or / / (double forward slash - like in C++/Java). Block comments start with /* (forward slash and asterisk) and are ended by */ (sterisk and forward slash) (like in C, C++, Java). Example: # this is a line comment // this is another line comment /* this is a block comment */ Important: be aware of preprocessor directives that start with #! (hash/pound and exclamation) - those are no longer line comments. ...

2 min · Eddie Wang

1.3 预处理指令 - 文件导入、宏定义、环境变量读取

预处理指令主要的目的是方便脚本维护,方便脚本在不同环境下运行。 预处理之后,脚本才会开始解析。 1. include_file include_file "path_to_file" Include the content of the file in config before parsing. path_to_file must be a 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 it. 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. ...

5 min · Eddie Wang

1.4 关键词

Keywords specific to SIP messages which can be used mainly in if expressions. 核心关键词主要用在if语句里,如果用在xlog里打印,那么就打印的是字符串了呀 1. af The address family of the received SIP message. It is INET if the message was received over IPv4 or INET6 if the message was received over IPv6. Exampe of usage: if (af==INET6) { log("Message received over IPv6 link\n"); } 2. dst_ip The IP of the local interface where the SIP message was received. When the proxy listens on many network interfaces, makes possible to detect which was the one that received the packet. ...

3 min · Eddie Wang

1.5 核心值

Values that can be used in 'if’ expressions to check against Core Keywords 主要还是用在if语句里做比较 1. INET This keyword can be used to test whether the SIP packet was received over an IPv4 connection. Example of usage: if (af==INET) { log("the SIP message was received over IPv4\n"); } 2. INET6 This keyword can be used to test whether the SIP packet was received over an IPv6 connection. Example of usage: ...

2 min · Eddie Wang

1.6 核心参数

kamailio和核心参数有106个,控制了kamailio的运行方式。 1. advertised_address 2. advertised_port 3. alias ⭐️ 4. async_workers 5. async_nonblock 6. async_usleep 7. async_workers_group 8. auto_aliases 9. auto_bind_ipv6 10. bind_ipv6_link_local 11. check_via ⭐️ 12. children 13. chroot 14. corelog 15. debug ⭐️ 16. description 17. disable_core_dump 18. disable_tls 19. enable_tls 20. exit_timeout 21. flags 22. force_rport ⭐️ 23. fork 24. fork_delay 25. group 26. http_reply_parse 27. ip_free_bind 28. ipv6_hex_style 29. kemi.onsend_route_callback 30. kemi.received_route_callback 31. kemi.reply_route_callback 32. kemi.pre_routing_callback 33. latency_cfg_log ⭐️ 34. latency_limit_action ⭐️ 35. latency_limit_db ⭐️ 36. latency_log ⭐️ 37. listen ⭐️ 38. loadmodule 39. loadmodulex 40. loadpath 41. local_rport ⭐️ 42. log_engine_data 43. log_engine_type 44. log_facility ⭐️ 45. log_name ⭐️ 46. log_prefix ⭐️ 47. log_prefix_mode ⭐️ 48. log_stderror 49. cfgengine 50. maxbuffer ⭐️ 51. max_branches 52. max_recursive_level 53. max_while_loops ⭐️ 54. mcast 55. mcast_loopback 56. mcast_ttl 57. memdbg 58. memlog ⭐️ 59. mem_join 60. mem_safety 61. mem_status_mode 62. mem_summary 63. mhomed 64. mlock_pages 65. modinit_delay 66. modparam 67. modparamx 68. onsend_route_reply 69. open_files_limit 70. phone2tel 71. pmtu_discovery 72. port 73. pv_buffer_size 74. pv_buffer_slots 75. pv_cache_limit 76. pv_cache_action 77. rundir 78. received_route_mode 79. reply_to_via 80. route_locks_size 81. server_id 82. server_header ⭐️ 83. server_signature ⭐️ 84. shm_force_alloc 85. shm_mem_size 86. sip_parser_log 87. sip_parser_mode ⭐️ 88. sip_warning (noisy feedback) 89. socket_workers 90. sql_buffer_size ⭐️ 91. statistics ⭐️ 92. stats_name_separator 93. tos 94. udp_mtu ⭐️ 95. udp_mtu_try_proto 96. uri_host_extra_chars 97. user ⭐️ 98. user_agent_header ⭐️ 99. verbose_startup ⭐️ 100. version_table ⭐️ 101. wait_worker1_mode 102. wait_worker1_time 103. wait_worker1_usleep 104. workdir ⭐️ 105. xavp_via_params 106. xavp_via_fields 1. advertised_address 用处不大,将会废弃 ...

31 min · Eddie Wang

1.7 DNS 参数

Note: See also file doc/tutorials/dns.txt for details about Kamailio’s DNS client. Kamailio has an internal DNS resolver with caching capabilities. If this caching resolver is activated (default setting) then the system’s stub resolver won’t be used. Thus, also local name resolution configuration like /etc/hosts entries will not be used. If the DNS cache is deactivated (use_dns_cache=no), then system’s resolver will be used. The DNS failover functionality in the tm module references directly records in the DNS cache (which saves a lot of memory) and hence DNS based failover only works if the internal DNS cache is enabled. ...

7 min · Eddie Wang