From e856df69d08040c03574f70a654357984a9e2f71 Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Thu, 20 Mar 2003 05:56:00 +0000
Subject: [PATCH] Add transfer ID for potential use in more complex NAT
 environments

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@661 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_iax2.c   | 14 ++++++++++++--
 channels/iax2-parser.c |  8 ++++++++
 channels/iax2-parser.h |  1 +
 channels/iax2.h        |  1 +
 dsp.c                  |  2 +-
 5 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index b1d2402ce7..b5212d3811 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -348,6 +348,8 @@ struct chan_iax2_pvt {
 
 	/* Transferring status */
 	int transferring;
+	/* Transfer identifier */
+	int transferid;
 	/* Already disconnected */
 	int alreadygone;
 	/* Who we are IAX transfering to */
@@ -1757,13 +1759,16 @@ static int iax2_start_transfer(struct ast_channel *c0, struct ast_channel *c1)
 	struct iax_ie_data ied1;
 	struct chan_iax2_pvt *p0 = c0->pvt->pvt;
 	struct chan_iax2_pvt *p1 = c1->pvt->pvt;
+	unsigned int transferid = rand();
 	memset(&ied0, 0, sizeof(ied0));
 	iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &p1->addr);
 	iax_ie_append_short(&ied0, IAX_IE_CALLNO, p1->peercallno);
+	iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
 
 	memset(&ied1, 0, sizeof(ied1));
 	iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &p0->addr);
 	iax_ie_append_short(&ied1, IAX_IE_CALLNO, p0->peercallno);
+	iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
 	
 	res = send_command(p0, AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
 	if (res)
@@ -2931,9 +2936,11 @@ static int try_transfer(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
 {
 	int newcall = 0;
 	char newip[256] = "";
-	
+	struct iax_ie_data ied;
 	struct sockaddr_in new;
 	
+	
+	memset(&ied, 0, sizeof(ied));
 	if (ies->apparent_addr)
 		memcpy(&new, ies->apparent_addr, sizeof(new));
 	if (ies->callno)
@@ -2947,7 +2954,10 @@ static int try_transfer(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
 	inet_aton(newip, &pvt->transfer.sin_addr);
 	pvt->transfer.sin_family = AF_INET;
 	pvt->transferring = TRANSFER_BEGIN;
-	send_command_transfer(pvt, AST_FRAME_IAX, IAX_COMMAND_TXCNT, 0, NULL, 0);
+	pvt->transferid = ies->transferid;
+	if (ies->transferid)
+		iax_ie_append_int(&ied, IAX_IE_TRANSFERID, ies->transferid);
+	send_command_transfer(pvt, AST_FRAME_IAX, IAX_COMMAND_TXCNT, 0, ied.buf, ied.pos);
 	return 0; 
 }
 
diff --git a/channels/iax2-parser.c b/channels/iax2-parser.c
index f7020562b8..1187a9098e 100755
--- a/channels/iax2-parser.c
+++ b/channels/iax2-parser.c
@@ -110,6 +110,7 @@ static struct iax2_ie {
 	{ IAX_IE_IAX_UNKNOWN, "UNKNOWN IAX CMD", dump_byte },
 	{ IAX_IE_MSGCOUNT, "MESSAGE COUNT", dump_short },
 	{ IAX_IE_AUTOANSWER, "AUTO ANSWER REQ" },
+	{ IAX_IE_TRANSFERID, "TRANSFER ID", dump_int },
 };
 
 const char *iax_ie2str(int ie)
@@ -478,6 +479,13 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 		case IAX_IE_MUSICONHOLD:
 			ies->musiconhold = 1;
 			break;
+		case IAX_IE_TRANSFERID:
+			if (len != sizeof(unsigned int)) {
+				snprintf(tmp, sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", sizeof(unsigned int), len);
+				errorf(tmp);
+			} else
+				ies->transferid = ntohl(*((unsigned int *)(data + 2)));
+			break;
 		default:
 			snprintf(tmp, sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
 			errorf(tmp);
diff --git a/channels/iax2-parser.h b/channels/iax2-parser.h
index e34ca1ca7c..f23b70d35b 100755
--- a/channels/iax2-parser.h
+++ b/channels/iax2-parser.h
@@ -41,6 +41,7 @@ struct iax_ies {
 	int msgcount;
 	int autoanswer;
 	int musiconhold;
+	unsigned int transferid;
 };
 
 #define DIRECTION_INGRESS 1
diff --git a/channels/iax2.h b/channels/iax2.h
index 88ffe0ca08..570c1e87ab 100755
--- a/channels/iax2.h
+++ b/channels/iax2.h
@@ -97,6 +97,7 @@
 #define IAX_IE_MSGCOUNT				24		/* How many messages waiting - short */
 #define IAX_IE_AUTOANSWER			25		/* Request auto-answering -- none */
 #define IAX_IE_MUSICONHOLD			26		/* Request musiconhold with QUELCH -- none or string */
+#define IAX_IE_TRANSFERID			27		/* Transfer Request Identifier -- int */
 
 #define IAX_AUTH_PLAINTEXT			(1 << 0)
 #define IAX_AUTH_MD5				(1 << 1)
diff --git a/dsp.c b/dsp.c
index 62e03420fe..afcb3fd79a 100755
--- a/dsp.c
+++ b/dsp.c
@@ -843,7 +843,7 @@ static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
 {
 	int x;
 	int pass;
-	int newstate;
+	int newstate = TONE_STATE_SILENCE;
 	int res = 0;
 	while(len) {
 		/* Take the lesser of the number of samples we need and what we have */
-- 
GitLab