Index: chan_sip.c
===================================================================
--- chan_sip.c	(revision 12343)
+++ chan_sip.c	(working copy)
@@ -614,6 +614,7 @@
 	int method;				/*!< SIP method that opened this dialog */
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(callid);	/*!< Global CallID */
+		AST_STRING_FIELD(calledid);	/*!< Called Party RPID Header String */
 		AST_STRING_FIELD(randdata);	/*!< Random data */
 		AST_STRING_FIELD(accountcode);	/*!< Account code */
 		AST_STRING_FIELD(realm);	/*!< Authorization realm */
@@ -909,6 +910,7 @@
 
 /*---------------------------- Forward declarations of functions in chan_sip.c */
 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req);
+static int transmit_response_with_calledrpid(struct sip_pvt *p, char *msg, struct sip_request *req);
 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported);
 static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, struct sip_request *req, const char *rand, int reliable, const char *header, int stale);
@@ -2764,7 +2766,11 @@
 			if (!ast_test_flag(p, SIP_PROGRESS_SENT) ||
 			    (ast_test_flag(p, SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {				
 				/* Send 180 ringing if out-of-band seems reasonable */
-				transmit_response(p, "180 Ringing", &p->initreq);
+				if (!ast_strlen_zero(p->calledid)) {
+					transmit_response_with_calledrpid(p, "180 Ringing", &p->initreq);
+				} else {
+					transmit_response(p, "180 Ringing", &p->initreq);
+				}	
 				ast_set_flag(p, SIP_RINGING);
 				if (ast_test_flag(p, SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
 					break;
@@ -4327,6 +4333,17 @@
 	add_header(req, "Date", tmpdat);
 }
 
+/*! \brief  transmit_response_with_calledrpid: Transmit response, no retransmits */
+static int transmit_response_with_calledrpid(struct sip_pvt *p, char *msg, struct sip_request *req) 
+{
+	struct sip_request resp;
+	respprep(&resp, p, msg, req);
+	append_date(&resp);
+	add_header(&resp, "Remote-Party-ID", p->calledid);
+	add_blank_header(&resp);
+	return send_response(p, &resp, 0, 0);
+}
+
 /*! \brief Append date and content length before transmitting response */
 static int transmit_response_with_date(struct sip_pvt *p, char *msg, struct sip_request *req)
 {
@@ -4668,6 +4685,8 @@
 		return -1;
 	}
 	respprep(&resp, p, msg, req);
+	if (!ast_strlen_zero(p->calledid))
+		add_header(&resp, "Remote-Party-ID", p->calledid);
 	if (p->rtp) {
 		add_sdp(&resp, p);
 	} else {
@@ -12842,6 +12861,19 @@
 "Adding the wrong headers may jeopardize the SIP dialog.\n"
 "Always returns 0\n";
 
+static char *app_sipcalledrpid = "SIPCalledRPID";
+static char *synopsis_sipcalledrpid = "Set the RPID header for Called Party Identification";
+ 
+static char *descrip_sipcalledrpid = ""
+"  SIPCalledRPID(name,number): \n"
+"Sets the Remote-Party-ID header information to be sent to a call\n"
+"originator on further progress through this extension. Many SIP\n"
+"phones use this header to display the name and number of the party\n"
+"which has been called from the telephone. This progress may be\n"
+"updated throughout the call progress whenever Asterisk sends a new\n"
+"180 Ringing, 183 Call Progress, or 200 OK response to the calling\n"
+"channel."
+"Always returns 0\n";
 
 /*! \brief Set the DTMFmode for an outbound SIP call (application) */
 static int sip_dtmfmode(struct ast_channel *chan, void *data)
@@ -12926,6 +12958,45 @@
 	return 0;
 }
 
+/*! \brief  sip_calledrpid: Set the value of the RPID header for called party identification ---*/
+static int sip_calledrpid(struct ast_channel *chan, void *data)
+{
+	struct sip_pvt *p;
+	char *argv, *name = NULL, *number = NULL;
+	char rpid[256];
+	
+	argv = ast_strdupa(data);
+	if (!argv) {
+		ast_log(LOG_DEBUG, "Memory allocation failed\n");
+		return 0;
+	}
+
+	if (strchr (argv, '|') ) {	/* Pick out argumenets */
+		name = strsep (&argv, "|");
+		number = strsep (&argv, "\0");
+	}
+
+	if (!name || !number) {
+		ast_log(LOG_DEBUG, "SIPCalledRPID: Ignoring command, Syntax error in argument\n");
+		return 0;
+	}
+
+	ast_mutex_lock(&chan->lock);
+
+	if (chan->tech != &sip_tech) {
+		ast_log(LOG_WARNING, "This application is only useful for calls originated from SIP UA's\n");
+		ast_mutex_unlock(&chan->lock);
+		return 0;
+	}
+	
+	p = chan->tech_pvt;
+	sprintf(rpid, "\"%s\" <sip:%s@foo.com>;party=called;id-type=subscriber;screen=yes",name, number);
+	ast_string_field_set(p, calledid, rpid);
+	
+	ast_mutex_unlock(&chan->lock);
+	return 0;
+}
+
 /*! \brief Transfer call before connect with a 302 redirect
 \note	Called by the transfer() dialplan application through the sip_transfer()
 	pbx interface function if the call is in ringing state 
@@ -13167,6 +13238,7 @@
 	/* Register dialplan applications */
 	ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
 	ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
+	ast_register_application(app_sipcalledrpid, sip_calledrpid, synopsis_sipcalledrpid, descrip_sipcalledrpid);
 
 	/* Register dialplan functions */
 	ast_custom_function_register(&sip_header_function);
@@ -13203,6 +13275,7 @@
 
 	ast_unregister_application(app_dtmfmode);
 	ast_unregister_application(app_sipaddheader);
+	ast_unregister_application(app_sipcalledrpid);
 
 	ast_cli_unregister_multiple(my_clis, sizeof(my_clis) / sizeof(my_clis[0]));
 
