ae5da276af3dda8e3d3e1b0369ef2ff02740dcc3
[kamailio] / src / modules / lost / lost.c
1 /*
2  * lost module
3  *
4  * Copyright (C) 2019 Wolfgang Kampichler
5  * DEC112, FREQUENTIS AG
6  *
7  * This file is part of Kamailio, a free SIP server.
8  *
9  * Kamailio is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * Kamailio is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 /*!
26  * \file
27  * \brief Kamailio lost ::
28  * \ingroup lost
29  * Module: \ref lost
30  */
31
32 #include "../../modules/http_client/curl_api.h"
33
34 #include "../../core/mod_fix.h"
35 #include "../../core/sr_module.h"
36 #include "../../core/ut.h"
37 #include "../../core/locking.h"
38
39 #include "../../core/pvar.h"
40 #include "../../core/mem/mem.h"
41 #include "../../core/dprint.h"
42
43 #include "../../core/script_cb.h"
44
45 #include "functions.h"
46
47 MODULE_VERSION
48
49 /* Module parameter variables */
50 httpc_api_t httpapi;
51
52 /* Module management function prototypes */
53 static int mod_init(void);
54 static int child_init(int);
55 static void destroy(void);
56
57 /* Fixup functions to be defined later */
58 static int fixup_lost_held_query(void **param, int param_no);
59 static int fixup_free_lost_held_query(void **param, int param_no);
60 static int fixup_lost_held_query_id(void **param, int param_no);
61 static int fixup_free_lost_held_query_id(void **param, int param_no);
62
63 static int fixup_lost_query(void **param, int param_no);
64 static int fixup_free_lost_query(void **param, int param_no);
65 static int fixup_lost_query_all(void **param, int param_no);
66 static int fixup_free_lost_query_all(void **param, int param_no);
67
68 /* Wrappers for http_query to be defined later */
69 static int w_lost_held_query(
70                 struct sip_msg *_m, char *_con, char *_pidf, char *_url, char *_err);
71 static int w_lost_held_query_id(struct sip_msg *_m, char *_con, char *_id,
72                 char *_pidf, char *_url, char *_err);
73 static int w_lost_query(
74                 struct sip_msg *_m, char *_con, char *_uri, char *_name, char *_err);
75 static int w_lost_query_all(struct sip_msg *_m, char *_con, char *_pidf,
76                 char *_urn, char *_uri, char *_name, char *_err);
77
78 /* Exported functions */
79 static cmd_export_t cmds[] = {
80                 {"lost_held_query", (cmd_function)w_lost_held_query, 4,
81                                 fixup_lost_held_query, fixup_free_lost_held_query,
82                                 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
83                 {"lost_held_query", (cmd_function)w_lost_held_query_id, 5,
84                                 fixup_lost_held_query_id, fixup_free_lost_held_query_id,
85                                 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
86                 {"lost_query", (cmd_function)w_lost_query, 4, fixup_lost_query,
87                                 fixup_free_lost_query,
88                                 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
89                 {"lost_query", (cmd_function)w_lost_query_all, 6, fixup_lost_query_all,
90                                 fixup_free_lost_query_all,
91                                 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
92                 {0, 0, 0, 0, 0, 0}};
93
94
95 /* Module interface */
96 struct module_exports exports = {
97                 "lost",                  /* module name*/
98                 DEFAULT_DLFLAGS, /* dlopen flags */
99                 cmds,                    /* exported functions */
100                 0,                               /* exported parameters */
101                 0,                               /* RPC method exports */
102                 0,                               /* exported pseudo-variables */
103                 0,                               /* response handling function */
104                 mod_init,                /* module initialization function */
105                 child_init,              /* per-child init function */
106                 destroy                  /* module destroy function */
107 };
108
109 /* Module initialization function */
110 static int mod_init(void)
111 {
112         LM_DBG("init lost module\n");
113
114         if(httpc_load_api(&httpapi) != 0) {
115                 LM_ERR("Can not bind to http_client API \n");
116                 return -1;
117         }
118
119         LM_DBG("**** init lost module done.\n");
120
121         return 0;
122 }
123
124 /* Child initialization function */
125 static int child_init(int rank)
126 {
127         return 0;
128 }
129
130 static void destroy(void)
131 {
132         ;
133         /* do nothing */
134 }
135
136 /*
137  * Fix 4 lost_held_query params: con (string/pvar)
138  * and pidf, url, err (writable pvar).
139  */
140 static int fixup_lost_held_query(void **param, int param_no)
141 {
142         if(param_no == 1) {
143                 return fixup_spve_null(param, 1);
144         }
145         if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
146                 if(fixup_pvar_null(param, 1) != 0) {
147                         LM_ERR("failed to fixup result pvar\n");
148                         return -1;
149                 }
150                 if(((pv_spec_t *)(*param))->setf == NULL) {
151                         LM_ERR("result pvar is not writable\n");
152                         return -1;
153                 }
154                 return 0;
155         }
156         LM_ERR("invalid parameter number <%d>\n", param_no);
157         return -1;
158 }
159
160 /*
161  * Free lost_held_query params.
162  */
163 static int fixup_free_lost_held_query(void **param, int param_no)
164 {
165         if(param_no == 1) {
166                 /* char strings don't need freeing */
167                 return 0;
168         }
169         if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
170                 return fixup_free_pvar_null(param, 1);
171         }
172         LM_ERR("invalid parameter number <%d>\n", param_no);
173         return -1;
174 }
175
176 /*
177  * Fix 5 lost_held_query_id params: con (string/pvar) id (string that may contain
178  * pvars) and pidf, url, err (writable pvar).
179  */
180 static int fixup_lost_held_query_id(void **param, int param_no)
181 {
182         if(param_no == 1) {
183                 return fixup_spve_null(param, 1);
184         }
185         if(param_no == 2) {
186                 return fixup_spve_null(param, 1);
187         }
188         if((param_no == 3) || (param_no == 4) || (param_no == 5)) {
189                 if(fixup_pvar_null(param, 1) != 0) {
190                         LM_ERR("failed to fixup result pvar\n");
191                         return -1;
192                 }
193                 if(((pv_spec_t *)(*param))->setf == NULL) {
194                         LM_ERR("result pvar is not writable\n");
195                         return -1;
196                 }
197                 return 0;
198         }
199         LM_ERR("invalid parameter number <%d>\n", param_no);
200         return -1;
201 }
202
203 /*
204  * Free lost_held_query_id params.
205  */
206 static int fixup_free_lost_held_query_id(void **param, int param_no)
207 {
208         if(param_no == 1) {
209                 return fixup_free_spve_null(param, 1);
210         }
211         if(param_no == 2) {
212                 return fixup_free_spve_null(param, 1);
213         }
214         if((param_no == 3) || (param_no == 4) || (param_no == 5)) {
215                 return fixup_free_pvar_null(param, 1);
216         }
217         LM_ERR("invalid parameter number <%d>\n", param_no);
218         return -1;
219 }
220
221 /*
222  * Fix 4 lost_query params: con (string/pvar)
223  * and uri, name, err (writable pvar).
224  */
225 static int fixup_lost_query(void **param, int param_no)
226 {
227         if(param_no == 1) {
228                 return fixup_spve_null(param, 1);
229         }
230         if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
231                 if(fixup_pvar_null(param, 1) != 0) {
232                         LM_ERR("failed to fixup result pvar\n");
233                         return -1;
234                 }
235                 if(((pv_spec_t *)(*param))->setf == NULL) {
236                         LM_ERR("result pvar is not writable\n");
237                         return -1;
238                 }
239                 return 0;
240         }
241         LM_ERR("invalid parameter number <%d>\n", param_no);
242         return -1;
243 }
244
245 /*
246  * Free lost_held_query_id params.
247  */
248 static int fixup_free_lost_query(void **param, int param_no)
249 {
250         if(param_no == 1) {
251                 return fixup_free_spve_null(param, 1);
252         }
253         if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
254                 return fixup_free_pvar_null(param, 1);
255         }
256         LM_ERR("invalid parameter number <%d>\n", param_no);
257         return -1;
258 }
259
260 /*
261  * Fix 6 lost_query params: con (string/pvar) pidf, urn (string that may contain
262  * pvars) and uri, name, err (writable pvar).
263  */
264 static int fixup_lost_query_all(void **param, int param_no)
265 {
266         if(param_no == 1) {
267                 return fixup_spve_null(param, 1);
268         }
269         if((param_no == 2) || (param_no == 3)) {
270                 return fixup_spve_null(param, 1);
271         }
272         if((param_no == 4) || (param_no == 5) || (param_no == 6)) {
273                 if(fixup_pvar_null(param, 1) != 0) {
274                         LM_ERR("failed to fixup result pvar\n");
275                         return -1;
276                 }
277                 if(((pv_spec_t *)(*param))->setf == NULL) {
278                         LM_ERR("result pvar is not writable\n");
279                         return -1;
280                 }
281                 return 0;
282         }
283         LM_ERR("invalid parameter number <%d>\n", param_no);
284         return -1;
285 }
286
287 /*
288  * Free lost_held_query_id params.
289  */
290 static int fixup_free_lost_query_all(void **param, int param_no)
291 {
292         if(param_no == 1) {
293                 return fixup_free_spve_null(param, 1);
294         }
295         if((param_no == 2) || (param_no == 3)) {
296                 return fixup_free_spve_null(param, 1);
297         }
298         if((param_no == 4) || (param_no == 5) || (param_no == 6)) {
299                 return fixup_free_pvar_null(param, 1);
300         }
301         LM_ERR("invalid parameter number <%d>\n", param_no);
302         return -1;
303 }
304
305 /*
306  * Wrapper for lost_held_query w/o id
307  */
308 static int w_lost_held_query(
309                 struct sip_msg *_m, char *_con, char *_pidf, char *_url, char *_err)
310 {
311         return lost_function_held(_m, _con, _pidf, _url, _err, NULL);
312 }
313
314 /*
315  * Wrapper for lost_held_query with id
316  */
317 static int w_lost_held_query_id(struct sip_msg *_m, char *_con, char *_id,
318                 char *_pidf, char *_url, char *_err)
319 {
320         return lost_function_held(_m, _con, _pidf, _url, _err, _id);
321 }
322
323 /*
324  * Wrapper for lost_query w/o pudf, urn
325  */
326 static int w_lost_query(
327                 struct sip_msg *_m, char *_con, char *_uri, char *_name, char *_err)
328 {
329         return lost_function(_m, _con, _uri, _name, _err, NULL, NULL);
330 }
331
332 /*
333  * Wrapper for lost_query with pidf, urn
334  */
335 static int w_lost_query_all(struct sip_msg *_m, char *_con, char *_pidf,
336                 char *_urn, char *_uri, char *_name, char *_err)
337 {
338         return lost_function(_m, _con, _uri, _name, _err, _pidf, _urn);
339 }