From 42644022e1c9209fd2ecba9064acfca8c72c321e Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 18 Oct 2007 16:17:42 +0000 Subject: [PATCH] improvements git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5967 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- conf/directory.xml | 4 +- src/include/switch_core.h | 3 +- .../mod_voicemail/mod_voicemail.c | 4 +- .../endpoints/mod_dingaling/mod_dingaling.c | 2 +- src/mod/endpoints/mod_sofia/sofia.c | 17 ++---- src/mod/endpoints/mod_sofia/sofia_glue.c | 24 ++++++-- src/mod/endpoints/mod_sofia/sofia_reg.c | 57 +++++++++++++------ src/switch_core_db.c | 10 +++- 8 files changed, 81 insertions(+), 40 deletions(-) diff --git a/conf/directory.xml b/conf/directory.xml index a1468eae15..53216c93aa 100644 --- a/conf/directory.xml +++ b/conf/directory.xml @@ -45,7 +45,9 @@ - + + + diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 5c7116dcdb..d4e32d34ab 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1077,9 +1077,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute(switch_core_db \brief perform a test query then perform a reactive query if the first one fails \param db the db handle \param test_sql the test sql + \param drop_sql the drop sql \param reactive_sql the reactive sql */ -SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *reactive_sql); +SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *drop_sql, char *reactive_sql); #define SWITCH_CORE_DB "core" /*! diff --git a/src/mod/applications/mod_voicemail/mod_voicemail.c b/src/mod/applications/mod_voicemail/mod_voicemail.c index 6390f6ed1e..eaa67c628a 100644 --- a/src/mod/applications/mod_voicemail/mod_voicemail.c +++ b/src/mod/applications/mod_voicemail/mod_voicemail.c @@ -404,8 +404,8 @@ static switch_status_t load_config(void) } else { #endif if ((db = switch_core_db_open_file(profile->dbname))) { - switch_core_db_test_reactive(db, "select * from voicemail_data", vm_sql); - switch_core_db_test_reactive(db, "select * from voicemail_prefs", vm_pref_sql); + switch_core_db_test_reactive(db, "select * from voicemail_data", NULL, vm_sql); + switch_core_db_test_reactive(db, "select * from voicemail_prefs", NULL, vm_pref_sql); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n"); continue; diff --git a/src/mod/endpoints/mod_dingaling/mod_dingaling.c b/src/mod/endpoints/mod_dingaling/mod_dingaling.c index 224d5cf8d0..aeeda4dc36 100644 --- a/src/mod/endpoints/mod_dingaling/mod_dingaling.c +++ b/src/mod/endpoints/mod_dingaling/mod_dingaling.c @@ -2213,7 +2213,7 @@ static switch_status_t load_config(void) } else { #endif if ((db = switch_core_db_open_file(profile->dbname))) { - switch_core_db_test_reactive(db, "select * from jabber_subscriptions", sub_sql); + switch_core_db_test_reactive(db, "select * from jabber_subscriptions", NULL, sub_sql); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n"); continue; diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index e5a6e82fae..17c24c33ee 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -257,12 +257,11 @@ void event_handler(switch_event_t *event) char *contact_str = switch_event_get_header(event, "orig-contact"); char *exp_str = switch_event_get_header(event, "orig-expires"); char *rpid = switch_event_get_header(event, "orig-rpid"); + char *call_id = switch_event_get_header(event, "orig-call-id"); long expires = (long) time(NULL) + atol(exp_str); char *profile_name = switch_event_get_header(event, "orig-profile-name"); sofia_profile_t *profile = NULL; - char *icontact = NULL, *p; - - + if (!rpid) { rpid = "unknown"; } @@ -272,13 +271,7 @@ void event_handler(switch_event_t *event) return; } if (sofia_test_pflag(profile, PFLAG_MULTIREG)) { - icontact = sofia_glue_get_url_from_contact(contact_str, 1); - - if ((p = strchr(icontact, ';'))) { - *p = '\0'; - } - sql = switch_mprintf("delete from sip_registrations where user='%q' and host='%q' and contact like '%%%q%%'", from_user, from_host, icontact); - switch_safe_free(icontact); + sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id); } else { sql = switch_mprintf("delete from sip_registrations where user='%q' and host='%q'", from_user, from_host); } @@ -287,8 +280,8 @@ void event_handler(switch_event_t *event) sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); switch_safe_free(sql); - sql = switch_mprintf("insert into sip_registrations values ('%q','%q','%q','Regestered', '%q', %ld)", - from_user, from_host, contact_str, rpid, expires); + sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','Regestered', '%q', %ld)", + call_id, from_user, from_host, contact_str, rpid, expires); if (sql) { sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index ab97e34f67..40c1ce2307 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -1550,6 +1550,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile) char reg_sql[] = "CREATE TABLE sip_registrations (\n" + " call_id VARCHAR(255),\n" " user VARCHAR(255),\n" " host VARCHAR(255),\n" " contact VARCHAR(1024),\n" @@ -1591,18 +1592,29 @@ int sofia_glue_init_sql(sofia_profile_t *profile) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn); - switch_odbc_handle_exec(profile->master_odbc, reg_sql, NULL); - switch_odbc_handle_exec(profile->master_odbc, sub_sql, NULL); - switch_odbc_handle_exec(profile->master_odbc, auth_sql, NULL); + if (switch_odbc_handle_exec(profile->master_odbc, "select call_id from sip_registrations", NULL) != SWITCH_ODBC_SUCCESS) { + switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_registrations", NULL); + switch_odbc_handle_exec(profile->master_odbc, reg_sql, NULL); + } + + if (switch_odbc_handle_exec(profile->master_odbc, "select contact from sip_subscriptions", NULL) != SWITCH_ODBC_SUCCESS) { + switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_subscriptions", NULL); + switch_odbc_handle_exec(profile->master_odbc, sub_sql, NULL); + } + + if (switch_odbc_handle_exec(profile->master_odbc, "select nonce from sip_authentication", NULL) != SWITCH_ODBC_SUCCESS) { + switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_authentication", NULL); + switch_odbc_handle_exec(profile->master_odbc, auth_sql, NULL); + } } else { #endif if (!(profile->master_db = switch_core_db_open_file(profile->dbname))) { return 0; } - switch_core_db_test_reactive(profile->master_db, "select contact from sip_registrations", reg_sql); - switch_core_db_test_reactive(profile->master_db, "select contact from sip_subscriptions", sub_sql); - switch_core_db_test_reactive(profile->master_db, "select * from sip_authentication", auth_sql); + switch_core_db_test_reactive(profile->master_db, "select call_id from sip_registrations", "DROP TABLE sip_registrations", reg_sql); + switch_core_db_test_reactive(profile->master_db, "select contact from sip_subscriptions", "DROP TABLE sip_subscriptions", sub_sql); + switch_core_db_test_reactive(profile->master_db, "select * from sip_authentication", "DROP TABLE sip_authentication", auth_sql); #ifdef SWITCH_HAVE_ODBC } diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 3071a0ee1b..75ab4ab350 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -340,6 +340,7 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han char *register_gateway = NULL; int network_port; int cd = 0; + char *call_id = NULL; /* all callers must confirm that sip, sip->sip_request and sip->sip_contact are not NULL */ assert(sip != NULL && sip->sip_contact != NULL && sip->sip_request != NULL); @@ -412,9 +413,11 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han } if (v_event && *v_event) { - register_gateway = switch_event_get_header(*v_event, "register-gateway"); - - if ((v_contact_str = switch_event_get_header(*v_event, "force-contact"))) { + char *exp_var; + + register_gateway = switch_event_get_header(*v_event, "sip-register-gateway"); + + if ((v_contact_str = switch_event_get_header(*v_event, "sip-force-contact"))) { if (!strcasecmp(v_contact_str, "nat-connectile-dysfunction") || !strcasecmp(v_contact_str, "NDLB-connectile-dysfunction")) { if (contact->m_url->url_params) { snprintf(contact_str, sizeof(contact_str), "%s ", @@ -423,6 +426,7 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han snprintf(contact_str, sizeof(contact_str), "%s ", display, contact->m_url->url_user, network_ip, network_port); } cd = 1; + exptime = 20; } else { char *p; switch_copy_string(contact_str, v_contact_str, sizeof(contact_str)); @@ -433,6 +437,13 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han } } } + + if ((exp_var = switch_event_get_header(*v_event, "sip-force-expires"))) { + int tmp = atoi(exp_var); + if (tmp > 0) { + exptime = tmp; + } + } } if (auth_res != AUTH_OK && !stale) { @@ -459,25 +470,23 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han if (regtype != REG_REGISTER) { return 0; } + + call_id = sip_header_as_string(profile->home, (void *) sip->sip_call_id); + assert(call_id); + if (exptime) { - + if (sofia_test_pflag(profile, PFLAG_MULTIREG)) { - char *icontact, *p; - icontact = sofia_glue_get_url_from_contact(contact_str, 1); - if ((p = strchr(icontact, ';'))) { - *p = '\0'; - } - - sql = switch_mprintf("delete from sip_registrations where user='%q' and host='%q' and contact like '%%%q%%'", to_user, to_host, icontact); - switch_safe_free(icontact); + sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id); } else { sql = switch_mprintf("delete from sip_registrations where user='%q' and 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); - sql = switch_mprintf("insert into sip_registrations values ('%q','%q','%q','%q', '%q', %ld)", + + sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld)", call_id, to_user, to_host, contact_str, cd ? "Registered(NATHACK)" : "Registered", rpid, (long) time(NULL) + (long) exptime * 2); @@ -493,6 +502,7 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from-user", "%s", to_user); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from-host", "%s", to_host); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "contact", "%s", contact_str); + switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "call-id", "%s", call_id); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "rpid", "%s", rpid); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "expires", "%ld", (long) exptime); switch_event_fire(&s_event); @@ -509,7 +519,6 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han switch_event_add_header(event, SWITCH_STACK_BOTTOM, "login", "%s", profile->url); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rpid", "%s", rpid); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, to_host); - switch_event_add_header(event, SWITCH_STACK_BOTTOM, "status", "Registered"); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "event_type", "presence"); switch_event_fire(&event); @@ -521,13 +530,16 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han if ((p = strchr(icontact, ';'))) { *p = '\0'; } + if ((p = strchr(icontact + 4, ':'))) { + *p = '\0'; + } if ((sql = switch_mprintf("delete from sip_subscriptions where user='%q' and 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_registrations where user='%q' and host='%q' and contact like '%%%q%%'", to_user, to_host, icontact))) { + if ((sql = switch_mprintf("delete from sip_registrations where call_id='%q'", to_user, to_host, call_id))) { sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); switch_safe_free(sql); sql = NULL; @@ -565,8 +577,21 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han switch_event_fire(&event); } + + if (call_id) { + su_free(profile->home, call_id); + } + if (regtype == REG_REGISTER) { - nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT(contact), NUTAG_WITH_THIS(nua), TAG_END()); + char *new_contact = NULL; + if (exptime) { + new_contact = switch_mprintf("%s;expires=%ld", contact_str, (long)exptime); + nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT_STR(new_contact), NUTAG_WITH_THIS(nua), TAG_END()); + switch_safe_free(new_contact); + } else { + nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT(contact), NUTAG_WITH_THIS(nua), TAG_END()); + } + return 1; } diff --git a/src/switch_core_db.c b/src/switch_core_db.c index 87f0eb6d12..6c5a822f1d 100644 --- a/src/switch_core_db.c +++ b/src/switch_core_db.c @@ -171,7 +171,7 @@ SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(char *filename) } -SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *reactive_sql) +SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *drop_sql, char *reactive_sql) { char *errmsg; @@ -183,6 +183,14 @@ SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *te switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql); switch_core_db_free(errmsg); errmsg = NULL; + if (drop_sql) { + switch_core_db_exec(db, drop_sql, NULL, NULL, &errmsg); + } + if (errmsg) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql); + switch_core_db_free(errmsg); + errmsg = NULL; + } switch_core_db_exec(db, reactive_sql, NULL, NULL, &errmsg); if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);