From a5a0a5f867a45eef2e7ccb6c64f3a4f2e8c3a7ba Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Sat, 3 Apr 2010 02:12:33 +0000
Subject: [PATCH] Consolidate ast_channel.cid.cid_rdnis into
 ast_channel.redirecting.from.number.

SWP-1229
ABE-2161

* Ensure chan_local.c:local_call() will not leak cid.cid_dnid when
copying.


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@256104 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_amd.c             |  3 ++-
 apps/app_dial.c            | 30 ++++++++++++++++--------------
 apps/app_dumpchan.c        |  2 +-
 apps/app_queue.c           | 28 ++++++++++++----------------
 channels/chan_dahdi.c      |  2 +-
 channels/chan_h323.c       |  6 +++---
 channels/chan_iax2.c       |  6 +++---
 channels/chan_local.c      | 20 +++++++-------------
 channels/chan_misdn.c      |  2 +-
 channels/chan_oss.c        |  3 ++-
 channels/chan_sip.c        |  4 ++--
 channels/sig_pri.c         | 13 ++++---------
 funcs/func_callerid.c      | 10 ++++------
 funcs/func_redirecting.c   |  6 +-----
 include/asterisk/channel.h | 13 +------------
 main/cel.c                 |  4 ++--
 main/channel.c             | 16 +---------------
 main/dial.c                | 10 ----------
 res/res_agi.c              |  2 +-
 res/snmp/agent.c           |  4 ++--
 20 files changed, 66 insertions(+), 118 deletions(-)

diff --git a/apps/app_amd.c b/apps/app_amd.c
index 30f6da0588..928afee0b1 100644
--- a/apps/app_amd.c
+++ b/apps/app_amd.c
@@ -188,7 +188,8 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
 		AST_APP_ARG(argMaximumWordLength);
 	);
 
-	ast_verb(3, "AMD: %s %s %s (Fmt: %s)\n", chan->name, chan->cid.cid_ani, chan->cid.cid_rdnis, ast_getformatname(chan->readformat));
+	ast_verb(3, "AMD: %s %s %s (Fmt: %s)\n", chan->name, chan->cid.cid_ani,
+		chan->redirecting.from.number, ast_getformatname(chan->readformat));
 
 	/* Lets parse the arguments. */
 	if (!ast_strlen_zero(parse)) {
diff --git a/apps/app_dial.c b/apps/app_dial.c
index d4b16a36b2..8a58932a83 100644
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -691,12 +691,11 @@ static void handle_cause(int cause, struct cause_args *num)
 	}
 }
 
-/* free the buffer if allocated, and set the pointer to the second arg */
-#define S_REPLACE(s, new_val)		\
-	do {				\
-		if (s)			\
-			ast_free(s);	\
-		s = (new_val);		\
+/*! \brief free the buffer if allocated, and set the pointer to the second arg */
+#define S_REPLACE(s, new_val) \
+	do {                      \
+		ast_free(s);          \
+		s = (new_val);        \
 	} while (0)
 
 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
@@ -773,8 +772,6 @@ static void do_forward(struct chanlist *o,
 	struct ast_channel *original = o->chan;
 	struct ast_channel *c = o->chan; /* the winner */
 	struct ast_channel *in = num->chan; /* the input channel */
-	struct ast_party_redirecting *apr = &o->chan->redirecting;
-	struct ast_party_connected_line *apc = &o->chan->connected;
 	char *stuff;
 	char *tech;
 	int cause;
@@ -825,12 +822,19 @@ static void do_forward(struct chanlist *o,
 			ast_rtp_instance_early_bridge_make_compatible(c, in);
 		}
 
-		ast_channel_set_redirecting(c, apr);
+		ast_channel_set_redirecting(c, &original->redirecting);
 		ast_channel_lock(c);
 		while (ast_channel_trylock(in)) {
 			CHANNEL_DEADLOCK_AVOIDANCE(c);
 		}
-		S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(original->cid.cid_rdnis, S_OR(in->macroexten, in->exten))));
+		if (ast_strlen_zero(c->redirecting.from.number)) {
+			/*
+			 * The call was not previously redirected so it is
+			 * now redirected from this number.
+			 */
+			S_REPLACE(c->redirecting.from.number,
+				ast_strdup(S_OR(in->macroexten, in->exten)));
+		}
 
 		c->cid.cid_tns = in->cid.cid_tns;
 
