tm: added t_is_set("target") function
authorDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 29 Jun 2012 10:25:24 +0000 (12:25 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 29 Jun 2012 10:25:24 +0000 (12:25 +0200)
- returns true if the attribute specified by the target parameter is set
  for current transaction (e.g., failure_route, branch_route,
  onreply_route)
- on_negative was replaced with on_failure internally to be consistent
  with config file naming

modules/tm/README
modules/tm/doc/functions.xml
modules/tm/h_table.h
modules/tm/t_fwd.c
modules/tm/t_lookup.c
modules/tm/t_reply.c
modules/tm/t_reply.h
modules/tm/timer.c
modules/tm/tm.c
modules/tm/tm_load.c

index 9a515f1..dee95e1 100644 (file)
@@ -106,6 +106,7 @@ Juha Heinanen
         1.5.43. t_replicate(params)
         1.5.44. t_relay_to(proxy, flags)
         1.5.45. t_set_no_e2e_cancel_reason(0|1)
+        1.5.46. t_is_set(target)
 
    1.6. TM Module API
 
@@ -2146,6 +2147,25 @@ opying
 ...
 }
 
+1.5.46. t_is_set(target)
+
+   Return true if the attribute specified by 'target' is set for
+   transaction.
+
+   The target parameter can be:
+     * branch_route - the function returns true if a branch route is set
+       to be executed.
+     * failure_route - the function returns true if a failure route is set
+       to be executed.
+     * onreply_route - the function returns true if an onreply route is
+       set to be executed.
+
+   Example 79. t_replicate usage
+...
+if(!t_is_set("failure_route"))
+    LM_DBG("no failure route will be executed for current transaction\n");
+...
+
 1.6. TM Module API
 
    There are applications which would like to generate SIP transactions
index 625e699..09626f7 100644 (file)
@@ -1535,5 +1535,40 @@ route {
        </example>
        </section>
 
+       <section id="t_is_set">
+       <title>
+           <function>t_is_set(target)</function>
+       </title>
+       <para>
+               Return true if the attribute specified by 'target' is set for transaction.
+       </para>
+       <para>The target parameter can be:</para>
+       <itemizedlist>
+           <listitem>
+               <para><emphasis>branch_route</emphasis> - the function returns true if a
+                       branch route is set to be executed.
+               </para>
+           </listitem>
+           <listitem>
+               <para><emphasis>failure_route</emphasis> - the function returns true if a
+                       failure route is set to be executed.
+               </para>
+           </listitem>
+           <listitem>
+               <para><emphasis>onreply_route</emphasis> - the function returns true if an
+                       onreply route is set to be executed.
+               </para>
+           </listitem>
+       </itemizedlist>
+       <example>
+           <title><function>t_replicate</function> usage</title>
+           <programlisting>
+...
+if(!t_is_set("failure_route"))
+    LM_DBG("no failure route will be executed for current transaction\n");
+...
+           </programlisting>
+       </example>
+    </section>
 
 </section>
index 0d7f163..8294001 100644 (file)
@@ -432,7 +432,7 @@ typedef struct cell
        short relayed_reply_branch;
 
        /* the route to take if no final positive reply arrived */
-       unsigned short on_negative;
+       unsigned short on_failure;
        /* the onreply_route to be processed if registered to do so */
        unsigned short on_reply;
         /* The route to take for each downstream branch separately */
index ad431b3..38312a2 100644 (file)
@@ -138,7 +138,7 @@ void t_on_branch( unsigned int go_to )
        if (!t || t==T_UNDEFINED ) {
                goto_on_branch=go_to;
        } else {
-               get_t()->on_branch = go_to;
+               t->on_branch = go_to;
        }
 }
 
index 397d3a5..26b8171 100644 (file)
@@ -67,7 +67,7 @@
  * 2003-03-30  set_kr for requests only (jiri)
  * 2003-04-04  bug_fix: RESPONSE_IN callback not called for local
  *             UAC transactions (jiri)
- * 2003-04-07  new transactions inherit on_negative and on_relpy from script
+ * 2003-04-07  new transactions inherit on_failure and on_relpy from script
  *             variables on instantiation (jiri)
  * 2003-04-30  t_newtran clean up (jiri)
  * 2003-08-21  request lookups fixed to skip UAC transactions, 
