]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
improvements
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 18 Oct 2007 16:17:42 +0000 (16:17 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 18 Oct 2007 16:17:42 +0000 (16:17 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5967 d0543943-73ff-0310-b7d9-9358b9ac24b2

conf/directory.xml
src/include/switch_core.h
src/mod/applications/mod_voicemail/mod_voicemail.c
src/mod/endpoints/mod_dingaling/mod_dingaling.c
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_glue.c
src/mod/endpoints/mod_sofia/sofia_reg.c
src/switch_core_db.c

index a1468eae15bdde4991afb363bb90bb77dc959402..53216c93aaa079b39fe9af4d255f6c2248277401 100644 (file)
@@ -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>
index 5c7116dcdb6ad91fa9127a4f1c8d6dac512831f9..d4e32d34ab529d25f653b59b2cff429224fa7ae9 100644 (file)
@@ -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"
 /*!
index 6390f6ed1e951ad5bc76ac1b2fda2771ebfdd5be..eaa67c628ac56145a0dc0f565579b51b1978c705 100644 (file)
@@ -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;
index 224d5cf8d09470a9cb80440526d66ee2f7567cb2..aeeda4dc36944fb23d7a51018fcd735f0feb8289 100644 (file)
@@ -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;
index e5a6e82fae9dd61246fcc6f894baf265f75939b7..17c24c33eef3492daddf584ac92fd8cf0a388bc7 100644 (file)
@@ -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);
index ab97e34f6769efc43c9373b1a43dfd2c74d745ea..40c1ce2307be1a84eb75c8547ddaef3482d5c1dc 100644 (file)
@@ -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
        }
index 3071a0ee1bec6e8330ee7f612890dd00c377880a..75ab4ab350936c7311dcc88b70663d1f1549bc55 100644 (file)
@@ -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 <sip:%s@%s:%d;%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 <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 @@ 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;
        }
 
index 87f0eb6d128d80faacce8d9e71bbbf9ecc663316..6c5a822f1d30493620856e7979075b0c20612a08 100644 (file)
@@ -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);