modules/ims_qos: added patch for flow-description bug when request originates from...
[sip-router] / onsend.h
1 /*
2  * Copyright (C) 2005 iptelorg GmbH
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*!
21  * \file
22  * \brief Kamailio core :: IP address handling
23  * \author andrei
24  * \ingroup core
25  * Module: \ref core
26  */
27
28 #ifndef onsend_h
29 #define onsend_h
30
31
32 #include "ip_addr.h"
33 #include "action.h"
34 #include "route.h"
35 #include "script_cb.h"
36 #include "sr_compat.h"
37 #include "kemi.h"
38
39 struct onsend_info{
40         union sockaddr_union* to;       /* dest info */
41         struct socket_info* send_sock;  /* local send socket */
42         char* buf;                      /* outgoing buffer */
43         int len;                        /* outgoing buffer len */
44         sip_msg_t *msg;                 /* original sip msg struct */
45 };
46
47 extern struct onsend_info* p_onsend;
48
49
50 #define get_onsend_info()       (p_onsend)
51
52 /*
53  * returns: 0 drop the message, >= ok, <0 error (but forward the message)
54  * it also migh change dst->send_flags!
55  * WARNING: buf must be 0 terminated (to allow regex matches on it) */
56 static inline int run_onsend(struct sip_msg* orig_msg, struct dest_info* dst,
57                                                                 char* buf, int len)
58 {
59         struct onsend_info onsnd_info = {0};
60         int ret;
61         struct run_act_ctx ra_ctx;
62         struct run_act_ctx *bctx;
63         int backup_route_type;
64         snd_flags_t fwd_snd_flags_bak;
65         snd_flags_t rpl_snd_flags_bak;
66         sr_kemi_eng_t *keng = NULL;
67
68         if(orig_msg==NULL || dst==NULL || buf==NULL) {
69                 LM_DBG("required parameters are not available - ignoring\n");
70                 return 1;
71         }
72         ret=1;
73         if (onsend_rt.rlist[DEFAULT_RT]){
74                 onsnd_info.to=&dst->to;
75                 onsnd_info.send_sock=dst->send_sock;
76                 onsnd_info.buf=buf;
77                 onsnd_info.len=len;
78                 onsnd_info.msg=orig_msg;
79                 p_onsend=&onsnd_info;
80                 backup_route_type=get_route_type();
81                 set_route_type(ONSEND_ROUTE);
82                 if (exec_pre_script_cb(orig_msg, ONSEND_CB_TYPE)>0) {
83                         /* backup orig_msg send flags */
84                         fwd_snd_flags_bak=orig_msg->fwd_send_flags;
85                         rpl_snd_flags_bak=orig_msg->rpl_send_flags;
86                         orig_msg->fwd_send_flags=dst->send_flags; /* intial value */
87                         init_run_actions_ctx(&ra_ctx);
88
89                         keng = sr_kemi_eng_get();
90                         if(unlikely(keng!=NULL)) {
91                                 bctx = sr_kemi_act_ctx_get();
92                                 sr_kemi_act_ctx_set(&ra_ctx);
93                                 ret=keng->froute(orig_msg, ONSEND_ROUTE, NULL, NULL);
94                                 sr_kemi_act_ctx_set(bctx);
95                         } else {
96                                 ret=run_actions(&ra_ctx, onsend_rt.rlist[DEFAULT_RT], orig_msg);
97                         }
98
99                         /* update dst send_flags */
100                         dst->send_flags=orig_msg->fwd_send_flags;
101                         /* restore orig_msg flags */
102                         orig_msg->fwd_send_flags=fwd_snd_flags_bak;
103                         orig_msg->rpl_send_flags=rpl_snd_flags_bak;
104                         exec_post_script_cb(orig_msg, ONSEND_CB_TYPE);
105                         if((ret==0) && !(ra_ctx.run_flags&DROP_R_F)){
106                                 ret = 1;
107                         }
108                 } else {
109                         ret=0; /* drop the message */
110                 }
111                 set_route_type(backup_route_type);
112                 p_onsend=0; /* reset it */
113         }
114         return ret;
115 }
116
117 #define onsend_route_enabled(rtype) (onsend_rt.rlist[DEFAULT_RT]?((rtype==SIP_REPLY)?onsend_route_reply:1):0)
118
119 #endif