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