1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
5 <section id="tm.functions" xmlns:xi="http://www.w3.org/2001/XInclude">
9 <revnumber>$Revision$</revnumber>
15 <title>Functions</title>
17 <section id="t_relay_to_udp">
19 <function>t_relay_to_udp(ip, port)</function>,
20 <function>t_relay_to_tcp(ip, port)</function>
23 Relay a message statefully to a fixed destination. This along with
24 <function>t_relay</function> is the function most users want to
25 use--all other are mostly for programming. Programmers interested
26 in writing <acronym>TM</acronym> logic should review how t_relay is
27 implemented in tm.c and how <acronym>TM</acronym> callbacks work.
29 <para>Meaning of the parameters is as follows:</para>
32 <para><emphasis>ip</emphasis> - IP address where the message should be sent.
36 <para><emphasis>port</emphasis> - Port number.
41 <title><function>t_relay_to_udp</function> usage</title>
44 t_relay_to_udp("1.2.3.4", "5060");
50 <section id="t_relay">
52 <function>t_relay()</function>
55 Relay a message statefully to destination indicated in current URI. (If the
56 original URI was rewritten by UsrLoc, RR, strip/prefix, etc., the new URI will
57 be taken). Returns a negative value on failure--you may still want to send a
58 negative reply upstream statelessly not to leave upstream UAC in lurch.
61 <title><function>t_relay</function> usage</title>
74 <section id="t_on_failure">
76 <function>t_on_failure(failure_route)</function>
79 Sets failure routing block, to which control is passed after a
80 transaction completed with a negative result but before sending a
81 final reply. In the referred block, you can either start a new
82 branch (good for services such as forward_on_no_reply) or send a
83 final reply on your own (good for example for message silo, which
84 received a negative reply from upstream and wants to tell upstream
85 "202 I will take care of it"). Note that the set of
86 commands which are usable within failure_routes is strictly limited to
87 rewriting URI, initiating new branches, logging, and sending
88 stateful replies (<function>t_reply</function>). Any other commands
89 may result in unpredictable behavior and possible server
90 failure. Note that whenever failure_route is entered, uri is reset to
91 value which it had on relaying. If it temporarily changed during a
92 reply_route processing, subsequent reply_route will ignore the
93 changed value and use again the original one.
95 <para>Meaning of the parameters is as follows:</para>
98 <para><emphasis>failure_route</emphasis> - Failure route block to be called.
103 <title><function>t_on_failure</function> usage</title>
113 setuser("voicemail");
120 See <filename>test/onr.cfg</filename> for a more complex example of
121 combination of serial with parallel forking.
125 <section id="t_on_reply">
127 <function>t_on_reply(onreply_route)</function>
130 Sets the reply routing block, to which control is passed when a
131 reply for the current transaction is received.
132 Note that the set of commands which are usable within onreply_routes is
135 <para>Meaning of the parameters is as follows:</para>
138 <para><emphasis>onreply_route</emphasis> - Onreply route block to be
144 <title><function>t_on_reply</function> usage</title>
147 loadmodule "/usr/local/lib/ser/modules/nathelper.so"
156 if (status=~ "(183)|2[0-9][0-9]"){
158 search_append('^(Contact|m)[ \t]*:.*sip:[^>[:cntrl:]]*', ';nat=yes');
160 if (nat_uac_test("1")){
168 <section id="t_on_branch">
170 <function>t_on_branch(branch_route)</function>
173 Sets the branch routing block, to which control is passed after
174 forking (when a new branch is created). For now branch routes
175 are intended only for last minute changes of the SIP messages
176 (like adding new headers).
177 Note that the set of commands which are usable within branch_routes is
178 very limited. It is not possible to drop a message or generate a reply.
180 <para>Meaning of the parameters is as follows:</para>
183 <para><emphasis>branch_route</emphasis> - branch route block to be
189 <title><function>t_on_branch</function> usage</title>
198 if (uri=~"sip:[0-9]+"){
199 append_hf("P-Warn: numeric uri\r\n");
206 <section id="append_branch">
208 <function>append_branch()</function>
211 Similarly to <function>t_fork_to</function>, it extends destination
212 set by a new entry. The difference is that current URI is taken
216 <title><function>append_branch</function> usage</title>
229 <section id="t_newtran">
231 <function>t_newtran()</function>
234 Creates a new transaction, returns a negative value on error. This
235 is the only way a script can add a new transaction in an atomic
236 way. Typically, it is used to deploy a UAS.
239 <title><function>t_newtran</function> usage</title>
244 t_reply("999","hello");
245 } else sl_reply_error();
250 See test/uas.cfg for more examples.
254 <section id="t_reply">
256 <function>t_reply(code, reason_phrase)</function>
259 Sends a stateful reply after a transaction has been
260 established. See <function>t_newtran</function> for usage.
262 <para>Meaning of the parameters is as follows:</para>
265 <para><emphasis>code</emphasis> - Reply code number.
269 <para><emphasis>reason_phrase</emphasis> - Reason string.
274 <title><function>t_reply</function> usage</title>
277 t_reply("404", "Not found");
283 <section id="t_lookup_request">
285 <function>t_lookup_request()</function>
288 Checks if a transaction exists. Returns a positive value if so,
289 negative otherwise. Most likely you will not want to use it, as a
290 typical application of a looku-up is to introduce a new transaction
291 if none was found. However this is safely (atomically) done using
292 <function>t_newtran</function>.
295 <title><function>t_lookup_request</function> usage</title>
298 if (t_lookup_request()) {
306 <section id="t_retransmit_reply">
308 <function>t_retransmit_reply()</function>
311 Retransmits a reply sent previously by UAS transaction.
314 <title><function>t_retransmit_reply</function> usage</title>
317 t_retransmit_reply();
323 <section id="t_release">
325 <function>t_release()</function>
328 Remove transaction from memory (it will be first put on a wait
329 timer to absorb delayed messages).
332 <title><function>t_release</function> usage</title>
341 <section id="t_forward_nonack">
343 <function>t_forward_nonack(ip, port)</function>
346 mainly for internal usage--forward a non-ACK request statefully.
348 <para>Meaning of the parameters is as follows:</para>
351 <para><emphasis>ip</emphasis> - IP address where the message should be sent.
355 <para><emphasis>port</emphasis> - Port number.
360 <title><function>t_forward_nonack</function> usage</title>
363 t_forward_nonack("1.2.3.4", "5060");
369 <section id="t_set_fr">
371 <function>t_set_fr(fr_inv_timeout [, fr_timeout])</function>
374 Sets the fr_inv_timeout and optionally fr_timeout for the current
375 transaction or for transactions created during the same script
376 invocation, after calling this function.
377 If the transaction is already created (e.g called after
378 <function>t_relay()</function> or in an onreply_route) all the
379 branches will have their final response timeout updated on-the-fly.
380 If one of the parameters is 0, it's value won't be changed.
382 <para>Meaning of the parameters is as follows:</para>
385 <para><emphasis>fr_inv_timeout</emphasis> - new final response timeout
386 (in milliseconds) for INVITEs. See also
387 <varname>fr_inv_timer</varname>.
389 <para><emphasis>fr_timeout</emphasis> - new final response timeout
390 (in milliseconds) for non-INVITE transaction, or INVITEs which
391 haven't received yet a provisional response. See also
392 <varname>fr_timer</varname>.
397 <title><function>t_set_fr</function> usage</title>
401 t_set_fr(10000); # set only fr invite timeout to 10s
407 # if we are calling the pstn, extend the invite timeout to 50s
408 # for all the branches, and set the no-reply-received timeout to 2s
409 if (uri=~"sip:[0-9]+"){
410 t_set_fr(50000, 2000);
417 <section id="t_set_retr">
419 <function>t_set_retr(retr_t1_interval, retr_t2_interval)</function>
422 Sets the retr_t1_interval and retr_t2_interval for the current
423 transaction or for transactions created during the same script
424 invocation, after calling this function.
425 If one of the parameters is 0, it's value won't be changed.
426 If the transaction is already created (e.g called after
427 <function>t_relay()</function> or in an onreply_route) all the
428 existing branches will have their retransmissions intervals updated
430 if the retransmission interval for the branch has not yet reached T2
431 the interval will be reset to retr_t1_interval, else to
432 retr_t2_interval. Note that the change will happen after the current
433 interval expires (after the next retransmission, the next-next
434 retransmission will take place at retr_t1_interval or
436 All new branches of the same transaction will start with the new
438 This function will work even if it's called in the script before
439 a transaction creating function (e.g.: t_set_retr(500, 4000);
440 t_relay()). All new transaction created after this function call,
441 during the same script invocation will use the new values.
442 Note that this function will work only if tm is compile with
443 -DTM_DIFF_RT_TIMEOUT (which increases every transaction size with
446 <para>Meaning of the parameters is as follows:</para>
449 <para><emphasis>retr_t1_interval</emphasis> - new T1 retransmission
450 interval (in milliseconds). See also
451 <varname>retr_t1_timeout</varname>.
453 <para><emphasis>retr_t2_interval</emphasis> - new T2 (or maximum)
454 retransmission interval (in milliseconds). See also
455 <varname>retr_t2_timeout</varname>.
460 <title><function>t_set_retr</function> usage</title>
464 t_set_retr(250, 0); # set only T1 to 250 ms
470 # if we are calling the a remote pstn, extend T1 and decrease T2
471 # for all the branches
472 if (uri=~"sip:[0-9]+"){
473 t_set_retr(500, 2000);
480 <section id="t_branch_timeout">
482 <function>t_branch_timeout()</function>
485 Returns true if the failure route is executed for a branch that did
486 timeout. It can be used only from the
487 <emphasis>failure_route</emphasis>.
490 <title><function>t_branch_timeout</function> usage</title>
494 if (t_branch_timeout()){
503 <section id="t_branch_replied">
505 <function>t_branch_replied()</function>
508 Returns true if the failure route is executed for a branch that did
509 receive at least one reply in the past (the "current" reply is not
510 taken into account). It can be used only from the
511 <emphasis>failure_route</emphasis>.
514 <title><function>t_branch_replied</function> usage</title>
518 if (t_branch_timeout()){
519 if (t_branch_replied())
520 log("timeout after receiving a reply (no answer?)\n");
522 log("timeout, remote side seems to be down\n");
530 <section id="t_any_timeout">
532 <function>t_any_timeout()</function>
535 Returns true if at least one of the current transactions branches
539 <title><function>t_any_timeout</function> usage</title>
543 if (!t_branch_timeout()){
544 if (t_any_timeout()){
545 log("one branch did timeout\n");
546 sl_send_reply("408", "Timeout");
554 <section id="t_any_replied">
556 <function>t_any_replied()</function>
559 Returns true if at least one of the current transactions branches
560 did receive some reply in the past. If called from a failure or
561 onreply route, the "current" reply is not taken into account.
564 <title><function>t_any_replied</function> usage</title>
568 if (!t_any_replied()){
569 log("first reply received\n");
577 <section id="t_grep_status">
579 <function>t_grep_status("code")</function>
582 Returns true if "code" is the the final reply received (or locally
583 generated) in at least one of the current transactions branches.
586 <title><function>t_grep_status</function> usage</title>
590 if (t_grep_status("486")){
591 /* force a 486 reply, even if this is not the winning branch */
592 t_reply("486", "Busy");
599 <section id="t_is_canceled">
601 <function>t_is_canceled()</function>
604 Returns true if the current transaction was canceled.
607 <title><function>t_is_canceled</function> usage</title>
611 if (t_is_canceled()){
612 log("transaction canceled\n");
620 <section id="t_relay_cancel">
622 <function>t_relay_cancel()</function>
625 Forwards the CANCEL if the corresponding INVITE transaction
626 exists. The function is supposed to be used at the very
627 beginning of the script, because the CANCELs can be caught
628 and the rest of the script can be bypassed this way. Do not disable
629 <varname>reparse_invite</varname> module parameter, and call
630 <varname>t_relay_cancel()</varname> right after the sanity tests.
633 Return value is 0 (drop) if the corresponding INVITE was found
634 and the CANCELs were successfully sent to the pending branches,
635 true if the INVITE was not found, and false in case of any error.
638 <title><function>t_relay_cancel</function> usage</title>
641 if (method == CANCEL) {
642 if (!t_relay_cancel()) { # implicit drop if relaying was successful,
645 # corresponding INVITE transaction found but error occurred
646 sl_reply("500", "Internal Server Error");
649 # bad luck, corresponding INVITE transaction is missing,
650 # do the same as for INVITEs
656 <section id="t_drop_replies">
658 <function>t_drop_replies()</function>
661 Drops all the previously received replies in failure_route
662 block to make sure that none of them is picked up again.
663 Works only if a new branch is added to the transaction,
664 or it is explicitly replied in the script!
667 <title><function>t_drop_replies()</function> usage</title>
671 if (t_check_status("5[0-9][0-9]")){
672 # I do not like the 5xx responses,
673 # so I give another chance to "foobar.com",
674 # and I drop all the replies to make sure that
675 # they are not forwarded to the caller.
678 rewritehostport("foobar.com");
687 <section id="t_save_lumps">
689 <function>t_save_lumps()</function>
692 Forces the modifications of the processed SIP message
693 to be saved in shared memory before t_relay() is called.
694 The new branches which are created in failure_route will
695 contain the same modifications, and any other modification
696 after t_save_lumps() will be lost.
699 Note that t_relay() automatically saves the modifications
700 when it is called the first time, there is no need for
701 t_save_lumps() unless message changes between t_save_lumps()
702 and t_relay() must not be propagated to failure_route.
705 The transaction must be created by t_newtran() before
706 calling t_save_lumps().
709 <title><function>t_save_lumps()</function> usage</title>
714 append_hf("hf1: my first header\r\n");
717 append_hf("hf2: my second header\r\n");
725 append_hf("hf3: my third header\r\n");
727 # This branch contains hf1 and hf3, but does
728 # not contain hf2 header.
729 # hf2 would be also present here without