2
0

rvalue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_INT_OP, /**< one member, returns (int)val (int) */
  85. RVE_STR_OP /**< one member, returns (str)val (str) */
  86. };
  87. struct str_re{
  88. str s;
  89. regex_t* regex;
  90. };
  91. union rval_val{
  92. void* p;
  93. long l;
  94. str s;
  95. avp_spec_t avps;
  96. select_t sel;
  97. pv_spec_t pvs;
  98. struct action* action;
  99. struct expr* bexpr;
  100. struct str_re re;
  101. };
  102. struct rvalue{
  103. enum rval_type type;
  104. int refcnt; /**< refcnt, on 0 the structure is destroyed */
  105. union rval_val v;
  106. int bsize; /**< extra data size */
  107. short flags;
  108. char buf[1]; /**< extra data, like string contents can be stored here */
  109. };
  110. /* rvalue flags */
  111. #define RV_CNT_ALLOCED_F 1 /**< free contents (pkg mem allocated) */
  112. #define RV_RV_ALLOCED_F 2 /**< free rv itself (pkg_free(rv)) */
  113. #define RV_ALL_ALLOCED_F (RV_CNT_ALLOCED|RV_RV_ALLOCED)
  114. #define RV_RE_F 4 /**< string is a RE with a valid v->re member */
  115. #define RV_RE_ALLOCED_F 8 /**< v->re.regex must be freed */
  116. struct rval_expr{
  117. enum rval_expr_op op;
  118. union{
  119. struct rval_expr* rve;
  120. struct rvalue rval;
  121. }left;
  122. union{
  123. struct rval_expr* rve;
  124. struct rvalue rval;
  125. }right;
  126. struct cfg_pos fpos;
  127. };
  128. enum rval_cache_type{
  129. RV_CACHE_EMPTY,
  130. RV_CACHE_PVAR,
  131. RV_CACHE_AVP,
  132. RV_CACHE_SELECT,
  133. RV_CACHE_INT2STR
  134. };
  135. /** value cache for a rvalue struct.
  136. * Used to optimize functions that would need to
  137. * get the value repeatedly (e.g. rval_get_btype() and then rval_get_int())
  138. */
  139. struct rval_cache{
  140. enum rval_cache_type cache_type;
  141. enum rval_type val_type;
  142. union{
  143. int_str avp_val; /**< avp value */
  144. pv_value_t pval; /**< pvar value */
  145. }c;
  146. char i2s[INT2STR_MAX_LEN]; /**< space for converting an int to string*/
  147. };
  148. /** allocates a new rval (should be freed by rval_destroy()). */
  149. struct rvalue* rval_new_empty(int extra_size);
  150. struct rvalue* rval_new_str(str* s, int extra_size);
  151. /**
  152. * @brief create a new pk_malloc'ed rvalue from a rval_val union
  153. * @param t rvalue type
  154. * @param v rvalue value
  155. * @param extra_size extra space to allocate
  156. * (so that future string operation can reuse the space)
  157. * @return new rv or 0 on error
  158. */
  159. struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size);
  160. /** inits a rvalue structure- */
  161. void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
  162. int flags);
  163. /** frees a rval_new(), rval_convert() or rval_expr_eval() returned rval. */
  164. void rval_destroy(struct rvalue* rv);
  165. /** frees a rval contents */
  166. void rval_clean(struct rvalue* rv);
  167. /** init a rval_cache struct */
  168. #define rval_cache_init(rvc) \
  169. do{ (rvc)->cache_type=RV_CACHE_EMPTY; (rvc)->val_type=RV_NONE; }while(0)
  170. /** destroy a rval_cache struct contents */
  171. void rval_cache_clean(struct rval_cache* rvc);
  172. /**
  173. * @brief Convert a rvalue to another rvalue, of a specific type
  174. *
  175. * Convert a rvalue to another rvalue, of a specific type.
  176. * The result is read-only in most cases (can be a reference
  177. * to another rvalue, can be checked by using rv_chg_in_place()) and
  178. * _must_ be rval_destroy()'ed.
  179. *
  180. * @param h run action context
  181. * @param msg SIP mesasge
  182. * @param type - type to convert to
  183. * @param v - rvalue to convert
  184. * @param c - rval_cache (cached v value if known/filled by another
  185. * function), can be 0 (unknown/not needed)
  186. * @return pointer to a rvalue (reference to an existing one or a new
  187. * one, @see rv_chg_in_place() and the above comment), or 0 on error.
  188. */
  189. struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
  190. enum rval_type type, struct rvalue* v,
  191. struct rval_cache* c);
  192. /** get the integer value of an rvalue. */
  193. int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg, int* i,
  194. struct rvalue* rv, struct rval_cache* cache);
  195. /** get the string value of an rv. */
  196. int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
  197. str* s, struct rvalue* rv,
  198. struct rval_cache* cache);
  199. /** get the string value of an rv in a tmp variable */
  200. int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
  201. str* tmpv, struct rvalue* rv,
  202. struct rval_cache* cache,
  203. struct rval_cache* tmp_cache);
  204. /** evals an integer expr to an int. */
  205. int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
  206. int* res, struct rval_expr* rve);
  207. /**
  208. * @brief Evals a rval expression
  209. * @warning result must be rval_destroy()'ed if non-null (it might be
  210. * a reference to another rval). The result can be modified only
  211. * if rv_chg_in_place() returns true.
  212. * @param h run action context
  213. * @param msg SIP message
  214. * @param rve rvalue expression
  215. * @return rvalue on success, 0 on error
  216. */
  217. struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
  218. struct rval_expr* rve);
  219. /**
  220. * @brief Evals a rval expression into an int or another rv(str)
  221. * @warning rv result (rv_res) must be rval_destroy()'ed if non-null
  222. * (it might be a reference to another rval). The result can be
  223. * modified only if rv_chg_in_place() returns true.
  224. * @param h run action context
  225. * @param msg SIP message
  226. * @param res_rv pointer to rvalue result, if non-null it means the
  227. * expression evaluated to a non-int (str), which will be stored here.
  228. * @param res_i pointer to int result, if res_rv==0 and the function
  229. * returns success => the result is an int which will be stored here.
  230. * @param rve expression that will be evaluated.
  231. * @param cache write-only value cache, it might be filled if non-null and
  232. * empty (rval_cache_init()). If non-null, it _must_ be rval_cache_clean()'ed
  233. * when done.
  234. * @return 0 on success, -1 on error, sets *res_rv or *res_i.
  235. */
  236. int rval_expr_eval_rvint( struct run_act_ctx* h, struct sip_msg* msg,
  237. struct rvalue** rv_res, int* i_res,
  238. struct rval_expr* rve, struct rval_cache* cache);
  239. /** guess the type of an expression. */
  240. enum rval_type rve_guess_type(struct rval_expr* rve);
  241. /** returns true if expression is constant. */
  242. int rve_is_constant(struct rval_expr* rve);
  243. /** returns true if the expression can have side-effect */
  244. int rve_has_side_effects(struct rval_expr* rve);
  245. /**
  246. * @brief Returns 1 if expression is valid (type-wise)
  247. * @param type filled with the type of the expression (RV_INT, RV_STR or
  248. * RV_NONE if it's dynamic)
  249. * @param rve checked expression
  250. * @param bad_rve set on failure to the subexpression for which the
  251. * type check failed
  252. * @param bad_t set on failure to the type of the bad subexpression
  253. * @param exp_t set on failure to the expected type for the bad
  254. * subexpression
  255. * @return 0 or 1 and sets *type to the resulting type
  256. * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
  257. */
  258. int rve_check_type(enum rval_type* type, struct rval_expr* rve,
  259. struct rval_expr** bad_rve, enum rval_type* bad_type,
  260. enum rval_type* exp_type);
  261. /** returns a string name for type (debugging).*/
  262. char* rval_type_name(enum rval_type type);
  263. /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type
  264. */
  265. struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
  266. struct cfg_pos* pos);
  267. /**
  268. * @brief Create a unary op. rval_expr
  269. * ret= op rve1
  270. * @param op - rval expr. unary operator
  271. * @param rve1 - rval expr. on which the operator will act.
  272. * @param pos configuration position
  273. * @return new pkg_malloc'ed rval_expr or 0 on error.
  274. */
  275. struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
  276. struct cfg_pos* pos);
  277. /**
  278. * @brief Create a rval_expr. from 2 other rval exprs, using op
  279. * ret = rve1 op rve2
  280. * @param op - rval expr. operator
  281. * @param rve1 - rval expr. on which the operator will act.
  282. * @param rve2 - rval expr. on which the operator will act.
  283. * @param pos configuration position
  284. * @return new pkg_malloc'ed rval_expr or 0 on error.
  285. */
  286. struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
  287. struct rval_expr* rve2,
  288. struct cfg_pos* pos);
  289. /** destroys a pkg_malloc'ed rve. */
  290. void rve_destroy(struct rval_expr* rve);
  291. /** fix a rval_expr. */
  292. int fix_rval_expr(void* p);
  293. #endif /* _rvalue_h */