Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 381718)
+++ channels/chan_skinny.c	(working copy)
@@ -1358,6 +1358,8 @@
 	int aa_beep;
 	int aa_mute;
 	int dialer_sched;
+	char *origtonum;
+	char *origtoname;
 
 	AST_LIST_ENTRY(skinny_subchannel) list;
 	struct skinny_subchannel *related;
@@ -2402,7 +2404,8 @@
 }
 
 //static void transmit_callinfo(struct skinny_subchannel *sub)
-static void transmit_callinfo(struct skinny_device *d, int instance, int callid, char *fromname, char *fromnum, char *toname, char *tonum, int calldirection)
+static void transmit_callinfo(struct skinny_device *d, int instance, int callid,
+	char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
 {
 	struct skinny_req *req;
 
@@ -2413,17 +2416,24 @@
 	ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
 	ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
 	ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
+	if (origtoname) {
+		ast_copy_string(req->data.callinfo.originalCalledPartyName, origtoname, sizeof(req->data.callinfo.originalCalledPartyName));
+	}
+	if (origtonum) {
+		ast_copy_string(req->data.callinfo.originalCalledParty, origtonum, sizeof(req->data.callinfo.originalCalledParty));
+	}
+
 	req->data.callinfo.instance = htolel(instance);
 	req->data.callinfo.reference = htolel(callid);
 	req->data.callinfo.type = htolel(calldirection);
 
-	SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE to %s, to %s(%s) from %s(%s) (dir=%d) on %s(%d)\n",
-		d->name, toname, tonum, fromname, fromnum, calldirection, d->name, instance);
+	SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
+		d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
 	transmit_response(d, req);
 }
 
 static void transmit_callinfo_variable(struct skinny_device *d, int instance, int callreference,
-	char *fromname, char *fromnum, char *toname, char *tonum, int calldirection)
+	char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
 {
 	struct skinny_req *req;
 	char *strptr;
@@ -2448,10 +2458,11 @@
 	thestrings[1] = "";                     /* Appears to be origfrom */
 	if (calldirection == SKINNY_OUTGOING) {
 		thestrings[2] = tonum;
+		thestrings[3] = origtonum;
 	} else {
 		thestrings[2] = "";
+		thestrings[3] = "";
 	}
-	thestrings[3] = "";
 	thestrings[4] = "";
 	thestrings[5] = "";
 	thestrings[6] = "";
@@ -2460,21 +2471,27 @@
 	thestrings[8] = "";
 	thestrings[9] = fromname;
 	thestrings[10] = toname;
-	thestrings[11] = "";
+	thestrings[11] = origtoname;
 	thestrings[12] = "";
 
 	strptr = req->data.callinfomessagevariable.calldetails;
 
 	for(i = 0; i < 13; i++) {
-		ast_copy_string(strptr, thestrings[i], callinfostrleft);
-		strptr += strlen(thestrings[i]) + 1;
-		callinfostrleft -= strlen(thestrings[i]) + 1;
+		if (thestrings[i]) {
+			ast_copy_string(strptr, thestrings[i], callinfostrleft);
+			strptr += strlen(thestrings[i]) + 1;
+			callinfostrleft -= strlen(thestrings[i]) + 1;
+		} else {
+			ast_copy_string(strptr, "", callinfostrleft);
+			strptr++;
+			callinfostrleft--;
+		}
 	}
 
 	req->len = req->len - (callinfostrleft & ~0x3);
 
-	SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE_VARIABLE to %s, to %s(%s) from %s(%s) (dir=%d) on %s(%d)\n",
-		d->name, toname, tonum, fromname, fromnum, calldirection, d->name, instance);
+	SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE_VARIABLE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
+		d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
 	transmit_response(d, req);
 }
 
@@ -2519,9 +2536,9 @@
 	}
 
 	if (d->protocolversion < 17) {
-		transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection);
+		transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
 	} else {
-		transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection);
+		transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
 	}
 }
 
@@ -2566,9 +2583,9 @@
 	}
 
 	if (d->protocolversion < 17) {
-		transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection);
+		transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
 	} else {
-		transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection);
+		transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
 	}
 }
 
@@ -3370,6 +3387,14 @@
 		return;
 	}
 
+	if (sub->calldirection == SKINNY_OUTGOING && !sub->origtonum) {
+		/* Do not set origtonum before here or origtoname won't be set */
+		sub->origtonum = ast_strdup(sub->exten);
+		if (ast_channel_connected(c)->id.name.valid) {
+			sub->origtoname = ast_strdup(ast_channel_connected(c)->id.name.str);
+		}
+	}
+
 	if (!ast_channel_caller(c)->id.number.valid
 		|| ast_strlen_zero(ast_channel_caller(c)->id.number.str)
 		|| !ast_channel_connected(c)->id.number.valid
@@ -4751,6 +4776,8 @@
 	sub->owner = NULL;
 	ast_channel_tech_pvt_set(ast, NULL);
 	destroy_rtp(sub);
+	ast_free(sub->origtonum);
+	ast_free(sub->origtoname);
 	ast_mutex_unlock(&sub->lock);
 	ast_free(sub);
 	ast_module_unref(ast_module_info->self);
@@ -5402,7 +5429,7 @@
 				ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
 			}
 			if (sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_RINGOUT) {
-				transmit_dialednumber(d, l->lastnumberdialed, l->instance, sub->callid);
+				transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
 			}
 			if (ast_channel_state(sub->owner) != AST_STATE_UP) {
 				ast_setstate(sub->owner, AST_STATE_UP);
@@ -5557,7 +5584,7 @@
 			transmit_start_tone(d, SKINNY_ALERT, l->instance, sub->callid);
 		}
 		transmit_callstate(d, l->instance, sub->callid, SKINNY_RINGOUT);
-		transmit_dialednumber(d, l->lastnumberdialed, l->instance, sub->callid);
+		transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
 		send_displaypromptstatus(d, OCTAL_RINGOUT, "", 0, l->instance, sub->callid);
 		send_callinfo(sub);
 		sub->substate = SUBSTATE_RINGOUT;
@@ -5628,7 +5655,7 @@
 			ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
 		}
 		if (sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_RINGOUT) {
-			transmit_dialednumber(d, l->lastnumberdialed, l->instance, sub->callid);
+			transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
 		}
 		if (ast_channel_state(sub->owner) != AST_STATE_UP) {
 			ast_setstate(sub->owner, AST_STATE_UP);
