pvar.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * $Id: items.h 2111 2007-05-01 11:18:08Z juhe $
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of SIP-Router, a free SIP server.
  7. *
  8. * SIP-Router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * SIP-Router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*!
  23. * \file
  24. * \brief Definitions for Pseudo-variable support
  25. */
  26. #ifndef _PVAR_H_
  27. #define _PVAR_H_
  28. #include "str.h"
  29. #include "usr_avp.h"
  30. #include "parser/msg_parser.h"
  31. #define PV_MARKER_STR "$"
  32. #define PV_MARKER '$'
  33. #define PV_LNBRACKET_STR "("
  34. #define PV_LNBRACKET '('
  35. #define PV_RNBRACKET_STR ")"
  36. #define PV_RNBRACKET ')'
  37. #define PV_LIBRACKET_STR "["
  38. #define PV_LIBRACKET '['
  39. #define PV_RIBRACKET_STR "]"
  40. #define PV_RIBRACKET ']'
  41. #define PV_VAL_NONE 0
  42. #define PV_VAL_NULL 1
  43. #define PV_VAL_EMPTY 2
  44. #define PV_VAL_STR 4
  45. #define PV_VAL_INT 8
  46. #define PV_TYPE_INT 16
  47. #define PV_VAL_PKG 32
  48. #define PV_VAL_SHM 64
  49. #define PV_NAME_INTSTR 0
  50. #define PV_NAME_PVAR 1
  51. #define PV_NAME_OTHER 2
  52. #define PV_IDX_INT 0
  53. #define PV_IDX_PVAR 1
  54. #define PV_IDX_ALL 2
  55. /*! if PV name is dynamic, integer, or str */
  56. #define pv_has_dname(pv) ((pv)->pvp.pvn.type==PV_NAME_PVAR)
  57. #define pv_has_iname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
  58. && !((pv)->pvp.pvn.u.isname.type&AVP_NAME_STR))
  59. #define pv_has_sname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
  60. && (pv)->pvp.pvn.u.isname.type&AVP_NAME_STR)
  61. #define pv_is_w(pv) ((pv)->setf!=NULL)
  62. enum _pv_type {
  63. PVT_NONE=0, PVT_EMPTY, PVT_NULL,
  64. PVT_MARKER, PVT_AVP, PVT_HDR,
  65. PVT_RURI, PVT_RURI_USERNAME, PVT_RURI_DOMAIN,
  66. PVT_DSTURI, PVT_COLOR, PVT_BRANCH,
  67. PVT_FROM, PVT_TO, PVT_OURI,
  68. PVT_SCRIPTVAR, PVT_MSG_BODY, PVT_CONTEXT,
  69. PVT_OTHER, PVT_EXTRA /* keep it last */
  70. };
  71. typedef enum _pv_type pv_type_t;
  72. typedef int pv_flags_t;
  73. typedef void (*pv_name_free_f)(void*);
  74. typedef struct _pv_value
  75. {
  76. str rs; /*!< string value */
  77. int ri; /*!< integer value */
  78. int flags; /*!< flags about the type of value */
  79. } pv_value_t, *pv_value_p;
  80. typedef struct _pv_name
  81. {
  82. int type; /*!< type of name */
  83. pv_name_free_f nfree; /*!< function to free name structure */
  84. union {
  85. struct {
  86. int type; /*!< type of int_str name - compatibility with AVPs */
  87. int_str name; /*!< the value of the name */
  88. } isname;
  89. void *dname; /*!< PV value - dynamic name */
  90. } u;
  91. } pv_name_t, *pv_name_p;
  92. typedef struct _pv_index
  93. {
  94. int type; /*!< type of PV index */
  95. union {
  96. int ival; /*!< integer value */
  97. void *dval; /*!< PV value - dynamic index */
  98. } u;
  99. } pv_index_t, *pv_index_p;
  100. typedef struct _pv_param
  101. {
  102. pv_name_t pvn; /*!< PV name */
  103. pv_index_t pvi; /*!< PV index */
  104. } pv_param_t, *pv_param_p;
  105. typedef int (*pv_getf_t) (struct sip_msg*, pv_param_t*, pv_value_t*);
  106. typedef int (*pv_setf_t) (struct sip_msg*, pv_param_t*, int, pv_value_t*);
  107. typedef struct _pv_spec {
  108. pv_type_t type; /*!< type of PV */
  109. pv_getf_t getf; /*!< get PV value function */
  110. pv_setf_t setf; /*!< set PV value function */
  111. pv_param_t pvp; /*!< parameter to be given to get/set functions */
  112. void *trans; /*!< transformations */
  113. } pv_spec_t, *pv_spec_p;
  114. typedef int (*pv_parse_name_f)(pv_spec_p sp, str *in);
  115. typedef int (*pv_parse_index_f)(pv_spec_p sp, str *in);
  116. typedef int (*pv_init_param_f)(pv_spec_p sp, int param);
  117. #define pv_alter_context(pv) ((pv)->type==PVT_CONTEXT \
  118. || (pv)->type==PVT_BRANCH)
  119. /*! \brief
  120. * PV spec format:
  121. * - $class_name
  122. * - $class_name(inner_name)
  123. * - $(class_name[index])
  124. * - $(class_name(inner_name)[index])
  125. * - $(class_name{transformation})
  126. * - $(class_name(inner_name){transformation})
  127. * - $(class_name[index]{transformation})
  128. * - $(class_name(inner_name)[index]{transformation})
  129. */
  130. typedef struct _pv_export {
  131. str name; /*!< class name of PV */
  132. pv_type_t type; /*!< type of PV */
  133. pv_getf_t getf; /*!< function to get the value */
  134. pv_setf_t setf; /*!< function to set the value */
  135. pv_parse_name_f parse_name; /*!< function to parse the inner name */
  136. pv_parse_index_f parse_index; /*!< function to parse the index of PV */
  137. pv_init_param_f init_param; /*!< function to init the PV spec */
  138. int iparam; /*!< parameter for the init function */
  139. } pv_export_t;
  140. typedef struct _pv_elem
  141. {
  142. str text;
  143. pv_spec_t *spec;
  144. struct _pv_elem *next;
  145. } pv_elem_t, *pv_elem_p;
  146. char* pv_parse_spec2(str *in, pv_spec_p sp, int silent);
  147. #define pv_parse_spec(in, sp) pv_parse_spec2((in), (sp), 0)
  148. int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
  149. int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
  150. pv_value_t *value);
  151. int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len);
  152. int pv_elem_free_all(pv_elem_p log);
  153. void pv_value_destroy(pv_value_t *val);
  154. void pv_spec_destroy(pv_spec_t *spec);
  155. void pv_spec_free(pv_spec_t *spec);
  156. int pv_spec_dbg(pv_spec_p sp);
  157. int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags);
  158. int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
  159. unsigned short *name_type);
  160. int pv_parse_avp_name(pv_spec_p sp, str *in);
  161. int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name);
  162. int pv_parse_format(str *in, pv_elem_p *el);
  163. int pv_parse_index(pv_spec_p sp, str *in);
  164. int pv_init_iname(pv_spec_p sp, int param);
  165. int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s);
  166. pv_spec_t* pv_spec_lookup(str *name, int *len);
  167. typedef struct _pvname_list {
  168. pv_spec_t sname;
  169. struct _pvname_list *next;
  170. } pvname_list_t, *pvname_list_p;
  171. typedef struct pv_spec_list {
  172. pv_spec_p spec;
  173. struct pv_spec_list *next;
  174. } pv_spec_list_t, *pv_spec_list_p;
  175. pvname_list_t* parse_pvname_list(str *in, unsigned int type);
  176. int register_pvars_mod(char *mod_name, pv_export_t *items);
  177. int pv_free_extra_list(void);
  178. int pv_locate_name(str *in);
  179. pv_spec_t* pv_cache_get(str *name);
  180. /*! \brief PV helper functions */
  181. int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
  182. int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
  183. pv_value_t *res, unsigned int uival);
  184. int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
  185. pv_value_t *res, int sival);
  186. int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
  187. pv_value_t *res, str *sval);
  188. int pv_get_strzval(struct sip_msg *msg, pv_param_t *param,
  189. pv_value_t *res, char *sval);
  190. int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
  191. pv_value_t *res, str *sval, int ival);
  192. int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
  193. pv_value_t *res, int ival, str *sval);
  194. /**
  195. * Transformations
  196. */
  197. #define TR_LBRACKET_STR "{"
  198. #define TR_LBRACKET '{'
  199. #define TR_RBRACKET_STR "}"
  200. #define TR_RBRACKET '}'
  201. #define TR_CLASS_MARKER '.'
  202. #define TR_PARAM_MARKER ','
  203. enum _tr_param_type { TR_PARAM_NONE=0, TR_PARAM_STRING, TR_PARAM_NUMBER,
  204. TR_PARAM_SPEC, TR_PARAM_SUBST, TR_PARAM_OTHER };
  205. typedef struct _tr_param {
  206. int type;
  207. union {
  208. int n;
  209. str s;
  210. void *data;
  211. } v;
  212. struct _tr_param *next;
  213. } tr_param_t, *tr_param_p;
  214. typedef int (*tr_func_t) (struct sip_msg *, tr_param_t*, int, pv_value_t*);
  215. typedef struct _trans {
  216. str name;
  217. int type;
  218. int subtype;
  219. tr_func_t trf;
  220. tr_param_t *params;
  221. struct _trans *next;
  222. } trans_t, *trans_p;
  223. typedef char* (*tr_parsef_t)(str *, trans_t *);
  224. typedef struct _tr_export {
  225. str tclass;
  226. tr_parsef_t tparse;
  227. } tr_export_t, *tr_export_p;
  228. char* tr_lookup(str *in, trans_t **tr);
  229. tr_export_t* tr_lookup_class(str *tclass);
  230. int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v);
  231. void tr_param_free(tr_param_t *tp);
  232. int register_trans_mod(char *mod_name, tr_export_t *items);
  233. #endif