mirror of https://github.com/sipwise/kamailio.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
5.2 KiB
226 lines
5.2 KiB
# $Id$
|
|
#
|
|
# Simple application level gateway config script.
|
|
#
|
|
#
|
|
|
|
# ------------------ module loading ----------------------------------
|
|
|
|
loadmodule "./modules/mysql/mysql.so"
|
|
|
|
loadmodule "./modules/iptrtpproxy/iptrtpproxy.so"
|
|
loadmodule "./modules/maxfwd/maxfwd.so"
|
|
loadmodule "./modules/rr/rr.so"
|
|
loadmodule "./modules/tm/tm.so"
|
|
loadmodule "./modules/sl/sl.so"
|
|
loadmodule "./modules/avp_db/avp_db.so"
|
|
loadmodule "./modules/usrloc/usrloc.so"
|
|
loadmodule "./modules/nathelper/nathelper.so"
|
|
loadmodule "./modules/registrar/registrar.so"
|
|
|
|
flags
|
|
FLAG_NAT_REG,
|
|
FLAG_NAT;
|
|
|
|
|
|
avpflags
|
|
rtpproxy_dlg,
|
|
dialog_cookie;
|
|
|
|
# ----------------- setting module-specific parameters ---------------
|
|
|
|
modparam("avp_db|usrloc", "db_url", "mysql://ser:heslo@127.0.0.1/ser");
|
|
|
|
modparam("usrloc", "db_mode", 1)
|
|
|
|
modparam("rr", "cookie_secret", "MyRRAVPcookiesecret");
|
|
|
|
modparam("nathelper", "rtpproxy_disable", 1);
|
|
|
|
modparam("iptrtpproxy", "config", "/etc/iptrtpproxy.cfg");
|
|
modparam("iptrtpproxy", "switchboard", "name=*;learning-timeout-a=10;learning-timeout-b=10;ringing-timeout=90");
|
|
|
|
modparam("registrar", "save_nat_flag", "FLAG_NAT_REG");
|
|
modparam("registrar", "load_nat_flag", "FLAG_NAT_REG");
|
|
|
|
modparam("avp_db", "attr_group", "id=rtp_dlg,flag=rtpproxy_dlg,table=rtpproxy_attrs");
|
|
|
|
# ------------------ main fun below ----------------------------------
|
|
|
|
route {
|
|
|
|
if (!mf_process_maxfwd_header("10")) {
|
|
sl_send_reply("483", "Too Many Hops");
|
|
break;
|
|
};
|
|
if (msg:len > max_len) {
|
|
sl_send_reply("513", "Message too big");
|
|
break;
|
|
};
|
|
# if (method=="INVITE") {
|
|
# if (!(uri == myself)) {
|
|
# sl_send_reply("403", "Call cannot be served here");
|
|
# break;
|
|
# }
|
|
# };
|
|
|
|
if (!t_newtran()) {
|
|
sl_send_reply("500", "Internal error newtran");
|
|
break;
|
|
};
|
|
# Do strict routing if pre-loaded route headers present
|
|
if (loose_route()) {
|
|
if ($dialog_id) { # AVP loaded from cookie
|
|
if (method == "INVITE" || method == "UPDATE") { # TODO: check if there is a SDP
|
|
# re-INVITEs
|
|
if (@route[0].ftag == @from.tag) {
|
|
$gate_a_to_b = 1;
|
|
if ($init_dialog)
|
|
$tmp = 3;
|
|
else
|
|
$tmp = 1;
|
|
}
|
|
else {
|
|
$gate_a_to_b = 0;
|
|
if ($init_dialog)
|
|
$tmp = 2;
|
|
else
|
|
$tmp = 0;
|
|
}
|
|
resetflag("FLAG_NAT");
|
|
# test if caller is behind NAT
|
|
if (nat_uac_test("18")) {
|
|
setflag("FLAG_NAT");
|
|
}
|
|
# is callee behind NAT? we know it from registration
|
|
if (lookup("location")) {
|
|
if (isflagset("FLAG_NAT_REG"))
|
|
setflag("FLAG_NAT");
|
|
}
|
|
else {
|
|
sl_reply("404", "Unknown user in To");
|
|
break;
|
|
}
|
|
|
|
if ($sess_ids) { # pending non-confirmed sessions?
|
|
iptrtpproxy_delete("$sess_ids");
|
|
$sess_ids = "";
|
|
}
|
|
if (isflagset("FLAG_NAT")) {
|
|
if (!iptrtpproxy_alloc("$tmp", "my")) {
|
|
sl_reply("500", "RTP proxy error");
|
|
break;
|
|
}
|
|
$sess_ids = @iptrtpproxy.session_ids;
|
|
t_on_reply("invite_response");
|
|
t_on_failure("invite_failure");
|
|
}
|
|
}
|
|
else if (method == "BYE") { # CANCEL ?
|
|
t_on_reply("bye_response");
|
|
}
|
|
}
|
|
t_relay();
|
|
break;
|
|
};
|
|
|
|
if (method=="REGISTER") {
|
|
|
|
if (nat_uac_test("18")) {
|
|
setflag("FLAG_NAT_REG");
|
|
}
|
|
|
|
if (!save_contacts("location")) {
|
|
sl_reply("400", "Invalid REGISTER Request");
|
|
break;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if (method=="INVITE" && @to.tag=="") {
|
|
$dialog_id = @sys.unique;
|
|
$init_dialog = 1;
|
|
|
|
# test if caller is behind NAT
|
|
if (nat_uac_test("18")) {
|
|
setflag("FLAG_NAT");
|
|
}
|
|
|
|
# is callee behind NAT? we know it from registration
|
|
if (lookup("location")) {
|
|
if (isflagset("FLAG_NAT_REG"))
|
|
setflag("FLAG_NAT");
|
|
}
|
|
else {
|
|
sl_reply("404", "Unknown user in To");
|
|
break;
|
|
}
|
|
# test if call comming at gate a or b by ip, port etc.
|
|
$gate_a_to_b = 1; # TODO: we do not consider two gates now, a->b & ringing-timeout
|
|
|
|
if (isflagset("FLAG_NAT")) {
|
|
if (iptrtpproxy_alloc("3", "my")) {
|
|
$sess_ids = @iptrtpproxy.session_ids;
|
|
t_on_reply("invite_response");
|
|
t_on_failure("invite_failure");
|
|
}
|
|
}
|
|
|
|
setavpflag("$f.dialog_id", "dialog_cookie");
|
|
record_route(); # it will add ftag to record-route, we can check direction
|
|
}
|
|
|
|
if (!t_relay()) {
|
|
sl_reply_error();
|
|
}
|
|
|
|
}
|
|
|
|
onreply_route["invite_response"] {
|
|
if (status=~"18[0-9]" || status=~"2[0-9][0-9]") {
|
|
if ($gate_a_to_b == "1") {
|
|
if ($init_dialog && status=~"18[0-9]")
|
|
$tmp = 2; # ringing-timeout
|
|
else
|
|
$tmp = 0;
|
|
}
|
|
else {
|
|
if ($init_dialog && status=~"18[0-9]")
|
|
$tmp = 3; # ringing-timeout
|
|
else
|
|
$tmp = 1;
|
|
}
|
|
if (iptrtpproxy_update("$tmp", "$sess_ids")) {
|
|
if (status=~"2[0-9][0-9]") {
|
|
if ($init_dialog) {
|
|
iptrtpproxy_adjust_timeout("$gate_a_to_b", "$sess_ids"); # decrease ringing-timeout
|
|
}
|
|
# delete old session, e.g. in case of re-INVITE
|
|
load_extra_attrs("rtp_dlg", "$dialog_id");
|
|
iptrtpproxy_delete("$dlg_sess_ids");
|
|
# save session ids
|
|
$dlg_sess_ids = @iptrtpproxy.session_ids;
|
|
setavpflag("$f.dlg_sess_ids", "rtpproxy_dlg");
|
|
save_extra_attrs("rtp_dlg", "$dialog_id");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
failure_route["invite_failure"] {
|
|
if ($sess_ids) {
|
|
iptrtpproxy_delete("$sess_ids");
|
|
$sess_ids = "";
|
|
}
|
|
}
|
|
|
|
onreply_route["bye_response"] {
|
|
if (status=~"2[0-9][0-9]") {
|
|
load_extra_attrs("rtp_dlg", "$dialog_id");
|
|
iptrtpproxy_delete("$dlg_sess_ids");
|
|
remove_extra_attrs("rtp_dlg", "$dialog_id");
|
|
}
|
|
}
|
|
|