5adef0f02f9c877587b8dc000a262ea20d18fa4c
[sip-router] / src / modules / tm / tm.c
1 /*
2  * TM (transaction management) module
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 /** TM :: Module API (core).
24  * @file
25  * @ingroup tm
26  */
27
28 /**
29  * @defgroup tm TM :: Transaction stateful proxy support
30  *
31  * The TM module enables stateful processing of SIP transactions. The main use
32  * of stateful logic, which is costly in terms of memory and CPU, is some
33  * services that inherently need state.
34  *
35  * For example, transaction-based accounting (module acc) needs to process
36  * transaction state as opposed to individual messages, and any kinds of
37  * forking must be implemented statefully. Other use of stateful processing
38  * is it trading CPU caused by retransmission processing for memory.
39  * That however only makes sense if CPU consumption per request is huge.
40  * For example, if you want to avoid costly DNS resolution for every
41  * retransmission of a request to an unresolvable destination, use stateful
42  * mode. Then, only the initial message burdens server by DNS queries,
43  * subsequent retransmissions will be dropped and will not result in more
44  * processes blocked by DNS resolution. The price is more memory consumption
45  * and higher processing latency.
46  */
47
48
49 #include "defs.h"
50
51
52 #include <stdio.h>
53 #include <string.h>
54 #include <netdb.h>
55
56 #include "../../core/sr_module.h"
57 #include "../../core/dprint.h"
58 #include "../../core/error.h"
59 #include "../../core/ut.h"
60 #include "../../core/script_cb.h"
61 #include "../../core/usr_avp.h"
62 #include "../../core/mem/mem.h"
63 #include "../../core/route_struct.h"
64 #include "../../core/route.h"
65 #include "../../core/cfg/cfg.h"
66 #include "../../core/globals.h"
67 #include "../../core/timer_ticks.h"
68 #include "../../core/mod_fix.h"
69 #include "../../core/kemi.h"
70
71 #include "config.h"
72 #include "sip_msg.h"
73 #include "h_table.h"
74 #include "t_hooks.h"
75 #include "tm_load.h"
76 #include "ut.h"
77 #include "t_reply.h"
78 #include "uac.h"
79 #include "t_fwd.h"
80 #include "t_lookup.h"
81 #include "t_stats.h"
82 #include "callid.h"
83 #include "t_cancel.h"
84 #include "t_fifo.h"
85 #include "timer.h"
86 #include "t_msgbuilder.h"
87 #include "select.h"
88 #include "t_serial.h"
89 #include "rpc_uac.h"
90
91 MODULE_VERSION
92
93 /* fixup functions */
94 static int fixup_hostport2proxy(void** param, int param_no);
95 static int fixup_proto_hostport2proxy(void** param, int param_no);
96 static int fixup_on_failure(void** param, int param_no);
97 static int fixup_on_branch_failure(void** param, int param_no);
98 static int fixup_on_reply(void** param, int param_no);
99 static int fixup_on_branch(void** param, int param_no);
100 static int fixup_t_reply(void** param, int param_no);
101 static int fixup_on_sl_reply(modparam_t type, void* val);
102 static int fixup_t_relay_to(void** param, int param_no);
103 static int fixup_t_is_set(void** param, int param_no);
104
105 /* init functions */
106 static int mod_init(void);
107 static int child_init(int rank);
108
109
110 /* exported functions */
111 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2);
112 inline static int w_t_lookup_cancel(struct sip_msg* msg, char* str, char* str2);
113 inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2);
114 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2);
115 inline static int w_t_retransmit_reply(struct sip_msg* p_msg, char* foo,
116                 char* bar );
117 inline static int w_t_newtran(struct sip_msg* p_msg, char* foo, char* bar );
118 inline static int w_t_relay( struct sip_msg  *p_msg , char *_foo, char *_bar);
119 inline static int w_t_relay2( struct sip_msg  *p_msg , char *proxy, char*);
120 inline static int w_t_relay_to_udp( struct sip_msg  *p_msg , char *proxy,
121                 char *);
122 inline static int w_t_relay_to_udp_uri( struct sip_msg  *p_msg , char*, char*);
123 #ifdef USE_TCP
124 inline static int w_t_relay_to_tcp( struct sip_msg  *p_msg , char *proxy,
125                 char *);
126 inline static int w_t_relay_to_tcp_uri( struct sip_msg  *p_msg , char*, char*);
127 #endif
128 #ifdef USE_TLS
129 inline static int w_t_relay_to_tls( struct sip_msg  *p_msg , char *proxy,
130                 char *);
131 inline static int w_t_relay_to_tls_uri( struct sip_msg  *p_msg , char*, char*);
132 #endif
133 #ifdef USE_SCTP
134 inline static int w_t_relay_to_sctp( struct sip_msg  *p_msg , char *proxy,
135                 char *);
136 inline static int w_t_relay_to_sctp_uri( struct sip_msg*, char*, char*);
137 #endif
138 inline static int w_t_relay_to_avp(struct sip_msg* msg, char* str,char*);
139 inline static int w_t_relay_to(struct sip_msg* msg, char* str,char*);
140 inline static int w_t_replicate_uri( struct sip_msg  *p_msg ,
141                 char *uri,       /* sip uri as string or variable */
142                 char *_foo       /* nothing expected */ );
143 inline static int w_t_replicate( struct sip_msg  *p_msg ,
144                 char *proxy, /* struct proxy_l *proxy expected */
145                 char *_foo       /* nothing expected */ );
146 inline static int w_t_replicate_udp( struct sip_msg  *p_msg ,
147                 char *proxy, /* struct proxy_l *proxy expected */
148                 char *_foo       /* nothing expected */ );
149 #ifdef USE_TCP
150 inline static int w_t_replicate_tcp( struct sip_msg  *p_msg ,
151                 char *proxy, /* struct proxy_l *proxy expected */
152                 char *_foo       /* nothing expected */ );
153 #endif
154 #ifdef USE_TLS
155 inline static int w_t_replicate_tls( struct sip_msg  *p_msg ,
156                 char *proxy, /* struct proxy_l *proxy expected */
157                 char *_foo       /* nothing expected */ );
158 #endif
159 #ifdef USE_SCTP
160 inline static int w_t_replicate_sctp( struct sip_msg  *p_msg ,
161                 char *proxy, /* struct proxy_l *proxy expected */
162                 char *_foo       /* nothing expected */ );
163 #endif
164 inline static int w_t_replicate_to(struct sip_msg* msg, char* str,char*);
165 inline static int w_t_forward_nonack(struct sip_msg* msg, char* str, char* );
166 inline static int w_t_forward_nonack_uri(struct sip_msg* msg, char* str,char*);
167 inline static int w_t_forward_nonack_udp(struct sip_msg* msg, char* str,char*);
168 #ifdef USE_TCP
169 inline static int w_t_forward_nonack_tcp(struct sip_msg*, char* str,char*);
170 #endif
171 #ifdef USE_TLS
172 inline static int w_t_forward_nonack_tls(struct sip_msg*, char* str,char*);
173 #endif
174 #ifdef USE_SCTP
175 inline static int w_t_forward_nonack_sctp(struct sip_msg*, char* str,char*);
176 #endif
177 inline static int w_t_forward_nonack_to(struct sip_msg* msg, char* str,char*);
178 inline static int w_t_relay_cancel(struct sip_msg *p_msg, char *_foo,
179                 char *_bar);
180 inline static int w_t_on_failure(struct sip_msg* msg, char *go_to, char *foo);
181 inline static int w_t_on_branch_failure(struct sip_msg* msg, char *go_to,
182                 char *foo);
183 inline static int w_t_on_branch(struct sip_msg* msg, char *go_to, char *foo);
184 inline static int w_t_on_reply(struct sip_msg* msg, char *go_to, char *foo );
185 inline static int t_check_status(struct sip_msg* msg, char *match, char *foo);
186 static int t_set_fr_inv(struct sip_msg* msg, char* fr_inv, char* foo);
187 static int t_set_fr_all(struct sip_msg* msg, char* fr_inv, char* fr);
188 static int w_t_reset_fr(struct sip_msg* msg, char* foo, char* bar);
189 static int w_t_set_retr(struct sip_msg* msg, char* retr_t1, char* retr_t2);
190 static int w_t_reset_retr(struct sip_msg* msg, char* foo, char* bar);
191 static int w_t_set_max_lifetime(struct sip_msg* msg, char* inv, char* noninv);
192 static int w_t_reset_max_lifetime(struct sip_msg* msg, char* foo, char* bar);
193 static int w_t_set_auto_inv_100(struct sip_msg* msg, char* on_off, char* foo);
194 static int w_t_set_disable_6xx(struct sip_msg* msg, char* on_off, char* foo);
195 static int w_t_set_disable_failover(struct sip_msg* msg, char* on_off, char* f);
196 #ifdef CANCEL_REASON_SUPPORT
197 static int w_t_set_no_e2e_cancel_reason(struct sip_msg* msg, char* on_off,
198                 char* f);
199 #endif /* CANCEL_REASON_SUPPORT */
200 static int w_t_set_disable_internal_reply(struct sip_msg* msg, char* on_off,
201                 char* f);
202 static int w_t_branch_timeout(struct sip_msg* msg, char*, char*);
203 static int w_t_branch_replied(struct sip_msg* msg, char*, char*);
204 static int w_t_any_timeout(struct sip_msg* msg, char*, char*);
205 static int w_t_any_replied(struct sip_msg* msg, char*, char*);
206 static int w_t_is_canceled(struct sip_msg* msg, char*, char*);
207 static int w_t_is_expired(struct sip_msg* msg, char*, char*);
208 static int w_t_is_retr_async_reply(struct sip_msg* msg, char*, char*);
209 static int w_t_grep_status(struct sip_msg* msg, char*, char*);
210 static int w_t_drop_replies(struct sip_msg* msg, char* foo, char* bar);
211 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar);
212 static int w_t_check_trans(struct sip_msg* msg, char* foo, char* bar);
213 static int w_t_is_set(struct sip_msg* msg, char* target, char* bar);
214 static int w_t_use_uac_headers(sip_msg_t* msg, char* foo, char* bar);
215 static int w_t_uac_send(sip_msg_t* msg, char* pmethod, char* pruri,
216                 char* pnexthop, char* psock, char *phdrs, char* pbody);
217
218
219 /* by default the fr timers avps are not set, so that the avps won't be
220  * searched for nothing each time a new transaction is created */
221 static char *fr_timer_param = 0 /*FR_TIMER_AVP*/;
222 static char *fr_inv_timer_param = 0 /*FR_INV_TIMER_AVP*/;
223
224 str contacts_avp = {0, 0};
225 str contact_flows_avp = {0, 0};
226 str ulattrs_xavp_name = {NULL, 0};
227
228 int tm_remap_503_500 = 1;
229
230 int tm_failure_exec_mode = 0;
231
232 int tm_dns_reuse_rcv_socket = 0;
233
234 static rpc_export_t tm_rpc[];
235
236 str tm_event_callback = STR_NULL;
237
238 static int fixup_t_check_status(void** param, int param_no);
239
240 static cmd_export_t cmds[]={
241         {"t_newtran",          w_t_newtran,             0, 0,
242                 REQUEST_ROUTE},
243         {"t_lookup_request",   w_t_check,               0, 0,
244                 REQUEST_ROUTE},
245         {"t_lookup_cancel",    w_t_lookup_cancel,       0, 0,
246                 REQUEST_ROUTE},
247         {"t_lookup_cancel",    w_t_lookup_cancel,       1, fixup_int_1,
248                 REQUEST_ROUTE},
249         {"t_reply",              w_t_reply,               2, fixup_t_reply,
250                 REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE },
251         {"t_retransmit_reply", w_t_retransmit_reply,    0, 0,
252                 REQUEST_ROUTE},
253         {"t_release",          w_t_release,             0, 0,
254                 REQUEST_ROUTE},
255         {"t_relay_to_udp",       w_t_relay_to_udp,        2, fixup_hostport2proxy,
256                 REQUEST_ROUTE|FAILURE_ROUTE},
257         {"t_relay_to_udp",       w_t_relay_to_udp_uri,    0, 0,
258                 REQUEST_ROUTE|FAILURE_ROUTE},
259 #ifdef USE_TCP
260         {"t_relay_to_tcp",       w_t_relay_to_tcp,        2, fixup_hostport2proxy,
261                 REQUEST_ROUTE|FAILURE_ROUTE},
262         {"t_relay_to_tcp",       w_t_relay_to_tcp_uri,    0, 0,
263                 REQUEST_ROUTE|FAILURE_ROUTE},
264 #endif
265 #ifdef USE_TLS
266         {"t_relay_to_tls",       w_t_relay_to_tls,        2, fixup_hostport2proxy,
267                 REQUEST_ROUTE|FAILURE_ROUTE},
268         {"t_relay_to_tls",       w_t_relay_to_tls_uri,    0, 0,
269                 REQUEST_ROUTE|FAILURE_ROUTE},
270 #endif
271 #ifdef USE_SCTP
272         {"t_relay_to_sctp",       w_t_relay_to_sctp,       2, fixup_hostport2proxy,
273                 REQUEST_ROUTE|FAILURE_ROUTE},
274         {"t_relay_to_sctp",       w_t_relay_to_sctp_uri,    0, 0,
275                 REQUEST_ROUTE|FAILURE_ROUTE},
276 #endif
277         {"t_replicate",        w_t_replicate_uri,       0, 0,
278                 REQUEST_ROUTE},
279         {"t_replicate",        w_t_replicate_uri,       1, fixup_spve_null,
280                 REQUEST_ROUTE},
281         {"t_replicate",        w_t_replicate,           2, fixup_hostport2proxy,
282                 REQUEST_ROUTE},
283         {"t_replicate_udp",    w_t_replicate_udp,       2, fixup_hostport2proxy,
284                 REQUEST_ROUTE},
285 #ifdef USE_TCP
286         {"t_replicate_tcp",    w_t_replicate_tcp,       2, fixup_hostport2proxy,
287                 REQUEST_ROUTE},
288 #endif
289 #ifdef USE_TLS
290         {"t_replicate_tls",    w_t_replicate_tls,       2, fixup_hostport2proxy,
291                 REQUEST_ROUTE},
292 #endif
293 #ifdef USE_SCTP
294         {"t_replicate_sctp",    w_t_replicate_sctp,     2, fixup_hostport2proxy,
295                 REQUEST_ROUTE},
296 #endif
297         {"t_replicate_to", w_t_replicate_to,            2, fixup_proto_hostport2proxy,
298                 REQUEST_ROUTE},
299         {"t_relay",              w_t_relay,               0, 0,
300                 REQUEST_ROUTE | FAILURE_ROUTE },
301         {"t_relay",              w_t_relay2,              2, fixup_hostport2proxy,
302                 REQUEST_ROUTE | FAILURE_ROUTE },
303         {"t_relay_to_avp", w_t_relay_to_avp,            2, fixup_proto_hostport2proxy,
304                 REQUEST_ROUTE},
305         {"t_relay_to",                  w_t_relay_to,           0, 0,
306                 REQUEST_ROUTE | FAILURE_ROUTE },
307         {"t_relay_to",                  w_t_relay_to,           1, fixup_t_relay_to,
308                 REQUEST_ROUTE | FAILURE_ROUTE },
309         {"t_relay_to",                  w_t_relay_to,           2, fixup_t_relay_to,
310                 REQUEST_ROUTE | FAILURE_ROUTE },
311         {"t_forward_nonack",     w_t_forward_nonack,      2, fixup_hostport2proxy,
312                 REQUEST_ROUTE},
313         {"t_forward_nonack_uri", w_t_forward_nonack_uri,  0, 0,
314                 REQUEST_ROUTE},
315         {"t_forward_nonack_udp", w_t_forward_nonack_udp,  2, fixup_hostport2proxy,
316                 REQUEST_ROUTE},
317 #ifdef USE_TCP
318         {"t_forward_nonack_tcp", w_t_forward_nonack_tcp,  2, fixup_hostport2proxy,
319                 REQUEST_ROUTE},
320 #endif
321 #ifdef USE_TLS
322         {"t_forward_nonack_tls", w_t_forward_nonack_tls,  2, fixup_hostport2proxy,
323                 REQUEST_ROUTE},
324 #endif
325 #ifdef USE_SCTP
326         {"t_forward_nonack_sctp", w_t_forward_nonack_sctp, 2, fixup_hostport2proxy,
327                 REQUEST_ROUTE},
328 #endif
329         {"t_forward_nonack_to", w_t_forward_nonack_to,  2, fixup_proto_hostport2proxy,
330                 REQUEST_ROUTE},
331         {"t_relay_cancel",     w_t_relay_cancel,        0, 0,
332                 REQUEST_ROUTE},
333         {"t_on_failure",       w_t_on_failure,         1, fixup_on_failure,
334                 REQUEST_ROUTE | FAILURE_ROUTE | TM_ONREPLY_ROUTE | BRANCH_ROUTE },
335         {"t_on_branch_failure",w_t_on_branch_failure,  1, fixup_on_branch_failure,
336                 REQUEST_ROUTE | FAILURE_ROUTE | TM_ONREPLY_ROUTE | BRANCH_ROUTE },
337         {"t_on_reply",         w_t_on_reply,            1, fixup_on_reply,
338                 REQUEST_ROUTE | FAILURE_ROUTE | TM_ONREPLY_ROUTE | BRANCH_ROUTE },
339         {"t_on_branch",       w_t_on_branch,         1, fixup_on_branch,
340                 REQUEST_ROUTE | FAILURE_ROUTE },
341         {"t_check_status",     t_check_status,          1, fixup_t_check_status,
342                 REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
343         {"t_write_req",       t_write_req,              2, fixup_t_write,
344                 REQUEST_ROUTE | FAILURE_ROUTE },
345         {"t_write_unix",      t_write_unix,             2, fixup_t_write,
346                 REQUEST_ROUTE | FAILURE_ROUTE },
347         {"t_set_fr",          t_set_fr_inv,             1, fixup_var_int_1,
348                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
349         {"t_set_fr",          t_set_fr_all,             2, fixup_var_int_12,
350                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
351         {"t_reset_fr",        w_t_reset_fr,             0, 0,
352                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
353         {"t_set_retr",        w_t_set_retr,               2, fixup_var_int_12,
354                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
355         {"t_reset_retr",      w_t_reset_retr,           0, 0,
356                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
357         {"t_set_max_lifetime", w_t_set_max_lifetime,      2, fixup_var_int_12,
358                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
359         {"t_reset_max_lifetime", w_t_reset_max_lifetime, 0, 0,
360                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
361         {"t_set_auto_inv_100", w_t_set_auto_inv_100,     1, fixup_var_int_1,
362                 REQUEST_ROUTE},
363         {"t_set_disable_6xx", w_t_set_disable_6xx,       1, fixup_var_int_1,
364                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
365         {"t_set_disable_failover", w_t_set_disable_failover, 1, fixup_var_int_1,
366                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
367 #ifdef CANCEL_REASON_SUPPORT
368         {"t_set_no_e2e_cancel_reason", w_t_set_no_e2e_cancel_reason, 1,
369                 fixup_var_int_1,
370                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
371         /* alias for t_set_no_e2e_cancel_reason */
372         {"t_disable_e2e_cancel_reason", w_t_set_no_e2e_cancel_reason, 1,
373                 fixup_var_int_1,
374                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
375 #endif /* CANCEL_REASON_SUPPORT */
376         {"t_set_disable_internal_reply", w_t_set_disable_internal_reply, 1,
377                 fixup_var_int_1,
378                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
379         {"t_branch_timeout",  w_t_branch_timeout,       0, 0,
380                 FAILURE_ROUTE|EVENT_ROUTE},
381         {"t_branch_replied",  w_t_branch_replied,       0, 0,
382                 FAILURE_ROUTE|EVENT_ROUTE},
383         {"t_any_timeout",     w_t_any_timeout,          0, 0,
384                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
385         {"t_any_replied",     w_t_any_replied,          0, 0,
386                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
387         {"t_is_canceled",     w_t_is_canceled,          0, 0,
388                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
389         {"t_is_retr_async_reply",     w_t_is_retr_async_reply,     0, 0,
390                 TM_ONREPLY_ROUTE},
391         {"t_is_expired",      w_t_is_expired,           0, 0,
392                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
393         {"t_grep_status",     w_t_grep_status,          1, fixup_var_int_1,
394                 REQUEST_ROUTE|TM_ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
395         {"t_drop_replies",    w_t_drop_replies,         0, 0,
396                 FAILURE_ROUTE},
397         {"t_drop_replies",    w_t_drop_replies,         1, 0,
398                 FAILURE_ROUTE},
399         {"t_save_lumps",      w_t_save_lumps,           0, 0,
400                 REQUEST_ROUTE},
401         {"t_check_trans",         w_t_check_trans,                      0, 0,
402                 REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE },
403         {"t_is_set",          w_t_is_set,                               1, fixup_t_is_set,
404                 ANY_ROUTE },
405         {"t_use_uac_headers",  w_t_use_uac_headers,             0, 0,
406                 ANY_ROUTE },
407         {"t_uac_send", (cmd_function)w_t_uac_send, 6, fixup_spve_all,
408                 ANY_ROUTE },
409
410         {"t_load_contacts", t_load_contacts,            0, 0,
411                 REQUEST_ROUTE | FAILURE_ROUTE},
412         {"t_next_contacts", t_next_contacts,            0, 0,
413                 REQUEST_ROUTE | FAILURE_ROUTE},
414         {"t_next_contact_flow", t_next_contact_flow,            0, 0,
415                 REQUEST_ROUTE },
416
417         /* not applicable from the script */
418         {"load_tm",            (cmd_function)load_tm,           NO_SCRIPT,   0, 0},
419         {"load_xtm",           (cmd_function)load_xtm,          NO_SCRIPT,   0, 0},
420         {0,0,0,0,0}
421 };
422
423
424 static param_export_t params[]={
425         {"ruri_matching",       PARAM_INT, &default_tm_cfg.ruri_matching         },
426         {"via1_matching",       PARAM_INT, &default_tm_cfg.via1_matching         },
427         {"callid_matching",     PARAM_INT, &default_tm_cfg.callid_matching       },
428         {"fr_timer",            PARAM_INT, &default_tm_cfg.fr_timeout            },
429         {"fr_inv_timer",        PARAM_INT, &default_tm_cfg.fr_inv_timeout        },
430         {"wt_timer",            PARAM_INT, &default_tm_cfg.wait_timeout          },
431         {"delete_timer",        PARAM_INT, &default_tm_cfg.delete_timeout        },
432         {"retr_timer1",         PARAM_INT, &default_tm_cfg.rt_t1_timeout_ms      },
433         {"retr_timer2"  ,       PARAM_INT, &default_tm_cfg.rt_t2_timeout_ms      },
434         {"max_inv_lifetime",    PARAM_INT, &default_tm_cfg.tm_max_inv_lifetime   },
435         {"max_noninv_lifetime", PARAM_INT, &default_tm_cfg.tm_max_noninv_lifetime},
436         {"noisy_ctimer",        PARAM_INT, &default_tm_cfg.noisy_ctimer          },
437         {"auto_inv_100",        PARAM_INT, &default_tm_cfg.tm_auto_inv_100       },
438         {"auto_inv_100_reason", PARAM_STRING, &default_tm_cfg.tm_auto_inv_100_r  },
439         {"unix_tx_timeout",     PARAM_INT, &default_tm_cfg.tm_unix_tx_timeout    },
440         {"restart_fr_on_each_reply", PARAM_INT,
441                 &default_tm_cfg.restart_fr_on_each_reply},
442         {"fr_timer_avp",        PARAM_STRING, &fr_timer_param                    },
443         {"fr_inv_timer_avp",    PARAM_STRING, &fr_inv_timer_param                },
444         {"tw_append",           PARAM_STRING|PARAM_USE_FUNC,
445                 (void*)parse_tw_append   },
446         {"pass_provisional_replies", PARAM_INT,
447                 &default_tm_cfg.pass_provisional_replies },
448         {"aggregate_challenges", PARAM_INT, &default_tm_cfg.tm_aggregate_auth    },
449         {"unmatched_cancel",    PARAM_INT, &default_tm_cfg.unmatched_cancel      },
450         {"default_code",        PARAM_INT, &default_tm_cfg.default_code          },
451         {"default_reason",      PARAM_STRING, &default_tm_cfg.default_reason     },
452         {"reparse_invite",      PARAM_INT, &default_tm_cfg.reparse_invite        },
453         {"ac_extra_hdrs",       PARAM_STR, &default_tm_cfg.ac_extra_hdrs         },
454         {"blst_503",            PARAM_INT, &default_tm_cfg.tm_blst_503           },
455         {"blst_503_def_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_default   },
456         {"blst_503_min_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_min       },
457         {"blst_503_max_timeout",PARAM_INT, &default_tm_cfg.tm_blst_503_max       },
458         {"blst_methods_add",    PARAM_INT, &default_tm_cfg.tm_blst_methods_add   },
459         {"blst_methods_lookup", PARAM_INT, &default_tm_cfg.tm_blst_methods_lookup},
460         {"cancel_b_method",     PARAM_INT, &default_tm_cfg.cancel_b_flags},
461         {"reparse_on_dns_failover", PARAM_INT,
462                 &default_tm_cfg.reparse_on_dns_failover},
463         {"on_sl_reply",         PARAM_STRING|PARAM_USE_FUNC, fixup_on_sl_reply   },
464         {"contacts_avp",        PARAM_STR, &contacts_avp                },
465         {"contact_flows_avp",   PARAM_STR, &contact_flows_avp           },
466         {"disable_6xx_block",   PARAM_INT, &default_tm_cfg.disable_6xx           },
467         {"local_ack_mode",      PARAM_INT, &default_tm_cfg.local_ack_mode        },
468         {"failure_reply_mode",  PARAM_INT, &failure_reply_mode                   },
469         {"faked_reply_prio",    PARAM_INT, &faked_reply_prio                     },
470         {"remap_503_500",       PARAM_INT, &tm_remap_503_500                     },
471         {"failure_exec_mode",   PARAM_INT, &tm_failure_exec_mode                 },
472         {"dns_reuse_rcv_socket",PARAM_INT, &tm_dns_reuse_rcv_socket              },
473 #ifdef CANCEL_REASON_SUPPORT
474         {"local_cancel_reason", PARAM_INT, &default_tm_cfg.local_cancel_reason   },
475         {"e2e_cancel_reason",   PARAM_INT, &default_tm_cfg.e2e_cancel_reason     },
476 #endif /* CANCEL_REASON_SUPPORT */
477         {"xavp_contact",        PARAM_STR, &ulattrs_xavp_name                    },
478         {"event_callback",      PARAM_STR, &tm_event_callback                    },
479         {0,0,0}
480 };
481
482 #ifdef STATIC_TM
483 struct module_exports tm_exports = {
484 #else
485 struct module_exports exports= {
486 #endif
487         "tm",
488         /* -------- exported functions ----------- */
489         cmds,
490         tm_rpc,    /* RPC methods */
491         /* ------------ exported variables ---------- */
492         params,
493
494         mod_init, /* module initialization function */
495         (response_function) reply_received,
496         (destroy_function) tm_shutdown,
497         0, /* w_onbreak, */
498         child_init /* per-child init function */
499 };
500
501
502
503 /* helper for fixup_on_* */
504 static int fixup_routes(char* r_type, struct route_list* rt, void** param)
505 {
506         int i;
507
508         i=route_get(rt, (char*)*param);
509         if (i==-1){
510                 LM_ERR("route_get failed\n");
511                 return E_UNSPEC;
512         }
513         if (r_type && rt->rlist[i]==0){
514                 LM_WARN("%s(\"%s\"): empty/non existing route\n",
515                                 r_type, (char*)*param);
516         }
517         *param=(void*)(long)i;
518         return 0;
519 }
520
521 static int fixup_t_reply(void** param, int param_no)
522 {
523         if (param_no == 1) {
524                 if (fixup_var_int_12(param, 1) != 0) return -1;
525         } else if (param_no == 2) {
526                 return fixup_var_str_12(param, 2);
527         }
528         return 0;
529 }
530
531 static int fixup_on_failure(void** param, int param_no)
532 {
533         if (param_no==1){
534                 if(strlen((char*)*param)<=1
535                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
536                         *param = (void*)0;
537                         return 0;
538                 }
539                 return fixup_routes("t_on_failure", &failure_rt, param);
540         }
541         return 0;
542 }
543
544 #define BRANCH_FAILURE_ROUTE_PREFIX "tm:branch-failure"
545 static int fixup_on_branch_failure(void** param, int param_no)
546 {
547         char *full_route_name = NULL;
548         int len;
549         int ret = 0;
550         if (param_no==1){
551                 if((len = strlen((char*)*param))<=1
552                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
553                         *param = (void*)0;
554                         return 0;
555                 }
556                 len += strlen(BRANCH_FAILURE_ROUTE_PREFIX) + 1;
557                 if ((full_route_name = pkg_malloc(len+1)) == NULL)
558                 {
559                         LM_ERR("No memory left in branch_failure fixup\n");
560                         return -1;
561                 }
562                 sprintf(full_route_name, "%s:%s", BRANCH_FAILURE_ROUTE_PREFIX, (char*)*param);
563                 *param=(void*)full_route_name;
564                 ret = fixup_routes("t_on_branch_failure", &event_rt, param);
565                 pkg_free(full_route_name);
566         }
567         return ret;
568 }
569
570
571 static int fixup_on_reply(void** param, int param_no)
572 {
573         if (param_no==1){
574                 if(strlen((char*)*param)<=1
575                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
576                         *param = (void*)0;
577                         return 0;
578                 }
579                 return fixup_routes("t_on_reply", &onreply_rt, param);
580         }
581         return 0;
582 }
583
584
585
586 static int fixup_on_branch(void** param, int param_no)
587 {
588         if (param_no==1){
589                 if(strlen((char*)*param)<=1
590                                 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
591                         *param = (void*)0;
592                         return 0;
593                 }
594                 return fixup_routes("t_on_branch", &branch_rt, param);
595         }
596         return 0;
597 }
598
599 static int fixup_on_sl_reply(modparam_t type, void* val)
600 {
601         if ((type & PARAM_STRING) == 0) {
602                 LM_ERR("not a string parameter\n");
603                 return -1;
604         }
605
606         if (fixup_routes(0, &onreply_rt, &val))
607                 return -1;
608
609         goto_on_sl_reply = (int)(long)val;
610         return 0;
611 }
612
613
614
615 /* (char *hostname, char *port_nr) ==> (struct proxy_l *, -)  */
616 static int fixup_hostport2proxy(void** param, int param_no)
617 {
618         unsigned int port;
619         char *host;
620         int err;
621         struct proxy_l *proxy;
622         action_u_t *a;
623         str s;
624
625         DBG("TM module: fixup_hostport2proxy(%s, %d)\n", (char*)*param, param_no);
626         if (param_no==1){
627                 return 0;
628         } else if (param_no==2) {
629                 a = fixup_get_param(param, param_no, 1);
630                 host= a->u.string;
631                 port=str2s(*param, strlen(*param), &err);
632                 if (err!=0) {
633                         LM_ERR("bad port number <%s>\n",
634                                         (char*)(*param));
635                         return E_UNSPEC;
636                 }
637                 s.s = host;
638                 s.len = strlen(host);
639                 proxy=mk_proxy(&s, port, 0); /* FIXME: udp or tcp? */
640                 if (proxy==0) {
641                         LM_ERR("bad host name in URI <%s>\n",
642                                         host );
643                         return E_BAD_ADDRESS;
644                 }
645                 /* success -- fix the first parameter to proxy now ! */
646
647                 a->u.data=proxy;
648                 return 0;
649         } else {
650                 LM_ERR("called with parameter number different than {1,2}\n");
651                 return E_BUG;
652         }
653 }
654
655 /* (char *$proto, char *$host:port) ==> (fparam, fparam)  */
656 static int fixup_proto_hostport2proxy(void** param, int param_no) {
657         int ret;
658
659         ret = fix_param(FPARAM_AVP, param);
660         if (ret <= 0) return ret;
661         if (fix_param(FPARAM_STRING, param) != 0) return -1;
662         return 0;
663 }
664
665
666 static int fixup_t_check_status(void** param, int param_no)
667 {
668         int ret;
669
670         ret = fix_param(FPARAM_AVP, param);
671         if (ret <= 0) return ret;
672
673         ret = fix_param(FPARAM_SELECT, param);
674         if (ret <= 0) return ret;
675
676         if (fix_param(FPARAM_REGEX, param) != 0) return -1;
677         return 0;
678 }
679
680
681 /***************************** init functions *****************************/
682 static int w_t_unref( struct sip_msg *foo, unsigned int flags, void *bar)
683 {
684         return t_unref(foo);
685 }
686
687
688 static int script_init( struct sip_msg *foo, unsigned int flags, void *bar)
689 {
690         /* we primarily reset all private memory here to make sure
691          * private values left over from previous message will
692          * not be used again */
693
694         /* make sure the new message will not inherit previous
695            message's t_on_failure value
696            */
697         t_on_failure( 0 );
698         t_on_branch_failure(0);
699         t_on_reply(0);
700         t_on_branch(0);
701         /* reset the kr status */
702         reset_kr();
703         /* set request mode so that multiple-mode actions know
704          * how to behave */
705         set_route_type(REQUEST_ROUTE);
706         lumps_are_cloned = 0;
707         return 1;
708 }
709
710
711 static int mod_init(void)
712 {
713         DBG( "TM - (sizeof cell=%ld, sip_msg=%ld) initializing...\n",
714                         (long)sizeof(struct cell), (long)sizeof(struct sip_msg));
715
716         /* checking if we have sufficient bitmap capacity for given
717          * maximum number of  branches */
718         if (sr_dst_max_branches+1>31) {
719                 LM_CRIT("Too many max UACs for UAC branch_bm_t bitmap: %d\n",
720                                 sr_dst_max_branches );
721                 return -1;
722         }
723
724         if (init_callid() < 0) {
725                 LM_CRIT("Error while initializing Call-ID generator\n");
726                 return -1;
727         }
728
729         /* building the hash table*/
730         if (!init_hash_table()) {
731                 LM_ERR("initializing hash_table failed\n");
732                 return -1;
733         }
734
735         /* init static hidden values */
736         init_t();
737
738         if (tm_init_selects()==-1) {
739                 LM_ERR("select init failed\n");
740                 return -1;
741         }
742
743         /* the default timer values must be fixed-up before
744          * declaring the configuration (Miklos) */
745         if (tm_init_timers()==-1) {
746                 LM_ERR("timer init failed\n");
747                 return -1;
748         }
749
750         /* the cancel branch flags must be fixed before declaring the
751          * configuration */
752         if (cancel_b_flags_get(&default_tm_cfg.cancel_b_flags,
753                                 default_tm_cfg.cancel_b_flags)<0){
754                 LM_ERR("bad cancel branch method\n");
755                 return -1;
756         }
757
758 #ifdef USE_DNS_FAILOVER
759         if (default_tm_cfg.reparse_on_dns_failover && mhomed) {
760                 LM_WARN("reparse_on_dns_failover is enabled on a"
761                                 " multihomed host -- check the readme of tm module!\n");
762         }
763 #endif
764
765         /* declare the configuration */
766         if (cfg_declare("tm", tm_cfg_def, &default_tm_cfg, cfg_sizeof(tm),
767                                 &tm_cfg)) {
768                 LM_ERR("failed to declare the configuration\n");
769                 return -1;
770         }
771
772         /* First tm_stat initialization function only allocates the top level stat
773          * structure in shared memory, the initialization will complete in child
774          * init with init_tm_stats_child when the final value of
775          * estimated_process_count is known
776          */
777         if (init_tm_stats() < 0) {
778                 LM_CRIT("failed to init stats\n");
779                 return -1;
780         }
781
782         if (uac_init()==-1) {
783                 LM_ERR("uac_init failed\n");
784                 return -1;
785         }
786
787         if (init_tmcb_lists()!=1) {
788                 LM_CRIT("failed to init tmcb lists\n");
789                 return -1;
790         }
791
792         tm_init_tags();
793         init_twrite_lines();
794         if (init_twrite_sock() < 0) {
795                 LM_ERR("Unable to create socket\n");
796                 return -1;
797         }
798
799         /* register post-script clean-up function */
800         if (register_script_cb( w_t_unref, POST_SCRIPT_CB|REQUEST_CB, 0)<0 ) {
801                 LM_ERR("failed to register POST request callback\n");
802                 return -1;
803         }
804         if (register_script_cb( script_init, PRE_SCRIPT_CB|REQUEST_CB , 0)<0 ) {
805                 LM_ERR("failed to register PRE request callback\n");
806                 return -1;
807         }
808
809         if (init_avp_params(fr_timer_param, fr_inv_timer_param) < 0) {
810                 LM_ERR("failed to process AVP params\n");
811                 return -1;
812         }
813         if ((contacts_avp.len > 0) && (contact_flows_avp.len == 0)) {
814                 LM_ERR("contact_flows_avp param has not been defined\n");
815                 return -1;
816         }
817
818 #ifdef WITH_EVENT_LOCAL_REQUEST
819         goto_on_local_req=route_lookup(&event_rt, "tm:local-request");
820         if (goto_on_local_req>=0 && event_rt.rlist[goto_on_local_req]==0)
821                 goto_on_local_req=-1; /* disable */
822         if (goto_on_local_req>=0 || tm_event_callback.len>0)
823                 set_child_rpc_sip_mode();
824 #endif /* WITH_EVENT_LOCAL_REQUEST */
825         if (goto_on_sl_reply && onreply_rt.rlist[goto_on_sl_reply]==0)
826                 LM_WARN("empty/non existing on_sl_reply route\n");
827
828 #ifdef WITH_TM_CTX
829         tm_ctx_init();
830 #endif
831         tm_init = 1;
832         return 0;
833 }
834
835 static int child_init(int rank)
836 {
837         if (rank == PROC_INIT) {
838                 /* we must init stats when rank==PROC_INIT: after mod_init we know
839                  * the exact number of processes and we must init the shared structure
840                  * before any other process is starting (or else some new process
841                  * might try to use the stats before the stats array is allocated) */
842                 if (init_tm_stats_child() < 0) {
843                         LM_ERR("Error while initializing tm statistics structures\n");
844                         return -1;
845                 }
846         }else if (child_init_callid(rank) < 0) {
847                 /* don't init callid for PROC_INIT*/
848                 LM_ERR("Error while initializing Call-ID generator\n");
849                 return -2;
850         }
851         return 0;
852 }
853
854
855
856 /**************************** wrapper functions ***************************/
857 static int t_check_status(struct sip_msg* msg, char *p1, char *foo)
858 {
859         regmatch_t pmatch;
860         struct cell *t;
861         char *status, *s = NULL;
862         char backup;
863         int lowest_status, n, ret;
864         fparam_t* fp;
865         regex_t* re = NULL;
866         str tmp;
867
868         fp = (fparam_t*)p1;
869         t = 0;
870         /* first get the transaction */
871         if (t_check(msg, 0 ) == -1) return -1;
872         if ((t = get_t()) == 0) {
873                 LM_ERR("cannot check status for a reply"
874                                 " which has no T-state established\n");
875                 goto error;
876         }
877         backup = 0;
878
879         switch(fp->type) {
880                 case FPARAM_REGEX:
881                         re = fp->v.regex;
882                         break;
883
884                 default:
885                         /* AVP or select, get the value and compile the regex */
886                         if (get_str_fparam(&tmp, msg, fp) < 0) goto error;
887                         s = pkg_malloc(tmp.len + 1);
888                         if (s == NULL) {
889                                 LM_ERR("Out of memory\n");
890                                 goto error;
891                         }
892                         memcpy(s, tmp.s, tmp.len);
893                         s[tmp.len] = '\0';
894
895                         if ((re = pkg_malloc(sizeof(regex_t))) == 0) {
896                                 LM_ERR("No memory left\n");
897                                 goto error;
898                         }
899
900                         if (regcomp(re, s, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
901                                 LM_ERR("Bad regular expression '%s'\n", s);
902                                 pkg_free(re);
903                                 re = NULL;
904                                 goto error;
905                         }
906                         break;
907         }
908
909         switch(get_route_type()) {
910                 case REQUEST_ROUTE:
911                         /* use the status of the last sent reply */
912                         status = int2str( t->uas.status, 0);
913                         break;
914
915                 case TM_ONREPLY_ROUTE:
916                 case CORE_ONREPLY_ROUTE:
917                         /* use the status of the current reply */
918                         status = msg->first_line.u.reply.status.s;
919                         backup = status[msg->first_line.u.reply.status.len];
920                         status[msg->first_line.u.reply.status.len] = 0;
921                         break;
922
923                 case FAILURE_ROUTE:
924                         /* use the status of the winning reply */
925                         ret = t_pick_branch( -1, 0, t, &lowest_status);
926                         if (ret == -1) {
927                                 /* t_pick_branch() retuns error also when there are only
928                                  * blind UACs. Let us give it another chance including the
929                                  * blind branches. */
930                                 LM_DBG("t_pick_branch returned error,"
931                                                 " trying t_pick_branch_blind\n");
932                                 ret = t_pick_branch_blind(t, &lowest_status);
933                         }
934                         if (ret < 0) {
935                                 LM_CRIT("BUG: t_pick_branch failed to get"
936                                                 " a final response in FAILURE_ROUTE\n");
937                                 goto error;
938                         }
939                         status = int2str( lowest_status , 0);
940                         break;
941                 case BRANCH_FAILURE_ROUTE:
942                         status = int2str(t->uac[get_t_branch()].last_received, 0);
943                         break;
944                 default:
945                         LM_ERR("unsupported route type %d\n",
946                                         get_route_type());
947                         goto error;
948         }
949
950         LM_DBG("checked status is <%s>\n",status);
951         /* do the checking */
952         n = regexec(re, status, 1, &pmatch, 0);
953
954         if (backup) status[msg->first_line.u.reply.status.len] = backup;
955         if (s) pkg_free(s);
956         if (fp->type != FPARAM_REGEX) {
957                 regfree(re);
958                 pkg_free(re);
959         }
960
961         if (unlikely(t && is_route_type(CORE_ONREPLY_ROUTE))){
962                 /* t_check() above has the side effect of setting T and
963                  * REFerencing T => we must unref and unset it.  */
964                 UNREF( t );
965                 set_t(T_UNDEFINED, T_BR_UNDEFINED);
966         }
967         if (n!=0) return -1;
968         return 1;
969
970 error:
971         if (unlikely(t && is_route_type(CORE_ONREPLY_ROUTE))){
972                 /* t_check() above has the side effect of setting T and
973                  * REFerencing T => we must unref and unset it.  */
974                 UNREF( t );
975                 set_t(T_UNDEFINED, T_BR_UNDEFINED);
976         }
977         if (s) pkg_free(s);
978         if ((fp->type != FPARAM_REGEX) && re) {
979                 regfree(re);
980                 pkg_free(re);
981         }
982         return -1;
983 }
984
985
986 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2)
987 {
988         return (t_check_msg( msg , 0  )==1) ? 1 : -1;
989 }
990
991 static int ki_t_lookup_request(struct sip_msg* msg)
992 {
993         return (t_check_msg( msg , 0  )==1) ? 1 : -1;
994 }
995
996 static int ki_t_lookup_cancel_flags(sip_msg_t* msg, int flags)
997 {
998         struct cell *ret;
999         if (msg->REQ_METHOD==METHOD_CANCEL) {
1000                 ret = t_lookupOriginalT( msg );
1001                 LM_DBG("lookup_original: t_lookupOriginalT returned: %p\n", ret);
1002                 if (ret != T_NULL_CELL) {
1003                         /* If the parameter is set to 1, overwrite the message flags of
1004                          * the CANCEL with the flags of the INVITE */
1005                         if (flags)
1006                                 msg->flags = ret->uas.request->flags;
1007
1008                         /* The cell is reffed by t_lookupOriginalT, but T is not set.
1009                            So we must unref it before returning. */
1010                         UNREF(ret);
1011                         return 1;
1012                 }
1013         } else {
1014                 LM_WARN("script error - t_lookup_cancel() called for non-CANCEL request\n");
1015         }
1016         return -1;
1017 }
1018
1019 static int ki_t_lookup_cancel(sip_msg_t* msg)
1020 {
1021         return ki_t_lookup_cancel_flags(msg, 0);
1022 }
1023
1024 inline static int w_t_lookup_cancel(struct sip_msg* msg, char* str, char* str2)
1025 {
1026         int i = 0;
1027
1028         if(str) {
1029                 if(get_int_fparam(&i, msg, (fparam_t*)str)<0) return -1;
1030         }
1031         return ki_t_lookup_cancel_flags(msg, i);
1032 }
1033
1034 inline static int str2proto(char *s, int len) {
1035         if (len == 3 && !strncasecmp(s, "udp", 3))
1036                 return PROTO_UDP;
1037         else if (len == 3 && !strncasecmp(s, "tcp", 3))  /* tcp&tls checks will be
1038                                                                                                                 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 if (len == 2 && !strncasecmp(s, "ws", 2))
1045                 return PROTO_WS;
1046         else if (len == 3 && !strncasecmp(s, "wss", 3)) {
1047                 LM_WARN("\"wss\" used somewhere...\n");
1048                 return PROTO_WS;
1049         } else
1050                 return PROTO_NONE;
1051 }
1052
1053 inline static struct proxy_l* t_protoaddr2proxy(char *proto_par, char *addr_par) {
1054         struct proxy_l *proxy = 0;
1055         avp_t* avp;
1056         avp_value_t val;
1057         int proto, port, err;
1058         str s;
1059         char *c;
1060
1061         switch(((fparam_t *)proto_par)->type) {
1062                 case FPARAM_AVP:
1063                         if (!(avp = search_first_avp(((fparam_t *)proto_par)->v.avp.flags,
1064                                                         ((fparam_t *)proto_par)->v.avp.name, &val, 0))) {
1065                                 proto = PROTO_NONE;
1066                         } else {
1067                                 if (avp->flags & AVP_VAL_STR) {
1068                                         proto = str2proto(val.s.s, val.s.len);
1069                                 }
1070                                 else {
1071                                         proto = val.n;
1072                                 }
1073                         }
1074                         break;
1075
1076                 case FPARAM_INT:
1077                         proto = ((fparam_t *)proto_par)->v.i;
1078                         break;
1079                 case FPARAM_STRING:
1080                         proto = str2proto( ((fparam_t *)proto_par)->v.asciiz,
1081                                         strlen(((fparam_t *)proto_par)->v.asciiz));
1082                         break;
1083                 default:
1084                         LM_ERR("Invalid proto parameter value in t_protoaddr2proxy\n");
1085                         return 0;
1086         }
1087
1088
1089         switch(((fparam_t *)addr_par)->type) {
1090                 case FPARAM_AVP:
1091                         if (!(avp = search_first_avp(((fparam_t *)addr_par)->v.avp.flags,
1092                                                         ((fparam_t *)addr_par)->v.avp.name, &val, 0))) {
1093                                 s.len = 0;
1094                         } else {
1095                                 if ((avp->flags & AVP_VAL_STR) == 0) {
1096                                         LM_ERR("avp <%.*s> value is not string\n",
1097                                                         ((fparam_t *)addr_par)->v.avp.name.s.len,
1098                                                         ((fparam_t *)addr_par)->v.avp.name.s.s);
1099                                         return 0;
1100                                 }
1101                                 s = val.s;
1102                         }
1103                         break;
1104
1105                 case FPARAM_STRING:
1106                         s.s = ((fparam_t *) addr_par)->v.asciiz;
1107                         s.len = strlen(s.s);
1108                         break;
1109
1110                 default:
1111                         LM_ERR("Invalid addr parameter value in t_protoaddr2proxy\n");
1112                         return 0;
1113         }
1114
1115         port = 5060;
1116         if (s.len) {
1117                 c = memchr(s.s, ':', s.len);
1118                 if (c) {
1119                         port = str2s(c+1, s.len-(c-s.s+1), &err);
1120                         if (err!=0) {
1121                                 LM_ERR("bad port number <%.*s>\n", s.len, s.s);
1122                                 return 0;
1123                         }
1124                         s.len = c-s.s;
1125                 }
1126         }
1127         if (!s.len) {
1128                 LM_ERR("host name is empty\n");
1129                 return 0;
1130         }
1131         proxy=mk_proxy(&s, port, proto);
1132         if (proxy==0) {
1133                 LM_ERR("bad host name in URI <%.*s>\n", s.len, s.s );
1134                 return 0;
1135         }
1136         return proxy;
1137 }
1138
1139 inline static int _w_t_forward_nonack(struct sip_msg* msg, struct proxy_l* proxy,
1140                 int proto)
1141 {
1142         struct cell *t;
1143         if (t_check( msg , 0 )==-1) {
1144                 LM_ERR("can't forward when no transaction was set up\n");
1145                 return -1;
1146         }
1147         t=get_t();
1148         if ( t && t!=T_UNDEFINED ) {
1149                 if (msg->REQ_METHOD==METHOD_ACK) {
1150                         LM_WARN("you don't really want to fwd hop-by-hop ACK\n");
1151                         return -1;
1152                 }
1153                 return t_forward_nonack(t, msg, proxy, proto );
1154         } else {
1155                 LM_DBG("no transaction found\n");
1156                 return -1;
1157         }
1158 }
1159
1160
1161 inline static int w_t_forward_nonack( struct sip_msg* msg, char* proxy,
1162                 char* foo)
1163 {
1164         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_NONE);
1165 }
1166
1167
1168 inline static int w_t_forward_nonack_uri(struct sip_msg* msg, char *foo,
1169                 char *bar)
1170 {
1171         return _w_t_forward_nonack(msg, 0, PROTO_NONE);
1172 }
1173
1174
1175 inline static int w_t_forward_nonack_udp( struct sip_msg* msg, char* proxy,
1176                 char* foo)
1177 {
1178         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_UDP);
1179 }
1180
1181
1182 #ifdef USE_TCP
1183 inline static int w_t_forward_nonack_tcp( struct sip_msg* msg, char* proxy,
1184                 char* foo)
1185 {
1186         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TCP);
1187 }
1188 #endif
1189
1190
1191 #ifdef USE_TLS
1192 inline static int w_t_forward_nonack_tls( struct sip_msg* msg, char* proxy,
1193                 char* foo)
1194 {
1195         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TLS);
1196 }
1197 #endif
1198
1199
1200 #ifdef USE_SCTP
1201 inline static int w_t_forward_nonack_sctp( struct sip_msg* msg, char* proxy,
1202                 char* foo)
1203 {
1204         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1205 }
1206 #endif
1207
1208
1209 inline static int w_t_forward_nonack_to( struct sip_msg  *p_msg ,
1210                 char *proto_par,
1211                 char *addr_par   )
1212 {
1213         struct proxy_l *proxy;
1214         int r = -1;
1215         proxy = t_protoaddr2proxy(proto_par, addr_par);
1216         if (proxy) {
1217                 r = _w_t_forward_nonack(p_msg, proxy, proxy->proto);
1218                 free_proxy(proxy);
1219                 pkg_free(proxy);
1220         }
1221         return r;
1222 }
1223
1224
1225 inline static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
1226 {
1227         struct cell *t;
1228         int code, ret = -1;
1229         str reason;
1230         char* r;
1231
1232         if (msg->REQ_METHOD==METHOD_ACK) {
1233                 LM_DBG("ACKs are not replied\n");
1234                 return -1;
1235         }
1236         if (t_check( msg , 0 )==-1) return -1;
1237         t=get_t();
1238         if (!t) {
1239                 LM_ERR("cannot send a t_reply to a message"
1240                                 " for which no T-state has been established\n");
1241                 return -1;
1242         }
1243
1244         if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
1245                 code = cfg_get(tm, tm_cfg, default_code);
1246         }
1247
1248         if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
1249                 r = cfg_get(tm, tm_cfg, default_reason);
1250         } else {
1251                 r = as_asciiz(&reason);
1252                 if (r == NULL) r = cfg_get(tm, tm_cfg, default_reason);
1253         }
1254
1255         /* if called from reply_route, make sure that unsafe version
1256          * is called; we are already in a mutex and another mutex in
1257          * the safe version would lead to a deadlock
1258          */
1259
1260         t->flags |= T_ADMIN_REPLY;
1261         if (is_route_type(FAILURE_ROUTE)) {
1262                 LM_DBG("t_reply_unsafe called from w_t_reply\n");
1263                 ret = t_reply_unsafe(t, msg, code, r);
1264         } else if (is_route_type(REQUEST_ROUTE)) {
1265                 ret = t_reply( t, msg, code, r);
1266         } else if (is_route_type(ONREPLY_ROUTE)) {
1267                 if (likely(t->uas.request)){
1268                         if (is_route_type(CORE_ONREPLY_ROUTE))
1269                                 ret=t_reply(t, t->uas.request, code, r);
1270                         else
1271                                 ret=t_reply_unsafe(t, t->uas.request, code, r);
1272                 }else
1273                         ret=-1;
1274                 /* t_check() above has the side effect of setting T and
1275                  * REFerencing T => we must unref and unset it.
1276                  * Note: this is needed only in the CORE_ONREPLY_ROUTE and not also in
1277                  * the TM_ONREPLY_ROUTE.
1278                  */
1279                 if (is_route_type(CORE_ONREPLY_ROUTE)) {
1280                         UNREF( t );
1281                         set_t(T_UNDEFINED, T_BR_UNDEFINED);
1282                 }
1283         } else {
1284                 LM_CRIT("w_t_reply entered in unsupported mode\n");
1285                 ret = -1;
1286         }
1287
1288         if (r && (r != cfg_get(tm, tm_cfg, default_reason))) pkg_free(r);
1289         return ret;
1290 }
1291
1292
1293 static int t_release(sip_msg_t* msg)
1294 {
1295         struct cell *t;
1296         int ret;
1297
1298         if(get_route_type()!=REQUEST_ROUTE)
1299         {
1300                 LM_INFO("invalid usage - not in request route\n");
1301                 return -1;
1302         }
1303
1304         if (t_check( msg  , 0  )==-1) return -1;
1305         t=get_t();
1306         if ( t && t!=T_UNDEFINED ) {
1307                 ret = t_release_transaction( t );
1308                 t_unref(msg);
1309                 return ret;
1310         }
1311         return 1;
1312 }
1313
1314 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2)
1315 {
1316         return t_release(msg);
1317 }
1318
1319 static int ki_t_retransmit_reply(struct sip_msg* p_msg)
1320 {
1321         struct cell *t;
1322
1323         if (t_check( p_msg  , 0 )==-1)
1324                 return 1;
1325         t=get_t();
1326         if (t) {
1327                 if (p_msg->REQ_METHOD==METHOD_ACK) {
1328                         LM_WARN("ACKs transmit_replies not replied\n");
1329                         return -1;
1330                 }
1331                 return t_retransmit_reply( t );
1332         } else
1333                 return -1;
1334 }
1335
1336 inline static int w_t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar)
1337 {
1338         return ki_t_retransmit_reply(p_msg);
1339 }
1340
1341 inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar )
1342 {
1343         /* t_newtran returns 0 on error (negative value means
1344          * 'transaction exists' */
1345         int ret;
1346         ret = t_newtran( p_msg );
1347         if (ret==E_SCRIPT) {
1348                 LM_NOTICE("transaction already in process %p\n", get_t() );
1349         }
1350         return ret;
1351 }
1352
1353
1354 inline static int w_t_on_failure( struct sip_msg* msg, char *go_to, char *foo)
1355 {
1356         t_on_failure( (unsigned int )(long) go_to );
1357         return 1;
1358 }
1359
1360
1361 inline static int w_t_on_branch_failure( struct sip_msg* msg, char *go_to, char *foo)
1362 {
1363         t_on_branch_failure( (unsigned int )(long) go_to );
1364         return 1;
1365 }
1366
1367
1368 inline static int w_t_on_branch( struct sip_msg* msg, char *go_to, char *foo)
1369 {
1370         t_on_branch( (unsigned int )(long) go_to );
1371         return 1;
1372 }
1373
1374
1375 inline static int w_t_on_reply( struct sip_msg* msg, char *go_to, char *foo )
1376 {
1377         t_on_reply( (unsigned int )(long) go_to );
1378         return 1;
1379 }
1380
1381
1382 static int t_is_set(sip_msg_t* msg, str *target)
1383 {
1384         int r;
1385         tm_cell_t *t = NULL;
1386
1387         r = 0;
1388         t = get_t();
1389         if (t==T_UNDEFINED) t = NULL;
1390
1391         switch(target->s[0]) {
1392                 case 'b':
1393                         if(t==NULL)
1394                                 r = get_on_branch();
1395                         else
1396                                 r = t->on_branch;
1397                         break;
1398                 case 'f':
1399                         if(t==NULL)
1400                                 r = get_on_failure();
1401                         else
1402                                 r = t->on_failure;
1403                         break;
1404                 case 'o':
1405                         if(t==NULL)
1406                                 r = get_on_reply();
1407                         else
1408                                 r = t->on_reply;
1409                         break;
1410         }
1411         if(r) return 1;
1412         return -1;
1413 }
1414
1415 static int w_t_is_set(struct sip_msg* msg, char *target, char *foo)
1416 {
1417         str s = STR_NULL;
1418
1419         s.s = target;
1420         return t_is_set(msg, &s);
1421 }
1422
1423 static int fixup_t_is_set(void** param, int param_no)
1424 {
1425         int len;
1426         if (param_no==1) {
1427                 len = strlen((char*)*param);
1428                 if((len==13 && strncmp((char*)*param, "failure_route", 13)==0)
1429                                 || (len==13 && strncmp((char*)*param, "onreply_route", 13)==0)
1430                                 || (len==12 && strncmp((char*)*param, "branch_route", 12)==0)) {
1431                         return 0;
1432                 }
1433
1434                 LM_ERR("invalid parameter value: %s\n", (char*)*param);
1435                 return 1;
1436         }
1437         return 0;
1438 }
1439
1440 inline static int _w_t_relay_to(struct sip_msg  *p_msg ,
1441                 struct proxy_l *proxy, int force_proto)
1442 {
1443         struct cell *t;
1444         int res;
1445
1446         if (is_route_type(FAILURE_ROUTE|BRANCH_FAILURE_ROUTE)) {
1447                 t=get_t();
1448                 if (!t || t==T_UNDEFINED) {
1449                         LM_CRIT("undefined T\n");
1450                         return -1;
1451                 }
1452                 res = t_forward_nonack(t, p_msg, proxy, force_proto);
1453                 if (res <= 0) {
1454                         if (res != E_CFG) {
1455                                 LM_ERR("t_forward_noack failed\n");
1456                                 /* let us save the error code, we might need it later
1457                                  * when the failure_route has finished (Miklos) */
1458                         }
1459                         tm_error=ser_error;
1460                         return -1;
1461                 }
1462                 return 1;
1463         }
1464         if (is_route_type(REQUEST_ROUTE))
1465                 return t_relay_to( p_msg, proxy, force_proto,
1466                                 0 /* no replication */ );
1467         LM_CRIT("unsupported route type: %d\n", get_route_type());
1468         return 0;
1469 }
1470
1471
1472 inline static int w_t_relay_to_udp( struct sip_msg  *p_msg ,
1473                 char *proxy,/* struct proxy_l * expected */
1474                 char *_foo       /* nothing expected */ )
1475 {
1476         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_UDP);
1477 }
1478
1479 /* forward to uri, but force udp as transport */
1480 inline static int w_t_relay_to_udp_uri( struct sip_msg  *p_msg ,
1481                 char *_foo, char *_bar   )
1482 {
1483         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_UDP);
1484 }
1485
1486
1487 #ifdef USE_TCP
1488 inline static int w_t_relay_to_tcp( struct sip_msg  *p_msg ,
1489                 char *proxy, /* struct proxy_l* */
1490                 char *_foo       /* nothing expected */ )
1491 {
1492         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TCP);
1493 }
1494
1495 /* forward to uri, but force tcp as transport */
1496 inline static int w_t_relay_to_tcp_uri( struct sip_msg  *p_msg ,
1497                 char *_foo, char *_bar   )
1498 {
1499         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TCP);
1500 }
1501 #endif
1502
1503
1504 #ifdef USE_TLS
1505 inline static int w_t_relay_to_tls( struct sip_msg  *p_msg ,
1506                 char *proxy, /* struct proxy_l* expected */
1507                 char *_foo       /* nothing expected */ )
1508 {
1509         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TLS);
1510 }
1511
1512 /* forward to uri, but force tls as transport */
1513 inline static int w_t_relay_to_tls_uri( struct sip_msg  *p_msg ,
1514                 char *_foo, char *_bar   )
1515 {
1516         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TLS);
1517 }
1518 #endif
1519
1520
1521 #ifdef USE_SCTP
1522 inline static int w_t_relay_to_sctp( struct sip_msg  *p_msg ,
1523                 char *proxy, /* struct proxy_l* */
1524                 char *_foo       /* nothing expected */ )
1525 {
1526         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1527 }
1528
1529 /* forward to uri, but force tcp as transport */
1530 inline static int w_t_relay_to_sctp_uri( struct sip_msg  *p_msg ,
1531                 char *_foo, char *_bar   )
1532 {
1533         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_SCTP);
1534 }
1535 #endif
1536
1537
1538 inline static int w_t_relay_to_avp( struct sip_msg  *p_msg ,
1539                 char *proto_par,
1540                 char *addr_par   )
1541 {
1542         struct proxy_l *proxy;
1543         int r = -1;
1544
1545         proxy = t_protoaddr2proxy(proto_par, addr_par);
1546         if (proxy) {
1547                 r = _w_t_relay_to(p_msg, proxy, PROTO_NONE);
1548                 free_proxy(proxy);
1549                 pkg_free(proxy);
1550         }
1551         return r;
1552 }
1553
1554 int t_replicate_uri(struct sip_msg *msg, str *suri)
1555 {
1556         struct proxy_l *proxy = NULL;
1557         struct sip_uri turi;
1558         int r = -1;
1559
1560         if (suri != NULL && suri->s != NULL && suri->len > 0)
1561         {
1562                 memset(&turi, 0, sizeof(struct sip_uri));
1563                 if(parse_uri(suri->s, suri->len, &turi)!=0)
1564                 {
1565                         LM_ERR("bad replicate SIP address!\n");
1566                         return -1;
1567                 }
1568
1569                 proxy=mk_proxy(&turi.host, turi.port_no, turi.proto);
1570                 if (proxy==0) {
1571                         LM_ERR("cannot create proxy from URI <%.*s>\n",
1572                                         suri->len, suri->s );
1573                         return -1;
1574                 }
1575
1576                 r = t_replicate(msg, proxy, proxy->proto);
1577                 free_proxy(proxy);
1578                 pkg_free(proxy);
1579         } else {
1580                 r = t_replicate(msg, NULL, 0);
1581         }
1582         return r;
1583 }
1584
1585 inline static int w_t_replicate_uri(struct sip_msg  *msg ,
1586                 char *uri,       /* sip uri as string or variable */
1587                 char *_foo       /* nothing expected */ )
1588 {
1589         str suri;
1590
1591         if(uri==NULL)
1592                 return t_replicate_uri(msg, NULL);
1593
1594         if(fixup_get_svalue(msg, (gparam_p)uri, &suri)!=0)
1595         {
1596                 LM_ERR("invalid replicate uri parameter");
1597                 return -1;
1598         }
1599         return t_replicate_uri(msg, &suri);
1600 }
1601
1602 inline static int w_t_replicate( struct sip_msg  *p_msg ,
1603                 char *proxy, /* struct proxy_l *proxy expected */
1604                 char *_foo       /* nothing expected */ )
1605 {
1606         return t_replicate(p_msg, ( struct proxy_l *) proxy, p_msg->rcv.proto );
1607 }
1608
1609 inline static int w_t_replicate_udp( struct sip_msg  *p_msg ,
1610                 char *proxy, /* struct proxy_l *proxy expected */
1611                 char *_foo       /* nothing expected */ )
1612 {
1613         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_UDP );
1614 }
1615
1616
1617 #ifdef USE_TCP
1618 inline static int w_t_replicate_tcp( struct sip_msg  *p_msg ,
1619                 char *proxy, /* struct proxy_l *proxy expected */
1620                 char *_foo       /* nothing expected */ )
1621 {
1622         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TCP );
1623 }
1624 #endif
1625
1626
1627 #ifdef USE_TLS
1628 inline static int w_t_replicate_tls( struct sip_msg  *p_msg ,
1629                 char *proxy, /* struct proxy_l *proxy expected */
1630                 char *_foo       /* nothing expected */ )
1631 {
1632         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TLS );
1633 }
1634 #endif
1635
1636
1637 #ifdef USE_SCTP
1638 inline static int w_t_replicate_sctp( struct sip_msg  *p_msg ,
1639                 char *proxy, /* struct proxy_l *proxy expected */
1640                 char *_foo       /* nothing expected */ )
1641 {
1642         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_SCTP );
1643 }
1644 #endif
1645
1646
1647 inline static int w_t_replicate_to( struct sip_msg  *p_msg ,
1648                 char *proto_par,
1649                 char *addr_par   )
1650 {
1651         struct proxy_l *proxy;
1652         int r = -1;
1653         proxy = t_protoaddr2proxy(proto_par, addr_par);
1654         if (proxy) {
1655                 r = t_replicate(p_msg, proxy, proxy->proto);
1656                 free_proxy(proxy);
1657                 pkg_free(proxy);
1658         }
1659         return r;
1660 }
1661
1662 inline static int w_t_relay( struct sip_msg  *p_msg ,
1663                 char *_foo, char *_bar)
1664 {
1665         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_NONE);
1666 }
1667
1668
1669 /* like t_relay but use the specified destination and port and the same proto
1670  * as the received msg */
1671 static int w_t_relay2( struct sip_msg  *p_msg , char *proxy,
1672                 char *_foo)
1673 {
1674         return _w_t_relay_to(p_msg, (struct proxy_l*) proxy, p_msg->rcv.proto);
1675 }
1676
1677
1678 /* relays CANCEL at the beginning of the script */
1679 inline static int w_t_relay_cancel( struct sip_msg  *p_msg ,
1680                 char *_foo, char *_bar)
1681 {
1682         if (p_msg->REQ_METHOD!=METHOD_CANCEL)
1683                 return 1;
1684
1685         /* it makes no sense to use this function without reparse_invite=1 */
1686         if (!cfg_get(tm, tm_cfg, reparse_invite))
1687                 LM_WARN("probably used with wrong configuration,"
1688                                 " check the readme for details\n");
1689
1690         return t_relay_cancel(p_msg);
1691 }
1692
1693 /* set fr_inv_timeout & or fr_timeout; 0 means: use the default value */
1694 static int t_set_fr_all(struct sip_msg* msg, char* p1, char* p2)
1695 {
1696         int fr, fr_inv;
1697
1698         if (get_int_fparam(&fr_inv, msg, (fparam_t*)p1) < 0) return -1;
1699         if (p2) {
1700                 if (get_int_fparam(&fr, msg, (fparam_t*)p2) < 0) return -1;
1701         } else {
1702                 fr = 0;
1703         }
1704
1705         return t_set_fr(msg, fr_inv, fr);
1706 }
1707
1708 static int t_set_fr_inv(struct sip_msg* msg, char* fr_inv, char* foo)
1709 {
1710         return t_set_fr_all(msg, fr_inv, (char*)0);
1711 }
1712
1713 static int ki_t_set_fr(struct sip_msg* msg, int fr_inv, int fr)
1714 {
1715         return t_set_fr(msg, fr_inv, fr);
1716 }
1717
1718 static int ki_t_set_fr_inv(struct sip_msg* msg, int fr_inv)
1719 {
1720         return t_set_fr(msg, fr_inv, 0);
1721 }
1722
1723 /* reset fr_timer and fr_inv_timer to the default values */
1724 static int w_t_reset_fr(struct sip_msg* msg, char* foo, char* bar)
1725 {
1726         return t_reset_fr();
1727 }
1728
1729 static int ki_t_reset_fr(struct sip_msg* msg)
1730 {
1731         return t_reset_fr();
1732 }
1733
1734 static int ki_t_set_retr(sip_msg_t* msg, int t1, int t2)
1735 {
1736 #ifdef TM_DIFF_RT_TIMEOUT
1737         return t_set_retr(msg, t1, t2);
1738 #else
1739         LM_ERR("support for changing retransmission intervals on "
1740                         "the fly not compiled in (re-compile tm with"
1741                         " -DTM_DIFF_RT_TIMEOUT)\n");
1742         return -1;
1743 #endif
1744 }
1745
1746 /* set retr. intervals per transaction; 0 means: use the default value */
1747 static int w_t_set_retr(struct sip_msg* msg, char* p1, char* p2)
1748 {
1749         int t1, t2;
1750
1751         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1752         if (p2) {
1753                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1754         } else {
1755                 t2 = 0;
1756         }
1757         return ki_t_set_retr(msg, t1, t2);
1758 }
1759
1760 /* reset retr. t1 and t2 to the default values */
1761 int ki_t_reset_retr(sip_msg_t* msg)
1762 {
1763 #ifdef TM_DIFF_RT_TIMEOUT
1764         return t_reset_retr();
1765 #else
1766         LM_ERR("support for changing retransmission intervals on "
1767                         "the fly not compiled in (re-compile tm with"
1768                         " -DTM_DIFF_RT_TIMEOUT)\n");
1769         return -1;
1770 #endif
1771 }
1772
1773 int w_t_reset_retr(struct sip_msg* msg, char* foo, char* bar)
1774 {
1775         return ki_t_reset_retr(msg);
1776 }
1777
1778 /* set maximum transaction lifetime for inv & noninv */
1779 static int w_t_set_max_lifetime(struct sip_msg* msg, char* p1, char* p2)
1780 {
1781         int t1, t2;
1782
1783         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1784         if (p2) {
1785                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1786         } else {
1787                 t2 = 0;
1788         }
1789         return t_set_max_lifetime(msg, t1, t2);
1790 }
1791
1792 static int ki_t_set_max_lifetime(sip_msg_t* msg, int t1, int t2)
1793 {
1794         return t_set_max_lifetime(msg, t1, t2);
1795 }
1796
1797 /* reset maximum invite/non-invite lifetime to the default value */
1798 int w_t_reset_max_lifetime(struct sip_msg* msg, char* foo, char* bar)
1799 {
1800         return t_reset_max_lifetime();
1801 }
1802
1803 int ki_t_reset_max_lifetime(sip_msg_t* msg)
1804 {
1805         return t_reset_max_lifetime();
1806 }
1807
1808
1809 /**
1810  * helper macro, builds a function for setting a cell flag from the script.
1811  * e.g. W_T_SET_FLAG_GEN_FUNC(t_set_foo, T_FOO) =>
1812  * static int t_set_foo(struct sip_msg* msg, char*, char* )
1813  * that will expect fparam as first param and will set or reset T_FOO
1814  * in the current or next to be created transaction. */
1815 #define T_SET_FLAG_GEN_FUNC(fname, T_FLAG_NAME) \
1816         static int fname(sip_msg_t* msg, int state) \
1817 { \
1818         struct cell* t; \
1819         unsigned int set_flags; \
1820         unsigned int reset_flags; \
1821         \
1822         t=get_t(); \
1823         /* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction; \
1824          * in REQUEST_ROUTE T will be set only if the transaction was already  \
1825          * created; if not -> use the static variables */ \
1826         if (!t || t==T_UNDEFINED ){ \
1827                 set_flags=get_msgid_val(user_cell_set_flags, msg->id, int); \
1828                 reset_flags=get_msgid_val(user_cell_reset_flags, msg->id, int); \
1829                 if (state){ \
1830                         /* set */ \
1831                         set_flags|= T_FLAG_NAME; \
1832                         reset_flags&=~T_FLAG_NAME; \
1833                 }else{ \
1834                         /* reset */ \
1835                         set_flags&=~T_FLAG_NAME; \
1836                         reset_flags|=T_FLAG_NAME; \
1837                 } \
1838                 set_msgid_val(user_cell_set_flags, msg->id, int, set_flags); \
1839                 set_msgid_val(user_cell_reset_flags, msg->id, int, reset_flags); \
1840         }else{ \
1841                 if (state) \
1842                 t->flags|=T_FLAG_NAME; \
1843                 else \
1844                 t->flags&=~T_FLAG_NAME; \
1845         } \
1846         return 1; \
1847 }
1848
1849 #define W_T_SET_FLAG_GEN_FUNC(fname, T_FLAG_NAME) \
1850         static int w_##fname(sip_msg_t* msg, char* p1, char* p2) \
1851 { \
1852         int state; \
1853         if (get_int_fparam(&state, msg, (fparam_t*)p1) < 0) return -1; \
1854         return fname(msg, state); \
1855 }
1856
1857 /* set automatically sending 100 replies on/off for the current or
1858  * next to be created transaction */
1859 T_SET_FLAG_GEN_FUNC(t_set_auto_inv_100, T_AUTO_INV_100)
1860
1861 W_T_SET_FLAG_GEN_FUNC(t_set_auto_inv_100, T_AUTO_INV_100)
1862
1863
1864 /* set 6xx handling for the current or next to be created transaction */
1865 T_SET_FLAG_GEN_FUNC(t_set_disable_6xx, T_DISABLE_6xx)
1866
1867 W_T_SET_FLAG_GEN_FUNC(t_set_disable_6xx, T_DISABLE_6xx)
1868
1869
1870 /* disable dns failover for the current transaction */
1871 T_SET_FLAG_GEN_FUNC(t_set_disable_failover, T_DISABLE_FAILOVER)
1872
1873 W_T_SET_FLAG_GEN_FUNC(t_set_disable_failover, T_DISABLE_FAILOVER)
1874
1875
1876 #ifdef CANCEL_REASON_SUPPORT
1877 /* disable/enable e2e cancel reason copy for the current transaction */
1878 T_SET_FLAG_GEN_FUNC(t_set_no_e2e_cancel_reason, T_NO_E2E_CANCEL_REASON)
1879
1880 W_T_SET_FLAG_GEN_FUNC(t_set_no_e2e_cancel_reason, T_NO_E2E_CANCEL_REASON)
1881 #endif /* CANCEL_REASON_SUPPORT */
1882
1883
1884 /* disable internal negative reply for the current transaction */
1885 T_SET_FLAG_GEN_FUNC(t_set_disable_internal_reply, T_DISABLE_INTERNAL_REPLY)
1886
1887 W_T_SET_FLAG_GEN_FUNC(t_set_disable_internal_reply, T_DISABLE_INTERNAL_REPLY)
1888
1889
1890 /* FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1891  * returns true if the choosed "failure" branch failed because of a timeout,
1892  * -1 otherwise */
1893 int t_branch_timeout(sip_msg_t* msg)
1894 {
1895         switch(get_route_type()) {
1896                 case FAILURE_ROUTE:
1897                 case BRANCH_FAILURE_ROUTE:
1898                         return (msg->msg_flags & FL_TIMEOUT)?1:-1;
1899                 default:
1900                         LM_ERR("unsupported route type %d\n", get_route_type());
1901         }
1902         return -1;
1903 }
1904
1905
1906 /* script function, FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1907  * returns true if the choosed "failure" branch failed because of a timeout,
1908  * -1 otherwise */
1909 int w_t_branch_timeout(sip_msg_t* msg, char* foo, char* bar)
1910 {
1911         return t_branch_timeout(msg);
1912 }
1913
1914 /* FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1915  * returns true if the choosed "failure" branch ever received a reply,
1916  * -1 otherwise */
1917 int t_branch_replied(sip_msg_t* msg)
1918 {
1919         switch(get_route_type()) {
1920                 case FAILURE_ROUTE:
1921                 case BRANCH_FAILURE_ROUTE:
1922                         return (msg->msg_flags & FL_REPLIED)?1:-1;
1923                 default:
1924                         LM_ERR("unsupported route type %d\n", get_route_type());
1925         }
1926         return -1;
1927 }
1928
1929
1930 /* script function, FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1931  * returns true if the choosed "failure" branch ever received a reply,
1932  * -1 otherwise */
1933 int w_t_branch_replied(sip_msg_t* msg, char* foo, char* bar)
1934 {
1935         return t_branch_replied(msg);
1936 }
1937
1938 /* script function, returns: 1 if the transaction was canceled, -1 if not */
1939 int t_is_canceled(struct sip_msg* msg)
1940 {
1941         struct cell *t;
1942         int ret;
1943
1944         if (t_check( msg , 0 )==-1) return -1;
1945         t=get_t();
1946         if ((t==0) || (t==T_UNDEFINED)){
1947                 LM_ERR("cannot check a message"
1948                                 " for which no T-state has been established\n");
1949                 ret=-1;
1950         }else{
1951                 ret=(t->flags & T_CANCELED)?1:-1;
1952         }
1953         return ret;
1954 }
1955
1956 static int w_t_is_canceled(sip_msg_t* msg, char* foo, char* bar)
1957 {
1958         return t_is_canceled(msg);
1959 }
1960
1961 /* returns: 1 if the transaction is currently suspended, -1 if not */
1962 int t_is_retr_async_reply(sip_msg_t* msg)
1963 {
1964         struct cell *t;
1965         int ret;
1966
1967         if (t_check( msg , 0 )==-1) return -1;
1968         t=get_t();
1969         if ((t==0) || (t==T_UNDEFINED)){
1970                 LM_ERR("cannot check a message"
1971                                 " for which no T-state has been established\n");
1972                 ret=-1;
1973         }else{
1974                 LM_DBG("TRANSACTION FLAGS IS %d\n", t->flags);
1975                 ret=(t->flags & T_ASYNC_SUSPENDED)?1:-1;
1976         }
1977         return ret;
1978 }
1979
1980 /* script function, returns: 1 if the transaction is currently suspended,
1981  * -1 if not */
1982 static int w_t_is_retr_async_reply(sip_msg_t* msg, char* foo, char* bar)
1983 {
1984         return t_is_retr_async_reply(msg);
1985 }
1986
1987 /* returns: 1 if the transaction lifetime interval has already elapsed, -1 if not */
1988 int t_is_expired(sip_msg_t* msg)
1989 {
1990         struct cell *t;
1991         int ret;
1992
1993         if (t_check( msg , 0 )==-1) return -1;
1994         t=get_t();
1995         if ((t==0) || (t==T_UNDEFINED)){
1996                 LM_ERR("cannot check a message"
1997                                 " for which no T-state has been established\n");
1998                 ret=-1;
1999         }else{
2000                 ret=(TICKS_GT(t->end_of_life, get_ticks_raw()))?-1:1;
2001         }
2002         return ret;
2003 }
2004
2005 /* script function, returns: 1 if the transaction lifetime interval
2006  * has already elapsed, -1 if not */
2007 int w_t_is_expired(sip_msg_t* msg, char* foo, char* bar)
2008 {
2009         return t_is_expired(msg);
2010 }
2011
2012 /* returns: 1 if any of the branches did timeout, -1 if not */
2013 int t_any_timeout(sip_msg_t* msg)
2014 {
2015         struct cell *t;
2016         int r;
2017
2018         if (t_check( msg , 0 )==-1) return -1;
2019         t=get_t();
2020         if ((t==0) || (t==T_UNDEFINED)){
2021                 LM_ERR("cannot check a message"
2022                                 " for which no T-state has been established\n");
2023                 return -1;
2024         }else{
2025                 for (r=0; r<t->nr_of_outgoings; r++){
2026                         if (t->uac[r].request.flags & F_RB_TIMEOUT)
2027                                 return 1;
2028                 }
2029         }
2030         return -1;
2031 }
2032
2033
2034 /* script function, returns: 1 if any of the branches did timeout, -1 if not */
2035 int w_t_any_timeout(sip_msg_t* msg, char* foo, char* bar)
2036 {
2037         return t_any_timeout(msg);
2038 }
2039
2040 /* returns: 1 if any of the branches received at leat one
2041  * reply, -1 if not */
2042 int t_any_replied(sip_msg_t* msg)
2043 {
2044         struct cell *t;
2045         int r;
2046
2047         if (t_check( msg , 0 )==-1) return -1;
2048         t=get_t();
2049         if ((t==0) || (t==T_UNDEFINED)){
2050                 LM_ERR("cannot check a message"
2051                                 " for which no T-state has been established\n");
2052                 return -1;
2053         }else{
2054                 for (r=0; r<t->nr_of_outgoings; r++){
2055                         if (t->uac[r].request.flags & F_RB_REPLIED)
2056                                 return 1;
2057                 }
2058         }
2059         return -1;
2060 }
2061
2062
2063 /* script function, returns: 1 if any of the branches received at leat one
2064  * reply, -1 if not */
2065 int w_t_any_replied(sip_msg_t* msg, char* foo, char* bar)
2066 {
2067         return t_any_replied(msg);
2068 }
2069
2070
2071 /* returns: 1 if any of the branches received the
2072  *  reply code "status" */
2073 int t_grep_status(sip_msg_t* msg, int code)
2074 {
2075         struct cell *t;
2076         int r;
2077
2078         if (t_check( msg , 0 )==-1) return -1;
2079         t=get_t();
2080         if ((t==0) || (t==T_UNDEFINED)){
2081                 LM_ERR("cannot check a message"
2082                                 " for which no T-state has been established\n");
2083                 return -1;
2084         }else{
2085                 for (r=0; r<t->nr_of_outgoings; r++){
2086                         if ((t->uac[r].last_received==code)  &&
2087                                         (t->uac[r].request.flags & F_RB_REPLIED))
2088                                 return 1;
2089                 }
2090         }
2091         return -1;
2092 }
2093
2094
2095 /* script function, returns: 1 if any of the branches received the
2096  *  reply code "status" */
2097 int w_t_grep_status(struct sip_msg* msg, char* status, char* bar)
2098 {
2099         int code;
2100
2101         if (get_int_fparam(&code, msg, (fparam_t*)status) < 0) return -1;
2102
2103         return t_grep_status(msg, code);
2104 }
2105
2106 /* drop all the existing replies in failure_route to make sure
2107  * that none of them is picked up again */
2108 static int t_drop_replies_helper(sip_msg_t* msg, char* mode)
2109 {
2110         if(mode==NULL)
2111                 t_drop_replies(1);
2112         else if(*mode=='n')
2113                 t_drop_replies(0);
2114         else if(*mode=='l')
2115                 t_drop_replies(2);
2116         else
2117                 t_drop_replies(1);
2118         return 1;
2119 }
2120
2121 static int w_t_drop_replies(struct sip_msg* msg, char* mode, char* bar)
2122 {
2123         return t_drop_replies_helper(msg, mode);
2124 }
2125
2126 static int ki_t_drop_replies(sip_msg_t* msg, str* mode)
2127 {
2128         return t_drop_replies_helper(msg, (mode)?mode->s:NULL);
2129 }
2130
2131 static int ki_t_drop_replies_all(sip_msg_t* msg)
2132 {
2133         return t_drop_replies_helper(msg, NULL);
2134 }
2135
2136 /* save the message lumps after t_newtran() but before t_relay() */
2137 static int ki_t_save_lumps(sip_msg_t* msg)
2138 {
2139         struct cell *t;
2140
2141         if (is_route_type(REQUEST_ROUTE)) {
2142                 t=get_t();
2143                 if (!t || t==T_UNDEFINED) {
2144                         LM_ERR("transaction has not been created yet\n");
2145                         return -1;
2146                 }
2147
2148                 if (save_msg_lumps(t->uas.request, msg)) {
2149                         LM_ERR("failed to save the message lumps\n");
2150                         return -1;
2151                 }
2152         } /* else nothing to do, the lumps have already been saved */
2153         return 1;
2154 }
2155
2156 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar)
2157 {
2158         return ki_t_save_lumps(msg);
2159 }
2160
2161 /* wrapper function needed after changes in w_t_reply */
2162 int w_t_reply_wrp(struct sip_msg *m, unsigned int code, char *txt)
2163 {
2164         fparam_t c;
2165         fparam_t r;
2166
2167         c.type = FPARAM_INT;
2168         c.orig = NULL; /* ? */
2169         c.v.i = code;
2170
2171         r.type = FPARAM_STRING;
2172         r.orig = NULL; /* ? */
2173         r.v.asciiz = txt;
2174
2175         return w_t_reply(m, (char *)&c, (char*)&r);
2176 }
2177
2178
2179
2180 /** script function, check if a msg is assoc. to a transaction.
2181  * @return -1 (not), 1 (reply, e2e ack or cancel for an existing transaction),
2182  *          0 (request retransmission, ack to negative reply or ack to local
2183  *           transaction)
2184  * Note: the e2e ack matching works only for local e2e acks or for
2185  *       transactions with E2EACK* callbacks installed (but even in this
2186  *       case matching E2EACKs on proxied transaction is not entirely
2187  *       reliable: if the ACK  is delayed the proxied transaction might
2188  *       be already deleted when it reaches the proxy (wait_timeout))
2189  */
2190 int t_check_trans(struct sip_msg* msg)
2191 {
2192         struct cell* t;
2193         int branch;
2194         int ret;
2195
2196         /* already processing a T */
2197         if(is_route_type(FAILURE_ROUTE)
2198                         || is_route_type(BRANCH_ROUTE)
2199                         || is_route_type(BRANCH_FAILURE_ROUTE)
2200                         || is_route_type(TM_ONREPLY_ROUTE)) {
2201                 return 1;
2202         }
2203
2204         if (msg->first_line.type==SIP_REPLY) {
2205                 branch = 0;
2206                 ret = (t_check_msg( msg , &branch)==1) ? 1 : -1;
2207                 tm_ctx_set_branch_index(branch);
2208                 return ret;
2209         } else if (msg->REQ_METHOD==METHOD_CANCEL) {
2210                 return w_t_lookup_cancel(msg, 0, 0);
2211         } else {
2212                 switch(t_check_msg(msg, 0)){
2213                         case -2: /* possible e2e ack */
2214                                 return 1;
2215                         case 1: /* found */
2216                                 t=get_t();
2217                                 if (msg->REQ_METHOD==METHOD_ACK){
2218                                         /* ack to neg. reply  or ack to local trans.
2219                                          * => process it and end the script */
2220                                         /* FIXME: there's no way to distinguish here
2221                                          * between acks to local trans. and neg. acks */
2222                                         if (unlikely(has_tran_tmcbs(t, TMCB_ACK_NEG_IN)))
2223                                                 run_trans_callbacks(TMCB_ACK_NEG_IN, t, msg,
2224                                                                 0, msg->REQ_METHOD);
2225                                         t_release_transaction(t);
2226                                 } else {
2227                                         /* is a retransmission */
2228                                         if (unlikely(has_tran_tmcbs(t, TMCB_REQ_RETR_IN)))
2229                                                 run_trans_callbacks(TMCB_REQ_RETR_IN, t, msg,
2230                                                                 0, msg->REQ_METHOD);
2231                                         t_retransmit_reply(t);
2232                                 }
2233                                 /* no need for UNREF(t); set_t(0) - the end-of-script
2234                                  * t_unref callback will take care of them */
2235                                 return 0; /* exit from the script */
2236                 }
2237                 /* not found or error */
2238         }
2239         return -1;
2240 }
2241
2242 static int w_t_check_trans(struct sip_msg* msg, char* foo, char* bar)
2243 {
2244         return t_check_trans(msg);
2245 }
2246
2247 static int hexatoi(str *s, unsigned int* result)
2248 {
2249         int i, xv, fact;
2250
2251         /* more than 32bit hexa? */
2252         if (s->len>8)
2253                 return -1;
2254
2255         *result = 0;
2256         fact = 1;
2257
2258         for(i=s->len-1; i>=0 ;i--)
2259         {
2260                 xv = hex2int(s->s[i]);
2261                 if(xv<0)
2262                         return -1;
2263
2264                 *result += (xv * fact);
2265                 fact *= 16;
2266         }
2267         return 0;
2268 }
2269
2270 static int fixup_t_relay_to(void** param, int param_no)
2271 {
2272
2273         int port;
2274         int proto;
2275         unsigned int flags;
2276         struct proxy_l *proxy;
2277         action_u_t *a;
2278         str s;
2279         str host;
2280
2281         s.s = (char*)*param;
2282         s.len = strlen(s.s);
2283         LM_DBG("fixing (%s, %d)\n", s.s, param_no);
2284         if (param_no==1){
2285                 a = fixup_get_param(param, param_no, 2);
2286                 if(a==NULL)
2287                 {
2288                         LM_CRIT("server error for parameter <%s>\n",s.s);
2289                         return E_UNSPEC;
2290                 }
2291                 if(a->u.string!=NULL) {
2292                         /* second parameter set, first should be proxy addr */
2293                         if (parse_phostport(s.s, &host.s, &host.len, &port, &proto)!=0){
2294                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2295                                 return E_UNSPEC;
2296                         }
2297
2298                         proxy = mk_proxy(&host, port, proto);
2299                         if (proxy==0) {
2300                                 LM_ERR("failed to build proxy structure for <%.*s>\n",
2301                                                 host.len, host.s );
2302                                 return E_UNSPEC;
2303                         }
2304                         *(param)=proxy;
2305                         return 0;
2306                 } else {
2307                         /* no second parameter, then is proxy addr or flags */
2308                         flags = 0;
2309                         if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2310                                 s.s += 2;
2311                                 s.len -= 2;
2312                                 if(hexatoi(&s, &flags)<0)
2313                                 {
2314                                         LM_CRIT("invalid hexa flags <%s>\n", s.s);
2315                                         return E_UNSPEC;
2316                                 }
2317                                 a->u.data = (void*)(unsigned long int)flags;
2318                                 *(param)= 0;
2319                                 return 0;
2320                         } else {
2321                                 if(str2int(&s, &flags)==0)
2322                                 {
2323                                         a->u.data = (void*)(unsigned long int)flags;
2324                                         *(param)= 0;
2325                                         return 0;
2326                                 } else {
2327                                         /* try proxy */
2328                                         if (parse_phostport(s.s, &host.s, &host.len,
2329                                                                 &port, &proto)!=0){
2330                                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2331                                                 return E_UNSPEC;
2332                                         }
2333
2334                                         proxy = mk_proxy(&host, port, proto);
2335                                         if (proxy==0) {
2336                                                 LM_ERR("failed to build proxy structure for <%.*s>\n",
2337                                                                 host.len, host.s );
2338                                                 return E_UNSPEC;
2339                                         }
2340                                         *(param)=proxy;
2341                                         return 0;
2342                                 }
2343                         }
2344                 }
2345         } else if (param_no==2) {
2346                 /* flags */
2347                 flags = 0;
2348                 if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2349                         s.s += 2;
2350                         s.len -= 2;
2351                         if(hexatoi(&s, &flags)<0)
2352                         {
2353                                 LM_CRIT("invalid hexa flags <%s>\n", s.s);
2354                                 return E_UNSPEC;
2355                         }
2356                         *(param) = (void*)(unsigned long int)flags;
2357                         return 0;
2358                 } else {
2359                         if(str2int(&s, &flags)==0)
2360                         {
2361                                 *(param) = (void*)(unsigned long int)flags;
2362                                 return 0;
2363                         } else {
2364                                 LM_CRIT("invalid flags <%s>\n", s.s);
2365                                 return E_UNSPEC;
2366                         }
2367                 }
2368         } else {
2369                 LM_ERR("invalid parameter number %d\n", param_no);
2370                 return E_BUG;
2371         }
2372 }
2373
2374
2375 inline static int w_t_relay_to(struct sip_msg *msg, char *proxy, char *flags)
2376 {
2377         unsigned int fl;
2378         struct proxy_l *px;
2379         fparam_t param;
2380
2381         fl = (unsigned int)(long)(void*)flags;
2382         px = (struct proxy_l*)proxy;
2383
2384         if(flags!=0)
2385         {
2386                 memset(&param, 0, sizeof(fparam_t));
2387                 param.type = FPARAM_INT;
2388                 /* no auto 100 trying */
2389                 if(fl&1) {
2390                         param.v.i = 0;
2391                         w_t_set_auto_inv_100(msg, (char*)(&param), 0);
2392                 }
2393                 /* no auto negative reply */
2394                 if(fl&2) {
2395                         param.v.i = 1;
2396                         w_t_set_disable_internal_reply(msg, (char*)(&param), 0);
2397                 }
2398                 /* no dns failover */
2399                 if(fl&4) {
2400                         param.v.i = 1;
2401                         w_t_set_disable_failover(msg, (char*)(&param), 0);
2402                 }
2403         }
2404         return _w_t_relay_to(msg, px, PROTO_NONE);
2405 }
2406
2407
2408 static int ki_t_use_uac_headers(sip_msg_t* msg)
2409 {
2410         tm_cell_t *t;
2411
2412         t=get_t();
2413         if (t!=NULL && t!=T_UNDEFINED) {
2414                 t->uas.request->msg_flags |= FL_USE_UAC_FROM|FL_USE_UAC_TO;
2415         }
2416         msg->msg_flags |= FL_USE_UAC_FROM|FL_USE_UAC_TO;
2417
2418         return 1;
2419 }
2420
2421 static int w_t_use_uac_headers(sip_msg_t* msg, char* foo, char* bar)
2422 {
2423         return ki_t_use_uac_headers(msg);
2424 }
2425
2426 static int w_t_uac_send(sip_msg_t* msg, char* pmethod, char* pruri,
2427                 char* pnexthop, char* psock, char *phdrs, char* pbody)
2428 {
2429         str method = STR_NULL;
2430         str ruri = STR_NULL;
2431         str nexthop = STR_NULL;
2432         str send_socket = STR_NULL;
2433         str headers = STR_NULL;
2434         str body = STR_NULL;
2435
2436         if(fixup_get_svalue(msg, (gparam_t*)pmethod, &method)!=0) {
2437                 LM_ERR("invalid method parameter");
2438                 return -1;
2439         }
2440         if(fixup_get_svalue(msg, (gparam_t*)pruri, &ruri)!=0) {
2441                 LM_ERR("invalid ruri parameter");
2442                 return -1;
2443         }
2444         if(fixup_get_svalue(msg, (gparam_t*)pnexthop, &nexthop)!=0) {
2445                 LM_ERR("invalid nexthop parameter");
2446                 return -1;
2447         }
2448         if(fixup_get_svalue(msg, (gparam_t*)psock, &send_socket)!=0) {
2449                 LM_ERR("invalid send socket parameter");
2450                 return -1;
2451         }
2452         if(fixup_get_svalue(msg, (gparam_t*)phdrs, &headers)!=0) {
2453                 LM_ERR("invalid headers parameter");
2454                 return -1;
2455         }
2456         if(fixup_get_svalue(msg, (gparam_t*)pbody, &body)!=0) {
2457                 LM_ERR("invalid body parameter");
2458                 return -1;
2459         }
2460
2461         if(t_uac_send(&method, &ruri, &nexthop, &send_socket, &headers, &body)<0) {
2462                 return -1;
2463         }
2464         return 1;
2465 }
2466
2467 static int ki_t_uac_send(sip_msg_t* msg, str* method, str* ruri,
2468                 str* nexthop, str* ssock, str *hdrs, str* body)
2469 {
2470         if(t_uac_send(method, ruri, nexthop, ssock, hdrs, body)<0) {
2471                 return -1;
2472         }
2473         return 1;
2474 }
2475
2476 /* rpc docs */
2477
2478 static const char* rpc_cancel_doc[2] = {
2479         "Cancel a pending transaction",
2480         0
2481 };
2482
2483 static const char* rpc_reply_doc[2] = {
2484         "Reply transaction",
2485         0
2486 };
2487
2488 static const char* rpc_reply_callid_doc[2] = {
2489         "Reply transaction by call-id",
2490         0
2491 };
2492
2493 static const char* tm_rpc_stats_doc[2] = {
2494         "Print transaction statistics.",
2495         0
2496 };
2497
2498 static const char* tm_rpc_hash_stats_doc[2] = {
2499         "Prints hash table statistics (can be used only if tm is compiled"
2500                 " with -DTM_HASH_STATS).",
2501         0
2502 };
2503
2504 static const char* rpc_t_uac_start_doc[2] = {
2505         "starts a tm uac using  a list of string parameters: method, ruri, dst_uri"
2506                 ", send_sock, headers (CRLF separated) and body (optional)",
2507         0
2508 };
2509
2510 static const char* rpc_t_uac_wait_doc[2] = {
2511         "starts a tm uac and waits for the final reply, using a list of string "
2512                 "parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
2513                 " and body (optional)",
2514         0
2515 };
2516
2517
2518 /* rpc exports */
2519 static rpc_export_t tm_rpc[] = {
2520         {"tm.cancel", rpc_cancel,   rpc_cancel_doc,   0},
2521         {"tm.reply",  rpc_reply,    rpc_reply_doc,    0},
2522         {"tm.reply_callid", rpc_reply_callid,   rpc_reply_callid_doc,   0},
2523         {"tm.stats",  tm_rpc_stats, tm_rpc_stats_doc, 0},
2524         {"tm.hash_stats",  tm_rpc_hash_stats, tm_rpc_hash_stats_doc, 0},
2525         {"tm.t_uac_start", rpc_t_uac_start, rpc_t_uac_start_doc, 0 },
2526         {"tm.t_uac_wait",  rpc_t_uac_wait,  rpc_t_uac_wait_doc, RET_ARRAY},
2527         {0, 0, 0, 0}
2528 };
2529
2530 /**
2531  *
2532  */
2533 static int ki_t_on_failure(sip_msg_t *msg, str *rname)
2534 {
2535         int ridx;
2536         sr_kemi_eng_t *keng;
2537
2538         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2539                 ridx = 0;
2540         } else {
2541                 keng = sr_kemi_eng_get();
2542                 if(keng==NULL) {
2543                         ridx = route_get(&failure_rt, rname->s);
2544                 } else {
2545                         ridx = sr_kemi_cbname_lookup_name(rname);
2546                 }
2547         }
2548         if(ridx<0) { ridx = 0; }
2549
2550         t_on_failure(ridx);
2551         return 1;
2552 }
2553
2554 /**
2555  *
2556  */
2557 static int ki_t_on_branch_failure(sip_msg_t *msg, str *rname)
2558 {
2559         int ridx;
2560         sr_kemi_eng_t *keng;
2561
2562         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2563                 ridx = 0;
2564         } else {
2565                 keng = sr_kemi_eng_get();
2566                 if(keng==NULL) {
2567                         ridx = route_get(&event_rt, rname->s);
2568                 } else {
2569                         ridx = sr_kemi_cbname_lookup_name(rname);
2570                 }
2571         }
2572         if(ridx<0) { ridx = 0; }
2573
2574         t_on_branch_failure(ridx);
2575         return 1;
2576 }
2577
2578
2579 /**
2580  *
2581  */
2582 static int ki_t_on_branch(sip_msg_t *msg, str *rname)
2583 {
2584         int ridx;
2585         sr_kemi_eng_t *keng;
2586
2587         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2588                 ridx = 0;
2589         } else {
2590                 keng = sr_kemi_eng_get();
2591                 if(keng==NULL) {
2592                         ridx = route_get(&branch_rt, rname->s);
2593                 } else {
2594                         ridx = sr_kemi_cbname_lookup_name(rname);
2595                 }
2596         }
2597         if(ridx<0) { ridx = 0; }
2598
2599         t_on_branch(ridx);
2600         return 1;
2601 }
2602
2603 /**
2604  *
2605  */
2606 static int ki_t_on_reply(sip_msg_t *msg, str *rname)
2607 {
2608         int ridx;
2609         sr_kemi_eng_t *keng;
2610
2611         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2612                 ridx = 0;
2613         } else {
2614                 keng = sr_kemi_eng_get();
2615                 if(keng==NULL) {
2616                         ridx = route_get(&onreply_rt, rname->s);
2617                 } else {
2618                         ridx = sr_kemi_cbname_lookup_name(rname);
2619                 }
2620         }
2621         if(ridx<0) { ridx = 0; }
2622
2623         t_on_reply(ridx);
2624         return 1;
2625 }
2626
2627 /**
2628  *
2629  */
2630 static int ki_t_relay(sip_msg_t *msg)
2631 {
2632         return _w_t_relay_to(msg, (struct proxy_l *)0, PROTO_NONE);
2633 }
2634
2635 /**
2636  *
2637  */
2638 static int ki_t_reply(sip_msg_t *msg, int code, str *reason)
2639 {
2640         return w_t_reply_wrp(msg, (unsigned int)code, reason->s);
2641 }
2642
2643 /**
2644  *
2645  */
2646 static sr_kemi_t tm_kemi_exports[] = {
2647         { str_init("tm"), str_init("t_relay"),
2648                 SR_KEMIP_INT, ki_t_relay,
2649                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2650                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2651         },
2652         { str_init("tm"), str_init("t_on_branch"),
2653                 SR_KEMIP_INT, ki_t_on_branch,
2654                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2655                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2656         },
2657         { str_init("tm"), str_init("t_on_failure"),
2658                 SR_KEMIP_INT, ki_t_on_failure,
2659                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2660                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2661         },
2662         { str_init("tm"), str_init("t_on_branch_failure"),
2663                 SR_KEMIP_INT, ki_t_on_branch_failure,
2664                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2665                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2666         },
2667         { str_init("tm"), str_init("t_on_reply"),
2668                 SR_KEMIP_INT, ki_t_on_reply,
2669                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2670                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2671         },
2672         { str_init("tm"), str_init("t_reply"),
2673                 SR_KEMIP_INT, ki_t_reply,
2674                 { SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
2675                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2676         },
2677         { str_init("tm"), str_init("t_check_trans"),
2678                 SR_KEMIP_INT, t_check_trans,
2679                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2680                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2681         },
2682         { str_init("tm"), str_init("t_is_canceled"),
2683                 SR_KEMIP_INT, t_is_canceled,
2684                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2685                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2686         },
2687         { str_init("tm"), str_init("t_newtran"),
2688                 SR_KEMIP_INT, t_newtran,
2689                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2690                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2691         },
2692         { str_init("tm"), str_init("t_release"),
2693                 SR_KEMIP_INT, t_release,
2694                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2695                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2696         },
2697         { str_init("tm"), str_init("t_replicate"),
2698                 SR_KEMIP_INT, t_replicate_uri,
2699                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2700                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2701         },
2702         { str_init("tm"), str_init("t_is_set"),
2703                 SR_KEMIP_INT, t_is_set,
2704                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2705                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2706         },
2707         { str_init("tm"), str_init("t_lookup_request"),
2708                 SR_KEMIP_INT, ki_t_lookup_request,
2709                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2710                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2711         },
2712         { str_init("tm"), str_init("t_lookup_cancel"),
2713                 SR_KEMIP_INT, ki_t_lookup_cancel,
2714                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2715                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2716         },
2717         { str_init("tm"), str_init("t_lookup_cancel_flags"),
2718                 SR_KEMIP_INT, ki_t_lookup_cancel_flags,
2719                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2720                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2721         },
2722         { str_init("tm"), str_init("t_retransmit_reply"),
2723                 SR_KEMIP_INT, ki_t_retransmit_reply,
2724                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2725                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2726         },
2727         { str_init("tm"), str_init("t_set_fr_inv"),
2728                 SR_KEMIP_INT, ki_t_set_fr_inv,
2729                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2730                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2731         },
2732         { str_init("tm"), str_init("t_set_fr"),
2733                 SR_KEMIP_INT, ki_t_set_fr,
2734                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2735                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2736         },
2737         { str_init("tm"), str_init("t_reset_fr"),
2738                 SR_KEMIP_INT, ki_t_reset_fr,
2739                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2740                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2741         },
2742         { str_init("tm"), str_init("t_set_max_lifetime"),
2743                 SR_KEMIP_INT, ki_t_set_max_lifetime,
2744                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2745                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2746         },
2747         { str_init("tm"), str_init("t_reset_max_lifetime"),
2748                 SR_KEMIP_INT, ki_t_reset_max_lifetime,
2749                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2750                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2751         },
2752         { str_init("tm"), str_init("t_set_retr"),
2753                 SR_KEMIP_INT, ki_t_set_retr,
2754                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2755                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2756         },
2757         { str_init("tm"), str_init("t_reset_retr"),
2758                 SR_KEMIP_INT, ki_t_reset_retr,
2759                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2760                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2761         },
2762         { str_init("tm"), str_init("t_uac_send"),
2763                 SR_KEMIP_INT, ki_t_uac_send,
2764                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
2765                         SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR }
2766         },
2767         { str_init("tm"), str_init("t_load_contacts"),
2768                 SR_KEMIP_INT, ki_t_load_contacts,
2769                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2770                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2771         },
2772         { str_init("tm"), str_init("t_next_contacts"),
2773                 SR_KEMIP_INT, ki_t_next_contacts,
2774                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2775                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2776         },
2777         { str_init("tm"), str_init("t_next_contact_flow"),
2778                 SR_KEMIP_INT, ki_t_next_contact_flow,
2779                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2780                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2781         },
2782         { str_init("tm"), str_init("t_drop_replies_all"),
2783                 SR_KEMIP_INT, ki_t_drop_replies_all,
2784                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2785                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2786         },
2787         { str_init("tm"), str_init("t_drop_replies"),
2788                 SR_KEMIP_INT, ki_t_drop_replies,
2789                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2790                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2791         },
2792         { str_init("tm"), str_init("t_use_uac_headers"),
2793                 SR_KEMIP_INT, ki_t_use_uac_headers,
2794                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2795                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2796         },
2797         { str_init("tm"), str_init("t_save_lumps"),
2798                 SR_KEMIP_INT, ki_t_save_lumps,
2799                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2800                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2801         },
2802         { str_init("tm"), str_init("t_is_expired"),
2803                 SR_KEMIP_INT, t_is_expired,
2804                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2805                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2806         },
2807         { str_init("tm"), str_init("t_grep_status"),
2808                 SR_KEMIP_INT, t_grep_status,
2809                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2810                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2811         },
2812         { str_init("tm"), str_init("t_is_retr_async_reply"),
2813                 SR_KEMIP_INT, t_is_retr_async_reply,
2814                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2815                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2816         },
2817         { str_init("tm"), str_init("t_any_replied"),
2818                 SR_KEMIP_INT, t_any_replied,
2819                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2820                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2821         },
2822         { str_init("tm"), str_init("t_any_timeout"),
2823                 SR_KEMIP_INT, t_any_timeout,
2824                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2825                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2826         },
2827         { str_init("tm"), str_init("t_branch_replied"),
2828                 SR_KEMIP_INT, t_branch_replied,
2829                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2830                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2831         },
2832         { str_init("tm"), str_init("t_branch_timeout"),
2833                 SR_KEMIP_INT, t_branch_timeout,
2834                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2835                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2836         },
2837         { str_init("tm"), str_init("t_set_auto_inv_100"),
2838                 SR_KEMIP_INT, t_set_auto_inv_100,
2839                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2840                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2841         },
2842         { str_init("tm"), str_init("t_set_disable_6xx"),
2843                 SR_KEMIP_INT, t_set_disable_6xx,
2844                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2845                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2846         },
2847         { str_init("tm"), str_init("t_set_disable_failover"),
2848                 SR_KEMIP_INT, t_set_disable_failover,
2849                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2850                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2851         },
2852         { str_init("tm"), str_init("t_set_no_e2e_cancel_reason"),
2853                 SR_KEMIP_INT, t_set_no_e2e_cancel_reason,
2854                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2855                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2856         },
2857         { str_init("tm"), str_init("t_set_disable_internal_reply"),
2858                 SR_KEMIP_INT, t_set_disable_internal_reply,
2859                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2860                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2861         },
2862
2863
2864         { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
2865 };
2866
2867
2868 int mod_register(char *path, int *dlflags, void *p1, void *p2)
2869 {
2870         sr_kemi_modules_add(tm_kemi_exports);
2871         return 0;
2872 }