a8a4e6ed8ba2362012030e39b1483906711a732a
[sip-router] / onsend.h
1 /*
2  *  $Id$
3  *
4  * Copyright (C) 2005 iptelorg GmbH
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26  */
27 /*
28  * History:
29  * -------
30  *  2005-12-11 created by andrei
31  *  2009-06-01 Pre- and post-script callbacks of onsend route are executed (Miklos)
32  */
33
34
35 #ifndef onsend_h
36 #define onsend_h
37
38
39 #include "ip_addr.h"
40 #include "action.h"
41 #include "route.h"
42 #include "script_cb.h"
43 #include "sr_compat.h"
44
45 struct onsend_info{
46         union sockaddr_union* to;       /* dest info */
47         struct socket_info* send_sock;  /* local send socket */
48         char* buf;                      /* outgoing buffer */
49         int len;                        /* outgoing buffer len */
50         sip_msg_t *msg;                 /* original sip msg struct */
51 };
52
53 extern struct onsend_info* p_onsend;
54
55
56 #define get_onsend_info()       (p_onsend)
57
58 /*
59  * returns: 0 drop the message, >= ok, <0 error (but forward the message)
60  * it also migh change dst->send_flags!
61  * WARNING: buf must be 0 terminated (to allow regex matches on it) */
62 static inline int run_onsend(struct sip_msg* orig_msg, struct dest_info* dst,
63                                                                 char* buf, int len)
64 {
65         struct onsend_info onsnd_info = {0};
66         int ret;
67         struct run_act_ctx ra_ctx;
68         int backup_route_type;
69         snd_flags_t fwd_snd_flags_bak;
70         snd_flags_t rpl_snd_flags_bak;
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                         ret=run_actions(&ra_ctx, onsend_rt.rlist[DEFAULT_RT], orig_msg);
89                         /* update dst send_flags */
90                         dst->send_flags=orig_msg->fwd_send_flags;
91                         /* restore orig_msg flags */
92                         orig_msg->fwd_send_flags=fwd_snd_flags_bak;
93                         orig_msg->rpl_send_flags=rpl_snd_flags_bak;
94                         exec_post_script_cb(orig_msg, ONSEND_CB_TYPE);
95                         if((ret==0) && !(ra_ctx.run_flags&DROP_R_F)){
96                                 ret = 1;
97                         }
98                 } else {
99                         ret=0; /* drop the message */
100                 }
101                 set_route_type(backup_route_type);
102                 p_onsend=0; /* reset it */
103         }
104         return ret;
105 }
106
107 #define onsend_route_enabled(rtype) (onsend_rt.rlist[DEFAULT_RT]?((rtype==SIP_REPLY)?onsend_route_reply:1):0)
108
109 #endif