Featured image of post kamailio dialplan 介绍

kamailio dialplan 介绍

背景介绍

dialplan 主要用于号码转换,规则匹配替换等. 必须依赖数据库,从数据库读取规则。

kamailio 的版本:

version: kamailio 5.8.5 (x86_64/linux)

参数解析

1
2
3
4
5
6
7
8
# 指定数据库地址
modparam("dialplan", "db_url", DBURL)
# 获取表字段attrs存储的的变量
modparam("dialplan", "attrs_pvar", "$avp(s:dest)")
# dialplain 是否动态匹配 
modparam("dialplan", "match_dynamic", 1)
# 重新加载的延迟时间,默认5s
modparam("dialplan", "reload_delta", 1)

重要函数

dp_replace(dpid, inval, outvar)

根据dpid规则,把inval替换成dpid表达式下的数据存储在outvar中.

dp_match(dpid, inval)

根据dpid规则匹配inval

dp_translate(id, [src[/dest]])

根据id规则转换srcdest,如果没有dest,只匹配src,并存储attrs$avp(s:dest)里。此id还是dpid

实战

构造数据

  1. 表结构
    name type size default null key extra attributes description
    id unsigned int 10 no primary auto increment unique ID
    dpid int 11 no Dialplan ID
    pr int 11 no Priority of rule
    match_op int 11 no Matching operator for rule (0-equal, 1-regexp)
    match_exp string 64 no Matching expression (regexp or string)
    match_len int 11 no Length of the matching expression (used only for equal operator)
    subst_exp string 64 no Substitution expression
    repl_exp string 256 no Replacement expression (sed like)
    attrs string 64 no General attributes string to be returned in case of rule matching

所有的字段都要填写

  1. 数据
1
INSERT INTO `dialplan` VALUES (1,100,10,0,'1004',0,'1004','200$fU','test');`

配置示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
loadmodule "dialplan.so"
modparam("dialplan", "db_url", DBURL)
modparam("dialplan", "attrs_pvar", "$avp(s:dest)")
modparam("dialplan", "match_dynamic", 1)
modparam("dialplan", "reload_delta", 1)
...
request_route {
        ...
        dp_replace("100", "$fU", "$var(newru)");
        xlog("L_INFO","$fU:$var(newru), $avp(s:dest)\n");
        # per request initial checks
        route(REQINIT);
        ...
}

目前使用的规则是号码添加前缀200, 实际效果打印的日志为:

1
<script>: 1004:2001004, test
本博客已稳定运行
发表了26篇文章 · 总计45.09k字
本站总访问量 次 · 您是本站第 位访问者
粤ICP备2025368587号-1| 使用 Hugo 构建
主题 StackJimmy 设计