]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add profile civil unions warning will nuke your db table next time you update
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 18 Sep 2008 00:01:03 +0000 (00:01 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 18 Sep 2008 00:01:03 +0000 (00:01 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9582 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 7e3704c9d7bea8cb1ec54085006cbb78af865b00..ad50979f5c8295d6612e662eb8af7c8dbe50aa75 100644 (file)
@@ -268,7 +268,7 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
                switch_core_hash_delete(tech_pvt->profile->chat_hash, tech_pvt->hash_key);
        }
 
-       if (session && (tech_pvt->profile->pflags & PFLAG_PRESENCE)) {
+       if (session && tech_pvt->profile->pres_type) {
                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, &sql, SWITCH_TRUE);
@@ -1214,13 +1214,14 @@ static int show_reg_callback(void *pArg, int argc, char **argv, char **columnNam
        }
 
        cb->stream->write_function(cb->stream,
-                                                          "Call-ID \t%s\n"
-                                                          "User    \t%s@%s\n"
-                                                          "Contact \t%s\n"
-                                                          "Agent   \t%s\n"
-                                                          "Status  \t%s(%s) EXP(%s)\n\n",
+                                                          "Call-ID: \t%s\n"
+                                                          "User:    \t%s@%s\n"
+                                                          "Contact: \t%s\n"
+                                                          "Agent:   \t%s\n"
+                                                          "Status:  \t%s(%s) EXP(%s)\n"
+                                                          "Host:    \t%s\n\n",
                                                           switch_str_nil(argv[0]), switch_str_nil(argv[1]), switch_str_nil(argv[2]), switch_str_nil(argv[3]),
-                                                          switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf);
+                                                          switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf, switch_str_nil(argv[11]));
        return 0;
 }
 
@@ -1279,6 +1280,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                        stream->write_function(stream, "Alias Of      \t\t%s\n", switch_str_nil(profile->name));
                                }
                                stream->write_function(stream, "DBName        \t\t%s\n", switch_str_nil(profile->dbname));
+                               stream->write_function(stream, "Pres Hosts    \t\t%s\n", switch_str_nil(profile->presence_hosts));
                                stream->write_function(stream, "Dialplan      \t\t%s\n", switch_str_nil(profile->dialplan));
                                stream->write_function(stream, "Context       \t\t%s\n", switch_str_nil(profile->context));
                                stream->write_function(stream, "RTP-IP        \t\t%s\n", switch_str_nil(profile->rtpip));
@@ -1320,7 +1322,10 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                cb.profile = profile;
                                cb.stream = stream;
 
-                               sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, "select * from sip_registrations", show_reg_callback, &cb);
+                               sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, 
+                                                                                               "select call_id,sip_user,sip_host,contact,status,"
+                                                                                               "rpid,expires,user_agent,server_user,server_host,profile_name,hostname"
+                                                                                               " from sip_registrations", show_reg_callback, &cb);
 
                                stream->write_function(stream, "%s\n", line);
 
