From 7fa3f7f3dd484a0f9688b9df1fc55e093e6d27f0 Mon Sep 17 00:00:00 2001
From: Giovanni Maruzzelli <gmaruzz@gmail.com>
Date: Sat, 21 May 2011 02:36:51 -0500
Subject: [PATCH] skypopen: fixing bug from Anton VG, adding some guards
 against NULL sessions and channels

---
 src/mod/endpoints/mod_skypopen/mod_skypopen.c | 65 +++++++++++--------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/src/mod/endpoints/mod_skypopen/mod_skypopen.c b/src/mod/endpoints/mod_skypopen/mod_skypopen.c
index 7b8bc16507..e86c86dc11 100644
--- a/src/mod/endpoints/mod_skypopen/mod_skypopen.c
+++ b/src/mod/endpoints/mod_skypopen/mod_skypopen.c
@@ -938,16 +938,21 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
 						char *p = digit_str;
 						switch_channel_t *channel = switch_core_session_get_channel(session);
 
-						while (p && *p) {
-							switch_dtmf_t dtmf;
-							dtmf.digit = *p;
-							dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
-							switch_channel_queue_dtmf(channel, &dtmf);
-							p++;
+						if(channel){
+
+							while (p && *p) {
+								switch_dtmf_t dtmf;
+								dtmf.digit = *p;
+								dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+								switch_channel_queue_dtmf(channel, &dtmf);
+								p++;
+							}
+							NOTICA("DTMF DETECTED: [%s] new_dtmf_timestamp: %u, delta_t: %u\n", SKYPOPEN_P_LOG, digit_str, (unsigned int) new_dtmf_timestamp,
+									(unsigned int) (new_dtmf_timestamp - tech_pvt->old_dtmf_timestamp));
+							tech_pvt->old_dtmf_timestamp = new_dtmf_timestamp;
+						}else{
+							WARNINGA("NO CHANNEL ?\n", SKYPOPEN_P_LOG);
 						}
-						NOTICA("DTMF DETECTED: [%s] new_dtmf_timestamp: %u, delta_t: %u\n", SKYPOPEN_P_LOG, digit_str, (unsigned int) new_dtmf_timestamp,
-							   (unsigned int) (new_dtmf_timestamp - tech_pvt->old_dtmf_timestamp));
-						tech_pvt->old_dtmf_timestamp = new_dtmf_timestamp;
 					}
 				}
 			}
@@ -2138,33 +2143,37 @@ int dtmf_received(private_t *tech_pvt, char *value)
 	switch_channel_t *channel = NULL;
 
 	session = switch_core_session_locate(tech_pvt->session_uuid_str);
-	channel = switch_core_session_get_channel(session);
+	if (session) {
+		channel = switch_core_session_get_channel(session);
 
-	if (channel) {
+		if (channel) {
 
-		if (switch_channel_test_flag(channel, CF_BRIDGED)
-			&& !switch_true(switch_channel_get_variable(channel, "skype_add_outband_dtmf_also_when_bridged"))) {
+			if (switch_channel_test_flag(channel, CF_BRIDGED)
+					&& !switch_true(switch_channel_get_variable(channel, "skype_add_outband_dtmf_also_when_bridged"))) {
 
 
-			NOTICA
-				("received DTMF '%c' on channel %s, but we're BRIDGED, so we DO NOT relay it out of band. If you DO want to relay it out of band when bridged too, on top of audio DTMF, set the channel variable 'skype_add_outband_dtmf_also_when_bridged=true' \n",
-				 SKYPOPEN_P_LOG, value[0], switch_channel_get_name(channel));
+				NOTICA
+					("received DTMF '%c' on channel %s, but we're BRIDGED, so we DO NOT relay it out of band. If you DO want to relay it out of band when bridged too, on top of audio DTMF, set the channel variable 'skype_add_outband_dtmf_also_when_bridged=true' \n",
+					 SKYPOPEN_P_LOG, value[0], switch_channel_get_name(channel));
 
+			} else {
+
+
+
+				switch_dtmf_t dtmf = { (char) value[0], switch_core_default_dtmf_duration(0) };
+				DEBUGA_SKYPE("received DTMF %c on channel %s\n", SKYPOPEN_P_LOG, dtmf.digit, switch_channel_get_name(channel));
+				switch_mutex_lock(tech_pvt->flag_mutex);
+				switch_channel_queue_dtmf(channel, &dtmf);
+				switch_set_flag(tech_pvt, TFLAG_DTMF);
+				switch_mutex_unlock(tech_pvt->flag_mutex);
+			}
 		} else {
-
-
-
-			switch_dtmf_t dtmf = { (char) value[0], switch_core_default_dtmf_duration(0) };
-			DEBUGA_SKYPE("received DTMF %c on channel %s\n", SKYPOPEN_P_LOG, dtmf.digit, switch_channel_get_name(channel));
-			switch_mutex_lock(tech_pvt->flag_mutex);
-			switch_channel_queue_dtmf(channel, &dtmf);
-			switch_set_flag(tech_pvt, TFLAG_DTMF);
-			switch_mutex_unlock(tech_pvt->flag_mutex);
+			WARNINGA("received %c DTMF, but no channel?\n", SKYPOPEN_P_LOG, value[0]);
 		}
-	} else {
-		WARNINGA("received %c DTMF, but no channel?\n", SKYPOPEN_P_LOG, value[0]);
+		switch_core_session_rwunlock(session);
+	}else{
+		WARNINGA("received %c DTMF, but no session?\n", SKYPOPEN_P_LOG, value[0]);
 	}
-	switch_core_session_rwunlock(session);
 
 	return 0;
 }