diff --git a/kamailio/db_kazoo.cfg b/kamailio/db_kazoo.cfg index d28d125..4989a32 100644 --- a/kamailio/db_kazoo.cfg +++ b/kamailio/db_kazoo.cfg @@ -3,7 +3,6 @@ #!trydef KZ_DB_HOOK_TRACE 1 #!trydef KZ_DB_TRACE 0 #!trydef KZ_DB_TRACE_LOG_LEVEL 3 -#!trydef KZ_DB_MAX_ROWS 0 #!trydef KZ_DB_TRACE_FILTER 110 loadmodule "db_kazoo.so" @@ -11,6 +10,5 @@ modparam("db_kazoo", "trace_hook", KZ_DB_HOOK_TRACE) modparam("db_kazoo", "trace_enable", KZ_DB_TRACE) modparam("db_kazoo", "trace_log_level", KZ_DB_TRACE_LOG_LEVEL) modparam("db_kazoo", "trace_filter", KZ_DB_TRACE_FILTER) -modparam("db_kazoo", "max_rows", KZ_DB_MAX_ROWS) include_file "db_queries_kazoo.cfg" diff --git a/kamailio/default.cfg b/kamailio/default.cfg index 4d90022..9e21478 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -267,6 +267,9 @@ include_file "nodes-role.cfg" #!ifdef SIP_TRACE_ROLE include_file "sip_trace-role.cfg" #!endif +#!ifdef KEEPALIVE_ROLE +include_file "keepalive.cfg" +#!endif ## auth ## include_file "auth.cfg" @@ -423,7 +426,7 @@ route[HANDLE_OPTIONS] sl_send_reply("200", "Rawr!!"); - #!ifdef NAT_TRAVERSAL_ROLE + #!ifdef KEEPALIVE_ROLE route(KEEPALIVE_ON_OPTIONS); #!endif } @@ -463,7 +466,6 @@ route[HANDLE_NOTIFY] } #!endif - if($hdr(Event) == "keep-alive") { xlog("L_INFO", "$ci|stop|replying to keep alive\n"); sl_send_reply("405", "Stay Alive / Method Not Allowed"); @@ -472,7 +474,7 @@ route[HANDLE_NOTIFY] sl_send_reply("200", "Rawr!!"); } - #!ifdef NAT_TRAVERSAL_ROLE + #!ifdef KEEPALIVE_ROLE route(KEEPALIVE_ON_NOTIFY); #!endif diff --git a/kamailio/keepalive.cfg b/kamailio/keepalive.cfg index 64c1b20..b7d28e1 100644 --- a/kamailio/keepalive.cfg +++ b/kamailio/keepalive.cfg @@ -1,5 +1,6 @@ ######## KEEPALIVE PINGING ######## +#!trydef KEEPALIVE_ENABLED 1 #!trydef KEEPALIVE_NAT_ONLY 0 #!trydef KEEPALIVE_UDP_ONLY 0 #!trydef KEEPALIVE_TIMERS 4 @@ -31,6 +32,7 @@ kazoo.keepalive_expired_registration_action = KEEPALIVE_EXPIRED_REGISTRATION_ACT kazoo.keepalive_expired_subscription_action = KEEPALIVE_EXPIRED_SUBSCRIPTION_ACTION descr "action when presence expires a subscription, 1 = delete , 2 = disable, 0 = none" kazoo.keepalive_on_registration_action = KEEPALIVE_ON_REGISTRATION_ACTION descr "action on registration, 1 = insert in keepalive , 0 = none" kazoo.keepalive_on_subscription_action = KEEPALIVE_ON_SUBSCRIPTION_ACTION descr "action on subscription, 1 = insert in keepalive , 0 = none" +kazoo.keepalive_enable = KEEPALIVE_ENABLED descr "enable keepalive, 1 = on , 0 = off" modparam("rtimer", "timer", "name=keepalive_timer;interval=1;mode=KEEPALIVE_S_TIMERS;") modparam("rtimer", "exec", "timer=keepalive_timer;route=KEEPALIVE_TIMER") @@ -38,8 +40,8 @@ modparam("rtimer", "exec", "timer=keepalive_timer;route=KEEPALIVE_TIMER") modparam("rtimer", "timer", "name=keepalive_db_timer;interval=1;mode=1;") modparam("rtimer", "exec", "timer=keepalive_db_timer;route=KEEPALIVE_DB_TIMER") -modparam("rtimer", "timer", "name=keepalive_cleanup;interval=5;mode=1;") -modparam("rtimer", "exec", "timer=keepalive_cleanup;route=KEEPALIVE_CLEANUP") +##modparam("rtimer", "timer", "name=keepalive_cleanup;interval=5;mode=1;") +##modparam("rtimer", "exec", "timer=keepalive_cleanup;route=KEEPALIVE_CLEANUP") modparam("mqueue","mqueue", "name=keepalive_db_queue") @@ -66,6 +68,8 @@ modparam("statistics","variable", "keepalive:removed_from_expired_registration") modparam("statistics","variable", "keepalive:disabled_from_expired_subscription") modparam("statistics","variable", "keepalive:removed_from_expired_subscription") +modparam("htable", "htable", "keepalive=>size=32;") + route[KEEPALIVE_DB_TIMER] { $var(runloop) = 1; @@ -92,42 +96,58 @@ route[KEEPALIVE_DB_TIMER] route[KEEPALIVE_CLEANUP] { - $var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 where selected < 3 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold)); - sql_query("cb", "$var(Query)"); + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; - if($sel(cfg_get.kazoo.keepalive_expire_registrations) == 1) { - $var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9)); - sql_query("cb", "$var(Query)"); - $var(stat_update) = $_s(+$sqlrows(cb)); - update_stat("keepalive:expired_registrations", "$var(stat_update)"); - } + $var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 WHERE slot = $var(slot) AND selected = 0 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold)); +# $var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 where selected < 3 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold)); + sql_query("cb", "$var(Query)"); - if($sel(cfg_get.kazoo.keepalive_expire_subscriptions) == 1) { - $var(Query) = $_s(DELETE FROM active_watchers where id in(select b.id from w_keepalive_contact a inner join w_watchers_contact b on a.contact = b.contact where selected = 9)); + if($sqlrows(cb) > 0) { + + if($sel(cfg_get.kazoo.keepalive_expire_registrations) == 1) { + if($def(REGISTRAR_DB_MODE) == 3) { + $var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9)); + sql_query("cb", "$var(Query)"); + $var(stat_update) = $_s(+$sqlrows(cb)); + update_stat("keepalive:expired_registrations", "$var(stat_update)"); + } else { + $var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9)); + sql_query("cb", "$var(Query)"); + $var(stat_update) = $_s(+$sqlrows(cb)); + update_stat("keepalive:expired_registrations", "$var(stat_update)"); + } + } + + if($sel(cfg_get.kazoo.keepalive_expire_subscriptions) == 1) { + $var(Query) = $_s(DELETE FROM active_watchers where id in(select b.id from w_keepalive_contact a inner join w_watchers_contact b on a.contact = b.contact where selected = 9)); + sql_query("cb", "$var(Query)"); + $var(stat_update) = $_s(+$sqlrows(cb)); + update_stat("keepalive:expired_subscriptions", "$var(stat_update)"); + } + + if($sel(cfg_get.kazoo.keepalive_failed_action) == 2) { + ## disable + $var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected = 9); + $var(stat) = "keepalive:disabled"; + } else if($sel(cfg_get.kazoo.keepalive_failed_action) == 1) { + ## delete - will be recreated on registration/subscription with same contact + $var(Query) = $_s(DELETE FROM keepalive where selected = 9); + $var(stat) = "keepalive:removed"; + } sql_query("cb", "$var(Query)"); $var(stat_update) = $_s(+$sqlrows(cb)); - update_stat("keepalive:expired_subscriptions", "$var(stat_update)"); - } - - if($sel(cfg_get.kazoo.keepalive_failed_action) == 2) { - ## disable - $var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected = 9); - $var(stat) = "keepalive:disabled"; - } else if($sel(cfg_get.kazoo.keepalive_failed_action) == 1) { - ## delete - will be recreated on registration/subscription with same contact - $var(Query) = $_s(DELETE FROM keepalive where selected = 9); - $var(stat) = "keepalive:removed"; + update_stat("$var(stat)", "$var(stat_update)"); } - sql_query("cb", "$var(Query)"); - $var(stat_update) = $_s(+$sqlrows(cb)); - update_stat("$var(stat)", "$var(stat_update)"); } route[KEEPALIVE_TIMER] { + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; + $var(base_slot) = $rtimer_worker * $sel(cfg_get.kazoo.keepalive_interval); $var(slot) = $var(base_slot) + $var(tick); $var(Query) = $_s(UPDATE keepalive SET selected = 1 WHERE slot = $var(slot) AND selected = 0 AND time_sent < datetime('now', '-$sel(cfg_get.kazoo.keepalive_interval) seconds')); +## xlog("L_NOTICE", "SQLTIMER ($var(base_slot) + $var(tick))> $var(Query)\n"); $var(sqlres) = sql_query("cb", "$var(Query)"); if($var(sqlres) < 0) { xlog("L_ERROR", "$rtimer_worker|$var(tick)|log|error running query : $var(Query)\n"); @@ -135,6 +155,8 @@ route[KEEPALIVE_TIMER] $var(nrows) = $sqlrows(cb); xlog("L_DEBUG", "$rtimer_worker|$var(tick)|log|selected $var(nrows) endpoints to ping\n"); } + + route(KEEPALIVE_CLEANUP); $var(Query) = $_s(SELECT id, contact, sockinfo from keepalive WHERE slot = $var(slot) AND selected = 1); xlog("L_DEBUG", "$rtimer_worker|$var(tick)|timer|SQL => $var(Query)\n"); @@ -219,6 +241,7 @@ route[KEEPALIVE_PING] route[KEEPALIVE_ON_REGISTRATION] { + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; if($sel(cfg_get.kazoo.keepalive_on_registration_action) == 0) { return; @@ -227,6 +250,10 @@ route[KEEPALIVE_ON_REGISTRATION] if($proto == "ws" || $proto == "wss") { return; } + + if($sht(keepalive=>$si~$sp~$prid) != $null) { + return; + } if (isbflagset(FLB_NATB)) { if(!isbflagset(FLB_NATSIPPING)) { @@ -259,24 +286,28 @@ route[KEEPALIVE_ON_REGISTRATION] route[KEEPALIVE_ON_SUBSCRIBE] { + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; + if($sel(cfg_get.kazoo.keepalive_on_subscription_action) == 0) { return; } - if(!( ($sel(cfg_get.kazoo.keepalive_udp_only) == 1 && $proto != "udp") - || (!isflagset(FLT_NATS) && $sel(cfg_get.kazoo.keepalive_nat_only) == 1) - || ($proto == "ws" || $proto == "wss") - )) { - $var(max_slots) = $sel(cfg_get.kazoo.keepalive_interval) * KEEPALIVE_S_TIMERS; - $var(slot) = $(subs(contact){s.corehash, $var(max_slots)}); - $var(alias) = $(subs(contact){uri.param,alias}); - $var(sql) = $_s(INSERT OR IGNORE INTO keepalive (contact, received, sockinfo, slot) values("$subs(contact)", "$var(alias)", "$subs(sockinfo)", $var(slot))); - mq_add("keepalive_db_queue", "keepalive:from_subscription", "$var(sql)"); + if($sht(keepalive=>$si~$sp~$prid) != $null) { + return; } + + $var(max_slots) = $sel(cfg_get.kazoo.keepalive_interval) * KEEPALIVE_S_TIMERS; + $var(slot) = $(subs(contact){s.corehash, $var(max_slots)}); + $var(alias) = $(subs(contact){uri.param,alias}); + $var(sql) = $_s(INSERT OR IGNORE INTO keepalive (contact, received, sockinfo, slot) values("$subs(contact)", "$var(alias)", "$subs(sockinfo)", $var(slot))); + mq_add("keepalive_db_queue", "keepalive:from_subscription", "$var(sql)"); + } route[KEEPALIVE_ON_EXPIRED_REGISTRATION] { + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; + if($sel(cfg_get.kazoo.keepalive_expired_registration_action) == 2) { ## disable $var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected < 3 and contact like "$ulc(exp=>addr)%"); @@ -286,16 +317,27 @@ route[KEEPALIVE_ON_EXPIRED_REGISTRATION] $var(Query) = $_s(DELETE FROM keepalive where selected < 3 and contact like "$ulc(exp=>addr)%"); mq_add("keepalive_db_queue", "keepalive:removed_from_expired_registration", "$var(Query)"); } + } route[KEEPALIVE_ON_OPTIONS] { - $var(Query) = $_s(UPDATE keepalive set selected = 3 where received = "$si~$sp~$prid" and selected <> 3 ); - mq_add("keepalive_db_queue", "keepalive:client_options", "$var(Query)"); + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; + + if($shtinc(keepalive=>$si~$sp~$prid) == 1) { + $var(Query) = $_s(UPDATE keepalive set selected = 3 where received = "$si~$sp~$prid" and selected <> 3 ); + mq_add("keepalive_db_queue", "keepalive:client_options", "$var(Query)"); + } + } route[KEEPALIVE_ON_NOTIFY] { - $var(Query) = $_s(UPDATE keepalive set selected = 4 where received = "$si~$sp~$prid" and selected <> 4 ); - mq_add("keepalive_db_queue", "keepalive:client_notify", "$var(Query)"); + if($sel(cfg_get.kazoo.keepalive_enable) == 0) return; + + if($shtinc(keepalive=>$si~$sp~$prid) == 1) { + $var(Query) = $_s(UPDATE keepalive set selected = 4 where received = "$si~$sp~$prid" and selected <> 4 ); + mq_add("keepalive_db_queue", "keepalive:client_notify", "$var(Query)"); + } + } diff --git a/kamailio/nat-traversal-role.cfg b/kamailio/nat-traversal-role.cfg index 9d9451f..6d62690 100644 --- a/kamailio/nat-traversal-role.cfg +++ b/kamailio/nat-traversal-role.cfg @@ -113,6 +113,4 @@ route[NAT_MANAGE] } -#!include_file "keepalive.cfg" - # vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab diff --git a/kamailio/presence-role.cfg b/kamailio/presence-role.cfg index f079f85..ecfe2de 100644 --- a/kamailio/presence-role.cfg +++ b/kamailio/presence-role.cfg @@ -13,6 +13,24 @@ #!trydef KZ_PRESENCE_NO_TARGETS_LOG_LEVEL L_DBG #!trydef KZ_PRESENCE_WITH_TARGETS_LOG_LEVEL L_INFO #!trydef KZ_PRESENCE_REQUIRE_AUTHN 0 +#!trydef KZ_PRESENCE_KEEPALIVE_NAT_ONLY 0 +#!trydef KZ_PRESENCE_KEEPALIVE_UDP_ONLY 0 + + +#!ifdef NAT_TRAVERSAL_ROLE +#!ifndef KEEPALIVE_ROLE +#!ifndef NAT_TRAVERSAL_LOADED +#!trydef NAT_TRAVERSAL_LOADED +loadmodule "nat_traversal.so" +#!endif + +modparam("nat_traversal", "keepalive_method", "OPTIONS") +modparam("nat_traversal", "keepalive_from", "sip:sipcheck@MY_HOSTNAME") +modparam("nat_traversal", "keepalive_state_file", "KAZOO_DATA_DIR/keep_alive_state") +modparam("nat_traversal", "keepalive_interval", 45) +#!endif +#!endif + modparam("htable", "htable", "first=>size=32;autoexpire=3600;initval=0;") @@ -62,6 +80,8 @@ kazoo.presence_max_call_per_presentity = KZ_PRESENCE_MAX_CALL_PER_PRESENTITY des kazoo.presence_no_targets_log_level = KZ_PRESENCE_NO_TARGETS_LOG_LEVEL descr "when a presence event is received and there no targets we can log at another level" kazoo.presence_with_targets_log_level = KZ_PRESENCE_WITH_TARGETS_LOG_LEVEL descr "when a presence event is received and there are targets we can log at another level" kazoo.presence_require_authn = KZ_PRESENCE_REQUIRE_AUTHN descr "require authenticated devices for presence" +kazoo.presence_keepalive_udp_only = KZ_PRESENCE_KEEPALIVE_UDP_ONLY descr "should we send keepalive for udp only" +kazoo.presence_keepalive_nat_only = KZ_PRESENCE_KEEPALIVE_NAT_ONLY descr "should we send keepalive for nat phones only" #!include_file "presence-query.cfg" #!include_file "presence-notify.cfg" @@ -70,6 +90,20 @@ kazoo.presence_require_authn = KZ_PRESENCE_REQUIRE_AUTHN descr "require authenti ####### Presence Logic ######## +route[PRESENCE_NAT] +{ + if(!( ($sel(cfg_get.kazoo.presence_keepalive_udp_only) == 1 && $proto != "udp") + || (!isflagset(FLT_NATS) && $sel(cfg_get.kazoo.presence_keepalive_nat_only) == 1) + || ($proto == "ws" || $proto == "wss") + )) { + #!ifdef KEEPALIVE_ROLE + route(KEEPALIVE_ON_SUBSCRIBE); + #!else + nat_keepalive(); + #!endif + } +} + route[HANDLE_SUBSCRIBE] { if (!is_method("SUBSCRIBE")) { @@ -167,7 +201,7 @@ route[HANDLE_NEW_SUBSCRIBE] route(SUBSCRIBE_AMQP); route(REQUEST_PROBE); #!ifdef NAT_TRAVERSAL_ROLE - route(KEEPALIVE_ON_SUBSCRIBE); + route(PRESENCE_NAT); #!endif } else { xlog("L_INFO", "$ci|stop|error $T_reply_code for new $hdr(Event) subscription from $fU to $tU in realm $fd\n"); diff --git a/kamailio/registrar-role.cfg b/kamailio/registrar-role.cfg index e2c7c43..4876acc 100644 --- a/kamailio/registrar-role.cfg +++ b/kamailio/registrar-role.cfg @@ -1,8 +1,8 @@ #### NAT PINGING PARAMS ### -## NEXT 2 SHOULD GO TOGETHER -## REGISTRAR_NAT_PING_TIMEOUT should be (REGISTRAR_NAT_PING_INTERVAL * 3 + 10) or 0 to disable #!trydef REGISTRAR_NAT_PING_INTERVAL 30 -#!trydef REGISTRAR_NAT_PING_TIMEOUT 100 + +## REGISTRAR_NAT_PING_TIMEOUT should be (REGISTRAR_NAT_PING_INTERVAL * 3 + 10) or 0 to disable +#!trydef REGISTRAR_NAT_PING_TIMEOUT 0 #!trydef REGISTRAR_NAT_PING_NAT_ONLY 1 #!trydef REGISTRAR_NAT_PING_WORKERS 5 @@ -101,15 +101,15 @@ modparam("registrar", "contact_max_size", REGISTRAR_CONTACT_MAX_SIZE) loadmodule "nathelper.so" #!trydef NATHELPER_LOADED #!endif -#!ifdef KEEPALIVE_USE_NATHELPER +#!ifdef KEEPALIVE_ROLE +modparam("nathelper", "natping_interval", 0) +modparam("nathelper", "natping_processes", 0) +#!else modparam("nathelper", "natping_interval", REGISTRAR_NAT_PING_INTERVAL) modparam("nathelper", "ping_nated_only", REGISTRAR_NAT_PING_NAT_ONLY) modparam("nathelper", "natping_processes", REGISTRAR_NAT_PING_WORKERS) modparam("nathelper", "sipping_bflag", FLB_NATSIPPING) modparam("nathelper", "keepalive_timeout", REGISTRAR_NAT_PING_TIMEOUT) -#!else -modparam("nathelper", "natping_interval", 0) -modparam("nathelper", "natping_processes", 0) #!endif #!endif @@ -426,11 +426,9 @@ route[SAVE_LOCATION] route(PUSHER_ON_REGISTRATION); #!endif - #!ifdef NAT_TRAVERSAL_ROLE - #!ifndef KEEPALIVE_USE_NATHELPER + #!ifdef KEEPALIVE_ROLE route(KEEPALIVE_ON_REGISTRATION); #!endif - #!endif exit; } @@ -531,11 +529,9 @@ event_route[usrloc:contact-expired] route(PRESENCE_EXPIRED_REGISTRATION); #!endif - #!ifdef NAT_TRAVERSAL_ROLE - #!ifndef KEEPALIVE_USE_NATHELPER + #!ifdef KEEPALIVE_ROLE route(KEEPALIVE_ON_EXPIRED_REGISTRATION); #!endif - #!endif ## return until we handle this in ecallmr xlog("L_INFO", "$ulc(exp=>callid)|expired|removed registration for $ulc(exp=>aor) with contact : $ulc(exp=>addr)\n");