Index: chan_sip.c
===================================================================
--- chan_sip.c	(revision 7380)
+++ chan_sip.c	(working copy)
@@ -688,6 +688,7 @@ static struct sip_pvt {
 	struct ast_rtp *vrtp;			/*!< Video RTP session */
 	struct sip_pkt *packets;		/*!< Packets scheduled for re-transmission */
 	struct sip_history *history;		/*!< History of this SIP dialog */
+	struct sip_history *history_tail;	/*!< Last history record of this SIP dialog */
 	struct ast_variable *chanvars;		/*!< Channel variables to set for call */
 	struct sip_pvt *next;			/*!< Next call in chain */
 	struct sip_invite_param *options;	/*!< Options for INVITE */
@@ -1123,38 +1124,46 @@ static int ast_sip_ouraddrfor(struct in_
 
 /*! \brief  append_history: Append to SIP dialog history */
 /*	Always returns 0 */
-static int append_history(struct sip_pvt *p, const char *event, const char *data)
+#define append_history(p, event, fmt , args... )	append_history_full(p, "%-15s " fmt, event, ## args)
+
+static int append_history_full(struct sip_pvt *p, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
+
+static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
 {
-	struct sip_history *hist, *prev;
 	char *c;
+	struct sip_history *hist = malloc(sizeof(struct sip_history));
 
-	if (!recordhistory || !p)
-		return 0;
-	if(!(hist = malloc(sizeof(struct sip_history)))) {
+	if (!hist) {
 		ast_log(LOG_WARNING, "Can't allocate memory for history");
-		return 0;
+		return;
 	}
 	memset(hist, 0, sizeof(struct sip_history));
-	snprintf(hist->event, sizeof(hist->event), "%-15s %s", event, data);
-	/* Trim up nicely */
+	vsnprintf(hist->event, sizeof(hist->event), fmt, ap);
+
+	/* Trim up everything after \r or \n */
 	c = hist->event;
-	while(*c) {
-		if ((*c == '\r') || (*c == '\n')) {
-			*c = '\0';
-			break;
-		}
-		c++;
-	}
+	strsep(&c, "\r\n");
+
 	/* Enqueue into history */
-	prev = p->history;
-	if (prev) {
-		while(prev->next)
-			prev = prev->next;
-		prev->next = hist;
-	} else {
+	if (p->history)
+		p->history_tail->next = hist;
+	else
 		p->history = hist;
-	}
-	return 0;
+	p->history_tail = hist;
+}
+
+static int append_history_full(struct sip_pvt *p, const char *fmt, ...)
+{
+        va_list ap;
+
+	if (!recordhistory || !p)
+		return 0;
+        va_start(ap, fmt);
+        append_history_va(p, fmt, ap);
+        va_end(ap);
+
+        return 0;
 }
 
 /*! \brief  retrans_pkt: Retransmit SIP message if no answer ---*/
@@ -1168,8 +1177,6 @@ static int retrans_pkt(void *data)
 	ast_mutex_lock(&pkt->owner->lock);
 
 	if (pkt->retrans < MAX_RETRANS) {
-		char buf[80];
-
 		pkt->retrans++;
  		if (!pkt->timer_t1) {	/* Re-schedule using timer_a and timer_t1 */
 			if (sipdebug && option_debug > 3)
@@ -1201,9 +1208,8 @@ static int retrans_pkt(void *data)
 			else
 				ast_verbose("Retransmitting #%d (no NAT) to %s:%d:\n%s\n---\n", pkt->retrans, ast_inet_ntoa(iabuf, sizeof(iabuf), pkt->owner->sa.sin_addr), ntohs(pkt->owner->sa.sin_port), pkt->data);
 		}
-		snprintf(buf, sizeof(buf), "ReTx %d", reschedule);
 
-		append_history(pkt->owner, buf, pkt->data);
+		append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
 		__sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
 		ast_mutex_unlock(&pkt->owner->lock);
 		return  reschedule;
@@ -1214,7 +1220,7 @@ static int retrans_pkt(void *data)
 		if ((pkt->method == SIP_OPTIONS) && sipdebug)
 			ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) \n", pkt->owner->callid);
 	}
-	append_history(pkt->owner, "MaxRetries", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
+	append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
  		
 	pkt->retransid = -1;
 
@@ -1326,13 +1332,10 @@ static int __sip_autodestruct(void *data
 /*! \brief  sip_scheddestroy: Schedule destruction of SIP call ---*/
 static int sip_scheddestroy(struct sip_pvt *p, int ms)
 {
-	char tmp[80];
 	if (sip_debug_test_pvt(p))
 		ast_verbose("Scheduling destruction of call '%s' in %d ms\n", p->callid, ms);
-	if (recordhistory) {
-		snprintf(tmp, sizeof(tmp), "%d ms", ms);
-		append_history(p, "SchedDestroy", tmp);
-	}
+	if (recordhistory)
+		append_history(p, "SchedDestroy", "%d ms", ms);
 
 	if (p->autokillid > -1)
 		ast_sched_del(sched, p->autokillid);
@@ -1464,31 +1467,22 @@ static void parse_copy(struct sip_reques
 static int send_response(struct sip_pvt *p, struct sip_request *req, int reliable, int seqno)
 {
 	int res;
-	char iabuf[INET_ADDRSTRLEN];
-	struct sip_request tmp;
-	char tmpmsg[80];
 
 	if (sip_debug_test_pvt(p)) {
+		char iabuf[INET_ADDRSTRLEN];
 		if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
 			ast_verbose("%sTransmitting (NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port), req->data);
 		else
 			ast_verbose("%sTransmitting (no NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), req->data);
 	}
-	if (reliable) {
-		if (recordhistory) {
-			parse_copy(&tmp, req);
-			snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
-			append_history(p, "TxRespRel", tmpmsg);
-		}
-		res = __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable > 1), req->method);
-	} else {
-		if (recordhistory) {
-			parse_copy(&tmp, req);
-			snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
-			append_history(p, "TxResp", tmpmsg);
-		}
-		res = __sip_xmit(p, req->data, req->len);
-	}
+	if (recordhistory) {
+		struct sip_request tmp;
+		parse_copy(&tmp, req);
+		append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
+	}
+	res = (reliable) ?
+		__sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable > 1), req->method) :
+		__sip_xmit(p, req->data, req->len);
 	if (res > 0)
 		return 0;
 	return res;
@@ -1498,31 +1492,22 @@ static int send_response(struct sip_pvt 
 static int send_request(struct sip_pvt *p, struct sip_request *req, int reliable, int seqno)
 {
 	int res;
-	char iabuf[INET_ADDRSTRLEN];
-	struct sip_request tmp;
-	char tmpmsg[80];
 
 	if (sip_debug_test_pvt(p)) {
+		char iabuf[INET_ADDRSTRLEN];
 		if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
 			ast_verbose("%sTransmitting (NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port), req->data);
 		else
 			ast_verbose("%sTransmitting (no NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), req->data);
 	}
-	if (reliable) {
-		if (recordhistory) {
-			parse_copy(&tmp, req);
-			snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
-			append_history(p, "TxReqRel", tmpmsg);
-		}
-		res = __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable > 1), req->method);
-	} else {
-		if (recordhistory) {
-			parse_copy(&tmp, req);
-			snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
-			append_history(p, "TxReq", tmpmsg);
-		}
-		res = __sip_xmit(p, req->data, req->len);
-	}
+	if (recordhistory) {
+		struct sip_request tmp;
+		parse_copy(&tmp, req);
+		append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
+	}
+	res = (reliable) ?
+		__sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable > 1), req->method) :
+		__sip_xmit(p, req->data, req->len);
 	return res;
 }
 
@@ -3628,7 +3616,7 @@ static int process_sdp(struct sip_pvt *p
 
 	/* Manager Hold and Unhold events must be generated, if necessary */
 	if (sin.sin_addr.s_addr && !sendonly) {	        
-	        append_history(p, "Unhold", req->data);
+	        append_history(p, "Unhold", "%s", req->data);
 
 		if (callevents && ast_test_flag(p, SIP_CALL_ONHOLD)) {
 			manager_event(EVENT_FLAG_CALL, "Unhold",
@@ -3641,7 +3629,7 @@ static int process_sdp(struct sip_pvt *p
 		ast_clear_flag(p, SIP_CALL_ONHOLD);
 	} else {	        
 		/* No address for RTP, we're on hold */
-	        append_history(p, "Hold", req->data);
+	        append_history(p, "Hold", "%s", req->data);
 
 	        if (callevents && !ast_test_flag(p, SIP_CALL_ONHOLD)) {
 			manager_event(EVENT_FLAG_CALL, "Hold",
@@ -5230,11 +5218,8 @@ static int sip_reregister(void *data) 
 	if (!r)
 		return 0;
 
-	if (r->call && recordhistory) {
-		char tmp[80];
-		snprintf(tmp, sizeof(tmp), "Account: %s@%s", r->username, r->hostname);
-		append_history(r->call, "RegistryRenew", tmp);
-	}
+	if (r->call && recordhistory)
+		append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
 	/* Since registry's are only added/removed by the the monitor thread, this
 	   may be overkill to reference/dereference at all here */
 	if (sipdebug)
@@ -5335,11 +5320,8 @@ static int transmit_register(struct sip_
 			ast_log(LOG_WARNING, "Unable to allocate registration call\n");
 			return 0;
 		}
-		if (recordhistory) {
-			char tmp[80];
-			snprintf(tmp, sizeof(tmp), "Account: %s@%s", r->username, r->hostname);
-			append_history(p, "RegistryInit", tmp);
-		}
+		if (recordhistory)
+			append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
 		/* Find address to hostname */
 		if (create_addr(p, r->hostname)) {
 			/* we have what we hope is a temporary network error,
@@ -6316,7 +6266,7 @@ static int cb_extensionstate(char *conte
 		ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
 		p->stateid = -1;
 		p->subscribed = NONE;
-		append_history(p, "Subscribestatus", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
+		append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
 		break;
 	default:	/* Tell user */
 		p->laststate = state;
@@ -8853,11 +8801,8 @@ static int do_register_auth(struct sip_p
  			/* No old challenge */
 		return -1;
 	}
-	if (recordhistory) {
-		char tmp[80];
-		snprintf(tmp, sizeof(tmp), "Try: %d", p->authtries);
-		append_history(p, "RegistryAuth", tmp);
-	}
+	if (recordhistory)
+		append_history(p, "RegistryAuth", "Try: %d", p->authtries);
  	if (sip_debug_test_pvt(p) && p->registry)
  		ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
 	return transmit_register(p->registry, SIP_REGISTER, digest, respheader); 
@@ -10800,7 +10746,7 @@ static int handle_request_subscribe(stru
 		} else {
 			transmit_response(p, "200 OK", req);
 			transmit_state_notify(p, firststate, 1, 1);	/* Send first notification */
-			append_history(p, "Subscribestatus", ast_extension_state2str(firststate));
+			append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
 		}
 		if (!p->expiry)
 			ast_set_flag(p, SIP_NEEDDESTROY);	
@@ -11095,12 +11041,8 @@ retrylock:
 			goto retrylock;
 		}
 		memcpy(&p->recv, &sin, sizeof(p->recv));
-		if (recordhistory) {
-			char tmp[80];
-			/* This is a response, note what it was for */
-			snprintf(tmp, sizeof(tmp), "%s / %s", req.data, get_header(&req, "CSeq"));
-			append_history(p, "Rx", tmp);
-		}
+		if (recordhistory) /* This is a response, note what it was for */
+			append_history(p, "Rx", "%s / %s", req.data, get_header(&req, "CSeq"));
 		nounlock = 0;
 		if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
 			/* Request failed */
