[Freeswitch-svn] [commit] r7718 - in freeswitch/trunk/src: . include

Freeswitch SVN anthm at freeswitch.org
Thu Feb 21 16:38:49 EST 2008


Author: anthm
Date: Thu Feb 21 16:38:49 2008
New Revision: 7718

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/include/switch_types.h
   freeswitch/trunk/src/switch_channel.c
   freeswitch/trunk/src/switch_core_io.c
   freeswitch/trunk/src/switch_core_memory.c
   freeswitch/trunk/src/switch_core_session.c
   freeswitch/trunk/src/switch_ivr_async.c
   freeswitch/trunk/src/switch_rtp.c

Log:
more protection

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Thu Feb 21 16:38:49 2008
@@ -350,6 +350,8 @@
 SWITCH_DECLARE(const switch_state_handler_table_t *) switch_core_get_state_handler(_In_ int index);
 ///\}
 
+SWITCH_DECLARE(void) switch_core_memory_pool_tag(switch_memory_pool_t *pool, const char *tag);
+
 SWITCH_DECLARE(switch_status_t) switch_core_perform_new_memory_pool(_Out_ switch_memory_pool_t **pool,
 																	_In_z_ const char *file, _In_z_ const char *func, _In_ int line);
 

Modified: freeswitch/trunk/src/include/switch_types.h
==============================================================================
--- freeswitch/trunk/src/include/switch_types.h	(original)
+++ freeswitch/trunk/src/include/switch_types.h	Thu Feb 21 16:38:49 2008
@@ -93,6 +93,7 @@
 #define SWITCH_SEQ_CLEARSCR SWITCH_SEQ_ESC SWITCH_SEQ_CLEARSCR_CHAR SWITCH_SEQ_HOME
 
 #define SWITCH_DEFAULT_DTMF_DURATION 2000
+#define SWITCH_MAX_DTMF_DURATION 192000
 #define SWITCH_DEFAULT_DIR_PERMS SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE | SWITCH_FPROT_UEXECUTE | SWITCH_FPROT_GREAD | SWITCH_FPROT_GEXECUTE
 
 #ifdef WIN32

