Revision
14353
Author
mikej
Date
2009-07-24 14:34:32 -0500 (Fri, 24 Jul 2009)

Log Message

mod_sofia: Optionally rewrite fs_path element of sip contact string on multicasted registrations (MODENDP-231)

Modified Paths

Diff

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (14352 => 14353)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2009-07-24 18:20:37 UTC (rev 14352)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2009-07-24 19:34:32 UTC (rev 14353)
@@ -274,6 +274,7 @@
 	int auto_restart;
 	int auto_nat;
 	int tracelevel;
+	int rewrite_multicasted_fs_path;
 };
 extern struct mod_sofia_globals mod_sofia_globals;
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (14352 => 14353)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-07-24 18:20:37 UTC (rev 14352)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-07-24 19:34:32 UTC (rev 14353)
@@ -623,6 +623,7 @@
 	if ((subclass = switch_event_get_header(event, "orig-event-subclass")) && !strcasecmp(subclass, MY_EVENT_REGISTER)) {
 		char *from_user = switch_event_get_header(event, "orig-from-user");
 		char *from_host = switch_event_get_header(event, "orig-from-host");
+		char *to_host = switch_event_get_header(event, "orig-to-host");
 		char *contact_str = switch_event_get_header(event, "orig-contact");
 		char *exp_str = switch_event_get_header(event, "orig-expires");
 		char *rpid = switch_event_get_header(event, "orig-rpid");
@@ -636,8 +637,8 @@
 		char *network_port = switch_event_get_header(event, "orig-network-port");
 		char *username = switch_event_get_header(event, "orig-username");
 		char *realm = switch_event_get_header(event, "orig-realm");
+		char *fixed_contact_str = NULL;
 
-		
 		sofia_profile_t *profile = NULL;
 
 		char guess_ip4[256];
@@ -660,6 +661,29 @@
 			sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", from_user, from_host);
 		}
 
+		if (mod_sofia_globals.rewrite_multicasted_fs_path && contact_str) {
+			const char *needle = ";fs_path=";
+			char *sptr, *eptr = NULL;
+			/* allocate enough room for worst-case scenario */
+			size_t len = strlen(contact_str) + strlen(to_host) + 14;
+			fixed_contact_str = malloc(len);
+			switch_assert(fixed_contact_str);
+			switch_copy_string(fixed_contact_str, contact_str, len);
+	
+			if ((sptr = strstr(fixed_contact_str, needle))) {
+				if (!(eptr = strchr(fixed_contact_str, ';'))) {
+					eptr = strrchr(fixed_contact_str, '>');
+				}
+			} else {
+				sptr = strchr(fixed_contact_str, '\0') - 1;
+			}
+
+			switch_snprintf(sptr, len - (sptr - fixed_contact_str), ";fs_path=sip:%s%s%s", to_host, eptr ? ";" : "", eptr ? eptr : ">");
+
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Rewrote contact string from '%s' to '%s'\n", contact_str, fixed_contact_str);
+			contact_str = fixed_contact_str;
+		}
+
 		switch_mutex_lock(profile->ireg_mutex);
 		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		
@@ -677,10 +701,11 @@
 		}
 		switch_mutex_unlock(profile->ireg_mutex);
 
-
 		if (profile) {
 			sofia_glue_release_profile(profile);
 		}
+
+		switch_safe_free(fixed_contact_str);
 	}
 }
 
@@ -2035,6 +2060,7 @@
 	}
 
 	mod_sofia_globals.auto_restart = SWITCH_TRUE;
+	mod_sofia_globals.rewrite_multicasted_fs_path = SWITCH_FALSE;
 
 	if ((settings = switch_xml_child(cfg, "global_settings"))) {
 		for (param = switch_xml_child(settings, "param"); param; param = param->next) {
@@ -2046,6 +2072,8 @@
 				mod_sofia_globals.debug_presence = atoi(val);
 			} else if (!strcasecmp(var, "auto-restart")) {
 				mod_sofia_globals.auto_restart = switch_true(val);
+			} else if (!strcasecmp(var, "rewrite-multicasted-fs-path")) {
+				mod_sofia_globals.rewrite_multicasted_fs_path = switch_true(val);
 			}
 		}
 	}