From 2bd8afde5c4836c33300f3aa0b539762b2105aca Mon Sep 17 00:00:00 2001
From: lucian balanceanu <lucian.balanceanu@1and1.ro>
Date: Wed, 17 Sep 2014 18:22:19 +0300
Subject: [PATCH]   * Temp onsend_route_reply patch

---
 cfg.lex                            |    3 +++
 cfg.y                              |    3 +++
 forward.c                          |   23 +++++++++++++++++------
 globals.h                          |    2 ++
 main.c                             |    5 +++++
 onsend.h                           |    3 ++-
 utils/misc/vim/syntax/kamailio.vim |    2 +-
 7 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/cfg.lex b/cfg.lex
index 10766b2..5b3e8d5 100644
--- a/cfg.lex
+++ b/cfg.lex
@@ -344,6 +344,7 @@ FORK_DELAY	fork_delay
 MODINIT_DELAY	modinit_delay
 LOGSTDERROR	log_stderror
 LOGFACILITY	log_facility
+ONSEND_RT_REPLY onsend_route_reply
 LOGNAME		log_name
 LOGCOLOR	log_color
 LOGPREFIX	log_prefix
@@ -708,6 +709,8 @@ IMPORTFILE      "import_file"
 <INITIAL>{MODINIT_DELAY}	{ count(); yylval.strval=yytext; return MODINIT_DELAY; }
 <INITIAL>{LOGSTDERROR}	{ yylval.strval=yytext; return LOGSTDERROR; }
 <INITIAL>{LOGFACILITY}	{ yylval.strval=yytext; return LOGFACILITY; }
+<INITIAL>{ONSEND_RT_REPLY}	{  yylval.intval=atoi(yytext);
+						yy_number_str=yytext; return NUMBER; }
 <INITIAL>{LOGNAME}	{ yylval.strval=yytext; return LOGNAME; }
 <INITIAL>{LOGCOLOR}	{ yylval.strval=yytext; return LOGCOLOR; }
 <INITIAL>{LOGPREFIX}	{ yylval.strval=yytext; return LOGPREFIX; }
diff --git a/cfg.y b/cfg.y
index 1e52289..5df76e8 100644
--- a/cfg.y
+++ b/cfg.y
@@ -393,6 +393,7 @@ extern char *default_routename;
 %token MODINIT_DELAY
 %token LOGSTDERROR
 %token LOGFACILITY
+%token ONSEND_RT_REPLY
 %token LOGNAME
 %token LOGCOLOR
 %token LOGPREFIX
@@ -840,6 +841,8 @@ assign_stm:
 			default_core_cfg.log_facility=i_tmp;
 	}
 	| LOGFACILITY EQUAL error { yyerror("ID expected"); }
+	| ONSEND_RT_REPLY EQUAL NUMBER { onsend_route_reply=$3; }
+	| ONSEND_RT_REPLY EQUAL error { yyerror("int value expected"); }	
 	| LOGNAME EQUAL STRING { log_name=$3; }
 	| LOGNAME EQUAL error { yyerror("string value expected"); }
 	| LOGCOLOR EQUAL NUMBER { log_color=$3; }
diff --git a/forward.c b/forward.c
index 849325f..b3f41ec 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,22 @@ static int do_forward_reply(struct sip_msg* msg, int mode)
 			goto done;
 		}
 	}
-	run_onsend(msg, &dst, new_buf, new_len);
+
+	if (onsend_reply_enabled(SIP_REPLY)){
+		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/globals.h b/globals.h
index 0e02c7c..635816d 100644
--- a/globals.h
+++ b/globals.h
@@ -49,6 +49,8 @@ extern int config_check;
 extern char* stat_file;
 extern unsigned short port_no;
 
+extern int onsend_route_reply;
+
 extern time_t up_since;
 extern pid_t creator_pid;  /* pid of first process before daemonization */
 extern int uid;
diff --git a/main.c b/main.c
index be75335..ecb55b2 100644
--- a/main.c
+++ b/main.c
@@ -378,6 +378,8 @@ int dont_fork = 0;
 int dont_daemonize = 0;
 int log_stderr = 0;
 int log_color = 0;
+/* onsend_route is executed for replies*/
+int onsend_route_reply = 0;
 /* set custom app name for syslog printing */
 char *log_name = 0;
 char *log_prefix_fmt = 0;
@@ -1433,6 +1435,9 @@ int main_loop(void)
 			LOG(L_ERR, "main_dontfork: init_child failed\n");
 			goto error;
 		}
