1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /*!
22  * \file
23  * \brief Kamailio core :: Definitions for Pseudo-variable support
24  */
25 
26 
27 #ifndef _PVAR_H_
28 #define _PVAR_H_
29 
30 #include "str.h"
31 #include "usr_avp.h"
32 #include "parser/msg_parser.h"
33 
34 #define PV_MARKER_STR	"$"
35 #define PV_MARKER		'$'
36 
37 #define PV_LNBRACKET_STR	"("
38 #define PV_LNBRACKET		'('
39 #define PV_RNBRACKET_STR	")"
40 #define PV_RNBRACKET		')'
41 
42 #define PV_LIBRACKET_STR	"["
43 #define PV_LIBRACKET		'['
44 #define PV_RIBRACKET_STR	"]"
45 #define PV_RIBRACKET		']'
46 
47 #define PV_VAL_NONE			0
48 #define PV_VAL_NULL			1
49 #define PV_VAL_EMPTY		2
50 #define PV_VAL_STR			4
51 #define PV_VAL_INT			8
52 #define PV_TYPE_INT			16
53 #define PV_VAL_PKG			32
54 #define PV_VAL_SHM			64
55 
56 #define PV_NAME_INTSTR	0
57 #define PV_NAME_PVAR	1
58 #define PV_NAME_OTHER	2
59 
60 #define PV_IDX_INT	0
61 #define PV_IDX_PVAR	1
62 #define PV_IDX_ALL	2
63 #define PV_IDX_ITR	3
64 #define PV_IDX_NONE 4
65 
66 /*! if PV name is dynamic, integer, or str */
67 #define pv_has_dname(pv) ((pv)->pvp.pvn.type==PV_NAME_PVAR)
68 #define pv_has_iname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
69 							&& !((pv)->pvp.pvn.u.isname.type&AVP_NAME_STR))
70 #define pv_has_sname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
71 							&& (pv)->pvp.pvn.u.isname.type&AVP_NAME_STR)
72 #define pv_is_w(pv)	((pv)->setf!=NULL)
73 
74 enum _pv_type {
75 	PVT_NONE=0,           PVT_EMPTY,             PVT_NULL,
76 	PVT_MARKER,           PVT_AVP,               PVT_HDR,
77 	PVT_RURI,             PVT_RURI_USERNAME,     PVT_RURI_DOMAIN,
78 	PVT_DSTURI,           PVT_COLOR,             PVT_BRANCH,
79 	PVT_FROM,             PVT_TO,                PVT_OURI,
80 	PVT_SCRIPTVAR,        PVT_MSG_BODY,          PVT_CONTEXT,
81 	PVT_XAVP,             PVT_XAVU,              PVT_XAVI,
82 	PVT_HDRC,             PVT_OTHER,             PVT_EXTRA /* keep it last */
83 };
84 
85 typedef enum _pv_type pv_type_t;
86 typedef int pv_flags_t;
87 
88 typedef void (*pv_name_free_f)(void*);
89 
90 typedef struct _pv_value
91 {
92 	str rs;    /*!< string value */
93 	int ri;    /*!< integer value */
94 	int flags; /*!< flags about the type of value */
95 } pv_value_t, *pv_value_p;
96 
97 typedef struct _pv_name
98 {
99 	int type;             /*!< type of name */
100 	pv_name_free_f nfree; /*!< function to free name structure */
101 	union {
102 		struct {
103 			int type;     /*!< type of int_str name - compatibility with AVPs */
104 			int_str name; /*!< the value of the name */
105 		} isname;
106 		void *dname;      /*!< PV value - dynamic name */
107 	} u;
108 } pv_name_t, *pv_name_p;
109 
110 typedef struct _pv_index
111 {
112 	int type; /*!< type of PV index */
113 	union {
114 		int ival;   /*!< integer value */
115 		void *dval; /*!< PV value - dynamic index */
116 	} u;
117 } pv_index_t, *pv_index_p;
118 
119 typedef struct _pv_param
120 {
121 	pv_name_t    pvn; /*!< PV name */
122 	pv_index_t   pvi; /*!< PV index */
123 } pv_param_t, *pv_param_p;
124 
125 typedef int (*pv_getf_t) (struct sip_msg*,  pv_param_t*, pv_value_t*);
126 typedef int (*pv_setf_t) (struct sip_msg*,  pv_param_t*, int, pv_value_t*);
127 
128 typedef struct _pv_spec {
129 	pv_type_t    type;   /*!< type of PV */
130 	pv_getf_t    getf;   /*!< get PV value function */
131 	pv_setf_t    setf;   /*!< set PV value function */
132 	pv_param_t   pvp;    /*!< parameter to be given to get/set functions */
133 	void         *trans; /*!< transformations */
134 } pv_spec_t, *pv_spec_p;
135 
136 typedef int (*pv_parse_name_f)(pv_spec_p sp, str *in);
137 typedef int (*pv_parse_index_f)(pv_spec_p sp, str *in);
138 typedef int (*pv_init_param_f)(pv_spec_p sp, int param);
139 
140 #define pv_alter_context(pv)	((pv)->type==PVT_CONTEXT \
141 									|| (pv)->type==PVT_BRANCH)
142 
143 /*! \brief
144  * PV spec format:
145  * - $class_name
146  * - $class_name(inner_name)
147  * - $(class_name[index])
148  * - $(class_name(inner_name)[index])
149  * - $(class_name{transformation})
150  * - $(class_name(inner_name){transformation})
151  * - $(class_name[index]{transformation})
152  * - $(class_name(inner_name)[index]{transformation})
153  */
154 typedef struct _pv_export {
155 	str name;                      /*!< class name of PV */
156 	pv_type_t type;                /*!< type of PV */
157 	pv_getf_t  getf;               /*!< function to get the value */
158 	pv_setf_t  setf;               /*!< function to set the value */
159 	pv_parse_name_f parse_name;    /*!< function to parse the inner name */
160 	pv_parse_index_f parse_index;  /*!< function to parse the index of PV */
161 	pv_init_param_f init_param;    /*!< function to init the PV spec */
162 	int iparam;                    /*!< parameter for the init function */
163 } pv_export_t;
164 
165 typedef struct _pv_elem
166 {
167 	str text;
168 	pv_spec_t *spec;
169 	struct _pv_elem *next;
170 } pv_elem_t, *pv_elem_p;
171 
172 char* pv_parse_spec2(str *in, pv_spec_p sp, int silent);
173 #define pv_parse_spec(in, sp) pv_parse_spec2((in), (sp), 0)
174 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
175 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
176 		pv_value_t *value);
177 int pv_printf_mode(sip_msg_t* msg, pv_elem_t* list, int mode, char *buf, int *len);
178 int pv_printf(sip_msg_t* msg, pv_elem_t* list, char *buf, int *len);
179 int pv_printf_size(sip_msg_t* msg, pv_elem_t *list);
180 int pv_elem_free_all(pv_elem_p log);
181 void pv_value_destroy(pv_value_t *val);
182 void pv_spec_destroy(pv_spec_t *spec);
183 void pv_spec_free(pv_spec_t *spec);
184 int pv_spec_dbg(pv_spec_p sp);
185 int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags);
186 int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
187 		unsigned short *name_type);
188 int pv_parse_avp_name(pv_spec_p sp, str *in);
189 int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name);
190 int pv_parse_format(str *in, pv_elem_p *el);
191 int pv_parse_index(pv_spec_p sp, str *in);
192 int pv_init_iname(pv_spec_p sp, int param);
193 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s);
194 pv_spec_t* pv_spec_lookup(str *name, int *len);
195 
196 typedef struct _pvname_list {
197 	pv_spec_t sname;
198 	struct _pvname_list *next;
199 } pvname_list_t, *pvname_list_p;
200 
201 typedef struct pv_spec_list {
202 	pv_spec_p spec;
203 	struct pv_spec_list *next;
204 } pv_spec_list_t, *pv_spec_list_p;
205 
206 pvname_list_t* parse_pvname_list(str *in, unsigned int type);
207 void free_pvname_list(pvname_list_t* head);
208 
209 int register_pvars_mod(char *mod_name, pv_export_t *items);
210 int pv_free_extra_list(void);
211 
212 int pv_locate_name(str *in);
213 pv_spec_t* pv_cache_get(str *name);
214 str* pv_cache_get_name(pv_spec_t *spec);
215 str *pv_get_null_str(void);
216 str *pv_get_empty_str(void);
217 
218 /*! \brief PV helper functions */
219 int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
220 int pv_get_strempty(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
221 
222 int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
223 		pv_value_t *res, unsigned int uival);
224 int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
225 		pv_value_t *res, int sival);
226 int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
227 		pv_value_t *res, str *sval);
228 int pv_get_strzval(struct sip_msg *msg, pv_param_t *param,
229 		pv_value_t *res, char *sval);
230 int pv_get_strlval(struct sip_msg *msg, pv_param_t *param,
231 		pv_value_t *res, char *sval, int slen);
232 int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
233 		pv_value_t *res, str *sval, int ival);
234 int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
235 		pv_value_t *res, int ival, str *sval);
236 
237 /**
238  * Core PV Cache
239  */
240 typedef struct _pv_cache
241 {
242 	str pvname;
243 	unsigned int pvid;
244 	pv_spec_t spec;
245 	struct _pv_cache *next;
246 } pv_cache_t;
247 
248 #define PV_CACHE_SIZE	512  /*!< pseudo-variables cache table size */
249 
250 pv_cache_t **pv_cache_get_table(void);
251 
252 
253 /**
254  * Transformations
255  */
256 #define TR_LBRACKET_STR		"{"
257 #define TR_LBRACKET		'{'
258 #define TR_RBRACKET_STR		"}"
259 #define TR_RBRACKET		'}'
260 #define TR_CLASS_MARKER		'.'
261 #define TR_PARAM_MARKER		','
262 
263 enum _tr_param_type { TR_PARAM_NONE=0, TR_PARAM_STRING, TR_PARAM_NUMBER,
264 	TR_PARAM_SPEC, TR_PARAM_SUBST, TR_PARAM_OTHER };
265 
266 typedef struct _tr_param {
267 	int type;
268 	union {
269 		int n;
270 		str s;
271 		void *data;
272 	} v;
273 	struct _tr_param *next;
274 } tr_param_t, *tr_param_p;
275 
276 typedef int (*tr_func_t) (struct sip_msg *, tr_param_t*, int, pv_value_t*);
277 
278 typedef struct _trans {
279 	str name;
280 	int type;
281 	int subtype;
282 	tr_func_t trf;
283 	tr_param_t *params;
284 	struct _trans *next;
285 } trans_t, *trans_p;
286 
287 typedef char* (*tr_parsef_t)(str *, trans_t *);
288 typedef struct _tr_export {
289 	str tclass;
290 	tr_parsef_t tparse;
291 } tr_export_t, *tr_export_p;
292 
293 char* tr_lookup(str *in, trans_t **tr);
294 tr_export_t* tr_lookup_class(str *tclass);
295 int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v);
296 void tr_param_free(tr_param_t *tp);
297 
298 int register_trans_mod(char *mod_name, tr_export_t *items);
299 
300 
301 /**
302  * XAVP
303  */
304 typedef struct _pv_xavp_name {
305 	str name;
306 	pv_spec_t index;
307 	struct _pv_xavp_name *next;
308 } pv_xavp_name_t;
309 
310 /**
311  * XAVU
312  */
313 typedef struct _pv_xavu_name {
314 	str name;
315 	struct _pv_xavu_name *next;
316 } pv_xavu_name_t;
317 
318 int pv_eval_str(sip_msg_t *msg, str *dst, str *src);
319 
320 #endif
321 
322