>From f807c41f382cbb988d73fe97091e11d36a689bcb Mon Sep 17 00:00:00 2001
From: lucian balanceanu <lucian.balanceanu@1and1.ro>
Date: Wed, 27 Aug 2014 14:06:13 +0300
Subject: [PATCH] core: onsend_route also called for replies

---
 forward.c             |   21 +++++++++++++++------
 modules/sl/sl_funcs.c |   12 ++++++++++++
 modules/tm/t_reply.c  |   36 +++++++++++++++++++++++++++++++++---
 3 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/forward.c b/forward.c
index 849325f..dab5897 100644
--- a/forward.c
+++ b/forward.c
@@ -777,6 +777,7 @@ static int do_forward_reply(struct sip_msg* msg, int mode)
 #endif
 	init_dest_info(&dst);
 	new_buf=0;
+	struct ip_addr ip; /* debugging */
 	/*check if first via host = us */
 	if (check_via){
 		if (check_self(&msg->via1->host,
@@ -851,11 +852,6 @@ static int do_forward_reply(struct sip_msg* msg, int mode)
 
 	apply_force_send_socket(&dst, msg);
 
-	if (msg_send(&dst, new_buf, new_len)<0)
-	{
-		STATS_RPL_FWD_DROP();
-		goto error;
-	}
 	/* call onsend_route */
 	if(dst.send_sock == NULL) {
 		dst.send_sock=get_send_socket(msg, &dst.to, dst.proto);
@@ -864,7 +860,20 @@ static int do_forward_reply(struct sip_msg* msg, int mode)
 			goto done;
 		}
 	}
-	run_onsend(msg, &dst, new_buf, new_len);
+
+	if (run_onsend(msg, &dst, new_buf, new_len)==0){
+		su2ip_addr(&ip, &(dst.to));
+		LOG(L_ERR, "forward_reply: reply to %s:%d(%d) dropped"
+				" (onsend_route)\n", ip_addr2a(&ip),
+					su_getport(&(dst.to)), dst.proto);
+		goto error; /* error ? */
+	}
+
+	if (msg_send(&dst, new_buf, new_len)<0)
+	{
+		STATS_RPL_FWD_DROP();
+		goto error;
+	}
 
 	done:
 #ifdef STATS
diff --git a/modules/sl/sl_funcs.c b/modules/sl/sl_funcs.c
index bfaa76f..a56aae9 100644
--- a/modules/sl/sl_funcs.c
+++ b/modules/sl/sl_funcs.c
@@ -149,6 +149,7 @@ int sl_reply_helper(struct sip_msg *msg, int code, char *reason, str *tag)
 	struct bookmark dummy_bm;
 	int backup_mhomed, ret;
 	str text;
+	struct ip_addr ip; /* debugging */
 
 	int backup_rt;
 	struct run_act_ctx ctx;
@@ -221,6 +222,17 @@ int sl_reply_helper(struct sip_msg *msg, int code, char *reason, str *tag)
 	dst.comp=msg->via1->comp_no;
 #endif
 	dst.send_flags=msg->rpl_send_flags;
+
+	//
+	//
+	if (run_onsend(msg, &dst, buf.s, buf.len)==0){
+		su2ip_addr(&ip, &(dst.to));
+		LOG(L_ERR, "forward_reply: reply to %s:%d(%d) dropped"
+				" (onsend_route)\n", ip_addr2a(&ip),
+					su_getport(&(dst.to)), dst.proto);
+		goto error; /* error ? */
+	}
+
 	ret = msg_send(&dst, buf.s, buf.len);
 	mhomed=backup_mhomed;
 
diff --git a/modules/tm/t_reply.c b/modules/tm/t_reply.c
index f258c75..eccd0e7 100644
--- a/modules/tm/t_reply.c
+++ b/modules/tm/t_reply.c
@@ -595,6 +595,8 @@ static int _reply_light( struct cell *trans, char* buf, unsigned int len,
 	int rt, backup_rt;
 	struct run_act_ctx ctx;
 	struct sip_msg pmsg;
+	int msg_built = 0;
+	struct ip_addr ip; /* debugging */
 
 	init_cancel_info(&cancel_data);
 	if (!buf)
@@ -682,6 +684,31 @@ static int _reply_light( struct cell *trans, char* buf, unsigned int len,
 	if (unlikely(!trans->uas.response.dst.send_sock)) {
 		LOG(L_ERR, "ERROR: _reply_light: no resolved dst to send reply to\n");
 	} else {
+
+		/* call onsend_route */
+		/*
+		if(dst.send_sock == NULL) {
+			dst.send_sock=get_send_socket(msg, &dst.to, dst.proto);
+			if (dst.send_sock==0){
+				LOG(L_ERR, "forward_reply: ERROR: cannot forward reply\n");
+				goto done;
+			}
+		}
+		*/
+		if (likely(build_sip_msg_from_buf(&pmsg, buf, len, inc_msg_no()) == 0))
+		{
+			msg_built = 1;
+			if (run_onsend(&pmsg, &rb->dst, buf, len)==0){
+				su2ip_addr(&ip, &(rb->dst.to));
+				LOG(L_INFO, "_reply_light: reply to %s:%d(%d) dropped"
+						" (onsend_route)\n", ip_addr2a(&ip),
+							su_getport(&(rb->dst.to)), rb->dst.proto);
+				free_sip_msg(&pmsg);
+				//TODO - should this be error3?
+				goto error2; /* error? */
+			}
+		}
+
 		if (likely(SEND_PR_BUFFER( rb, buf, len )>=0)){
 			if (unlikely(code>=200 && !is_local(trans) &&
 						has_tran_tmcbs(trans, TMCB_RESPONSE_OUT)) ){
@@ -702,7 +729,7 @@ static int _reply_light( struct cell *trans, char* buf, unsigned int len,
 			rt = route_lookup(&event_rt, "tm:local-response");
 			if (unlikely(rt >= 0 && event_rt.rlist[rt] != NULL))
 			{
-				if (likely(build_sip_msg_from_buf(&pmsg, buf, len, inc_msg_no()) == 0))
+				if (likely(msg_built == 1))
 				{
 					struct onsend_info onsnd_info;
 
@@ -718,15 +745,18 @@ static int _reply_light( struct cell *trans, char* buf, unsigned int len,
 					run_top_route(event_rt.rlist[rt], &pmsg, 0);
 					set_route_type(backup_rt);
 					p_onsend=0;
-
-					free_sip_msg(&pmsg);
 				}
 			}
 
 		}
+
+		if (msg_built)
+			free_sip_msg(&pmsg);
+
 		DBG("DEBUG: reply sent out. buf=%p: %.20s..., shmem=%p: %.20s\n",
 			buf, buf, rb->buffer, rb->buffer );
 	}
+
 	if (code>=200) {
 		/* start wait timer after finishing with t so that this function can
 		 * be safely called from a fr_timer which allows quick timer dels
-- 
1.7.4.1

