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