+
+		LOG(L_ERR, "!!!!!!!onsend_route_reply=<%d> !!!!!!!!\n", onsend_route_reply);
+
 		return udp_rcv_loop();
 	}else{ /* fork: */
 
diff --git a/onsend.h b/onsend.h
index 0b2eb08..a6dc587 100644
--- a/onsend.h
+++ b/onsend.h
@@ -52,7 +52,7 @@ struct onsend_info{
 
 extern struct onsend_info* p_onsend;
 
-
+#define onsend_reply_enabled(rtype) (onsend_rt.rlist[DEFAULT_RT]?((rtype==SIP_REPLY)?onsend_route_reply:1):0)
 #define get_onsend_info()	(p_onsend)
 
 /*
@@ -70,6 +70,7 @@ static inline int run_onsend(struct sip_msg* orig_msg, struct dest_info* dst,
 	snd_flags_t rpl_snd_flags_bak;
 	
 	ret=1;
+
 	if (onsend_rt.rlist[DEFAULT_RT]){
 		onsnd_info.to=&dst->to;
 		onsnd_info.send_sock=dst->send_sock;
diff --git a/utils/misc/vim/syntax/kamailio.vim b/utils/misc/vim/syntax/kamailio.vim
index 01eeb72..d1e84ac 100644
--- a/utils/misc/vim/syntax/kamailio.vim
+++ b/utils/misc/vim/syntax/kamailio.vim
@@ -53,7 +53,7 @@ syn keyword kamailioCoreValue		udp UDP tcp TCP tls TLS sctp SCTP ws WS wss WSS i
 
 syn keyword	kamailioCoreFunction	forward forward_tcp forward_udp forward_tls forward_sctp send send_tcp log error exec force_rport add_rport force_tcp_alias add_tcp_alias udp_mtu udp_mtu_try_proto setflag resetflag isflagset flags bool setavpflag resetavpflag isavpflagset avpflags rewritehost sethost seth rewritehostport sethostport sethp rewritehostporttrans sethostporttrans sethpt rewriteuser setuser setu rewriteuserpass setuserpass setup rewriteport setport setp rewriteuri seturi revert_uri prefix strip strip_tail userphone append_branch set_advertised_address set_advertised_port force_send_socket remove_branch clear_branches cfg_select cfg_reset contained
 
-syn keyword	kamailioCoreParameter debug fork log_stderror log_facility log_name log_color log_prefix listen alias auto_aliases dns rev_dns dns_try_ipv6 dns_try_naptr dns_srv_lb dns_srv_loadbalancing dns_udp_pref dns_udp_preference dns_tcp_pref dns_tcp_preference dns_tls_pref dns_tls_preference dns_sctp_pref dns_sctp_preference dns_retr_time dns_retr_no dns_servers_no dns_use_search_list dns_search_full_match dns_cache_init use_dns_cache use_dns_failover dns_cache_flags dns_cache_negative_ttl dns_cache_min_ttl dns_cache_max_ttl dns_cache_mem dns_cache_gc_interval dns_cache_del_nonexp dns_cache_delete_nonexpired dst_blacklist_init use_dst_blacklist dst_blacklist_mem dst_blacklist_expire dst_blacklist_ttl dst_blacklist_gc_interval port statistics maxbuffer children check_via phone2tel syn_branch memlog mem_log memdbg mem_dbg sip_warning server_signature reply_to_via user uid group gid chroot workdir wdir mhomed disable_tcp tcp_children tcp_accept_aliases tcp_send_timeout tcp_connect_timeout tcp_connection_lifetime tcp_poll_method tcp_max_connections tcp_no_connect tcp_source_ipv4 tcp_source_ipv6 tcp_fd_cache tcp_buf_write tcp_async tcp_conn_wq_max tcp_wq_max tcp_rd_buf_size tcp_wq_blk_size tcp_defer_accept tcp_delayed_ack tcp_syncnt tcp_linger2 tcp_keepalive tcp_keepidle tcp_keepintvl tcp_keepcnt tcp_crlf_ping disable_tls tls_disable enable_tls tls_enable tlslog tls_log tls_port_no tls_method tls_verify tls_require_certificate tls_certificate tls_private_key tls_ca_list tls_handshake_timeout tls_send_timeout disable_sctp enable_sctp sctp_children sctp_socket_rcvbuf sctp_socket_receive_buffer sctp_socket_sndbuf sctp_socket_send_buffer sctp_autoclose sctp_send_ttl sctp_send_retries socket_workers advertised_address advertised_port disable_core_dump open_files_limit shm_force_alloc mlock_pages real_time rt_prio rt_policy rt_timer1_prio rt_fast_timer_prio rt_ftimer_prio rt_timer1_policy rt_ftimer_policy rt_timer2_prio rt_stimer_prio rt_timer2_policy rt_stimer_policy mcast_loopback mcast_ttl tos pmtu_discovery exit_timeout ser_kill_timeout max_while_loops stun_refresh_interval stun_allow_stun stun_allow_fp server_id description descr desc loadpath mpath fork_delay modinit_delay http_reply_hack latency_log latency_limit_action latency_limit_db mem_join mem_safety msg_time tcp_clone_rcvbuf tls_max_connections async_workers max_recursive_level dns_naptr_ignore_rfc http_reply_parse version_table tcp_accept_no_cl advertise auto_bind_ipv6 sql_buffer_size pv_buffer_size pv_buffer_slots corelog core_log udp4_raw udp4_raw_mtu udp4_raw_ttl contained
+syn keyword	kamailioCoreParameter debug fork log_stderror log_facility onsend_route_reply log_name log_color log_prefix listen alias auto_aliases dns rev_dns dns_try_ipv6 dns_try_naptr dns_srv_lb dns_srv_loadbalancing dns_udp_pref dns_udp_preference dns_tcp_pref dns_tcp_preference dns_tls_pref dns_tls_preference dns_sctp_pref dns_sctp_preference dns_retr_time dns_retr_no dns_servers_no dns_use_search_list dns_search_full_match dns_cache_init use_dns_cache use_dns_failover dns_cache_flags dns_cache_negative_ttl dns_cache_min_ttl dns_cache_max_ttl dns_cache_mem dns_cache_gc_interval dns_cache_del_nonexp dns_cache_delete_nonexpired dst_blacklist_init use_dst_blacklist dst_blacklist_mem dst_blacklist_expire dst_blacklist_ttl dst_blacklist_gc_interval port statistics maxbuffer children check_via phone2tel syn_branch memlog mem_log memdbg mem_dbg sip_warning server_signature reply_to_via user uid group gid chroot workdir wdir mhomed disable_tcp tcp_children tcp_accept_aliases tcp_send_timeout tcp_connect_timeout tcp_connection_lifetime tcp_poll_method tcp_max_connections tcp_no_connect tcp_source_ipv4 tcp_source_ipv6 tcp_fd_cache tcp_buf_write tcp_async tcp_conn_wq_max tcp_wq_max tcp_rd_buf_size tcp_wq_blk_size tcp_defer_accept tcp_delayed_ack tcp_syncnt tcp_linger2 tcp_keepalive tcp_keepidle tcp_keepintvl tcp_keepcnt tcp_crlf_ping disable_tls tls_disable enable_tls tls_enable tlslog tls_log tls_port_no tls_method tls_verify tls_require_certificate tls_certificate tls_private_key tls_ca_list tls_handshake_timeout tls_send_timeout disable_sctp enable_sctp sctp_children sctp_socket_rcvbuf sctp_socket_receive_buffer sctp_socket_sndbuf sctp_socket_send_buffer sctp_autoclose sctp_send_ttl sctp_send_retries socket_workers advertised_address advertised_port disable_core_dump open_files_limit shm_force_alloc mlock_pages real_time rt_prio rt_policy rt_timer1_prio rt_fast_timer_prio rt_ftimer_prio rt_timer1_policy rt_ftimer_policy rt_timer2_prio rt_stimer_prio rt_timer2_policy rt_stimer_policy mcast_loopback mcast_ttl tos pmtu_discovery exit_timeout ser_kill_timeout max_while_loops stun_refresh_interval stun_allow_stun stun_allow_fp server_id description descr desc loadpath mpath fork_delay modinit_delay http_reply_hack latency_log latency_limit_action latency_limit_db mem_join mem_safety msg_time tcp_clone_rcvbuf tls_max_connections async_workers max_recursive_level dns_naptr_ignore_rfc http_reply_parse version_table tcp_accept_no_cl advertise auto_bind_ipv6 sql_buffer_size pv_buffer_size pv_buffer_slots corelog core_log udp4_raw udp4_raw_mtu udp4_raw_ttl contained
 
 syn region	kamailioBlock	start='{' end='}' contained contains=kamailioBlock,@kamailioCodeElements
 
-- 
1.7.4.1

