4 * Copyright (C) 2008 iptelorg GmbH
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * @brief rvalue expressions
25 * 2008-12-01 initial version (andrei)
26 * 2009-04-24 added support for defined, strempty, strlen (andrei)
27 * 2009-04-28 int and str automatic conversions: (int)undef=0,
28 * (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
29 * handle undef == expr, in function of the UNDEF_EQ_* defines.
31 * 2009-05-05 casts operator for int & string (andrei)
36 * UNDEF_EQ_* - how to behave when undef is on the right side of a generic
38 * UNDEF_EQ_ALWAYS_FALSE: undef == something is always false
39 * UNDEF_EQ_UNDEF_TRUE : undef == something false except for undef==undef
41 * no UNDEF_EQ* define : undef == expr => convert undef to typeof(expr)
42 * and perform normal ==. undef == undef will be
43 * converted to string and it will be true
45 * NOTE: expr == undef, with defined(expr) is always evaluated this way:
46 expr == (type_of(expr))undef
47 * RV_STR2INT_VERBOSE_ERR - if a string conversion to int fails, log (L_WARN)
48 * the string that caused it (only the string, not
49 * the expression position).
50 * RV_STR2INT_ERR - if a string conversion to int fails, don't ignore
51 * the error (return error).
52 * RVAL_GET_INT_ERR_WARN - if a conversion to int fails, log a warning with
53 * the expression position.
54 * Depends on RV_STR2INT_ERR.
55 * RVAL_GET_INT_ERR_IGN - if a conversion to int fails, ignore the error
56 * (the result will be 0). Can be combined with
57 * RVAL_GET_INT_ERR_WARN.
58 * Depends on RV_STR2INT_ERR.
63 * \brief SIP-router core ::
70 #include <stdlib.h> /* abort() */
72 /* if defined warn when str2int conversions fail */
73 #define RV_STR2INT_VERBOSE_ERR
75 /* if defined rval_get_int will fail if str2int conversion fail
76 (else convert to 0) */
77 #define RV_STR2INT_ERR
79 /* if a rval_get_int fails (conversion to int), warn
80 Depends on RV_STR2INT_ERR.
82 #define RVAL_GET_INT_ERR_WARN
84 /* if a rval_get_int fails, ignore it (expression evaluation will not fail,
85 the int conversion will result in 0).
86 Can be combined with RVAL_GET_INT_ERR_WARN.
87 Depends on RV_STR2INT_ERR.
89 #define RVAL_GET_INT_ERR_IGN
91 /* minimum size alloc'ed for STR RVs (to accomodate
92 * strops without reallocs) */
93 #define RV_STR_EXTRA 80
95 #define rv_ref(rv) ((rv)->refcnt++)
97 /** unref rv and returns true if 0 */
98 #define rv_unref(rv) ((--(rv)->refcnt)==0)
101 inline static void rval_force_clean(struct rvalue* rv)
103 if (rv->flags & RV_CNT_ALLOCED_F){
111 BUG("RV_CNT_ALLOCED_F not supported for type %d\n", rv->type);
113 rv->flags&=~RV_CNT_ALLOCED_F;
115 if (rv->flags & RV_RE_ALLOCED_F){
117 if (unlikely(rv->type!=RV_STR || !(rv->flags & RV_RE_F))){
118 BUG("RV_RE_ALLOCED_F not supported for type %d or "
119 "bad flags %x\n", rv->type, rv->flags);
121 regfree(rv->v.re.regex);
122 pkg_free(rv->v.re.regex);
125 rv->flags&=~(RV_RE_ALLOCED_F|RV_RE_F);
131 /** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
132 * Note: it will be freed only when refcnt reaches 0
134 void rval_destroy(struct rvalue* rv)
136 if (rv && rv_unref(rv)){
137 rval_force_clean(rv);
138 /* still an un-regfreed RE ? */
139 if ((rv->flags & RV_RE_F) && rv->v.re.regex){
140 if (unlikely(rv->type!=RV_STR))
141 BUG("RV_RE_F not supported for type %d\n", rv->type);
142 regfree(rv->v.re.regex);
144 if (rv->flags & RV_RV_ALLOCED_F){
152 void rval_clean(struct rvalue* rv)
155 rval_force_clean(rv);
160 void rve_destroy(struct rval_expr* rve)
163 if (rve->op==RVE_RVAL_OP){
164 if (rve->left.rval.refcnt){
165 if (rve->left.rval.refcnt==1)
166 rval_destroy(&rve->left.rval);
168 BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
169 "\n", rve->left.rval.refcnt,
170 rve->fpos.s_line, rve->fpos.s_col,
171 rve->fpos.e_line, rve->fpos.e_col);
173 if (rve->right.rval.refcnt){
174 if (rve->right.rval.refcnt==1)
175 rval_destroy(&rve->right.rval);
177 BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
178 "\n", rve->right.rval.refcnt,
179 rve->fpos.s_line, rve->fpos.s_col,
180 rve->fpos.e_line, rve->fpos.e_col);
184 rve_destroy(rve->left.rve);
186 rve_destroy(rve->right.rve);
194 void rval_cache_clean(struct rval_cache* rvc)
196 if ((rvc->cache_type==RV_CACHE_PVAR) && (rvc->val_type!=RV_NONE)){
197 pv_value_destroy(&rvc->c.pval);
199 rvc->cache_type=RV_CACHE_EMPTY;
200 rvc->val_type=RV_NONE;
204 #define rv_chg_in_place(rv) ((rv)->refcnt==1)
208 /** init a rvalue structure.
209 * Note: not needed if the structure is allocate with one of the
210 * rval_new* functions
212 void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
221 memset (&rv->v, 0, sizeof(rv->v));
227 /** create a new pk_malloc'ed empty rvalue.
229 * @param extra_size - extra space to allocate
230 * (e.g.: so that future string operation can reuse
232 * @return new rv or 0 on error
234 struct rvalue* rval_new_empty(int extra_size)
237 int size; /* extra size at the end */
239 size=ROUND_LONG(sizeof(*rv)-sizeof(rv->buf)+extra_size); /* round up */
242 rv->bsize=size-sizeof(*rv)-sizeof(rv->buf); /* remaining size->buffer*/
243 rv->flags=RV_RV_ALLOCED_F;
252 /** create a new pk_malloc'ed rv from a str.
254 * @param s - pointer to str, must be non-null
255 * @param extra_size - extra space to allocate
256 * (so that future string operation can reuse
258 * @return new rv or 0 on error
260 struct rvalue* rval_new_str(str* s, int extra_size)
264 rv=rval_new_empty(extra_size+s->len+1/* 0 term */);
267 rv->v.s.s=&rv->buf[0];
269 memcpy(rv->v.s.s, s->s, s->len);
277 /** create a new pk_malloc'ed RE rv from a str re.
278 * It acts as rval_new_str, but also compiles a RE from the str
279 * and sets v->re.regex.
280 * @param s - pointer to str, must be non-null, zero-term'ed and a valid RE.
281 * @return new rv or 0 on error
283 struct rvalue* rval_new_re(str* s)
288 offs=(long)&((struct rvalue*)0)->buf[0]; /* offset of the buf. member */
289 /* make sure we reserve enough space so that we can satisfy any regex_t
290 alignment requirement (pointer) */
291 rv=rval_new_empty(ROUND_POINTER(offs)-offs+sizeof(*rv->v.re.regex)+
295 /* make sure regex points to a properly aligned address
296 (use max./pointer alignment to be sure ) */
297 rv->v.re.regex=(regex_t*)((char*)&rv->buf[0]+ROUND_POINTER(offs)-offs);
298 rv->v.s.s=(char*)rv->v.re.regex+sizeof(*rv->v.re.regex);
300 memcpy(rv->v.s.s, s->s, s->len);
302 /* compile the regex */
303 /* same flags as for expr. =~ (fix_expr()) */
304 if (unlikely(regcomp(rv->v.re.regex, s->s,
305 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
317 /** get string name for a type.
319 * @return - null terminated name of the type
321 char* rval_type_name(enum rval_type type)
342 return "error_unkown_type";
347 /** create a new pk_malloc'ed rvalue from a rval_val union.
349 * @param s - pointer to str, must be non-null
350 * @param extra_size - extra space to allocate
351 * (so that future string operation can reuse
353 * @return new rv or 0 on error
355 struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size)
359 if (t==RV_STR && v && v->s.s)
360 return rval_new_str(&v->s, extra_size);
361 rv=rval_new_empty(extra_size);
364 if (likely(v && t!=RV_STR)){
366 }else if (t==RV_STR){
367 rv->v.s.s=&rv->buf[0];
369 if (likely(extra_size)) rv->v.s.s[0]=0;
371 memset (&rv->v, 0, sizeof(rv->v));
378 /** get rvalue basic type (RV_INT or RV_STR).
380 * Given a rvalue it tries to determinte its basic type.
381 * Fills val_cache if non-null and empty (can be used in other rval*
382 * function calls, to avoid re-resolving avps or pvars). It must be
383 * rval_cache_clean()'en when no longer needed.
385 * @param rv - target rvalue
386 * @param val_cache - write-only: value cache, might be filled if non-null,
387 * it _must_ be rval_cache_clean()'en when done.
388 * @return - basic type or RV_NONE on error
390 inline static enum rval_type rval_get_btype(struct run_act_ctx* h,
393 struct rval_cache* val_cache)
401 enum rval_type* ptype;
411 if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
412 pv=&val_cache->c.pval;
413 val_cache->cache_type=RV_CACHE_PVAR;
418 memset(pv, 0, sizeof(tmp_pval));
419 if (likely(pv_get_spec_value(msg, &rv->v.pvs, pv)==0)){
420 if (pv->flags & PV_TYPE_INT){
421 if (likely(val_cache!=0))
422 val_cache->val_type=RV_INT;
424 pv_value_destroy(pv);
426 }else if (pv->flags & PV_VAL_STR){
427 if (likely(val_cache!=0))
428 val_cache->val_type=RV_STR;
430 pv_value_destroy(pv);
433 pv_value_destroy(pv);
434 if (likely(val_cache!=0))
435 val_cache->val_type=RV_NONE; /* undefined */
439 if (likely(val_cache!=0))
440 val_cache->val_type=RV_NONE; /* undefined */
445 if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
446 ptype=&val_cache->val_type;
447 avpv=&val_cache->c.avp_val;
448 val_cache->cache_type=RV_CACHE_AVP;
453 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
454 avpv, rv->v.avps.index);
456 if (r_avp->flags & AVP_VAL_STR){
471 BUG("rv type %d not handled\n", rv->type);
479 /** guess the type of an expression.
480 * @return RV_INT, RV_STR or RV_NONE (when type could not be found,
483 enum rval_type rve_guess_type( struct rval_expr* rve)
487 switch(rve->left.rval.type){
529 case RVE_STREMPTY_OP:
534 /* '+' evaluates to the type of the left operand */
535 return rve_guess_type(rve->left.rve);
547 /** returns true if expression is constant.
551 int rve_is_constant(struct rval_expr* rve)
555 switch(rve->left.rval.type){
574 case RVE_STREMPTY_OP:
578 return rve_is_constant(rve->left.rve);
604 return rve_is_constant(rve->left.rve) &&
605 rve_is_constant(rve->right.rve);
614 /** returns true if the expression has side-effects.
615 * @return 1 for possible side-effects, 0 for no side-effects
616 * TODO: add better checks
618 int rve_has_side_effects(struct rval_expr* rve)
620 return !rve_is_constant(rve);
625 /** returns true if operator is unary (takes only 1 arg).
628 static int rve_op_unary(enum rval_expr_op op)
631 case RVE_RVAL_OP: /* not realy an operator */
638 case RVE_STREMPTY_OP:
678 /** returns 1 if expression is valid (type-wise).
679 * @param type - filled with the type of the expression (RV_INT, RV_STR or
680 * RV_NONE if it's dynamic)
681 * @param rve - checked expression
682 * @param bad_rve - set on failure to the subexpression for which the
684 * @param bad_type - set on failure to the type of the bad subexpression
685 * @param exp_type - set on failure to the expected type for the bad
687 * @return 0 or 1 and sets *type to the resulting type
688 * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
690 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
691 struct rval_expr** bad_rve,
692 enum rval_type* bad_t,
693 enum rval_type* exp_t)
695 enum rval_type type1, type2;
699 *type=rve_guess_type(rve);
706 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
708 if (bad_rve) *bad_rve=rve->left.rve;
709 if (bad_t) *bad_t=type1;
710 if (exp_t) *exp_t=RV_INT;
736 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
738 if (bad_rve) *bad_rve=rve->left.rve;
739 if (bad_t) *bad_t=type1;
740 if (exp_t) *exp_t=RV_INT;
743 if (rve_check_type(&type2, rve->right.rve, bad_rve,
746 if (bad_rve) *bad_rve=rve->right.rve;
747 if (bad_t) *bad_t=type2;
748 if (exp_t) *exp_t=RV_INT;
758 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
759 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
761 if ((type2!=type1) && (type1!=RV_NONE) &&
763 !(type1==RV_STR && type2==RV_INT)){
764 if (bad_rve) *bad_rve=rve->right.rve;
765 if (bad_t) *bad_t=type2;
766 if (exp_t) *exp_t=type1;
775 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
776 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
778 if ((type2!=type1) && (type1!=RV_NONE) &&
780 !(type1==RV_STR && type2==RV_INT)){
781 if (bad_rve) *bad_rve=rve->right.rve;
782 if (bad_t) *bad_t=type2;
783 if (exp_t) *exp_t=type1;
793 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
794 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
796 if ((type2!=type1) && (type1!=RV_NONE) &&
798 !(type1==RV_STR && type2==RV_INT)){
799 if (bad_rve) *bad_rve=rve->right.rve;
800 if (bad_t) *bad_t=type2;
801 if (exp_t) *exp_t=type1;
805 if (bad_rve) *bad_rve=rve->left.rve;
806 if (bad_t) *bad_t=type1;
807 if (exp_t) *exp_t=RV_STR;
818 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
819 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
821 if ((type2!=type1) && (type1!=RV_NONE) &&
823 !(type1==RV_STR && type2==RV_INT)){
824 if (bad_rve) *bad_rve=rve->right.rve;
825 if (bad_t) *bad_t=type2;
826 if (exp_t) *exp_t=type1;
830 if (bad_rve) *bad_rve=rve->left.rve;
831 if (bad_t) *bad_t=type1;
832 if (exp_t) *exp_t=RV_STR;
840 case RVE_STREMPTY_OP:
843 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
845 if (bad_rve) *bad_rve=rve->left.rve;
846 if (bad_t) *bad_t=type1;
847 if (exp_t) *exp_t=RV_STR;
863 BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
864 rve->fpos.s_line, rve->fpos.s_col,
865 rve->fpos.e_line, rve->fpos.e_col);
866 if (bad_rve) *bad_rve=rve;
867 if (bad_t) *bad_t=RV_NONE;
868 if (exp_t) *exp_t=RV_STR;
876 /** get the integer value of an rvalue.
878 * if rv == undefined select, avp or pvar, return 0.
879 * if an error occurs while evaluating a select, avp or pvar, behave as
880 * for the undefined case (and return success).
881 * @param h - script context handle
882 * @param msg - sip msg
883 * @param i - pointer to int, where the conversion result will be stored
884 * @param rv - rvalue to be converted
885 * @param cache - cached rv value (read-only), can be 0
887 * @return 0 on success, \<0 on error and EXPR_DROP on drop
889 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
890 int* i, struct rvalue* rv,
891 struct rval_cache* cache)
912 *i=eval_expr(h, rv->v.bexpr, msg);
920 *i=(run_actions_safe(h, rv->v.action, msg)>0);
921 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
927 r=run_select(&tmp, &rv->v.sel, msg);
931 else /* i>0 => undefined */
937 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
938 if (likely(cache->val_type==RV_INT)){
939 *i=cache->c.avp_val.n;
940 }else if (cache->val_type==RV_STR){
941 s=&cache->c.avp_val.s;
943 }else if (cache->val_type==RV_NONE)
945 else goto error_cache;
947 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
948 &avp_val, rv->v.avps.index);
950 if (unlikely(r_avp->flags & AVP_VAL_STR)){
962 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
963 if (likely((cache->val_type==RV_INT) ||
964 (cache->c.pval.flags & PV_VAL_INT))){
966 }else if (cache->val_type==RV_STR){
969 }else if (cache->val_type==RV_NONE)
971 else goto error_cache;
973 memset(&pval, 0, sizeof(pval));
974 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
975 if (likely(pval.flags & PV_VAL_INT)){
977 pv_value_destroy(&pval);
978 }else if (likely(pval.flags & PV_VAL_STR)){
979 destroy_pval=1; /* we must pv_value_destroy() later*/
983 /* no PV_VAL_STR and no PV_VAL_INT => undef
985 pv_value_destroy(&pval);
994 BUG("rv type %d not handled\n", rv->type);
999 eval_error: /* same as undefined */
1000 /* handle undefined => result 0, return success */
1004 /* rv is of string type => try to convert it to int */
1005 /* if "" => 0 (most likely case) */
1006 if (likely(s->len==0)) *i=0;
1007 else if (unlikely(str2sint(s, i)!=0)){
1008 /* error converting to int => non numeric => 0 */
1010 #ifdef RV_STR2INT_VERBOSE_ERR
1011 WARN("automatic string to int conversion for \"%.*s\" failed\n",
1013 /* return an error code */
1015 #ifdef RV_STR2INT_ERR
1020 pv_value_destroy(&pval);
1023 BUG("invalid cached value:cache type %d, value type %d\n",
1024 cache?cache->cache_type:0, cache?cache->val_type:0);
1027 pv_value_destroy(&pval);
1034 /** log a message, appending rve position and a '\n'.*/
1035 #define RVE_LOG(lev, rve, txt) \
1036 LOG((lev), txt " (%d,%d-%d,%d)\n", \
1037 (rve)->fpos.s_line, rve->fpos.s_col, \
1038 (rve)->fpos.e_line, rve->fpos.e_col )
1041 /** macro for checking and handling rval_get_int() retcode.
1042 * check if the return code is an rval_get_int error and if so
1043 * handle the error (e.g. print a log message, ignore the error by
1044 * setting ret to 0 a.s.o.)
1045 * @param ret - retcode as returned by rval_get_int() (might be changed)
1046 * @param txt - warning message txt (no pointer allowed)
1047 * @param rve - rval_expr, used to access the config. pos
1049 #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
1050 #define rval_get_int_handle_ret(ret, txt, rve) \
1052 if (unlikely((ret)<0)) { \
1053 RVE_LOG(L_WARN, rve, txt); \
1057 #elif defined RVAL_GET_INT_ERR_WARN
1058 #define rval_get_int_handle_ret(ret, txt, rve) \
1060 if (unlikely((ret)<0)) \
1061 RVE_LOG(L_WARN, rve, txt); \
1063 #elif defined RVAL_GET_INT_ERR_IGN
1064 #define rval_get_int_handle_ret(ret, txt, rve) \
1066 if (unlikely((ret)<0)) \
1070 #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
1078 /** get the string value of an rv in a tmp variable
1080 * if rv == undefined select, avp or pvar, return "".
1081 * if an error occurs while evaluating a select, avp or pvar, behave as
1082 * for the undefined case (and return success).
1083 * The result points either inside the passed rv, inside
1084 * new_cache or inside an avp. new_cache must be non zero,
1085 * initialized previously and it _must_ be rval_cache_clean(...)'ed when
1087 * WARNING: it's not intended for general use. It might return a pointer
1088 * inside rv so the result _must_ be treated as read-only. rv and new_cache
1089 * must not be released/freed until the result is no longer needed.
1090 * For general use see rval_get_str().
1091 * @param h - script context handle
1092 * @param msg - sip msg
1093 * @param tmpv - str return value (pointer to a str struct that will be
1094 * be filled with the conversion result)
1095 * @param rv - rvalue to be converted
1096 * @param cache - cached rv value (read-only), can be 0
1097 * @param tmp_cache - used for temporary storage (so that tmpv will not
1098 * point to possible freed data), it must be non-null,
1099 * initialized and cleaned afterwards.
1100 * @return 0 on success, <0 on error and EXPR_DROP on drop
1102 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1103 str* tmpv, struct rvalue* rv,
1104 struct rval_cache* cache,
1105 struct rval_cache* tmp_cache)
1112 tmpv->s=sint2strbuf(rv->v.l, tmp_cache->i2s,
1113 sizeof(tmp_cache->i2s), &tmpv->len);
1114 tmp_cache->cache_type = RV_CACHE_INT2STR;
1121 i=(run_actions_safe(h, rv->v.action, msg)>0);
1122 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1126 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1127 sizeof(tmp_cache->i2s), &tmpv->len);
1128 tmp_cache->cache_type = RV_CACHE_INT2STR;
1131 i=eval_expr(h, rv->v.bexpr, msg);
1134 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1135 sizeof(tmp_cache->i2s), &tmpv->len);
1136 tmp_cache->cache_type = RV_CACHE_INT2STR;
1139 tmpv->s=sint2strbuf(i, tmp_cache->i2s, sizeof(tmp_cache->i2s),
1141 tmp_cache->cache_type = RV_CACHE_INT2STR;
1144 i=run_select(tmpv, &rv->v.sel, msg);
1145 if (unlikely(i!=0)){
1148 }else { /* i>0 => undefined */
1154 if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1155 if (likely(cache->val_type==RV_STR)){
1156 *tmpv=cache->c.avp_val.s;
1157 }else if (cache->val_type==RV_INT){
1158 i=cache->c.avp_val.n;
1159 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1160 sizeof(tmp_cache->i2s), &tmpv->len);
1161 tmp_cache->cache_type = RV_CACHE_INT2STR;
1162 }else if (cache->val_type==RV_NONE){
1164 }else goto error_cache;
1166 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1167 &tmp_cache->c.avp_val,
1170 if (likely(r_avp->flags & AVP_VAL_STR)){
1171 tmp_cache->cache_type=RV_CACHE_AVP;
1172 tmp_cache->val_type=RV_STR;
1173 *tmpv=tmp_cache->c.avp_val.s;
1175 i=tmp_cache->c.avp_val.n;
1176 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1177 sizeof(tmp_cache->i2s), &tmpv->len);
1178 tmp_cache->cache_type = RV_CACHE_INT2STR;
1184 if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1185 if (likely(cache->val_type==RV_STR)){
1186 *tmpv=cache->c.pval.rs;
1187 }else if (cache->val_type==RV_INT){
1189 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1190 sizeof(tmp_cache->i2s), &tmpv->len);
1191 tmp_cache->cache_type = RV_CACHE_INT2STR;
1192 }else if (cache->val_type==RV_NONE){
1194 }else goto error_cache;
1196 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1197 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1198 &tmp_cache->c.pval)==0)){
1199 if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1200 /* the value is not destroyed, but saved instead
1201 in tmp_cache so that it can be destroyed later
1202 when no longer needed */
1203 tmp_cache->cache_type=RV_CACHE_PVAR;
1204 tmp_cache->val_type=RV_STR;
1205 *tmpv=tmp_cache->c.pval.rs;
1206 }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1207 i=tmp_cache->c.pval.ri;
1208 pv_value_destroy(&tmp_cache->c.pval);
1209 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1210 sizeof(tmp_cache->i2s), &tmpv->len);
1211 tmp_cache->cache_type = RV_CACHE_INT2STR;
1213 /* no PV_VAL_STR and no PV_VAL_INT => undef
1215 pv_value_destroy(&tmp_cache->c.pval);
1224 BUG("rv type %d not handled\n", rv->type);
1229 eval_error: /* same as undefined */
1230 /* handle undefined => result "", return success */
1235 BUG("invalid cached value:cache type %d, value type %d\n",
1236 cache?cache->cache_type:0, cache?cache->val_type:0);
1245 /** get the string value of an rv.
1247 * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1248 * @return 0 on success, <0 on error and EXPR_DROP on drop
1250 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1251 str* s, struct rvalue* rv,
1252 struct rval_cache* cache)
1255 struct rval_cache tmp_cache;
1257 rval_cache_init(&tmp_cache);
1258 if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1260 s->s=pkg_malloc(tmp.len+1/* 0 term */);
1261 if (unlikely(s->s==0)){
1262 ERR("memory allocation error\n");
1266 memcpy(s->s, tmp.s, tmp.len);
1267 s->s[tmp.len]=0; /* 0 term */
1268 rval_cache_clean(&tmp_cache);
1271 rval_cache_clean(&tmp_cache);
1277 /** convert a rvalue to another rvalue, of a specific type.
1279 * The result is read-only in most cases (can be a reference
1280 * to another rvalue, can be checked by using rv_chg_in_place()) and
1281 * _must_ be rval_destroy()'ed.
1283 * @param type - type to convert to
1284 * @param v - rvalue to convert
1285 * @param c - rval_cache (cached v value if known/filled by another
1286 * function), can be 0 (unknown/not needed)
1287 * @return pointer to a rvalue (reference to an existing one or a new
1288 * one, @see rv_chg_in_place() and the above comment) or 0 on error.
1290 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1291 enum rval_type type, struct rvalue* v,
1292 struct rval_cache* c)
1295 struct rval_cache tmp_cache;
1306 if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1309 return rval_new(RV_INT, &val, 0);
1311 rval_cache_init(&tmp_cache);
1312 if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1314 rval_cache_clean(&tmp_cache);
1317 ret=rval_new_str(&tmp, RV_STR_EXTRA);
1318 rval_cache_clean(&tmp_cache);
1322 BUG("unsupported conversion to type %d\n", type);
1330 /** integer operation: *res= op v.
1331 * @return 0 on succes, \<0 on error
1333 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1349 BUG("rv unsupported intop1 %d\n", op);
1357 /** integer operation: *res= v1 op v2
1358 * @return 0 on succes, \<0 on error
1360 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1374 if (unlikely(v2==0)){
1375 ERR("rv div by 0\n");
1381 if (unlikely(v2==0)){
1382 ERR("rv mod by 0\n");
1396 case RVE_BLSHIFT_OP:
1399 case RVE_BRSHIFT_OP:
1430 /* invalid operand for int */
1433 BUG("rv unsupported intop %d\n", op);
1441 /** internal helper: compare 2 RV_STR RVs.
1442 * Warning: rv1 & rv2 must be RV_STR
1443 * @return 0 on success, -1 on error
1445 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1446 struct rvalue* rv1, struct rvalue* rv2)
1457 *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1460 case RVE_STRDIFF_OP:
1461 *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1464 if (likely(rv2->flags & RV_RE_F)){
1465 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1467 /* we need to compile the RE on the fly */
1468 if (unlikely(regcomp(&tmp_re, s2->s,
1469 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1471 ERR("Bad regular expression \"%s\"\n", s2->s);
1474 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1479 BUG("rv unsupported intop %d\n", op);
1490 /** integer returning operation on string: *res= op str (returns integer)
1491 * @return 0 on succes, \<0 on error
1493 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1499 case RVE_STREMPTY_OP:
1503 BUG("rv unsupported int_strop1 %d\n", op);
1512 /** integer operation: ret= op v (returns a rvalue).
1513 * @return rvalue on success, 0 on error
1515 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1516 struct sip_msg* msg,
1517 enum rval_expr_op op,
1525 rv2=rval_convert(h, msg, RV_INT, v, 0);
1526 if (unlikely(rv2==0)){
1527 ERR("rval int conversion failed\n");
1530 if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1532 if (rv_chg_in_place(rv2)){
1535 }else if (rv_chg_in_place(v)){
1539 ret=rval_new(RV_INT, &rv2->v, 0);
1540 if (unlikely(ret==0)){
1541 ERR("eval out of memory\n");
1555 /** integer operation: ret= l op r (returns a rvalue).
1556 * @return rvalue on success, 0 on error
1558 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1559 struct sip_msg* msg,
1560 enum rval_expr_op op,
1571 if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1573 if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1575 if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1577 if (rv_chg_in_place(rv1)){
1578 /* try reusing rv1 */
1581 }else if (rv_chg_in_place(rv2)){
1582 /* try reusing rv2 */
1585 }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1588 } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1592 ret=rval_new(RV_INT, &rv1->v, 0);
1593 if (unlikely(ret==0)){
1594 ERR("rv eval out of memory\n");
1610 /** string add operation: ret= l . r (returns a rvalue).
1611 * Can use cached rvalues (c1 & c2).
1612 * @return rvalue on success, 0 on error
1614 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1615 struct sip_msg* msg,
1617 struct rval_cache* c1,
1619 struct rval_cache* c2
1636 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1638 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1641 len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1643 if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1644 /* try reusing rv1 */
1648 if (ret->v.s.s == &ret->buf[0]) s1=0;
1652 ret->flags &= ~RV_CNT_ALLOCED_F;
1653 ret->v.s.s=&ret->buf[0];
1657 }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1658 /* try reusing rv2 */
1662 if (ret->v.s.s == &ret->buf[0])
1667 ret->flags &= ~RV_CNT_ALLOCED_F;
1668 ret->v.s.s=&ret->buf[0];
1672 }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1676 if (ret->v.s.s == &ret->buf[0]) s1=0;
1680 ret->flags &= ~RV_CNT_ALLOCED_F;
1681 ret->v.s.s=&ret->buf[0];
1685 } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1689 if (ret->v.s.s == &ret->buf[0])
1694 ret->flags &= ~RV_CNT_ALLOCED_F;
1695 ret->v.s.s=&ret->buf[0];
1700 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1701 if (unlikely(ret==0)){
1702 ERR("rv eval out of memory\n");
1708 /* do the actual copy */
1709 memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1711 memcpy(ret->buf, s1->s, s1->len);
1713 ret->v.s.len=rv1->v.s.len+s2->len;
1714 ret->v.s.s[ret->v.s.len]=0;
1715 /* cleanup if needed */
1716 if (flags & RV_CNT_ALLOCED_F)
1729 /** bool operation on rval evaluated as strings.
1730 * Can use cached rvalues (c1 & c2).
1731 * @return 0 success, -1 on error
1733 inline static int rval_str_lop2(struct run_act_ctx* h,
1734 struct sip_msg* msg,
1736 enum rval_expr_op op,
1738 struct rval_cache* c1,
1740 struct rval_cache* c2)
1748 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1750 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1752 ret=bool_rvstrop2(op, res, rv1, rv2);
1764 /** integer operation on rval evaluated as string.
1765 * Can use cached rvalues (c1 & c2).
1766 * @param res - will be set to the result
1767 * @return 0 success, -1 on error
1769 inline static int rval_int_strop1(struct run_act_ctx* h,
1770 struct sip_msg* msg,
1772 enum rval_expr_op op,
1774 struct rval_cache* c1)
1781 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1783 ret=int_strop1(res, op, &rv1->v.s);
1794 /** checks if rv is defined.
1795 * @param res - set to the result 1 - defined, 0 not defined
1796 * @return 0 on success, -1 on error
1797 * Can use cached rvalues (cache).
1798 * Note: a rv can be undefined if it's an undefined avp or pvar or select or
1800 * Note2: an error in the avp, pvar or select search is equivalent to
1801 * undefined (and it's not reported)
1803 inline static int rv_defined(struct run_act_ctx* h,
1804 struct sip_msg* msg, int* res,
1805 struct rvalue* rv, struct rval_cache* cache)
1815 if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1816 *res=(cache->val_type!=RV_NONE);
1818 /* run select returns 0 on success, -1 on error and >0 on
1819 undefined. error is considered undefined */
1820 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1823 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1824 *res=(cache->val_type!=RV_NONE);
1826 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1827 &avp_val, rv->v.avps.index);
1828 if (unlikely(r_avp==0)){
1834 /* PV_VAL_NULL or pv_get_spec_value error => undef */
1835 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1836 *res=(cache->val_type!=RV_NONE);
1838 memset(&pval, 0, sizeof(pval));
1839 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1840 if ((pval.flags & PV_VAL_NULL) &&
1841 ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1844 pv_value_destroy(&pval);
1846 *res=0; /* in case of error, consider it undef */
1860 /** defined (integer) operation on rve.
1861 * @param res - set to 1 defined, 0 not defined
1862 * @return - 0 on success, -1 on error
1864 inline static int int_rve_defined(struct run_act_ctx* h,
1865 struct sip_msg* msg, int* res,
1866 struct rval_expr* rve)
1868 /* only a rval can be undefined, any expression consisting on more
1869 then one rval => defined */
1870 if (likely(rve->op==RVE_RVAL_OP))
1871 return rv_defined(h, msg, res, &rve->left.rval, 0);
1878 /** evals an integer expr to an int.
1880 * *res=(int)eval(rve)
1881 * @return 0 on success, \<0 on error
1883 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1884 int* res, struct rval_expr* rve)
1887 struct rval_cache c1, c2;
1894 ret=rval_get_int(h, msg, res, &rve->left.rval, 0);
1895 rval_get_int_handle_ret(ret, "rval expression conversion to int"
1903 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1905 ret=int_intop1(res, rve->op, i1);
1908 ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1919 case RVE_BLSHIFT_OP:
1920 case RVE_BRSHIFT_OP:
1928 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1931 (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1933 ret=int_intop2(res, rve->op, i1, i2);
1937 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1942 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1943 rve->right.rve)) <0) )
1951 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1956 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1957 rve->right.rve)) <0) )
1965 /* if left is string, eval left & right as string and
1967 * if left is int eval as int using int diff
1968 * if left is undef, look at right and convert to right type
1970 rval_cache_init(&c1);
1971 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1972 rve->left.rve, &c1))<0)){
1974 rval_cache_clean(&c1);
1977 if (likely(rv1==0)){
1979 rval_cache_clean(&c1);
1980 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1981 rve->right.rve)) <0) )
1983 ret=int_intop2(res, rve->op, i1, i2);
1985 /* not int => str or undef */
1986 /* check for undefined left operand */
1987 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
1988 c1.val_type==RV_NONE)){
1989 #ifdef UNDEF_EQ_ALWAYS_FALSE
1990 /* undef == something always false
1991 undef != something always true*/
1992 ret=(rve->op==RVE_DIFF_OP);
1993 #elif defined UNDEF_EQ_UNDEF_TRUE
1994 /* undef == something defined always false
1995 undef == undef true */
1996 if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
1998 rval_cache_clean(&c1);
2002 ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
2003 #else /* ! UNDEF_EQ_* */
2005 * => convert to (type_of(val)) (undef) == val */
2006 rval_cache_init(&c2);
2007 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
2008 rve->right.rve, &c2))<0)){
2010 rval_cache_clean(&c1);
2011 rval_cache_clean(&c2);
2017 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
2020 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
2022 rval_cache_clean(&c2);
2025 #endif /* UNDEF_EQ_* */
2026 rval_cache_clean(&c1);
2029 /* left value == defined and != int => str
2030 * => lval == (str) val */
2031 if (unlikely((rv2=rval_expr_eval(h, msg,
2032 rve->right.rve))==0)){
2035 rval_cache_clean(&c1);
2038 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
2039 rval_cache_clean(&c1);
2046 /* eval expression => string */
2047 if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
2051 /* convert to int */
2052 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2053 rval_get_int_handle_ret(ret, "rval expression conversion to int"
2058 /* (str)expr => eval expression */
2059 rval_cache_init(&c1);
2060 if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2061 rve->left.rve, &c1))<0)){
2063 rval_cache_clean(&c1);
2067 /* expr evaluated to string => (int)(str)v == (int)v */
2068 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2069 rval_get_int_handle_ret(ret, "rval expression conversion"
2070 " to int failed", rve);
2072 rval_cache_clean(&c1);
2074 => expr evaluated to int =>
2075 return (int)(str)v == (int)v => do nothing */
2079 /* same thing as above, but in a not optimized, easier to
2081 /* 1. (str) expr => eval expr */
2082 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2086 /* 2. convert to str and then convert to int
2087 but since (int)(str)v == (int)v skip over (str)v */
2088 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2092 case RVE_DEFINED_OP:
2093 ret=int_rve_defined(h, msg, res, rve->left.rve);
2096 case RVE_STRDIFF_OP:
2098 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2102 if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2107 ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2112 case RVE_STREMPTY_OP:
2113 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2117 ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2122 BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2123 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2124 rve->fpos.e_line, rve->fpos.e_col);
2132 /** evals a rval expr. into an int or another rv(str).
2133 * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
2134 * (it might be a reference to another rval). The result can be
2135 * modified only if rv_chg_in_place() returns true.
2136 * @param res_rv - pointer to rvalue result, if non-null it means the
2137 * expression evaluated to a non-int (str), which will be
2139 * @param res_i - pointer to int result, if res_rv==0 and the function
2140 * returns success => the result is an int which will be
2142 * @param rve - expression that will be evaluated.
2143 * @param cache - write-only value cache, it might be filled if non-null and
2144 * empty (rval_cache_init()). If non-null, it _must_ be
2145 * rval_cache_clean()'ed when done.
2147 * @result 0 on success, -1 on error, sets *res_rv or *res_i.
2149 int rval_expr_eval_rvint( struct run_act_ctx* h,
2150 struct sip_msg* msg,
2151 struct rvalue** res_rv,
2153 struct rval_expr* rve,
2154 struct rval_cache* cache
2159 struct rval_cache c1; /* local cache */
2162 enum rval_type type;
2169 rv1=&rve->left.rval;
2171 type=rval_get_btype(h, msg, rv1, cache);
2173 r=rval_get_int(h, msg, res_i, rv1, cache);
2174 rval_get_int_handle_ret(r, "rval expression conversion"
2175 " to int failed", rve);
2177 ret=r; /* equiv. to if (r<0) goto error */
2179 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the
2180 cached resolved value in cache*/
2197 case RVE_BLSHIFT_OP:
2198 case RVE_BRSHIFT_OP:
2211 case RVE_STRDIFF_OP:
2214 case RVE_STREMPTY_OP:
2215 case RVE_DEFINED_OP:
2217 /* operator forces integer type */
2218 ret=rval_expr_eval_int(h, msg, res_i, rve);
2222 rval_cache_init(&c1);
2223 r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2225 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2226 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2227 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2229 rval_cache_clean(&c1);
2233 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2234 rve->right.rve))<0)){
2235 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2236 "\n", rve->right.rve->fpos.s_line,
2237 rve->right.rve->fpos.s_col,
2238 rve->right.rve->fpos.e_line,
2239 rve->right.rve->fpos.e_col);
2240 rval_cache_clean(&c1);
2243 ret=int_intop2(res_i, rve->op, i, j);
2246 rv2=rval_expr_eval(h, msg, rve->right.rve);
2247 if (unlikely(rv2==0)){
2248 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2249 rve->right.rve->fpos.s_line,
2250 rve->right.rve->fpos.s_col,
2251 rve->right.rve->fpos.e_line,
2252 rve->right.rve->fpos.e_col);
2253 rval_cache_clean(&c1);
2256 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2259 rval_cache_clean(&c1);
2263 *res_rv=rval_expr_eval(h, msg, rve);
2268 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2269 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2270 rve->fpos.e_line, rve->fpos.e_col);
2284 /** evals a rval expr..
2285 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2286 * a reference to another rval). The result can be modified only
2287 * if rv_chg_in_place() returns true.
2288 * @return rvalue on success, 0 on error
2290 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2291 struct rval_expr* rve)
2296 struct rval_cache c1;
2299 enum rval_type type;
2306 rv_ref(&rve->left.rval);
2307 return &rve->left.rval;
2320 case RVE_BLSHIFT_OP:
2321 case RVE_BRSHIFT_OP:
2334 case RVE_STRDIFF_OP:
2337 case RVE_STREMPTY_OP:
2338 case RVE_DEFINED_OP:
2340 /* operator forces integer type */
2341 r=rval_expr_eval_int(h, msg, &i, rve);
2344 ret=rval_new(RV_INT, &v, 0);
2345 if (unlikely(ret==0)){
2346 ERR("rv eval int expression: out of memory\n");
2351 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2352 rve->fpos.s_line, rve->fpos.s_col,
2353 rve->fpos.e_line, rve->fpos.e_col);
2358 rv1=rval_expr_eval(h, msg, rve->left.rve);
2359 if (unlikely(rv1==0)){
2360 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2361 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2362 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2365 rval_cache_init(&c1);
2366 type=rval_get_btype(h, msg, rv1, &c1);
2369 r=rval_get_int(h, msg, &i, rv1, &c1);
2370 rval_get_int_handle_ret(r, "rval expression left side "
2371 "conversion to int failed",
2374 rval_cache_clean(&c1);
2377 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2378 rve->right.rve))<0)){
2379 rval_cache_clean(&c1);
2380 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2381 " could not evaluate right side to int\n",
2382 rve->fpos.s_line, rve->fpos.s_col,
2383 rve->fpos.e_line, rve->fpos.e_col);
2386 int_intop2(&r, rve->op, i, j);
2387 if (rv_chg_in_place(rv1)){
2393 ret=rval_new(RV_INT, &v, 0);
2394 if (unlikely(ret==0)){
2395 rval_cache_clean(&c1);
2396 ERR("rv eval int expression: out of memory\n");
2403 rv2=rval_expr_eval(h, msg, rve->right.rve);
2404 if (unlikely(rv2==0)){
2405 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2406 "\n", rve->right.rve->fpos.s_line,
2407 rve->right.rve->fpos.s_col,
2408 rve->right.rve->fpos.e_line,
2409 rve->right.rve->fpos.e_col);
2410 rval_cache_clean(&c1);
2413 ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2416 BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2417 rve->fpos.s_line, rve->fpos.s_col,
2418 rve->fpos.e_line, rve->fpos.e_col);
2420 rval_cache_clean(&c1);
2423 rv1=rval_expr_eval(h, msg, rve->left.rve);
2424 if (unlikely(rv1==0)){
2425 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2426 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2427 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2430 rv2=rval_expr_eval(h, msg, rve->right.rve);
2431 if (unlikely(rv2==0)){
2432 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2433 rve->right.rve->fpos.s_line,
2434 rve->right.rve->fpos.s_col,
2435 rve->right.rve->fpos.e_line,
2436 rve->right.rve->fpos.e_col);
2439 ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2442 rv1=rval_expr_eval(h, msg, rve->left.rve);
2443 if (unlikely(rv1==0)){
2444 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2445 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2446 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2449 ret=rval_convert(h, msg, RV_STR, rv1, 0);
2453 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2454 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2455 rve->fpos.e_line, rve->fpos.e_col);
2469 /** evals a rval expr and always returns a new rval.
2470 * like rval_expr_eval, but always returns a new rvalue (never a reference
2471 * to an exisiting one).
2472 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2473 * a reference to another rval). The result can be modified only
2474 * if rv_chg_in_place() returns true.
2475 * @result rvalue on success, 0 on error
2477 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2478 struct rval_expr* rve)
2483 ret=rval_expr_eval(h, msg, rve);
2484 if (ret && !rv_chg_in_place(ret)){
2486 /* create a new rv */
2487 ret=rval_new(rv->type, &rv->v, 0);
2495 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2497 * @param rv_type - rval type
2498 * @param val - rval value
2499 * @param pos - config position
2500 * @return new pkg_malloc'ed rval_expr or 0 on error.
2502 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
2503 struct cfg_pos* pos)
2505 struct rval_expr* rve;
2510 rve=pkg_malloc(sizeof(*rve));
2513 memset(rve, 0, sizeof(*rve));
2521 v.s.s=pkg_malloc(s->len+1 /*0*/);
2523 ERR("memory allocation failure\n");
2527 memcpy(v.s.s, s->s, s->len);
2529 flags=RV_CNT_ALLOCED_F;
2532 v.avps=*(avp_spec_t*)val;
2535 v.pvs=*(pv_spec_t*)val;
2538 v.sel=*(select_t*)val;
2541 v.bexpr=(struct expr*)val;
2544 v.action=(struct action*)val;
2547 BUG("unsupported rv type %d\n", rv_type);
2551 rval_init(&rve->left.rval, rv_type, &v, flags);
2552 rve->op=RVE_RVAL_OP;
2553 if (pos) rve->fpos=*pos;
2559 /** create a unary op. rval_expr..
2561 * @param op - rval expr. unary operator
2562 * @param rve1 - rval expr. on which the operator will act.
2563 * @return new pkg_malloc'ed rval_expr or 0 on error.
2565 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2566 struct cfg_pos* pos)
2568 struct rval_expr* ret;
2576 case RVE_STREMPTY_OP:
2577 case RVE_DEFINED_OP:
2582 BUG("unsupported unary operator %d\n", op);
2585 ret=pkg_malloc(sizeof(*ret));
2588 memset(ret, 0, sizeof(*ret));
2591 if (pos) ret->fpos=*pos;
2597 /** create a rval_expr. from 2 other rval exprs, using op.
2598 * ret = rve1 op rve2
2599 * @param op - rval expr. operator
2600 * @param rve1 - rval expr. on which the operator will act.
2601 * @param rve2 - rval expr. on which the operator will act.
2602 * @return new pkg_malloc'ed rval_expr or 0 on error.
2604 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2605 struct rval_expr* rve2,
2606 struct cfg_pos* pos)
2608 struct rval_expr* ret;
2618 case RVE_BLSHIFT_OP:
2619 case RVE_BRSHIFT_OP:
2633 case RVE_STRDIFF_OP:
2638 BUG("unsupported operator %d\n", op);
2641 ret=pkg_malloc(sizeof(*ret));
2644 memset(ret, 0, sizeof(*ret));
2647 ret->right.rve=rve2;
2648 if (pos) ret->fpos=*pos;
2654 /** returns true if the operator is associative. */
2655 static int rve_op_is_assoc(enum rval_expr_op op)
2665 case RVE_STREMPTY_OP:
2666 case RVE_DEFINED_OP:
2669 /* one operand expression => cannot be assoc. */
2674 case RVE_BLSHIFT_OP:
2675 case RVE_BRSHIFT_OP:
2678 /* the generic plus is not assoc, e.g.
2679 "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2700 case RVE_STRDIFF_OP:
2709 /** returns true if the operator is commutative. */
2710 static int rve_op_is_commutative(enum rval_expr_op op)
2720 case RVE_STREMPTY_OP:
2721 case RVE_DEFINED_OP:
2724 /* one operand expression => cannot be commut. */
2729 case RVE_BLSHIFT_OP:
2730 case RVE_BRSHIFT_OP:
2733 /* non commut. when diff. type
2734 (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2735 (specific same type versions are covered by IPLUS & CONCAT) */
2747 case RVE_STRDIFF_OP:
2758 /* non. commut. in general, only for same type e.g.:
2759 "" == 0 diff. 0 == "" ( "" == "0" and 0 == 0)
2760 same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2762 return 0 /* asymmetrical undef handling */;
2769 /** returns true if the rval expr can be optimized to an int.
2770 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2771 * ints return true, else false)
2774 static int rve_can_optimize_int(struct rval_expr* rve)
2778 if (rve->op == RVE_RVAL_OP)
2780 if (rve->left.rve->op != RVE_RVAL_OP)
2782 if (rve->left.rve->left.rval.type!=RV_INT)
2784 if (rve->right.rve){
2785 if (rve->right.rve->op != RVE_RVAL_OP)
2787 if (rve->right.rve->left.rval.type!=RV_INT)
2790 DBG("rve_can_optimize_int: left %d, right %d\n",
2791 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2797 /** returns true if the rval expr can be optimized to a str.
2798 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2799 * str or left is str and right is int return true, else false)
2802 static int rve_can_optimize_str(struct rval_expr* rve)
2806 if (rve->op == RVE_RVAL_OP)
2808 DBG("rve_can_optimize_str: left %d, right %d\n",
2809 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2810 if (rve->left.rve->op != RVE_RVAL_OP)
2812 if (rve->left.rve->left.rval.type!=RV_STR)
2814 if (rve->right.rve){
2815 if (rve->right.rve->op != RVE_RVAL_OP)
2817 if ((rve->right.rve->left.rval.type!=RV_STR) &&
2818 (rve->right.rve->left.rval.type!=RV_INT))
2827 static int fix_rval(struct rvalue* rv)
2829 DBG("RV fixing type %d\n", rv->type);
2833 DBG("RV is int: %d\n", (int)rv->v.l);
2837 DBG("RV is str: \"%s\"\n", rv->v.s.s);
2840 return fix_expr(rv->v.bexpr);
2842 return fix_actions(rv->v.action);
2844 if (resolve_select(&rv->v.sel)<0){
2845 ERR("Unable to resolve select\n");
2846 print_select(&rv->v.sel);
2850 /* nothing to do, resolved at runtime */
2853 /* nothing to do, resolved at parsing time */
2856 BUG("uninitialized rvalue\n");
2859 BUG("unknown rvalue type %d\n", rv->type);
2865 /** helper function: replace a rve (in-place) with a constant rval_val.
2866 * WARNING: since it replaces in-place, one should make sure that if
2867 * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2869 * @param rve - expression to be replaced (in-place)
2870 * @param v - pointer to a rval_val union containing the replacement
2872 * @param flags - value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
2873 * @return 0 on success, -1 on error */
2874 static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2875 union rval_val* v, int flags)
2879 refcnt=1; /* replaced-in-place rval refcnt */
2880 if (rve->op!=RVE_RVAL_OP){
2881 rve_destroy(rve->left.rve);
2882 if (rve_op_unary(rve->op)==0)
2883 rve_destroy(rve->right.rve);
2885 if (rve->left.rval.refcnt!=1){
2886 BUG("trying to replace a referenced rval! (refcnt=%d)\n",
2887 rve->left.rval.refcnt);
2888 /* try to recover */