bd1b54aeedddb634ab0ec3fa227c90c009ba6c51
[sip-router] / modules / tm / tm.c
1 /*
2  * $Id$
3  *
4  * TM module
5  *
6  *
7  * ***************************************************
8  * * Jiri's Source Memorial                          *
9  * *                                                 *
10  * * Welcome, pilgrim ! This is the greatest place   *
11  * * where dramatic changes happend. There are not   *
12  * * many places with a history like this, as there  *
13  * * are not so many people like Jiri, one of the    *
14  * * ser's fathers, who brought everywhere the wind  *
15  * * of change, the flood of clean-up. We all felt   *
16  * * his fatherly eye watching over us day and night.*
17  * *                                                 *
18  * * Please, preserve this codework heritage, as     *
19  * * it's unlikely for fresh, juicy pieces of code to  *
20  * * arise to give him the again the chance to       *
21  * * demonstrate his clean-up and improvement skills.*
22  * *                                                 *
23  * * Hereby, we solicit you to adopt this historical *
24  * * piece of code. For $100, your name will be      *
25  * * be printed in this banner and we will use       *
26  * * collected funds to create and display an ASCII  *
27  * * statue of Jiri  .                               *
28  * ***************************************************
29  *
30  *
31  * Copyright (C) 2001-2003 FhG Fokus
32  *
33  * This file is part of SIP-router, a free SIP server.
34  *
35  * SIP-router is free software; you can redistribute it and/or modify
36  * it under the terms of the GNU General Public License as published by
37  * the Free Software Foundation; either version 2 of the License, or
38  * (at your option) any later version
39  *
40  * SIP-router is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
48  */
49 /*
50  * History:
51  * --------
52  *  2003-02-18  added t_forward_nonack_{udp, tcp}, t_relay_to_{udp,tcp},
53  *               t_replicate_{udp, tcp} (andrei)
54  *  2003-02-19  added t_rely_{udp, tcp} (andrei)
55  *  2003-03-06  voicemail changes accepted (jiri)
56  *  2003-03-10  module export interface updated to the new format (andrei)
57  *  2003-03-16  flags export parameter added (janakj)
58  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
59  *  2003-03-30  set_kr for requests only (jiri)
60  *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
61  *  2003-04-14  use protocol from uri (jiri)
62  *  2003-07-07  added t_relay_to_tls, t_replicate_tls, t_forward_nonack_tls
63  *              added #ifdef USE_TCP, USE_TLS
64  *              removed t_relay_{udp,tcp,tls} (andrei)
65  *  2003-09-26  added t_forward_nonack_uri() - same as t_forward_nonack() but
66  *              takes no parameters -> forwards to uri (bogdan)
67  *  2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
68  *  2004-02-18  t_reply exported via FIFO - imported from VM (bogdan)
69  *  2004-10-01  added a new param.: restart_fr_on_each_reply (andrei)
70  *  2005-11-14  new timer support, changed timer related module params (andrei)
71  *  2005-12-09  fixup_hostport2proxy uses route_struct to access param #1
72  *              when fixing param #2
73  *  2005-12-09  added t_set_fr() (andrei)
74  *  2006-02-07  named routes support (andrei)
75  *  2006-09-28  added t_branch_replied, t_branch_timeout, t_any_replied, 
76  *               t_any_timeout, t_is_canceled (andrei)
77  *  2006-10-16  added a new param.: aggregate challenges (andrei)
78  *  2007-05-28  two new params: reparse_invite, ac_extra_hdrs
79  *              added w_t_relay_cancel() (Miklos)
80  *  2007-06-05  added t_set_auto_inv_100() and auto_inv_100 (param);
81  *               t_set_max_lifetime(), max_{non}inv_lifetime  (andrei)
82  *  2008-02-05  module config parameters use the configuration framework (Miklos)
83  *  2008-02-29  added t_grep_status(code) (andrei)
84  *  2008-05-15  added t_relay(host, port) (similar to forward(host, port)) &
85  *               t_relay_to_{udp,tcp,tls}(<no param>) (force protocol, but 
86  *               forward to uri)  (andrei)
87  *  2008-08-11  sctp support: t_relay_to_sctp, t_replicate_sctp,
88  *               t_forward_nonack_sctp (andrei)
89  *  2009-03-18  added a new param: auto_inv_100_reason (aheise) 
90  */
91
92 /*!
93  * \file 
94  * \brief TM :: Module API (core)
95  * \ingroup tm
96  */
97
98 /*!
99  * \defgroup tm TM :: Transaction stateful proxy support
100  *
101    The TM module enables stateful processing of SIP transactions. The main use
102    of stateful logic, which is costly in terms of memory and CPU, is some
103    services that inherently need state. 
104
105    For example, transaction-based accounting (module acc) needs to process 
106    transaction state as opposed to individual messages, and any kinds of 
107    forking must be implemented statefully. Other use of stateful processing 
108    is it trading CPU caused by retransmission processing for memory. 
109    That however only makes sense if CPU consumption per request is huge. 
110    For example, if you want to avoid costly DNS resolution for every 
111    retransmission of a request to an unresolvable destination, use stateful 
112    mode. Then, only the initial message burdens server by DNS queries, 
113    subsequent retransmissions will be dropped and will not result in more 
114    processes blocked by DNS resolution. The price is more memory consumption 
115    and higher processing latency.
116
117  *
118 \code
119  * ***************************************************
120  * * Jiri's Source Memorial                          *
121  * *                                                 *
122  * * Welcome, pilgrim ! This is the greatest place   *
123  * * where dramatic changes happend. There are not   *
124  * * many places with a history like this, as there  *
125  * * are not so many people like Jiri, one of the    *
126  * * ser's fathers, who brought everywhere the wind  *
127  * * of change, the flood of clean-up. We all felt   *
128  * * his fatherly eye watching over us day and night.*
129  * *                                                 *
130  * * Please, preserve this codework heritage, as     *
131  * * it's unlikely for fresh, juicy pieces of code to  *
132  * * arise to give him the again the chance to       *
133  * * demonstrate his clean-up and improvement skills.*
134  * *                                                 *
135  * * Hereby, we solicit you to adopt this historical *
136  * * piece of code. For $100, your name will be      *
137  * * be printed in this banner and we will use       *
138  * * collected funds to create and display an ASCII  *
139  * * statue of Jiri  .                               *
140  * ***************************************************
141 \endcode
142  */
143
144
145 #include "defs.h"
146
147
148 #include <stdio.h>
149 #include <string.h>
150 #include <netdb.h>
151
152 #include "../../sr_module.h"
153 #include "../../dprint.h"
154 #include "../../error.h"
155 #include "../../ut.h"
156 #include "../../script_cb.h"
157 #include "../../usr_avp.h"
158 #include "../../mem/mem.h"
159 #include "../../route_struct.h"
160 #include "../../route.h"
161 #include "../../cfg/cfg.h"
162 #include "../../globals.h"
163 #include "../../timer_ticks.h"
164 #include "../../mod_fix.h"
165
166 #include "config.h"
167 #include "sip_msg.h"
168 #include "h_table.h"
169 #include "t_hooks.h"
170 #include "tm_load.h"
171 #include "ut.h"
172 #include "t_reply.h"
173 #include "uac.h"
174 #include "t_fwd.h"
175 #include "t_lookup.h"
176 #include "t_stats.h"
177 #include "callid.h"
178 #include "t_cancel.h"
179 #include "t_fifo.h"
180 #include "timer.h"
181 #include "t_msgbuilder.h"
182 #include "select.h"
183 #include "t_serial.h"
184 #include "rpc_uac.h"
185
186 MODULE_VERSION
187
188 /* fixup functions */
189 static int fixup_hostport2proxy(void** param, int param_no);
190 static int fixup_proto_hostport2proxy(void** param, int param_no);
191 static int fixup_on_failure(void** param, int param_no);
192 static int fixup_on_reply(void** param, int param_no);
193 static int fixup_on_branch(void** param, int param_no);
194 static int fixup_t_reply(void** param, int param_no);
195 static int fixup_on_sl_reply(modparam_t type, void* val);
196 static int fixup_t_relay_to(void** param, int param_no);
197
198 /* init functions */
199 static int mod_init(void);
200 static int child_init(int rank);
201
202
203 /* exported functions */
204 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2);
205 inline static int w_t_lookup_cancel(struct sip_msg* msg, char* str, char* str2);
206 inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2);
207 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2);
208 inline static int w_t_retransmit_reply(struct sip_msg* p_msg, char* foo,
209                                 char* bar );
210 inline static int w_t_newtran(struct sip_msg* p_msg, char* foo, char* bar );
211 inline static int w_t_relay( struct sip_msg  *p_msg , char *_foo, char *_bar);
212 inline static int w_t_relay2( struct sip_msg  *p_msg , char *proxy, char*);
213 inline static int w_t_relay_to_udp( struct sip_msg  *p_msg , char *proxy,
214                                  char *);
215 inline static int w_t_relay_to_udp_uri( struct sip_msg  *p_msg , char*, char*);
216 #ifdef USE_TCP
217 inline static int w_t_relay_to_tcp( struct sip_msg  *p_msg , char *proxy,
218                                 char *);
219 inline static int w_t_relay_to_tcp_uri( struct sip_msg  *p_msg , char*, char*);
220 #endif
221 #ifdef USE_TLS
222 inline static int w_t_relay_to_tls( struct sip_msg  *p_msg , char *proxy,
223                                 char *);
224 inline static int w_t_relay_to_tls_uri( struct sip_msg  *p_msg , char*, char*);
225 #endif
226 #ifdef USE_SCTP
227 inline static int w_t_relay_to_sctp( struct sip_msg  *p_msg , char *proxy,
228                                 char *);
229 inline static int w_t_relay_to_sctp_uri( struct sip_msg*, char*, char*);
230 #endif
231 inline static int w_t_relay_to_avp(struct sip_msg* msg, char* str,char*);
232 inline static int w_t_relay_to(struct sip_msg* msg, char* str,char*);
233 inline static int w_t_replicate_uri( struct sip_msg  *p_msg ,
234                                 char *uri,       /* sip uri as string or variable */
235                                 char *_foo       /* nothing expected */ );
236 inline static int w_t_replicate( struct sip_msg  *p_msg ,
237                                 char *proxy, /* struct proxy_l *proxy expected */
238                                 char *_foo       /* nothing expected */ );
239 inline static int w_t_replicate_udp( struct sip_msg  *p_msg ,
240                                 char *proxy, /* struct proxy_l *proxy expected */
241                                 char *_foo       /* nothing expected */ );
242 #ifdef USE_TCP
243 inline static int w_t_replicate_tcp( struct sip_msg  *p_msg ,
244                                 char *proxy, /* struct proxy_l *proxy expected */
245                                 char *_foo       /* nothing expected */ );
246 #endif
247 #ifdef USE_TLS
248 inline static int w_t_replicate_tls( struct sip_msg  *p_msg ,
249                                 char *proxy, /* struct proxy_l *proxy expected */
250                                 char *_foo       /* nothing expected */ );
251 #endif
252 #ifdef USE_SCTP
253 inline static int w_t_replicate_sctp( struct sip_msg  *p_msg ,
254                                 char *proxy, /* struct proxy_l *proxy expected */
255                                 char *_foo       /* nothing expected */ );
256 #endif
257 inline static int w_t_replicate_to(struct sip_msg* msg, char* str,char*);
258 inline static int w_t_forward_nonack(struct sip_msg* msg, char* str, char* );
259 inline static int w_t_forward_nonack_uri(struct sip_msg* msg, char* str,char*);
260 inline static int w_t_forward_nonack_udp(struct sip_msg* msg, char* str,char*);
261 #ifdef USE_TCP
262 inline static int w_t_forward_nonack_tcp(struct sip_msg*, char* str,char*);
263 #endif
264 #ifdef USE_TLS
265 inline static int w_t_forward_nonack_tls(struct sip_msg*, char* str,char*);
266 #endif
267 #ifdef USE_SCTP
268 inline static int w_t_forward_nonack_sctp(struct sip_msg*, char* str,char*);
269 #endif
270 inline static int w_t_forward_nonack_to(struct sip_msg* msg, char* str,char*);
271 inline static int w_t_relay_cancel(struct sip_msg *p_msg, char *_foo, char *_bar);
272 inline static int w_t_on_negative(struct sip_msg* msg, char *go_to, char *foo);
273 inline static int w_t_on_branch(struct sip_msg* msg, char *go_to, char *foo);
274 inline static int w_t_on_reply(struct sip_msg* msg, char *go_to, char *foo );
275 inline static int t_check_status(struct sip_msg* msg, char *match, char *foo);
276 static int t_set_fr_inv(struct sip_msg* msg, char* fr_inv, char* foo);
277 static int t_set_fr_all(struct sip_msg* msg, char* fr_inv, char* fr);
278 static int w_t_reset_fr(struct sip_msg* msg, char* foo, char* bar);
279 static int w_t_set_retr(struct sip_msg* msg, char* retr_t1, char* retr_t2);
280 static int w_t_reset_retr(struct sip_msg* msg, char* foo, char* bar);
281 static int w_t_set_max_lifetime(struct sip_msg* msg, char* inv, char* noninv);
282 static int w_t_reset_max_lifetime(struct sip_msg* msg, char* foo, char* bar);
283 static int t_set_auto_inv_100(struct sip_msg* msg, char* on_off, char* foo);
284 static int t_set_disable_6xx(struct sip_msg* msg, char* on_off, char* foo);
285 static int t_set_disable_failover(struct sip_msg* msg, char* on_off, char* f);
286 static int t_branch_timeout(struct sip_msg* msg, char*, char*);
287 static int t_branch_replied(struct sip_msg* msg, char*, char*);
288 static int t_any_timeout(struct sip_msg* msg, char*, char*);
289 static int t_any_replied(struct sip_msg* msg, char*, char*);
290 static int t_is_canceled(struct sip_msg* msg, char*, char*);
291 static int t_is_expired(struct sip_msg* msg, char*, char*);
292 static int t_grep_status(struct sip_msg* msg, char*, char*);
293 static int w_t_drop_replies(struct sip_msg* msg, char* foo, char* bar);
294 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar);
295 static int t_check_trans(struct sip_msg* msg, char* foo, char* bar);
296
297
298 /* by default the fr timers avps are not set, so that the avps won't be
299  * searched for nothing each time a new transaction is created */
300 static char *fr_timer_param = 0 /*FR_TIMER_AVP*/;
301 static char *fr_inv_timer_param = 0 /*FR_INV_TIMER_AVP*/;
302 static char *contacts_avp_param = 0;
303
304 static rpc_export_t tm_rpc[];
305
306 static int fixup_t_check_status(void** param, int param_no);
307
308 static cmd_export_t cmds[]={
309         {"t_newtran",          w_t_newtran,             0, 0,
310                         REQUEST_ROUTE},
311         {"t_lookup_request",   w_t_check,               0, 0,
312                         REQUEST_ROUTE},
313         {"t_lookup_cancel",    w_t_lookup_cancel,       0, 0,
314                         REQUEST_ROUTE},
315         {"t_lookup_cancel",    w_t_lookup_cancel,       1, fixup_int_1,
316                         REQUEST_ROUTE},
317         {T_REPLY,              w_t_reply,               2, fixup_t_reply,
318                         REQUEST_ROUTE | FAILURE_ROUTE },
319         {"t_retransmit_reply", w_t_retransmit_reply,    0, 0,
320                         REQUEST_ROUTE},
321         {"t_release",          w_t_release,             0, 0,
322                         REQUEST_ROUTE},
323         {T_RELAY_TO_UDP,       w_t_relay_to_udp,        2, fixup_hostport2proxy,
324                         REQUEST_ROUTE|FAILURE_ROUTE},
325         {T_RELAY_TO_UDP,       w_t_relay_to_udp_uri,    0, 0,
326                         REQUEST_ROUTE|FAILURE_ROUTE},
327 #ifdef USE_TCP
328         {T_RELAY_TO_TCP,       w_t_relay_to_tcp,        2, fixup_hostport2proxy,
329                         REQUEST_ROUTE|FAILURE_ROUTE},
330         {T_RELAY_TO_TCP,       w_t_relay_to_tcp_uri,    0, 0,
331                         REQUEST_ROUTE|FAILURE_ROUTE},
332 #endif
333 #ifdef USE_TLS
334         {T_RELAY_TO_TLS,       w_t_relay_to_tls,        2, fixup_hostport2proxy,
335                         REQUEST_ROUTE|FAILURE_ROUTE},
336         {T_RELAY_TO_TLS,       w_t_relay_to_tls_uri,    0, 0,
337                         REQUEST_ROUTE|FAILURE_ROUTE},
338 #endif
339 #ifdef USE_SCTP
340         {T_RELAY_TO_SCTP,       w_t_relay_to_sctp,       2, fixup_hostport2proxy,
341                         REQUEST_ROUTE|FAILURE_ROUTE},
342         {T_RELAY_TO_SCTP,       w_t_relay_to_sctp_uri,    0, 0,
343                         REQUEST_ROUTE|FAILURE_ROUTE},
344 #endif
345         {"t_replicate",        w_t_replicate_uri,       1, fixup_var_str_1,
346                         REQUEST_ROUTE},
347         {"t_replicate",        w_t_replicate,           2, fixup_hostport2proxy,
348                         REQUEST_ROUTE},
349         {"t_replicate_udp",    w_t_replicate_udp,       2, fixup_hostport2proxy,
350                         REQUEST_ROUTE},
351 #ifdef USE_TCP
352         {"t_replicate_tcp",    w_t_replicate_tcp,       2, fixup_hostport2proxy,
353                         REQUEST_ROUTE},
354 #endif
355 #ifdef USE_TLS
356         {"t_replicate_tls",    w_t_replicate_tls,       2, fixup_hostport2proxy,
357                         REQUEST_ROUTE},
358 #endif
359 #ifdef USE_SCTP
360         {"t_replicate_sctp",    w_t_replicate_sctp,     2, fixup_hostport2proxy,
361                         REQUEST_ROUTE},
362 #endif
363         {"t_replicate_to", w_t_replicate_to,            2, fixup_proto_hostport2proxy,
364                         REQUEST_ROUTE},
365         {T_RELAY,              w_t_relay,               0, 0,
366                         REQUEST_ROUTE | FAILURE_ROUTE },
367         {T_RELAY,              w_t_relay2,              2, fixup_hostport2proxy,
368                         REQUEST_ROUTE | FAILURE_ROUTE },
369         {"t_relay_to_avp", w_t_relay_to_avp,            2, fixup_proto_hostport2proxy,
370                         REQUEST_ROUTE},
371         {"t_relay_to",                  w_t_relay_to,           0, 0,
372                         REQUEST_ROUTE | FAILURE_ROUTE },
373         {"t_relay_to",                  w_t_relay_to,           1, fixup_t_relay_to,
374                         REQUEST_ROUTE | FAILURE_ROUTE },
375         {"t_relay_to",                  w_t_relay_to,           2, fixup_t_relay_to,
376                         REQUEST_ROUTE | FAILURE_ROUTE },
377         {T_FORWARD_NONACK,     w_t_forward_nonack,      2, fixup_hostport2proxy,
378                         REQUEST_ROUTE},
379         {T_FORWARD_NONACK_URI, w_t_forward_nonack_uri,  0, 0,
380                         REQUEST_ROUTE},
381         {T_FORWARD_NONACK_UDP, w_t_forward_nonack_udp,  2, fixup_hostport2proxy,
382                         REQUEST_ROUTE},
383 #ifdef USE_TCP
384         {T_FORWARD_NONACK_TCP, w_t_forward_nonack_tcp,  2, fixup_hostport2proxy,
385                         REQUEST_ROUTE},
386 #endif
387 #ifdef USE_TLS
388         {T_FORWARD_NONACK_TLS, w_t_forward_nonack_tls,  2, fixup_hostport2proxy,
389                         REQUEST_ROUTE},
390 #endif
391 #ifdef USE_SCTP
392         {T_FORWARD_NONACK_SCTP, w_t_forward_nonack_sctp, 2, fixup_hostport2proxy,
393                         REQUEST_ROUTE},
394 #endif
395         {"t_forward_nonack_to", w_t_forward_nonack_to,  2, fixup_proto_hostport2proxy,
396                         REQUEST_ROUTE},
397         {"t_relay_cancel",     w_t_relay_cancel,        0, 0,
398                         REQUEST_ROUTE},
399         {"t_on_failure",       w_t_on_negative,         1, fixup_on_failure,
400                         REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE },
401         {"t_on_reply",         w_t_on_reply,            1, fixup_on_reply,
402                         REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE },
403         {"t_on_branch",       w_t_on_branch,         1, fixup_on_branch,
404                         REQUEST_ROUTE | FAILURE_ROUTE },
405         {"t_check_status",     t_check_status,          1, fixup_t_check_status,
406                         REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE },
407         {"t_write_req",       t_write_req,              2, fixup_t_write,
408                         REQUEST_ROUTE | FAILURE_ROUTE },
409         {"t_write_unix",      t_write_unix,             2, fixup_t_write,
410                         REQUEST_ROUTE | FAILURE_ROUTE },
411         {"t_set_fr",          t_set_fr_inv,             1, fixup_var_int_1,
412                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
413         {"t_set_fr",          t_set_fr_all,             2, fixup_var_int_12,
414                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
415         {"t_reset_fr",        w_t_reset_fr,             0, 0,
416                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
417         {"t_set_retr",        w_t_set_retr,               2, fixup_var_int_12,
418                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
419         {"t_reset_retr",      w_t_reset_retr,           0, 0,
420                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
421         {"t_set_max_lifetime", w_t_set_max_lifetime,      2, fixup_var_int_12,
422                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
423         {"t_reset_max_lifetime", w_t_reset_max_lifetime, 0, 0,
424                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
425         {"t_set_auto_inv_100", t_set_auto_inv_100,       1, fixup_var_int_1,
426                                                                                                           REQUEST_ROUTE},
427         {"t_set_disable_6xx", t_set_disable_6xx,         1, fixup_var_int_1,
428                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
429         {"t_set_disable_failover", t_set_disable_failover, 1, fixup_var_int_1,
430                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
431         {"t_branch_timeout",  t_branch_timeout,         0, 0,  FAILURE_ROUTE},
432         {"t_branch_replied",  t_branch_replied,         0, 0,  FAILURE_ROUTE},
433         {"t_any_timeout",     t_any_timeout,            0, 0, 
434                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
435         {"t_any_replied",     t_any_replied,            0, 0, 
436                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
437         {"t_is_canceled",     t_is_canceled,            0, 0,
438                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
439         {"t_is_expired",      t_is_expired,             0, 0,
440                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
441         {"t_grep_status",     t_grep_status,            1, fixup_var_int_1, 
442                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
443         {"t_drop_replies",    w_t_drop_replies,         0, 0,
444                         FAILURE_ROUTE},
445         {"t_drop_replies",    w_t_drop_replies,         1, 0,
446                         FAILURE_ROUTE},
447         {"t_save_lumps",      w_t_save_lumps,           0, 0,
448                         REQUEST_ROUTE},
449         {"t_check_trans",       t_check_trans,                          0, 0,
450                         REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE },
451
452         {"t_load_contacts", t_load_contacts,            0, 0,
453                         REQUEST_ROUTE | FAILURE_ROUTE},
454         {"t_next_contacts", t_next_contacts,            0, 0,
455                         REQUEST_ROUTE | FAILURE_ROUTE},
456
457         /* not applicable from the script */
458         {"register_tmcb",      (cmd_function)register_tmcb,     NO_SCRIPT,   0, 0},
459         {"load_tm",            (cmd_function)load_tm,           NO_SCRIPT,   0, 0},
460         {T_REPLY_WB,           (cmd_function)t_reply_with_body, NO_SCRIPT,   0, 0},
461         {T_IS_LOCAL,           (cmd_function)t_is_local,        NO_SCRIPT,   0, 0},
462         {T_GET_TI,             (cmd_function)t_get_trans_ident, NO_SCRIPT,   0, 0},
463         {T_LOOKUP_IDENT,       (cmd_function)t_lookup_ident,    NO_SCRIPT,   0, 0},
464         {T_ADDBLIND,           (cmd_function)add_blind_uac,     NO_SCRIPT,   0, 0},
465         {"t_request_within",   (cmd_function)req_within,        NO_SCRIPT,   0, 0},
466         {"t_request_outside",  (cmd_function)req_outside,       NO_SCRIPT,   0, 0},
467         {"t_request",          (cmd_function)request,           NO_SCRIPT,   0, 0},
468         {"new_dlg_uac",        (cmd_function)new_dlg_uac,       NO_SCRIPT,   0, 0},
469         {"dlg_response_uac",   (cmd_function)dlg_response_uac,  NO_SCRIPT,   0, 0},
470         {"new_dlg_uas",        (cmd_function)new_dlg_uas,       NO_SCRIPT,   0, 0},
471         {"update_dlg_uas",     (cmd_function)update_dlg_uas,    NO_SCRIPT,   0, 0},
472         {"dlg_request_uas",    (cmd_function)dlg_request_uas,   NO_SCRIPT,   0, 0},
473         {"set_dlg_target",     (cmd_function)set_dlg_target,    NO_SCRIPT,   0, 0},
474         {"free_dlg",           (cmd_function)free_dlg,          NO_SCRIPT,   0, 0},
475         {"print_dlg",          (cmd_function)print_dlg,         NO_SCRIPT,   0, 0},
476         {T_GETT,               (cmd_function)get_t,             NO_SCRIPT,   0, 0},
477         {"calculate_hooks",    (cmd_function)w_calculate_hooks, NO_SCRIPT,   0, 0},
478         {"t_uac",              (cmd_function)t_uac,             NO_SCRIPT,   0, 0},
479         {"t_uac_with_ids",     (cmd_function)t_uac_with_ids,    NO_SCRIPT,   0, 0},
480         {"t_unref",            (cmd_function)t_unref,           NO_SCRIPT,   0, 0},
481         {"run_failure_handlers", (cmd_function)run_failure_handlers, NO_SCRIPT,   0, 0},
482         {"cancel_uacs",        (cmd_function)cancel_uacs,       NO_SCRIPT,   0, 0},
483         {"cancel_all_uacs",    (cmd_function)cancel_all_uacs,        NO_SCRIPT,   0, 0},
484 #ifdef WITH_AS_SUPPORT
485         {"ack_local_uac",      (cmd_function)ack_local_uac,     NO_SCRIPT,   0, 0},
486         {"t_get_canceled_ident",   (cmd_function)t_get_canceled_ident,  NO_SCRIPT,
487                         0, 0},
488 #endif
489         {"t_suspend",          (cmd_function)t_suspend,         NO_SCRIPT,   0, 0},
490         {"t_continue",         (cmd_function)t_continue,        NO_SCRIPT,   0, 0},
491         {"t_cancel_suspend",   (cmd_function)t_cancel_suspend,  NO_SCRIPT,   0, 0},
492         {0,0,0,0,0}
493 };
494
495
496 static param_export_t params[]={
497         {"ruri_matching",       PARAM_INT, &default_tm_cfg.ruri_matching         },
498         {"via1_matching",       PARAM_INT, &default_tm_cfg.via1_matching         },
499         {"fr_timer",            PARAM_INT, &default_tm_cfg.fr_timeout            },
500         {"fr_inv_timer",        PARAM_INT, &default_tm_cfg.fr_inv_timeout        },
501         {"fr_inv_timer_next",   PARAM_INT, &default_tm_cfg.fr_inv_timeout_next   },
502         {"wt_timer",            PARAM_INT, &default_tm_cfg.wait_timeout          },
503         {"delete_timer",        PARAM_INT, &default_tm_cfg.delete_timeout        },
504         {"retr_timer1",         PARAM_INT, &default_tm_cfg.rt_t1_timeout         },
505         {"retr_timer2"  ,       PARAM_INT, &default_tm_cfg.rt_t2_timeout         },
506         {"max_inv_lifetime",    PARAM_INT, &default_tm_cfg.tm_max_inv_lifetime   },
507         {"max_noninv_lifetime", PARAM_INT, &default_tm_cfg.tm_max_noninv_lifetime},
508         {"noisy_ctimer",        PARAM_INT, &default_tm_cfg.noisy_ctimer          },
509         {"auto_inv_100",        PARAM_INT, &default_tm_cfg.tm_auto_inv_100       },
510         {"auto_inv_100_reason", PARAM_STRING, &default_tm_cfg.tm_auto_inv_100_r  },
511         {"unix_tx_timeout",     PARAM_INT, &default_tm_cfg.tm_unix_tx_timeout    },
512         {"restart_fr_on_each_reply", PARAM_INT,
513                                                                         &default_tm_cfg.restart_fr_on_each_reply},
514         {"fr_timer_avp",        PARAM_STRING, &fr_timer_param                    },
515         {"fr_inv_timer_avp",    PARAM_STRING, &fr_inv_timer_param                },
516         {"tw_append",           PARAM_STRING|PARAM_USE_FUNC, 
517                                                                                                         (void*)parse_tw_append   },
518         {"pass_provisional_replies", PARAM_INT, 
519                                                                         &default_tm_cfg.pass_provisional_replies },
520         {"aggregate_challenges", PARAM_INT, &default_tm_cfg.tm_aggregate_auth    },
521         {"unmatched_cancel",    PARAM_INT, &default_tm_cfg.unmatched_cancel      },
522         {"default_code",        PARAM_INT, &default_tm_cfg.default_code          },
523         {"default_reason",      PARAM_STRING, &default_tm_cfg.default_reason     },
524         {"reparse_invite",      PARAM_INT, &default_tm_cfg.reparse_invite        },
525         {"ac_extra_hdrs",       PARAM_STR, &default_tm_cfg.ac_extra_hdrs         },
526         {"blst_503",            PARAM_INT, &default_tm_cfg.tm_blst_503           },
527         {"blst_503_def_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_default   },
528         {"blst_503_min_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_min       },
529         {"blst_503_max_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_max       },
530         {"blst_methods_add",    PARAM_INT, &default_tm_cfg.tm_blst_methods_add   },
531         {"blst_methods_lookup", PARAM_INT, &default_tm_cfg.tm_blst_methods_lookup},
532         {"cancel_b_method",     PARAM_INT, &default_tm_cfg.cancel_b_flags},
533         {"reparse_on_dns_failover", PARAM_INT, &default_tm_cfg.reparse_on_dns_failover},
534         {"on_sl_reply",         PARAM_STRING|PARAM_USE_FUNC, fixup_on_sl_reply   },
535         {"contacts_avp",        PARAM_STRING, &contacts_avp_param                },
536         {"disable_6xx_block",   PARAM_INT, &default_tm_cfg.disable_6xx           },
537         {"local_ack_mode",      PARAM_INT, &default_tm_cfg.local_ack_mode        },
538         {0,0,0}
539 };
540
541 #ifdef STATIC_TM
542 struct module_exports tm_exports = {
543 #else
544 struct module_exports exports= {
545 #endif
546         "tm",
547         /* -------- exported functions ----------- */
548         cmds,
549         tm_rpc,    /* RPC methods */
550         /* ------------ exported variables ---------- */
551         params,
552
553         mod_init, /* module initialization function */
554         (response_function) reply_received,
555         (destroy_function) tm_shutdown,
556         0, /* w_onbreak, */
557         child_init /* per-child init function */
558 };
559
560
561
562 /* helper for fixup_on_* */
563 static int fixup_routes(char* r_type, struct route_list* rt, void** param)
564 {
565         int i;
566         
567         i=route_get(rt, (char*)*param);
568         if (i==-1){
569                 LOG(L_ERR, "ERROR: tm: fixup_routes: route_get failed\n");
570                 return E_UNSPEC;
571         }
572         if (r_type && rt->rlist[i]==0){
573                 LOG(L_WARN, "WARNING: %s(\"%s\"): empty/non existing route\n",
574                                 r_type, (char*)*param);
575         }
576         *param=(void*)(long)i;
577         return 0;
578 }
579
580 static int fixup_t_reply(void** param, int param_no)
581 {
582         if (param_no == 1) {
583                 if (fixup_var_int_12(param, 1) != 0) return -1;
584         } else if (param_no == 2) {
585                 return fixup_var_str_12(param, 2);
586         }
587     return 0;
588 }
589
590 static int fixup_on_failure(void** param, int param_no)
591 {
592         if (param_no==1){
593                 if(strlen((char*)*param)<=1
594                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
595                         *param = (void*)0;
596                         return 0;
597                 }
598                 return fixup_routes("t_on_failure", &failure_rt, param);
599         }
600         return 0;
601 }
602
603
604
605 static int fixup_on_reply(void** param, int param_no)
606 {
607         if (param_no==1){
608                 if(strlen((char*)*param)<=1
609                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
610                         *param = (void*)0;
611                         return 0;
612                 }
613                 return fixup_routes("t_on_reply", &onreply_rt, param);
614         }
615         return 0;
616 }
617
618
619
620 static int fixup_on_branch(void** param, int param_no)
621 {
622         if (param_no==1){
623                 if(strlen((char*)*param)<=1
624                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
625                         *param = (void*)0;
626                         return 0;
627                 }
628                 return fixup_routes("t_on_branch", &branch_rt, param);
629         }
630         return 0;
631 }
632
633 static int fixup_on_sl_reply(modparam_t type, void* val)
634 {
635         if ((type & PARAM_STRING) == 0) {
636                 LOG(L_ERR, "ERROR: tm: fixup_on_sl_reply: not a string parameter\n");
637                 return -1;
638         }
639
640         if (fixup_routes(0, &onreply_rt, &val))
641                 return -1;
642
643         goto_on_sl_reply = (int)(long)val;
644         return 0;
645 }
646
647
648
649 /* (char *hostname, char *port_nr) ==> (struct proxy_l *, -)  */
650 static int fixup_hostport2proxy(void** param, int param_no)
651 {
652         unsigned int port;
653         char *host;
654         int err;
655         struct proxy_l *proxy;
656         action_u_t *a;
657         str s;
658
659         DBG("TM module: fixup_hostport2proxy(%s, %d)\n", (char*)*param, param_no);
660         if (param_no==1){
661                 return 0;
662         } else if (param_no==2) {
663                 a = fixup_get_param(param, param_no, 1);
664                 host= a->u.string;
665                 port=str2s(*param, strlen(*param), &err);
666                 if (err!=0) {
667                         LOG(L_ERR, "TM module:fixup_hostport2proxy: bad port number <%s>\n",
668                                 (char*)(*param));
669                          return E_UNSPEC;
670                 }
671                 s.s = host;
672                 s.len = strlen(host);
673                 proxy=mk_proxy(&s, port, 0); /* FIXME: udp or tcp? */
674                 if (proxy==0) {
675                         LOG(L_ERR, "ERROR: fixup_hostport2proxy: bad host name in URI <%s>\n",
676                                 host );
677                         return E_BAD_ADDRESS;
678                 }
679                 /* success -- fix the first parameter to proxy now ! */
680
681                 a->u.data=proxy;
682                 return 0;
683         } else {
684                 LOG(L_ERR,"ERROR: fixup_hostport2proxy called with parameter #<>{1,2}\n");
685                 return E_BUG;
686         }
687 }
688
689 /* (char *$proto, char *$host:port) ==> (fparam, fparam)  */
690 static int fixup_proto_hostport2proxy(void** param, int param_no) {
691         int ret;
692
693         ret = fix_param(FPARAM_AVP, param);
694         if (ret <= 0) return ret;
695 /*      if (param_no = 1) {             FIXME: param_str currently does not offer INT/STR overloading
696                 ret = fix_param(FPARAM_INT, param);
697                 if (ret <= 0) return ret;
698         } */
699         if (fix_param(FPARAM_STRING, param) != 0) return -1;
700         return 0;
701 }
702
703
704 static int fixup_t_check_status(void** param, int param_no)
705 {
706         int ret;
707
708         ret = fix_param(FPARAM_AVP, param);
709         if (ret <= 0) return ret;
710
711         ret = fix_param(FPARAM_SELECT, param);
712         if (ret <= 0) return ret;
713
714         if (fix_param(FPARAM_REGEX, param) != 0) return -1;
715         return 0;
716 }
717
718
719 /***************************** init functions *****************************/
720 static int w_t_unref( struct sip_msg *foo, unsigned int flags, void *bar)
721 {
722         return t_unref(foo);
723 }
724
725
726 static int script_init( struct sip_msg *foo, unsigned int flags, void *bar)
727 {
728         /* we primarily reset all private memory here to make sure
729          * private values left over from previous message will
730          * not be used again */
731
732         /* make sure the new message will not inherit previous
733                 message's t_on_negative value
734         */
735         t_on_negative( 0 );
736         t_on_reply(0);
737         t_on_branch(0);
738         /* reset the kr status */
739         reset_kr(0);
740         /* set request mode so that multiple-mode actions know
741          * how to behave */
742         set_route_type(REQUEST_ROUTE);
743
744 #ifdef POSTPONE_MSG_CLONING
745         lumps_are_cloned = 0;
746 #endif
747         return 1;
748 }
749
750
751 static int mod_init(void)
752 {
753         DBG( "TM - (sizeof cell=%ld, sip_msg=%ld) initializing...\n",
754                         (long)sizeof(struct cell), (long)sizeof(struct sip_msg));
755
756         /* checking if we have sufficient bitmap capacity for given
757            maximum number of  branches */
758         if (MAX_BRANCHES+1>31) {
759                 LOG(L_CRIT, "Too many max UACs for UAC branch_bm_t bitmap: %d\n",
760                         MAX_BRANCHES );
761                 return -1;
762         }
763
764         if (init_callid() < 0) {
765                 LOG(L_CRIT, "Error while initializing Call-ID generator\n");
766                 return -1;
767         }
768
769         /* building the hash table*/
770         if (!init_hash_table()) {
771                 LOG(L_ERR, "ERROR: mod_init: initializing hash_table failed\n");
772                 return -1;
773         }
774
775         /* init static hidden values */
776         init_t();
777
778         if (tm_init_selects()==-1) {
779                 LOG(L_ERR, "ERROR: mod_init: select init failed\n");
780                 return -1;
781         }
782
783         /* the default timer values must be fixed-up before
784          * declaring the configuration (Miklos) */
785         if (tm_init_timers()==-1) {
786                 LOG(L_ERR, "ERROR: mod_init: timer init failed\n");
787                 return -1;
788         }
789         
790         /* the cancel branch flags must be fixed before declaring the 
791          * configuration */
792         if (cancel_b_flags_get(&default_tm_cfg.cancel_b_flags, 
793                                                         default_tm_cfg.cancel_b_flags)<0){
794                 LOG(L_ERR, "ERROR: mod_init: bad cancel branch method\n");
795                 return -1;
796         }
797
798 #ifdef USE_DNS_FAILOVER
799         if (default_tm_cfg.reparse_on_dns_failover && mhomed) {
800                 LOG(L_WARN, "WARNING: mod_init: "
801                         "reparse_on_dns_failover is enabled on a "
802                         "multihomed host -- check the readme of tm module!\n");
803         }
804 #endif
805
806         /* declare the configuration */
807         if (cfg_declare("tm", tm_cfg_def, &default_tm_cfg, cfg_sizeof(tm),
808                          &tm_cfg)) {
809                 LOG(L_ERR, "ERROR: mod_init: failed to declare the configuration\n");
810                 return -1;
811         }
812
813              /* First tm_stat initialization function only allocates the top level stat
814               * structure in shared memory, the initialization will complete in child
815               * init with init_tm_stats_child when the final value of estimated_process_count is
816               * known
817               */
818         if (init_tm_stats() < 0) {
819                 LOG(L_CRIT, "ERROR: mod_init: failed to init stats\n");
820                 return -1;
821         }
822
823         if (uac_init()==-1) {
824                 LOG(L_ERR, "ERROR: mod_init: uac_init failed\n");
825                 return -1;
826         }
827
828         if (init_tmcb_lists()!=1) {
829                 LOG(L_CRIT, "ERROR:tm:mod_init: failed to init tmcb lists\n");
830                 return -1;
831         }
832         
833         tm_init_tags();
834         init_twrite_lines();
835         if (init_twrite_sock() < 0) {
836                 LOG(L_ERR, "ERROR:tm:mod_init: Unable to create socket\n");
837                 return -1;
838         }
839
840         /* register post-script clean-up function */
841         if (register_script_cb( w_t_unref, POST_SCRIPT_CB|REQUEST_CB, 0)<0 ) {
842                 LOG(L_ERR,"ERROR:tm:mod_init: failed to register POST request "
843                         "callback\n");
844                 return -1;
845         }
846         if (register_script_cb( script_init, PRE_SCRIPT_CB|REQUEST_CB , 0)<0 ) {
847                 LOG(L_ERR,"ERROR:tm:mod_init: failed to register PRE request "
848                         "callback\n");
849                 return -1;
850         }
851
852         if (init_avp_params( fr_timer_param, fr_inv_timer_param,
853                                                  contacts_avp_param)<0 ){
854                 LOG(L_ERR,"ERROR:tm:mod_init: failed to process AVP params\n");
855                 return -1;
856         }
857 #ifdef WITH_EVENT_LOCAL_REQUEST
858         goto_on_local_req=route_lookup(&event_rt, "tm:local-request");
859         if (goto_on_local_req>=0 && event_rt.rlist[goto_on_local_req]==0)
860                 goto_on_local_req=-1; /* disable */
861 #endif /* WITH_EVENT_LOCAL_REQUEST */
862         if (goto_on_sl_reply && onreply_rt.rlist[goto_on_sl_reply]==0)
863                 WARN("empty/non existing on_sl_reply route\n");
864         tm_init = 1;
865         return 0;
866 }
867
868 static int child_init(int rank)
869 {
870         if (rank == PROC_INIT) {
871                 /* we must init stats when rank==PROC_INIT: after mod_init we know
872                  * the exact number of processes and we must init the shared structure
873                  * before any other process is starting (or else some new process
874                  * might try to use the stats before the stats array is allocated) */
875                 if (init_tm_stats_child() < 0) {
876                         ERR("Error while initializing tm statistics structures\n");
877                         return -1;
878                 }
879         }else if (child_init_callid(rank) < 0) { 
880                 /* don't init callid for PROC_INIT*/
881                 LOG(L_ERR, "ERROR: child_init: Error while initializing Call-ID"
882                                 " generator\n");
883                 return -2;
884         }
885         return 0;
886 }
887
888
889
890
891
892 /**************************** wrapper functions ***************************/
893 static int t_check_status(struct sip_msg* msg, char *p1, char *foo)
894 {
895         regmatch_t pmatch;
896         struct cell *t;
897         char *status, *s = NULL;
898         char backup;
899         int lowest_status, n, ret;
900         fparam_t* fp;
901         regex_t* re = NULL;
902         str tmp;
903         
904         fp = (fparam_t*)p1;
905         
906         /* first get the transaction */
907         if (t_check(msg, 0 ) == -1) return -1;
908         if ((t = get_t()) == 0) {
909                 LOG(L_ERR, "ERROR: t_check_status: cannot check status for a reply "
910                         "which has no T-state established\n");
911                 goto error;
912         }
913         backup = 0;
914         
915         switch(fp->type) {
916         case FPARAM_REGEX:
917                 re = fp->v.regex;
918                 break;
919                 
920         default:
921                 /* AVP or select, get the value and compile the regex */
922                 if (get_str_fparam(&tmp, msg, fp) < 0) goto error;
923                 s = pkg_malloc(tmp.len + 1);
924                 if (s == NULL) {
925                         ERR("Out of memory\n");
926                         goto error;
927                 }
928                 memcpy(s, tmp.s, tmp.len);
929                 s[tmp.len] = '\0';
930                 
931                 if ((re = pkg_malloc(sizeof(regex_t))) == 0) {
932                         ERR("No memory left\n");
933                         goto error;
934                 }
935                 
936                 if (regcomp(re, s, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
937                         ERR("Bad regular expression '%s'\n", s);
938                         goto error;
939                 }
940                 break;
941         }
942         
943         switch(route_type) {
944         case REQUEST_ROUTE:
945                 /* use the status of the last sent reply */
946                 status = int2str( t->uas.status, 0);
947                 break;
948                 
949         case ONREPLY_ROUTE:
950                 /* use the status of the current reply */
951                 status = msg->first_line.u.reply.status.s;
952                 backup = status[msg->first_line.u.reply.status.len];
953                 status[msg->first_line.u.reply.status.len] = 0;
954                 break;
955
956         case FAILURE_ROUTE:
957                 /* use the status of the winning reply */
958                 ret = t_pick_branch( -1, 0, t, &lowest_status);
959                 if (ret == -1) {
960                         /* t_pick_branch() retuns error also when there are only
961                          * blind UACs. Let us give it another chance including the
962                          * blind branches. */
963                         LOG(L_DBG, "DEBUG: t_check_status: t_pick_branch returned error, "
964                                 "trying t_pick_branch_blind\n");
965                         ret = t_pick_branch_blind(t, &lowest_status);
966                 }
967                 if (ret < 0) {
968                         LOG(L_CRIT,"BUG:t_check_status: t_pick_branch failed to get "
969                                 " a final response in FAILURE_ROUTE\n");
970                         goto error;
971                 }
972                 status = int2str( lowest_status , 0);
973                 break;
974
975         default:
976                 LOG(L_ERR,"ERROR:t_check_status: unsupported route type %d\n",route_type);
977                 goto error;
978         }
979
980         DBG("DEBUG:t_check_status: checked status is <%s>\n",status);
981         /* do the checking */
982         n = regexec(re, status, 1, &pmatch, 0);
983
984         if (backup) status[msg->first_line.u.reply.status.len] = backup;
985         if (s) pkg_free(s);
986         if ((fp->type != FPARAM_REGEX) && re) {
987                 regfree(re);
988                 pkg_free(re);
989         }
990
991         if (n!=0) return -1;
992         return 1;
993
994  error:
995         if (s) pkg_free(s);
996         if ((fp->type != FPARAM_REGEX) && re) {
997                 regfree(re);
998                 pkg_free(re);
999         }
1000         return -1;
1001 }
1002
1003
1004 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2)
1005 {
1006         return (t_check_msg( msg , 0  )==1) ? 1 : -1;
1007 }
1008
1009 inline static int w_t_lookup_cancel(struct sip_msg* msg, char* str, char* str2)
1010 {
1011         struct cell *ret;
1012         int i=0;
1013         if (msg->REQ_METHOD==METHOD_CANCEL) {
1014                 ret = t_lookupOriginalT( msg );
1015                 DBG("lookup_original: t_lookupOriginalT returned: %p\n", ret);
1016                 if (ret != T_NULL_CELL) {
1017                         /* If the parameter is set to 1, overwrite the message flags of
1018                          * the CANCEL with the flags of the INVITE */
1019                         if (str && (get_int_fparam(&i, msg, (fparam_t*)str)==0) && i)
1020                                 msg->flags = ret->uas.request->flags;
1021
1022                         /* The cell is reffed by t_lookupOriginalT, but T is not set.
1023                         So we must unref it before returning. */
1024                         UNREF(ret);
1025                         return 1;
1026                 }
1027         } else {
1028                 LOG(L_WARN, "WARNING: script error t_lookup_cancel() called for non-CANCEL request\n");
1029         }
1030         return -1;
1031 }
1032
1033 inline static int str2proto(char *s, int len) {
1034         if (len == 3 && !strncasecmp(s, "udp", 3))
1035                 return PROTO_UDP;
1036         else if (len == 3 && !strncasecmp(s, "tcp", 3))  /* tcp&tls checks will be passed in getproto() */
1037                 return PROTO_TCP;
1038         else if (len == 3 && !strncasecmp(s, "tls", 3))
1039                 return PROTO_TLS;       
1040         else if (len == 4 && !strncasecmp(s, "sctp", 4))
1041                 return PROTO_SCTP;
1042         else
1043                 return PROTO_NONE;
1044 }
1045
1046 inline static struct proxy_l* t_protoaddr2proxy(char *proto_par, char *addr_par) {
1047         struct proxy_l *proxy = 0;
1048         avp_t* avp;
1049         avp_value_t val;
1050         int proto, port, err;
1051         str s;
1052         char *c;
1053         
1054         switch(((fparam_t *)proto_par)->type) {
1055         case FPARAM_AVP:
1056                 if (!(avp = search_first_avp(((fparam_t *)proto_par)->v.avp.flags, ((fparam_t *)proto_par)->v.avp.name, &val, 0))) {
1057                         proto = PROTO_NONE;
1058                 } else {
1059                         if (avp->flags & AVP_VAL_STR) {
1060                                 proto = str2proto(val.s.s, val.s.len);
1061                         }
1062                         else {
1063                                 proto = val.n;
1064                         }
1065                 }
1066                 break;
1067
1068         case FPARAM_INT:
1069                 proto = ((fparam_t *)proto_par)->v.i;
1070                 break;
1071         case FPARAM_STRING:
1072                 proto = str2proto( ((fparam_t *)proto_par)->v.asciiz, strlen(((fparam_t *)proto_par)->v.asciiz));
1073                 break;
1074         default:
1075                 ERR("BUG: Invalid proto parameter value in t_protoaddr2proxy\n");
1076                 return 0;
1077         }
1078
1079
1080         switch(((fparam_t *)addr_par)->type) {
1081         case FPARAM_AVP:
1082                 if (!(avp = search_first_avp(((fparam_t *)addr_par)->v.avp.flags, ((fparam_t *)addr_par)->v.avp.name, &val, 0))) {
1083                         s.len = 0;
1084                 } else {
1085                         if ((avp->flags & AVP_VAL_STR) == 0) {
1086                                 LOG(L_ERR, "tm:t_protoaddr2proxy: avp <%.*s> value is not string\n",
1087                                         ((fparam_t *)addr_par)->v.avp.name.s.len, ((fparam_t *)addr_par)->v.avp.name.s.s);
1088                                 return 0;
1089                         }
1090                         s = val.s;
1091                 }
1092                 break;
1093
1094         case FPARAM_STRING:
1095                 s.s = ((fparam_t *) addr_par)->v.asciiz;
1096                 s.len = strlen(s.s);
1097                 break;
1098
1099         default:
1100                 ERR("BUG: Invalid addr parameter value in t_protoaddr2proxy\n");
1101                 return 0;
1102         }
1103
1104         port = 5060;
1105         if (s.len) {
1106                 c = memchr(s.s, ':', s.len);
1107                 if (c) {
1108                         port = str2s(c+1, s.len-(c-s.s+1), &err);
1109                         if (err!=0) {
1110                                 LOG(L_ERR, "tm:t_protoaddr2proxy: bad port number <%.*s>\n",
1111                                         s.len, s.s);
1112                                  return 0;
1113                         }
1114                         s.len = c-s.s;
1115                 }
1116         }
1117         if (!s.len) {
1118                 LOG(L_ERR, "tm: protoaddr2proxy: host name is empty\n");
1119                 return 0;
1120         }
1121         proxy=mk_proxy(&s, port, proto);
1122         if (proxy==0) {
1123                 LOG(L_ERR, "tm: protoaddr2proxy: bad host name in URI <%.*s>\n",
1124                         s.len, s.s );
1125                 return 0;
1126         }
1127         return proxy;
1128 }
1129
1130 inline static int _w_t_forward_nonack(struct sip_msg* msg, struct proxy_l* proxy,
1131         int proto)
1132 {
1133         struct cell *t;
1134         if (t_check( msg , 0 )==-1) {
1135                 LOG(L_ERR, "ERROR: forward_nonack: "
1136                                 "can't forward when no transaction was set up\n");
1137                 return -1;
1138         }
1139         t=get_t();
1140         if ( t && t!=T_UNDEFINED ) {
1141                 if (msg->REQ_METHOD==METHOD_ACK) {
1142                         LOG(L_WARN,"WARNING: you don't really want to fwd hbh ACK\n");
1143                         return -1;
1144                 }
1145                 return t_forward_nonack(t, msg, proxy, proto );
1146         } else {
1147                 DBG("DEBUG: forward_nonack: no transaction found\n");
1148                 return -1;
1149         }
1150 }
1151
1152
1153 inline static int w_t_forward_nonack( struct sip_msg* msg, char* proxy,
1154                                                                                 char* foo)
1155 {
1156         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_NONE);
1157 }
1158
1159
1160 inline static int w_t_forward_nonack_uri(struct sip_msg* msg, char *foo,
1161                                                                                                                                         char *bar)
1162 {
1163         return _w_t_forward_nonack(msg, 0, PROTO_NONE);
1164 }
1165
1166
1167 inline static int w_t_forward_nonack_udp( struct sip_msg* msg, char* proxy,
1168                                                                                 char* foo)
1169 {
1170         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_UDP);
1171 }
1172
1173
1174 #ifdef USE_TCP
1175 inline static int w_t_forward_nonack_tcp( struct sip_msg* msg, char* proxy,
1176                                                                                 char* foo)
1177 {
1178         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TCP);
1179 }
1180 #endif
1181
1182
1183 #ifdef USE_TLS
1184 inline static int w_t_forward_nonack_tls( struct sip_msg* msg, char* proxy,
1185                                                                                 char* foo)
1186 {
1187         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TLS);
1188 }
1189 #endif
1190
1191
1192 #ifdef USE_SCTP
1193 inline static int w_t_forward_nonack_sctp( struct sip_msg* msg, char* proxy,
1194                                                                                 char* foo)
1195 {
1196         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1197 }
1198 #endif
1199
1200
1201 inline static int w_t_forward_nonack_to( struct sip_msg  *p_msg ,
1202         char *proto_par, 
1203         char *addr_par   )
1204 {
1205         struct proxy_l *proxy;
1206         int r = -1;
1207         proxy = t_protoaddr2proxy(proto_par, addr_par);
1208         if (proxy) {
1209                 r = _w_t_forward_nonack(p_msg, proxy, proxy->proto);            
1210                 free_proxy(proxy);
1211         }
1212         return r;
1213 }
1214
1215
1216 inline static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
1217 {
1218         struct cell *t;
1219         int code, ret = -1;
1220         str reason;
1221         char* r;
1222
1223         if (msg->REQ_METHOD==METHOD_ACK) {
1224                 LOG(L_WARN, "WARNING: t_reply: ACKs are not replied\n");
1225                 return -1;
1226         }
1227         if (t_check( msg , 0 )==-1) return -1;
1228         t=get_t();
1229         if (!t) {
1230                 LOG(L_ERR, "ERROR: t_reply: cannot send a t_reply to a message "
1231                         "for which no T-state has been established\n");
1232                 return -1;
1233         }
1234
1235         if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
1236             code = cfg_get(tm, tm_cfg, default_code);
1237         }
1238         
1239         if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
1240                 r = cfg_get(tm, tm_cfg, default_reason);
1241         } else {
1242                 r = as_asciiz(&reason);
1243                 if (r == NULL) r = cfg_get(tm, tm_cfg, default_reason);
1244         }
1245         
1246         /* if called from reply_route, make sure that unsafe version
1247          * is called; we are already in a mutex and another mutex in
1248          * the safe version would lead to a deadlock
1249          */
1250          
1251         if (is_route_type(FAILURE_ROUTE)) {
1252                 DBG("DEBUG: t_reply_unsafe called from w_t_reply\n");
1253                 ret = t_reply_unsafe(t, msg, code, r);
1254         } else if (is_route_type(REQUEST_ROUTE)) {
1255                 ret = t_reply( t, msg, code, r);
1256         } else {
1257                 LOG(L_CRIT, "BUG: w_t_reply entered in unsupported mode\n");
1258                 ret = -1;
1259         }
1260
1261         if (r && (r != cfg_get(tm, tm_cfg, default_reason))) pkg_free(r);
1262         return ret;
1263 }
1264
1265
1266 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2)
1267 {
1268         struct cell *t;
1269         int ret;
1270         
1271         if (t_check( msg  , 0  )==-1) return -1;
1272         t=get_t();
1273         if ( t && t!=T_UNDEFINED ) {
1274                 ret = t_release_transaction( t );
1275                 t_unref(msg);
1276                 return ret;
1277         }
1278         return 1;
1279 }
1280
1281
1282 inline static int w_t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar)
1283 {
1284         struct cell *t;
1285
1286
1287         if (t_check( p_msg  , 0 )==-1)
1288                 return 1;
1289         t=get_t();
1290         if (t) {
1291                 if (p_msg->REQ_METHOD==METHOD_ACK) {
1292                         LOG(L_WARN, "WARNING: : ACKs transmit_replies not replied\n");
1293                         return -1;
1294                 }
1295                 return t_retransmit_reply( t );
1296         } else
1297                 return -1;
1298 }
1299
1300
1301 inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar )
1302 {
1303         /* t_newtran returns 0 on error (negative value means
1304            'transaction exists' */
1305         int ret;
1306         ret = t_newtran( p_msg );
1307         if (ret==E_SCRIPT) {
1308                 LOG(L_ERR, "ERROR: t_newtran: "
1309                         "transaction already in process %p\n", get_t() );
1310         }
1311         return ret;
1312 }
1313
1314
1315 inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo)
1316 {
1317         t_on_negative( (unsigned int )(long) go_to );
1318         return 1;
1319 }
1320
1321 inline static int w_t_on_branch( struct sip_msg* msg, char *go_to, char *foo)
1322 {
1323         t_on_branch( (unsigned int )(long) go_to );
1324         return 1;
1325 }
1326
1327
1328 inline static int w_t_on_reply( struct sip_msg* msg, char *go_to, char *foo )
1329 {
1330         t_on_reply( (unsigned int )(long) go_to );
1331         return 1;
1332 }
1333
1334
1335
1336 inline static int _w_t_relay_to(struct sip_msg  *p_msg ,
1337                                                                         struct proxy_l *proxy, int force_proto)
1338 {
1339         struct cell *t;
1340
1341         if (is_route_type(FAILURE_ROUTE)) {
1342                 t=get_t();
1343                 if (!t || t==T_UNDEFINED) {
1344                         LOG(L_CRIT, "BUG: w_t_relay_to: undefined T\n");
1345                         return -1;
1346                 }
1347                 if (t_forward_nonack(t, p_msg, proxy, force_proto)<=0 ) {
1348                         LOG(L_ERR, "ERROR: w_t_relay_to: t_relay_to failed\n");
1349                         /* let us save the error code, we might need it later
1350                         when the failure_route has finished (Miklos) */
1351                         tm_error=ser_error;
1352                         return -1;
1353                 }
1354                 return 1;
1355         }
1356         if (is_route_type(REQUEST_ROUTE))
1357                 return t_relay_to( p_msg, proxy, force_proto,
1358                         0 /* no replication */ );
1359         LOG(L_CRIT, "ERROR: w_t_relay_to: unsupported route type: %d\n", route_type);
1360         return 0;
1361 }
1362
1363
1364 inline static int w_t_relay_to_udp( struct sip_msg  *p_msg ,
1365                                                                         char *proxy,/* struct proxy_l * expected */
1366                                                                         char *_foo       /* nothing expected */ )
1367 {
1368         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_UDP);
1369 }
1370
1371 /* forward to uri, but force udp as transport */
1372 inline static int w_t_relay_to_udp_uri( struct sip_msg  *p_msg ,
1373                                                                                 char *_foo, char *_bar   )
1374 {
1375         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_UDP);
1376 }
1377
1378
1379 #ifdef USE_TCP
1380 inline static int w_t_relay_to_tcp( struct sip_msg  *p_msg ,
1381                                                                         char *proxy, /* struct proxy_l* */
1382                                                                         char *_foo       /* nothing expected */ )
1383 {
1384         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TCP);
1385 }
1386
1387 /* forward to uri, but force tcp as transport */
1388 inline static int w_t_relay_to_tcp_uri( struct sip_msg  *p_msg ,
1389                                                                                 char *_foo, char *_bar   )
1390 {
1391         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TCP);
1392 }
1393 #endif
1394
1395
1396 #ifdef USE_TLS
1397 inline static int w_t_relay_to_tls( struct sip_msg  *p_msg ,
1398                                                                         char *proxy, /* struct proxy_l* expected */
1399                                                                         char *_foo       /* nothing expected */ )
1400 {
1401         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TLS);
1402 }
1403
1404 /* forward to uri, but force tls as transport */
1405 inline static int w_t_relay_to_tls_uri( struct sip_msg  *p_msg ,
1406                                                                                 char *_foo, char *_bar   )
1407 {
1408         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TLS);
1409 }
1410 #endif
1411
1412
1413 #ifdef USE_SCTP
1414 inline static int w_t_relay_to_sctp( struct sip_msg  *p_msg ,
1415                                                                         char *proxy, /* struct proxy_l* */
1416                                                                         char *_foo       /* nothing expected */ )
1417 {
1418         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1419 }
1420
1421 /* forward to uri, but force tcp as transport */
1422 inline static int w_t_relay_to_sctp_uri( struct sip_msg  *p_msg ,
1423                                                                                 char *_foo, char *_bar   )
1424 {
1425         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_SCTP);
1426 }
1427 #endif
1428
1429
1430 inline static int w_t_relay_to_avp( struct sip_msg  *p_msg ,
1431                                                                         char *proto_par, 
1432                                                                         char *addr_par   )
1433 {
1434         struct proxy_l *proxy;
1435         int r = -1;
1436         
1437         proxy = t_protoaddr2proxy(proto_par, addr_par);
1438         if (proxy) {
1439                 r = _w_t_relay_to(p_msg, proxy, PROTO_NONE);
1440                 free_proxy(proxy);
1441         }
1442         return r;
1443 }
1444
1445 inline static int w_t_replicate_uri(struct sip_msg  *msg ,
1446                                 char *uri,       /* sip uri as string or variable */
1447                                 char *_foo       /* nothing expected */ )
1448 {
1449         struct proxy_l *proxy;
1450         struct sip_uri turi;
1451         str suri;
1452         int r = -1;
1453
1454         memset(&turi, 0, sizeof(struct sip_uri));
1455         if(fixup_get_svalue(msg, (gparam_p)uri, &suri)!=0)
1456         {
1457                 LM_ERR("invalid replicate uri parameter");
1458                 return -1;
1459         }
1460         if(parse_uri(suri.s, suri.len, &turi)!=0)
1461         {
1462                 LM_ERR("bad replicate SIP address!\n");
1463                 return -1;
1464         }
1465
1466         proxy=mk_proxy(&turi.host, turi.port_no, turi.proto);
1467         if (proxy==0) {
1468                 LM_ERR("cannot create proxy from URI <%.*s>\n",
1469                         suri.len, suri.s );
1470                 return -1;
1471         }
1472
1473         r = t_replicate(msg, proxy, proxy->proto);
1474         free_proxy(proxy);
1475         return r;
1476
1477 }
1478
1479 inline static int w_t_replicate( struct sip_msg  *p_msg ,
1480         char *proxy, /* struct proxy_l *proxy expected */
1481         char *_foo       /* nothing expected */ )
1482 {
1483         return t_replicate(p_msg, ( struct proxy_l *) proxy, p_msg->rcv.proto );
1484 }
1485
1486 inline static int w_t_replicate_udp( struct sip_msg  *p_msg ,
1487         char *proxy, /* struct proxy_l *proxy expected */
1488         char *_foo       /* nothing expected */ )
1489 {
1490         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_UDP );
1491 }
1492
1493
1494 #ifdef USE_TCP
1495 inline static int w_t_replicate_tcp( struct sip_msg  *p_msg ,
1496         char *proxy, /* struct proxy_l *proxy expected */
1497         char *_foo       /* nothing expected */ )
1498 {
1499         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TCP );
1500 }
1501 #endif
1502
1503
1504 #ifdef USE_TLS
1505 inline static int w_t_replicate_tls( struct sip_msg  *p_msg ,
1506         char *proxy, /* struct proxy_l *proxy expected */
1507         char *_foo       /* nothing expected */ )
1508 {
1509         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TLS );
1510 }
1511 #endif
1512
1513
1514 #ifdef USE_SCTP
1515 inline static int w_t_replicate_sctp( struct sip_msg  *p_msg ,
1516         char *proxy, /* struct proxy_l *proxy expected */
1517         char *_foo       /* nothing expected */ )
1518 {
1519         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_SCTP );
1520 }
1521 #endif
1522
1523
1524 inline static int w_t_replicate_to( struct sip_msg  *p_msg ,
1525         char *proto_par, 
1526         char *addr_par   )
1527 {
1528         struct proxy_l *proxy;
1529         int r = -1;
1530         proxy = t_protoaddr2proxy(proto_par, addr_par);
1531         if (proxy) {
1532                 r = t_replicate(p_msg, proxy, proxy->proto);            
1533                 free_proxy(proxy);
1534         }
1535         return r;
1536 }
1537
1538 inline static int w_t_relay( struct sip_msg  *p_msg ,
1539                                                                 char *_foo, char *_bar)
1540 {
1541         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_NONE);
1542 }
1543
1544
1545 /* like t_relay but use the specified destination and port and the same proto
1546  * as the received msg */
1547 static int w_t_relay2( struct sip_msg  *p_msg , char *proxy,
1548                                                                 char *_foo)
1549 {
1550         return _w_t_relay_to(p_msg, (struct proxy_l*) proxy, p_msg->rcv.proto);
1551 }
1552
1553
1554 /* relays CANCEL at the beginning of the script */
1555 inline static int w_t_relay_cancel( struct sip_msg  *p_msg ,
1556                                                 char *_foo, char *_bar)
1557 {
1558         if (p_msg->REQ_METHOD!=METHOD_CANCEL)
1559                 return 1;
1560
1561         /* it makes no sense to use this function without reparse_invite=1 */
1562         if (!cfg_get(tm, tm_cfg, reparse_invite))
1563                 LOG(L_WARN, "WARNING: t_relay_cancel is probably used with "
1564                         "wrong configuration, check the readme for details\n");
1565
1566         return t_relay_cancel(p_msg);
1567 }
1568
1569 /* set fr_inv_timeout & or fr_timeout; 0 means: use the default value */
1570 static int t_set_fr_all(struct sip_msg* msg, char* p1, char* p2)
1571 {
1572     int fr, fr_inv;
1573
1574     if (get_int_fparam(&fr_inv, msg, (fparam_t*)p1) < 0) return -1;
1575     if (p2) {
1576         if (get_int_fparam(&fr, msg, (fparam_t*)p2) < 0) return -1;
1577     } else {
1578         fr = 0;
1579     }
1580
1581     return t_set_fr(msg, fr_inv, fr);
1582 }
1583
1584 static int t_set_fr_inv(struct sip_msg* msg, char* fr_inv, char* foo)
1585 {
1586         return t_set_fr_all(msg, fr_inv, (char*)0);
1587 }
1588
1589 /* reset fr_timer and fr_inv_timer to the default values */
1590 static int w_t_reset_fr(struct sip_msg* msg, char* foo, char* bar)
1591 {
1592         return t_reset_fr();
1593 }
1594
1595 /* set retr. intervals per transaction; 0 means: use the default value */
1596 static int w_t_set_retr(struct sip_msg* msg, char* p1, char* p2)
1597 {
1598         int t1, t2;
1599         
1600         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1601         if (p2) {
1602                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1603         } else {
1604                 t2 = 0;
1605         }
1606 #ifdef TM_DIFF_RT_TIMEOUT
1607         return t_set_retr(msg, t1, t2);
1608 #else
1609         ERR("w_t_set_retr: support for changing retransmission intervals on "
1610                         "the fly not compiled in (re-compile tm with"
1611                         " -DTM_DIFF_RT_TIMEOUT)\n");
1612         return -1;
1613 #endif
1614 }
1615
1616 /* reset retr. t1 and t2 to the default values */
1617 int w_t_reset_retr(struct sip_msg* msg, char* foo, char* bar)
1618 {
1619 #ifdef TM_DIFF_RT_TIMEOUT
1620         return t_reset_retr();
1621 #else
1622         ERR("w_t_reset_retr: support for changing retransmission intervals on "
1623                         "the fly not compiled in (re-compile tm with"
1624                         " -DTM_DIFF_RT_TIMEOUT)\n");
1625         return -1;
1626 #endif
1627 }
1628
1629 /* set maximum transaction lifetime for inv & noninv */
1630 static int w_t_set_max_lifetime(struct sip_msg* msg, char* p1, char* p2)
1631 {
1632         int t1, t2;
1633         
1634         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1635         if (p2) {
1636                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1637         } else {
1638                 t2 = 0;
1639         }
1640         return t_set_max_lifetime(msg, t1, t2);
1641 }
1642
1643 /* reset maximum invite/non-invite lifetime to the default value */
1644 int w_t_reset_max_lifetime(struct sip_msg* msg, char* foo, char* bar)
1645 {
1646         return t_reset_max_lifetime();
1647 }
1648
1649
1650
1651 /* helper macro, builds a function for setting a cell flag from the script.
1652    e.g. T_SET_FLAG_GEN_FUNC(t_set_foo, T_FOO) =>
1653    static int t_set_foo(struct sip_msg* msg, char*, char* )
1654    that will expect fparam as first param and will set or reset T_FOO
1655    in the current or next to be created transaction. */
1656 #define T_SET_FLAG_GEN_FUNC(fname, T_FLAG_NAME) \
1657 static int fname(struct sip_msg* msg, char* p1, char* p2) \
1658 { \
1659         int state; \
1660         struct cell* t; \
1661         unsigned int set_flags; \
1662         unsigned int reset_flags; \
1663          \
1664         if (get_int_fparam(&state, msg, (fparam_t*)p1) < 0) return -1; \
1665         t=get_t(); \
1666         /* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction; \
1667          * in REQUEST_ROUTE T will be set only if the transaction was already  \
1668          * created; if not -> use the static variables */ \
1669         if (!t || t==T_UNDEFINED ){ \
1670                 set_flags=get_msgid_val(user_cell_set_flags, msg->id, int); \
1671                 reset_flags=get_msgid_val(user_cell_reset_flags, msg->id, int); \
1672                 if (state){ \
1673                         /* set */ \
1674                         set_flags|= T_FLAG_NAME; \
1675                         reset_flags&=~T_FLAG_NAME; \
1676                 }else{ \
1677                         /* reset */ \
1678                         set_flags&=~T_FLAG_NAME; \
1679                         reset_flags|=T_FLAG_NAME; \
1680                 } \
1681                 set_msgid_val(user_cell_set_flags, msg->id, int, set_flags); \
1682                 set_msgid_val(user_cell_reset_flags, msg->id, int, reset_flags); \
1683         }else{ \
1684                 if (state) \
1685                         t->flags|=T_FLAG_NAME; \
1686                 else \
1687                         t->flags&=~T_FLAG_NAME; \
1688         } \
1689         return 1; \
1690 }
1691
1692
1693
1694 /* set automatically sending 100 replies on/off for the current or
1695  * next to be created transaction */
1696 T_SET_FLAG_GEN_FUNC(t_set_auto_inv_100, T_AUTO_INV_100)
1697
1698
1699 /* set 6xx handling for the current or next to be created transaction */
1700 T_SET_FLAG_GEN_FUNC(t_set_disable_6xx, T_DISABLE_6xx)
1701
1702
1703 /* disable dns failover for the current transaction */
1704 T_SET_FLAG_GEN_FUNC(t_set_disable_failover, T_DISABLE_FAILOVER)
1705
1706
1707
1708 /* script function, FAILURE_ROUTE only, returns true if the 
1709  * choosed "failure" branch failed because of a timeout, 
1710  * -1 otherwise */
1711 int t_branch_timeout(struct sip_msg* msg, char* foo, char* bar)
1712 {
1713         return (msg->msg_flags & FL_TIMEOUT)?1:-1;
1714 }
1715
1716
1717
1718 /* script function, FAILURE_ROUTE only, returns true if the 
1719  * choosed "failure" branch ever received a reply, -1 otherwise */
1720 int t_branch_replied(struct sip_msg* msg, char* foo, char* bar)
1721 {
1722         return (msg->msg_flags & FL_REPLIED)?1:-1;
1723 }
1724
1725
1726
1727 /* script function, returns: 1 if the transaction was canceled, -1 if not */
1728 int t_is_canceled(struct sip_msg* msg, char* foo, char* bar)
1729 {
1730         struct cell *t;
1731         int ret;
1732         
1733         
1734         if (t_check( msg , 0 )==-1) return -1;
1735         t=get_t();
1736         if ((t==0) || (t==T_UNDEFINED)){
1737                 LOG(L_ERR, "ERROR: t_is_canceled: cannot check a message "
1738                         "for which no T-state has been established\n");
1739                 ret=-1;
1740         }else{
1741                 ret=(t->flags & T_CANCELED)?1:-1;
1742         }
1743         return ret;
1744 }
1745
1746 /* script function, returns: 1 if the transaction lifetime interval has already elapsed, -1 if not */
1747 int t_is_expired(struct sip_msg* msg, char* foo, char* bar)
1748 {
1749         struct cell *t;
1750         int ret;
1751         
1752         
1753         if (t_check( msg , 0 )==-1) return -1;
1754         t=get_t();
1755         if ((t==0) || (t==T_UNDEFINED)){
1756                 LOG(L_ERR, "ERROR: t_is_expired: cannot check a message "
1757                         "for which no T-state has been established\n");
1758                 ret=-1;
1759         }else{
1760                 ret=(TICKS_GT(t->end_of_life, get_ticks_raw()))?-1:1;
1761         }
1762         return ret;
1763 }
1764
1765 /* script function, returns: 1 if any of the branches did timeout, -1 if not */
1766 int t_any_timeout(struct sip_msg* msg, char* foo, char* bar)
1767 {
1768         struct cell *t;
1769         int r;
1770         
1771         if (t_check( msg , 0 )==-1) return -1;
1772         t=get_t();
1773         if ((t==0) || (t==T_UNDEFINED)){
1774                 LOG(L_ERR, "ERROR: t_any_timeout: cannot check a message "
1775                         "for which no T-state has been established\n");
1776                 return -1;
1777         }else{
1778                 for (r=0; r<t->nr_of_outgoings; r++){
1779                         if (t->uac[r].request.flags & F_RB_TIMEOUT)
1780                                 return 1;
1781                 }
1782         }
1783         return -1;
1784 }
1785
1786
1787
1788 /* script function, returns: 1 if any of the branches received at leat one
1789  * reply, -1 if not */
1790 int t_any_replied(struct sip_msg* msg, char* foo, char* bar)
1791 {
1792         struct cell *t;
1793         int r;
1794         
1795         if (t_check( msg , 0 )==-1) return -1;
1796         t=get_t();
1797         if ((t==0) || (t==T_UNDEFINED)){
1798                 LOG(L_ERR, "ERROR: t_any_replied: cannot check a message "
1799                         "for which no T-state has been established\n");
1800                 return -1;
1801         }else{
1802                 for (r=0; r<t->nr_of_outgoings; r++){
1803                         if (t->uac[r].request.flags & F_RB_REPLIED)
1804                                 return 1;
1805                 }
1806         }
1807         return -1;
1808 }
1809
1810
1811
1812 /* script function, returns: 1 if any of the branches received the
1813  *  reply code "status" */
1814 int t_grep_status(struct sip_msg* msg, char* status, char* bar)
1815 {
1816         struct cell *t;
1817         int r;
1818         int code;
1819         
1820     if (get_int_fparam(&code, msg, (fparam_t*)status) < 0) return -1;
1821         if (t_check( msg , 0 )==-1) return -1;
1822         t=get_t();
1823         if ((t==0) || (t==T_UNDEFINED)){
1824                 LOG(L_ERR, "ERROR: t_any_replied: cannot check a message "
1825                         "for which no T-state has been established\n");
1826                 return -1;
1827         }else{
1828                 for (r=0; r<t->nr_of_outgoings; r++){
1829                         if ((t->uac[r].last_received==code)  && 
1830                                         (t->uac[r].request.flags & F_RB_REPLIED))
1831                                 return 1;
1832                 }
1833         }
1834         return -1;
1835 }
1836
1837 /* drop all the existing replies in failure_route to make sure
1838  * that none of them is picked up again */
1839 static int w_t_drop_replies(struct sip_msg* msg, char* foo, char* bar)
1840 {
1841         if(foo==NULL)
1842                 t_drop_replies(1);
1843         else if(*foo=='n')
1844                 t_drop_replies(0);
1845         else if(*foo=='l')
1846                 t_drop_replies(2);
1847         else
1848                 t_drop_replies(1);
1849         return 1;
1850 }
1851
1852 /* save the message lumps after t_newtran() but before t_relay() */
1853 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar)
1854 {
1855 #ifdef POSTPONE_MSG_CLONING
1856         struct cell *t;
1857
1858         t=get_t();
1859         if (!t || t==T_UNDEFINED) {
1860                 LOG(L_ERR, "ERROR: w_t_save_lumps: transaction has not been created yet\n");
1861                 return -1;
1862         }
1863
1864         if (save_msg_lumps(t->uas.request, msg)) {
1865                 LOG(L_ERR, "ERROR: w_t_save_lumps: "
1866                         "failed to save the message lumps\n");
1867                 return -1;
1868         }
1869         return 1;
1870 #else
1871         LOG(L_ERR, "ERROR: w_t_save_lumps: POSTPONE_MSG_CLONING is not defined,"
1872                         " thus, the functionality is not supported\n");
1873         return -1;
1874 #endif
1875 }
1876
1877 static rpc_export_t tm_rpc[] = {
1878         {"tm.cancel", rpc_cancel,   rpc_cancel_doc,   0},
1879         {"tm.reply",  rpc_reply,    rpc_reply_doc,    0},
1880         {"tm.stats",  tm_rpc_stats, tm_rpc_stats_doc, 0},
1881         {"tm.hash_stats",  tm_rpc_hash_stats, tm_rpc_hash_stats_doc, 0},
1882         {"tm.t_uac_start", rpc_t_uac_start, rpc_t_uac_start_doc, 0 },
1883         {"tm.t_uac_wait",  rpc_t_uac_wait,  rpc_t_uac_wait_doc, RET_ARRAY},
1884         {0, 0, 0, 0}
1885 };
1886
1887 /* wrapper function needed after changes in w_t_reply */
1888 int w_t_reply_wrp(struct sip_msg *m, unsigned int code, char *txt)
1889 {
1890         fparam_t c;
1891         fparam_t r;
1892         
1893         c.type = FPARAM_INT;
1894         c.orig = NULL; /* ? */
1895         c.v.i = code;
1896         
1897         r.type = FPARAM_STRING;
1898         r.orig = NULL; /* ? */
1899         r.v.asciiz = txt;
1900
1901         return w_t_reply(m, (char *)&c, (char*)&r);
1902 }
1903
1904
1905
1906 /** script function, check if a msg is assoc. to a transaction.
1907  * @return -1 (not), 1 (reply, e2e ack or cancel for an existing transaction),
1908  *          0 (request retransmission, ack to negative reply or ack to local
1909  *           transaction)
1910  * Note: the e2e ack matching works only for local e2e acks or for
1911  *       transactions with E2EACK* callbacks installed (but even in this
1912  *       case matching E2EACKs on proxied transaction is not entirely
1913  *       reliable: if the ACK  is delayed the proxied transaction might
1914  *       be already deleted when it reaches the proxy (wait_timeout))
1915  */
1916 static int t_check_trans(struct sip_msg* msg, char* foo, char* bar)
1917 {
1918         struct cell* t;
1919         
1920         if (msg->first_line.type==SIP_REPLY)
1921                 return w_t_check(msg, 0 ,0);
1922         else if (msg->REQ_METHOD==METHOD_CANCEL)
1923                 return w_t_lookup_cancel(msg, 0, 0);
1924         else{
1925                 switch(t_check_msg(msg, 0)){
1926                         case -2: /* possible e2e ack */
1927                                 return 1;
1928                         case 1: /* found */
1929                                 t=get_t();
1930                                 if (msg->REQ_METHOD==METHOD_ACK){
1931                                         /* ack to neg. reply  or ack to local trans.
1932                                            => process it and end the script */
1933                                         /* FIXME: there's no way to distinguish here 
1934                                            between acks to local trans. and neg. acks */
1935                                         if (unlikely(has_tran_tmcbs(t, TMCB_ACK_NEG_IN)))
1936                                                 run_trans_callbacks(TMCB_ACK_NEG_IN, t, msg,
1937                                                                                         0, msg->REQ_METHOD);
1938                                         t_release_transaction(t);
1939                                 } else {
1940                                         /* is a retransmission */
1941                                         if (unlikely(has_tran_tmcbs(t, TMCB_REQ_RETR_IN)))
1942                                                 run_trans_callbacks(TMCB_REQ_RETR_IN, t, msg,
1943                                                                                         0, msg->REQ_METHOD);
1944                                         t_retransmit_reply(t);
1945                                 }
1946                                 /* no need for UNREF(t); set_t(0) - the end-of-script
1947                                    t_unref callback will take care of them */
1948                                 return 0; /* return from the script */
1949                 }
1950                 /* not found or error */
1951         }
1952         return -1;
1953 }
1954
1955 static int hexatoi(str *s, unsigned int* result)
1956 {
1957         int i, xv, fact;
1958
1959         /* more than 32bit hexa? */
1960         if (s->len>8)
1961                 return -1;
1962
1963         *result = 0;
1964         fact = 1;
1965
1966         for(i=s->len-1; i>=0 ;i--)
1967         {
1968                 xv = hex2int(s->s[i]);
1969                 if(xv<0)
1970                         return -1;
1971
1972                 *result += (xv * fact);
1973                 fact *= 16;
1974         }
1975         return 0;
1976 }
1977
1978 static int fixup_t_relay_to(void** param, int param_no)
1979 {
1980
1981         int port;
1982         int proto;
1983         unsigned int flags;
1984         struct proxy_l *proxy;
1985         action_u_t *a;
1986         str s;
1987         str host;
1988
1989         s.s = (char*)*param;
1990         s.len = strlen(s.s);
1991         LM_DBG("fixing (%s, %d)\n", s.s, param_no);
1992         if (param_no==1){
1993                 a = fixup_get_param(param, param_no, 2);
1994                 if(a==NULL)
1995                 {
1996                         LM_CRIT("server error for parameter <%s>\n",s.s);
1997                         return E_UNSPEC;
1998                 }
1999                 if(a->u.string!=NULL) {
2000                         /* second parameter set, first should be proxy addr */
2001                         if (parse_phostport(s.s, &host.s, &host.len, &port, &proto)!=0){
2002                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2003                                 return E_UNSPEC;
2004                         }
2005
2006                         proxy = mk_proxy(&host, port, proto);
2007                         if (proxy==0) {
2008                                 LM_ERR("failed to build proxy structure for <%.*s>\n", host.len, host.s );
2009                                 return E_UNSPEC;
2010                         }
2011                         *(param)=proxy;
2012                         return 0;
2013                 } else {
2014                         /* no second parameter, then is proxy addr or flags */
2015                         flags = 0;
2016                         if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2017                                 s.s += 2;
2018                                 s.len -= 2;
2019                                 if(hexatoi(&s, &flags)<0)
2020                                 {
2021                                         LM_CRIT("invalid hexa flags <%s>\n", s.s);
2022                                         return E_UNSPEC;
2023                                 }
2024                                 a->u.data = (void*)(unsigned long int)flags;
2025                                 *(param)= 0;
2026                                 return 0;
2027                         } else {
2028                                 if(str2int(&s, &flags)==0)
2029                                 {
2030                                         a->u.data = (void*)(unsigned long int)flags;
2031                                         *(param)= 0;
2032                                         return 0;
2033                                 } else {
2034                                         /* try proxy */
2035                                         if (parse_phostport(s.s, &host.s, &host.len, &port, &proto)!=0){
2036                                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2037                                                 return E_UNSPEC;
2038                                         }
2039
2040                                         proxy = mk_proxy(&host, port, proto);
2041                                         if (proxy==0) {
2042                                                 LM_ERR("failed to build proxy structure for <%.*s>\n", host.len, host.s );
2043                                                 return E_UNSPEC;
2044                                         }
2045                                         *(param)=proxy;
2046                                         return 0;
2047                                 }
2048                         }
2049                 }
2050         } else if (param_no==2) {
2051                 /* flags */
2052                 flags = 0;
2053                 if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2054                         s.s += 2;
2055                         s.len -= 2;
2056                         if(hexatoi(&s, &flags)<0)
2057                         {
2058                                 LM_CRIT("invalid hexa flags <%s>\n", s.s);
2059                                 return E_UNSPEC;
2060                         }
2061                         *(param) = (void*)(unsigned long int)flags;
2062                         return 0;
2063                 } else {
2064                         if(str2int(&s, &flags)==0)
2065                         {
2066                                 *(param) = (void*)(unsigned long int)flags;
2067                                 return 0;
2068                         } else {
2069                                 LM_CRIT("invalid flags <%s>\n", s.s);
2070                                 return E_UNSPEC;
2071                         }
2072                 }
2073         } else {
2074                 LM_ERR("invalid parameter number %d\n", param_no);
2075                 return E_BUG;
2076         }
2077 }
2078
2079
2080 inline static int w_t_relay_to(struct sip_msg *msg, char *proxy, char *flags)
2081 {
2082         unsigned int fl;
2083         struct proxy_l *px;
2084         fparam_t param;
2085
2086         fl = (unsigned int)(long)(void*)flags;
2087         px = (struct proxy_l*)proxy;
2088
2089         if(flags!=0)
2090         {
2091                 memset(&param, 0, sizeof(fparam_t));
2092                 param.type = FPARAM_INT;
2093                 /* no auto 100 trying */
2094                 if(fl&1) {
2095                         param.v.i = 0;
2096                         t_set_auto_inv_100(msg, (char*)(&param), 0);
2097                 }
2098                 /* no auto negative reply - not implemented */
2099                 /*
2100                 if(fl&2) {
2101                         param.v.i = 1;
2102                         t_set_disable_internal_reply(msg, (char*)param, 0);
2103                 }
2104                 */
2105                 /* no dns failover */
2106                 if(fl&4) {
2107                         param.v.i = 1;
2108                         t_set_disable_failover(msg, (char*)(&param), 0);
2109                 }
2110         }
2111         return _w_t_relay_to(msg, px, PROTO_NONE);
2112 }
2113