tm: cleaner handling of dynamic regex param for t_check_status()
[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* re0 = NULL;
866         regex_t* re = NULL;
867         str tmp;
868
869         fp = (fparam_t*)p1;
870         t = 0;
871         /* first get the transaction */
872         if (t_check(msg, 0 ) == -1) return -1;
873         if ((t = get_t()) == 0) {
874                 LM_ERR("cannot check status for a reply"
875                                 " which has no T-state established\n");
876                 goto error;
877         }
878         backup = 0;
879
880         switch(fp->type) {
881                 case FPARAM_REGEX:
882                         re = fp->v.regex;
883                         break;
884
885                 default:
886                         /* AVP or select, get the value and compile the regex */
887                         if (get_str_fparam(&tmp, msg, fp) < 0) goto error;
888                         s = pkg_malloc(tmp.len + 1);
889                         if (s == NULL) {
890                                 LM_ERR("Out of memory\n");
891                                 goto error;
892                         }
893                         memcpy(s, tmp.s, tmp.len);
894                         s[tmp.len] = '\0';
895
896                         if ((re0 = pkg_malloc(sizeof(regex_t))) == 0) {
897                                 LM_ERR("No memory left\n");
898                                 goto error;
899                         }
900
901                         if (regcomp(re0, s, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
902                                 LM_ERR("Bad regular expression '%s'\n", s);
903                                 pkg_free(re0);
904                                 re0 = NULL;
905                                 goto error;
906                         }
907                         re = re0;
908                         break;
909         }
910
911         switch(get_route_type()) {
912                 case REQUEST_ROUTE:
913                         /* use the status of the last sent reply */
914                         status = int2str( t->uas.status, 0);
915                         break;
916
917                 case TM_ONREPLY_ROUTE:
918                 case CORE_ONREPLY_ROUTE:
919                         /* use the status of the current reply */
920                         status = msg->first_line.u.reply.status.s;
921                         backup = status[msg->first_line.u.reply.status.len];
922                         status[msg->first_line.u.reply.status.len] = 0;
923                         break;
924
925                 case FAILURE_ROUTE:
926                         /* use the status of the winning reply */
927                         ret = t_pick_branch( -1, 0, t, &lowest_status);
928                         if (ret == -1) {
929                                 /* t_pick_branch() retuns error also when there are only
930                                  * blind UACs. Let us give it another chance including the
931                                  * blind branches. */
932                                 LM_DBG("t_pick_branch returned error,"
933                                                 " trying t_pick_branch_blind\n");
934                                 ret = t_pick_branch_blind(t, &lowest_status);
935                         }
936                         if (ret < 0) {
937                                 LM_CRIT("BUG: t_pick_branch failed to get"
938                                                 " a final response in FAILURE_ROUTE\n");
939                                 goto error;
940                         }
941                         status = int2str( lowest_status , 0);
942                         break;
943                 case BRANCH_FAILURE_ROUTE:
944                         status = int2str(t->uac[get_t_branch()].last_received, 0);
945                         break;
946                 default:
947                         LM_ERR("unsupported route type %d\n",
948                                         get_route_type());
949                         goto error;
950         }
951
952         LM_DBG("checked status is <%s>\n",status);
953         /* do the checking */
954         n = regexec(re, status, 1, &pmatch, 0);
955
956         if (backup) status[msg->first_line.u.reply.status.len] = backup;
957         if (s) pkg_free(s);
958         if (re0) {
959                 regfree(re0);
960                 pkg_free(re0);
961         }
962
963         if (unlikely(t && is_route_type(CORE_ONREPLY_ROUTE))){
964                 /* t_check() above has the side effect of setting T and
965                  * REFerencing T => we must unref and unset it.  */
966                 UNREF( t );
967                 set_t(T_UNDEFINED, T_BR_UNDEFINED);
968         }
969         if (n!=0) return -1;
970         return 1;
971
972 error:
973         if (unlikely(t && is_route_type(CORE_ONREPLY_ROUTE))){
974                 /* t_check() above has the side effect of setting T and
975                  * REFerencing T => we must unref and unset it.  */
976                 UNREF( t );
977                 set_t(T_UNDEFINED, T_BR_UNDEFINED);
978         }
979         if (s) pkg_free(s);
980         if (re0) {
981                 regfree(re0);
982                 pkg_free(re0);
983         }
984         return -1;
985 }
986
987
988 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2)
989 {
990         return (t_check_msg( msg , 0  )==1) ? 1 : -1;
991 }
992
993 static int ki_t_lookup_request(struct sip_msg* msg)
994 {
995         return (t_check_msg( msg , 0  )==1) ? 1 : -1;
996 }
997
998 static int ki_t_lookup_cancel_flags(sip_msg_t* msg, int flags)
999 {
1000         struct cell *ret;
1001         if (msg->REQ_METHOD==METHOD_CANCEL) {
1002                 ret = t_lookupOriginalT( msg );
1003                 LM_DBG("lookup_original: t_lookupOriginalT returned: %p\n", ret);
1004                 if (ret != T_NULL_CELL) {
1005                         /* If the parameter is set to 1, overwrite the message flags of
1006                          * the CANCEL with the flags of the INVITE */
1007                         if (flags)
1008                                 msg->flags = ret->uas.request->flags;
1009
1010                         /* The cell is reffed by t_lookupOriginalT, but T is not set.
1011                            So we must unref it before returning. */
1012                         UNREF(ret);
1013                         return 1;
1014                 }
1015         } else {
1016                 LM_WARN("script error - t_lookup_cancel() called for non-CANCEL request\n");
1017         }
1018         return -1;
1019 }
1020
1021 static int ki_t_lookup_cancel(sip_msg_t* msg)
1022 {
1023         return ki_t_lookup_cancel_flags(msg, 0);
1024 }
1025
1026 inline static int w_t_lookup_cancel(struct sip_msg* msg, char* str, char* str2)
1027 {
1028         int i = 0;
1029
1030         if(str) {
1031                 if(get_int_fparam(&i, msg, (fparam_t*)str)<0) return -1;
1032         }
1033         return ki_t_lookup_cancel_flags(msg, i);
1034 }
1035
1036 inline static int str2proto(char *s, int len) {
1037         if (len == 3 && !strncasecmp(s, "udp", 3))
1038                 return PROTO_UDP;
1039         else if (len == 3 && !strncasecmp(s, "tcp", 3))  /* tcp&tls checks will be
1040                                                                                                                 passed in getproto() */
1041                 return PROTO_TCP;
1042         else if (len == 3 && !strncasecmp(s, "tls", 3))
1043                 return PROTO_TLS;
1044         else if (len == 4 && !strncasecmp(s, "sctp", 4))
1045                 return PROTO_SCTP;
1046         else if (len == 2 && !strncasecmp(s, "ws", 2))
1047                 return PROTO_WS;
1048         else if (len == 3 && !strncasecmp(s, "wss", 3)) {
1049                 LM_WARN("\"wss\" used somewhere...\n");
1050                 return PROTO_WS;
1051         } else
1052                 return PROTO_NONE;
1053 }
1054
1055 inline static struct proxy_l* t_protoaddr2proxy(char *proto_par, char *addr_par) {
1056         struct proxy_l *proxy = 0;
1057         avp_t* avp;
1058         avp_value_t val;
1059         int proto, port, err;
1060         str s;
1061         char *c;
1062
1063         switch(((fparam_t *)proto_par)->type) {
1064                 case FPARAM_AVP:
1065                         if (!(avp = search_first_avp(((fparam_t *)proto_par)->v.avp.flags,
1066                                                         ((fparam_t *)proto_par)->v.avp.name, &val, 0))) {
1067                                 proto = PROTO_NONE;
1068                         } else {
1069                                 if (avp->flags & AVP_VAL_STR) {
1070                                         proto = str2proto(val.s.s, val.s.len);
1071                                 }
1072                                 else {
1073                                         proto = val.n;
1074                                 }
1075                         }
1076                         break;
1077
1078                 case FPARAM_INT:
1079                         proto = ((fparam_t *)proto_par)->v.i;
1080                         break;
1081                 case FPARAM_STRING:
1082                         proto = str2proto( ((fparam_t *)proto_par)->v.asciiz,
1083                                         strlen(((fparam_t *)proto_par)->v.asciiz));
1084                         break;
1085                 default:
1086                         LM_ERR("Invalid proto parameter value in t_protoaddr2proxy\n");
1087                         return 0;
1088         }
1089
1090
1091         switch(((fparam_t *)addr_par)->type) {
1092                 case FPARAM_AVP:
1093                         if (!(avp = search_first_avp(((fparam_t *)addr_par)->v.avp.flags,
1094                                                         ((fparam_t *)addr_par)->v.avp.name, &val, 0))) {
1095                                 s.len = 0;
1096                         } else {
1097                                 if ((avp->flags & AVP_VAL_STR) == 0) {
1098                                         LM_ERR("avp <%.*s> value is not string\n",
1099                                                         ((fparam_t *)addr_par)->v.avp.name.s.len,
1100                                                         ((fparam_t *)addr_par)->v.avp.name.s.s);
1101                                         return 0;
1102                                 }
1103                                 s = val.s;
1104                         }
1105                         break;
1106
1107                 case FPARAM_STRING:
1108                         s.s = ((fparam_t *) addr_par)->v.asciiz;
1109                         s.len = strlen(s.s);
1110                         break;
1111
1112                 default:
1113                         LM_ERR("Invalid addr parameter value in t_protoaddr2proxy\n");
1114                         return 0;
1115         }
1116
1117         port = 5060;
1118         if (s.len) {
1119                 c = memchr(s.s, ':', s.len);
1120                 if (c) {
1121                         port = str2s(c+1, s.len-(c-s.s+1), &err);
1122                         if (err!=0) {
1123                                 LM_ERR("bad port number <%.*s>\n", s.len, s.s);
1124                                 return 0;
1125                         }
1126                         s.len = c-s.s;
1127                 }
1128         }
1129         if (!s.len) {
1130                 LM_ERR("host name is empty\n");
1131                 return 0;
1132         }
1133         proxy=mk_proxy(&s, port, proto);
1134         if (proxy==0) {
1135                 LM_ERR("bad host name in URI <%.*s>\n", s.len, s.s );
1136                 return 0;
1137         }
1138         return proxy;
1139 }
1140
1141 inline static int _w_t_forward_nonack(struct sip_msg* msg, struct proxy_l* proxy,
1142                 int proto)
1143 {
1144         struct cell *t;
1145         if (t_check( msg , 0 )==-1) {
1146                 LM_ERR("can't forward when no transaction was set up\n");
1147                 return -1;
1148         }
1149         t=get_t();
1150         if ( t && t!=T_UNDEFINED ) {
1151                 if (msg->REQ_METHOD==METHOD_ACK) {
1152                         LM_WARN("you don't really want to fwd hop-by-hop ACK\n");
1153                         return -1;
1154                 }
1155                 return t_forward_nonack(t, msg, proxy, proto );
1156         } else {
1157                 LM_DBG("no transaction found\n");
1158                 return -1;
1159         }
1160 }
1161
1162
1163 inline static int w_t_forward_nonack( struct sip_msg* msg, char* proxy,
1164                 char* foo)
1165 {
1166         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_NONE);
1167 }
1168
1169
1170 inline static int w_t_forward_nonack_uri(struct sip_msg* msg, char *foo,
1171                 char *bar)
1172 {
1173         return _w_t_forward_nonack(msg, 0, PROTO_NONE);
1174 }
1175
1176
1177 inline static int w_t_forward_nonack_udp( struct sip_msg* msg, char* proxy,
1178                 char* foo)
1179 {
1180         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_UDP);
1181 }
1182
1183
1184 #ifdef USE_TCP
1185 inline static int w_t_forward_nonack_tcp( struct sip_msg* msg, char* proxy,
1186                 char* foo)
1187 {
1188         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TCP);
1189 }
1190 #endif
1191
1192
1193 #ifdef USE_TLS
1194 inline static int w_t_forward_nonack_tls( struct sip_msg* msg, char* proxy,
1195                 char* foo)
1196 {
1197         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_TLS);
1198 }
1199 #endif
1200
1201
1202 #ifdef USE_SCTP
1203 inline static int w_t_forward_nonack_sctp( struct sip_msg* msg, char* proxy,
1204                 char* foo)
1205 {
1206         return _w_t_forward_nonack(msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1207 }
1208 #endif
1209
1210
1211 inline static int w_t_forward_nonack_to( struct sip_msg  *p_msg ,
1212                 char *proto_par,
1213                 char *addr_par   )
1214 {
1215         struct proxy_l *proxy;
1216         int r = -1;
1217         proxy = t_protoaddr2proxy(proto_par, addr_par);
1218         if (proxy) {
1219                 r = _w_t_forward_nonack(p_msg, proxy, proxy->proto);
1220                 free_proxy(proxy);
1221                 pkg_free(proxy);
1222         }
1223         return r;
1224 }
1225
1226
1227 inline static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
1228 {
1229         struct cell *t;
1230         int code, ret = -1;
1231         str reason;
1232         char* r;
1233
1234         if (msg->REQ_METHOD==METHOD_ACK) {
1235                 LM_DBG("ACKs are not replied\n");
1236                 return -1;
1237         }
1238         if (t_check( msg , 0 )==-1) return -1;
1239         t=get_t();
1240         if (!t) {
1241                 LM_ERR("cannot send a t_reply to a message"
1242                                 " for which no T-state has been established\n");
1243                 return -1;
1244         }
1245
1246         if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
1247                 code = cfg_get(tm, tm_cfg, default_code);
1248         }
1249
1250         if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
1251                 r = cfg_get(tm, tm_cfg, default_reason);
1252         } else {
1253                 r = as_asciiz(&reason);
1254                 if (r == NULL) r = cfg_get(tm, tm_cfg, default_reason);
1255         }
1256
1257         /* if called from reply_route, make sure that unsafe version
1258          * is called; we are already in a mutex and another mutex in
1259          * the safe version would lead to a deadlock
1260          */
1261
1262         t->flags |= T_ADMIN_REPLY;
1263         if (is_route_type(FAILURE_ROUTE)) {
1264                 LM_DBG("t_reply_unsafe called from w_t_reply\n");
1265                 ret = t_reply_unsafe(t, msg, code, r);
1266         } else if (is_route_type(REQUEST_ROUTE)) {
1267                 ret = t_reply( t, msg, code, r);
1268         } else if (is_route_type(ONREPLY_ROUTE)) {
1269                 if (likely(t->uas.request)){
1270                         if (is_route_type(CORE_ONREPLY_ROUTE))
1271                                 ret=t_reply(t, t->uas.request, code, r);
1272                         else
1273                                 ret=t_reply_unsafe(t, t->uas.request, code, r);
1274                 }else
1275                         ret=-1;
1276                 /* t_check() above has the side effect of setting T and
1277                  * REFerencing T => we must unref and unset it.
1278                  * Note: this is needed only in the CORE_ONREPLY_ROUTE and not also in
1279                  * the TM_ONREPLY_ROUTE.
1280                  */
1281                 if (is_route_type(CORE_ONREPLY_ROUTE)) {
1282                         UNREF( t );
1283                         set_t(T_UNDEFINED, T_BR_UNDEFINED);
1284                 }
1285         } else {
1286                 LM_CRIT("w_t_reply entered in unsupported mode\n");
1287                 ret = -1;
1288         }
1289
1290         if (r && (r != cfg_get(tm, tm_cfg, default_reason))) pkg_free(r);
1291         return ret;
1292 }
1293
1294
1295 static int t_release(sip_msg_t* msg)
1296 {
1297         struct cell *t;
1298         int ret;
1299
1300         if(get_route_type()!=REQUEST_ROUTE)
1301         {
1302                 LM_INFO("invalid usage - not in request route\n");
1303                 return -1;
1304         }
1305
1306         if (t_check( msg  , 0  )==-1) return -1;
1307         t=get_t();
1308         if ( t && t!=T_UNDEFINED ) {
1309                 ret = t_release_transaction( t );
1310                 t_unref(msg);
1311                 return ret;
1312         }
1313         return 1;
1314 }
1315
1316 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2)
1317 {
1318         return t_release(msg);
1319 }
1320
1321 static int ki_t_retransmit_reply(struct sip_msg* p_msg)
1322 {
1323         struct cell *t;
1324
1325         if (t_check( p_msg  , 0 )==-1)
1326                 return 1;
1327         t=get_t();
1328         if (t) {
1329                 if (p_msg->REQ_METHOD==METHOD_ACK) {
1330                         LM_WARN("ACKs transmit_replies not replied\n");
1331                         return -1;
1332                 }
1333                 return t_retransmit_reply( t );
1334         } else
1335                 return -1;
1336 }
1337
1338 inline static int w_t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar)
1339 {
1340         return ki_t_retransmit_reply(p_msg);
1341 }
1342
1343 inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar )
1344 {
1345         /* t_newtran returns 0 on error (negative value means
1346          * 'transaction exists' */
1347         int ret;
1348         ret = t_newtran( p_msg );
1349         if (ret==E_SCRIPT) {
1350                 LM_NOTICE("transaction already in process %p\n", get_t() );
1351         }
1352         return ret;
1353 }
1354
1355
1356 inline static int w_t_on_failure( struct sip_msg* msg, char *go_to, char *foo)
1357 {
1358         t_on_failure( (unsigned int )(long) go_to );
1359         return 1;
1360 }
1361
1362
1363 inline static int w_t_on_branch_failure( struct sip_msg* msg, char *go_to, char *foo)
1364 {
1365         t_on_branch_failure( (unsigned int )(long) go_to );
1366         return 1;
1367 }
1368
1369
1370 inline static int w_t_on_branch( struct sip_msg* msg, char *go_to, char *foo)
1371 {
1372         t_on_branch( (unsigned int )(long) go_to );
1373         return 1;
1374 }
1375
1376
1377 inline static int w_t_on_reply( struct sip_msg* msg, char *go_to, char *foo )
1378 {
1379         t_on_reply( (unsigned int )(long) go_to );
1380         return 1;
1381 }
1382
1383
1384 static int t_is_set(sip_msg_t* msg, str *target)
1385 {
1386         int r;
1387         tm_cell_t *t = NULL;
1388
1389         r = 0;
1390         t = get_t();
1391         if (t==T_UNDEFINED) t = NULL;
1392
1393         switch(target->s[0]) {
1394                 case 'b':
1395                         if(t==NULL)
1396                                 r = get_on_branch();
1397                         else
1398                                 r = t->on_branch;
1399                         break;
1400                 case 'f':
1401                         if(t==NULL)
1402                                 r = get_on_failure();
1403                         else
1404                                 r = t->on_failure;
1405                         break;
1406                 case 'o':
1407                         if(t==NULL)
1408                                 r = get_on_reply();
1409                         else
1410                                 r = t->on_reply;
1411                         break;
1412         }
1413         if(r) return 1;
1414         return -1;
1415 }
1416
1417 static int w_t_is_set(struct sip_msg* msg, char *target, char *foo)
1418 {
1419         str s = STR_NULL;
1420
1421         s.s = target;
1422         return t_is_set(msg, &s);
1423 }
1424
1425 static int fixup_t_is_set(void** param, int param_no)
1426 {
1427         int len;
1428         if (param_no==1) {
1429                 len = strlen((char*)*param);
1430                 if((len==13 && strncmp((char*)*param, "failure_route", 13)==0)
1431                                 || (len==13 && strncmp((char*)*param, "onreply_route", 13)==0)
1432                                 || (len==12 && strncmp((char*)*param, "branch_route", 12)==0)) {
1433                         return 0;
1434                 }
1435
1436                 LM_ERR("invalid parameter value: %s\n", (char*)*param);
1437                 return 1;
1438         }
1439         return 0;
1440 }
1441
1442 inline static int _w_t_relay_to(struct sip_msg  *p_msg ,
1443                 struct proxy_l *proxy, int force_proto)
1444 {
1445         struct cell *t;
1446         int res;
1447
1448         if (is_route_type(FAILURE_ROUTE|BRANCH_FAILURE_ROUTE)) {
1449                 t=get_t();
1450                 if (!t || t==T_UNDEFINED) {
1451                         LM_CRIT("undefined T\n");
1452                         return -1;
1453                 }
1454                 res = t_forward_nonack(t, p_msg, proxy, force_proto);
1455                 if (res <= 0) {
1456                         if (res != E_CFG) {
1457                                 LM_ERR("t_forward_noack failed\n");
1458                                 /* let us save the error code, we might need it later
1459                                  * when the failure_route has finished (Miklos) */
1460                         }
1461                         tm_error=ser_error;
1462                         return -1;
1463                 }
1464                 return 1;
1465         }
1466         if (is_route_type(REQUEST_ROUTE))
1467                 return t_relay_to( p_msg, proxy, force_proto,
1468                                 0 /* no replication */ );
1469         LM_CRIT("unsupported route type: %d\n", get_route_type());
1470         return 0;
1471 }
1472
1473
1474 inline static int w_t_relay_to_udp( struct sip_msg  *p_msg ,
1475                 char *proxy,/* struct proxy_l * expected */
1476                 char *_foo       /* nothing expected */ )
1477 {
1478         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_UDP);
1479 }
1480
1481 /* forward to uri, but force udp as transport */
1482 inline static int w_t_relay_to_udp_uri( struct sip_msg  *p_msg ,
1483                 char *_foo, char *_bar   )
1484 {
1485         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_UDP);
1486 }
1487
1488
1489 #ifdef USE_TCP
1490 inline static int w_t_relay_to_tcp( struct sip_msg  *p_msg ,
1491                 char *proxy, /* struct proxy_l* */
1492                 char *_foo       /* nothing expected */ )
1493 {
1494         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TCP);
1495 }
1496
1497 /* forward to uri, but force tcp as transport */
1498 inline static int w_t_relay_to_tcp_uri( struct sip_msg  *p_msg ,
1499                 char *_foo, char *_bar   )
1500 {
1501         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TCP);
1502 }
1503 #endif
1504
1505
1506 #ifdef USE_TLS
1507 inline static int w_t_relay_to_tls( struct sip_msg  *p_msg ,
1508                 char *proxy, /* struct proxy_l* expected */
1509                 char *_foo       /* nothing expected */ )
1510 {
1511         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_TLS);
1512 }
1513
1514 /* forward to uri, but force tls as transport */
1515 inline static int w_t_relay_to_tls_uri( struct sip_msg  *p_msg ,
1516                 char *_foo, char *_bar   )
1517 {
1518         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_TLS);
1519 }
1520 #endif
1521
1522
1523 #ifdef USE_SCTP
1524 inline static int w_t_relay_to_sctp( struct sip_msg  *p_msg ,
1525                 char *proxy, /* struct proxy_l* */
1526                 char *_foo       /* nothing expected */ )
1527 {
1528         return _w_t_relay_to( p_msg, ( struct proxy_l *) proxy, PROTO_SCTP);
1529 }
1530
1531 /* forward to uri, but force tcp as transport */
1532 inline static int w_t_relay_to_sctp_uri( struct sip_msg  *p_msg ,
1533                 char *_foo, char *_bar   )
1534 {
1535         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_SCTP);
1536 }
1537 #endif
1538
1539
1540 inline static int w_t_relay_to_avp( struct sip_msg  *p_msg ,
1541                 char *proto_par,
1542                 char *addr_par   )
1543 {
1544         struct proxy_l *proxy;
1545         int r = -1;
1546
1547         proxy = t_protoaddr2proxy(proto_par, addr_par);
1548         if (proxy) {
1549                 r = _w_t_relay_to(p_msg, proxy, PROTO_NONE);
1550                 free_proxy(proxy);
1551                 pkg_free(proxy);
1552         }
1553         return r;
1554 }
1555
1556 int t_replicate_uri(struct sip_msg *msg, str *suri)
1557 {
1558         struct proxy_l *proxy = NULL;
1559         struct sip_uri turi;
1560         int r = -1;
1561
1562         if (suri != NULL && suri->s != NULL && suri->len > 0)
1563         {
1564                 memset(&turi, 0, sizeof(struct sip_uri));
1565                 if(parse_uri(suri->s, suri->len, &turi)!=0)
1566                 {
1567                         LM_ERR("bad replicate SIP address!\n");
1568                         return -1;
1569                 }
1570
1571                 proxy=mk_proxy(&turi.host, turi.port_no, turi.proto);
1572                 if (proxy==0) {
1573                         LM_ERR("cannot create proxy from URI <%.*s>\n",
1574                                         suri->len, suri->s );
1575                         return -1;
1576                 }
1577
1578                 r = t_replicate(msg, proxy, proxy->proto);
1579                 free_proxy(proxy);
1580                 pkg_free(proxy);
1581         } else {
1582                 r = t_replicate(msg, NULL, 0);
1583         }
1584         return r;
1585 }
1586
1587 inline static int w_t_replicate_uri(struct sip_msg  *msg ,
1588                 char *uri,       /* sip uri as string or variable */
1589                 char *_foo       /* nothing expected */ )
1590 {
1591         str suri;
1592
1593         if(uri==NULL)
1594                 return t_replicate_uri(msg, NULL);
1595
1596         if(fixup_get_svalue(msg, (gparam_p)uri, &suri)!=0)
1597         {
1598                 LM_ERR("invalid replicate uri parameter");
1599                 return -1;
1600         }
1601         return t_replicate_uri(msg, &suri);
1602 }
1603
1604 inline static int w_t_replicate( struct sip_msg  *p_msg ,
1605                 char *proxy, /* struct proxy_l *proxy expected */
1606                 char *_foo       /* nothing expected */ )
1607 {
1608         return t_replicate(p_msg, ( struct proxy_l *) proxy, p_msg->rcv.proto );
1609 }
1610
1611 inline static int w_t_replicate_udp( struct sip_msg  *p_msg ,
1612                 char *proxy, /* struct proxy_l *proxy expected */
1613                 char *_foo       /* nothing expected */ )
1614 {
1615         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_UDP );
1616 }
1617
1618
1619 #ifdef USE_TCP
1620 inline static int w_t_replicate_tcp( struct sip_msg  *p_msg ,
1621                 char *proxy, /* struct proxy_l *proxy expected */
1622                 char *_foo       /* nothing expected */ )
1623 {
1624         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TCP );
1625 }
1626 #endif
1627
1628
1629 #ifdef USE_TLS
1630 inline static int w_t_replicate_tls( struct sip_msg  *p_msg ,
1631                 char *proxy, /* struct proxy_l *proxy expected */
1632                 char *_foo       /* nothing expected */ )
1633 {
1634         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_TLS );
1635 }
1636 #endif
1637
1638
1639 #ifdef USE_SCTP
1640 inline static int w_t_replicate_sctp( struct sip_msg  *p_msg ,
1641                 char *proxy, /* struct proxy_l *proxy expected */
1642                 char *_foo       /* nothing expected */ )
1643 {
1644         return t_replicate(p_msg, ( struct proxy_l *) proxy, PROTO_SCTP );
1645 }
1646 #endif
1647
1648
1649 inline static int w_t_replicate_to( struct sip_msg  *p_msg ,
1650                 char *proto_par,
1651                 char *addr_par   )
1652 {
1653         struct proxy_l *proxy;
1654         int r = -1;
1655         proxy = t_protoaddr2proxy(proto_par, addr_par);
1656         if (proxy) {
1657                 r = t_replicate(p_msg, proxy, proxy->proto);
1658                 free_proxy(proxy);
1659                 pkg_free(proxy);
1660         }
1661         return r;
1662 }
1663
1664 inline static int w_t_relay( struct sip_msg  *p_msg ,
1665                 char *_foo, char *_bar)
1666 {
1667         return _w_t_relay_to(p_msg, (struct proxy_l *)0, PROTO_NONE);
1668 }
1669
1670
1671 /* like t_relay but use the specified destination and port and the same proto
1672  * as the received msg */
1673 static int w_t_relay2( struct sip_msg  *p_msg , char *proxy,
1674                 char *_foo)
1675 {
1676         return _w_t_relay_to(p_msg, (struct proxy_l*) proxy, p_msg->rcv.proto);
1677 }
1678
1679
1680 /* relays CANCEL at the beginning of the script */
1681 inline static int w_t_relay_cancel( struct sip_msg  *p_msg ,
1682                 char *_foo, char *_bar)
1683 {
1684         if (p_msg->REQ_METHOD!=METHOD_CANCEL)
1685                 return 1;
1686
1687         /* it makes no sense to use this function without reparse_invite=1 */
1688         if (!cfg_get(tm, tm_cfg, reparse_invite))
1689                 LM_WARN("probably used with wrong configuration,"
1690                                 " check the readme for details\n");
1691
1692         return t_relay_cancel(p_msg);
1693 }
1694
1695 /* set fr_inv_timeout & or fr_timeout; 0 means: use the default value */
1696 static int t_set_fr_all(struct sip_msg* msg, char* p1, char* p2)
1697 {
1698         int fr, fr_inv;
1699
1700         if (get_int_fparam(&fr_inv, msg, (fparam_t*)p1) < 0) return -1;
1701         if (p2) {
1702                 if (get_int_fparam(&fr, msg, (fparam_t*)p2) < 0) return -1;
1703         } else {
1704                 fr = 0;
1705         }
1706
1707         return t_set_fr(msg, fr_inv, fr);
1708 }
1709
1710 static int t_set_fr_inv(struct sip_msg* msg, char* fr_inv, char* foo)
1711 {
1712         return t_set_fr_all(msg, fr_inv, (char*)0);
1713 }
1714
1715 static int ki_t_set_fr(struct sip_msg* msg, int fr_inv, int fr)
1716 {
1717         return t_set_fr(msg, fr_inv, fr);
1718 }
1719
1720 static int ki_t_set_fr_inv(struct sip_msg* msg, int fr_inv)
1721 {
1722         return t_set_fr(msg, fr_inv, 0);
1723 }
1724
1725 /* reset fr_timer and fr_inv_timer to the default values */
1726 static int w_t_reset_fr(struct sip_msg* msg, char* foo, char* bar)
1727 {
1728         return t_reset_fr();
1729 }
1730
1731 static int ki_t_reset_fr(struct sip_msg* msg)
1732 {
1733         return t_reset_fr();
1734 }
1735
1736 static int ki_t_set_retr(sip_msg_t* msg, int t1, int t2)
1737 {
1738 #ifdef TM_DIFF_RT_TIMEOUT
1739         return t_set_retr(msg, t1, t2);
1740 #else
1741         LM_ERR("support for changing retransmission intervals on "
1742                         "the fly not compiled in (re-compile tm with"
1743                         " -DTM_DIFF_RT_TIMEOUT)\n");
1744         return -1;
1745 #endif
1746 }
1747
1748 /* set retr. intervals per transaction; 0 means: use the default value */
1749 static int w_t_set_retr(struct sip_msg* msg, char* p1, char* p2)
1750 {
1751         int t1, t2;
1752
1753         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1754         if (p2) {
1755                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1756         } else {
1757                 t2 = 0;
1758         }
1759         return ki_t_set_retr(msg, t1, t2);
1760 }
1761
1762 /* reset retr. t1 and t2 to the default values */
1763 int ki_t_reset_retr(sip_msg_t* msg)
1764 {
1765 #ifdef TM_DIFF_RT_TIMEOUT
1766         return t_reset_retr();
1767 #else
1768         LM_ERR("support for changing retransmission intervals on "
1769                         "the fly not compiled in (re-compile tm with"
1770                         " -DTM_DIFF_RT_TIMEOUT)\n");
1771         return -1;
1772 #endif
1773 }
1774
1775 int w_t_reset_retr(struct sip_msg* msg, char* foo, char* bar)
1776 {
1777         return ki_t_reset_retr(msg);
1778 }
1779
1780 /* set maximum transaction lifetime for inv & noninv */
1781 static int w_t_set_max_lifetime(struct sip_msg* msg, char* p1, char* p2)
1782 {
1783         int t1, t2;
1784
1785         if (get_int_fparam(&t1, msg, (fparam_t*)p1) < 0) return -1;
1786         if (p2) {
1787                 if (get_int_fparam(&t2, msg, (fparam_t*)p2) < 0) return -1;
1788         } else {
1789                 t2 = 0;
1790         }
1791         return t_set_max_lifetime(msg, t1, t2);
1792 }
1793
1794 static int ki_t_set_max_lifetime(sip_msg_t* msg, int t1, int t2)
1795 {
1796         return t_set_max_lifetime(msg, t1, t2);
1797 }
1798
1799 /* reset maximum invite/non-invite lifetime to the default value */
1800 int w_t_reset_max_lifetime(struct sip_msg* msg, char* foo, char* bar)
1801 {
1802         return t_reset_max_lifetime();
1803 }
1804
1805 int ki_t_reset_max_lifetime(sip_msg_t* msg)
1806 {
1807         return t_reset_max_lifetime();
1808 }
1809
1810
1811 /**
1812  * helper macro, builds a function for setting a cell flag from the script.
1813  * e.g. W_T_SET_FLAG_GEN_FUNC(t_set_foo, T_FOO) =>
1814  * static int t_set_foo(struct sip_msg* msg, char*, char* )
1815  * that will expect fparam as first param and will set or reset T_FOO
1816  * in the current or next to be created transaction. */
1817 #define T_SET_FLAG_GEN_FUNC(fname, T_FLAG_NAME) \
1818         static int fname(sip_msg_t* msg, int state) \
1819 { \
1820         struct cell* t; \
1821         unsigned int set_flags; \
1822         unsigned int reset_flags; \
1823         \
1824         t=get_t(); \
1825         /* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction; \
1826          * in REQUEST_ROUTE T will be set only if the transaction was already  \
1827          * created; if not -> use the static variables */ \
1828         if (!t || t==T_UNDEFINED ){ \
1829                 set_flags=get_msgid_val(user_cell_set_flags, msg->id, int); \
1830                 reset_flags=get_msgid_val(user_cell_reset_flags, msg->id, int); \
1831                 if (state){ \
1832                         /* set */ \
1833                         set_flags|= T_FLAG_NAME; \
1834                         reset_flags&=~T_FLAG_NAME; \
1835                 }else{ \
1836                         /* reset */ \
1837                         set_flags&=~T_FLAG_NAME; \
1838                         reset_flags|=T_FLAG_NAME; \
1839                 } \
1840                 set_msgid_val(user_cell_set_flags, msg->id, int, set_flags); \
1841                 set_msgid_val(user_cell_reset_flags, msg->id, int, reset_flags); \
1842         }else{ \
1843                 if (state) \
1844                 t->flags|=T_FLAG_NAME; \
1845                 else \
1846                 t->flags&=~T_FLAG_NAME; \
1847         } \
1848         return 1; \
1849 }
1850
1851 #define W_T_SET_FLAG_GEN_FUNC(fname, T_FLAG_NAME) \
1852         static int w_##fname(sip_msg_t* msg, char* p1, char* p2) \
1853 { \
1854         int state; \
1855         if (get_int_fparam(&state, msg, (fparam_t*)p1) < 0) return -1; \
1856         return fname(msg, state); \
1857 }
1858
1859 /* set automatically sending 100 replies on/off for the current or
1860  * next to be created transaction */
1861 T_SET_FLAG_GEN_FUNC(t_set_auto_inv_100, T_AUTO_INV_100)
1862
1863 W_T_SET_FLAG_GEN_FUNC(t_set_auto_inv_100, T_AUTO_INV_100)
1864
1865
1866 /* set 6xx handling for the current or next to be created transaction */
1867 T_SET_FLAG_GEN_FUNC(t_set_disable_6xx, T_DISABLE_6xx)
1868
1869 W_T_SET_FLAG_GEN_FUNC(t_set_disable_6xx, T_DISABLE_6xx)
1870
1871
1872 /* disable dns failover for the current transaction */
1873 T_SET_FLAG_GEN_FUNC(t_set_disable_failover, T_DISABLE_FAILOVER)
1874
1875 W_T_SET_FLAG_GEN_FUNC(t_set_disable_failover, T_DISABLE_FAILOVER)
1876
1877
1878 #ifdef CANCEL_REASON_SUPPORT
1879 /* disable/enable e2e cancel reason copy for the current transaction */
1880 T_SET_FLAG_GEN_FUNC(t_set_no_e2e_cancel_reason, T_NO_E2E_CANCEL_REASON)
1881
1882 W_T_SET_FLAG_GEN_FUNC(t_set_no_e2e_cancel_reason, T_NO_E2E_CANCEL_REASON)
1883 #endif /* CANCEL_REASON_SUPPORT */
1884
1885
1886 /* disable internal negative reply for the current transaction */
1887 T_SET_FLAG_GEN_FUNC(t_set_disable_internal_reply, T_DISABLE_INTERNAL_REPLY)
1888
1889 W_T_SET_FLAG_GEN_FUNC(t_set_disable_internal_reply, T_DISABLE_INTERNAL_REPLY)
1890
1891
1892 /* FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1893  * returns true if the choosed "failure" branch failed because of a timeout,
1894  * -1 otherwise */
1895 int t_branch_timeout(sip_msg_t* msg)
1896 {
1897         switch(get_route_type()) {
1898                 case FAILURE_ROUTE:
1899                 case BRANCH_FAILURE_ROUTE:
1900                         return (msg->msg_flags & FL_TIMEOUT)?1:-1;
1901                 default:
1902                         LM_ERR("unsupported route type %d\n", get_route_type());
1903         }
1904         return -1;
1905 }
1906
1907
1908 /* script function, FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1909  * returns true if the choosed "failure" branch failed because of a timeout,
1910  * -1 otherwise */
1911 int w_t_branch_timeout(sip_msg_t* msg, char* foo, char* bar)
1912 {
1913         return t_branch_timeout(msg);
1914 }
1915
1916 /* FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1917  * returns true if the choosed "failure" branch ever received a reply,
1918  * -1 otherwise */
1919 int t_branch_replied(sip_msg_t* msg)
1920 {
1921         switch(get_route_type()) {
1922                 case FAILURE_ROUTE:
1923                 case BRANCH_FAILURE_ROUTE:
1924                         return (msg->msg_flags & FL_REPLIED)?1:-1;
1925                 default:
1926                         LM_ERR("unsupported route type %d\n", get_route_type());
1927         }
1928         return -1;
1929 }
1930
1931
1932 /* script function, FAILURE_ROUTE and BRANCH_FAILURE_ROUTE only,
1933  * returns true if the choosed "failure" branch ever received a reply,
1934  * -1 otherwise */
1935 int w_t_branch_replied(sip_msg_t* msg, char* foo, char* bar)
1936 {
1937         return t_branch_replied(msg);
1938 }
1939
1940 /* script function, returns: 1 if the transaction was canceled, -1 if not */
1941 int t_is_canceled(struct sip_msg* msg)
1942 {
1943         struct cell *t;
1944         int ret;
1945
1946         if (t_check( msg , 0 )==-1) return -1;
1947         t=get_t();
1948         if ((t==0) || (t==T_UNDEFINED)){
1949                 LM_ERR("cannot check a message"
1950                                 " for which no T-state has been established\n");
1951                 ret=-1;
1952         }else{
1953                 ret=(t->flags & T_CANCELED)?1:-1;
1954         }
1955         return ret;
1956 }
1957
1958 static int w_t_is_canceled(sip_msg_t* msg, char* foo, char* bar)
1959 {
1960         return t_is_canceled(msg);
1961 }
1962
1963 /* returns: 1 if the transaction is currently suspended, -1 if not */
1964 int t_is_retr_async_reply(sip_msg_t* msg)
1965 {
1966         struct cell *t;
1967         int ret;
1968
1969         if (t_check( msg , 0 )==-1) return -1;
1970         t=get_t();
1971         if ((t==0) || (t==T_UNDEFINED)){
1972                 LM_ERR("cannot check a message"
1973                                 " for which no T-state has been established\n");
1974                 ret=-1;
1975         }else{
1976                 LM_DBG("TRANSACTION FLAGS IS %d\n", t->flags);
1977                 ret=(t->flags & T_ASYNC_SUSPENDED)?1:-1;
1978         }
1979         return ret;
1980 }
1981
1982 /* script function, returns: 1 if the transaction is currently suspended,
1983  * -1 if not */
1984 static int w_t_is_retr_async_reply(sip_msg_t* msg, char* foo, char* bar)
1985 {
1986         return t_is_retr_async_reply(msg);
1987 }
1988
1989 /* returns: 1 if the transaction lifetime interval has already elapsed, -1 if not */
1990 int t_is_expired(sip_msg_t* msg)
1991 {
1992         struct cell *t;
1993         int ret;
1994
1995         if (t_check( msg , 0 )==-1) return -1;
1996         t=get_t();
1997         if ((t==0) || (t==T_UNDEFINED)){
1998                 LM_ERR("cannot check a message"
1999                                 " for which no T-state has been established\n");
2000                 ret=-1;
2001         }else{
2002                 ret=(TICKS_GT(t->end_of_life, get_ticks_raw()))?-1:1;
2003         }
2004         return ret;
2005 }
2006
2007 /* script function, returns: 1 if the transaction lifetime interval
2008  * has already elapsed, -1 if not */
2009 int w_t_is_expired(sip_msg_t* msg, char* foo, char* bar)
2010 {
2011         return t_is_expired(msg);
2012 }
2013
2014 /* returns: 1 if any of the branches did timeout, -1 if not */
2015 int t_any_timeout(sip_msg_t* msg)
2016 {
2017         struct cell *t;
2018         int r;
2019
2020         if (t_check( msg , 0 )==-1) return -1;
2021         t=get_t();
2022         if ((t==0) || (t==T_UNDEFINED)){
2023                 LM_ERR("cannot check a message"
2024                                 " for which no T-state has been established\n");
2025                 return -1;
2026         }else{
2027                 for (r=0; r<t->nr_of_outgoings; r++){
2028                         if (t->uac[r].request.flags & F_RB_TIMEOUT)
2029                                 return 1;
2030                 }
2031         }
2032         return -1;
2033 }
2034
2035
2036 /* script function, returns: 1 if any of the branches did timeout, -1 if not */
2037 int w_t_any_timeout(sip_msg_t* msg, char* foo, char* bar)
2038 {
2039         return t_any_timeout(msg);
2040 }
2041
2042 /* returns: 1 if any of the branches received at leat one
2043  * reply, -1 if not */
2044 int t_any_replied(sip_msg_t* msg)
2045 {
2046         struct cell *t;
2047         int r;
2048
2049         if (t_check( msg , 0 )==-1) return -1;
2050         t=get_t();
2051         if ((t==0) || (t==T_UNDEFINED)){
2052                 LM_ERR("cannot check a message"
2053                                 " for which no T-state has been established\n");
2054                 return -1;
2055         }else{
2056                 for (r=0; r<t->nr_of_outgoings; r++){
2057                         if (t->uac[r].request.flags & F_RB_REPLIED)
2058                                 return 1;
2059                 }
2060         }
2061         return -1;
2062 }
2063
2064
2065 /* script function, returns: 1 if any of the branches received at leat one
2066  * reply, -1 if not */
2067 int w_t_any_replied(sip_msg_t* msg, char* foo, char* bar)
2068 {
2069         return t_any_replied(msg);
2070 }
2071
2072
2073 /* returns: 1 if any of the branches received the
2074  *  reply code "status" */
2075 int t_grep_status(sip_msg_t* msg, int code)
2076 {
2077         struct cell *t;
2078         int r;
2079
2080         if (t_check( msg , 0 )==-1) return -1;
2081         t=get_t();
2082         if ((t==0) || (t==T_UNDEFINED)){
2083                 LM_ERR("cannot check a message"
2084                                 " for which no T-state has been established\n");
2085                 return -1;
2086         }else{
2087                 for (r=0; r<t->nr_of_outgoings; r++){
2088                         if ((t->uac[r].last_received==code)  &&
2089                                         (t->uac[r].request.flags & F_RB_REPLIED))
2090                                 return 1;
2091                 }
2092         }
2093         return -1;
2094 }
2095
2096
2097 /* script function, returns: 1 if any of the branches received the
2098  *  reply code "status" */
2099 int w_t_grep_status(struct sip_msg* msg, char* status, char* bar)
2100 {
2101         int code;
2102
2103         if (get_int_fparam(&code, msg, (fparam_t*)status) < 0) return -1;
2104
2105         return t_grep_status(msg, code);
2106 }
2107
2108 /* drop all the existing replies in failure_route to make sure
2109  * that none of them is picked up again */
2110 static int t_drop_replies_helper(sip_msg_t* msg, char* mode)
2111 {
2112         if(mode==NULL)
2113                 t_drop_replies(1);
2114         else if(*mode=='n')
2115                 t_drop_replies(0);
2116         else if(*mode=='l')
2117                 t_drop_replies(2);
2118         else
2119                 t_drop_replies(1);
2120         return 1;
2121 }
2122
2123 static int w_t_drop_replies(struct sip_msg* msg, char* mode, char* bar)
2124 {
2125         return t_drop_replies_helper(msg, mode);
2126 }
2127
2128 static int ki_t_drop_replies(sip_msg_t* msg, str* mode)
2129 {
2130         return t_drop_replies_helper(msg, (mode)?mode->s:NULL);
2131 }
2132
2133 static int ki_t_drop_replies_all(sip_msg_t* msg)
2134 {
2135         return t_drop_replies_helper(msg, NULL);
2136 }
2137
2138 /* save the message lumps after t_newtran() but before t_relay() */
2139 static int ki_t_save_lumps(sip_msg_t* msg)
2140 {
2141         struct cell *t;
2142
2143         if (is_route_type(REQUEST_ROUTE)) {
2144                 t=get_t();
2145                 if (!t || t==T_UNDEFINED) {
2146                         LM_ERR("transaction has not been created yet\n");
2147                         return -1;
2148                 }
2149
2150                 if (save_msg_lumps(t->uas.request, msg)) {
2151                         LM_ERR("failed to save the message lumps\n");
2152                         return -1;
2153                 }
2154         } /* else nothing to do, the lumps have already been saved */
2155         return 1;
2156 }
2157
2158 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar)
2159 {
2160         return ki_t_save_lumps(msg);
2161 }
2162
2163 /* wrapper function needed after changes in w_t_reply */
2164 int w_t_reply_wrp(struct sip_msg *m, unsigned int code, char *txt)
2165 {
2166         fparam_t c;
2167         fparam_t r;
2168
2169         c.type = FPARAM_INT;
2170         c.orig = NULL; /* ? */
2171         c.v.i = code;
2172
2173         r.type = FPARAM_STRING;
2174         r.orig = NULL; /* ? */
2175         r.v.asciiz = txt;
2176
2177         return w_t_reply(m, (char *)&c, (char*)&r);
2178 }
2179
2180
2181
2182 /** script function, check if a msg is assoc. to a transaction.
2183  * @return -1 (not), 1 (reply, e2e ack or cancel for an existing transaction),
2184  *          0 (request retransmission, ack to negative reply or ack to local
2185  *           transaction)
2186  * Note: the e2e ack matching works only for local e2e acks or for
2187  *       transactions with E2EACK* callbacks installed (but even in this
2188  *       case matching E2EACKs on proxied transaction is not entirely
2189  *       reliable: if the ACK  is delayed the proxied transaction might
2190  *       be already deleted when it reaches the proxy (wait_timeout))
2191  */
2192 int t_check_trans(struct sip_msg* msg)
2193 {
2194         struct cell* t;
2195         int branch;
2196         int ret;
2197
2198         /* already processing a T */
2199         if(is_route_type(FAILURE_ROUTE)
2200                         || is_route_type(BRANCH_ROUTE)
2201                         || is_route_type(BRANCH_FAILURE_ROUTE)
2202                         || is_route_type(TM_ONREPLY_ROUTE)) {
2203                 return 1;
2204         }
2205
2206         if (msg->first_line.type==SIP_REPLY) {
2207                 branch = 0;
2208                 ret = (t_check_msg( msg , &branch)==1) ? 1 : -1;
2209                 tm_ctx_set_branch_index(branch);
2210                 return ret;
2211         } else if (msg->REQ_METHOD==METHOD_CANCEL) {
2212                 return w_t_lookup_cancel(msg, 0, 0);
2213         } else {
2214                 switch(t_check_msg(msg, 0)){
2215                         case -2: /* possible e2e ack */
2216                                 return 1;
2217                         case 1: /* found */
2218                                 t=get_t();
2219                                 if (msg->REQ_METHOD==METHOD_ACK){
2220                                         /* ack to neg. reply  or ack to local trans.
2221                                          * => process it and end the script */
2222                                         /* FIXME: there's no way to distinguish here
2223                                          * between acks to local trans. and neg. acks */
2224                                         if (unlikely(has_tran_tmcbs(t, TMCB_ACK_NEG_IN)))
2225                                                 run_trans_callbacks(TMCB_ACK_NEG_IN, t, msg,
2226                                                                 0, msg->REQ_METHOD);
2227                                         t_release_transaction(t);
2228                                 } else {
2229                                         /* is a retransmission */
2230                                         if (unlikely(has_tran_tmcbs(t, TMCB_REQ_RETR_IN)))
2231                                                 run_trans_callbacks(TMCB_REQ_RETR_IN, t, msg,
2232                                                                 0, msg->REQ_METHOD);
2233                                         t_retransmit_reply(t);
2234                                 }
2235                                 /* no need for UNREF(t); set_t(0) - the end-of-script
2236                                  * t_unref callback will take care of them */
2237                                 return 0; /* exit from the script */
2238                 }
2239                 /* not found or error */
2240         }
2241         return -1;
2242 }
2243
2244 static int w_t_check_trans(struct sip_msg* msg, char* foo, char* bar)
2245 {
2246         return t_check_trans(msg);
2247 }
2248
2249 static int hexatoi(str *s, unsigned int* result)
2250 {
2251         int i, xv, fact;
2252
2253         /* more than 32bit hexa? */
2254         if (s->len>8)
2255                 return -1;
2256
2257         *result = 0;
2258         fact = 1;
2259
2260         for(i=s->len-1; i>=0 ;i--)
2261         {
2262                 xv = hex2int(s->s[i]);
2263                 if(xv<0)
2264                         return -1;
2265
2266                 *result += (xv * fact);
2267                 fact *= 16;
2268         }
2269         return 0;
2270 }
2271
2272 static int fixup_t_relay_to(void** param, int param_no)
2273 {
2274
2275         int port;
2276         int proto;
2277         unsigned int flags;
2278         struct proxy_l *proxy;
2279         action_u_t *a;
2280         str s;
2281         str host;
2282
2283         s.s = (char*)*param;
2284         s.len = strlen(s.s);
2285         LM_DBG("fixing (%s, %d)\n", s.s, param_no);
2286         if (param_no==1){
2287                 a = fixup_get_param(param, param_no, 2);
2288                 if(a==NULL)
2289                 {
2290                         LM_CRIT("server error for parameter <%s>\n",s.s);
2291                         return E_UNSPEC;
2292                 }
2293                 if(a->u.string!=NULL) {
2294                         /* second parameter set, first should be proxy addr */
2295                         if (parse_phostport(s.s, &host.s, &host.len, &port, &proto)!=0){
2296                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2297                                 return E_UNSPEC;
2298                         }
2299
2300                         proxy = mk_proxy(&host, port, proto);
2301                         if (proxy==0) {
2302                                 LM_ERR("failed to build proxy structure for <%.*s>\n",
2303                                                 host.len, host.s );
2304                                 return E_UNSPEC;
2305                         }
2306                         *(param)=proxy;
2307                         return 0;
2308                 } else {
2309                         /* no second parameter, then is proxy addr or flags */
2310                         flags = 0;
2311                         if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2312                                 s.s += 2;
2313                                 s.len -= 2;
2314                                 if(hexatoi(&s, &flags)<0)
2315                                 {
2316                                         LM_CRIT("invalid hexa flags <%s>\n", s.s);
2317                                         return E_UNSPEC;
2318                                 }
2319                                 a->u.data = (void*)(unsigned long int)flags;
2320                                 *(param)= 0;
2321                                 return 0;
2322                         } else {
2323                                 if(str2int(&s, &flags)==0)
2324                                 {
2325                                         a->u.data = (void*)(unsigned long int)flags;
2326                                         *(param)= 0;
2327                                         return 0;
2328                                 } else {
2329                                         /* try proxy */
2330                                         if (parse_phostport(s.s, &host.s, &host.len,
2331                                                                 &port, &proto)!=0){
2332                                                 LM_CRIT("invalid proxy addr parameter <%s>\n",s.s);
2333                                                 return E_UNSPEC;
2334                                         }
2335
2336                                         proxy = mk_proxy(&host, port, proto);
2337                                         if (proxy==0) {
2338                                                 LM_ERR("failed to build proxy structure for <%.*s>\n",
2339                                                                 host.len, host.s );
2340                                                 return E_UNSPEC;
2341                                         }
2342                                         *(param)=proxy;
2343                                         return 0;
2344                                 }
2345                         }
2346                 }
2347         } else if (param_no==2) {
2348                 /* flags */
2349                 flags = 0;
2350                 if (s.len>2 && s.s[0]=='0' && s.s[1]=='x') {
2351                         s.s += 2;
2352                         s.len -= 2;
2353                         if(hexatoi(&s, &flags)<0)
2354                         {
2355                                 LM_CRIT("invalid hexa flags <%s>\n", s.s);
2356                                 return E_UNSPEC;
2357                         }
2358                         *(param) = (void*)(unsigned long int)flags;
2359                         return 0;
2360                 } else {
2361                         if(str2int(&s, &flags)==0)
2362                         {
2363                                 *(param) = (void*)(unsigned long int)flags;
2364                                 return 0;
2365                         } else {
2366                                 LM_CRIT("invalid flags <%s>\n", s.s);
2367                                 return E_UNSPEC;
2368                         }
2369                 }
2370         } else {
2371                 LM_ERR("invalid parameter number %d\n", param_no);
2372                 return E_BUG;
2373         }
2374 }
2375
2376
2377 inline static int w_t_relay_to(struct sip_msg *msg, char *proxy, char *flags)
2378 {
2379         unsigned int fl;
2380         struct proxy_l *px;
2381         fparam_t param;
2382
2383         fl = (unsigned int)(long)(void*)flags;
2384         px = (struct proxy_l*)proxy;
2385
2386         if(flags!=0)
2387         {
2388                 memset(&param, 0, sizeof(fparam_t));
2389                 param.type = FPARAM_INT;
2390                 /* no auto 100 trying */
2391                 if(fl&1) {
2392                         param.v.i = 0;
2393                         w_t_set_auto_inv_100(msg, (char*)(&param), 0);
2394                 }
2395                 /* no auto negative reply */
2396                 if(fl&2) {
2397                         param.v.i = 1;
2398                         w_t_set_disable_internal_reply(msg, (char*)(&param), 0);
2399                 }
2400                 /* no dns failover */
2401                 if(fl&4) {
2402                         param.v.i = 1;
2403                         w_t_set_disable_failover(msg, (char*)(&param), 0);
2404                 }
2405         }
2406         return _w_t_relay_to(msg, px, PROTO_NONE);
2407 }
2408
2409
2410 static int ki_t_use_uac_headers(sip_msg_t* msg)
2411 {
2412         tm_cell_t *t;
2413
2414         t=get_t();
2415         if (t!=NULL && t!=T_UNDEFINED) {
2416                 t->uas.request->msg_flags |= FL_USE_UAC_FROM|FL_USE_UAC_TO;
2417         }
2418         msg->msg_flags |= FL_USE_UAC_FROM|FL_USE_UAC_TO;
2419
2420         return 1;
2421 }
2422
2423 static int w_t_use_uac_headers(sip_msg_t* msg, char* foo, char* bar)
2424 {
2425         return ki_t_use_uac_headers(msg);
2426 }
2427
2428 static int w_t_uac_send(sip_msg_t* msg, char* pmethod, char* pruri,
2429                 char* pnexthop, char* psock, char *phdrs, char* pbody)
2430 {
2431         str method = STR_NULL;
2432         str ruri = STR_NULL;
2433         str nexthop = STR_NULL;
2434         str send_socket = STR_NULL;
2435         str headers = STR_NULL;
2436         str body = STR_NULL;
2437
2438         if(fixup_get_svalue(msg, (gparam_t*)pmethod, &method)!=0) {
2439                 LM_ERR("invalid method parameter");
2440                 return -1;
2441         }
2442         if(fixup_get_svalue(msg, (gparam_t*)pruri, &ruri)!=0) {
2443                 LM_ERR("invalid ruri parameter");
2444                 return -1;
2445         }
2446         if(fixup_get_svalue(msg, (gparam_t*)pnexthop, &nexthop)!=0) {
2447                 LM_ERR("invalid nexthop parameter");
2448                 return -1;
2449         }
2450         if(fixup_get_svalue(msg, (gparam_t*)psock, &send_socket)!=0) {
2451                 LM_ERR("invalid send socket parameter");
2452                 return -1;
2453         }
2454         if(fixup_get_svalue(msg, (gparam_t*)phdrs, &headers)!=0) {
2455                 LM_ERR("invalid headers parameter");
2456                 return -1;
2457         }
2458         if(fixup_get_svalue(msg, (gparam_t*)pbody, &body)!=0) {
2459                 LM_ERR("invalid body parameter");
2460                 return -1;
2461         }
2462
2463         if(t_uac_send(&method, &ruri, &nexthop, &send_socket, &headers, &body)<0) {
2464                 return -1;
2465         }
2466         return 1;
2467 }
2468
2469 static int ki_t_uac_send(sip_msg_t* msg, str* method, str* ruri,
2470                 str* nexthop, str* ssock, str *hdrs, str* body)
2471 {
2472         if(t_uac_send(method, ruri, nexthop, ssock, hdrs, body)<0) {
2473                 return -1;
2474         }
2475         return 1;
2476 }
2477
2478 /* rpc docs */
2479
2480 static const char* rpc_cancel_doc[2] = {
2481         "Cancel a pending transaction",
2482         0
2483 };
2484
2485 static const char* rpc_reply_doc[2] = {
2486         "Reply transaction",
2487         0
2488 };
2489
2490 static const char* rpc_reply_callid_doc[2] = {
2491         "Reply transaction by call-id",
2492         0
2493 };
2494
2495 static const char* tm_rpc_stats_doc[2] = {
2496         "Print transaction statistics.",
2497         0
2498 };
2499
2500 static const char* tm_rpc_hash_stats_doc[2] = {
2501         "Prints hash table statistics (can be used only if tm is compiled"
2502                 " with -DTM_HASH_STATS).",
2503         0
2504 };
2505
2506 static const char* rpc_t_uac_start_doc[2] = {
2507         "starts a tm uac using  a list of string parameters: method, ruri, dst_uri"
2508                 ", send_sock, headers (CRLF separated) and body (optional)",
2509         0
2510 };
2511
2512 static const char* rpc_t_uac_wait_doc[2] = {
2513         "starts a tm uac and waits for the final reply, using a list of string "
2514                 "parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
2515                 " and body (optional)",
2516         0
2517 };
2518
2519
2520 /* rpc exports */
2521 static rpc_export_t tm_rpc[] = {
2522         {"tm.cancel", rpc_cancel,   rpc_cancel_doc,   0},
2523         {"tm.reply",  rpc_reply,    rpc_reply_doc,    0},
2524         {"tm.reply_callid", rpc_reply_callid,   rpc_reply_callid_doc,   0},
2525         {"tm.stats",  tm_rpc_stats, tm_rpc_stats_doc, 0},
2526         {"tm.hash_stats",  tm_rpc_hash_stats, tm_rpc_hash_stats_doc, 0},
2527         {"tm.t_uac_start", rpc_t_uac_start, rpc_t_uac_start_doc, 0 },
2528         {"tm.t_uac_wait",  rpc_t_uac_wait,  rpc_t_uac_wait_doc, RET_ARRAY},
2529         {0, 0, 0, 0}
2530 };
2531
2532 /**
2533  *
2534  */
2535 static int ki_t_on_failure(sip_msg_t *msg, str *rname)
2536 {
2537         int ridx;
2538         sr_kemi_eng_t *keng;
2539
2540         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2541                 ridx = 0;
2542         } else {
2543                 keng = sr_kemi_eng_get();
2544                 if(keng==NULL) {
2545                         ridx = route_get(&failure_rt, rname->s);
2546                 } else {
2547                         ridx = sr_kemi_cbname_lookup_name(rname);
2548                 }
2549         }
2550         if(ridx<0) { ridx = 0; }
2551
2552         t_on_failure(ridx);
2553         return 1;
2554 }
2555
2556 /**
2557  *
2558  */
2559 static int ki_t_on_branch_failure(sip_msg_t *msg, str *rname)
2560 {
2561         int ridx;
2562         sr_kemi_eng_t *keng;
2563
2564         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2565                 ridx = 0;
2566         } else {
2567                 keng = sr_kemi_eng_get();
2568                 if(keng==NULL) {
2569                         ridx = route_get(&event_rt, rname->s);
2570                 } else {
2571                         ridx = sr_kemi_cbname_lookup_name(rname);
2572                 }
2573         }
2574         if(ridx<0) { ridx = 0; }
2575
2576         t_on_branch_failure(ridx);
2577         return 1;
2578 }
2579
2580
2581 /**
2582  *
2583  */
2584 static int ki_t_on_branch(sip_msg_t *msg, str *rname)
2585 {
2586         int ridx;
2587         sr_kemi_eng_t *keng;
2588
2589         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2590                 ridx = 0;
2591         } else {
2592                 keng = sr_kemi_eng_get();
2593                 if(keng==NULL) {
2594                         ridx = route_get(&branch_rt, rname->s);
2595                 } else {
2596                         ridx = sr_kemi_cbname_lookup_name(rname);
2597                 }
2598         }
2599         if(ridx<0) { ridx = 0; }
2600
2601         t_on_branch(ridx);
2602         return 1;
2603 }
2604
2605 /**
2606  *
2607  */
2608 static int ki_t_on_reply(sip_msg_t *msg, str *rname)
2609 {
2610         int ridx;
2611         sr_kemi_eng_t *keng;
2612
2613         if(rname==NULL || rname->s==NULL || rname->len<=0 || rname->s[0]=='\0') {
2614                 ridx = 0;
2615         } else {
2616                 keng = sr_kemi_eng_get();
2617                 if(keng==NULL) {
2618                         ridx = route_get(&onreply_rt, rname->s);
2619                 } else {
2620                         ridx = sr_kemi_cbname_lookup_name(rname);
2621                 }
2622         }
2623         if(ridx<0) { ridx = 0; }
2624
2625         t_on_reply(ridx);
2626         return 1;
2627 }
2628
2629 /**
2630  *
2631  */
2632 static int ki_t_relay(sip_msg_t *msg)
2633 {
2634         return _w_t_relay_to(msg, (struct proxy_l *)0, PROTO_NONE);
2635 }
2636
2637 /**
2638  *
2639  */
2640 static int ki_t_reply(sip_msg_t *msg, int code, str *reason)
2641 {
2642         return w_t_reply_wrp(msg, (unsigned int)code, reason->s);
2643 }
2644
2645 /**
2646  *
2647  */
2648 static sr_kemi_t tm_kemi_exports[] = {
2649         { str_init("tm"), str_init("t_relay"),
2650                 SR_KEMIP_INT, ki_t_relay,
2651                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2652                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2653         },
2654         { str_init("tm"), str_init("t_on_branch"),
2655                 SR_KEMIP_INT, ki_t_on_branch,
2656                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2657                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2658         },
2659         { str_init("tm"), str_init("t_on_failure"),
2660                 SR_KEMIP_INT, ki_t_on_failure,
2661                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2662                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2663         },
2664         { str_init("tm"), str_init("t_on_branch_failure"),
2665                 SR_KEMIP_INT, ki_t_on_branch_failure,
2666                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2667                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2668         },
2669         { str_init("tm"), str_init("t_on_reply"),
2670                 SR_KEMIP_INT, ki_t_on_reply,
2671                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2672                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2673         },
2674         { str_init("tm"), str_init("t_reply"),
2675                 SR_KEMIP_INT, ki_t_reply,
2676                 { SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
2677                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2678         },
2679         { str_init("tm"), str_init("t_check_trans"),
2680                 SR_KEMIP_INT, t_check_trans,
2681                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2682                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2683         },
2684         { str_init("tm"), str_init("t_is_canceled"),
2685                 SR_KEMIP_INT, t_is_canceled,
2686                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2687                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2688         },
2689         { str_init("tm"), str_init("t_newtran"),
2690                 SR_KEMIP_INT, t_newtran,
2691                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2692                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2693         },
2694         { str_init("tm"), str_init("t_release"),
2695                 SR_KEMIP_INT, t_release,
2696                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2697                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2698         },
2699         { str_init("tm"), str_init("t_replicate"),
2700                 SR_KEMIP_INT, t_replicate_uri,
2701                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2702                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2703         },
2704         { str_init("tm"), str_init("t_is_set"),
2705                 SR_KEMIP_INT, t_is_set,
2706                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2707                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2708         },
2709         { str_init("tm"), str_init("t_lookup_request"),
2710                 SR_KEMIP_INT, ki_t_lookup_request,
2711                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2712                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2713         },
2714         { str_init("tm"), str_init("t_lookup_cancel"),
2715                 SR_KEMIP_INT, ki_t_lookup_cancel,
2716                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2717                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2718         },
2719         { str_init("tm"), str_init("t_lookup_cancel_flags"),
2720                 SR_KEMIP_INT, ki_t_lookup_cancel_flags,
2721                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2722                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2723         },
2724         { str_init("tm"), str_init("t_retransmit_reply"),
2725                 SR_KEMIP_INT, ki_t_retransmit_reply,
2726                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2727                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2728         },
2729         { str_init("tm"), str_init("t_set_fr_inv"),
2730                 SR_KEMIP_INT, ki_t_set_fr_inv,
2731                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2732                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2733         },
2734         { str_init("tm"), str_init("t_set_fr"),
2735                 SR_KEMIP_INT, ki_t_set_fr,
2736                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2737                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2738         },
2739         { str_init("tm"), str_init("t_reset_fr"),
2740                 SR_KEMIP_INT, ki_t_reset_fr,
2741                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2742                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2743         },
2744         { str_init("tm"), str_init("t_set_max_lifetime"),
2745                 SR_KEMIP_INT, ki_t_set_max_lifetime,
2746                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2747                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2748         },
2749         { str_init("tm"), str_init("t_reset_max_lifetime"),
2750                 SR_KEMIP_INT, ki_t_reset_max_lifetime,
2751                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2752                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2753         },
2754         { str_init("tm"), str_init("t_set_retr"),
2755                 SR_KEMIP_INT, ki_t_set_retr,
2756                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
2757                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2758         },
2759         { str_init("tm"), str_init("t_reset_retr"),
2760                 SR_KEMIP_INT, ki_t_reset_retr,
2761                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2762                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2763         },
2764         { str_init("tm"), str_init("t_uac_send"),
2765                 SR_KEMIP_INT, ki_t_uac_send,
2766                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
2767                         SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR }
2768         },
2769         { str_init("tm"), str_init("t_load_contacts"),
2770                 SR_KEMIP_INT, ki_t_load_contacts,
2771                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2772                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2773         },
2774         { str_init("tm"), str_init("t_next_contacts"),
2775                 SR_KEMIP_INT, ki_t_next_contacts,
2776                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2777                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2778         },
2779         { str_init("tm"), str_init("t_next_contact_flow"),
2780                 SR_KEMIP_INT, ki_t_next_contact_flow,
2781                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2782                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2783         },
2784         { str_init("tm"), str_init("t_drop_replies_all"),
2785                 SR_KEMIP_INT, ki_t_drop_replies_all,
2786                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2787                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2788         },
2789         { str_init("tm"), str_init("t_drop_replies"),
2790                 SR_KEMIP_INT, ki_t_drop_replies,
2791                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2792                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2793         },
2794         { str_init("tm"), str_init("t_use_uac_headers"),
2795                 SR_KEMIP_INT, ki_t_use_uac_headers,
2796                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2797                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2798         },
2799         { str_init("tm"), str_init("t_save_lumps"),
2800                 SR_KEMIP_INT, ki_t_save_lumps,
2801                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2802                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2803         },
2804         { str_init("tm"), str_init("t_is_expired"),
2805                 SR_KEMIP_INT, t_is_expired,
2806                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2807                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2808         },
2809         { str_init("tm"), str_init("t_grep_status"),
2810                 SR_KEMIP_INT, t_grep_status,
2811                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2812                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2813         },
2814         { str_init("tm"), str_init("t_is_retr_async_reply"),
2815                 SR_KEMIP_INT, t_is_retr_async_reply,
2816                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2817                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2818         },
2819         { str_init("tm"), str_init("t_any_replied"),
2820                 SR_KEMIP_INT, t_any_replied,
2821                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2822                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2823         },
2824         { str_init("tm"), str_init("t_any_timeout"),
2825                 SR_KEMIP_INT, t_any_timeout,
2826                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2827                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2828         },
2829         { str_init("tm"), str_init("t_branch_replied"),
2830                 SR_KEMIP_INT, t_branch_replied,
2831                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2832                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2833         },
2834         { str_init("tm"), str_init("t_branch_timeout"),
2835                 SR_KEMIP_INT, t_branch_timeout,
2836                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2837                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2838         },
2839         { str_init("tm"), str_init("t_set_auto_inv_100"),
2840                 SR_KEMIP_INT, t_set_auto_inv_100,
2841                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2842                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2843         },
2844         { str_init("tm"), str_init("t_set_disable_6xx"),
2845                 SR_KEMIP_INT, t_set_disable_6xx,
2846                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2847                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2848         },
2849         { str_init("tm"), str_init("t_set_disable_failover"),
2850                 SR_KEMIP_INT, t_set_disable_failover,
2851                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2852                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2853         },
2854         { str_init("tm"), str_init("t_set_no_e2e_cancel_reason"),
2855                 SR_KEMIP_INT, t_set_no_e2e_cancel_reason,
2856                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2857                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2858         },
2859         { str_init("tm"), str_init("t_set_disable_internal_reply"),
2860                 SR_KEMIP_INT, t_set_disable_internal_reply,
2861                 { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
2862                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2863         },
2864
2865
2866         { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
2867 };
2868
2869
2870 int mod_register(char *path, int *dlflags, void *p1, void *p2)
2871 {
2872         sr_kemi_modules_add(tm_kemi_exports);
2873         return 0;
2874 }