rvalue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2008 iptelorg GmbH
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /**
  17. * @file
  18. * @brief Kamailio core :: rvalue expressions
  19. * @ingroup core
  20. * Module: \ref core
  21. * @author andrei
  22. */
  23. #ifndef _rvalue_h_
  24. #define _rvalue_h_
  25. #include "str.h"
  26. #include "ut.h"
  27. #include "usr_avp.h"
  28. #include "select.h"
  29. #include "pvar.h"
  30. #include "route.h"
  31. #include "parser/msg_parser.h"
  32. #include "action.h"
  33. enum rval_type{
  34. RV_NONE, RV_INT, RV_STR, /* basic types */
  35. RV_BEXPR, RV_ACTION_ST, /* special values */
  36. RV_SEL, RV_AVP, RV_PVAR
  37. };
  38. enum rval_expr_op{
  39. RVE_NONE_OP, /**< uninit / empty */
  40. RVE_RVAL_OP, /**< special op, means that the expr. is in fact a rval */
  41. RVE_UMINUS_OP, /**< one member expression, returns -(val) */
  42. RVE_BOOL_OP, /**< one member evaluate as bool. : (val!=0)*/
  43. RVE_LNOT_OP, /**< one member evaluate as bool. : (!val)*/
  44. RVE_BNOT_OP, /**< one member evaluate as binary : (~ val)*/
  45. RVE_MUL_OP, /**< 2 members, returns left * right */
  46. RVE_DIV_OP, /**< 2 members, returns left / right */
  47. RVE_MOD_OP, /**< 2 members, returns left % right */
  48. RVE_MINUS_OP, /**< 2 members, returns left - right */
  49. RVE_BAND_OP, /**< 2 members, returns left | right */
  50. RVE_BOR_OP, /**< 2 members, returns left & right */
  51. RVE_BXOR_OP, /**< 2 members, returns left XOR right */
  52. RVE_BLSHIFT_OP, /**< 2 members, returns left << right */
  53. RVE_BRSHIFT_OP, /**< 2 members, returns left >> right */
  54. RVE_LAND_OP, /**< 2 members, returns left && right */
  55. RVE_LOR_OP, /**< 2 members, returns left || right */
  56. RVE_GT_OP, /**< 2 members, returns left > right */
  57. RVE_GTE_OP, /**< 2 members, returns left >= right */
  58. RVE_LT_OP, /**< 2 members, returns left < right */
  59. RVE_LTE_OP, /**< 2 members, returns left <= right */
  60. RVE_IEQ_OP, /**< 2 members, int == version, returns left == right */
  61. RVE_IDIFF_OP, /**< 2 members, int != version, returns left != right */
  62. RVE_IPLUS_OP, /**< 2 members, integer +, returns int(a)+int(b) */
  63. /* common int & str */
  64. RVE_PLUS_OP, /**< generic plus (int or str) returns left + right */
  65. RVE_EQ_OP, /**< 2 members, returns left == right (int)*/
  66. RVE_DIFF_OP, /**< 2 members, returns left != right (int)*/
  67. /* str only */
  68. RVE_CONCAT_OP, /**< 2 members, string concat, returns left . right (str)*/
  69. RVE_STRLEN_OP, /**< one member, string length:, returns strlen(val) (int)*/
  70. RVE_STREMPTY_OP, /**< one member, returns val=="" (bool) */
  71. RVE_STREQ_OP, /**< 2 members, string == , returns left == right (bool)*/
  72. RVE_STRDIFF_OP,/**< 2 members, string != , returns left != right (bool)*/
  73. RVE_MATCH_OP, /**< 2 members, string ~), returns left matches re(right) */
  74. /* avp, pvars a.s.o */
  75. RVE_DEFINED_OP, /**< one member, returns is_defined(val) (bool) */
  76. RVE_NOTDEFINED_OP, /**< one member, returns is_not_defined(val) (bool) */
  77. RVE_INT_OP, /**< one member, returns (int)val (int) */
  78. RVE_STR_OP /**< one member, returns (str)val (str) */
  79. };
  80. struct str_re{
  81. str s;
  82. regex_t* regex;
  83. };
  84. union rval_val{
  85. void* p;
  86. long l;
  87. str s;
  88. avp_spec_t avps;
  89. select_t sel;
  90. pv_spec_t pvs;
  91. struct action* action;
  92. struct expr* bexpr;
  93. struct str_re re;
  94. };
  95. struct rvalue{
  96. enum rval_type type;
  97. int refcnt; /**< refcnt, on 0 the structure is destroyed */
  98. union rval_val v;
  99. int bsize; /**< extra data size */
  100. short flags;
  101. char buf[1]; /**< extra data, like string contents can be stored here */
  102. };
  103. /* rvalue flags */
  104. #define RV_CNT_ALLOCED_F 1 /**< free contents (pkg mem allocated) */
  105. #define RV_RV_ALLOCED_F 2 /**< free rv itself (pkg_free(rv)) */
  106. #define RV_ALL_ALLOCED_F (RV_CNT_ALLOCED|RV_RV_ALLOCED)
  107. #define RV_RE_F 4 /**< string is a RE with a valid v->re member */
  108. #define RV_RE_ALLOCED_F 8 /**< v->re.regex must be freed */
  109. struct rval_expr{
  110. enum rval_expr_op op;
  111. union{
  112. struct rval_expr* rve;
  113. struct rvalue rval;
  114. }left;
  115. union{
  116. struct rval_expr* rve;
  117. struct rvalue rval;
  118. }right;
  119. struct cfg_pos fpos;
  120. };
  121. enum rval_cache_type{
  122. RV_CACHE_EMPTY,
  123. RV_CACHE_PVAR,
  124. RV_CACHE_AVP,
  125. RV_CACHE_SELECT,
  126. RV_CACHE_INT2STR
  127. };
  128. /** value cache for a rvalue struct.
  129. * Used to optimize functions that would need to
  130. * get the value repeatedly (e.g. rval_get_btype() and then rval_get_int())
  131. */
  132. struct rval_cache{
  133. enum rval_cache_type cache_type;
  134. enum rval_type val_type;
  135. union{
  136. int_str avp_val; /**< avp value */
  137. pv_value_t pval; /**< pvar value */
  138. }c;
  139. char i2s[INT2STR_MAX_LEN]; /**< space for converting an int to string*/
  140. };
  141. /** allocates a new rval (should be freed by rval_destroy()). */
  142. struct rvalue* rval_new_empty(int extra_size);
  143. struct rvalue* rval_new_str(str* s, int extra_size);
  144. /**
  145. * @brief create a new pk_malloc'ed rvalue from a rval_val union
  146. * @param t rvalue type
  147. * @param v rvalue value
  148. * @param extra_size extra space to allocate
  149. * (so that future string operation can reuse the space)
  150. * @return new rv or 0 on error
  151. */
  152. struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size);
  153. /** inits a rvalue structure- */
  154. void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
  155. int flags);
  156. /** frees a rval_new(), rval_convert() or rval_expr_eval() returned rval. */
  157. void rval_destroy(struct rvalue* rv);
  158. /** frees a rval contents */
  159. void rval_clean(struct rvalue* rv);
  160. /** init a rval_cache struct */
  161. #define rval_cache_init(rvc) \
  162. do{ (rvc)->cache_type=RV_CACHE_EMPTY; (rvc)->val_type=RV_NONE; }while(0)
  163. /** destroy a rval_cache struct contents */
  164. void rval_cache_clean(struct rval_cache* rvc);
  165. /**
  166. * @brief Convert a rvalue to another rvalue, of a specific type
  167. *
  168. * Convert a rvalue to another rvalue, of a specific type.
  169. * The result is read-only in most cases (can be a reference
  170. * to another rvalue, can be checked by using rv_chg_in_place()) and
  171. * _must_ be rval_destroy()'ed.
  172. *
  173. * @param h run action context
  174. * @param msg SIP mesasge
  175. * @param type - type to convert to
  176. * @param v - rvalue to convert
  177. * @param c - rval_cache (cached v value if known/filled by another
  178. * function), can be 0 (unknown/not needed)
  179. * @return pointer to a rvalue (reference to an existing one or a new
  180. * one, @see rv_chg_in_place() and the above comment), or 0 on error.
  181. */
  182. struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
  183. enum rval_type type, struct rvalue* v,
  184. struct rval_cache* c);
  185. /** get the integer value of an rvalue. */
  186. int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg, int* i,
  187. struct rvalue* rv, struct rval_cache* cache);
  188. /** get the string value of an rv. */
  189. int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
  190. str* s, struct rvalue* rv,
  191. struct rval_cache* cache);
  192. /** get the string value of an rv in a tmp variable */
  193. int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
  194. str* tmpv, struct rvalue* rv,
  195. struct rval_cache* cache,
  196. struct rval_cache* tmp_cache);
  197. /** evals an integer expr to an int. */
  198. int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
  199. int* res, struct rval_expr* rve);
  200. /**
  201. * @brief Evals a rval expression
  202. * @warning result must be rval_destroy()'ed if non-null (it might be
  203. * a reference to another rval). The result can be modified only
  204. * if rv_chg_in_place() returns true.
  205. * @param h run action context
  206. * @param msg SIP message
  207. * @param rve rvalue expression
  208. * @return rvalue on success, 0 on error
  209. */
  210. struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
  211. struct rval_expr* rve);
  212. /**
  213. * @brief Evals a rval expression into an int or another rv(str)
  214. * @warning rv result (rv_res) must be rval_destroy()'ed if non-null
  215. * (it might be a reference to another rval). The result can be
  216. * modified only if rv_chg_in_place() returns true.
  217. * @param h run action context
  218. * @param msg SIP message
  219. * @param res_rv pointer to rvalue result, if non-null it means the
  220. * expression evaluated to a non-int (str), which will be stored here.
  221. * @param res_i pointer to int result, if res_rv==0 and the function
  222. * returns success => the result is an int which will be stored here.
  223. * @param rve expression that will be evaluated.
  224. * @param cache write-only value cache, it might be filled if non-null and
  225. * empty (rval_cache_init()). If non-null, it _must_ be rval_cache_clean()'ed
  226. * when done.
  227. * @return 0 on success, -1 on error, sets *res_rv or *res_i.
  228. */
  229. int rval_expr_eval_rvint( struct run_act_ctx* h, struct sip_msg* msg,
  230. struct rvalue** rv_res, int* i_res,
  231. struct rval_expr* rve, struct rval_cache* cache);
  232. /** guess the type of an expression. */
  233. enum rval_type rve_guess_type(struct rval_expr* rve);
  234. /** returns true if expression is constant. */
  235. int rve_is_constant(struct rval_expr* rve);
  236. /** returns true if the expression can have side-effect */
  237. int rve_has_side_effects(struct rval_expr* rve);
  238. /**
  239. * @brief Returns 1 if expression is valid (type-wise)
  240. * @param type filled with the type of the expression (RV_INT, RV_STR or
  241. * RV_NONE if it's dynamic)
  242. * @param rve checked expression
  243. * @param bad_rve set on failure to the subexpression for which the
  244. * type check failed
  245. * @param bad_t set on failure to the type of the bad subexpression
  246. * @param exp_t set on failure to the expected type for the bad
  247. * subexpression
  248. * @return 0 or 1 and sets *type to the resulting type
  249. * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
  250. */
  251. int rve_check_type(enum rval_type* type, struct rval_expr* rve,
  252. struct rval_expr** bad_rve, enum rval_type* bad_type,
  253. enum rval_type* exp_type);
  254. /** returns a string name for type (debugging).*/
  255. char* rval_type_name(enum rval_type type);
  256. /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type
  257. */
  258. struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
  259. struct cfg_pos* pos);
  260. /**
  261. * @brief Create a unary op. rval_expr
  262. * ret= op rve1
  263. * @param op - rval expr. unary operator
  264. * @param rve1 - rval expr. on which the operator will act.
  265. * @param pos configuration position
  266. * @return new pkg_malloc'ed rval_expr or 0 on error.
  267. */
  268. struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
  269. struct cfg_pos* pos);
  270. /**
  271. * @brief Create a rval_expr. from 2 other rval exprs, using op
  272. * ret = rve1 op rve2
  273. * @param op - rval expr. operator
  274. * @param rve1 - rval expr. on which the operator will act.
  275. * @param rve2 - rval expr. on which the operator will act.
  276. * @param pos configuration position
  277. * @return new pkg_malloc'ed rval_expr or 0 on error.
  278. */
  279. struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
  280. struct rval_expr* rve2,
  281. struct cfg_pos* pos);
  282. /** destroys a pkg_malloc'ed rve. */
  283. void rve_destroy(struct rval_expr* rve);
  284. /** fix a rval_expr. */
  285. int fix_rval_expr(void* p);
  286. #endif /* _rvalue_h */