Modified: freeswitch/trunk/src/switch_channel.c
==============================================================================
--- freeswitch/trunk/src/switch_channel.c	(original)
+++ freeswitch/trunk/src/switch_channel.c	Thu Feb 21 16:38:49 2008
@@ -210,19 +210,32 @@
 {
 	switch_status_t status;
 	void *pop;
+	switch_dtmf_t new_dtmf;
+	
+	switch_assert(dtmf);
 
 	switch_mutex_lock(channel->dtmf_mutex);	
-
+	new_dtmf = *dtmf;
+	
 	if ((status = switch_core_session_recv_dtmf(channel->session, dtmf) != SWITCH_STATUS_SUCCESS)) {
 		goto done;
 	}
 
-	if (is_dtmf(dtmf->digit)) {
+	if (is_dtmf(new_dtmf.digit)) {
 		switch_dtmf_t *dt;
 		int x = 0;
 
+		if (new_dtmf.duration > SWITCH_MAX_DTMF_DURATION) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s EXECSSIVE DTMF DIGIT [%c] LEN [%d]\n", 
+							  switch_channel_get_name(channel), new_dtmf.digit, new_dtmf.duration);
+			new_dtmf.duration = SWITCH_MAX_DTMF_DURATION;
+		} else if (!new_dtmf.duration) {
+			new_dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+		
 		switch_zmalloc(dt, sizeof(*dt));
-		*dt = *dtmf;
+		*dt = new_dtmf;
+
 		while (switch_queue_trypush(channel->dtmf_queue, dt) != SWITCH_STATUS_SUCCESS) {
 			switch_queue_trypop(channel->dtmf_queue, &pop);
 			if (++x > 100) {
@@ -268,6 +281,14 @@
 			}
 		}
 
+		if (dtmf.duration > SWITCH_MAX_DTMF_DURATION) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "EXECSSIVE DTMF DIGIT LEN %c %d\n", dtmf.digit, dtmf.duration);
+			dtmf.duration = SWITCH_MAX_DTMF_DURATION;
+		} else if (!dtmf.duration) {
+			dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+
+
 		for (p = argv[i]; p && *p; p++) {
 			if (is_dtmf(*p)) {
 				dtmf.digit = *p;
@@ -295,9 +316,17 @@
 
 	if (switch_queue_trypop(channel->dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
 		dt = (switch_dtmf_t *) pop;
-		/* TODO: Shouldn't we be doing a memcpy here? */
 		*dtmf = *dt;
 		free(dt);
+
+		if (dtmf->duration > SWITCH_MAX_DTMF_DURATION) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s EXECSSIVE DTMF DIGIT [%c] LEN [%d]\n", 
+							  switch_channel_get_name(channel), dtmf->digit, dtmf->duration);
+			dtmf->duration = SWITCH_MAX_DTMF_DURATION;
+		} else if (!dtmf->duration) {
+			dtmf->duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+
 		status = SWITCH_STATUS_SUCCESS;
 	}
 	switch_mutex_unlock(channel->dtmf_mutex);

Modified: freeswitch/trunk/src/switch_core_io.c
==============================================================================
--- freeswitch/trunk/src/switch_core_io.c	(original)
+++ freeswitch/trunk/src/switch_core_io.c	Thu Feb 21 16:38:49 2008
@@ -915,9 +915,22 @@
 {
 	switch_io_event_hook_recv_dtmf_t *ptr;	
 	switch_status_t status;
+	switch_dtmf_t new_dtmf;
+	
+	switch_assert(dtmf);
+
+	new_dtmf = *dtmf;
+
+	if (new_dtmf.duration > SWITCH_MAX_DTMF_DURATION) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s EXECSSIVE DTMF DIGIT [%c] LEN [%d]\n", 
+						  switch_channel_get_name(session->channel), new_dtmf.digit, new_dtmf.duration);
+		new_dtmf.duration = SWITCH_MAX_DTMF_DURATION;
+	} else if (!new_dtmf.duration) {
+		new_dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+	}
 
 	for (ptr = session->event_hooks.recv_dtmf; ptr; ptr = ptr->next) {
-		if ((status = ptr->recv_dtmf(session, dtmf)) != SWITCH_STATUS_SUCCESS) {
+		if ((status = ptr->recv_dtmf(session, &new_dtmf)) != SWITCH_STATUS_SUCCESS) {
 			return status;
 		}
 	}
@@ -928,8 +941,21 @@
 {
 	switch_io_event_hook_send_dtmf_t *ptr;
 	switch_status_t status = SWITCH_STATUS_FALSE;
+	switch_dtmf_t new_dtmf;
 	
-	
+	switch_assert(dtmf);
+
+	new_dtmf = *dtmf;
+
+	if (new_dtmf.duration > SWITCH_MAX_DTMF_DURATION) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s EXECSSIVE DTMF DIGIT [%c] LEN [%d]\n", 
+						  switch_channel_get_name(session->channel), new_dtmf.digit, new_dtmf.duration);
+		new_dtmf.duration = SWITCH_MAX_DTMF_DURATION;
+	} else if (!new_dtmf.duration) {
+		new_dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+	}
+
+
 	for (ptr = session->event_hooks.send_dtmf; ptr; ptr = ptr->next) {
 		if ((status = ptr->send_dtmf(session, dtmf)) != SWITCH_STATUS_SUCCESS) {
 			return SWITCH_STATUS_SUCCESS;
@@ -942,7 +968,7 @@
 		} else if (dtmf->digit == 'W') {
 			switch_yield(1000000);
 		} else {
-			status = session->endpoint_interface->io_routines->send_dtmf(session, dtmf);
+			status = session->endpoint_interface->io_routines->send_dtmf(session, &new_dtmf);
 		}
 	}
 	return status;
@@ -986,6 +1012,15 @@
 			}
 		}
 