@@ -1599,7 +1604,7 @@ static int contact_callback(void *pArg, int argc, char **argv, char **columnName
        char *contact;
 
        if (!switch_strlen_zero(argv[0]) && (contact = sofia_glue_get_url_from_contact(argv[0], 1)) ) {
-               cb->stream->write_function(cb->stream, "%ssofia/%s/%s,", argv[1], cb->profile->name, sofia_glue_strip_proto(contact));
+               cb->stream->write_function(cb->stream, "%ssofia/%s/%s,", argv[2], argv[1], sofia_glue_strip_proto(contact));
                free(contact);
        }
 
@@ -1682,11 +1687,14 @@ SWITCH_STANDARD_API(sofia_contact_function)
                        cb.stream = &mystream;
 
                        if (exclude_contact) {
-                               sql = switch_mprintf("select contact, '%q' from sip_registrations where sip_user='%q' and sip_host='%q' and contact not like '%%%s%%'",
-                                                                        ( concat != NULL ) ? concat : "", user, domain, exclude_contact);
+                               sql = switch_mprintf("select contact, profile_name, '%q' "
+                                                                        "from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%') "
+                                                                        "and contact not like '%%%s%%'",
+                                                                        ( concat != NULL ) ? concat : "", user, domain, domain, exclude_contact);
                        } else {
-                               sql = switch_mprintf("select contact, '%q' from sip_registrations where sip_user='%q' and sip_host='%q'", 
-                                                                        ( concat != NULL ) ? concat : "", user, domain);
+                               sql = switch_mprintf("select contact, profile_name, '%q' "
+                                                                        "from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%')", 
+                                                                        ( concat != NULL ) ? concat : "", user, domain, domain);
                        }
 
                        switch_assert(sql);
@@ -2006,7 +2014,7 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
 
        if (tech_pvt->local_url) {
                switch_channel_set_variable(nchannel, "sip_local_url", tech_pvt->local_url);
-               if (sofia_test_pflag(profile, PFLAG_PRESENCE)) {
+               if (profile->pres_type) {
                        switch_channel_set_variable(nchannel, "presence_id", tech_pvt->local_url);
                }
        }
@@ -2219,6 +2227,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load)
        switch_mutex_init(&mod_sofia_globals.mutex, SWITCH_MUTEX_NESTED, mod_sofia_globals.pool);
 
        switch_find_local_ip(mod_sofia_globals.guess_ip, sizeof(mod_sofia_globals.guess_ip), AF_INET);
+       gethostname(mod_sofia_globals.hostname, sizeof(mod_sofia_globals.hostname));
+
 
        switch_core_hash_init(&mod_sofia_globals.profile_hash, mod_sofia_globals.pool);
        switch_core_hash_init(&mod_sofia_globals.gateway_hash, mod_sofia_globals.pool);
index 15d5606c0765ff4dffedb9ac92bc65ec100ac870..32f79b47aeae0031205afde9341b78c4b593ea31 100644 (file)
@@ -137,7 +137,7 @@ typedef enum {
        PFLAG_BLIND_REG = (1 << 1),
        PFLAG_AUTH_ALL = (1 << 2),
        PFLAG_FULL_ID = (1 << 3),
-       PFLAG_PRESENCE = (1 << 4),
+       PFLAG_USE_ME = (1 << 4),
        PFLAG_PASS_RFC2833 = (1 << 5),
        PFLAG_DISABLE_TRANSCODING = (1 << 6),
        PFLAG_REWRITE_TIMESTAMPS = (1 << 7),
@@ -218,6 +218,7 @@ struct mod_sofia_globals {
        int32_t threads;
        switch_mutex_t *mutex;
        char guess_ip[80];
+       char hostname[512];
        switch_queue_t *presence_queue;
        switch_queue_t *mwi_queue;
        struct sofia_private destroy_private;
@@ -295,6 +296,12 @@ struct sofia_gateway {
        struct sofia_gateway *next;
 };
 
+typedef enum {
+       PRES_TYPE_NONE = 0,
+       PRES_TYPE_FULL = 1,
+       PRES_TYPE_PASSIVE = 2
+} sofia_presence_type_t;
+
 struct sofia_profile {
        int debug;
        char *name;
@@ -321,6 +328,7 @@ struct sofia_profile {
        char *reg_db_domain;
        char *user_agent;
        char *record_template;
+       char *presence_hosts;
        sofia_dtmf_t dtmf_type;
        int sip_port;
        int tls_sip_port;
@@ -367,6 +375,7 @@ struct sofia_profile {
        char *nat_acl[SOFIA_MAX_ACL];
        uint32_t nat_acl_count;
        int rport_level;
+       sofia_presence_type_t pres_type;
 };
 
 struct private_object {
index 3ae58b3e0199ff763daa1e35b6c31848f7a282b3..950d4ce062e615a0111cfb8f6c758d5d1119b9a8 100644 (file)
@@ -72,7 +72,7 @@ void sofia_handle_sip_r_notify(switch_core_session_t *session, int status,
 {
        if (status >= 300 && sip && sip->sip_call_id) {
                char *sql;
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "delete subscriptions for failed notify\n");
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "delete subscriptions for failed notify\n");
                sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", sip->sip_call_id->i_id);
                switch_assert(sql != NULL);
                sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
@@ -387,6 +387,7 @@ void event_handler(switch_event_t *event)
                long expires = (long) switch_timestamp(NULL);
                char *profile_name = switch_event_get_header(event, "orig-profile-name");
                char *to_user = switch_event_get_header(event, "orig-to-user");
+               char *presence_hosts = switch_event_get_header(event, "presence-hosts");
                sofia_profile_t *profile = NULL;
 
                char guess_ip4[256];
@@ -413,8 +414,11 @@ void event_handler(switch_event_t *event)
                sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                
                switch_find_local_ip(guess_ip4, sizeof(guess_ip4), AF_INET);
-               sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','Registered', '%q', %ld, '%q', '%q', '%q')",
-                                                        call_id, from_user, from_host, contact_str, rpid, expires, user_agent, to_user, guess_ip4);
+               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) "
+                                                        "values ('%q', '%q','%q','%q','Registered', '%q', %ld, '%q', '%q', '%q','%q','%q')",
+                                                        call_id, from_user, from_host, presence_hosts, contact_str, rpid, expires, user_agent, to_user, guess_ip4, 
+                                                        profile_name,mod_sofia_globals.hostname);
 
                if (sql) {
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
@@ -572,16 +576,16 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
                                   NUTAG_ALLOW_EVENTS("talk"),
                                   NUTAG_SESSION_TIMER(profile->session_timeout),
                                   NTATAG_MAX_PROCEEDING(profile->max_proceeding),
-                                  TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW("PUBLISH")),
-                                  TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW("SUBSCRIBE")),
-                                  TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ENABLEMESSAGE(1)),
-                                  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")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW("PUBLISH")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW("SUBSCRIBE")),
+                                  TAG_IF(profile->pres_type, NUTAG_ENABLEMESSAGE(1)),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("presence")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("dialog")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("call-info")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("sla")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("include-session-description")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("presence.winfo")),
+                                  TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("message-summary")),
                                   SIPTAG_SUPPORTED_STR(supported), SIPTAG_USER_AGENT_STR(profile->user_agent), TAG_END());
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Set params for %s\n", profile->name);
@@ -600,8 +604,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
                                           TAG_IF((profile->mflags & MFLAG_REGISTER), NUTAG_ALLOW("REGISTER")),
                                           TAG_IF((profile->mflags & MFLAG_REFER), NUTAG_ALLOW("REFER")),
                                           NUTAG_ALLOW("INFO"),
