From 43a3b7115324fa3181ae7581cc11c843a96a11e2 Mon Sep 17 00:00:00 2001
From: Michael Jerris <mike@jerris.com>
Date: Wed, 16 Jul 2008 19:42:40 +0000
Subject: [PATCH] add server registration fiels to sip_registration database
 (MODENDP-118)

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9060 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_sofia/sofia.c      |  6 ++++--
 src/mod/endpoints/mod_sofia/sofia_glue.c |  8 +++++---
 src/mod/endpoints/mod_sofia/sofia_reg.c  | 16 ++++++++++++++--
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index 801474707d..36d3534a6e 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -378,6 +378,8 @@ void event_handler(switch_event_t *event)
 		char *user_agent = switch_event_get_header(event, "user-agent");
 		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 *to_host = switch_event_get_header(event, "orig-to-host");
 		sofia_profile_t *profile = NULL;
 
 		if (exp_str) {
@@ -401,8 +403,8 @@ void event_handler(switch_event_t *event)
 		switch_mutex_lock(profile->ireg_mutex);
 		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);
+		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, to_host);
 
 		if (sql) {
 			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c
index e1c895f6a7..e682575b3f 100644
--- a/src/mod/endpoints/mod_sofia/sofia_glue.c
+++ b/src/mod/endpoints/mod_sofia/sofia_glue.c
@@ -2574,7 +2574,9 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
 		"   status          VARCHAR(255),\n"
 		"   rpid            VARCHAR(255),\n" 
 		"   expires         INTEGER,\n" 
-		"   user_agent      VARCHAR(255)\n" ");\n";
+		"   user_agent      VARCHAR(255),\n" 
+		"   server_user        VARCHAR(255),\n"
+		"   server_host        VARCHAR(255)\n" ");\n";
 
 	char dialog_sql[] =
 		"CREATE TABLE sip_dialogs (\n"
@@ -2625,7 +2627,7 @@ 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 from sip_registrations", NULL) != SWITCH_ODBC_SUCCESS) {
+		if (switch_odbc_handle_exec(profile->master_odbc, "select sip_user,user_agent,server_host 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);
 		}
@@ -2653,7 +2655,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
 			return 0;
 		}
 
-		switch_core_db_test_reactive(profile->master_db, "select sip_user,user_agent from sip_registrations", "DROP TABLE sip_registrations", reg_sql);
+		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);
diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c
index b2c493cd08..f4cafe55c8 100644
--- a/src/mod/endpoints/mod_sofia/sofia_reg.c
+++ b/src/mod/endpoints/mod_sofia/sofia_reg.c
@@ -404,6 +404,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 								  uint32_t keylen, switch_event_t **v_event, const char *is_nat)
 {
 	sip_to_t const *to = NULL;
+	sip_from_t const *from = NULL;
 	sip_expires_t const *expires = NULL;
 	sip_authorization_t const *authorization = NULL;
 	sip_contact_t const *contact = NULL;
@@ -411,6 +412,8 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 	switch_event_t *s_event;
 	const char *to_user = NULL;
 	const char *to_host = NULL;
+	const char *from_user = NULL;
+	const char *from_host = NULL;
 	char contact_str[1024] = "";
 	int nat_hack = 0;
 	uint8_t multi_reg = 0, avoid_multi_reg = 0;
@@ -456,6 +459,13 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 		return 1;
 	}
 
+	from = sip->sip_from;
+
+	if (from) {
+		from_user = from->a_url->url_user;
+		from_host = from->a_url->url_host;
+	}
+
 	if (contact->m_url) {
 		const char *port = contact->m_url->url_port;
 		char new_port[25] = "";
@@ -675,8 +685,8 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 		switch_mutex_lock(profile->ireg_mutex);
 		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, rpid, (long) switch_timestamp(NULL) + (long) exptime * 2, agent);
+		sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q')", call_id,
+							 to_user, to_host, contact_str, reg_desc, rpid, (long) switch_timestamp(NULL) + (long) exptime * 2, agent, from_user, from_host);
 		if (sql) {
 			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		}
@@ -690,6 +700,8 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 			switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "call-id", "%s", call_id);
 			switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "rpid", "%s", rpid);
 			switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "expires", "%ld", (long) exptime);
+			switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to-user", "%s", from_user);
+			switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to-host", "%s", from_host);
 			switch_event_fire(&s_event);
 		}