<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15441] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15441">15441</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-11-11 21:52:07 -0600 (Wed, 11 Nov 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>solve problem from <a href="http://jira.freeswitch.org/browse/MODENDP-258">MODENDP-258</a> with an alternate approach, and make send-message-query-on-register=first-only and sql-in-transactions=true the defaults</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiamod_sofiah">freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_presencec">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_regc">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_slac">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_sla.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -2042,7 +2042,7 @@
</span><span class="cx">                                                                                  profile->name);
</span><span class="cx">                                 }
</span><span class="cx">
</span><del>-                                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, show_reg_callback, &cb);
</del><ins>+                                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, show_reg_callback, &cb);
</ins><span class="cx">                                 switch_safe_free(sql);
</span><span class="cx">
</span><span class="cx">                                 stream->write_function(stream, "%s\n", line);
</span><span class="lines">@@ -2263,7 +2263,7 @@
</span><span class="cx">                                                                                  profile->name);
</span><span class="cx">                                 }
</span><span class="cx">
</span><del>-                                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, show_reg_callback_xml, &cb);
</del><ins>+                                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, show_reg_callback_xml, &cb);
</ins><span class="cx">                                 switch_safe_free(sql);
</span><span class="cx">
</span><span class="cx">                                 stream->write_function(stream, " </registrations>\n");
</span><span class="lines">@@ -2653,7 +2653,7 @@
</span><span class="cx">                         }
</span><span class="cx">
</span><span class="cx">                         switch_assert(sql);
</span><del>-                        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, contact_callback, &cb);
</del><ins>+                        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, contact_callback, &cb);
</ins><span class="cx">                         switch_safe_free(sql);
</span><span class="cx">                         reply = (char *) mystream.data;
</span><span class="cx">                         if (!zstr(reply) && end_of(reply) == ',') {
</span><span class="lines">@@ -3365,7 +3365,7 @@
</span><span class="cx">                                 
</span><span class="cx">
</span><span class="cx">                                 switch_mutex_lock(profile->ireg_mutex);
</span><del>-                                sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, notify_callback, profile);
</del><ins>+                                sofia_glue_execute_sql_callback(profile, NULL, sql, notify_callback, profile);
</ins><span class="cx">                                 switch_mutex_unlock(profile->ireg_mutex);
</span><span class="cx">                                 sofia_glue_release_profile(profile);
</span><span class="cx">
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiamod_sofiah"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -475,7 +475,8 @@
</span><span class="cx">         sofia_gateway_t *gateways;
</span><span class="cx">         su_home_t *home;
</span><span class="cx">         switch_hash_t *chat_hash;
</span><del>-        switch_core_db_t *master_db;
</del><ins>+        switch_hash_t *db_hash;
+        //switch_core_db_t *master_db;
</ins><span class="cx">         switch_thread_rwlock_t *rwlock;
</span><span class="cx">         switch_mutex_t *flag_mutex;
</span><span class="cx">         uint32_t inuse;
</span><span class="lines">@@ -670,6 +671,11 @@
</span><span class="cx"> char *route_uri;
</span><span class="cx"> } sofia_destination_t;
</span><span class="cx">
</span><ins>+typedef struct {
+        switch_core_db_t *db;
+        time_t last_used;
+} sofia_cache_db_handle_t;
+
</ins><span class="cx"> #define sofia_test_pflag(obj, flag) ((obj)->pflags[flag] ? 1 : 0)
</span><span class="cx"> #define sofia_set_pflag(obj, flag) (obj)->pflags[flag] = 1
</span><span class="cx"> #define sofia_set_pflag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
</span><span class="lines">@@ -747,7 +753,7 @@
</span><span class="cx"> void sofia_reg_auth_challenge(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_regtype_t regtype, const char *realm, int stale);
</span><span class="cx"> auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t const *authorization,
</span><span class="cx">                                                                 sip_t const *sip, const char *regstr, char *np, size_t nplen, char *ip, switch_event_t **v_event,
</span><del>-                                                                long exptime, sofia_regtype_t regtype, const char *to_user, switch_event_t **auth_params);
</del><ins>+                                                                long exptime, sofia_regtype_t regtype, const char *to_user, switch_event_t **auth_params, long *reg_count);
</ins><span class="cx">                                                                 
</span><span class="cx">
</span><span class="cx"> void sofia_reg_handle_sip_r_challenge(int status,
</span><span class="lines">@@ -773,7 +779,7 @@
</span><span class="cx">                                          nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]);
</span><span class="cx">
</span><span class="cx"> void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
</span><del>-void sofia_glue_actually_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex);
</del><ins>+void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
</ins><span class="cx"> void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot);
</span><span class="cx"> void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
</span><span class="cx"> void sofia_sub_check_gateway(sofia_profile_t *profile, time_t now);
</span><span class="lines">@@ -795,7 +801,6 @@
</span><span class="cx"> int sofia_glue_init_sql(sofia_profile_t *profile);
</span><span class="cx"> char *sofia_overcome_sip_uri_weakness(switch_core_session_t *session, const char *uri, const sofia_transport_t transport, switch_bool_t uri_only, const char *params);
</span><span class="cx"> switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
</span><del>-                                                                                         switch_bool_t master,
</del><span class="cx">                                                                                          switch_mutex_t *mutex,
</span><span class="cx">                                                                                          char *sql,
</span><span class="cx">                                                                                          switch_core_db_callback_func_t callback,
</span><span class="lines">@@ -925,3 +930,4 @@
</span><span class="cx"> void sofia_info_send_sipfrag(switch_core_session_t *aleg, switch_core_session_t *bleg);
</span><span class="cx"> void sofia_update_callee_id(switch_core_session_t *session, sofia_profile_t *profile, sip_t const *sip, switch_bool_t send);
</span><span class="cx"> void sofia_send_callee_id(switch_core_session_t *session, const char *name, const char *number);
</span><ins>+int sofia_sla_supported(sip_t const *sip);
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -591,7 +591,7 @@
</span><span class="cx">                         sofia_glue_get_addr(nua_current_request(nua), network_ip, sizeof(network_ip), NULL);
</span><span class="cx">                         auth_res = sofia_reg_parse_auth(profile, authorization, sip,
</span><span class="cx">                                                                                         (char *) sip->sip_request->rq_method_name, tech_pvt->key, strlen(tech_pvt->key), network_ip, NULL, 0,
</span><del>-                                                                                        REG_INVITE, NULL, NULL);
</del><ins>+                                                                                        REG_INVITE, NULL, NULL, NULL);
</ins><span class="cx">                 }
</span><span class="cx">                 
</span><span class="cx">                 if (auth_res != AUTH_OK) {
</span><span class="lines">@@ -943,7 +943,7 @@
</span><span class="cx">                                 sprintf(sqlbuf + len, "commit;\n");
</span><span class="cx">
</span><span class="cx">                                 //printf("TRANS:\n%s\n", sqlbuf);
</span><del>-                                sofia_glue_actually_execute_sql(profile, SWITCH_TRUE, sqlbuf, NULL);
</del><ins>+                                sofia_glue_actually_execute_sql(profile, sqlbuf, NULL);
</ins><span class="cx">                                 switch_mutex_unlock(profile->ireg_mutex);
</span><span class="cx">                                 loop_count = 0;
</span><span class="cx">                         }
</span><span class="lines">@@ -951,7 +951,7 @@
</span><span class="cx">                         if (qsize) {
</span><span class="cx">                                 switch_mutex_lock(profile->ireg_mutex);
</span><span class="cx">                                 while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
</span><del>-                                        sofia_glue_actually_execute_sql(profile, SWITCH_TRUE, (char *) pop, NULL);
</del><ins>+                                        sofia_glue_actually_execute_sql(profile, (char *) pop, NULL);
</ins><span class="cx">                                         free(pop);
</span><span class="cx">                                 }
</span><span class="cx">                                 switch_mutex_unlock(profile->ireg_mutex);
</span><span class="lines">@@ -978,7 +978,7 @@
</span><span class="cx">
</span><span class="cx">         switch_mutex_lock(profile->ireg_mutex);
</span><span class="cx">         while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
</span><del>-                sofia_glue_actually_execute_sql(profile, SWITCH_TRUE, (char *) pop, NULL);
</del><ins>+                sofia_glue_actually_execute_sql(profile, (char *) pop, NULL);
</ins><span class="cx">                 free(pop);
</span><span class="cx">         }
</span><span class="cx">         switch_mutex_unlock(profile->ireg_mutex);
</span><span class="lines">@@ -1098,6 +1098,7 @@
</span><span class="cx">                                  NUTAG_APPL_METHOD("NOTIFY"),
</span><span class="cx">                                  NUTAG_APPL_METHOD("INFO"),
</span><span class="cx">                                  NUTAG_APPL_METHOD("ACK"),
</span><ins>+                                 NUTAG_APPL_METHOD("SUBSCRIBE"),
</ins><span class="cx"> #ifdef MANUAL_BYE
</span><span class="cx">                                  NUTAG_APPL_METHOD("BYE"),
</span><span class="cx"> #endif
</span><span class="lines">@@ -1267,6 +1268,7 @@
</span><span class="cx">
</span><span class="cx">         sofia_glue_del_profile(profile);
</span><span class="cx">         switch_core_hash_destroy(&profile->chat_hash);
</span><ins>+        switch_core_hash_destroy(&profile->db_hash);
</ins><span class="cx">
</span><span class="cx">         switch_thread_rwlock_unlock(profile->rwlock);
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name);
</span><span class="lines">@@ -1864,6 +1866,7 @@
</span><span class="cx">                                                 if (switch_true(val)) {
</span><span class="cx">                                                         sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</span><span class="cx">                                                 } else if (!strcasecmp(val, "first-only")) {
</span><ins>+                                                        sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</ins><span class="cx">                                                         sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER);
</span><span class="cx">                                                 } else {
</span><span class="cx">                                                         sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</span><span class="lines">@@ -2387,6 +2390,7 @@
</span><span class="cx">
</span><span class="cx">                                 profile->dbname = switch_core_strdup(profile->pool, url);
</span><span class="cx">                                 switch_core_hash_init(&profile->chat_hash, profile->pool);
</span><ins>+                                switch_core_hash_init(&profile->db_hash, profile->pool);
</ins><span class="cx">                                 switch_thread_rwlock_create(&profile->rwlock, profile->pool);
</span><span class="cx">                                 switch_mutex_init(&profile->flag_mutex, SWITCH_MUTEX_NESTED, profile->pool);
</span><span class="cx">                                 profile->dtmf_duration = 100;
</span><span class="lines">@@ -2397,10 +2401,11 @@
</span><span class="cx">                                 sofia_set_pflag(profile, PFLAG_DISABLE_100REL);
</span><span class="cx">                                 profile->auto_restart = 1;
</span><span class="cx">                                 sofia_set_pflag(profile, PFLAG_AUTOFIX_TIMING);
</span><del>-                                sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</del><span class="cx">                                 sofia_set_pflag(profile, PFLAG_RTP_AUTOFLUSH_DURING_BRIDGE);
</span><span class="cx">                                 profile->contact_user = SOFIA_DEFAULT_CONTACT_USER;
</span><span class="cx">                                 sofia_set_pflag(profile, PFLAG_PASS_CALLEE_ID);
</span><ins>+                                sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER);
+                                sofia_set_pflag(profile, PFLAG_SQL_IN_TRANS);
</ins><span class="cx">
</span><span class="cx">                                 for (param = switch_xml_child(settings, "param"); param; param = param->next) {
</span><span class="cx">                                         char *var = (char *) switch_xml_attr_soft(param, "name");
</span><span class="lines">@@ -2479,6 +2484,7 @@
</span><span class="cx">                                                 if (switch_true(val)) {
</span><span class="cx">                                                         sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</span><span class="cx">                                                 } else if (!strcasecmp(val, "first-only")) {
</span><ins>+                                                        sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</ins><span class="cx">                                                         sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER);
</span><span class="cx">                                                 } else {
</span><span class="cx">                                                         sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER);
</span><span class="lines">@@ -2653,6 +2659,7 @@
</span><span class="cx">                                         } else if (!strcasecmp(var, "manage-shared-appearance")) {
</span><span class="cx">                                                 if (switch_true(val)) {
</span><span class="cx">                                                         sofia_set_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE);
</span><ins>+                                                        profile->pres_type = PRES_TYPE_FULL;
</ins><span class="cx">                                                         profile->sla_contact = switch_core_sprintf(profile->pool, "sla-agent");
</span><span class="cx">                                                 }
</span><span class="cx">                                         } else if (!strcasecmp(var, "disable-srv")) {
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -3609,6 +3609,7 @@
</span><span class="cx"> int sofia_glue_init_sql(sofia_profile_t *profile)
</span><span class="cx"> {
</span><span class="cx">         char *test_sql = NULL;
</span><ins>+        switch_core_db_t *db = NULL;
</ins><span class="cx">
</span><span class="cx">         char reg_sql[] =
</span><span class="cx">                 "CREATE TABLE sip_registrations (\n"
</span><span class="lines">@@ -3780,7 +3781,8 @@
</span><span class="cx">                 
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
</span><span class="cx">                                                                  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
</span><del>-                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'",
</del><ins>+                                                                 "and network_ip like '%%' and network_port like '%%' and sip_username "
+                                                                 "like '%%' and mwi_user like '%%' and mwi_host like '%%'",
</ins><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">
</span><span class="cx">                 if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="lines">@@ -3790,7 +3792,8 @@
</span><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">
</span><del>-                test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and network_ip!='-1' and network_port!='-1'", mod_sofia_globals.hostname);
</del><ins>+                test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and network_ip like '%%' and network_port like '%%'",
+                                                                 mod_sofia_globals.hostname);
</ins><span class="cx">
</span><span class="cx">                 if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                         switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_subscriptions", NULL);
</span><span class="lines">@@ -3813,7 +3816,7 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 free(test_sql);
</span><del>-                test_sql = switch_mprintf("delete from sip_authentication where hostname='%q' and last_nc >= 0", mod_sofia_globals.hostname);
</del><ins>+                test_sql = switch_mprintf("delete from sip_authentication where hostname='%q' or last_nc >= 0", mod_sofia_globals.hostname);
</ins><span class="cx">
</span><span class="cx">                 if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                         switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_authentication", NULL);
</span><span class="lines">@@ -3821,7 +3824,7 @@
</span><span class="cx">                 }
</span><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><del>-                test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where contact_str='' or hostname='%q' and network_ip!='-1'",
</del><ins>+                test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where contact_str='' or hostname='%q' and network_ip like '%%'",
</ins><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">                 if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                         switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_shared_appearance_subscriptions", NULL);
</span><span class="lines">@@ -3830,7 +3833,7 @@
</span><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">
</span><del>-                test_sql = switch_mprintf("delete from sip_shared_appearance_dialogs where contact_str='' or hostname='%q' and network_ip!='-1'",
</del><ins>+                test_sql = switch_mprintf("delete from sip_shared_appearance_dialogs where contact_str='' or hostname='%q' and network_ip like '%%'",
</ins><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">                 if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                         switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_shared_appearance_dialogs", NULL);
</span><span class="lines">@@ -3847,111 +3850,119 @@
</span><span class="cx">         } else if (profile->odbc_dsn) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
</span><span class="cx">         } else {
</span><del>-                if (!(profile->master_db = switch_core_db_open_file(profile->dbname))) {
</del><ins>+                if (!(db = switch_core_db_open_file(profile->dbname))) {
</ins><span class="cx">                         return 0;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
</span><span class="cx">                                                                  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
</span><del>-                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'",
</del><ins>+                                                                 "and network_ip like '%%' and network_port like '%%' and sip_username "
+                                                                 "like '%%' and mwi_user like '%%' and mwi_host like '%%'",
</ins><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">                 
</span><del>-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_registrations", reg_sql);
</del><ins>+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_registrations", reg_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><del>-                test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and network_ip!='-1' and network_port!='-1'", mod_sofia_globals.hostname);
-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_subscriptions", sub_sql);
</del><ins>+                test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and network_ip like '%%' or network_port like '%%'",
+                                                                 mod_sofia_globals.hostname);
+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_subscriptions", sub_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and contact like '%%'", mod_sofia_globals.hostname);
</span><del>-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_dialogs", dialog_sql);
</del><ins>+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_dialogs", dialog_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_presence where hostname='%q'", mod_sofia_globals.hostname);
</span><del>-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_presence", pres_sql);
</del><ins>+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_presence", pres_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><del>-                test_sql = switch_mprintf("delete from sip_authentication where hostname='%q' and last_nc >= 0", mod_sofia_globals.hostname);
-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_authentication", auth_sql);
</del><ins>+                test_sql = switch_mprintf("delete from sip_authentication where hostname='%q' or last_nc >= 0", mod_sofia_globals.hostname);
+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_authentication", auth_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">                 
</span><del>-                test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where contact_str = '' or hostname='%q' and network_ip!='-1'",
</del><ins>+                test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where contact_str = '' or hostname='%q' and network_ip like '%%'",
</ins><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><del>-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_shared_appearance_subscriptions", shared_appearance_sql);
</del><ins>+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_shared_appearance_subscriptions", shared_appearance_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_shared_appearance_dialogs where contact_str = '' or hostname='%q'", mod_sofia_globals.hostname);
</span><del>-                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_shared_appearance_dialogs", shared_appearance_dialogs_sql);
</del><ins>+                switch_core_db_test_reactive(db, test_sql, "DROP TABLE sip_shared_appearance_dialogs", shared_appearance_dialogs_sql);
</ins><span class="cx">                 free(test_sql);
</span><span class="cx">                 
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssa_hostname on sip_shared_appearance_subscriptions (hostname)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssa_hostname on sip_shared_appearance_subscriptions (hostname)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssa_subscriber on sip_shared_appearance_subscriptions (subscriber)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssa_subscriber on sip_shared_appearance_subscriptions (subscriber)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssa_profile_name on sip_shared_appearance_subscriptions (profile_name)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssa_profile_name on sip_shared_appearance_subscriptions (profile_name)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssa_aor on sip_shared_appearance_subscriptions (aor)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssa_aor on sip_shared_appearance_subscriptions (aor)", NULL, NULL, NULL);
</ins><span class="cx">                 
</span><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_profile_name on sip_shared_appearance_dialogs (profile_name)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_profile_name on sip_shared_appearance_dialogs (profile_name)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_hostname on sip_shared_appearance_dialogs (hostname)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_hostname on sip_shared_appearance_dialogs (hostname)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_hostname on sip_shared_appearance_dialogs (network_ip)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_hostname on sip_shared_appearance_dialogs (network_ip)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_contact_str on sip_shared_appearance_dialogs (contact_str)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_contact_str on sip_shared_appearance_dialogs (contact_str)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_call_id on sip_shared_appearance_dialogs (call_id)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_call_id on sip_shared_appearance_dialogs (call_id)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ssd_expires on sip_shared_appearance_dialogs (expires)",
</del><ins>+                switch_core_db_exec(db, "create index if not exists ssd_expires on sip_shared_appearance_dialogs (expires)",
</ins><span class="cx">                                                         NULL, NULL, NULL);
</span><span class="cx">                 
</span><span class="cx">
</span><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists sr_call_id on sip_registrations (call_id)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_user on sip_registrations (sip_user)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_host on sip_registrations (sip_host)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_profile_name on sip_registrations (profile_name)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_presence_hosts on sip_registrations (presence_hosts)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_contact on sip_registrations (contact)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_expires on sip_registrations (expires)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_hostname on sip_registrations (hostname)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_status on sip_registrations (status)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_network_ip on sip_registrations (network_ip)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_network_port on sip_registrations (network_port)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_username on sip_registrations (sip_username)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_realm on sip_registrations (sip_realm)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists sr_call_id on sip_registrations (call_id)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_sip_user on sip_registrations (sip_user)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_sip_host on sip_registrations (sip_host)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_profile_name on sip_registrations (profile_name)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_presence_hosts on sip_registrations (presence_hosts)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_contact on sip_registrations (contact)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_expires on sip_registrations (expires)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_hostname on sip_registrations (hostname)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_status on sip_registrations (status)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_network_ip on sip_registrations (network_ip)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_network_port on sip_registrations (network_port)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_sip_username on sip_registrations (sip_username)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sr_sip_realm on sip_registrations (sip_realm)", NULL, NULL, NULL);
</ins><span class="cx">
</span><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists ss_call_id on sip_subscriptions (call_id)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_hostname on sip_subscriptions (hostname)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_hostname on sip_subscriptions (network_ip)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_sip_user on sip_subscriptions (sip_user)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_sip_host on sip_subscriptions (sip_host)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_presence_hosts on sip_subscriptions (presence_hosts)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_event on sip_subscriptions (event)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_proto on sip_subscriptions (proto)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_sub_to_user on sip_subscriptions (sub_to_user)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists ss_sub_to_host on sip_subscriptions (sub_to_host)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists ss_call_id on sip_subscriptions (call_id)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_hostname on sip_subscriptions (hostname)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_hostname on sip_subscriptions (network_ip)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_sip_user on sip_subscriptions (sip_user)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_sip_host on sip_subscriptions (sip_host)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_presence_hosts on sip_subscriptions (presence_hosts)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_event on sip_subscriptions (event)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_proto on sip_subscriptions (proto)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_sub_to_user on sip_subscriptions (sub_to_user)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists ss_sub_to_host on sip_subscriptions (sub_to_host)", NULL, NULL, NULL);
</ins><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists sd_uuid on sip_dialogs (uuid)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sd_hostname on sip_dialogs (hostname)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sd_hostname on sip_dialogs (contact)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sd_hostname on sip_dialogs (presence_id)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sd_hostname on sip_dialogs (presence_data)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists sd_uuid on sip_dialogs (uuid)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sd_hostname on sip_dialogs (hostname)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sd_hostname on sip_dialogs (contact)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sd_hostname on sip_dialogs (presence_id)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sd_hostname on sip_dialogs (presence_data)", NULL, NULL, NULL);
</ins><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists sp_hostname on sip_presence (hostname)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists sp_hostname on sip_presence (hostname)", NULL, NULL, NULL);
</ins><span class="cx">
</span><del>-                switch_core_db_exec(profile->master_db, "create index if not exists sa_nonce on sip_authentication (nonce)", NULL, NULL, NULL);
-                switch_core_db_exec(profile->master_db, "create index if not exists sa_hostname on sip_authentication (hostname)", NULL, NULL, NULL);
</del><ins>+                switch_core_db_exec(db, "create index if not exists sa_nonce on sip_authentication (nonce)", NULL, NULL, NULL);
+                switch_core_db_exec(db, "create index if not exists sa_hostname on sip_authentication (hostname)", NULL, NULL, NULL);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         if (switch_odbc_available() && profile->odbc_dsn) {
</span><span class="cx">                 return profile->master_odbc ? 1 : 0;
</span><span class="cx">         }
</span><ins>+        
+        if (db) {
+                switch_core_db_close(db);
+                return 1;
+        } else {
+                return 0;
+        }
</ins><span class="cx">
</span><del>-        return profile->master_db ? 1 : 0;
</del><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void sofia_glue_sql_close(sofia_profile_t *profile)
</span><span class="lines">@@ -3959,8 +3970,25 @@
</span><span class="cx">         if (switch_odbc_available() && profile->master_odbc) {
</span><span class="cx">                 switch_odbc_handle_destroy(&profile->master_odbc);
</span><span class="cx">         } else {
</span><del>-                switch_core_db_close(profile->master_db);
-                profile->master_db = NULL;
</del><ins>+                switch_hash_index_t *hi;
+                const void *var;
+                void *val;
+                sofia_cache_db_handle_t *dbh = NULL;
+
+                switch_mutex_lock(profile->ireg_mutex);
+        top:
+ for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
+                        switch_hash_this(hi, &var, NULL, &val);
+ if ((dbh = (sofia_cache_db_handle_t *) val)) {
+                                switch_core_db_close(dbh->db);
+                                dbh->db = NULL;
+                                switch_core_hash_delete(profile->db_hash, (char *)var);
+
+                                goto top;
+                        }
+                }
+                switch_mutex_unlock(profile->ireg_mutex);
+
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -3988,7 +4016,7 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         if (status != SWITCH_STATUS_SUCCESS) {
</span><del>-                sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
</del><ins>+                sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_safe_free(d_sql);
</span><span class="lines">@@ -3998,8 +4026,34 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><del>-void sofia_glue_actually_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex)
</del><ins>+switch_core_db_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
</ins><span class="cx"> {
</span><ins>+        switch_thread_id_t self = switch_thread_self();
+        char thread_str[256] = "";
+        switch_core_db_t *db = NULL;
+        sofia_cache_db_handle_t *dbh = NULL;
+
+        snprintf(thread_str, sizeof(thread_str) - 1, "%lu", (unsigned long)(intptr_t)self);
+
+        switch_mutex_lock(profile->ireg_mutex);
+        if ((dbh = switch_core_hash_find(profile->db_hash, thread_str))) {
+                db = dbh->db;
+        } else {
+                dbh = switch_core_alloc(profile->pool, sizeof(*dbh));
+                dbh->db = switch_core_db_open_file(profile->dbname);
+                dbh->last_used = switch_epoch_time_now(NULL);
+                switch_core_hash_insert(profile->db_hash, thread_str, dbh);
+                db = dbh->db;
+        }
+        switch_mutex_unlock(profile->ireg_mutex);
+
+        return db;
+}
+
+
+
+void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
+{
</ins><span class="cx">         switch_core_db_t *db;
</span><span class="cx">
</span><span class="cx">         if (mutex) {
</span><span class="lines">@@ -4022,24 +4076,18 @@
</span><span class="cx">         } else {
</span><span class="cx">                 char *errmsg;
</span><span class="cx">
</span><del>-                if (master) {
-                        db = profile->master_db;
-                } else {
-                        if (!(db = switch_core_db_open_file(profile->dbname))) {
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                                goto end;
-                        }
</del><ins>+                
+                if (!(db = sofia_glue_get_db_handle(profile))) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
+                        goto end;
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
</span><ins>+
</ins><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
</span><span class="cx">                         switch_core_db_free(errmsg);
</span><span class="cx">                 }
</span><del>-
-                if (!master) {
-                        switch_core_db_close(db);
-                }
</del><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> end:
</span><span class="lines">@@ -4049,7 +4097,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
</span><del>-                                                                                         switch_bool_t master, switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
</del><ins>+                                                                                         switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
</ins><span class="cx"> {
</span><span class="cx">         switch_bool_t ret = SWITCH_FALSE;
</span><span class="cx">         switch_core_db_t *db;
</span><span class="lines">@@ -4066,25 +4114,18 @@
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
</span><span class="cx">         } else {
</span><span class="cx">
</span><del>-                if (master) {
-                        db = profile->master_db;
-                } else {
-                        if (!(db = switch_core_db_open_file(profile->dbname))) {
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                                goto end;
-                        }
</del><ins>+                if (!(db = sofia_glue_get_db_handle(profile))) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
+                        goto end;
</ins><span class="cx">                 }
</span><span class="cx">
</span><ins>+
</ins><span class="cx">                 switch_core_db_exec(db, sql, callback, pdata, &errmsg);
</span><span class="cx">
</span><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
</span><span class="cx">                         free(errmsg);
</span><span class="cx">                 }
</span><del>-
-                if (!master && db) {
-                        switch_core_db_close(db);
-                }
</del><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> end:
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_presencec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -190,7 +190,7 @@
</span><span class="cx">                         }
</span><span class="cx">                         helper.profile = profile;
</span><span class="cx">                         helper.event = NULL;
</span><del>-                        if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper) != SWITCH_TRUE) {
</del><ins>+                        if (sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper) != SWITCH_TRUE) {
</ins><span class="cx">                                 continue;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -202,13 +202,13 @@
</span><span class="cx"> void sofia_presence_establish_presence(sofia_profile_t *profile)
</span><span class="cx"> {
</span><span class="cx">
</span><del>-        if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex,
</del><ins>+        if (sofia_glue_execute_sql_callback(profile, profile->ireg_mutex,
</ins><span class="cx">                                                                                 "select sip_user,sip_host,'Registered','unknown','' from sip_registrations",
</span><span class="cx">                                                                                 sofia_presence_resub_callback, profile) != SWITCH_TRUE) {
</span><span class="cx">                 return;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex,
</del><ins>+        if (sofia_glue_execute_sql_callback(profile, profile->ireg_mutex,
</ins><span class="cx">                                                                                 "select sub_to_user,sub_to_host,'Online','unknown',proto from sip_subscriptions "
</span><span class="cx">                                                                                 "where proto='ext' or proto='user' or proto='conf'", sofia_presence_resub_callback, profile) != SWITCH_TRUE) {
</span><span class="cx">                 return;
</span><span class="lines">@@ -337,7 +337,7 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">         if (sql) {
</span><del>-                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_mwi_callback, &h);
</del><ins>+                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_mwi_callback, &h);
</ins><span class="cx">                 free(sql);
</span><span class="cx">                 sql = NULL;
</span><span class="cx">                 
</span><span class="lines">@@ -355,7 +355,7 @@
</span><span class="cx">
</span><span class="cx">         if (sql) {
</span><span class="cx">                 switch_assert(sql != NULL);
</span><del>-                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_mwi_callback2, &h);
</del><ins>+                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_mwi_callback2, &h);
</ins><span class="cx">                 free(sql);
</span><span class="cx">                 sql = NULL;
</span><span class="cx">         }
</span><span class="lines">@@ -456,7 +456,7 @@
</span><span class="cx">                         }
</span><span class="cx">                         helper.profile = profile;
</span><span class="cx">                         helper.event = NULL;
</span><del>-                        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper);
</del><ins>+                        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper);
</ins><span class="cx">                 }
</span><span class="cx">                 switch_mutex_unlock(mod_sofia_globals.hash_mutex);
</span><span class="cx">                 free(sql);
</span><span class="lines">@@ -534,7 +534,7 @@
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s DUMP PRESENCE_PROBE_SQL:\n%s\n", profile->name, sql);
</span><span class="cx">                                 }
</span><span class="cx">
</span><del>-                                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_resub_callback, profile);
</del><ins>+                                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_resub_callback, profile);
</ins><span class="cx">                                 if (mod_sofia_globals.debug_presence > 0) {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_PROBE_SQL\n\n", profile->name);
</span><span class="cx">                                 }
</span><span class="lines">@@ -573,7 +573,6 @@
</span><span class="cx">                         }
</span><span class="cx">                         continue;
</span><span class="cx">                 }
</span><del>-                
</del><span class="cx">
</span><span class="cx">                 if ((sql = switch_mprintf(
</span><span class="cx">                                                         
</span><span class="lines">@@ -615,7 +614,7 @@
</span><span class="cx">                                 free(buf);
</span><span class="cx">                         }
</span><span class="cx">
</span><del>-                        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE,
</del><ins>+                        sofia_glue_execute_sql_callback(profile,
</ins><span class="cx">                                                                                         NULL, sql, sofia_presence_sub_callback, &helper);
</span><span class="cx">
</span><span class="cx">                         if (mod_sofia_globals.debug_presence > 0) {
</span><span class="lines">@@ -1764,7 +1763,7 @@
</span><span class="cx">                                                                  "full_via,expires,user_agent,accept,profile_name,network_ip"
</span><span class="cx">                                                                  " from sip_subscriptions where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%')",
</span><span class="cx">                                                                  to_host, to_user, to_host, to_host))) {
</span><del>-                        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_reg_callback, profile);
</del><ins>+                        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_reg_callback, profile);
</ins><span class="cx">                         
</span><span class="cx">                         switch_safe_free(sql);
</span><span class="cx">                 }
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_regc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -521,7 +521,7 @@
</span><span class="cx">         switch_safe_free(sqlextra);
</span><span class="cx">         
</span><span class="cx">         switch_mutex_lock(profile->ireg_mutex);
</span><del>-        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_del_callback, profile);
</del><ins>+        sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_del_callback, profile);
</ins><span class="cx">         switch_mutex_unlock(profile->ireg_mutex);
</span><span class="cx">         switch_safe_free(sql);
</span><span class="cx">         
</span><span class="lines">@@ -543,11 +543,6 @@
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
</span><span class="cx">                         return;
</span><span class="cx">                 }
</span><del>-        } else {
-                if (!profile->master_db) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                        return;
-                }
</del><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_mutex_lock(profile->ireg_mutex);
</span><span class="lines">@@ -562,7 +557,7 @@
</span><span class="cx">                                                 ",%d from sip_registrations where expires > 0", reboot);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_del_callback, profile);
</del><ins>+        sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_del_callback, profile);
</ins><span class="cx">         if (now) {
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and expires <= %ld and hostname='%s'",
</span><span class="cx">                                                 (long) now, mod_sofia_globals.hostname);
</span><span class="lines">@@ -570,7 +565,7 @@
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
</del><ins>+        sofia_glue_actually_execute_sql(profile, sql, NULL);
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -579,12 +574,12 @@
</span><span class="cx">                                                 "and profile_name='%s' and expires <= %ld",
</span><span class="cx">                                                 mod_sofia_globals.hostname, profile->name, (long) now);
</span><span class="cx">         
</span><del>-                sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_sla_dialog_del_callback, profile);
</del><ins>+                sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_sla_dialog_del_callback, profile);
</ins><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and hostname='%s' and expires <= %ld",
</span><span class="cx">                                                 mod_sofia_globals.hostname, (long) now);
</span><span class="cx">
</span><span class="cx">
</span><del>-                sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
</del><ins>+                sofia_glue_actually_execute_sql(profile, sql, NULL);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -595,7 +590,7 @@
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_presence where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
</del><ins>+        sofia_glue_actually_execute_sql(profile, sql, NULL);
</ins><span class="cx">
</span><span class="cx">         if (now) {
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld and hostname='%s'",
</span><span class="lines">@@ -604,7 +599,7 @@
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
</del><ins>+        sofia_glue_actually_execute_sql(profile, sql, NULL);
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -615,7 +610,7 @@
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "select call_id from sip_subscriptions where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_sub_del_callback, profile);
</del><ins>+        sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_sub_del_callback, profile);
</ins><span class="cx">
</span><span class="cx">         if (now) {
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and expires <= %ld and hostname='%s'",
</span><span class="lines">@@ -624,7 +619,7 @@
</span><span class="cx">                 switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
</del><ins>+        sofia_glue_actually_execute_sql(profile, sql, NULL);
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">         if (now && sofia_test_pflag(profile, PFLAG_NAT_OPTIONS_PING)) {
</span><span class="lines">@@ -633,7 +628,7 @@
</span><span class="cx">                                                 " from sip_registrations where (status like '%%AUTO-NAT%%' "
</span><span class="cx">                                                 "or status like '%%UDP-NAT%%') and hostname='%s'", mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_nat_callback, profile);
</del><ins>+                sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_nat_callback, profile);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_mutex_unlock(profile->ireg_mutex);
</span><span class="lines">@@ -661,7 +656,7 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">
</span><del>-        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_reg_find_callback, &cbt);
</del><ins>+        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_reg_find_callback, &cbt);
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">         if (cbt.matches) {
</span><span class="lines">@@ -686,9 +681,10 @@
</span><span class="cx">                                                  switch_epoch_time_now(NULL) + (profile->nonce_ttl ? profile->nonce_ttl : DEFAULT_NONCE_TTL),
</span><span class="cx">                                                  profile->name, mod_sofia_globals.hostname);
</span><span class="cx">         switch_assert(sql != NULL);
</span><del>-        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
</del><ins>+        sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
</ins><span class="cx">         switch_safe_free(sql);
</span><span class="cx">
</span><ins>+        //auth_str = switch_mprintf("Digest realm=\"%q\", nonce=\"%q\",%s algorithm=MD5, qop=\"auth\"", realm, uuid_str, stale ? " stale=true," : "");
</ins><span class="cx">         auth_str = switch_mprintf("Digest realm=\"%q\", nonce=\"%q\",%s algorithm=MD5, qop=\"auth\"", realm, uuid_str, stale ? " stale=\"true\"," : "");
</span><span class="cx">
</span><span class="cx">         if (regtype == REG_REGISTER) {
</span><span class="lines">@@ -741,6 +737,7 @@
</span><span class="cx">         char *path_val = NULL;
</span><span class="cx">         switch_event_t *auth_params = NULL;
</span><span class="cx">         int r = 0;
</span><ins>+        long reg_count = 0;
</ins><span class="cx">
</span><span class="cx">         /* all callers must confirm that sip, sip->sip_request and sip->sip_contact are not NULL */
</span><span class="cx">         switch_assert(sip != NULL && sip->sip_contact != NULL && sip->sip_request != NULL);
</span><span class="lines">@@ -896,7 +893,7 @@
</span><span class="cx">         if (authorization) {
</span><span class="cx">                 char *v_contact_str;
</span><span class="cx">                 if ((auth_res = sofia_reg_parse_auth(profile, authorization, sip, sip->sip_request->rq_method_name,
</span><del>-                                                                                         key, keylen, network_ip, v_event, exptime, regtype, to_user, &auth_params)) == AUTH_STALE) {
</del><ins>+                                                                                         key, keylen, network_ip, v_event, exptime, regtype, to_user, &auth_params, &reg_count)) == AUTH_STALE) {
</ins><span class="cx">                         stale = 1;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -1131,16 +1128,21 @@
</span><span class="cx">                         switch_event_fire(&event);
</span><span class="cx">                 }
</span><span class="cx"> #else
</span><del>-                if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) {
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", "sip");
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
-                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, reg_host);
-                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, reg_host);
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", "Registered");
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_subtype", "probe");
-                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
-                        switch_event_fire(&event);
</del><ins>+
+                if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER) ||
+                        (reg_count == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) {
+
+                        if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) {
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", "sip");
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
+                                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, reg_host);
+                                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, reg_host);
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", "Registered");
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_subtype", "probe");
+                                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
+                                switch_event_fire(&event);
+                        }
</ins><span class="cx">                 }
</span><span class="cx"> #endif
</span><span class="cx">
</span><span class="lines">@@ -1213,28 +1215,15 @@
</span><span class="cx">         if (regtype == REG_REGISTER) {
</span><span class="cx">                 char exp_param[128] = "";
</span><span class="cx">                 char date[80] = "";
</span><del>-                long nc_long = 0;                
-                
</del><ins>+
</ins><span class="cx">                 s_event = NULL;
</span><span class="cx">
</span><span class="cx">                 if (exptime) {
</span><span class="cx">                         switch_snprintf(exp_param, sizeof(exp_param), "expires=%ld", exptime);
</span><span class="cx">                         sip_contact_add_param(nua_handle_home(nh), sip->sip_contact, exp_param);
</span><span class="cx">                         
</span><del>-                        if (auth_params && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER)) {
-                                const char *nc = NULL;
-
-                                if ((nc = switch_event_get_header(auth_params, "sip_auth_nc"))) {
-                                        nc_long = strtoul(nc, 0, 16);
-                                } else {
-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                         "No nonce count, cannot determine if this was the first register, sending notify implicitly.\n");
-                                        nc_long = 1;
-                                }
-                        }
-                        
</del><span class="cx">                         if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER) ||
</span><del>-                                (nc_long == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) {
</del><ins>+                                (reg_count == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) {
</ins><span class="cx">                                 if (switch_event_create(&s_event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                                         switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", mwi_user, mwi_host);
</span><span class="cx">                                         switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Sofia-Profile", profile->name);
</span><span class="lines">@@ -1263,13 +1252,9 @@
</span><span class="cx">                 if (s_event) {
</span><span class="cx">                         switch_event_fire(&s_event);
</span><span class="cx">                 }
</span><del>-
-                if (sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE)) {
-                        char *full_contact = sip_header_as_string(nua_handle_home(nh), (void *) sip->sip_contact);
-                        if (full_contact && switch_stristr("SUBSCRIBE", full_contact)) {
-                                sofia_sla_handle_register(nua, profile, sip, exptime, full_contact);
-                                su_free(nua_handle_home(nh), full_contact);
-                        }
</del><ins>+                
+                if (contact_str && sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE) && sofia_sla_supported(sip)) {
+                        sofia_sla_handle_register(nua, profile, sip, exptime, contact_str);
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 switch_goto_int(r, 1, end);
</span><span class="lines">@@ -1594,6 +1579,20 @@
</span><span class="cx">
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+typedef struct {
+        char *nonce;
+        switch_size_t nplen;
+        int last_nc;
+} nonce_cb_t;
+
+static int sofia_reg_nonce_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        nonce_cb_t *cb = (nonce_cb_t *) pArg;
+        switch_copy_string(cb->nonce, argv[0], cb->nplen);
+        cb->last_nc = zstr(argv[1]) ? 0 : atoi(argv[1]);
+        return 0;
+}
+
</ins><span class="cx"> auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
</span><span class="cx">                                                                 sip_authorization_t const *authorization,
</span><span class="cx">                                                                 sip_t const *sip,
</span><span class="lines">@@ -1606,7 +1605,8 @@
</span><span class="cx">                                                                 sofia_regtype_t
</span><span class="cx">                                                                 regtype,
</span><span class="cx">                                                                 const char *to_user,
</span><del>-                                                                switch_event_t **auth_params)
</del><ins>+                                                                switch_event_t **auth_params,
+                                                                long *reg_count)
</ins><span class="cx"> {
</span><span class="cx">         int indexnum;
</span><span class="cx">         const char *cur;
</span><span class="lines">@@ -1626,6 +1626,7 @@
</span><span class="cx">         char *domain_name = NULL;
</span><span class="cx">         switch_event_t *params = NULL;
</span><span class="cx">         const char *auth_acl = NULL;
</span><ins>+        long ncl = 0;
</ins><span class="cx">
</span><span class="cx">         username = realm = nonce = uri = qop = cnonce = nc = response = NULL;
</span><span class="cx">
</span><span class="lines">@@ -1684,20 +1685,35 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         if (zstr(np)) {
</span><ins>+                nonce_cb_t cb = { 0 };
+                long nc_long = 0;
</ins><span class="cx">                 first = 1;
</span><ins>+                
</ins><span class="cx">                 if (nc) {
</span><del>-                        sql = switch_mprintf("select nonce from sip_authentication where nonce='%q' and last_nc < %lu", nonce, strtoul(nc, 0, 16));
</del><ins>+                        nc_long = strtoul(nc, 0, 16);
+                        sql = switch_mprintf("select nonce,last_nc from sip_authentication where nonce='%q' and last_nc < %lu", nonce, nc_long);
</ins><span class="cx">                 } else {
</span><span class="cx">                         sql = switch_mprintf("select nonce from sip_authentication where nonce='%q'", nonce);
</span><span class="cx">                 }
</span><span class="cx">
</span><ins>+                cb.nonce = np;
+                cb.nplen = nplen;
+                
</ins><span class="cx">                 switch_assert(sql != NULL);
</span><del>-                if (!sofia_glue_execute_sql2str(profile, profile->ireg_mutex, sql, np, nplen)) {
-                        free(sql);
</del><ins>+                sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_nonce_callback, &cb);
+                free(sql);
+
+                //if (!sofia_glue_execute_sql2str(profile, profile->ireg_mutex, sql, np, nplen)) {
+                if (zstr(np)) {
+                        sql = switch_mprintf("delete from sip_authentication where nonce='%q'", nonce);
+                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
</ins><span class="cx">                         ret = AUTH_STALE;
</span><span class="cx">                         goto end;
</span><span class="cx">                 }
</span><del>-                free(sql);
</del><ins>+
+                if (reg_count) {
+                        *reg_count = cb.last_nc + 1;
+                }
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_event_create(&params, SWITCH_EVENT_REQUEST_PARAMS);
</span><span class="lines">@@ -2082,6 +2098,7 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">         if (nc && cnonce && qop) {
</span><ins>+                ncl = strtoul(nc, 0, 16);
</ins><span class="cx">
</span><span class="cx"> #if defined(_WIN32) && !defined(_WIN64)
</span><span class="cx"> #define        LL_FMT "ll"
</span><span class="lines">@@ -2089,10 +2106,10 @@
</span><span class="cx"> #define        LL_FMT "l"
</span><span class="cx"> #endif
</span><span class="cx">                 sql = switch_mprintf("update sip_authentication set expires='%"LL_FMT"u',last_nc=%lu where nonce='%s'",
</span><del>-                                                         switch_epoch_time_now(NULL) + (profile->nonce_ttl ? profile->nonce_ttl : exptime + 10), strtoul(nc, 0, 16), nonce);
</del><ins>+                                                         switch_epoch_time_now(NULL) + (profile->nonce_ttl ? profile->nonce_ttl : exptime + 10), ncl, nonce);
</ins><span class="cx">
</span><span class="cx">                 switch_assert(sql != NULL);
</span><del>-                sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
</del><ins>+                sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -2112,6 +2129,18 @@
</span><span class="cx">         switch_safe_free(nc);
</span><span class="cx">         switch_safe_free(response);
</span><span class="cx">
</span><ins>+        if (reg_count && !*reg_count) {
+                if (ret == AUTH_OK) {
+                        if (ncl) {
+                                *reg_count = ncl;
+                        } else {
+                                *reg_count = 1;
+                        }
+                } else {
+                        *reg_count = 0;
+                }
+        }
+
</ins><span class="cx">         return ret;
</span><span class="cx">
</span><span class="cx"> }
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_slac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_sla.c (15440 => 15441)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_sla.c        2009-11-12 03:48:07 UTC (rev 15440)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_sla.c        2009-11-12 03:52:07 UTC (rev 15441)
</span><span class="lines">@@ -49,6 +49,25 @@
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+int sofia_sla_supported(sip_t const *sip)
+{
+        if (sip && sip->sip_user_agent && sip->sip_user_agent->g_string) {
+                const char *ua = sip->sip_user_agent->g_string;
+
+                if (switch_stristr("polycom", ua)) {
+                        return 1;
+                }
+
+                if (switch_stristr("snom", ua)) {
+                        return 1;
+                }
+
+        }
+
+        return 0;
+}
+
+
</ins><span class="cx"> void sofia_sla_handle_register(nua_t *nua, sofia_profile_t *profile, sip_t const *sip, long exptime, const char *full_contact)
</span><span class="cx"> {
</span><span class="cx">         nua_handle_t *nh = NULL;
</span><span class="lines">@@ -60,12 +79,15 @@
</span><span class="cx">         sofia_transport_t transport = sofia_glue_url2transport(sip->sip_contact->m_url);
</span><span class="cx">         char network_ip[80];
</span><span class="cx">         int network_port = 0;
</span><ins>+        sofia_destination_t *dst;
+        char *route_uri = NULL;
+        char port_str[25] = "";
</ins><span class="cx">
</span><span class="cx">         sofia_glue_get_addr(nua_current_request(nua), network_ip, sizeof(network_ip), &network_port);
</span><span class="cx">
</span><span class="cx">         sql = switch_mprintf("select call_id from sip_shared_appearance_dialogs where hostname='%q' and profile_name='%q' and contact_str='%q'",
</span><span class="cx">                                                  mod_sofia_globals.hostname, profile->name, contact_str);
</span><del>-        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, get_call_id_callback, &sh);
</del><ins>+        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, get_call_id_callback, &sh);
</ins><span class="cx">
</span><span class="cx">         free(sql);
</span><span class="cx">         
</span><span class="lines">@@ -85,21 +107,35 @@
</span><span class="cx">         nua_handle_bind(nh, &mod_sofia_globals.keep_private);
</span><span class="cx">
</span><span class="cx">         switch_snprintf(exp_str, sizeof(exp_str), "%ld", exptime + 30);
</span><ins>+
+        switch_snprintf(port_str, sizeof(port_str), ":%ld", sofia_glue_transport_has_tls(transport) ? profile->tls_sip_port : profile->sip_port);
+
</ins><span class="cx">         if (sofia_glue_check_nat(profile, network_ip)) {
</span><del>-                switch_snprintf(my_contact, sizeof(my_contact), "<sip:%s@%s;transport=%s>;expires=%s", profile->sla_contact,
-                                                profile->extsipip, sofia_glue_transport2str(transport), exp_str);
</del><ins>+                switch_snprintf(my_contact, sizeof(my_contact), "<sip:%s@%s%s;transport=%s>;expires=%s", profile->sla_contact,
+                                                profile->extsipip, port_str, sofia_glue_transport2str(transport), exp_str);
</ins><span class="cx">         } else {
</span><del>-                switch_snprintf(my_contact, sizeof(my_contact), "<sip:%s@%s;transport=%s>;expires=%s", profile->sla_contact,
-                                                profile->sipip, sofia_glue_transport2str(transport), exp_str);
</del><ins>+                switch_snprintf(my_contact, sizeof(my_contact), "<sip:%s@%s%s;transport=%s>;expires=%s", profile->sla_contact,
+                                                profile->sipip, port_str, sofia_glue_transport2str(transport), exp_str);
</ins><span class="cx">         }
</span><ins>+
+        dst = sofia_glue_get_destination((char*) full_contact);
+
+        if(dst->route_uri) {
+                route_uri = sofia_glue_strip_uri(dst->route_uri);
+        }
+
</ins><span class="cx">         nua_subscribe(nh,
</span><ins>+                                 TAG_IF(dst->route_uri, NUTAG_PROXY(route_uri)), TAG_IF(dst->route, SIPTAG_ROUTE_STR(dst->route)),
</ins><span class="cx">                                  SIPTAG_TO(sip->sip_to),
</span><span class="cx">                                  SIPTAG_FROM(sip->sip_to),
</span><span class="cx">                                  SIPTAG_CONTACT_STR(my_contact),
</span><span class="cx">                                  SIPTAG_EXPIRES_STR(exp_str),
</span><del>-                                 SIPTAG_EVENT_STR("dialog;sla"),        /* some phones want ;include-session-description too? */
</del><ins>+                                 SIPTAG_EVENT_STR("dialog;sla;include-session-description"),
+                                 SIPTAG_ACCEPT_STR("application/dialog-info+xml"),
</ins><span class="cx">                                  TAG_NULL());
</span><span class="cx">
</span><ins>+        sofia_glue_free_destination(dst);
+
</ins><span class="cx">         free(contact_str);
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -119,6 +155,7 @@
</span><span class="cx">         char network_ip[80];
</span><span class="cx">         int network_port = 0;
</span><span class="cx">         sofia_transport_t transport = sofia_glue_url2transport(sip->sip_contact->m_url);
</span><ins>+        char *pl;
</ins><span class="cx">
</span><span class="cx">         sofia_glue_get_addr(nua_current_request(nua), network_ip, sizeof(network_ip), &network_port);
</span><span class="cx">         /*
</span><span class="lines">@@ -194,6 +231,21 @@
</span><span class="cx">                                 /* sofia_presence says something about needing TAG_IF(sticky, NUTAG_PROXY(sticky)) for NAT stuff? */
</span><span class="cx">                                 TAG_END());
</span><span class="cx">
</span><ins>+
+        
+        pl = switch_mprintf("<?xml version=\"1.0\"?>\n"
+                                                "<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" "
+                                                "version=\"0\" state=\"full\" entity=\"%s\"></dialog-info>\n", aor);
+        
+        nua_notify(nh,
+                         SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=300"), /* XXX MTK FIXME - this is totally fake calculation */
+                         TAG_IF(route_uri, NUTAG_PROXY(route_uri)),
+                         SIPTAG_CONTENT_TYPE_STR("application/dialog-info+xml"),        /* could've just kept the type from the payload */
+                         SIPTAG_PAYLOAD_STR(pl),
+                         TAG_END());
+        
+        
+
</ins><span class="cx">         switch_safe_free(aor);
</span><span class="cx">         switch_safe_free(subscriber);
</span><span class="cx">         switch_safe_free(route_uri);
</span><span class="lines">@@ -295,7 +347,7 @@
</span><span class="cx">                 helper.payload = sip->sip_payload->pl_data;         /* could just send the WHOLE payload. you'd get the type that way. */
</span><span class="cx">
</span><span class="cx">                 /* which mutex if any is correct to hold in this callback? XXX MTK FIXME */
</span><del>-                sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_sla_sub_callback, &helper);
</del><ins>+                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_sla_sub_callback, &helper);
</ins><span class="cx">
</span><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">                 switch_safe_free(aor);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>