-                                          TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW("PUBLISH")),
-                                          TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ENABLEMESSAGE(1)),
+                                          TAG_IF(profile->pres_type, NUTAG_ALLOW("PUBLISH")),
+                                          TAG_IF(profile->pres_type, NUTAG_ENABLEMESSAGE(1)),
                                           SIPTAG_SUPPORTED_STR(supported), SIPTAG_USER_AGENT_STR(profile->user_agent), TAG_END());
        }
 
@@ -628,7 +632,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
 
        sofia_glue_add_profile(profile->name, profile);
 
-       if (profile->pflags & PFLAG_PRESENCE) {
+       if (profile->pres_type) {
                sofia_presence_establish_presence(profile);
        }
 
@@ -1443,8 +1447,12 @@ switch_status_t config_sofia(int reload, char *profile_name)
                                                if (switch_true(val)) {
                                                        profile->rport_level = 2;
                                                }
+                                       } else if (!strcasecmp(var, "dbname")) {
+                                               profile->dbname = switch_core_strdup(profile->pool, val);
+                                       } else if (!strcasecmp(var, "presence-hosts")) {
+                                               profile->presence_hosts = switch_core_strdup(profile->pool, val);
                                        } else if (!strcasecmp(var, "record-template")) {
-                                               profile->record_template = switch_core_strdup(profile->pool, val);;
+                                               profile->record_template = switch_core_strdup(profile->pool, val);
                                        } else if ((!strcasecmp(var, "inbound-no-media") || !strcasecmp(var, "inbound-bypass-media")) && switch_true(val)) {
                                                switch_set_flag(profile, TFLAG_INB_NOMEDIA);
                                        } else if (!strcasecmp(var, "inbound-late-negotiation") && switch_true(val)) {
@@ -1584,9 +1592,12 @@ switch_status_t config_sofia(int reload, char *profile_name)
                                        } else if (!strcasecmp(var, "disable-register") && switch_true(val)) {
                                                profile->mflags &= ~MFLAG_REGISTER;
                                        } else if (!strcasecmp(var, "manage-presence")) {
-                                               if (switch_true(val)) {
-                                                       profile->pflags |= PFLAG_PRESENCE;
-                                               }
+                                               if (!strcasecmp(val, "passive")) {
+                                                       profile->pres_type = PRES_TYPE_PASSIVE;
+                                               
+                                               } else if (switch_true(val)) {
+                                                       profile->pres_type = PRES_TYPE_FULL;
+                                               } 
                                        } else if (!strcasecmp(var, "unregister-on-options-fail")) {
                                                if (switch_true(val)) {
                                                        profile->pflags |= PFLAG_UNREG_OPTIONS_FAIL;
@@ -2086,17 +2097,22 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status
                                        contact_host = switch_str_nil(contact->url_host);
                                }
 
-                               if (profile->pflags & PFLAG_PRESENCE) {
-                                       sql = switch_mprintf("insert into sip_dialogs values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
+                               if (profile->pres_type) {
+                                       sql = switch_mprintf("insert into sip_dialogs "
+                                                                                "(call_id,uuid,sip_to_user,sip_to_host,sip_from_user,sip_from_host,contact_user,"
+                                                                                "contact_host,state,direction,user_agent,profile_name,hostname) "
+                                                                                "values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
                                                                                 call_id,
                                                                                 switch_core_session_get_uuid(session),
-                                                                                to_user, to_host, from_user, from_host, contact_user, contact_host, astate, "outbound", user_agent);
+                                                                                to_user, to_host, from_user, from_host, contact_user, 
+                                                                                contact_host, astate, "outbound", user_agent,
+                                                                                profile->name, mod_sofia_globals.hostname);
 
                                        switch_assert(sql);
 
                                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                                }
-                       } else if (status == 200 && (profile->pflags & PFLAG_PRESENCE)) {
+                       } else if (status == 200 && (profile->pres_type)) {
                                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);
@@ -3636,7 +3652,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
                su_free(profile->home, tmp);
        }
 