@@ -1269,7 +1269,7 @@ static inline void init_new_t(struct cell *new_cell, struct sip_msg *p_msg)
                if (likely(lifetime==0))
                        lifetime=cfg_get(tm, tm_cfg, tm_max_noninv_lifetime);
        }
-       new_cell->on_negative=get_on_negative();
+       new_cell->on_failure=get_on_failure();
        new_cell->on_reply=get_on_reply();
        new_cell->end_of_life=get_ticks_raw()+lifetime;;
        new_cell->fr_timeout=(ticks_t)get_msgid_val(user_fr_timeout,
index 0e8de0b..32110d3 100644 (file)
@@ -170,8 +170,8 @@ char tm_tags[TOTAG_VALUE_LEN];
 static str  tm_tag = {tm_tags,TOTAG_VALUE_LEN};
 char *tm_tag_suffix;
 
-/* where to go if there is no positive reply */
-static int goto_on_negative=0;
+/* where to go if there is no positive reply (>=300) */
+static int goto_on_failure=0;
 /* where to go on receipt of reply */
 static int goto_on_reply=0;
 /* where to go on receipt of reply without transaction context */
@@ -245,7 +245,7 @@ int t_get_picked_branch(void)
 */
 
 
-void t_on_negative( unsigned int go_to )
+void t_on_failure( unsigned int go_to )
 {
        struct cell *t = get_t();
 
@@ -253,9 +253,9 @@ void t_on_negative( unsigned int go_to )
         * in REQUEST_ROUTE T will be set only if the transaction was already
         * created; if not -> use the static variable */
        if (!t || t==T_UNDEFINED )
-               goto_on_negative=go_to;
+               goto_on_failure=go_to;
        else
-               get_t()->on_negative = go_to;
+               t->on_failure = go_to;
 }
 
 
@@ -269,13 +269,13 @@ void t_on_reply( unsigned int go_to )
        if (!t || t==T_UNDEFINED )
                goto_on_reply=go_to;
        else
-               get_t()->on_reply = go_to;
+               t->on_reply = go_to;
 }
 
 