@@ -842,10 +846,9 @@ static void do_forward(struct chanlist *o,
 			ast_party_caller_copy(&c->cid, &in->cid);
 			ast_string_field_set(c, accountcode, in->accountcode);
 		}
-		ast_party_connected_line_copy(&c->connected, apc);
+		ast_party_connected_line_copy(&c->connected, &original->connected);
 
-		S_REPLACE(in->cid.cid_rdnis, ast_strdup(c->cid.cid_rdnis));
-		ast_channel_update_redirecting(in, apr);
+		ast_channel_update_redirecting(in, &c->redirecting);
 
 		ast_clear_flag64(peerflags, OPT_IGNORE_CONNECTEDLINE);
 		if (ast_test_flag64(peerflags, OPT_CANCEL_TIMEOUT)) {
@@ -1913,7 +1916,6 @@ static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast
 			ast_connected_line_copy_from_caller(&tc->connected, &chan->cid);
 		}
 
-		S_REPLACE(tc->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
 		ast_party_redirecting_copy(&tc->redirecting, &chan->redirecting);
 
 		tc->cid.cid_tns = chan->cid.cid_tns;
diff --git a/apps/app_dumpchan.c b/apps/app_dumpchan.c
index a7b4fc001e..d66661db6e 100644
--- a/apps/app_dumpchan.c
+++ b/apps/app_dumpchan.c
@@ -119,7 +119,7 @@ static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
 			S_OR(c->cid.cid_num, "(N/A)"),
 			S_OR(c->cid.cid_name, "(N/A)"),
 			S_OR(c->cid.cid_dnid, "(N/A)"),
-			S_OR(c->cid.cid_rdnis, "(N/A)"),
+			S_OR(c->redirecting.from.number, "(N/A)"),
 			c->parkinglot,
 			c->language,
 			ast_state2str(c->_state),
diff --git a/apps/app_queue.c b/apps/app_queue.c
index 3dac6a4ab4..e689cb0800 100644
--- a/apps/app_queue.c
+++ b/apps/app_queue.c
@@ -2835,9 +2835,6 @@ static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies
 		tmp->update_connectedline = 0;
 	}
 
-	if (tmp->chan->cid.cid_rdnis)
-		ast_free(tmp->chan->cid.cid_rdnis);
-	tmp->chan->cid.cid_rdnis = ast_strdup(qe->chan->cid.cid_rdnis);
 	ast_party_redirecting_copy(&tmp->chan->redirecting, &qe->chan->redirecting);
 
 	tmp->chan->cid.cid_tns = qe->chan->cid.cid_tns;