-       if (sofia_test_pflag(profile, PFLAG_PRESENCE)) {
+       if (profile->pres_type) {
                const char *user = switch_str_nil(sip->sip_from->a_url->url_user);
                const char *host = switch_str_nil(sip->sip_from->a_url->url_host);
 
@@ -3709,7 +3725,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
        sofia_private->is_call++;
        tech_pvt->sofia_private = sofia_private;
 
-       if ((profile->pflags & PFLAG_PRESENCE)) {
+       if ((profile->pres_type)) {
                sofia_presence_set_chat_hash(tech_pvt, sip);
        }
        switch_copy_string(tech_pvt->sofia_private->uuid, switch_core_session_get_uuid(session), sizeof(tech_pvt->sofia_private->uuid));
@@ -3756,12 +3772,17 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
                }
 
 
-               if (profile->pflags & PFLAG_PRESENCE) {
+               if (profile->pres_type) {
 
-                       sql = switch_mprintf("insert into sip_dialogs values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
+                       sql = switch_mprintf("insert into sip_dialogs "
+                                                                "(call_id,uuid,sip_to_user,sip_to_host,sip_from_user,sip_from_host,contact_user,"
+                                                                "contact_host,state,direction,user_agent,profile_name,hostname) "
+                                                                "values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
                                                                 call_id,
                                                                 tech_pvt->sofia_private->uuid,
-                                                                to_user, to_host, dialog_from_user, dialog_from_host, contact_user, contact_host, "confirmed", "inbound", user_agent);
+                                                                to_user, to_host, dialog_from_user, dialog_from_host, 
+                                                                contact_user, contact_host, "confirmed", "inbound", user_agent,
+                                                                profile->name, mod_sofia_globals.hostname);
 
                        switch_assert(sql);
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
index d1e104dd1b5866d4f7eeecb86f31009680d7cf02..140ac6044e3a9360a5f38391677cb844ec8338b8 100644 (file)
@@ -1231,7 +1231,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session)
 
                tech_pvt->chat_from = tech_pvt->from_str;
                tech_pvt->chat_to = tech_pvt->dest;
-               if (tech_pvt->profile->pflags & PFLAG_PRESENCE) {
+               if (tech_pvt->profile->pres_type) {
                        tech_pvt->hash_key = switch_core_session_strdup(tech_pvt->session, hash_key);
                        switch_mutex_lock(tech_pvt->profile->flag_mutex);
                        switch_core_hash_insert(tech_pvt->profile->chat_hash, tech_pvt->hash_key, tech_pvt);
@@ -2715,18 +2715,24 @@ void sofia_glue_del_profile(sofia_profile_t *profile)
 
 int sofia_glue_init_sql(sofia_profile_t *profile)
 {
+       char *test_sql = NULL;
+
        char reg_sql[] =
                "CREATE TABLE sip_registrations (\n"
                "   call_id         VARCHAR(255),\n"
                "   sip_user        VARCHAR(255),\n"
                "   sip_host        VARCHAR(255),\n"
+               "   presence_hosts  VARCHAR(255),\n"
                "   contact         VARCHAR(1024),\n"
                "   status          VARCHAR(255),\n"
                "   rpid            VARCHAR(255),\n" 
                "   expires         INTEGER,\n" 
                "   user_agent      VARCHAR(255),\n" 
-               "   server_user        VARCHAR(255),\n"
-               "   server_host        VARCHAR(255)\n" ");\n";
+               "   server_user     VARCHAR(255),\n"
+               "   server_host     VARCHAR(255),\n" 
+               "   profile_name    VARCHAR(255),\n" 
+               "   hostname        VARCHAR(255)\n" 
+               ");\n";
 
        char dialog_sql[] =
                "CREATE TABLE sip_dialogs (\n"
@@ -2740,7 +2746,9 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "   contact_host    VARCHAR(255),\n"
                "   state           VARCHAR(255),\n" 
                "   direction       VARCHAR(255),\n" 
-               "   user_agent      VARCHAR(255)\n" 
+               "   user_agent      VARCHAR(255),\n" 
+               "   profile_name    VARCHAR(255),\n"
+        "   hostname        VARCHAR(255)\n"
                ");\n";
 
        char sub_sql[] =
@@ -2750,6 +2758,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "   sip_host        VARCHAR(255),\n"
                "   sub_to_user     VARCHAR(255),\n"
                "   sub_to_host     VARCHAR(255),\n"
+               "   presence_hosts  VARCHAR(255),\n"
                "   event           VARCHAR(255),\n"
                "   contact         VARCHAR(1024),\n"
                "   call_id         VARCHAR(255),\n"
@@ -2757,13 +2766,18 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "   full_via        VARCHAR(255),\n"
                "   expires         INTEGER,\n" 
                "   user_agent      VARCHAR(255),\n" 
-               "   accept          VARCHAR(255)\n" 
+               "   accept          VARCHAR(255),\n"
+               "   profile_name    VARCHAR(255),\n"
+               "   hostname        VARCHAR(255)\n"
                ");\n";
 
        char auth_sql[] = 
                "CREATE TABLE sip_authentication (\n" 
                "   nonce           VARCHAR(255),\n" 
-               "   expires         INTEGER" ");\n";
+               "   expires         INTEGER," 
+               "   profile_name    VARCHAR(255),\n"
+               "   hostname        VARCHAR(255)\n"
+               ");\n";
 
        if (profile->odbc_dsn) {
 #ifdef SWITCH_HAVE_ODBC
@@ -2776,27 +2790,37 @@ 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);
-
-               if (switch_odbc_handle_exec(profile->master_odbc, "select sip_user,user_agent,server_host from sip_registrations", NULL) != SWITCH_ODBC_SUCCESS) {
+               
+               if (switch_odbc_handle_exec(profile->master_odbc, "select hostname 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, "delete from sip_subscriptions where sip_user != '' or accept != ''", NULL) !=
-                       SWITCH_ODBC_SUCCESS) {
+
+               test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q'", mod_sofia_globals.hostname);
+
+               if (switch_odbc_handle_exec(profile->master_odbc, test_sql, 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, "delete from sip_dialogs", NULL) != SWITCH_ODBC_SUCCESS) {
+               free(test_sql);
+               test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q'", mod_sofia_globals.hostname);
+
+               if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_dialogs", NULL);
                        switch_odbc_handle_exec(profile->master_odbc, dialog_sql, NULL);
                }
 
-               if (switch_odbc_handle_exec(profile->master_odbc, "select nonce from sip_authentication", NULL) != SWITCH_ODBC_SUCCESS) {
+               free(test_sql);
+               test_sql = switch_mprintf("delete from sip_authentication where hostname='%q'", mod_sofia_globals.hostname);
+
+               if (switch_odbc_handle_exec(profile->master_odbc, test_sql, 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);
                }
+               free(test_sql);
+
 #else
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
 #endif
@@ -2805,12 +2829,18 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                        return 0;
                }
 
-               switch_core_db_test_reactive(profile->master_db, "select sip_user,user_agent,server_host from sip_registrations", "DROP TABLE sip_registrations", reg_sql);
-               switch_core_db_test_reactive(profile->master_db, "delete from sip_subscriptions where sip_user != '' or accept != ''",
-                                                                        "DROP TABLE sip_subscriptions", sub_sql);
-               switch_core_db_test_reactive(profile->master_db, "delete from sip_dialogs", "DROP TABLE sip_dialogs", dialog_sql);
-               switch_core_db_test_reactive(profile->master_db, "select * from sip_authentication", "DROP TABLE sip_authentication", auth_sql);
-
+               switch_core_db_test_reactive(profile->master_db, "select hostname from sip_registrations", "DROP TABLE sip_registrations", reg_sql);
+               
+               test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q'", mod_sofia_globals.hostname);
+               switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_subscriptions", sub_sql);
+               free(test_sql);
+               
+               test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q'", mod_sofia_globals.hostname);
+               switch_core_db_test_reactive(profile->master_db, "delete from sip_dialogs", test_sql, dialog_sql);
+               free(test_sql);
+               test_sql = switch_mprintf("delete from sip_authentication where hostname='%q'", mod_sofia_globals.hostname);
+               switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_authentication", auth_sql);
+               free(test_sql);
        }
 
 #ifdef SWITCH_HAVE_ODBC
index bf7bc8d10d429d15481fb640372c6d8b537406ef..f773768a64428c28eb5502bd7e226394b8f45314 100644 (file)
@@ -143,13 +143,16 @@ void sofia_presence_cancel(void)
 
        if (!mod_sofia_globals.profile_hash)
                return;
-
-       if ((sql = switch_mprintf("select *,-1,'unavailable','unavailable' from sip_subscriptions where event='presence'"))) {
+       
+       if ((sql = switch_mprintf(
+                                                         "select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                         "full_via,expires,user_agent,accept,profile_name"
+                                                         ",-1,'unavailable','unavailable' from sip_subscriptions where event='presence'"))) {
                switch_mutex_lock(mod_sofia_globals.hash_mutex);
                for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
                        switch_hash_this(hi, NULL, NULL, &val);
                        profile = (sofia_profile_t *) val;
-                       if (!(profile->pflags & PFLAG_PRESENCE)) {
+                       if (profile->pres_type != PRES_TYPE_FULL) {
                                continue;
                        }
                        helper.profile = profile;
@@ -272,19 +275,20 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
        }
 
        stream.write_function(&stream, "\r\n");
-
-       sql = switch_mprintf("select *,'%q' from sip_subscriptions where event='message-summary' and sub_to_user='%q' and sub_to_host='%q'",
-                                                stream.data, user, host);
-
+       
+       sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                "full_via,expires,user_agent,accept,profile_name"
+                                                ",'%q','%q' from sip_subscriptions where event='message-summary' "
+                                                "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
+                                                stream.data, host, user, host, host);
 
        switch_assert(sql != NULL);
        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_mwi_callback, &h);
-
+       
        switch_safe_free(sql);
 
-
-
-       sql = switch_mprintf("select sip_user,sip_host,contact,'%q' from sip_registrations where sip_user='%q' and sip_host='%q'", stream.data, user, host);
+       sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q' from sip_registrations where sip_user='%q' and sip_host='%q'", 
+                                                stream.data, user, host);
 
 
 
@@ -357,9 +361,14 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                        return;
 
                if (from) {
-                       sql = switch_mprintf("select *,1,'%q','%q' from sip_subscriptions where event='presence' and full_from like '%%%q%%'", status, rpid, from);
+                       sql = switch_mprintf(
+                                                                "select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                                "full_via,expires,user_agent,accept,profile_name"
+                                                                ",1,'%q','%q' from sip_subscriptions where event='presence' and full_from like '%%%q%%'", status, rpid, from);
                } else {
-                       sql = switch_mprintf("select *,1,'%q','%q' from sip_subscriptions where event='presence'", status, rpid);
+                       sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                                "full_via,expires,user_agent,accept,profile_name"
+                                                                ",1,'%q','%q' from sip_subscriptions where event='presence'", status, rpid);
                }
 
                switch_assert(sql != NULL);
@@ -367,7 +376,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
                        switch_hash_this(hi, NULL, NULL, &val);
                        profile = (sofia_profile_t *) val;
-                       if (!(profile->pflags & PFLAG_PRESENCE)) {
+                       if (profile->pres_type != PRES_TYPE_FULL) {
                                continue;
                        }
                        helper.profile = profile;
@@ -445,16 +454,18 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                }
                return;
        case SWITCH_EVENT_PRESENCE_IN:
-               sql =
-                       switch_mprintf
-                       ("select *,1,'%q','%q' from sip_subscriptions where (event='%q' or event='%q') and sub_to_user='%q' and sub_to_host='%q'",
-                        status, rpid, event_type, alt_event_type, euser, host);
+               sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                        "full_via,expires,user_agent,accept,profile_name"
+                                                        ",1,'%q','%q','%q' from sip_subscriptions where (event='%q' or event='%q') and sub_to_user='%q' "
+                                                        "and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
+                                                        status, rpid, host, event_type, alt_event_type, euser, host, host);
                break;
        case SWITCH_EVENT_PRESENCE_OUT:
-               sql =
-                       switch_mprintf
-                       ("select *,0,'%q','%q' from sip_subscriptions where (event='%q' or event='%q') and sub_to_user='%q' and sub_to_host='%q'",
-                        status, rpid, event_type, alt_event_type, euser, host);
+               sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                        "full_via,expires,user_agent,accept,profile_name"
+                                                        ",0,'%q','%q','%q' from sip_subscriptions where (event='%q' or event='%q') "
+                                                        "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
+                                                        status, rpid, host, event_type, alt_event_type, euser, host, host);
                break;
        default:
                break;
@@ -467,7 +478,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
        for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
                switch_hash_this(hi, NULL, NULL, &val);
                profile = (sofia_profile_t *) val;
-               if (!(profile->pflags & PFLAG_PRESENCE)) {
+               if (profile->pres_type != PRES_TYPE_FULL) {
                        continue;
                }
 
@@ -818,23 +829,22 @@ static char *gen_pidf(char *user_agent, char *id, char *url, char *open, char *r
 static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char **columnNames)
 {
        struct presence_helper *helper = (struct presence_helper *) pArg;
-       sofia_profile_t *profile = helper->profile;
        char *pl = NULL;
        char *clean_id = NULL, *id = NULL;
-       uint32_t in = atoi(argv[13]);
-       char *status = argv[14];
-       char *rpid = argv[15];
+       uint32_t in = atoi(argv[14]);
+       char *status = argv[15];
+       char *rpid = argv[16];
 
        char *proto = argv[0];
        char *user = argv[1];
        char *host = argv[2];
        char *sub_to_user = argv[3];
-       char *sub_to_host = argv[4];
+       char *sub_to_host = argv[17];
        char *event = argv[5];
        char *call_id = argv[7];
        char *expires = argv[10];
        char *user_agent = argv[11];
-       //char *accept = argv[12];
+       char *profile_name = argv[13];
        nua_handle_t *nh;
        char *to = NULL;
        char *open;
@@ -844,6 +854,15 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
        time_t exptime = switch_timestamp(NULL) + 3600;
        char exp[80] = "";
        int is_dialog = 0;
+       sofia_profile_t *ext_profile = NULL, *profile = helper->profile;
+
+       
+       if (profile_name && strcasecmp(profile_name, helper->profile->name)) {
+        if ((ext_profile = sofia_glue_find_profile(profile_name))) {
+            profile = ext_profile;
+        }
+    }
+       
 
        if (expires) {
                long tmp = atol(expires);
@@ -854,7 +873,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 
        if (!(nh = nua_handle_by_call_id(profile->nua, call_id))) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Cannot find handle for %s\n", call_id);
-               return 0;
+               goto end;
        }
 
        if (!rpid) {
@@ -1087,6 +1106,10 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
        nua_notify(nh, SIPTAG_SUBSCRIPTION_STATE_STR(exp), SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR(ct), SIPTAG_PAYLOAD_STR(pl), TAG_END());
        
   end:
+       
+       if (ext_profile) {
+               sofia_glue_release_profile(ext_profile);
+       }
 
        switch_safe_free(id);
        switch_safe_free(clean_id);
@@ -1102,14 +1125,15 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
        //char *user = argv[1];
        //char *host = argv[2];
        char *sub_to_user = argv[3];
-       char *sub_to_host = argv[4];
+       char *sub_to_host = argv[15];
        char *event = argv[5];
        //char *contact = argv[6];
        char *call_id = argv[7];
        //char *full_from = argv[8];
        //char *full_via = argv[9];
        char *expires = argv[10];
-       char *body = argv[13];
+       char *profile_name = argv[13];
+       char *body = argv[14];
        char *exp;
        //sofia_profile_t *profile = NULL;
        char *id = NULL;
@@ -1117,11 +1141,19 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
        int expire_sec = atoi(expires);
        //int *total = (int *) pArg;
        struct mwi_helper *h = (struct mwi_helper *) pArg;
+       sofia_profile_t *ext_profile = NULL, *profile = h->profile;
+       
 
+       if (profile_name && strcasecmp(profile_name, h->profile->name)) {
+               if ((ext_profile = sofia_glue_find_profile(profile_name))) {
+                       profile = ext_profile;
+               }
+       }
 
+       
        if (!(nh = nua_handle_by_call_id(h->profile->nua, call_id))) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find handle for %s\n", call_id);
-               return 0;
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Cannot find handle for %s\n", call_id);
+               goto end;
        }
 
        id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
@@ -1139,6 +1171,13 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
        switch_safe_free(exp);
 
        h->total++;
+
+ end:
+
+       if (ext_profile) {
+               sofia_glue_release_profile(ext_profile);
+       }
+
        return 0;
 }
 
@@ -1151,17 +1190,25 @@ static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char
        char *sub_to_host = argv[1];
        char *event = "message-summary";
        char *contact, *o_contact = argv[2];
-       char *body = argv[3];
+       char *profile_name = argv[3];
+       char *body = argv[4];
        char *id = NULL;
        nua_handle_t *nh;
        struct mwi_helper *h = (struct mwi_helper *) pArg;
+       sofia_profile_t *ext_profile = NULL, *profile = h->profile;
+       
 
+       if (profile_name && strcasecmp(profile_name, h->profile->name)) {
+               if ((ext_profile = sofia_glue_find_profile(profile_name))) {
+                       profile = ext_profile;
+               }
+       }
 
        id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
 
        contact = sofia_glue_get_url_from_contact(o_contact, 1);
 
-       nh = nua_handle(h->profile->nua, NULL, NUTAG_URL(contact), SIPTAG_FROM_STR(id), SIPTAG_TO_STR(id), SIPTAG_CONTACT_STR(h->profile->url), TAG_END());
+       nh = nua_handle(profile->nua, NULL, NUTAG_URL(contact), SIPTAG_FROM_STR(id), SIPTAG_TO_STR(id), SIPTAG_CONTACT_STR(h->profile->url), TAG_END());
 
        nua_notify(nh,
                           NUTAG_NEWSUB(1),
@@ -1171,6 +1218,10 @@ static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char
        switch_safe_free(contact);
        switch_safe_free(id);
 
+       if (ext_profile) {
+               sofia_glue_release_profile(ext_profile);
+       }
+
        return 0;
 }
 
@@ -1383,8 +1434,8 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                        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);
+                                                                "proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q' and hostname='%q'",
+                                                                proto, from_user, from_host, to_user, to_host, event, mod_sofia_globals.hostname);
                }
 
 
@@ -1402,9 +1453,14 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                                switch_snprintf(accept + strlen(accept), sizeof(accept) - strlen(accept), "%s%s ", ap->ac_type, ap->ac_next ? "," : "");
                                ap = ap->ac_next;
                        }
-                       sql = switch_mprintf("insert into sip_subscriptions values ('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld,'%q','%q')",
-                                                                proto, from_user, from_host, to_user, to_host, event, contact_str, call_id, full_from, full_via, exp, full_agent, accept);
-
+                       sql = switch_mprintf("insert into sip_subscriptions "
+                                                                "(proto,sip_user,sip_host,sub_to_user,sub_to_host,presence_hosts,event,contact,call_id,full_from,"
+                                                                "full_via,expires,user_agent,accept,profile_name,hostname) "
+                                                                "values ('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld,'%q','%q','%q','%q')",
+                                                                proto, from_user, from_host, to_user, to_host, profile->presence_hosts ? profile->presence_hosts : to_host, 
+                                                                event, contact_str, call_id, full_from, full_via, exp, full_agent, accept, profile->name,mod_sofia_globals.hostname);
+                                                                
+                       
                        switch_assert(sql != NULL);
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 
@@ -1450,7 +1506,10 @@ void sofia_presence_handle_sip_i_subscribe(int status,
 
                switch_safe_free(sstr);
 
-               if ((sql = switch_mprintf("select * from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, to_host))) {
+               if ((sql = switch_mprintf(
+                                                                 "select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
+                                                                 "full_via,expires,user_agent,accept,profile_name"
+                                                                 " from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, to_host))) {
                        sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_reg_callback, profile);
 
                        switch_safe_free(sql);
index 887e544ba0570ca624010fcef0646f62f29b9e0a..be9a5aac5cbb8856cd14a74cf4b720f54ab838a2 100644 (file)
@@ -286,7 +286,7 @@ int sofia_reg_del_callback(void *pArg, int argc, char **argv, char **columnNames
        switch_event_t *s_event;
        sofia_profile_t *profile = (sofia_profile_t *) pArg;
        
-       if (argc > 10 && atoi(argv[10]) == 1) {
+       if (argc > 11 && atoi(argv[11]) == 1) {
                sofia_reg_send_reboot(profile, argv[1], argv[2], argv[3], argv[7]);
        }
 
@@ -322,15 +322,16 @@ void sofia_reg_expire_call_id(sofia_profile_t *profile, const char *call_id, int
                host = "none";
        }
 
-       switch_snprintf(sql, sizeof(sql), "select *,%d from sip_registrations where call_id='%s' or (sip_user='%s' and sip_host='%s')", 
-                                       reboot, call_id, user, host);
+       switch_snprintf(sql, sizeof(sql), "select call_id,sip_user,sip_host,contact,status,rpid,expires,user_agent,server_user,server_host,profile_name"
+                                       ",%d from sip_registrations where call_id='%s' or (sip_user='%s' and sip_host='%s') and hostname='%q'", 
+                                       reboot, call_id, user, host, mod_sofia_globals.hostname);
        
        switch_mutex_lock(profile->ireg_mutex);
        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_del_callback, profile);
        switch_mutex_unlock(profile->ireg_mutex);
 
-       switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where call_id='%s' or (sip_user='%s' and sip_host='%s')", 
-                                       call_id, user, host);
+       switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where call_id='%s' or (sip_user='%s' and sip_host='%s') and hostname='%q'", 
+                                       call_id, user, host, mod_sofia_globals.hostname);
        sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE);
 
        switch_safe_free(user);
@@ -361,24 +362,28 @@ void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot)
        switch_mutex_lock(profile->ireg_mutex);
 
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "select *,%d from sip_registrations where expires > 0 and expires <= %ld", reboot, (long) now);
+               switch_snprintf(sql, sizeof(sql), "select call_id,sip_user,sip_host,contact,status,rpid,expires,user_agent,server_user,server_host,profile_name"
+                                               ",%d from sip_registrations where expires > 0 and expires <= %ld", reboot, (long) now);
        } else {
-               switch_snprintf(sql, sizeof(sql), "select *,%d from sip_registrations where expires > 0", reboot);
+               switch_snprintf(sql, sizeof(sql), "select call_id,sip_user,sip_host,contact,status,rpid,expires,user_agent,server_user,server_host,profile_name"
+                                               ",%d from sip_registrations where expires > 0", reboot);
        }
 
        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_del_callback, profile);
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and expires <= %ld", (long) now);
+               switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and expires <= %ld and hostname='%q'", 
+                                               (long) now, mod_sofia_globals.hostname);
        } else {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0");
+               switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and hostname='%q'", mod_sofia_globals.hostname);
        }
 
        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
 
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld", (long) now);
+               switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld and hostname='%q'", 
+                                               (long) now, mod_sofia_globals.hostname);
        } else {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0");
+               switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and hostname='%q'", mod_sofia_globals.hostname);
        }
 
        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
