浏览代码

core: c code for executing route blocks on send out events moved to onsend.c

- helper functions for executing event_route blocks for send out cases
Daniel-Constantin Mierla 5 年之前
父节点
当前提交
05e87e66cd
共有 3 个文件被更改,包括 170 次插入69 次删除
  1. 0 1
      src/core/action.c
  2. 162 0
      src/core/onsend.c
  3. 8 68
      src/core/onsend.h

+ 0 - 1
src/core/action.c

@@ -75,7 +75,6 @@
 #endif
 #endif
 
 
 int _last_returned_code  = 0;
 int _last_returned_code  = 0;
-struct onsend_info* p_onsend=0; /* onsend route send info */
 
 
 /* current action executed from config file */
 /* current action executed from config file */
 static cfg_action_t *_cfg_crt_action = 0;
 static cfg_action_t *_cfg_crt_action = 0;

+ 162 - 0
src/core/onsend.c

@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2005 iptelorg GmbH
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+/*!
+ * \file
+ * \brief Kamailio core :: IP address handling
+ * \author andrei
+ * \ingroup core
+ * Module: \ref core
+ */
+
+
+#include "onsend.h"
+
+onsend_info_t *p_onsend=0; /* onsend route send info */
+
+
+/*
+ * returns: 0 drop the message, >= ok, <0 error (but forward the message)
+ * it also migh change dst->send_flags!
+ * WARNING: buf must be 0 terminated (to allow regex matches on it) */
+int run_onsend(sip_msg_t* orig_msg, dest_info_t* dst, char* buf, int len)
+{
+	onsend_info_t onsnd_info = {0};
+	int ret;
+	run_act_ctx_t ra_ctx;
+	run_act_ctx_t *bctx;
+	int backup_route_type;
+	snd_flags_t fwd_snd_flags_bak;
+	snd_flags_t rpl_snd_flags_bak;
+	sr_kemi_eng_t *keng = NULL;
+
+	if(orig_msg==NULL || dst==NULL || buf==NULL) {
+		LM_DBG("required parameters are not available - ignoring\n");
+		return 1;
+	}
+	ret=1;
+	// do if onsend_route{} or cfgengine exists
+	if(kemi_onsend_route_callback.len>0) {
+		keng = sr_kemi_eng_get();
+	}
+	if (onsend_rt.rlist[DEFAULT_RT] || keng){
+		onsnd_info.to=&dst->to;
+		onsnd_info.send_sock=dst->send_sock;
+		onsnd_info.buf=buf;
+		onsnd_info.len=len;
+		onsnd_info.msg=orig_msg;
+		p_onsend=&onsnd_info;
+		backup_route_type=get_route_type();
+		set_route_type(ONSEND_ROUTE);
+		if (exec_pre_script_cb(orig_msg, ONSEND_CB_TYPE)>0) {
+			/* backup orig_msg send flags */
+			fwd_snd_flags_bak=orig_msg->fwd_send_flags;
+			rpl_snd_flags_bak=orig_msg->rpl_send_flags;
+			orig_msg->fwd_send_flags=dst->send_flags; /* intial value */
+			init_run_actions_ctx(&ra_ctx);
+
+			if(keng) {
+				bctx = sr_kemi_act_ctx_get();
+				sr_kemi_act_ctx_set(&ra_ctx);
+				ret=sr_kemi_route(keng, orig_msg, ONSEND_ROUTE, NULL, NULL);
+				sr_kemi_act_ctx_set(bctx);
+			} else {
+				ret=run_actions(&ra_ctx, onsend_rt.rlist[DEFAULT_RT], orig_msg);
+			}
+
+			/* update dst send_flags */
+			dst->send_flags=orig_msg->fwd_send_flags;
+			/* restore orig_msg flags */
+			orig_msg->fwd_send_flags=fwd_snd_flags_bak;
+			orig_msg->rpl_send_flags=rpl_snd_flags_bak;
+			exec_post_script_cb(orig_msg, ONSEND_CB_TYPE);
+			if((ret==0) && !(ra_ctx.run_flags&DROP_R_F)){
+				ret = 1;
+			}
+		} else {
+			ret=0; /* drop the message */
+		}
+		set_route_type(backup_route_type);
+		p_onsend=0; /* reset it */
+	}
+	return ret;
+}
+
+/**
+ * return: 0 drop the message
+ *    >= ok
+ *    <0 error (but forward the message)
+ * it also migh change sndinfo dst->send_flags!
+ * WARNING: sndinfo buf must be 0 terminated (to allow regex matches on it)
+ */
+int run_onsend_evroute(onsend_info_t *sndinfo, int evrt, str *evcb, str *evname)
+{
+	int ret;
+	run_act_ctx_t ra_ctx;
+	run_act_ctx_t *bctx;
+	int backup_route_type;
+	snd_flags_t fwd_snd_flags_bak;
+	snd_flags_t rpl_snd_flags_bak;
+	sr_kemi_eng_t *keng = NULL;
+
+	if(sndinfo==NULL || sndinfo->dst==NULL
+			|| sndinfo->msg==NULL || sndinfo->buf==NULL) {
+		LM_DBG("required parameters are not available - ignoring\n");
+		return 1;
+	}
+	ret=1;
+	// do if onsend_route{} or cfgengine exists
+	if(evcb!=NULL && evcb->len>0) {
+		keng = sr_kemi_eng_get();
+	}
+	if (evrt<0 && keng==NULL) {
+		return 1;
+	}
+
+	p_onsend = sndinfo;
+	backup_route_type=get_route_type();
+	set_route_type(EVENT_ROUTE);
+	/* backup orig_msg send flags */
+	fwd_snd_flags_bak=sndinfo->msg->fwd_send_flags;
+	rpl_snd_flags_bak=sndinfo->msg->rpl_send_flags;
+	sndinfo->msg->fwd_send_flags=sndinfo->dst->send_flags; /* intial value */
+	init_run_actions_ctx(&ra_ctx);
+
+	if(keng) {
+		bctx = sr_kemi_act_ctx_get();
+		sr_kemi_act_ctx_set(&ra_ctx);
+		ret=sr_kemi_route(keng, sndinfo->msg, EVENT_ROUTE, evcb, evname);
+		sr_kemi_act_ctx_set(bctx);
+	} else {
+		ret=run_actions(&ra_ctx, event_rt.rlist[evrt], sndinfo->msg);
+	}
+
+	/* update dst send_flags */
+	sndinfo->dst->send_flags=sndinfo->msg->fwd_send_flags;
+	/* restore orig_msg flags */
+	sndinfo->msg->fwd_send_flags=fwd_snd_flags_bak;
+	sndinfo->msg->rpl_send_flags=rpl_snd_flags_bak;
+	if((ret==0) && !(ra_ctx.run_flags&DROP_R_F)){
+		ret = 1;
+	}
+	set_route_type(backup_route_type);
+	p_onsend=0; /* reset it */
+
+	return ret;
+}