+
+		if (dtmf.duration > SWITCH_MAX_DTMF_DURATION) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s EXECSSIVE DTMF DIGIT [%c] LEN [%d]\n", 
+							  switch_channel_get_name(session->channel), dtmf.digit, dtmf.duration);
+			dtmf.duration = SWITCH_MAX_DTMF_DURATION;
+		} else if (!dtmf.duration) {
+			dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+
 		for (p = argv[i]; p && *p; p++) {
 			if (is_dtmf(*p)) {
 				dtmf.digit = *p;

Modified: freeswitch/trunk/src/switch_core_memory.c
==============================================================================
--- freeswitch/trunk/src/switch_core_memory.c	(original)
+++ freeswitch/trunk/src/switch_core_memory.c	Thu Feb 21 16:38:49 2008
@@ -35,7 +35,7 @@
 #include <switch.h>
 #include "private/switch_core_pvt.h"
 /*#define LOCK_MORE*/
-/*#define PER_POOL_LOCK 1*/
+#define PER_POOL_LOCK 1
 
 static struct {
 	switch_mutex_t *mem_lock;
@@ -246,21 +246,31 @@
 	return duped;
 }
 
+SWITCH_DECLARE(void) switch_core_memory_pool_tag(switch_memory_pool_t *pool, const char *tag)
+{
+	apr_pool_tag(pool, tag);
+}
+
 SWITCH_DECLARE(switch_status_t) switch_core_perform_new_memory_pool(switch_memory_pool_t **pool, const char *file, const char *func, int line)
 {
 	char *tmp;
-	void *pop;
+#ifdef PER_POOL_LOCK
+		apr_allocator_t *my_allocator = NULL;
+        apr_thread_mutex_t *my_mutex;
+#else
+		void *pop = NULL;
+#endif
 
 	switch_mutex_lock(memory_manager.mem_lock);
 	switch_assert(pool != NULL);
 
-	if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS) {
+#ifndef PER_POOL_LOCK
+	if (switch_queue_trypop(memory_manager.pool_recycle_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
 		*pool = (switch_memory_pool_t *) pop;
 	} else {
-#ifdef PER_POOL_LOCK
-		apr_allocator_t *my_allocator = NULL;
-        apr_thread_mutex_t *my_mutex;
+#endif
 
+#ifdef PER_POOL_LOCK
 		if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
 			return SWITCH_STATUS_MEMERR;
 		}
@@ -280,8 +290,8 @@
 #else
 		apr_pool_create(pool, NULL);
 		switch_assert(*pool != NULL);
-#endif
 	}
+#endif
 
 #ifdef DEBUG_ALLOC2
 	printf("New Pool %s %s:%d\n", file, func, line);
@@ -356,9 +366,6 @@
 
 static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t * thread, void *obj)
 {
-	void *pop = NULL;
-	switch_memory_pool_t *pool;
-
 	memory_manager.pool_thread_running = 1;
 
 	while (memory_manager.pool_thread_running == 1) {
@@ -370,26 +377,21 @@
 			switch_yield(1000000);
 			switch_mutex_lock(memory_manager.mem_lock);
 			while (x > 0) {
-				if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS) {
+				void *pop = NULL;
+				if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS || !pop) {
 					done = 1;
 					break;
 				}
+
 			
-				if (!pop) {
-					done = 1;
-					break;
-				}
-				
-				pool = (switch_memory_pool_t *) pop;
-#if PER_POOL_LOCK
-				apr_pool_destroy(pool);
+#if defined(PER_POOL_LOCK) || defined(DESTROY_POOLS)
+				apr_pool_destroy(pop);
 #else
-				apr_pool_clear(pool);
-				if (switch_queue_trypush(memory_manager.pool_recycle_queue, pool) != SWITCH_STATUS_SUCCESS) {
-					apr_pool_destroy(pool);
+				apr_pool_clear(pop);
+				if (switch_queue_trypush(memory_manager.pool_recycle_queue, pop) != SWITCH_STATUS_SUCCESS) {
+					apr_pool_destroy(pop);
 				}
 #endif
-				pool = NULL;
 				x--;
 			}
 			switch_mutex_unlock(memory_manager.mem_lock);
@@ -404,12 +406,12 @@
  done:
 	switch_core_memory_reclaim();
 
-	while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS) {
-		pool = (switch_memory_pool_t *) pop;
-		if (!pool) {
-			break;
+	{
+		void *pop = NULL;
+		while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
+			apr_pool_destroy(pop);
+			pop = NULL;
 		}
-		apr_pool_destroy(pool);
 	}
 
 	memory_manager.pool_thread_running = 0;

Modified: freeswitch/trunk/src/switch_core_session.c
==============================================================================
--- freeswitch/trunk/src/switch_core_session.c	(original)
+++ freeswitch/trunk/src/switch_core_session.c	Thu Feb 21 16:38:49 2008
@@ -690,6 +690,10 @@
 static void *SWITCH_THREAD_FUNC switch_core_session_thread(switch_thread_t * thread, void *obj)
 {
 	switch_core_session_t *session = obj;
+	switch_event_t *event;
+	char *event_str = NULL;
+	const char *val;
+
 	session->thread = thread;
 
 	switch_core_session_run(session);
@@ -697,6 +701,17 @@
 					  session->id, switch_channel_get_name(session->channel));
 	switch_core_session_write_lock(session);
 	switch_set_flag(session, SSF_DESTROYED);
+
+	if ((val = switch_channel_get_variable(session->channel, "memory_debug")) && switch_true(val)) {
+		if (switch_event_create(&event, SWITCH_EVENT_MESSAGE) == SWITCH_STATUS_SUCCESS) {
+			switch_channel_event_set_data(session->channel, event);
+			switch_event_serialize(event, &event_str, SWITCH_FALSE);
+			switch_assert(event_str);
+			switch_core_memory_pool_tag(switch_core_session_get_pool(session), switch_core_session_strdup(session, event_str));
+			free(event_str);
+		}
+	}
+
 	switch_core_session_rwunlock(session);
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Session %"SWITCH_SIZE_T_FMT" (%s) Ended\n",

Modified: freeswitch/trunk/src/switch_ivr_async.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_async.c	(original)
+++ freeswitch/trunk/src/switch_ivr_async.c	Thu Feb 21 16:38:49 2008
@@ -979,9 +979,15 @@
 			while (switch_queue_trypop(pvt->digit_queue, &pop) == SWITCH_STATUS_SUCCESS) {
 				switch_dtmf_t *dtmf = (switch_dtmf_t *) pop;
 				char buf[2] = "";
-			
+				int duration = dtmf->duration;
+
 				buf[0] = dtmf->digit;
-				pvt->ts.duration = dtmf->duration;
+				if (duration > 8000) {
+					duration = 4000;
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s Truncating ridiculous DTMF duration %d ms to 1/2 second.\n", 
+									  switch_channel_get_name(switch_core_session_get_channel(pvt->session)), dtmf->duration / 8);
+				}
+				pvt->ts.duration = duration;
 				teletone_run(&pvt->ts, buf);
 			}
 			

Modified: freeswitch/trunk/src/switch_rtp.c
==============================================================================
--- freeswitch/trunk/src/switch_rtp.c	(original)
+++ freeswitch/trunk/src/switch_rtp.c	Thu Feb 21 16:38:49 2008
@@ -1371,6 +1371,10 @@
 
 	if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
 		*rdigit = *dtmf;
+		if (rdigit->duration < SWITCH_DEFAULT_DTMF_DURATION) {
+			rdigit->duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+		
 		if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_queue, rdigit)) != SWITCH_STATUS_SUCCESS) {
 			free(rdigit);
 			return SWITCH_STATUS_FALSE;
@@ -1389,9 +1393,13 @@
 	if (!switch_rtp_ready(rtp_session)) {
 		return SWITCH_STATUS_FALSE;
 	}
-
+	
 	if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
 		*rdigit = *dtmf;
+		if (rdigit->duration < SWITCH_DEFAULT_DTMF_DURATION) {
+			rdigit->duration = SWITCH_DEFAULT_DTMF_DURATION;
+		}
+
 		if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_inqueue, rdigit)) != SWITCH_STATUS_SUCCESS) {
 			free(rdigit);
 			return SWITCH_STATUS_FALSE;



More information about the Freeswitch-svn mailing list