@@ -386,24 +391,27 @@ void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot)
 
 
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "select call_id from sip_subscriptions where expires > 0 and expires <= %ld", (long) now);
+               switch_snprintf(sql, sizeof(sql), "select call_id from sip_subscriptions where expires > 0 and expires <= %ld and hostname='%q'", 
+                                               (long) now, mod_sofia_globals.hostname);
        } else {
-               switch_snprintf(sql, sizeof(sql), "select call_id from sip_subscriptions where expires > 0");
+               switch_snprintf(sql, sizeof(sql), "select call_id from sip_subscriptions where expires > 0 and hostname='%q'", mod_sofia_globals.hostname);
        }
 
        sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_sub_del_callback, profile);
 
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and expires <= %ld", (long) now);
+               switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and expires <= %ld and hostname='%q'", 
+                                               (long) now, mod_sofia_globals.hostname);
        } else {
-               switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0");
+               switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and hostname='%q'", mod_sofia_globals.hostname);
        }
 
        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
 
 
        if (now) {
-               switch_snprintf(sql, sizeof(sql), "select * from sip_registrations where status like '%%AUTO-NAT%%' or status like '%%UDP-NAT%%'");
+               switch_snprintf(sql, sizeof(sql), "select call_id,sip_user,sip_host,contact,status,rpid,expires,user_agent,server_user,server_host,profile_name"
+                                               " from sip_registrations where status like '%%AUTO-NAT%%' or status like '%%UDP-NAT%%'");
                sofia_glue_execute_sql_callback(profile, SWITCH_TRUE, NULL, sql, sofia_reg_nat_callback, profile);
        }
 