+ 8 - 68
src/core/onsend.h

@@ -30,14 +30,15 @@
 
 
 
 
 #include "ip_addr.h"
 #include "ip_addr.h"
-#include "action.h"
-#include "route.h"
 #include "script_cb.h"
 #include "script_cb.h"
 #include "sr_compat.h"
 #include "sr_compat.h"
+#include "action.h"
+#include "route.h"
 #include "kemi.h"
 #include "kemi.h"
 
 
-typedef struct onsend_info{
-	union sockaddr_union* to;       /* dest info */
+typedef struct onsend_info {
+	dest_info_t* dst;               /* destination info */
+	union sockaddr_union* to;       /* destination address */
 	struct socket_info* send_sock;  /* local send socket */
 	struct socket_info* send_sock;  /* local send socket */
 	char* buf;                      /* outgoing buffer */
 	char* buf;                      /* outgoing buffer */
 	int len;                        /* outgoing buffer len */
 	int len;                        /* outgoing buffer len */
@@ -48,77 +49,16 @@ typedef struct onsend_info{
 
 
 extern onsend_info_t* p_onsend;
 extern onsend_info_t* p_onsend;
 
 
-
 #define get_onsend_info()	(p_onsend)
 #define get_onsend_info()	(p_onsend)
 
 
 /*
 /*
  * returns: 0 drop the message, >= ok, <0 error (but forward the message)
  * returns: 0 drop the message, >= ok, <0 error (but forward the message)
  * it also migh change dst->send_flags!
  * it also migh change dst->send_flags!
  * WARNING: buf must be 0 terminated (to allow regex matches on it) */
  * WARNING: buf must be 0 terminated (to allow regex matches on it) */
-static inline int run_onsend(sip_msg_t* orig_msg, dest_info_t* dst,
-								char* buf, int len)
-{
-	onsend_info_t onsnd_info = {0};
-	int ret;
-	run_act_ctx_t ra_ctx;
-	run_act_ctx_t *bctx;
-	int backup_route_type;
-	snd_flags_t fwd_snd_flags_bak;
-	snd_flags_t rpl_snd_flags_bak;
-	sr_kemi_eng_t *keng = NULL;
-
-	if(orig_msg==NULL || dst==NULL || buf==NULL) {
-		LM_DBG("required parameters are not available - ignoring\n");
-		return 1;
-	}
-	ret=1;
-	// do if onsend_route{} or cfgengine exists
-	if(kemi_onsend_route_callback.len>0) {
-		keng = sr_kemi_eng_get();
-	}
-	if (onsend_rt.rlist[DEFAULT_RT] || keng){
-		onsnd_info.to=&dst->to;
-		onsnd_info.send_sock=dst->send_sock;
-		onsnd_info.buf=buf;
-		onsnd_info.len=len;
-		onsnd_info.msg=orig_msg;
-		p_onsend=&onsnd_info;
-		backup_route_type=get_route_type();
-		set_route_type(ONSEND_ROUTE);
-		if (exec_pre_script_cb(orig_msg, ONSEND_CB_TYPE)>0) {
-			/* backup orig_msg send flags */
-			fwd_snd_flags_bak=orig_msg->fwd_send_flags;
-			rpl_snd_flags_bak=orig_msg->rpl_send_flags;
-			orig_msg->fwd_send_flags=dst->send_flags; /* intial value */
-			init_run_actions_ctx(&ra_ctx);
-
-			if(keng) {
-				bctx = sr_kemi_act_ctx_get();
-				sr_kemi_act_ctx_set(&ra_ctx);
-				ret=sr_kemi_route(keng, orig_msg, ONSEND_ROUTE, NULL, NULL);
-				sr_kemi_act_ctx_set(bctx);
-			} else {
-				ret=run_actions(&ra_ctx, onsend_rt.rlist[DEFAULT_RT], orig_msg);
-			}
-
-			/* update dst send_flags */
-			dst->send_flags=orig_msg->fwd_send_flags;
-			/* restore orig_msg flags */
-			orig_msg->fwd_send_flags=fwd_snd_flags_bak;
-			orig_msg->rpl_send_flags=rpl_snd_flags_bak;
-			exec_post_script_cb(orig_msg, ONSEND_CB_TYPE);
-			if((ret==0) && !(ra_ctx.run_flags&DROP_R_F)){
-				ret = 1;
-			}
-		} else {
-			ret=0; /* drop the message */
-		}
-		set_route_type(backup_route_type);
-		p_onsend=0; /* reset it */
-	}
-	return ret;
-}
+int run_onsend(sip_msg_t* orig_msg, dest_info_t* dst, char* buf, int len);
 
 
 #define onsend_route_enabled(rtype) (onsend_rt.rlist[DEFAULT_RT]?((rtype==SIP_REPLY)?onsend_route_reply:1):0)
 #define onsend_route_enabled(rtype) (onsend_rt.rlist[DEFAULT_RT]?((rtype==SIP_REPLY)?onsend_route_reply:1):0)
 
 
+int run_onsend_evroute(onsend_info_t *sndinfo, int evrt, str *evcb, str *evname);
+
 #endif
 #endif