-unsigned int get_on_negative()
+unsigned int get_on_failure()
 {
-       return goto_on_negative;
+       return goto_on_failure;
 }
 unsigned int get_on_reply()
 {
@@ -875,7 +875,7 @@ void faked_env( struct cell *t, struct sip_msg *msg)
 int fake_req(struct sip_msg *faked_req,
                struct sip_msg *shmem_msg, int extra_flags, struct ua_client *uac)
 {
-       /* on_negative_reply faked msg now copied from shmem msg (as opposed
+       /* on_failure_reply faked msg now copied from shmem msg (as opposed
         * to zero-ing) -- more "read-only" actions (exec in particular) will
         * work from reply_route as they will see msg->from, etc.; caution,
         * rw actions may append some pkg stuff to msg, which will possibly be
@@ -986,15 +986,15 @@ int run_failure_handlers(struct cell *t, struct sip_msg *rpl,
        /* failure_route for a local UAC? */
        if (!shmem_msg) {
                LOG(L_WARN,"Warning: run_failure_handlers: no UAC support (%d, %d) \n",
-                       t->on_negative, t->tmcb_hl.reg_types);
+                       t->on_failure, t->tmcb_hl.reg_types);
                return 0;
        }
 
        /* don't start faking anything if we don't have to */
-       if (unlikely(!t->on_negative && !has_tran_tmcbs( t, TMCB_ON_FAILURE))) {
+       if (unlikely(!t->on_failure && !has_tran_tmcbs( t, TMCB_ON_FAILURE))) {
                LOG(L_WARN,
                        "Warning: run_failure_handlers: no negative handler (%d, %d)\n",
-                       t->on_negative,
+                       t->on_failure,
                        t->tmcb_hl.reg_types);
                return 1;
        }
@@ -1010,12 +1010,12 @@ int run_failure_handlers(struct cell *t, struct sip_msg *rpl,
        if (unlikely(has_tran_tmcbs( t, TMCB_ON_FAILURE)) ) {
                run_trans_callbacks( TMCB_ON_FAILURE, t, &faked_req, rpl, code);
        }
-       if (t->on_negative) {
+       if (t->on_failure) {
                /* avoid recursion -- if failure_route forwards, and does not
                 * set next failure route, failure_route will not be reentered
                 * on failure */
-               on_failure = t->on_negative;
-               t->on_negative=0;
+               on_failure = t->on_failure;
+               t->on_failure=0;
                if (exec_pre_script_cb(&faked_req, FAILURE_CB_TYPE)>0) {
                        /* run a failure_route action if some was marked */
                        if (run_top_route(failure_rt.rlist[on_failure], &faked_req, 0)<0)
@@ -1287,7 +1287,7 @@ static enum rps t_should_relay_response( struct cell *Trans , int new_code,
                replies_dropped = 0;
                /* run ON_FAILURE handlers ( route and callbacks) */
                if (unlikely(has_tran_tmcbs( Trans, TMCB_ON_FAILURE_RO|TMCB_ON_FAILURE)
-                                               || Trans->on_negative )) {
+                                               || Trans->on_failure )) {
                        extra_flags=
                                ((Trans->uac[picked_branch].request.flags & F_RB_TIMEOUT)?
                                                        FL_TIMEOUT:0) | 
index 3ebbd73..560ca23 100644 (file)
@@ -201,14 +201,14 @@ void set_final_timer( /* struct s_table *h_table,*/ struct cell *t );
 
 void cleanup_uac_timers( struct cell *t );
 
-void on_negative_reply( struct cell* t, struct sip_msg* msg,
+void on_failure_reply( struct cell* t, struct sip_msg* msg,
        int code, void *param  );
 
 /* set which 'reply' structure to take if only negative
    replies arrive 
 */
-void t_on_negative( unsigned int go_to );
-unsigned int get_on_negative(void);
+void t_on_failure( unsigned int go_to );
+unsigned int get_on_failure(void);
 void t_on_reply( unsigned int go_to );
 unsigned int get_on_reply(void);
 
index 22356c8..aae9532 100644 (file)
@@ -471,7 +471,7 @@ inline static void final_response_handler(  struct retr_buf* r_buf,
                && t->nr_of_outgoings==1
                /* on_negativ reply handler not installed -- serial forking 
                 * could occur otherwise */
-               && t->on_negative==0
+               && t->on_failure==0
                /* the same for FAILURE callbacks */
                && !has_tran_tmcbs( t, TMCB_ON_FAILURE_RO|TMCB_ON_FAILURE) 
                /* something received -- we will not be silent on error */
index 3ca56ba..b191685 100644 (file)
@@ -195,6 +195,7 @@ static int fixup_on_branch(void** param, int param_no);
 static int fixup_t_reply(void** param, int param_no);
 static int fixup_on_sl_reply(modparam_t type, void* val);
 static int fixup_t_relay_to(void** param, int param_no);
+static int fixup_t_is_set(void** param, int param_no);
 
 /* init functions */
 static int mod_init(void);
@@ -270,7 +271,7 @@ inline static int w_t_forward_nonack_sctp(struct sip_msg*, char* str,char*);
 #endif
 inline static int w_t_forward_nonack_to(struct sip_msg* msg, char* str,char*);
 inline static int w_t_relay_cancel(struct sip_msg *p_msg, char *_foo, char *_bar);
-inline static int w_t_on_negative(struct sip_msg* msg, char *go_to, char *foo);
+inline static int w_t_on_failure(struct sip_msg* msg, char *go_to, char *foo);
 inline static int w_t_on_branch(struct sip_msg* msg, char *go_to, char *foo);
 inline static int w_t_on_reply(struct sip_msg* msg, char *go_to, char *foo );
 inline static int t_check_status(struct sip_msg* msg, char *match, char *foo);
@@ -298,6 +299,7 @@ static int t_grep_status(struct sip_msg* msg, char*, char*);
 static int w_t_drop_replies(struct sip_msg* msg, char* foo, char* bar);
 static int w_t_save_lumps(struct sip_msg* msg, char* foo, char* bar);
 static int w_t_check_trans(struct sip_msg* msg, char* foo, char* bar);
+static int w_t_is_set(struct sip_msg* msg, char* target, char* bar);
 
 
 /* by default the fr timers avps are not set, so that the avps won't be
@@ -401,7 +403,7 @@ static cmd_export_t cmds[]={
                        REQUEST_ROUTE},
        {"t_relay_cancel",     w_t_relay_cancel,        0, 0,
                        REQUEST_ROUTE},
-       {"t_on_failure",       w_t_on_negative,         1, fixup_on_failure,
+       {"t_on_failure",       w_t_on_failure,         1, fixup_on_failure,
                        REQUEST_ROUTE | FAILURE_ROUTE | TM_ONREPLY_ROUTE | BRANCH_ROUTE },
        {"t_on_reply",         w_t_on_reply,            1, fixup_on_reply,
                        REQUEST_ROUTE | FAILURE_ROUTE | TM_ONREPLY_ROUTE | BRANCH_ROUTE },
@@ -462,6 +464,8 @@ static cmd_export_t cmds[]={
                        REQUEST_ROUTE},
        {"t_check_trans",         w_t_check_trans,                      0, 0,
                        REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE },
+       {"t_is_set",          w_t_is_set,                               1, fixup_t_is_set,
+                       ANY_ROUTE },
 
        {"t_load_contacts", t_load_contacts,            0, 0,
                        REQUEST_ROUTE | FAILURE_ROUTE},
@@ -717,9 +721,9 @@ static int script_init( struct sip_msg *foo, unsigned int flags, void *bar)
         * not be used again */
 
        /* make sure the new message will not inherit previous
-               message's t_on_negative value
+               message's t_on_failure value
        */
-       t_on_negative( 0 );
+       t_on_failure( 0 );
        t_on_reply(0);
        t_on_branch(0);
        /* reset the kr status */
@@ -1336,9 +1340,9 @@ inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar )
 }
 
 
-inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo)
+inline static int w_t_on_failure( struct sip_msg* msg, char *go_to, char *foo)
 {
-       t_on_negative( (unsigned int )(long) go_to );
+       t_on_failure( (unsigned int )(long) go_to );
        return 1;
 }
 
@@ -1356,6 +1360,55 @@ inline static int w_t_on_reply( struct sip_msg* msg, char *go_to, char *foo )
 }
 
 
+static int w_t_is_set(struct sip_msg* msg, char *target, char *foo )
+{
+       int r;
+       tm_cell_t *t = NULL;
+       
+       r = 0;
+       t = get_t();
+       if (t==T_UNDEFINED) t = NULL;
+
+       switch(target[0]) {
+               case 'b':
+                       if(t==NULL)
+                               r = get_on_branch();
+                       else
+                               r = t->on_branch;
+                       break;
+               case 'f':
+                       if(t==NULL)
+                               r = get_on_failure();
+                       else
+                               r = t->on_failure;
+                       break;
+               case 'o':
+                       if(t==NULL)
+                               r = get_on_reply();
+                       else
+                               r = t->on_reply;
+                       break;
+       }
+       if(r) return 1;
+       return -1;
+}
+
+static int fixup_t_is_set(void** param, int param_no)
+{
+       int len;
+       if (param_no==1) {
+               len = strlen((char*)*param);
+               if((len==13 && strncmp((char*)*param, "failure_route", 13)==0)
+                               || (len==13 && strncmp((char*)*param, "onreply_route", 13)==0)
+                               || (len==12 && strncmp((char*)*param, "branch_route", 12)==0)) {
+                       return 0;
+               }
+
+               LM_ERR("invalid parameter value: %s\n", (char*)*param);
+               return 1;
+       }
+       return 0;
+}
 
 inline static int _w_t_relay_to(struct sip_msg  *p_msg ,
                                                                        struct proxy_l *proxy, int force_proto)
index 9cb6a4d..5a0c936 100644 (file)
@@ -157,7 +157,7 @@ int load_xtm(tm_xapi_t *xapi)
 
        memset(xapi, 0, sizeof(tm_xapi_t));
 
-       xapi->t_on_failure    = t_on_negative;
+       xapi->t_on_failure    = t_on_failure;
        xapi->t_on_branch     = t_on_branch;
        xapi->t_on_reply      = t_on_reply;
        xapi->t_check_trans   = t_check_trans;