@@ -3275,8 +3272,6 @@ static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callatte
 					winner = NULL;
 					continue;
 				} else if (!ast_strlen_zero(o->chan->call_forward)) {
-					struct ast_party_redirecting *apr = &o->chan->redirecting;
-					struct ast_party_connected_line *apc = &o->chan->connected;
 					struct ast_channel *original = o->chan;
 					char tmpchan[256];
 					char *stuff;
@@ -3312,22 +3307,23 @@ static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callatte
 
 						ast_string_field_set(o->chan, accountcode, in->accountcode);
 
-						ast_channel_set_redirecting(o->chan, apr);
-
-						if (o->chan->cid.cid_rdnis)
-							ast_free(o->chan->cid.cid_rdnis);
-						o->chan->cid.cid_rdnis = ast_strdup(S_OR(original->cid.cid_rdnis,S_OR(in->macroexten, in->exten)));
+						ast_channel_set_redirecting(o->chan, &original->redirecting);
+						if (ast_strlen_zero(o->chan->redirecting.from.number)) {
+							/*
+							 * The call was not previously redirected so it is
+							 * now redirected from this number.
+							 */
+							ast_free(o->chan->redirecting.from.number);
+							o->chan->redirecting.from.number =
+								ast_strdup(S_OR(in->macroexten, in->exten));
+						}
 
 						o->chan->cid.cid_tns = in->cid.cid_tns;
 
 						ast_party_caller_copy(&o->chan->cid, &in->cid);
-						ast_party_connected_line_copy(&o->chan->connected, apc);
+						ast_party_connected_line_copy(&o->chan->connected, &original->connected);
 
-						ast_channel_update_redirecting(in, apr);
-						if (in->cid.cid_rdnis) {
-							ast_free(in->cid.cid_rdnis);
-						}
-						in->cid.cid_rdnis = ast_strdup(o->chan->cid.cid_rdnis);
+						ast_channel_update_redirecting(in, &o->chan->redirecting);
 
 						update_connectedline = 1;
 
diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index 7f1840e633..72e341ebd2 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -8741,7 +8741,7 @@ static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpb
 	if (!ast_strlen_zero(i->exten))
 		ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
 	if (!ast_strlen_zero(i->rdnis))
-		tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
+		tmp->redirecting.from.number = ast_strdup(i->rdnis);
 	if (!ast_strlen_zero(i->dnid))
 		tmp->cid.cid_dnid = ast_strdup(i->dnid);
 
diff --git a/channels/chan_h323.c b/channels/chan_h323.c
index 2746199f52..51d3cb1d2d 100644
--- a/channels/chan_h323.c
+++ b/channels/chan_h323.c
@@ -615,8 +615,8 @@ static int oh323_call(struct ast_channel *c, char *dest, int timeout)
 	if (c->connected.id.name)
 		ast_copy_string(pvt->options.cid_name, c->connected.id.name, sizeof(pvt->options.cid_name));
 
-	if (c->cid.cid_rdnis) {
-		ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
+	if (c->redirecting.from.number) {
+		ast_copy_string(pvt->options.cid_rdnis, c->redirecting.from.number, sizeof(pvt->options.cid_rdnis));
 	}
 
 	pvt->options.presentation = c->connected.id.number_presentation;
@@ -1078,7 +1078,7 @@ static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const c
 		ch->cid.cid_ani = ast_strdup(cid_num);
 
 		if (pvt->cd.redirect_reason >= 0) {
-			ch->cid.cid_rdnis = ast_strdup(pvt->cd.redirect_number);
+			ch->redirecting.from.number = ast_strdup(pvt->cd.redirect_number);
 			pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
 		}
 		ch->cid.cid_pres = pvt->cd.presentation;
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 1d89eab3c8..6cf3f652cb 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -4952,8 +4952,8 @@ static int iax2_call(struct ast_channel *c, char *dest, int timeout)
 		iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
 	if (!ast_strlen_zero(c->cid.cid_dnid))
 		iax_ie_append_str(&ied, IAX_IE_DNID, c->cid.cid_dnid);
-	if (!ast_strlen_zero(c->cid.cid_rdnis))
-		iax_ie_append_str(&ied, IAX_IE_RDNIS, c->cid.cid_rdnis);
+	if (!ast_strlen_zero(c->redirecting.from.number))
+		iax_ie_append_str(&ied, IAX_IE_RDNIS, c->redirecting.from.number);
 
 	if (pds.context)
 		iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.context);
@@ -5549,7 +5549,7 @@ static struct ast_channel *ast_iax2_new(int callno, int state, format_t capabili
 	else
 		tmp->cid.cid_ani = ast_strdup(i->cid_num);
 	tmp->cid.cid_dnid = ast_strdup(i->dnid);
-	tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
+	tmp->redirecting.from.number = ast_strdup(i->rdnis);
 	tmp->cid.cid_pres = i->calling_pres;
 	tmp->cid.cid_ton = i->calling_ton;
 	tmp->cid.cid_tns = i->calling_tns;
diff --git a/channels/chan_local.c b/channels/chan_local.c
index b2c7b9ed4d..5e522e7971 100644
--- a/channels/chan_local.c
+++ b/channels/chan_local.c
@@ -574,21 +574,15 @@ start_over:
 	 * All these failure points just return -1. The individual strings will
 	 * be cleared when we destroy the channel.
 	 */
-	if (p->owner->cid.cid_rdnis) {
-		if (!(p->chan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis))) {
-			ast_mutex_unlock(&p->lock);
-			ast_channel_unlock(p->chan);
-			return -1;
-		}
-	}
 	ast_party_redirecting_copy(&p->chan->redirecting, &p->owner->redirecting);
 
-	if (p->owner->cid.cid_dnid) {
-		if (!(p->chan->cid.cid_dnid = ast_strdup(p->owner->cid.cid_dnid))) {
-			ast_mutex_unlock(&p->lock);
-			ast_channel_unlock(p->chan);
-			return -1;
-		}
+	ast_free(p->chan->cid.cid_dnid);
+	p->chan->cid.cid_dnid = ast_strdup(p->owner->cid.cid_dnid);
+	if (!p->chan->cid.cid_dnid && p->owner->cid.cid_dnid) {
+		/* Allocation failure */
+		ast_mutex_unlock(&p->lock);
+		ast_channel_unlock(p->chan);
+		return -1;
 	}
 	p->chan->cid.cid_tns = p->owner->cid.cid_tns;
 
diff --git a/channels/chan_misdn.c b/channels/chan_misdn.c
index 7d00a8bf77..d37d66a313 100644
--- a/channels/chan_misdn.c
+++ b/channels/chan_misdn.c
@@ -6090,7 +6090,7 @@ static void misdn_update_connected_line(struct ast_channel *ast, struct misdn_bc
 static void misdn_copy_redirecting_from_ast(struct misdn_bchannel *bc, struct ast_channel *ast)
 {
 	ast_copy_string(bc->redirecting.from.name, S_OR(ast->redirecting.from.name, ""), sizeof(bc->redirecting.from.name));
-	ast_copy_string(bc->redirecting.from.number, S_OR(ast->cid.cid_rdnis, ""), sizeof(bc->redirecting.from.number));
+	ast_copy_string(bc->redirecting.from.number, S_OR(ast->redirecting.from.number, ""), sizeof(bc->redirecting.from.number));
 	bc->redirecting.from.presentation = ast_to_misdn_pres(ast->redirecting.from.number_presentation);
 	bc->redirecting.from.screening = ast_to_misdn_screen(ast->redirecting.from.number_presentation);
 	bc->redirecting.from.number_type = ast_to_misdn_ton(ast->redirecting.from.number_type);
diff --git a/channels/chan_oss.c b/channels/chan_oss.c
index 09ca4b8306..4142685767 100644
--- a/channels/chan_oss.c
+++ b/channels/chan_oss.c
@@ -601,7 +601,8 @@ static int oss_call(struct ast_channel *c, char *dest, int timeout)
 
 	AST_NONSTANDARD_APP_ARGS(args, parse, '/');
 
-	ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n", dest, c->cid.cid_dnid, c->cid.cid_rdnis, c->cid.cid_name, c->cid.cid_num);
+	ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n",
+		dest, c->cid.cid_dnid, c->redirecting.from.number, c->cid.cid_name, c->cid.cid_num);
 	if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
 		f.subclass.integer = AST_CONTROL_ANSWER;
 		ast_queue_frame(c, &f);
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 59ba5dce2b..934a925f0d 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -5568,7 +5568,7 @@ static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *tit
 	 * generate an unnecessary NewCallerID event  */
 	tmp->cid.cid_ani = ast_strdup(i->cid_num);
 	if (!ast_strlen_zero(i->rdnis))
-		tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
+		tmp->redirecting.from.number = ast_strdup(i->rdnis);
 	
 	if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
 		tmp->cid.cid_dnid = ast_strdup(i->exten);
@@ -9654,7 +9654,7 @@ static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt)
 		return;
 	}
 
-	diverting_number = pvt->owner->cid.cid_rdnis;
+	diverting_number = pvt->owner->redirecting.from.number;
 	diverting_name = pvt->owner->redirecting.from.name;
 	reason = sip_reason_code_to_str(pvt->owner->redirecting.reason);
 
diff --git a/channels/sig_pri.c b/channels/sig_pri.c
index c1dff5f5e9..fd3b4111e2 100644
--- a/channels/sig_pri.c
+++ b/channels/sig_pri.c
@@ -665,19 +665,14 @@ static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct
 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
 {
 	struct pri_party_redirecting pri_redirecting;
-	struct ast_party_redirecting ast_redirecting;
-
-	/* Gather asterisk redirecting data */
-	ast_redirecting = ast->redirecting;
-	ast_redirecting.from.number = ast->cid.cid_rdnis;
 
 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
 
 	memset(&pri_redirecting, 0, sizeof(pri_redirecting));
-	sig_pri_party_id_from_ast(&pri_redirecting.from, &ast_redirecting.from);
-	sig_pri_party_id_from_ast(&pri_redirecting.to, &ast_redirecting.to);
-	pri_redirecting.count = ast_redirecting.count;
-	pri_redirecting.reason = ast_to_pri_reason(ast_redirecting.reason);
+	sig_pri_party_id_from_ast(&pri_redirecting.from, &ast->redirecting.from);
+	sig_pri_party_id_from_ast(&pri_redirecting.to, &ast->redirecting.to);
+	pri_redirecting.count = ast->redirecting.count;
+	pri_redirecting.reason = ast_to_pri_reason(ast->redirecting.reason);
 
 	pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
 }
diff --git a/funcs/func_callerid.c b/funcs/func_callerid.c
index 3e9e74e0a5..3de69950e0 100644
--- a/funcs/func_callerid.c
+++ b/funcs/func_callerid.c
@@ -209,8 +209,8 @@ static int callerid_read(struct ast_channel *chan, const char *cmd, char *data,
 				}
 			}
 		} else if (!strncasecmp("rdnis", data, 5)) {
-			if (chan->cid.cid_rdnis) {
-				ast_copy_string(buf, chan->cid.cid_rdnis, len);
+			if (chan->redirecting.from.number) {
+				ast_copy_string(buf, chan->redirecting.from.number, len);
 			}
 		} else if (!strncasecmp("pres", data, 4)) {
 			ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
@@ -311,10 +311,8 @@ static int callerid_write(struct ast_channel *chan, const char *cmd, char *data,
 		ast_channel_unlock(chan);
 	} else if (!strncasecmp("rdnis", data, 5)) {
 		ast_channel_lock(chan);
-		if (chan->cid.cid_rdnis) {
-			ast_free(chan->cid.cid_rdnis);
-		}
-		chan->cid.cid_rdnis = ast_strdup(value);
+		ast_free(chan->redirecting.from.number);
+		chan->redirecting.from.number = ast_strdup(value);
 		if (chan->cdr) {
 			ast_cdr_setcid(chan->cdr, chan);
 		}
diff --git a/funcs/func_redirecting.c b/funcs/func_redirecting.c
index 62c6d7bc99..87fc41abeb 100644
--- a/funcs/func_redirecting.c
+++ b/funcs/func_redirecting.c
@@ -183,11 +183,7 @@ static int redirecting_read(struct ast_channel *chan, const char *cmd, char *dat
 	ast_channel_lock(chan);
 
 	if (!strncasecmp("from-", data, 5)) {
-		struct ast_party_id from_id;
-
-		from_id = chan->redirecting.from;
-		from_id.number = chan->cid.cid_rdnis;
-		switch (redirecting_id_read(buf, len, data + 5, &from_id)) {
+		switch (redirecting_id_read(buf, len, data + 5, &chan->redirecting.from)) {
 		case ID_FIELD_VALID:
 		case ID_FIELD_INVALID:
 			break;
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 76206c4f73..a32486a397 100644
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -256,12 +256,6 @@ struct ast_callerid {
 	 */
 	char *cid_ani;
 
-	/*!
-	 * \brief Malloc'd Redirecting Directory Number Information Service (RDNIS)
-	 * (Field will eventually move to struct ast_channel.redirecting.from.number)
-	 */
-	char *cid_rdnis;
-
 	/*!
 	 * \brief Callerid Q.931 encoded number presentation/screening fields
 	 * (Field will eventually move to struct ast_channel.caller.id.number_presentation)
@@ -700,12 +694,7 @@ struct ast_channel {
 	 */
 	struct ast_party_connected_line connected;
 
-	/*!
-	 * \brief Redirecting/Diversion information
-	 * \note Until struct ast_channel.cid.cid_rdnis is replaced
-	 * with ast_channel.redirecting.from.number, the
-	 * ast_channel.redirecting.from.number field is not used.
-	 */
+	/*! \brief Redirecting/Diversion information */
 	struct ast_party_redirecting redirecting;
 
 	struct ast_frame dtmff;				/*!< DTMF frame */
diff --git a/main/cel.c b/main/cel.c
index 0e08e95c65..2c6dd9b27d 100644
--- a/main/cel.c
+++ b/main/cel.c
@@ -440,7 +440,7 @@ struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event
 	tchan->cid.cid_name = ast_strdup(record.caller_id_name);
 	tchan->cid.cid_num = ast_strdup(record.caller_id_num);
 	tchan->cid.cid_ani = ast_strdup(record.caller_id_ani);
-	tchan->cid.cid_rdnis = ast_strdup(record.caller_id_rdnis);
+	tchan->redirecting.from.number = ast_strdup(record.caller_id_rdnis);
 	tchan->cid.cid_dnid = ast_strdup(record.caller_id_dnid);
 
 	ast_copy_string(tchan->exten, record.extension, sizeof(tchan->exten));
@@ -533,7 +533,7 @@ int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event
 			AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->cid.cid_name, ""),
 			AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->cid.cid_num, ""),
 			AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->cid.cid_ani, ""),
-			AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->cid.cid_rdnis, ""),
+			AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->redirecting.from.number, ""),
 			AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->cid.cid_dnid, ""),
 			AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, chan->exten,
 			AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, chan->context,
diff --git a/main/channel.c b/main/channel.c
index d3c9e9dd5f..5be973a49b 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -1464,9 +1464,7 @@ static void free_cid(struct ast_callerid *cid)
 		ast_free(cid->cid_name);	
 	if (cid->cid_ani)
 		ast_free(cid->cid_ani);
-	if (cid->cid_rdnis)
-		ast_free(cid->cid_rdnis);
-	cid->cid_dnid = cid->cid_num = cid->cid_name = cid->cid_ani = cid->cid_rdnis = NULL;
+	cid->cid_dnid = cid->cid_num = cid->cid_name = cid->cid_ani = NULL;
 	ast_party_subaddress_free(&cid->subaddress);
 	ast_party_subaddress_free(&cid->dialed_subaddress);
 }
@@ -6973,18 +6971,6 @@ void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_part
 	ast_channel_lock(chan);
 
 	ast_party_id_set(&chan->redirecting.from, &redirecting->from);
-	if (redirecting->from.number
-		&& redirecting->from.number != chan->redirecting.from.number) {
-		/*
-		 * Must move string to ast_channel.cid.cid_rdnis until it goes away.
-		 */
-		if (chan->cid.cid_rdnis) {
-			ast_free(chan->cid.cid_rdnis);
-		}
-		chan->cid.cid_rdnis = chan->redirecting.from.number;
-		chan->redirecting.from.number = NULL;
-	}
-
 	ast_party_id_set(&chan->redirecting.to, &redirecting->to);
 	chan->redirecting.reason = redirecting->reason;
 	chan->redirecting.count = redirecting->count;
diff --git a/main/dial.c b/main/dial.c
index 31ac899f8a..7435d926cf 100644
--- a/main/dial.c
+++ b/main/dial.c
@@ -174,15 +174,6 @@ static const struct ast_option_types {
 	{ AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*!< Terminator of list */
 };
 
-/*! \brief free the buffer if allocated, and set the pointer to the second arg */
-#define S_REPLACE(s, new_val) \
-	do {                      \
-		if (s) {              \
-			free(s);          \
-		}                     \
-		s = (new_val);        \
-	} while (0)
-
 /*! \brief Maximum number of channels we can watch at a time */
 #define AST_MAX_WATCHERS 256
 
@@ -275,7 +266,6 @@ static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_chann
 		ast_channel_datastore_inherit(chan, channel->owner);
 
 		/* Copy over callerid information */
-		S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
 		ast_party_redirecting_copy(&channel->owner->redirecting, &chan->redirecting);
 
 		channel->owner->cid.cid_tns = chan->cid.cid_tns;
diff --git a/res/res_agi.c b/res/res_agi.c
index cf1679daf9..d0ba17e8d2 100644
--- a/res/res_agi.c
+++ b/res/res_agi.c
@@ -1633,7 +1633,7 @@ static void setup_env(struct ast_channel *chan, char *request, int fd, int enhan
 	ast_agi_send(fd, chan, "agi_callington: %d\n", chan->cid.cid_ton);
 	ast_agi_send(fd, chan, "agi_callingtns: %d\n", chan->cid.cid_tns);
 	ast_agi_send(fd, chan, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
-	ast_agi_send(fd, chan, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
+	ast_agi_send(fd, chan, "agi_rdnis: %s\n", S_OR(chan->redirecting.from.number, "unknown"));
 
 	/* Context information */
 	ast_agi_send(fd, chan, "agi_context: %s\n", chan->context);
diff --git a/res/snmp/agent.c b/res/snmp/agent.c
index df4523339c..b902affcc2 100644
--- a/res/snmp/agent.c
+++ b/res/snmp/agent.c
@@ -451,8 +451,8 @@ static u_char *ast_var_channels_table(struct variable *vp, oid *name, size_t *le
 		}
 		break;
 	case ASTCHANCIDRDNIS:
-		if (chan->cid.cid_rdnis) {
-			strncpy(string_ret, chan->cid.cid_rdnis, sizeof(string_ret));
+		if (chan->redirecting.from.number) {
+			strncpy(string_ret, chan->redirecting.from.number, sizeof(string_ret));
 			string_ret[sizeof(string_ret) - 1] = '\0';
 			*var_len = strlen(string_ret);
 			ret = (u_char *)string_ret;
-- 
GitLab