Author: anthm Date: Thu Oct 18 12:17:42 2007 New Revision: 5967 Modified: freeswitch/trunk/conf/directory.xml freeswitch/trunk/src/include/switch_core.h freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c freeswitch/trunk/src/switch_core_db.c Log: improvements Modified: freeswitch/trunk/conf/directory.xml ============================================================================== --- freeswitch/trunk/conf/directory.xml (original) +++ freeswitch/trunk/conf/directory.xml Thu Oct 18 12:17:42 2007 @@ -45,7 +45,9 @@ <variables> <!--all variables here will be set on all inbound calls that originate from this user --> <variable name="testing" value="this"/> - <!--<variable name="force-contact" value="nat-connectile-dysfunction"/>--> + <!--<variable name="sip-force-contact" value="nat-connectile-dysfunction"/>--> + <!--<variable name="sip-force-expires" value="10"/>--> + <!--<variable name="sip-register-gateway" value="cluecon.com"/>--> </variables> <vcard> Modified: freeswitch/trunk/src/include/switch_core.h ============================================================================== --- freeswitch/trunk/src/include/switch_core.h (original) +++ freeswitch/trunk/src/include/switch_core.h Thu Oct 18 12:17:42 2007 @@ -1077,9 +1077,10 @@ \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" /*! Modified: freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c ============================================================================== --- freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c (original) +++ freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c Thu Oct 18 12:17:42 2007 @@ -404,8 +404,8 @@ } 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; Modified: freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c ============================================================================== --- freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c (original) +++ freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c Thu Oct 18 12:17:42 2007 @@ -2213,7 +2213,7 @@ } 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; Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c ============================================================================== --- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (original) +++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c Thu Oct 18 12:17:42 2007 @@ -257,12 +257,11 @@ 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 @@ 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 @@ 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); Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c ============================================================================== --- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (original) +++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c Thu Oct 18 12:17:42 2007 @@ -1550,6 +1550,7 @@ 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 @@ 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 } Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c ============================================================================== --- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (original) +++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c Thu Oct 18 12:17:42 2007 @@ -340,6 +340,7 @@ 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 @@ } 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 <sip:%s@%s:%d;%s>", @@ -423,6 +426,7 @@ snprintf(contact_str, sizeof(contact_str), "%s <sip:%s@%s:%d>", 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 @@ } } } + + 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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; } Modified: freeswitch/trunk/src/switch_core_db.c ============================================================================== --- freeswitch/trunk/src/switch_core_db.c (original) +++ freeswitch/trunk/src/switch_core_db.c Thu Oct 18 12:17:42 2007 @@ -171,7 +171,7 @@ } -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_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);