1 /**
2  * Copyright (C) 2016 Daniel-Constantin Mierla (asipto.com)
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 #ifndef _SR_KEMI_H_
23 #define _SR_KEMI_H_
24 
25 #include "str.h"
26 #include "parser/msg_parser.h"
27 #include "action.h"
28 
29 #define SR_KEMIP_NONE	(0)		/* no type */
30 #define SR_KEMIP_INT	(1<<0)	/* type integer */
31 #define SR_KEMIP_STR	(1<<1)	/* type str* */
32 #define SR_KEMIP_BOOL	(1<<2)	/* type boolean (0/1) */
33 #define SR_KEMIP_XVAL	(1<<3)	/* type extended value (integer, str*, ...) */
34 #define SR_KEMIP_NULL	(1<<4)	/* type NULL */
35 #define SR_KEMIP_DICT	(1<<5)	/* type dictionary */
36 #define SR_KEMIP_ARRAY	(1<<6)	/* type array */
37 
38 #define SR_KEMI_FALSE	0
39 #define SR_KEMI_TRUE	1
40 
41 #define SR_KEMI_PARAMS_MAX	6
42 
43 extern str kemi_onsend_route_callback;
44 extern str kemi_reply_route_callback;
45 extern str kemi_event_route_callback;
46 
47 typedef struct sr_kemi {
48 	str mname; /* sub-module name */
49 	str fname; /* function name */
50 	int rtype; /* return type (supported SR_KEMIP_INT/BOOL) */
51 	void *func; /* pointer to the C function to be executed */
52 	int ptypes[SR_KEMI_PARAMS_MAX]; /* array with the type of parameters */
53 } sr_kemi_t;
54 
55 typedef struct sr_kemi_item {
56 	sr_kemi_t *item;
57 	int nparams;
58 	struct sr_kemi_item *next;
59 } sr_kemi_item_t;
60 
61 typedef struct sr_kemi_module {
62 	str mname;
63 	sr_kemi_t *kexp;
64 } sr_kemi_module_t;
65 
66 typedef union {
67 	int n;
68 	str s;
69 } sr_kemi_val_t;
70 
71 typedef struct sr_kemi_dict_item
72 {
73 	struct sr_kemi_dict_item *next;
74 	str name;
75 	int vtype;
76 	union {
77 		int n;
78 		str s;
79 		struct sr_kemi_dict_item *dict;
80 	} v;
81 } sr_kemi_dict_item_t;
82 
83 typedef struct sr_kemi_xval {
84 	int vtype;
85 	union {
86 		int n;
87 		str s;
88 		sr_kemi_dict_item_t *dict;
89 	} v;
90 } sr_kemi_xval_t;
91 
92 /* only sip_msg_t */
93 typedef int (*sr_kemi_fm_f)(sip_msg_t*);
94 
95 /* sip_msg_t and one int|str param */
96 typedef int (*sr_kemi_fmn_f)(sip_msg_t*, int);
97 typedef int (*sr_kemi_fms_f)(sip_msg_t*, str*);
98 
99 /* sip_msg_t and two int|str params */
100 typedef int (*sr_kemi_fmnn_f)(sip_msg_t*, int, int);
101 typedef int (*sr_kemi_fmns_f)(sip_msg_t*, int, str*);
102 typedef int (*sr_kemi_fmsn_f)(sip_msg_t*, str*, int);
103 typedef int (*sr_kemi_fmss_f)(sip_msg_t*, str*, str*);
104 
105 /* sip_msg_t and three int|str params */
106 typedef int (*sr_kemi_fmnnn_f)(sip_msg_t*, int, int, int);
107 typedef int (*sr_kemi_fmnns_f)(sip_msg_t*, int, int, str*);
108 typedef int (*sr_kemi_fmnsn_f)(sip_msg_t*, int, str*, int);
109 typedef int (*sr_kemi_fmnss_f)(sip_msg_t*, int, str*, str*);
110 typedef int (*sr_kemi_fmsnn_f)(sip_msg_t*, str*, int, int);
111 typedef int (*sr_kemi_fmsns_f)(sip_msg_t*, str*, int, str*);
112 typedef int (*sr_kemi_fmssn_f)(sip_msg_t*, str*, str*, int);
113 typedef int (*sr_kemi_fmsss_f)(sip_msg_t*, str*, str*, str*);
114 
115 /* sip_msg_t and four int|str params */
116 typedef int (*sr_kemi_fmnnnn_f)(sip_msg_t*, int, int, int, int);
117 typedef int (*sr_kemi_fmnnns_f)(sip_msg_t*, int, int, int, str*);
118 typedef int (*sr_kemi_fmnnsn_f)(sip_msg_t*, int, int, str*, int);
119 typedef int (*sr_kemi_fmnnss_f)(sip_msg_t*, int, int, str*, str*);
120 typedef int (*sr_kemi_fmnsnn_f)(sip_msg_t*, int, str*, int, int);
121 typedef int (*sr_kemi_fmnsns_f)(sip_msg_t*, int, str*, int, str*);
122 typedef int (*sr_kemi_fmnssn_f)(sip_msg_t*, int, str*, str*, int);
123 typedef int (*sr_kemi_fmnsss_f)(sip_msg_t*, int, str*, str*, str*);
124 typedef int (*sr_kemi_fmsnnn_f)(sip_msg_t*, str*, int, int, int);
125 typedef int (*sr_kemi_fmsnns_f)(sip_msg_t*, str*, int, int, str*);
126 typedef int (*sr_kemi_fmsnsn_f)(sip_msg_t*, str*, int, str*, int);
127 typedef int (*sr_kemi_fmsnss_f)(sip_msg_t*, str*, int, str*, str*);
128 typedef int (*sr_kemi_fmssnn_f)(sip_msg_t*, str*, str*, int, int);
129 typedef int (*sr_kemi_fmssns_f)(sip_msg_t*, str*, str*, int, str*);
130 typedef int (*sr_kemi_fmsssn_f)(sip_msg_t*, str*, str*, str*, int);
131 typedef int (*sr_kemi_fmssss_f)(sip_msg_t*, str*, str*, str*, str*);
132 
133 /* sip_msg_t and five int|str params */
134 typedef int (*sr_kemi_fmsssss_f)(sip_msg_t*, str*, str*, str*, str*, str*);
135 typedef int (*sr_kemi_fmssssn_f)(sip_msg_t*, str*, str*, str*, str*, int);
136 typedef int (*sr_kemi_fmsssns_f)(sip_msg_t*, str*, str*, str*, int, str*);
137 typedef int (*sr_kemi_fmsssnn_f)(sip_msg_t*, str*, str*, str*, int, int);
138 typedef int (*sr_kemi_fmssnss_f)(sip_msg_t*, str*, str*, int, str*, str*);
139 typedef int (*sr_kemi_fmssnsn_f)(sip_msg_t*, str*, str*, int, str*, int);
140 typedef int (*sr_kemi_fmssnns_f)(sip_msg_t*, str*, str*, int, int, str*);
141 typedef int (*sr_kemi_fmssnnn_f)(sip_msg_t*, str*, str*, int, int, int);
142 typedef int (*sr_kemi_fmsnsss_f)(sip_msg_t*, str*, int, str*, str*, str*);
143 typedef int (*sr_kemi_fmsnssn_f)(sip_msg_t*, str*, int, str*, str*, int);
144 typedef int (*sr_kemi_fmsnsns_f)(sip_msg_t*, str*, int, str*, int, str*);
145 typedef int (*sr_kemi_fmsnsnn_f)(sip_msg_t*, str*, int, str*, int, int);
146 typedef int (*sr_kemi_fmsnnss_f)(sip_msg_t*, str*, int, int, str*, str*);
147 typedef int (*sr_kemi_fmsnnsn_f)(sip_msg_t*, str*, int, int, str*, int);
148 typedef int (*sr_kemi_fmsnnns_f)(sip_msg_t*, str*, int, int, int, str*);
149 typedef int (*sr_kemi_fmsnnnn_f)(sip_msg_t*, str*, int, int, int, int);
150 typedef int (*sr_kemi_fmnssss_f)(sip_msg_t*, int, str*, str*, str*, str*);
151 typedef int (*sr_kemi_fmnsssn_f)(sip_msg_t*, int, str*, str*, str*, int);
152 typedef int (*sr_kemi_fmnssns_f)(sip_msg_t*, int, str*, str*, int, str*);
153 typedef int (*sr_kemi_fmnssnn_f)(sip_msg_t*, int, str*, str*, int, int);
154 typedef int (*sr_kemi_fmnsnss_f)(sip_msg_t*, int, str*, int, str*, str*);
155 typedef int (*sr_kemi_fmnsnsn_f)(sip_msg_t*, int, str*, int, str*, int);
156 typedef int (*sr_kemi_fmnsnns_f)(sip_msg_t*, int, str*, int, int, str*);
157 typedef int (*sr_kemi_fmnsnnn_f)(sip_msg_t*, int, str*, int, int, int);
158 typedef int (*sr_kemi_fmnnsss_f)(sip_msg_t*, int, int, str*, str*, str*);
159 typedef int (*sr_kemi_fmnnssn_f)(sip_msg_t*, int, int, str*, str*, int);
160 typedef int (*sr_kemi_fmnnsns_f)(sip_msg_t*, int, int, str*, int, str*);
161 typedef int (*sr_kemi_fmnnsnn_f)(sip_msg_t*, int, int, str*, int, int);
162 typedef int (*sr_kemi_fmnnnss_f)(sip_msg_t*, int, int, int, str*, str*);
163 typedef int (*sr_kemi_fmnnnsn_f)(sip_msg_t*, int, int, int, str*, int);
164 typedef int (*sr_kemi_fmnnnns_f)(sip_msg_t*, int, int, int, int, str*);
165 typedef int (*sr_kemi_fmnnnnn_f)(sip_msg_t*, int, int, int, int, int);
166 
167 /* sip_msg_t and six int|str params */
168 typedef int (*sr_kemi_fmssssss_f)(sip_msg_t*, str*, str*, str*, str*, str*, str*);
169 
170 /* return xval, params only sip_msg_t */
171 typedef sr_kemi_xval_t* (*sr_kemi_xfm_f)(sip_msg_t*);
172 
173 /* return xval, params sip_msg_t and one int|str param */
174 typedef sr_kemi_xval_t* (*sr_kemi_xfmn_f)(sip_msg_t*, int);
175 typedef sr_kemi_xval_t* (*sr_kemi_xfms_f)(sip_msg_t*, str*);
176 
177 /* return xval, params sip_msg_t and two int|str params */
178 typedef sr_kemi_xval_t* (*sr_kemi_xfmnn_f)(sip_msg_t*, int, int);
179 typedef sr_kemi_xval_t* (*sr_kemi_xfmns_f)(sip_msg_t*, int, str*);
180 typedef sr_kemi_xval_t* (*sr_kemi_xfmsn_f)(sip_msg_t*, str*, int);
181 typedef sr_kemi_xval_t* (*sr_kemi_xfmss_f)(sip_msg_t*, str*, str*);
182 
183 sr_kemi_t* sr_kemi_lookup(str *mname, int midx, str *fname);
184 
185 int sr_kemi_modules_add(sr_kemi_t *klist);
186 int sr_kemi_modules_size_get(void);
187 sr_kemi_module_t* sr_kemi_modules_get(void);
188 
189 typedef int (*sr_kemi_eng_route_f)(sip_msg_t*, int, str*, str*);
190 
191 #define SR_KEMI_BNAME_SIZE	256
192 typedef struct sr_kemi_eng {
193 	char bname[SR_KEMI_BNAME_SIZE];
194 	str  ename;
195 	sr_kemi_eng_route_f froute;
196 } sr_kemi_eng_t;
197 
198 int sr_kemi_eng_register(str *ename, sr_kemi_eng_route_f froute);
199 int sr_kemi_eng_set(str *ename, str *cpath);
200 int sr_kemi_eng_setz(char *ename, char *cpath);
201 sr_kemi_eng_t* sr_kemi_eng_get(void);
202 
203 int sr_kemi_cbname_list_init(void);
204 int sr_kemi_cbname_lookup_name(str *name);
205 str* sr_kemi_cbname_lookup_idx(int idx);
206 
207 void sr_kemi_act_ctx_set(run_act_ctx_t *ctx);
208 run_act_ctx_t* sr_kemi_act_ctx_get(void);
209 
210 str *sr_kemi_param_map_get_name(int ptype);
211 str *sr_kemi_param_map_get_params(int *ptypes);
212 
213 int sr_kemi_core_set_drop(sip_msg_t *msg);
214 
215 int sr_kemi_route(sr_kemi_eng_t *keng, sip_msg_t *msg, int rtype,
216 		str *ename, str *edata);
217 int sr_kemi_ctx_route(sr_kemi_eng_t *keng, run_act_ctx_t *ctx, sip_msg_t *msg,
218 		int rtype, str *ename, str *edata);
219 
220 sr_kemi_t* sr_kemi_exports_get_pv(void);
221 
222 #define SR_KEMI_XVAL_NULL_NONE 0
223 #define SR_KEMI_XVAL_NULL_PRINT 1
224 #define SR_KEMI_XVAL_NULL_EMPTY 2
225 #define SR_KEMI_XVAL_NULL_ZERO 3
226 void sr_kemi_xval_null(sr_kemi_xval_t *xval, int rmode);
227 void sr_kemi_xval_free(sr_kemi_xval_t *xval);
228 
229 /* functions exported to kemi that are used in other places */
230 int sr_kemi_hdr_remove(sip_msg_t *msg, str *hname);
231 
232 #endif
233