diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 9d2354806c..c75d4d2613 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -265,8 +265,7 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session) if (session && (tech_pvt->profile->pflags & PFLAG_PRESENCE)) { char *sql = switch_mprintf("delete from sip_dialogs where call_id='%q'", tech_pvt->call_id); switch_assert(sql); - sofia_glue_execute_sql(tech_pvt->profile, SWITCH_FALSE, sql, tech_pvt->profile->ireg_mutex); - free(sql); + sofia_glue_execute_sql(tech_pvt->profile, &sql, SWITCH_TRUE); } if (tech_pvt->kick && (a_session = switch_core_session_locate(tech_pvt->kick))) { diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index fd8ffa4da7..ce688e170f 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -88,12 +88,6 @@ typedef struct private_object private_object_t; #include #include -typedef struct { - switch_bool_t master; - char *sql; -} sofia_sql_job_t; - - typedef enum { DTMF_2833, DTMF_INFO, @@ -509,7 +503,7 @@ void sofia_presence_handle_sip_i_subscribe(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]); -void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex); +void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t dyn); void sofia_glue_actually_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex); void sofia_reg_check_expire(sofia_profile_t *profile, time_t now); void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now); diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 4f701cd959..a496cb0404 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -73,8 +73,7 @@ void sofia_handle_sip_r_notify(switch_core_session_t *session, int status, sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id); switch_assert(sql != NULL); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); nua_handle_destroy(nh); } } @@ -169,8 +168,7 @@ void sofia_handle_sip_r_message(int status, sofia_profile_t *profile, nua_handle sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", user, host); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Deleting registration for %s@%s\n", user, host); - sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL); - switch_safe_free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } } @@ -398,16 +396,13 @@ void event_handler(switch_event_t *event) } switch_mutex_lock(profile->ireg_mutex); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - switch_safe_free(sql); - + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); + sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','Registered', '%q', %ld, '%q')", call_id, from_user, from_host, contact_str, rpid, expires, user_agent); if (sql) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - switch_safe_free(sql); - sql = NULL; + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Propagating registration for %s@%s->%s\n", from_user, from_host, contact_str); } switch_mutex_unlock(profile->ireg_mutex); @@ -424,9 +419,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread sofia_profile_t *profile = (sofia_profile_t *) obj; uint32_t ireg_loops = 0; uint32_t gateway_loops = 0; - void *pop; - sofia_sql_job_t *job; int loops = 0; + uint32_t qsize; ireg_loops = IREG_SECONDS; gateway_loops = GATEWAY_SECONDS; @@ -435,15 +429,19 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread switch_queue_create(&profile->sql_queue, 500000, profile->pool); - while ((mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) || switch_queue_size(profile->sql_queue)) { - while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) { - job = (sofia_sql_job_t *) pop; - sofia_glue_actually_execute_sql(profile, job->master, job->sql, NULL); - free(job->sql); - free(job); - job = NULL; + qsize = switch_queue_size(profile->sql_queue); + + while ((mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) || qsize) { + if (qsize) { + void *pop; + switch_mutex_lock(profile->ireg_mutex); + while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) { + sofia_glue_actually_execute_sql(profile, SWITCH_TRUE, (char *) pop, NULL); + free(pop); + } + switch_mutex_unlock(profile->ireg_mutex); } - + if (++loops >= 100) { if (++ireg_loops >= IREG_SECONDS) { sofia_reg_check_expire(profile, switch_timestamp(NULL)); @@ -458,6 +456,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread } switch_yield(10000); + qsize = switch_queue_size(profile->sql_queue); } sofia_clear_pflag_locked(profile, PFLAG_WORKER_RUNNING); @@ -470,13 +469,20 @@ void launch_sofia_worker_thread(sofia_profile_t *profile) { switch_thread_t *thread; switch_threadattr_t *thd_attr = NULL; + int x = 0; switch_threadattr_create(&thd_attr, profile->pool); switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_priority_increase(thd_attr); switch_thread_create(&thread, thd_attr, sofia_profile_worker_thread_run, profile, profile->pool); - switch_yield(1000000); + + while(!sofia_test_pflag(profile, PFLAG_WORKER_RUNNING)) { + switch_yield(100000); + if (++x >= 100) { + break; + } + } } void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void *obj) @@ -544,6 +550,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("presence")), TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("dialog")), TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("call-info")), + TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("sla")), + TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("include-session-description")), TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("presence.winfo")), TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("message-summary")), SIPTAG_SUPPORTED_STR("100rel, precondition, timer"), SIPTAG_USER_AGENT_STR(profile->user_agent), TAG_END()); @@ -599,11 +607,12 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting thread for %s\n", profile->name); profile->started = switch_timestamp(NULL); - switch_yield(1000000); sofia_set_pflag_locked(profile, PFLAG_RUNNING); launch_sofia_worker_thread(profile); + switch_yield(1000000); + while (mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING) && sofia_test_pflag(profile, PFLAG_WORKER_RUNNING)) { su_root_step(profile->s_root, 1000); } @@ -1489,15 +1498,13 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status switch_assert(sql); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } } else if (status == 200 && (profile->pflags & PFLAG_PRESENCE)) { char *sql = NULL; sql = switch_mprintf("update sip_dialogs set state='%s' where uuid='%s';\n", astate, switch_core_session_get_uuid(session)); switch_assert(sql); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } } } @@ -2873,8 +2880,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ ); switch_assert(sql); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } return; diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index 189cce0a2c..bd0062fa9a 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -2443,27 +2443,34 @@ void sofia_glue_sql_close(sofia_profile_t *profile) } -void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex) +void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t dyn) { switch_status_t status = SWITCH_STATUS_FALSE; - sofia_sql_job_t *job = NULL; + char *d_sql = NULL, *sql; + + switch_assert(sqlp && *sqlp); + sql = *sqlp; if (profile->sql_queue) { - switch_zmalloc(job, sizeof(*job)); + if (dyn) { + d_sql = sql; + } else { + d_sql = strdup(sql); + } - job->sql = strdup(sql); - switch_assert(job->sql); - job->master = master; - - status = switch_queue_trypush(profile->sql_queue, job); + switch_assert(d_sql); + status = switch_queue_trypush(profile->sql_queue, d_sql); } - if (status != SWITCH_STATUS_SUCCESS) { - if (job) { - free(job->sql); - free(job); + if (status == SWITCH_STATUS_SUCCESS) { + if (dyn) { + *sqlp = NULL; } - sofia_glue_actually_execute_sql(profile, master, sql, mutex); + } else { + if (!dyn) { + switch_safe_free(d_sql); + } + sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); } } diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 56b3bf5492..f58661cbda 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -461,9 +461,10 @@ void sofia_presence_event_handler(switch_event_t *event) if (!switch_strlen_zero((char *)helper.stream.data)) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, helper.stream.data, profile->ireg_mutex); + char *ssql = (char *)helper.stream.data; + sofia_glue_execute_sql(profile, &ssql, SWITCH_TRUE); + helper.stream.data = NULL; } - switch_safe_free(helper.stream.data); } } switch_mutex_unlock(mod_sofia_globals.hash_mutex); @@ -781,7 +782,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char * ct = "application/dialog-info+xml"; } - if (astate && uuid && helper && helper->stream.data) { + if (!switch_strlen_zero(astate) && !switch_strlen_zero(uuid) && helper && helper->stream.data) { helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where uuid='%s';\n", astate, uuid); } @@ -1131,20 +1132,23 @@ void sofia_presence_handle_sip_i_subscribe(int status, exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600); exp = (long) switch_timestamp(NULL) + exp_raw; + if (sofia_test_pflag(profile, PFLAG_MULTIREG)) { + sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id); + } else { + sql = switch_mprintf("delete from sip_subscriptions where " + "proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'", + proto, + from_user, + from_host, + to_user, + to_host, event + ); + } + + switch_mutex_lock(profile->ireg_mutex); - - sql = switch_mprintf("delete from sip_subscriptions where " - "proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'", - proto, - from_user, - from_host, - to_user, - to_host, event - ); - switch_assert(sql != NULL); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); if (sub_state == nua_substate_terminated) { sstr = switch_mprintf("terminated"); @@ -1162,8 +1166,7 @@ void sofia_presence_handle_sip_i_subscribe(int status, contact_str, call_id, full_from, full_via, exp, full_agent, accept); switch_assert(sql != NULL); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); sstr = switch_mprintf("active;expires=%ld", exp_raw); if (status < 200) { @@ -1295,8 +1298,7 @@ void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, n if ((sql = switch_mprintf("update sip_registrations set status='%q',rpid='%q' where sip_user='%q' and sip_host='%q'", note_txt, rpid, from_user, from_host))) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - switch_safe_free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } event_type = sip_header_as_string(profile->home, (void *) sip->sip_event); diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index d114e55ce4..b33cdf2b22 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -210,6 +210,7 @@ int sofia_reg_del_callback(void *pArg, int argc, char **argv, char **columnNames void sofia_reg_check_expire(sofia_profile_t *profile, time_t now) { char sql[1024]; + char *psql = sql; #ifdef SWITCH_HAVE_ODBC if (profile->odbc_dsn) { @@ -245,19 +246,19 @@ void sofia_reg_check_expire(sofia_profile_t *profile, time_t now) } else { switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0"); } - sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL); + sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE); if (now) { switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld", (long) now); } else { switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0"); } - sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL); + sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE); if (now) { switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and expires <= %ld", (long) now); } else { switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0"); } - sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL); + sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE); if (now) { switch_snprintf(sql, sizeof(sql), "select * from sip_registrations where status like '%%NATHACK%%'"); @@ -317,13 +318,10 @@ void sofia_reg_auth_challange(nua_t *nua, sofia_profile_t *profile, nua_handle_t switch_uuid_get(&uuid); switch_uuid_format(uuid_str, &uuid); - switch_mutex_lock(profile->ireg_mutex); sql = switch_mprintf("insert into sip_authentication (nonce, expires) values('%q', %ld)", uuid_str, switch_timestamp(NULL) + profile->nonce_ttl); switch_assert(sql != NULL); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - switch_safe_free(sql); - switch_mutex_unlock(profile->ireg_mutex); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); auth_str = switch_mprintf("Digest realm=\"%q\", nonce=\"%q\",%s algorithm=MD5, qop=\"auth\"", realm, uuid_str, stale ? " stale=\"true\"," : ""); @@ -521,8 +519,7 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, to_host); } switch_mutex_lock(profile->ireg_mutex); - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - switch_safe_free(sql); + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld, '%q')", call_id, to_user, to_host, contact_str, reg_desc, @@ -530,9 +527,7 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han if (sql) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); - switch_safe_free(sql); - sql = NULL; + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } switch_mutex_unlock(profile->ireg_mutex); @@ -573,29 +568,21 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han if ((p = strchr(icontact + 4, ':'))) { *p = '\0'; } - if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q' and contact like '%%%q%%'", to_user, to_host, icontact))) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - switch_safe_free(sql); - sql = NULL; + if ((sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id))) { + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } if ((sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id))) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - switch_safe_free(sql); - sql = NULL; + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } switch_safe_free(icontact); } else { if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, to_host))) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - switch_safe_free(sql); - sql = NULL; + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } if ((sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, to_host))) { - sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); - switch_safe_free(sql); - sql = NULL; + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); } } if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_OUT) == SWITCH_STATUS_SUCCESS) { @@ -654,19 +641,25 @@ void sofia_reg_handle_sip_i_register(nua_t * nua, sofia_profile_t *profile, nua_ if (!sip || !sip->sip_request || !sip->sip_request->rq_method_name) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received an invalid packet!\n"); nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END()); - return; + goto end; } if (!(sip->sip_contact && sip->sip_contact->m_url)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO CONTACT!\n"); nua_respond(nh, 400, "Missing Contact Header", TAG_END()); - return; + goto end; } sofia_reg_handle_register(nua, profile, nh, sip, REG_REGISTER, key, sizeof(key), &v_event); + if (v_event) { switch_event_fire(&v_event); } + + end: + + nua_handle_destroy(nh); + }