]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
support multiple subscriptions for sla goodies
authorBrian West <brian@freeswitch.org>
Wed, 5 Mar 2008 20:31:18 +0000 (20:31 +0000)
committerBrian West <brian@freeswitch.org>
Wed, 5 Mar 2008 20:31:18 +0000 (20:31 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7793 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/endpoints/mod_sofia/mod_sofia.c
src/mod/endpoints/mod_sofia/mod_sofia.h
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_glue.c
src/mod/endpoints/mod_sofia/sofia_presence.c
src/mod/endpoints/mod_sofia/sofia_reg.c

index 9d2354806c50618bdc4bc20b38e1abd389100dde..c75d4d2613c0d187486ded2eba9fbd9188da4358 100644 (file)
@@ -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))) {
index fd8ffa4da7be87d09de11b75044191ecf8eaefc3..ce688e170fd138630130772e51b6e6d295a8be6b 100644 (file)
@@ -88,12 +88,6 @@ typedef struct private_object private_object_t;
 #include <sofia-sip/nea.h>
 #include <sofia-sip/msg_addr.h>
 
-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);
index 4f701cd959988cf77e891e3b56ad07450795ce99..a496cb04047c164078fd4f2b9f619bb9a7844c06 100644 (file)
@@ -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;
index 189cce0a2ca9ea8f40941a8d9bb28457fa6212fc..bd0062fa9a988865e39f16d8834fd4edf5322b0c 100644 (file)
@@ -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;
+               }
+       } else {
+               if (!dyn) {
+                       switch_safe_free(d_sql);
                }
-               sofia_glue_actually_execute_sql(profile, master, sql, mutex);
+               sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
        }
 
 }
index 56b3bf54923d9e95144034b02e1ebb645094ff8e..f58661cbda098267bf2a61aebe999fb4f3ffdad9 100644 (file)
@@ -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;
 
-               switch_mutex_lock(profile->ireg_mutex);
+               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
+                                                                );
+               }
 
-               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);
                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);
index d114e55ce4982ab14ebdba101fd31e17116ed251..b33cdf2b22282e3875e9a5822acd4c52c04cddee 100644 (file)
@@ -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);
+
 }