使用场景
registrar
和mid_registrar
都是用来存储用户注册信息的模块,mid_registrar
主要是充当注册中间件,需要代理转发,
另外也可以降低被代理方的注册请求的次数。registrar
模块也支持代理转发,但是不能拦截注册请求到被代理方。
两个模块的数据都缓存在usrloc
里。
使用示例
opensips版本:
opensips 3.3.10 (x86_64/linux)
registrar
重要参数介绍
default_expires
: 默认注册过期时间,单位秒
min_expires
: 最小注册过期时间,Contact中的过期时间小于此值,会被设置为此值,单位秒
max_expires
: 最大注册过期时间,Contact中的过期时间大于此值,会被设置为此值,单位秒
tcp_presentent_flag
: 此参数会设置tcp连接的生命周期为contact的过期时间,如果设置为-1,则不设置。
received_avp
: 存储received字段的avp,常常和nathelper
模块一起使用.
recevied_param
: 设置200OK信令的Contact
中的received
字段,默认为received
max_contacts
: 限制每个用户最大注册数的contact,超过会报错5xx。
代理配置示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
socket=udp:172.16.4.111:5360
socket=tcp:172.16.4.111:5360
socket=udp:172.16.4.111:5361
loadmodule "usrloc.so"
loadmodule "registrar.so"
loadmodule "nathelper.so"
modparam("registrar|nathelper", "received_avp", "$avp(rcv)")
modparam("registrar", "default_expires", 1800)
modparam("registrar", "tcp_persistent_flag", "TCP_PERSIST_DURATION")
route {
...
if (is_method("REGISTER")) {
# indicate that the client supports DTLS
# so we know when he is called
if (isflagset("SRC_WS"))
setbflag("DST_WS");
fix_nated_register();
if (!save("location","rfc1"))
sl_reply_error();
if ($socket_in(proto) == "tcp") {
setflag("TCP_PERSIST_DURATION");
xlog("L_DBG","[$cfg_line][$ci]---main-1-:$rm|$tu|$socket_in(port)|$socket_in(proto)\n");
}
$socket_out = "udp:172.16.4.111:5361";
sethostport("172.16.4.114:5060");
if (!t_relay()) {
send_reply(500, "Internal Error");
}
exit;
}
if (!lookup("location","m")) {
t_newtran();
t_reply(404, "Not Found");
exit;
}
route(relay);
}
|
坐席的信令图:

save("location","rfc1")
: rfc1 表示usrloc一个用户只保存一个Contact信息,这样在webrtc连接中,及时更新往新的连接发送请求地址。
如果opensips中坐席过期时间小于被代理方,opensips过期之后不会重新往被代理方发送注册请求。
mid_registrar
代理配置示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
socket=udp:172.16.4.111:5326
socket=udp:172.16.4.111:5360
socket=tcp:172.16.4.111:5360
socket=udp:172.16.4.111:5361
loadmodule "mid_registrar.so"
modparam("mid_registrar", "mode", 0)
modparam("mid_registrar|nathelper", "received_avp", "$avp(rcv)")
modparam("mid_registrar", "outgoing_expires", 80)
modparam("mid_registrar", "tcp_persistent_flag", "TCP_PERSIST_REGISTRATIONS")
modparam("mid_registrar", "max_expires", 130)
modparam("mid_registrar", "attr_avp", "$avp(attr)")
route {
...
if (is_method("REGISTER")) {
# indicate that the client supports DTLS
# so we know when he is called
if (isflagset("SRC_WS"))
setbflag("DST_WS");
#fix_nated_register();
mid_registrar_save("location");
switch ($retcode) {
case 1:
if ($socket_in(proto) == "tcp") {
setflag("TCP_PERSIST_REGISTRATIONS");
xlog("L_DBG","[$cfg_line][$ci]---main-1-:$rm|$tu|$socket_in(port)|$socket_in(proto)\n");
}
$socket_out = "udp:172.16.4.111:5361";
$ru = "sip:172.16.4.114:5060";
if (!t_relay()) {
send_reply(500, "Internal Error");
}
break;
case 2:
xlog("L_INFO", "REGISTER has been absorbed!\n");
break;
default:
xlog("L_ERR", "mid-registrar error!\n");
send_reply(500, "Server Internal Error 2");
}
exit;
}
}
|
当outgoing_expires大于max_expires时:

能够得出的结论为:
- 软电话的Expires:200, opensips的过期时间为:130,此时发送给114的Expires为200.
114保存的过期时间为200, 而opensips保存的过期时间为130,返回给软电话的过期时间为130.
- 软电话130s过期之后,会重新发送Register,此时opensips会转发请求给114机器。
当outgoing_expires小于max_expires时:

当outgoing_expires 比opensips的max_expires很大时:
sip信息:

能够得出的结论为:
- opensips保存的expires为80, 被代理方114机器保存的expires为:outgoing_expires
- 当软电话的过期时间到了之后,opensips不会再转发请求到114.
当outgoing_expires 比opensips的max_expires较大时:
sip信息:

能够看到,opensips内部应该有对被代理方Expires的计数器,当收到软电话的注册信息时,如果被代理方的过期时间还很长,便不会转发请求到被代理方,时间较短时,会把注册请求重新转发给被代理方。
当outgoing_expires小于max_expires时:
此时可以看到软电话过期时,opensips会转发请求到被代理方。
当outgoing_expires很大时:
和mode=1 情况一样
当outgoing_expires大于max_expires时:

114上的坐席过期之后,opensips不会再往114转发请求。
当outgoing_expires小于max_expires时:
sip信息:

此时,被代理方114机器的过期时间到了之后,坐席信息被删除,之后的REGISTER并未再转发到114机器上,导致114机器上一直都没有坐席的信息。
总结
- mode=0 时,被代理方的过期时间和软电话传的一致, opensips的过期时间为max_expires时间。
- mode=1 时,被代理方的过期时间和outgoing_expires时间一致,opensips过期时间为max_expires时间。
- mode=2 时,opensips不会再转发REGISTER到114机器上,即使被代理方的坐席过期。