]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-3768 --resolve
authorMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 16 Dec 2011 19:31:15 +0000 (14:31 -0500)
committerMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 16 Dec 2011 19:31:15 +0000 (14:31 -0500)
src/mod/endpoints/mod_sofia/mod_sofia.h
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_reg.c

index 7a1dc878680f15d8ef01f465117602f5bf1ec776..5aebcd5fd27c2751c932bdcf4a42f0a2aed9e117 100644 (file)
@@ -800,6 +800,7 @@ typedef enum {
        AUTH_OK,
        AUTH_FORBIDDEN,
        AUTH_STALE,
+       AUTH_RENEWED,
 } auth_res_t;
 
 typedef struct {
index 759d8f1219c364a3562c7635b4df64a9f5acc7ea..84fe5aff2a8544b4d8faf9259c3ecc032909286d 100644 (file)
@@ -952,7 +952,7 @@ static void our_sofia_event_callback(nua_event_t event,
                                                                                        REG_INVITE, NULL, NULL, NULL);
                }
 
-               if (auth_res != AUTH_OK) {
+               if ((auth_res != AUTH_OK && auth_res != AUTH_RENEWED)) {
                        //switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
                        nua_respond(nh, SIP_401_UNAUTHORIZED, TAG_END());
                        goto done;
index def1673c30f85c8b5840ce8cc7e547eb78aee690..1123fbd37018d8e6327d3d6733fc5ee448d3441b 100644 (file)
@@ -1371,7 +1371,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                        }
                }
 
