[asterisk-commits] bbryant: branch bbryant/iax2_rotation r115533 - in /team/bbryant/iax2_rotatio...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed May 7 15:06:15 CDT 2008


Author: bbryant
Date: Wed May  7 15:06:15 2008
New Revision: 115533

URL: http://svn.digium.com/view/asterisk?view=rev&rev=115533
Log:
Update branch with some debug code.

Modified:
    team/bbryant/iax2_rotation/channels/chan_iax2.c
    team/bbryant/iax2_rotation/channels/iax2.h
    team/bbryant/iax2_rotation/main/sched.c

Modified: team/bbryant/iax2_rotation/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/team/bbryant/iax2_rotation/channels/chan_iax2.c?view=diff&rev=115533&r1=115532&r2=115533
==============================================================================
--- team/bbryant/iax2_rotation/channels/chan_iax2.c (original)
+++ team/bbryant/iax2_rotation/channels/chan_iax2.c Wed May  7 15:06:15 2008
@@ -2231,6 +2231,10 @@
 	
 	owner = pvt ? pvt->owner : NULL;
 
+	if (pvt->rotateid > 0 && !ast_sched_del(sched, pvt->rotateid)) {
+		pvt->rotateid = -1;
+	}
+
 	if (owner) {
 		if (ast_channel_trylock(owner)) {
 			ast_log(LOG_NOTICE, "Avoiding IAX destroy deadlock\n");
@@ -3728,12 +3732,15 @@
 	return &ast_null_frame;
 }
 
+static int iax2_send_rotate_transfer(struct chan_iax2_pvt *, struct chan_iax2_pvt *);
+
 static int iax2_start_transfer(unsigned short callno0, unsigned short callno1, int mediaonly)
 {
 	int res;
 	struct iax_ie_data ied0;
 	struct iax_ie_data ied1;
 	unsigned int transferid = (unsigned int)ast_random();
+
 	memset(&ied0, 0, sizeof(ied0));
 	iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
 	iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
@@ -3744,6 +3751,11 @@
 	iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
 	iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
 	
+	if ((ast_test_flag(iaxs[callno0], IAX_ENCRYPTED) && ast_test_flag(iaxs[callno0], IAX_KEYPOPULATED)) &&
+		(ast_test_flag(iaxs[callno1], IAX_ENCRYPTED) && ast_test_flag(iaxs[callno1], IAX_KEYPOPULATED))) {
+		iax2_send_rotate_transfer(iaxs[callno0], iaxs[callno1]);
+	}
+
 	res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
 	if (res)
 		return -1;
@@ -4572,23 +4584,71 @@
 	return res;
 }
 
-static int iax2_rotate(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
+static int iax2_rotate(struct chan_iax2_pvt *pvt, int subclass, struct iax_ies *ies)
 {
 	struct MD5Context md5;
 	unsigned char digest[16];
-	char dgststr[33] = "";
+	char dgststr[33] = "", *tmp;
 	int i;
 
 	MD5Init(&md5);
 	MD5Update(&md5, (unsigned char *)ies->challenge, strlen(ies->challenge));
 	MD5Final(digest, &md5);
 
-	for (i = 0; i < 16; i++)
-		snprintf(dgststr, sizeof(dgststr), "%s%02x", dgststr, digest[i]);
-
-	ast_log(LOG_NOTICE, "iax2_rotate: rotating dcx to new key '%s'", dgststr);
-
-	ast_aes_decrypt_key((unsigned char *)ies->challenge, &pvt->dcx);
+	for (tmp = dgststr, i = 0; i < 16; i++, tmp += 2)
+		snprintf(tmp, 3, "%02x", digest[i]);
+
+//	ast_log(LOG_NOTICE, "iax2_rotate: challenge (%d) == '%s'\n", (int)strlen(ies->challenge), ies->challenge);
+
+	ast_log(LOG_NOTICE, "iax2_rotate [%d]: rotating %s to new key '%s'\n", pvt->callno, (subclass == IAX_COMMAND_RTENC) ? "dcx" : "ecx", dgststr);
+
+	if (subclass == IAX_COMMAND_RTENC) {
+		ast_aes_decrypt_key(digest, &pvt->dcx);
+	} else if (subclass == IAX_COMMAND_RTDEC) {
+		ast_aes_encrypt_key(digest, &pvt->ecx);
+	} else {
+		ast_log(LOG_ERROR, "iax2_rotate: invalid IAX_COMMAND = %d\n", subclass);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int iax2_send_rotate_transfer(struct chan_iax2_pvt *pvt0, struct chan_iax2_pvt *pvt1)
+{
+	struct iax_ie_data ied;
+	struct MD5Context md5;
+	char challenge[11] = "";
+	unsigned char digest[16];
+	char dgststr[33] = "", *tmp;
+	int i;
+
+	memset(&ied, 0, sizeof(ied));
+
+	snprintf(challenge, sizeof(challenge), "%x", (int)ast_random());
+
+	iax_ie_append_str(&ied, IAX_IE_CHALLENGE, challenge);
+
+	if (send_command(pvt0, AST_FRAME_IAX, IAX_COMMAND_RTENC, 0, ied.buf, ied.pos, -1) ||
+		send_command(pvt0, AST_FRAME_IAX, IAX_COMMAND_RTDEC, 0, ied.buf, ied.pos, -1) ||
+		send_command(pvt1, AST_FRAME_IAX, IAX_COMMAND_RTENC, 0, ied.buf, ied.pos, -1) ||
+		send_command(pvt1, AST_FRAME_IAX, IAX_COMMAND_RTDEC, 0, ied.buf, ied.pos, -1))
+		return -1;
+
+	
+	MD5Init(&md5);
+	MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
+	MD5Final(digest, &md5);
+
+	for (tmp = dgststr, i = 0; i < 16; i++, tmp += 2)
+		snprintf(tmp, 3, "%02x", digest[i]);
+
+	ast_log(LOG_NOTICE, "iax2_send_rotate_transfer: challenge (%d) == '%s'\n", (int)strlen(challenge), challenge);
+
+	ast_aes_encrypt_key(digest, &pvt0->ecx);
+	ast_aes_decrypt_key(digest, &pvt0->dcx);
+	ast_aes_encrypt_key(digest, &pvt1->ecx);
+	ast_aes_decrypt_key(digest, &pvt1->dcx);
 
 	return 0;
 }
@@ -4600,26 +4660,32 @@
 	struct MD5Context md5;
 	char challenge[11] = "";
 	unsigned char digest[16];
-	char dgststr[33] = "";
+	char dgststr[33] = "", *tmp;
 	int res = 0, i;
 
-	pvt->rotateid = iax2_sched_add(sched, ((ast_random() % 4) + 2) * 3600000, iax2_send_rotate, pvt);
+	//ast_sched_del(sched, p->rotateid);
+	ast_log(LOG_ERROR, " -- iax2_send_rotate: START SCHEDULER! %d\n",
+		(pvt->rotateid = iax2_sched_add(sched, 200, iax2_send_rotate, pvt))
+	);
+//	pvt->rotateid = iax2_sched_add(sched, ((ast_random() % 4) + 2) * 3600000, iax2_send_rotate, pvt);
 
 	memset(&ied, 0, sizeof(ied));
 
 	snprintf(challenge, sizeof(challenge), "%x", (int)ast_random());
 
-	iax_ie_append_raw(&ied, IAX_IE_CHALLENGE, challenge, sizeof(challenge)-1);
-	res = send_command(pvt, AST_FRAME_IAX, IAX_COMMAND_ROTATE, 0, ied.buf, ied.pos, -1);
+	iax_ie_append_str(&ied, IAX_IE_CHALLENGE, challenge); //, sizeof(challenge));
+	res = send_command(pvt, AST_FRAME_IAX, IAX_COMMAND_RTENC, 0, ied.buf, ied.pos, -1);
 	
 	MD5Init(&md5);
-	MD5Update(&md5, (unsigned char *)challenge, sizeof(challenge));
+	MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
 	MD5Final(digest, &md5);
 
-	for (i = 0; i < 16; i++)
-		snprintf(dgststr, sizeof(dgststr), "%s%02x", dgststr, digest[i]);
-
-	ast_log(LOG_NOTICE, "iax2_send_rotate: sending new ecx key '%s'", dgststr);
+	for (tmp = dgststr, i = 0; i < 16; i++, tmp += 2)
+		snprintf(tmp, 3, "%02x", digest[i]);
+
+	ast_log(LOG_NOTICE, "iax2_send_rotate: challenge (%d) == '%s'\n", (int)strlen(challenge), challenge);
+
+//	ast_log(LOG_NOTICE, "iax2_send_rotate: sending new ecx key '%s'\n", dgststr);
 
 	ast_aes_encrypt_key(digest, &pvt->ecx);
 
@@ -8041,7 +8107,7 @@
 	}
 	if (ast_test_flag(iaxs[fr->callno], IAX_ENCRYPTED)) {
 		if (decrypt_frame(fr->callno, fh, &f, &res)) {
-			ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
+			//ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
 			ast_mutex_unlock(&iaxsl[fr->callno]);
 			return 1;
 		}
@@ -9319,12 +9385,13 @@
 					return 1;
 				}
 				break;
-			case IAX_COMMAND_ROTATE:
+			case IAX_COMMAND_RTENC:
+			case IAX_COMMAND_RTDEC:
 				if (!ast_test_flag(iaxs[fr->callno], IAX_ENCRYPTED) || !ast_test_flag(iaxs[fr->callno], IAX_KEYPOPULATED)) {
 					break;
 				}
 
-				iax2_rotate(iaxs[fr->callno], &ies);
+				iax2_rotate(iaxs[fr->callno], f.subclass, &ies);
 				break;
 			default:
 				ast_debug(1, "Unknown IAX command %d on %d/%d\n", f.subclass, fr->callno, iaxs[fr->callno]->peercallno);

Modified: team/bbryant/iax2_rotation/channels/iax2.h
URL: http://svn.digium.com/view/asterisk/team/bbryant/iax2_rotation/channels/iax2.h?view=diff&rev=115533&r1=115532&r2=115533
==============================================================================
--- team/bbryant/iax2_rotation/channels/iax2.h (original)
+++ team/bbryant/iax2_rotation/channels/iax2.h Wed May  7 15:06:15 2008
@@ -109,8 +109,10 @@
 	IAX_COMMAND_FWDATA =    37,
 	/*! Transfer media only */
 	IAX_COMMAND_TXMEDIA =   38,
-	/*! Rotate Encryption */
-	IAX_COMMAND_ROTATE =	39,
+	/*! Command to send new key used for encryption */
+	IAX_COMMAND_RTENC =	39,
+	/*! Command to send new key used for decryption */
+	IAX_COMMAND_RTDEC =	40,
 };
 
 /*! By default require re-registration once per minute */

Modified: team/bbryant/iax2_rotation/main/sched.c
URL: http://svn.digium.com/view/asterisk/team/bbryant/iax2_rotation/main/sched.c?view=diff&rev=115533&r1=115532&r2=115533
==============================================================================
--- team/bbryant/iax2_rotation/main/sched.c (original)
+++ team/bbryant/iax2_rotation/main/sched.c Wed May  7 15:06:15 2008
@@ -27,7 +27,7 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#ifdef DEBUG_SCHEDULER
+#if defined(DEBUG_SCHEDULER) || !defined(DEBUG_SCHEDULER)
 #define DEBUG(a) do { \
 	if (option_debug) \
 		DEBUG_M(a) \
@@ -269,6 +269,8 @@
 	if (!when) {
 		ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
 		return -1;
+	} else {
+		ast_log(LOG_NOTICE, "Scheduled event in %d ms\n", when);
 	}
 	ast_mutex_lock(&con->lock);
 	if ((tmp = sched_alloc(con))) {




More information about the asterisk-commits mailing list