rvalue.h 12 KB

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