-               if (auth_res != AUTH_OK && !stale) {
+               if (auth_res != AUTH_OK && auth_res != AUTH_RENEWED && !stale) {
                        if (auth_res == AUTH_FORBIDDEN) {
                                nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(de->data->e_msg), TAG_END());
                                forbidden = 1;
@@ -1483,41 +1483,49 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                const char *realm = reg_host;
                char *url = NULL;
                char *contact = NULL;
+               switch_bool_t update_registration = SWITCH_FALSE;
 
                if (auth_params) {
                        username = switch_event_get_header(auth_params, "sip_auth_username");
                        realm = switch_event_get_header(auth_params, "sip_auth_realm");
                }
+               if (auth_res != AUTH_RENEWED) {
+                       if (multi_reg) {
 
-               if (multi_reg) {
-
-                       if (delete_subs) {
-                               if (reg_count == 1) {
-                                       sql = switch_mprintf("update sip_subscriptions set expires=%ld where sip_user='%q' and sip_host='%q' and contact='%q'", 
-                                                                                (long) switch_epoch_time_now(NULL), to_user, sub_host, contact_str);
-                                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                               if (delete_subs) {
+                                       if (reg_count == 1) {
+                                               sql = switch_mprintf("update sip_subscriptions set expires=%ld where sip_user='%q' and sip_host='%q' and contact='%q'", 
+                                                               (long) switch_epoch_time_now(NULL), to_user, sub_host, contact_str);
+                                               sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                                       }
                                }
-                       }
-                               
 
-                       if (multi_reg_contact) {
-                               sql =
-                                       switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q' and contact='%q'", 
-                                                                  to_user, reg_host, contact_str);
+
+                               if (multi_reg_contact) {
+                                       sql =
+                                               switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q' and contact='%q'", to_user, reg_host, contact_str);
+                               } else {
+                                       sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
+                               }
                        } else {
-                               sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
+                               if (delete_subs) {
+                                       sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, sub_host);
+                                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                               }
+                               sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, reg_host);
                        }
+                       switch_mutex_lock(profile->ireg_mutex);
+                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
                } else {
-                       if (delete_subs) {
-                               sql = switch_mprintf("update sip_subscriptions set expires=%ld where sip_user='%q' and sip_host='%q'", 
-                                                                        (long) switch_epoch_time_now(NULL), to_user, sub_host);
-                               sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                       char buf[32] = "";
+                       sql = switch_mprintf("select count(*) from sip_registrations where sip_user='%q' and sip_host='%q' and contact='%q'", to_user, reg_host, contact_str);
+
+                       sofia_glue_execute_sql2str(profile, profile->ireg_mutex, sql, buf, sizeof(buf));
+                       switch_safe_free(sql);
+                       if (atoi(buf) > 0) {
+                               update_registration = SWITCH_TRUE;
                        }
-                       sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", 
-                                                                to_user, reg_host);
                }
-               switch_mutex_lock(profile->ireg_mutex);
-               sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
 
                switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET);
 
@@ -1530,21 +1538,25 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                switch_safe_free(url);
                switch_safe_free(contact);
 
-               sql = switch_mprintf("insert into sip_registrations "
-                                                        "(call_id,sip_user,sip_host,presence_hosts,contact,status,rpid,expires,"
-                                                        "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm,"
-                                                        "mwi_user,mwi_host, orig_server_host, orig_hostname) "
-                                                        "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q','%q','%q','%q','%q')", 
-                                                        call_id, to_user, reg_host, profile->presence_hosts ? profile->presence_hosts : reg_host, 
-                                                        contact_str, reg_desc, rpid, (long) switch_epoch_time_now(NULL) + (long) exptime + 60, 
-                                                        agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm, 
-                                                        mwi_user, mwi_host, guess_ip4, mod_sofia_globals.hostname);
-                                                        
+               if (!update_registration) {
+                       sql = switch_mprintf("insert into sip_registrations "
+                                       "(call_id,sip_user,sip_host,presence_hosts,contact,status,rpid,expires,"
+                                       "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm,"
+                                       "mwi_user,mwi_host, orig_server_host, orig_hostname) "
+                                       "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q','%q','%q','%q','%q')", 
+                                       call_id, to_user, reg_host, profile->presence_hosts ? profile->presence_hosts : reg_host, 
+                                       contact_str, reg_desc, rpid, (long) switch_epoch_time_now(NULL) + (long) exptime + 60, 
+                                       agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm, 
+                                       mwi_user, mwi_host, guess_ip4, mod_sofia_globals.hostname);
+               } else {
+                       sql = switch_mprintf("update sip_registrations set expires = %ld where sip_user='%q' and sip_host='%q' and contact='%q'", (long) switch_epoch_time_now(NULL) + (long) exptime + 60, to_user, reg_host, contact_str);
+               }                                
+
                if (sql) {
                        sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
                }
 
-               if (sofia_reg_reg_count(profile, to_user, reg_host) == 1) {
+               if (!update_registration && sofia_reg_reg_count(profile, to_user, reg_host) == 1) {
                        sql = switch_mprintf("delete from sip_presence where sip_user='%q' and sip_host='%q' and profile_name='%q' and open_closed='closed'", 
                                                                 to_user, reg_host, profile->name);
                        if (mod_sofia_globals.debug_presence > 0) {
@@ -2658,8 +2670,7 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
        switch_safe_free(input2);
 
   skip_auth:
-
-       if (first && ret == AUTH_OK) {
+       if (first && (ret == AUTH_OK || ret == AUTH_RENEWED)) {
                if (v_event) {
                        switch_event_create_plain(v_event, SWITCH_EVENT_REQUEST_PARAMS);
                }
@@ -2785,6 +2796,9 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
                switch_assert(sql != NULL);
                sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
                switch_safe_free(sql);
+
+               if (ret == AUTH_OK)
+                       ret = AUTH_RENEWED;
        }
 
        switch_event_destroy(&params);
@@ -2804,7 +2818,7 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
        switch_safe_free(response);
 
        if (reg_count && !*reg_count) {
-               if (ret == AUTH_OK) {
+               if ((ret == AUTH_OK || ret == AUTH_RENEWED)) {
                        if (ncl) {
                                *reg_count = ncl;
                        } else {