定一个需求:用户注册到Kamailio,FreeSWITCH把INVITE送到Kamailio,后者要么Relay到注册用户,要么Relay到落地网关(比如Vos),那么Kamailio的路由脚本要怎么写呢?
我们首先在fs里面配置两个网关:
<include>
<gateway name="user">
<param name="username" value="fs"/>
<param name="proxy" value="192.168.1.125"/> <!--指向kamailio-->
<param name="register" value="false"/>
<param name="register-transport" value="udp"/>
<param name="caller-id-in-from" value="true"/>
<!--extra sip params to send in the contact-->
<param name="contact-params" value="a=1;b=2"/> <!--contact参数可以在kamailio里面访问到,这里给的仅仅是个例子-->
<!-- Put the extension in the contact -->
<param name="extension-in-contact" value="true"/>
</gateway>
</include>
<include>
<gateway name="vos">
<param name="username" value="fs"/>
<param name="proxy" value="192.168.1.125"/> <!--指向kamailio-->
<param name="register" value="false"/>
<param name="register-transport" value="udp"/>
<param name="caller-id-in-from" value="true"/>
<!--extra sip params to send in the contact-->
<param name="contact-params" value="destination=192.168.1.100"/> <!--vos地址-->
<!-- Put the extension in the contact -->
<param name="extension-in-contact" value="true"/>
</gateway>
</include>
现在执行这个命令 fs_cli -x 'originate sofia/gateway/user/1001 &echo'
发出来的INVITE包是这样的:
INVITE sip:1001@192.168.1.125 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.125:5080;rport;branch=z9hG4bKe0rH75XD3gKFc
Max-Forwards: 70
From: <sip:0000000000@192.168.1.125>;tag=FU8vg8U231ZZj
To: <sip:1001@192.168.1.125>
Call-ID: a6bb9ac8-2cfb-123c-0fa8-080027683759
CSeq: 63962709 INVITE
Contact: <sip:fs@192.168.1.125:5080;a=1;b=2;transport=udp;gw=user>
...
fs_cli -x 'originate sofia/gateway/vos/13812345678 &echo'
INVITE sip:13812345678@192.168.1.125 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.125:5080;rport;branch=z9hG4bKBy95pU40ar6vD
Max-Forwards: 70
From: <sip:0000000000@192.168.1.125>;tag=BycjtjZFBBecc
To: <sip:13812345678@192.168.1.125>
Call-ID: cc398b08-2cfe-123c-0fa8-080027683759
CSeq: 63963384 INVITE
Contact: <sip:fs@192.168.1.125:5080;destination=192.168.1.100;transport=udp;gw=vos>
...
请留意,两个包的Contact头有所不同
Kamailio的路由脚本如下:
-
先定义一个事务标志
#!define FLT_FROM_FS 10 /*范围是0到31之间,跟其他的事务标志不冲突*/
-
REQINIT子路由里面检查$si是否来自FreeSWITCH
route[REQINIT] {
if(ds_is_from_list("100", "3")) setflag(FLT_FROM_FS); /* 100 是fs的组号 */
...
}
-
AUTH子路由里面跳过对fs的认证
route[AUTH] {
if (isflagset(FLT_FROM_FS)) return;
...
}
-
INVITE_FROM_FS子路由里面处理
route[INVITE_FROM_FS] {
if (!has_totag()) return;
if (!is_method("INVITE")) return;
if (!isflagset(FLT_FROM_FS)) return;
$var(contact_params) = @contact.uri.params; /* 这是select, 可参考本文末尾提供的链接*/
xinfo("***$var(contact_params)***\n");
$var(gw) = $(var(contact_params){param.value,gw}); /* 这是transformation */
xinfo("gw = $var(gw)\n");
$var(destination) = $(var(contact_params){param.value,destination});
xinfo("destination = $var(destination)\n");
if ($var(gw) == "user") {
route(LOCATION); /* 呼叫注册用户 */
} else {
$du = "sip:" + $var(destination); /* 落地网关地址 */
route(RELAY);
exit;
}
}
关于select,参考这里:
cookbooks:devel:selects [Kamailio SIP Server Wiki]
关于transformation,参考这里:
cookbooks:devel:transformations [Kamailio SIP Server Wiki]
路由脚本参考这里:
https://github.com/kamailio/kamailio/blob/master/etc/kamailio.cfg
我做测试用的是fs1.10.7版本,INVITE包里面的contact头有点小问题,但1.10.8修复的很好。