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){
525 case RVE_STREMPTY_OP:
530 /* '+' evaluates to the type of the left operand */
531 return rve_guess_type(rve->left.rve);
543 /** returns true if expression is constant.
547 int rve_is_constant(struct rval_expr* rve)
551 switch(rve->left.rval.type){
569 case RVE_STREMPTY_OP:
573 return rve_is_constant(rve->left.rve);
596 return rve_is_constant(rve->left.rve) &&
597 rve_is_constant(rve->right.rve);
606 /** returns true if the expression has side-effects.
607 * @return 1 for possible side-effects, 0 for no side-effects
608 * TODO: add better checks
610 int rve_has_side_effects(struct rval_expr* rve)
612 return !rve_is_constant(rve);
617 /** returns true if operator is unary (takes only 1 arg).
620 static int rve_op_unary(enum rval_expr_op op)
623 case RVE_RVAL_OP: /* not realy an operator */
629 case RVE_STREMPTY_OP:
666 /** returns 1 if expression is valid (type-wise).
667 * @param type - filled with the type of the expression (RV_INT, RV_STR or
668 * RV_NONE if it's dynamic)
669 * @param rve - checked expression
670 * @param bad_rve - set on failure to the subexpression for which the
672 * @param bad_type - set on failure to the type of the bad subexpression
673 * @param exp_type - set on failure to the expected type for the bad
675 * @return 0 or 1 and sets *type to the resulting type
676 * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
678 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
679 struct rval_expr** bad_rve,
680 enum rval_type* bad_t,
681 enum rval_type* exp_t)
683 enum rval_type type1, type2;
687 *type=rve_guess_type(rve);
693 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
695 if (bad_rve) *bad_rve=rve->left.rve;
696 if (bad_t) *bad_t=type1;
697 if (exp_t) *exp_t=RV_INT;
720 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
722 if (bad_rve) *bad_rve=rve->left.rve;
723 if (bad_t) *bad_t=type1;
724 if (exp_t) *exp_t=RV_INT;
727 if (rve_check_type(&type2, rve->right.rve, bad_rve,
730 if (bad_rve) *bad_rve=rve->right.rve;
731 if (bad_t) *bad_t=type2;
732 if (exp_t) *exp_t=RV_INT;
742 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
743 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
745 if ((type2!=type1) && (type1!=RV_NONE) &&
747 !(type1==RV_STR && type2==RV_INT)){
748 if (bad_rve) *bad_rve=rve->right.rve;
749 if (bad_t) *bad_t=type2;
750 if (exp_t) *exp_t=type1;
759 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
760 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
762 if ((type2!=type1) && (type1!=RV_NONE) &&
764 !(type1==RV_STR && type2==RV_INT)){
765 if (bad_rve) *bad_rve=rve->right.rve;
766 if (bad_t) *bad_t=type2;
767 if (exp_t) *exp_t=type1;
777 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
778 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
780 if ((type2!=type1) && (type1!=RV_NONE) &&
782 !(type1==RV_STR && type2==RV_INT)){
783 if (bad_rve) *bad_rve=rve->right.rve;
784 if (bad_t) *bad_t=type2;
785 if (exp_t) *exp_t=type1;
789 if (bad_rve) *bad_rve=rve->left.rve;
790 if (bad_t) *bad_t=type1;
791 if (exp_t) *exp_t=RV_STR;
802 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
803 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
805 if ((type2!=type1) && (type1!=RV_NONE) &&
807 !(type1==RV_STR && type2==RV_INT)){
808 if (bad_rve) *bad_rve=rve->right.rve;
809 if (bad_t) *bad_t=type2;
810 if (exp_t) *exp_t=type1;
814 if (bad_rve) *bad_rve=rve->left.rve;
815 if (bad_t) *bad_t=type1;
816 if (exp_t) *exp_t=RV_STR;
824 case RVE_STREMPTY_OP:
827 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
829 if (bad_rve) *bad_rve=rve->left.rve;
830 if (bad_t) *bad_t=type1;
831 if (exp_t) *exp_t=RV_STR;
847 BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
848 rve->fpos.s_line, rve->fpos.s_col,
849 rve->fpos.e_line, rve->fpos.e_col);
850 if (bad_rve) *bad_rve=rve;
851 if (bad_t) *bad_t=RV_NONE;
852 if (exp_t) *exp_t=RV_STR;
860 /** get the integer value of an rvalue.
862 * if rv == undefined select, avp or pvar, return 0.
863 * if an error occurs while evaluating a select, avp or pvar, behave as
864 * for the undefined case (and return success).
865 * @param h - script context handle
866 * @param msg - sip msg
867 * @param i - pointer to int, where the conversion result will be stored
868 * @param rv - rvalue to be converted
869 * @param cache - cached rv value (read-only), can be 0
871 * @return 0 on success, \<0 on error and EXPR_DROP on drop
873 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
874 int* i, struct rvalue* rv,
875 struct rval_cache* cache)
896 *i=eval_expr(h, rv->v.bexpr, msg);
904 *i=(run_actions(h, rv->v.action, msg)>0);
909 r=run_select(&tmp, &rv->v.sel, msg);
913 else /* i>0 => undefined */
919 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
920 if (likely(cache->val_type==RV_INT)){
921 *i=cache->c.avp_val.n;
922 }else if (cache->val_type==RV_STR){
923 s=&cache->c.avp_val.s;
925 }else if (cache->val_type==RV_NONE)
927 else goto error_cache;
929 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
930 &avp_val, rv->v.avps.index);
932 if (unlikely(r_avp->flags & AVP_VAL_STR)){
944 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
945 if (likely((cache->val_type==RV_INT) ||
946 (cache->c.pval.flags & PV_VAL_INT))){
948 }else if (cache->val_type==RV_STR){
951 }else if (cache->val_type==RV_NONE)
953 else goto error_cache;
955 memset(&pval, 0, sizeof(pval));
956 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
957 if (likely(pval.flags & PV_VAL_INT)){
959 pv_value_destroy(&pval);
960 }else if (likely(pval.flags & PV_VAL_STR)){
961 destroy_pval=1; /* we must pv_value_destroy() later*/
965 /* no PV_VAL_STR and no PV_VAL_INT => undef
967 pv_value_destroy(&pval);
976 BUG("rv type %d not handled\n", rv->type);
981 eval_error: /* same as undefined */
982 /* handle undefined => result 0, return success */
986 /* rv is of string type => try to convert it to int */
987 /* if "" => 0 (most likely case) */
988 if (likely(s->len==0)) *i=0;
989 else if (unlikely(str2sint(s, i)!=0)){
990 /* error converting to int => non numeric => 0 */
992 #ifdef RV_STR2INT_VERBOSE_ERR
993 WARN("automatic string to int conversion for \"%.*s\" failed\n",
995 /* return an error code */
997 #ifdef RV_STR2INT_ERR
1002 pv_value_destroy(&pval);
1005 BUG("invalid cached value:cache type %d, value type %d\n",
1006 cache?cache->cache_type:0, cache?cache->val_type:0);
1009 pv_value_destroy(&pval);
1016 /** log a message, appending rve position and a '\n'.*/
1017 #define RVE_LOG(lev, rve, txt) \
1018 LOG((lev), txt " (%d,%d-%d,%d)\n", \
1019 (rve)->fpos.s_line, rve->fpos.s_col, \
1020 (rve)->fpos.e_line, rve->fpos.e_col )
1023 /** macro for checking and handling rval_get_int() retcode.
1024 * check if the return code is an rval_get_int error and if so
1025 * handle the error (e.g. print a log message, ignore the error by
1026 * setting ret to 0 a.s.o.)
1027 * @param ret - retcode as returned by rval_get_int() (might be changed)
1028 * @param txt - warning message txt (no pointer allowed)
1029 * @param rve - rval_expr, used to access the config. pos
1031 #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
1032 #define rval_get_int_handle_ret(ret, txt, rve) \
1034 if (unlikely((ret)<0)) { \
1035 RVE_LOG(L_WARN, rve, txt); \
1039 #elif defined RVAL_GET_INT_ERR_WARN
1040 #define rval_get_int_handle_ret(ret, txt, rve) \
1042 if (unlikely((ret)<0)) \
1043 RVE_LOG(L_WARN, rve, txt); \
1045 #elif defined RVAL_GET_INT_ERR_IGN
1046 #define rval_get_int_handle_ret(ret, txt, rve) \
1048 if (unlikely((ret)<0)) \
1052 #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
1060 /** get the string value of an rv in a tmp variable
1062 * if rv == undefined select, avp or pvar, return "".
1063 * if an error occurs while evaluating a select, avp or pvar, behave as
1064 * for the undefined case (and return success).
1065 * The result points either inside the passed rv, inside
1066 * new_cache or inside an avp. new_cache must be non zero,
1067 * initialized previously and it _must_ be rval_cache_clean(...)'ed when
1069 * WARNING: it's not intended for general use. It might return a pointer
1070 * inside rv so the result _must_ be treated as read-only. rv and new_cache
1071 * must not be released/freed until the result is no longer needed.
1072 * For general use see rval_get_str().
1073 * @param h - script context handle
1074 * @param msg - sip msg
1075 * @param tmpv - str return value (pointer to a str struct that will be
1076 * be filled with the conversion result)
1077 * @param rv - rvalue to be converted
1078 * @param cache - cached rv value (read-only), can be 0
1079 * @param tmp_cache - used for temporary storage (so that tmpv will not
1080 * point to possible freed data), it must be non-null,
1081 * initialized and cleaned afterwards.
1082 * @return 0 on success, <0 on error and EXPR_DROP on drop
1084 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1085 str* tmpv, struct rvalue* rv,
1086 struct rval_cache* cache,
1087 struct rval_cache* tmp_cache)
1094 tmpv->s=sint2strbuf(rv->v.l, tmp_cache->i2s,
1095 sizeof(tmp_cache->i2s), &tmpv->len);
1096 tmp_cache->cache_type = RV_CACHE_INT2STR;
1103 i=(run_actions(h, rv->v.action, msg)>0);
1106 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1107 sizeof(tmp_cache->i2s), &tmpv->len);
1108 tmp_cache->cache_type = RV_CACHE_INT2STR;
1111 i=eval_expr(h, rv->v.bexpr, msg);
1114 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1115 sizeof(tmp_cache->i2s), &tmpv->len);
1116 tmp_cache->cache_type = RV_CACHE_INT2STR;
1119 tmpv->s=sint2strbuf(i, tmp_cache->i2s, sizeof(tmp_cache->i2s),
1121 tmp_cache->cache_type = RV_CACHE_INT2STR;
1124 i=run_select(tmpv, &rv->v.sel, msg);
1125 if (unlikely(i!=0)){
1128 }else { /* i>0 => undefined */
1134 if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1135 if (likely(cache->val_type==RV_STR)){
1136 *tmpv=cache->c.avp_val.s;
1137 }else if (cache->val_type==RV_INT){
1138 i=cache->c.avp_val.n;
1139 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1140 sizeof(tmp_cache->i2s), &tmpv->len);
1141 tmp_cache->cache_type = RV_CACHE_INT2STR;
1142 }else if (cache->val_type==RV_NONE){
1144 }else goto error_cache;
1146 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1147 &tmp_cache->c.avp_val,
1150 if (likely(r_avp->flags & AVP_VAL_STR)){
1151 tmp_cache->cache_type=RV_CACHE_AVP;
1152 tmp_cache->val_type=RV_STR;
1153 *tmpv=tmp_cache->c.avp_val.s;
1155 i=tmp_cache->c.avp_val.n;
1156 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1157 sizeof(tmp_cache->i2s), &tmpv->len);
1158 tmp_cache->cache_type = RV_CACHE_INT2STR;
1164 if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1165 if (likely(cache->val_type==RV_STR)){
1166 *tmpv=cache->c.pval.rs;
1167 }else if (cache->val_type==RV_INT){
1169 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1170 sizeof(tmp_cache->i2s), &tmpv->len);
1171 tmp_cache->cache_type = RV_CACHE_INT2STR;
1172 }else if (cache->val_type==RV_NONE){
1174 }else goto error_cache;
1176 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1177 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1178 &tmp_cache->c.pval)==0)){
1179 if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1180 /* the value is not destroyed, but saved instead
1181 in tmp_cache so that it can be destroyed later
1182 when no longer needed */
1183 tmp_cache->cache_type=RV_CACHE_PVAR;
1184 tmp_cache->val_type=RV_STR;
1185 *tmpv=tmp_cache->c.pval.rs;
1186 }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1187 i=tmp_cache->c.pval.ri;
1188 pv_value_destroy(&tmp_cache->c.pval);
1189 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1190 sizeof(tmp_cache->i2s), &tmpv->len);
1191 tmp_cache->cache_type = RV_CACHE_INT2STR;
1193 /* no PV_VAL_STR and no PV_VAL_INT => undef
1195 pv_value_destroy(&tmp_cache->c.pval);
1204 BUG("rv type %d not handled\n", rv->type);
1209 eval_error: /* same as undefined */
1210 /* handle undefined => result "", return success */
1215 BUG("invalid cached value:cache type %d, value type %d\n",
1216 cache?cache->cache_type:0, cache?cache->val_type:0);
1225 /** get the string value of an rv.
1227 * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1228 * @return 0 on success, <0 on error and EXPR_DROP on drop
1230 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1231 str* s, struct rvalue* rv,
1232 struct rval_cache* cache)
1235 struct rval_cache tmp_cache;
1237 rval_cache_init(&tmp_cache);
1238 if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1240 s->s=pkg_malloc(tmp.len+1/* 0 term */);
1241 if (unlikely(s->s==0)){
1242 ERR("memory allocation error\n");
1246 memcpy(s->s, tmp.s, tmp.len);
1247 s->s[tmp.len]=0; /* 0 term */
1248 rval_cache_clean(&tmp_cache);
1251 rval_cache_clean(&tmp_cache);
1257 /** convert a rvalue to another rvalue, of a specific type.
1259 * The result is read-only in most cases (can be a reference
1260 * to another rvalue, can be checked by using rv_chg_in_place()) and
1261 * _must_ be rval_destroy()'ed.
1263 * @param type - type to convert to
1264 * @param v - rvalue to convert
1265 * @param c - rval_cache (cached v value if known/filled by another
1266 * function), can be 0 (unknown/not needed)
1267 * @return pointer to a rvalue (reference to an existing one or a new
1268 * one, @see rv_chg_in_place() and the above comment) or 0 on error.
1270 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1271 enum rval_type type, struct rvalue* v,
1272 struct rval_cache* c)
1275 struct rval_cache tmp_cache;
1286 if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1289 return rval_new(RV_INT, &val, 0);
1291 rval_cache_init(&tmp_cache);
1292 if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1294 rval_cache_clean(&tmp_cache);
1297 ret=rval_new_str(&tmp, RV_STR_EXTRA);
1298 rval_cache_clean(&tmp_cache);
1302 BUG("unsupported conversion to type %d\n", type);
1310 /** integer operation: *res= op v.
1311 * @return 0 on succes, \<0 on error
1313 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1326 BUG("rv unsupported intop1 %d\n", op);
1334 /** integer operation: *res= v1 op v2
1335 * @return 0 on succes, \<0 on error
1337 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1351 if (unlikely(v2==0)){
1352 ERR("rv div by 0\n");
1358 if (unlikely(v2==0)){
1359 ERR("rv mod by 0\n");
1398 /* invalid operand for int */
1401 BUG("rv unsupported intop %d\n", op);
1409 /** internal helper: compare 2 RV_STR RVs.
1410 * Warning: rv1 & rv2 must be RV_STR
1411 * @return 0 on success, -1 on error
1413 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1414 struct rvalue* rv1, struct rvalue* rv2)
1425 *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1428 case RVE_STRDIFF_OP:
1429 *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1432 if (likely(rv2->flags & RV_RE_F)){
1433 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1435 /* we need to compile the RE on the fly */
1436 if (unlikely(regcomp(&tmp_re, s2->s,
1437 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1439 ERR("Bad regular expression \"%s\"\n", s2->s);
1442 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1447 BUG("rv unsupported intop %d\n", op);
1458 /** integer returning operation on string: *res= op str (returns integer)
1459 * @return 0 on succes, \<0 on error
1461 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1467 case RVE_STREMPTY_OP:
1471 BUG("rv unsupported int_strop1 %d\n", op);
1480 /** integer operation: ret= op v (returns a rvalue).
1481 * @return rvalue on success, 0 on error
1483 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1484 struct sip_msg* msg,
1485 enum rval_expr_op op,
1493 rv2=rval_convert(h, msg, RV_INT, v, 0);
1494 if (unlikely(rv2==0)){
1495 ERR("rval int conversion failed\n");
1498 if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1500 if (rv_chg_in_place(rv2)){
1503 }else if (rv_chg_in_place(v)){
1507 ret=rval_new(RV_INT, &rv2->v, 0);
1508 if (unlikely(ret==0)){
1509 ERR("eval out of memory\n");
1523 /** integer operation: ret= l op r (returns a rvalue).
1524 * @return rvalue on success, 0 on error
1526 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1527 struct sip_msg* msg,
1528 enum rval_expr_op op,
1539 if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1541 if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1543 if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1545 if (rv_chg_in_place(rv1)){
1546 /* try reusing rv1 */
1549 }else if (rv_chg_in_place(rv2)){
1550 /* try reusing rv2 */
1553 }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1556 } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1560 ret=rval_new(RV_INT, &rv1->v, 0);
1561 if (unlikely(ret==0)){
1562 ERR("rv eval out of memory\n");
1578 /** string add operation: ret= l . r (returns a rvalue).
1579 * Can use cached rvalues (c1 & c2).
1580 * @return rvalue on success, 0 on error
1582 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1583 struct sip_msg* msg,
1585 struct rval_cache* c1,
1587 struct rval_cache* c2
1604 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1606 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1609 len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1611 if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1612 /* try reusing rv1 */
1616 if (ret->v.s.s == &ret->buf[0]) s1=0;
1620 ret->flags &= ~RV_CNT_ALLOCED_F;
1621 ret->v.s.s=&ret->buf[0];
1625 }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1626 /* try reusing rv2 */
1630 if (ret->v.s.s == &ret->buf[0])
1635 ret->flags &= ~RV_CNT_ALLOCED_F;
1636 ret->v.s.s=&ret->buf[0];
1640 }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1644 if (ret->v.s.s == &ret->buf[0]) s1=0;
1648 ret->flags &= ~RV_CNT_ALLOCED_F;
1649 ret->v.s.s=&ret->buf[0];
1653 } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1657 if (ret->v.s.s == &ret->buf[0])
1662 ret->flags &= ~RV_CNT_ALLOCED_F;
1663 ret->v.s.s=&ret->buf[0];
1668 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1669 if (unlikely(ret==0)){
1670 ERR("rv eval out of memory\n");
1676 /* do the actual copy */
1677 memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1679 memcpy(ret->buf, s1->s, s1->len);
1681 ret->v.s.len=rv1->v.s.len+s2->len;
1682 ret->v.s.s[ret->v.s.len]=0;
1683 /* cleanup if needed */
1684 if (flags & RV_CNT_ALLOCED_F)
1697 /** bool operation on rval evaluated as strings.
1698 * Can use cached rvalues (c1 & c2).
1699 * @return 0 success, -1 on error
1701 inline static int rval_str_lop2(struct run_act_ctx* h,
1702 struct sip_msg* msg,
1704 enum rval_expr_op op,
1706 struct rval_cache* c1,
1708 struct rval_cache* c2)
1716 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1718 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1720 ret=bool_rvstrop2(op, res, rv1, rv2);
1732 /** integer operation on rval evaluated as string.
1733 * Can use cached rvalues (c1 & c2).
1734 * @param res - will be set to the result
1735 * @return 0 success, -1 on error
1737 inline static int rval_int_strop1(struct run_act_ctx* h,
1738 struct sip_msg* msg,
1740 enum rval_expr_op op,
1742 struct rval_cache* c1)
1749 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1751 ret=int_strop1(res, op, &rv1->v.s);
1762 /** checks if rv is defined.
1763 * @param res - set to the result 1 - defined, 0 not defined
1764 * @return 0 on success, -1 on error
1765 * Can use cached rvalues (cache).
1766 * Note: a rv can be undefined if it's an undefined avp or pvar or select or
1768 * Note2: an error in the avp, pvar or select search is equivalent to
1769 * undefined (and it's not reported)
1771 inline static int rv_defined(struct run_act_ctx* h,
1772 struct sip_msg* msg, int* res,
1773 struct rvalue* rv, struct rval_cache* cache)
1783 if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1784 *res=(cache->val_type!=RV_NONE);
1786 /* run select returns 0 on success, -1 on error and >0 on
1787 undefined. error is considered undefined */
1788 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1791 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1792 *res=(cache->val_type!=RV_NONE);
1794 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1795 &avp_val, rv->v.avps.index);
1796 if (unlikely(r_avp==0)){
1802 /* PV_VAL_NULL or pv_get_spec_value error => undef */
1803 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1804 *res=(cache->val_type!=RV_NONE);
1806 memset(&pval, 0, sizeof(pval));
1807 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1808 if ((pval.flags & PV_VAL_NULL) &&
1809 ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1812 pv_value_destroy(&pval);
1814 *res=0; /* in case of error, consider it undef */
1828 /** defined (integer) operation on rve.
1829 * @param res - set to 1 defined, 0 not defined
1830 * @return - 0 on success, -1 on error
1832 inline static int int_rve_defined(struct run_act_ctx* h,
1833 struct sip_msg* msg, int* res,
1834 struct rval_expr* rve)
1836 /* only a rval can be undefined, any expression consisting on more
1837 then one rval => defined */
1838 if (likely(rve->op==RVE_RVAL_OP))
1839 return rv_defined(h, msg, res, &rve->left.rval, 0);
1846 /** evals an integer expr to an int.
1848 * *res=(int)eval(rve)
1849 * @return 0 on success, \<0 on error
1851 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1852 int* res, struct rval_expr* rve)
1855 struct rval_cache c1, c2;
1862 ret=rval_get_int(h, msg, res, &rve->left.rval, 0);
1863 rval_get_int_handle_ret(ret, "rval expression conversion to int"
1870 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1872 ret=int_intop1(res, rve->op, i1);
1875 ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1892 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1895 (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1897 ret=int_intop2(res, rve->op, i1, i2);
1901 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1906 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1907 rve->right.rve)) <0) )
1915 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1920 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1921 rve->right.rve)) <0) )
1929 /* if left is string, eval left & right as string and
1931 * if left is int eval as int using int diff
1932 * if left is undef, look at right and convert to right type
1934 rval_cache_init(&c1);
1935 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1936 rve->left.rve, &c1))<0)){
1938 rval_cache_clean(&c1);
1941 if (likely(rv1==0)){
1943 rval_cache_clean(&c1);
1944 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1945 rve->right.rve)) <0) )
1947 ret=int_intop2(res, rve->op, i1, i2);
1949 /* not int => str or undef */
1950 /* check for undefined left operand */
1951 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
1952 c1.val_type==RV_NONE)){
1953 #ifdef UNDEF_EQ_ALWAYS_FALSE
1954 /* undef == something always false
1955 undef != something always true*/
1956 ret=(rve->op==RVE_DIFF_OP);
1957 #elif defined UNDEF_EQ_UNDEF_TRUE
1958 /* undef == something defined always false
1959 undef == undef true */
1960 if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
1962 rval_cache_clean(&c1);
1966 ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
1967 #else /* ! UNDEF_EQ_* */
1969 * => convert to (type_of(val)) (undef) == val */
1970 rval_cache_init(&c2);
1971 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
1972 rve->right.rve, &c2))<0)){
1974 rval_cache_clean(&c1);
1975 rval_cache_clean(&c2);
1981 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
1984 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
1986 rval_cache_clean(&c2);
1989 #endif /* UNDEF_EQ_* */
1990 rval_cache_clean(&c1);
1993 /* left value == defined and != int => str
1994 * => lval == (str) val */
1995 if (unlikely((rv2=rval_expr_eval(h, msg,
1996 rve->right.rve))==0)){
1999 rval_cache_clean(&c1);
2002 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
2003 rval_cache_clean(&c1);
2010 /* eval expression => string */
2011 if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
2015 /* convert to int */
2016 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2017 rval_get_int_handle_ret(ret, "rval expression conversion to int"
2022 /* (str)expr => eval expression */
2023 rval_cache_init(&c1);
2024 if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2025 rve->left.rve, &c1))<0)){
2027 rval_cache_clean(&c1);
2031 /* expr evaluated to string => (int)(str)v == (int)v */
2032 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2033 rval_get_int_handle_ret(ret, "rval expression conversion"
2034 " to int failed", rve);
2036 rval_cache_clean(&c1);
2038 => expr evaluated to int =>
2039 return (int)(str)v == (int)v => do nothing */
2043 /* same thing as above, but in a not optimized, easier to
2045 /* 1. (str) expr => eval expr */
2046 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2050 /* 2. convert to str and then convert to int
2051 but since (int)(str)v == (int)v skip over (str)v */
2052 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2056 case RVE_DEFINED_OP:
2057 ret=int_rve_defined(h, msg, res, rve->left.rve);
2060 case RVE_STRDIFF_OP:
2062 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2066 if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2071 ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2076 case RVE_STREMPTY_OP:
2077 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2081 ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2086 BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2087 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2088 rve->fpos.e_line, rve->fpos.e_col);
2096 /** evals a rval expr. into an int or another rv(str).
2097 * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
2098 * (it might be a reference to another rval). The result can be
2099 * modified only if rv_chg_in_place() returns true.
2100 * @param res_rv - pointer to rvalue result, if non-null it means the
2101 * expression evaluated to a non-int (str), which will be
2103 * @param res_i - pointer to int result, if res_rv==0 and the function
2104 * returns success => the result is an int which will be
2106 * @param rve - expression that will be evaluated.
2107 * @param cache - write-only value cache, it might be filled if non-null and
2108 * empty (rval_cache_init()). If non-null, it _must_ be
2109 * rval_cache_clean()'ed when done.
2111 * @result 0 on success, -1 on error, sets *res_rv or *res_i.
2113 int rval_expr_eval_rvint( struct run_act_ctx* h,
2114 struct sip_msg* msg,
2115 struct rvalue** res_rv,
2117 struct rval_expr* rve,
2118 struct rval_cache* cache
2123 struct rval_cache c1; /* local cache */
2126 enum rval_type type;
2133 rv1=&rve->left.rval;
2135 type=rval_get_btype(h, msg, rv1, cache);
2137 r=rval_get_int(h, msg, res_i, rv1, cache);
2138 rval_get_int_handle_ret(r, "rval expression conversion"
2139 " to int failed", rve);
2141 ret=r; /* equiv. to if (r<0) goto error */
2143 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the
2144 cached resolved value in cache*/
2171 case RVE_STRDIFF_OP:
2174 case RVE_STREMPTY_OP:
2175 case RVE_DEFINED_OP:
2177 /* operator forces integer type */
2178 ret=rval_expr_eval_int(h, msg, res_i, rve);
2182 rval_cache_init(&c1);
2183 r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2185 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2186 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2187 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2189 rval_cache_clean(&c1);
2193 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2194 rve->right.rve))<0)){
2195 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2196 "\n", rve->right.rve->fpos.s_line,
2197 rve->right.rve->fpos.s_col,
2198 rve->right.rve->fpos.e_line,
2199 rve->right.rve->fpos.e_col);
2200 rval_cache_clean(&c1);
2203 ret=int_intop2(res_i, rve->op, i, j);
2206 rv2=rval_expr_eval(h, msg, rve->right.rve);
2207 if (unlikely(rv2==0)){
2208 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2209 rve->right.rve->fpos.s_line,
2210 rve->right.rve->fpos.s_col,
2211 rve->right.rve->fpos.e_line,
2212 rve->right.rve->fpos.e_col);
2213 rval_cache_clean(&c1);
2216 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2219 rval_cache_clean(&c1);
2223 *res_rv=rval_expr_eval(h, msg, rve);
2228 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2229 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2230 rve->fpos.e_line, rve->fpos.e_col);
2244 /** evals a rval expr..
2245 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2246 * a reference to another rval). The result can be modified only
2247 * if rv_chg_in_place() returns true.
2248 * @return rvalue on success, 0 on error
2250 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2251 struct rval_expr* rve)
2256 struct rval_cache c1;
2259 enum rval_type type;
2266 rv_ref(&rve->left.rval);
2267 return &rve->left.rval;
2290 case RVE_STRDIFF_OP:
2293 case RVE_STREMPTY_OP:
2294 case RVE_DEFINED_OP:
2296 /* operator forces integer type */
2297 r=rval_expr_eval_int(h, msg, &i, rve);
2300 ret=rval_new(RV_INT, &v, 0);
2301 if (unlikely(ret==0)){
2302 ERR("rv eval int expression: out of memory\n");
2307 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2308 rve->fpos.s_line, rve->fpos.s_col,
2309 rve->fpos.e_line, rve->fpos.e_col);
2314 rv1=rval_expr_eval(h, msg, rve->left.rve);
2315 if (unlikely(rv1==0)){
2316 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2317 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2318 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2321 rval_cache_init(&c1);
2322 type=rval_get_btype(h, msg, rv1, &c1);
2325 r=rval_get_int(h, msg, &i, rv1, &c1);
2326 rval_get_int_handle_ret(r, "rval expression left side "
2327 "conversion to int failed",
2330 rval_cache_clean(&c1);
2333 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2334 rve->right.rve))<0)){
2335 rval_cache_clean(&c1);
2336 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2337 " could not evaluate right side to int\n",
2338 rve->fpos.s_line, rve->fpos.s_col,
2339 rve->fpos.e_line, rve->fpos.e_col);
2342 int_intop2(&r, rve->op, i, j);
2343 if (rv_chg_in_place(rv1)){
2349 ret=rval_new(RV_INT, &v, 0);
2350 if (unlikely(ret==0)){
2351 rval_cache_clean(&c1);
2352 ERR("rv eval int expression: out of memory\n");
2359 rv2=rval_expr_eval(h, msg, rve->right.rve);
2360 if (unlikely(rv2==0)){
2361 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2362 "\n", rve->right.rve->fpos.s_line,
2363 rve->right.rve->fpos.s_col,
2364 rve->right.rve->fpos.e_line,
2365 rve->right.rve->fpos.e_col);
2366 rval_cache_clean(&c1);
2369 ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2372 BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2373 rve->fpos.s_line, rve->fpos.s_col,
2374 rve->fpos.e_line, rve->fpos.e_col);
2376 rval_cache_clean(&c1);
2379 rv1=rval_expr_eval(h, msg, rve->left.rve);
2380 if (unlikely(rv1==0)){
2381 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2382 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2383 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2386 rv2=rval_expr_eval(h, msg, rve->right.rve);
2387 if (unlikely(rv2==0)){
2388 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2389 rve->right.rve->fpos.s_line,
2390 rve->right.rve->fpos.s_col,
2391 rve->right.rve->fpos.e_line,
2392 rve->right.rve->fpos.e_col);
2395 ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2398 rv1=rval_expr_eval(h, msg, rve->left.rve);
2399 if (unlikely(rv1==0)){
2400 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2401 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2402 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2405 ret=rval_convert(h, msg, RV_STR, rv1, 0);
2409 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2410 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2411 rve->fpos.e_line, rve->fpos.e_col);
2425 /** evals a rval expr and always returns a new rval.
2426 * like rval_expr_eval, but always returns a new rvalue (never a reference
2427 * to an exisiting one).
2428 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2429 * a reference to another rval). The result can be modified only
2430 * if rv_chg_in_place() returns true.
2431 * @result rvalue on success, 0 on error
2433 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2434 struct rval_expr* rve)
2439 ret=rval_expr_eval(h, msg, rve);
2440 if (ret && !rv_chg_in_place(ret)){
2442 /* create a new rv */
2443 ret=rval_new(rv->type, &rv->v, 0);
2451 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2453 * @param rv_type - rval type
2454 * @param val - rval value
2455 * @param pos - config position
2456 * @return new pkg_malloc'ed rval_expr or 0 on error.
2458 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
2459 struct cfg_pos* pos)
2461 struct rval_expr* rve;
2466 rve=pkg_malloc(sizeof(*rve));
2469 memset(rve, sizeof(*rve), 0);
2477 v.s.s=pkg_malloc(s->len+1 /*0*/);
2479 ERR("memory allocation failure\n");
2483 memcpy(v.s.s, s->s, s->len);
2485 flags=RV_CNT_ALLOCED_F;
2488 v.avps=*(avp_spec_t*)val;
2491 v.pvs=*(pv_spec_t*)val;
2494 v.sel=*(select_t*)val;
2497 v.bexpr=(struct expr*)val;
2500 v.action=(struct action*)val;
2503 BUG("unsupported rv type %d\n", rv_type);
2507 rval_init(&rve->left.rval, rv_type, &v, flags);
2508 rve->op=RVE_RVAL_OP;
2509 if (pos) rve->fpos=*pos;
2515 /** create a unary op. rval_expr..
2517 * @param op - rval expr. unary operator
2518 * @param rve1 - rval expr. on which the operator will act.
2519 * @return new pkg_malloc'ed rval_expr or 0 on error.
2521 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2522 struct cfg_pos* pos)
2524 struct rval_expr* ret;
2531 case RVE_STREMPTY_OP:
2532 case RVE_DEFINED_OP:
2537 BUG("unsupported unary operator %d\n", op);
2540 ret=pkg_malloc(sizeof(*ret));
2543 memset(ret, sizeof(*ret), 0);
2546 if (pos) ret->fpos=*pos;
2552 /** create a rval_expr. from 2 other rval exprs, using op.
2553 * ret = rve1 op rve2
2554 * @param op - rval expr. operator
2555 * @param rve1 - rval expr. on which the operator will act.
2556 * @param rve2 - rval expr. on which the operator will act.
2557 * @return new pkg_malloc'ed rval_expr or 0 on error.
2559 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2560 struct rval_expr* rve2,
2561 struct cfg_pos* pos)
2563 struct rval_expr* ret;
2585 case RVE_STRDIFF_OP:
2590 BUG("unsupported operator %d\n", op);
2593 ret=pkg_malloc(sizeof(*ret));
2596 memset(ret, sizeof(*ret), 0);
2599 ret->right.rve=rve2;
2600 if (pos) ret->fpos=*pos;
2606 /** returns true if the operator is associative. */
2607 static int rve_op_is_assoc(enum rval_expr_op op)
2616 case RVE_STREMPTY_OP:
2617 case RVE_DEFINED_OP:
2620 /* one operand expression => cannot be assoc. */
2627 /* the generic plus is not assoc, e.g.
2628 "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2648 case RVE_STRDIFF_OP:
2657 /** returns true if the operator is commutative. */
2658 static int rve_op_is_commutative(enum rval_expr_op op)
2667 case RVE_STREMPTY_OP:
2668 case RVE_DEFINED_OP:
2671 /* one operand expression => cannot be commut. */
2678 /* non commut. when diff. type
2679 (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2680 (specific same type versions are covered by IPLUS & CONCAT) */
2691 case RVE_STRDIFF_OP:
2702 /* non. commut. in general, only for same type e.g.:
2703 "" == 0 diff. 0 == "" ( "" == "0" and 0 == 0)
2704 same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2706 return 0 /* asymmetrical undef handling */;
2713 /** returns true if the rval expr can be optimized to an int.
2714 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2715 * ints return true, else false)
2718 static int rve_can_optimize_int(struct rval_expr* rve)
2722 if (rve->op == RVE_RVAL_OP)
2724 if (rve->left.rve->op != RVE_RVAL_OP)
2726 if (rve->left.rve->left.rval.type!=RV_INT)
2728 if (rve->right.rve){
2729 if (rve->right.rve->op != RVE_RVAL_OP)
2731 if (rve->right.rve->left.rval.type!=RV_INT)
2734 DBG("rve_can_optimize_int: left %d, right %d\n",
2735 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2741 /** returns true if the rval expr can be optimized to a str.
2742 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2743 * str or left is str and right is int return true, else false)
2746 static int rve_can_optimize_str(struct rval_expr* rve)
2750 if (rve->op == RVE_RVAL_OP)
2752 DBG("rve_can_optimize_str: left %d, right %d\n",
2753 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2754 if (rve->left.rve->op != RVE_RVAL_OP)
2756 if (rve->left.rve->left.rval.type!=RV_STR)
2758 if (rve->right.rve){
2759 if (rve->right.rve->op != RVE_RVAL_OP)
2761 if ((rve->right.rve->left.rval.type!=RV_STR) &&
2762 (rve->right.rve->left.rval.type!=RV_INT))
2771 static int fix_rval(struct rvalue* rv)
2773 DBG("RV fixing type %d\n", rv->type);
2777 DBG("RV is int: %d\n", (int)rv->v.l);
2781 DBG("RV is str: \"%s\"\n", rv->v.s.s);
2784 return fix_expr(rv->v.bexpr);
2786 return fix_actions(rv->v.action);
2788 if (resolve_select(&rv->v.sel)<0){
2789 BUG("Unable to resolve select\n");
2790 print_select(&rv->v.sel);
2794 /* nothing to do, resolved at runtime */
2797 /* nothing to do, resolved at parsing time */
2800 BUG("uninitialized rvalue\n");
2803 BUG("unknown rvalue type %d\n", rv->type);
2809 /** helper function: replace a rve (in-place) with a constant rval_val.
2810 * WARNING: since it replaces in-place, one should make sure that if
2811 * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2813 * @param rve - expression to be replaced (in-place)
2814 * @param v - pointer to a rval_val union containing the replacement
2816 * @param flags - value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
2817 * @return 0 on success, -1 on error */
2818 static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2819 union rval_val* v, int flags)
2823 refcnt=1; /* replaced-in-place rval refcnt */
2824 if (rve->op!=RVE_RVAL_OP){
2825 rve_destroy(rve->left.rve);
2826 if (rve_op_unary(rve->op)==0)
2827 rve_destroy(rve->right.rve);
2829 if (rve->left.rval.refcnt!=1){
2830 BUG("trying to replace a referenced rval! (refcnt=%d)\n",
2831 rve->left.rval.refcnt);
2832 /* try to recover */
2833 refcnt=rve->left.rval.refcnt;
2834 abort(); /* find bugs quicker -- andrei */
2836 rval_destroy(&rve->left.rval);
2838 rval_init(&rve->left.rval, type, v, flags);
2839 rve->left.rval.refcnt=refcnt;
2840 rval_init(&rve->right.rval, RV_NONE, 0, 0);
2841 rve->op=RVE_RVAL_OP;
2847 /** helper function: replace a rve (in-place) with a constant rvalue.
2848 * @param rve - expression to be replaced (in-place)
2849 * @param rv - pointer to the replacement _constant_ rvalue structure
2850 * @return 0 on success, -1 on error */
2851 static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
2853 enum rval_type type;
2860 if (rv->type==RV_INT){
2861 if (rval_get_int(0, 0, &i, rv, 0)!=0){
2862 BUG("unexpected int evaluation failure (%d,%d-%d,%d)\n",
2863 rve->fpos.s_line, rve->fpos.s_col,
2864 rve->fpos.e_line, rve->fpos.e_col);
2868 }else if(rv->type==RV_STR){
2869 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2870 BUG("unexpected str evaluation failure(%d,%d-%d,%d)\n",
2871 rve->fpos.s_line, rve->fpos.s_col,
2872 rve->fpos.e_line, rve->fpos.e_col);
2875 flags|=RV_CNT_ALLOCED_F;
2877 BUG("unknown constant expression type %d (%d,%d-%d,%d)\n", rv->type,
2878 rve->fpos.s_line, rve->fpos.s_col,
2879 rve->fpos.e_line, rve->fpos.e_col);
2882 return rve_replace_with_val(rve, type, &v, flags);
2887 /** try to replace the right side of the rve with a compiled regex.
2888 * @return 0 on success and -1 on error.
2890 static int fix_match_rve(struct rval_expr* rve)