@@ -425,7 +433,8 @@ char *sofia_reg_find_reg_url(sofia_profile_t *profile, const char *user, const c
        cbt.len = len;
 
        if (host) {
-               switch_snprintf(sql, sizeof(sql), "select contact from sip_registrations where sip_user='%s' and sip_host='%s'", user, host);
+               switch_snprintf(sql, sizeof(sql), "select contact from sip_registrations where sip_user='%s' and (sip_host='%s' or presence_hosts like '%%%q%%')"
+                                               , user, host, host);
        } else {
                switch_snprintf(sql, sizeof(sql), "select contact from sip_registrations where sip_user='%s'", user);
        }
@@ -451,7 +460,8 @@ void sofia_reg_auth_challenge(nua_t *nua, sofia_profile_t *profile, nua_handle_t
        switch_uuid_get(&uuid);
        switch_uuid_format(uuid_str, &uuid);
 
-       sql = switch_mprintf("insert into sip_authentication (nonce, expires) values('%q', %ld)", uuid_str, switch_timestamp(NULL) + profile->nonce_ttl);
+       sql = switch_mprintf("insert into sip_authentication (nonce,expires,profile_name,hostname) "
+                                                "values('%q', %ld, '%q', '%q')", uuid_str, switch_timestamp(NULL) + profile->nonce_ttl, profile->name, mod_sofia_globals.hostname);
        switch_assert(sql != NULL);
        sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
        switch_safe_free(sql);
@@ -754,14 +764,20 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                if (multi_reg) {
                        sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
                } else {
-                       sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, reg_host);
+                       sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q' and hostname='%q'", 
+                                                                to_user, reg_host, mod_sofia_globals.hostname);
                }
                switch_mutex_lock(profile->ireg_mutex);
                sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                
                switch_find_local_ip(guess_ip4, sizeof(guess_ip4), AF_INET);
-               sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q')", call_id,
-                                                        to_user, reg_host, contact_str, reg_desc, rpid, (long) switch_timestamp(NULL) + (long) exptime * 2, agent, from_user,  guess_ip4);
+               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) "
+                                                        "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%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_timestamp(NULL) + (long) exptime * 2, 
+                                                        agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname);
+                                                        
                if (sql) {
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                }
@@ -771,6 +787,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "profile-name", profile->name);
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "from-user", to_user);
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "from-host", reg_host);
+                       switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "presence-hosts", profile->presence_hosts ? profile->presence_hosts : reg_host);
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "contact", contact_str);
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "call-id", call_id);
                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "rpid", rpid);
@@ -817,11 +834,13 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                        }
                        switch_safe_free(icontact);
                } else {
-                       if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, reg_host))) {
+                       if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q' and hostname='%q'", to_user, reg_host, 
+                                                                         mod_sofia_globals.hostname))) {
                                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, reg_host))) {
+                       if ((sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q' and hostname='%q'", to_user, reg_host,
+                                                                         mod_sofia_globals.hostname))) {
                                sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                        }
                }