Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 381718)
+++ channels/chan_skinny.c	(working copy)
@@ -1358,6 +1358,9 @@
 	int aa_beep;
 	int aa_mute;
 	int dialer_sched;
+	char *origtonum;
+	char *origtoname;
+	int origtoset;
 
 	AST_LIST_ENTRY(skinny_subchannel) list;
 	struct skinny_subchannel *related;
@@ -2423,7 +2426,7 @@
 }
 
 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 +2451,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 +2464,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);
 }
 
@@ -2521,7 +2531,7 @@
 	if (d->protocolversion < 17) {
 		transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection);
 	} 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);
 	}
 }
 
@@ -2568,7 +2578,7 @@
 	if (d->protocolversion < 17) {
 		transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection);
 	} else {
-		transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->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);
 	}
 }
 
@@ -3370,6 +3380,14 @@
 		return;
 	}
 
+	if (!sub->origtoset) {
+		sub->origtoset = 1;
+		sub->origtonum = ast_strdup(l->lastnumberdialed);
+		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 +4769,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);
@@ -5165,6 +5185,7 @@
 			sub->calldirection = direction;
 			sub->aa_sched = 0;
 			sub->dialer_sched = 0;
+			sub->origtoset = 0;
 
 			if (subline) {
 				sub->subline = subline;
