1 /*
2  * Copyright (C) 2005-2006 iptelorg GmbH
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 :: Selcct Framework
24  * \author mma
25  * \ingroup core
26  * Module: \ref core
27  */
28 
29 
30 #ifndef _SELECT_H
31 #define _SELECT_H
32 
33 #include "str.h"
34 #include "parser/msg_parser.h"
35 
36 #define MAX_SELECT_PARAMS 32
37 #define MAX_NESTED_CALLS  4
38 
39 /* Flags for parser table FLAG bitfiels
40  */
41 #define DIVERSION_MASK   0x00FF
42 
43 /* if DIVERSION is set and the function is accepted
44  * the param is changed into SEL_PARAM_DIV and the value is set to (flags & DIVERSION_MASK)
45  *  - it is valuable for STR params (saves parsing time)
46  *  - does not release the memory occupied by the parameter
47  */
48 #define DIVERSION        1<<8
49 
50 /* set if any parameter is expected at this stage
51  * (the function must be resolved further)
52  */
53 #define SEL_PARAM_EXPECTED   1<<9
54 
55 /* accept if following parameter is STR (any)
56  * consume that extra parameter in one step
57  */
58 #define CONSUME_NEXT_STR 1<<10
59 
60 /* accept if following parameter is INT
61  * consume that extra parameter in one ste
62  */
63 #define CONSUME_NEXT_INT 1<<11
64 
65 /* accept all the following parameters
66  * without checking them
67  */
68 #define CONSUME_ALL	1<<12
69 
70 /* next parameter is optional (use with CONSUME_NEXT_STR or CONSUME_NEXT_INT
71  * resolution is accepted even if there is no other parameter
72  * or the parameter is of wrong type
73  */
74 #define OPTIONAL         1<<13
75 
76 /* left function is noted to be called
77  * rigth function continues in resolution
78  * NOTE: the parameter is not consumed for PARENT,
79  * so you can leave it as ..,SEL_PARAM_INT, 0,..
80  *
81  * run_select then calls all functions with PARENT flag
82  * in the order of resolution until the final call or
83  * the result is != 0 (<0 error, 1 null str)
84  * the only one parameter passed between nested calls
85  * is the result str*
86  */
87 #define NESTED		1<<14
88 
89 /* "fixup call" would be done, when the structure is resolved to this node
90  * which means call with res and msg NULL
91  *
92  * if the fixup call return value <0, the select resolution will fail
93  */
94 #define FIXUP_CALL	1<<15
95 
96 /*
97  * Selector call parameter
98  */
99 typedef enum {
100 	SEL_PARAM_INT,  /* Integer parameter */
101 	SEL_PARAM_STR,  /* String parameter */
102 	SEL_PARAM_DIV,  /* Integer value got from parsing table */
103 	SEL_PARAM_PTR   /* void* data got from e.g. fixup call */
104 } select_param_type_t;
105 
106 typedef union {
107 	int i;  /* Integer value */
108 	str s;  /* String value */
109 	void* p;/* Any data ptr */
110 } select_param_value_t;
111 
112 typedef struct sel_param {
113         select_param_type_t type;
114         select_param_value_t v;
115 } select_param_t;
116 
117 struct select;
118 
119 typedef int (*select_f)(str* res, struct select* s, struct sip_msg* msg);
120 
121 typedef struct select {
122 	select_f f[MAX_NESTED_CALLS];
123 	int param_offset[MAX_NESTED_CALLS+1];
124 	/* contains broken down select string (@foo.bar[-2].foo -> 4 entries) */
125 	select_param_t params[MAX_SELECT_PARAMS];
126 	/* how many elements are used in 'params' */
127 	int n;
128 } select_t;
129 
130 typedef struct {
131 	select_f curr_f;
132 	select_param_type_t type;
133 	str name;
134 	select_f new_f;
135 	int flags;
136 } select_row_t;
137 
138 typedef struct select_table {
139   select_row_t *table;
140   struct select_table *next;
141 } select_table_t;
142 
143 /* the level of the select call that is being evaluated
144  * by the child process
145  */
146 extern int select_level;
147 
148 /* pointer to the SIP uri being processed.
149  * Nested function calls can pass information to each
150  * other using this pointer. Only for performace reasons.
151  * (Miklos)
152  */
153 extern struct sip_uri	*select_uri_p;
154 
155 /*
156  * Lookup corresponding select function based on
157  * the select parameters
158  */
159 int resolve_select(select_t* s);
160 
161 /*
162  * Run the select function
163  */
164 int run_select(str* res, select_t* s, struct sip_msg* msg);
165 
166 /*
167  * Print select for debugging purposes
168  */
169 void print_select(select_t* s);
170 
171 /*
172  * Print select for error purposes
173  */
174 void err_select(select_t* s);
175 
176 /*
177  * Register modules' own select parser table
178  */
179 int register_select_table(select_row_t *table);
180 
181 /*
182  * Tries to parse string pointed by *p (up to first non alpha char) into select structure
183  * if parsing succeeded, call resolve_select
184  * if resolving passes, returns final structure
185  * *p moves to first unused char
186  * return 0
187  *
188  * if memory allocation fails, returns -1
189  * if parsing or resolving fails, returns -2
190  */
191 int parse_select (char** p, select_t** s);
192 
193 /**
194  * Frees the select obtained with parse_select().
195  */
196 void free_select(select_t *s);
197 /*
198  * Select parser, result is stored in SHARED memory
199  *
200  * If you call this, you must ensure, that the string which
201  * is being parsed MUST be at the same place for all child
202  * processes, e.g. allocated in the shared memory as well
203  *
204  * parameters and results same as parse_select
205  */
206 int shm_parse_select (char** p, select_t** s);
207 
208 /**
209  * Frees the select obtained with shm_parse_select().
210  */
211 void shm_free_select(select_t *s);
212 
213 
214 #define SELECT_F(function) extern int function (str* res, select_t* s, struct sip_msg* msg);
215 #define ABSTRACT_F(function) int function (str* res, select_t* s, struct sip_msg* msg) {return -1;}
216 
217 #endif /* _SELECT_H */
218