Debian Codegraph @

src/modules/lost/lost.c

raw symbols SCIP
1 /*
2 * lost module
3 *
4 * Copyright (C) 2021 Wolfgang Kampichler
5 * DEC112, FREQUENTIS AG
6 *
7 * This file is part of Kamailio, a free SIP server.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 *
11 * Kamailio is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version
15 *
16 * Kamailio is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27 /*!
28 * \file
29 * \brief Kamailio lost ::
30 * \ingroup lost
31 * Module: \ref lost
32 */
33
34 #include "../../modules/http_client/curl_api.h"
35
36 #include "../../core/mod_fix.h"
37 #include "../../core/sr_module.h"
38 #include "../../core/ut.h"
39 #include "../../core/locking.h"
40
41 #include "../../core/pvar.h"
42 #include "../../core/mem/mem.h"
43 #include "../../core/dprint.h"
44
45 #include "../../core/script_cb.h"
46
47 #include "functions.h"
48
49 MODULE_VERSION
50
51 /* Module parameter variables */
52 httpc_api_t httpapi;
53
54 /* lost: any (0), cid (1), http (2) or https (3) (default: 0) */
55 int lost_geoloc_type = 0;
56 /* lost: Geolocation header value order: first (0) or last (1) (default: 0) */
57 int lost_geoloc_order = 0;
58 /* lost: Geolocation header 3d representation: yes (1) or no (0) (default: 0) */
59 int lost_geoloc_3d = 0;
60 /* lost: Recursion allowed: yes (1) or no (0) (default: 1 = allowed) */
61 int lost_recursion = 1;
62 /* lost geo profile: first (0), last (1), geo (2) or civic (3) (default: 0) */
63 int lost_profile = 0;
64 /* lost verbose report: no (0), yes (1) (default: 0) */
65 int lost_verbose = 0;
66 /* held request: response time (default: 0 = "emergencyRouting") */
67 int held_resp_time = 0;
68 /* held request: exact is true (1) or false (0) (default: false) */
69 int held_exact_type = 0;
70 /* held request: POST to deref. location: yes (1) or no (0) (default: 0 = no) */
71 int held_post_req = 0;
72 /* held request: location type */
73 str held_loc_type = STR_NULL;
74
75 /* Module management function prototypes */
76 static int mod_init(void);
77 static int child_init(int);
78 static void destroy(void);
79
80 /* Fixup functions to be defined later */
81 static int fixup_lost_held_query(void **param, int param_no);
82 static int fixup_free_lost_held_query(void **param, int param_no);
83 static int fixup_lost_held_query_id(void **param, int param_no);
84 static int fixup_free_lost_held_query_id(void **param, int param_no);
85 static int fixup_lost_held_deref(void **param, int param_no);
86 static int fixup_free_lost_held_deref(void **param, int param_no);
87
88 static int fixup_lost_query(void **param, int param_no);
89 static int fixup_free_lost_query(void **param, int param_no);
90 static int fixup_lost_query_all(void **param, int param_no);
91 static int fixup_free_lost_query_all(void **param, int param_no);
92
93 /* Wrappers for http_query to be defined later */
94 static int w_lost_held_query(
95 struct sip_msg *_m, char *_con, char *_pidf, char *_url, char *_err);
96 static int w_lost_held_query_id(struct sip_msg *_m, char *_con, char *_id,
97 char *_pidf, char *_url, char *_err);
98 static int w_lost_held_deref(struct sip_msg *_m, char *_url, char *_rtime,
99 char *_rtype, char *_pidf, char *_err);
100 static int w_lost_query(
101 struct sip_msg *_m, char *_con, char *_uri, char *_name, char *_err);
102 static int w_lost_query_all(struct sip_msg *_m, char *_con, char *_pidf,
103 char *_urn, char *_uri, char *_name, char *_err);
104
105 /* clang-format off */
106 /* Exported functions */
107 static cmd_export_t cmds[] = {
108 {"lost_held_query", (cmd_function)w_lost_held_query, 4,
109 fixup_lost_held_query, fixup_free_lost_held_query,
110 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
111 {"lost_held_query", (cmd_function)w_lost_held_query_id, 5,
112 fixup_lost_held_query_id, fixup_free_lost_held_query_id,
113 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
114 {"lost_held_dereference", (cmd_function)w_lost_held_deref, 5,
115 fixup_lost_held_deref, fixup_free_lost_held_deref,
116 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
117 {"lost_query", (cmd_function)w_lost_query, 4, fixup_lost_query,
118 fixup_free_lost_query,
119 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
120 {"lost_query", (cmd_function)w_lost_query_all, 6, fixup_lost_query_all,
121 fixup_free_lost_query_all,
122 REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
123 {0, 0, 0, 0, 0, 0}
124 };
125
126 /* Exported parameters */
127 static param_export_t params[] = {{"exact_type", PARAM_INT, &held_exact_type},
128 {"response_time", PARAM_INT, &held_resp_time},
129 {"post_request", PARAM_INT, &held_post_req},
130 {"location_type", PARAM_STR, &held_loc_type},
131 {"recursion", PARAM_INT, &lost_recursion},
132 {"location_profile", PARAM_INT, &lost_profile},
133 {"location_3d", PARAM_INT, &lost_geoloc_3d},
134 {"verbose", PARAM_INT, &lost_verbose},
135 {"geoheader_type", PARAM_INT, &lost_geoloc_type},
136 {"geoheader_order", PARAM_INT, &lost_geoloc_order}, {0, 0, 0}
137 };
138
139 /* Module interface */
140 struct module_exports exports = {
141 "lost", /* module name*/
142 DEFAULT_DLFLAGS, /* dlopen flags */
143 cmds, /* exported functions */
144 params, /* exported parameters */
145 0, /* RPC method exports */
146 0, /* exported pseudo-variables */
147 0, /* response handling function */
148 mod_init, /* module initialization function */
149 child_init, /* per-child init function */
150 destroy /* module destroy function */
151 };
152 /* clang-format on */
153
154 /* Module initialization function */
155 static int mod_init(void)
156 {
157 LM_DBG("init lost module\n");
158
159 if(httpc_load_api(&httpapi) != 0) {
160 LM_ERR("can not bind to http_client API \n");
161 return -1;
162 }
163
164 if(held_loc_type.len > 0) {
165 held_loc_type.s = lost_held_type(
166 held_loc_type.s, &held_exact_type, &held_loc_type.len);
167 LM_DBG("**** init lost: held location type: %.*s \n", held_loc_type.len,
168 held_loc_type.s);
169 }
170
171 LM_DBG("**** init lost: held response time: %d \n", held_resp_time);
172
173 LM_DBG("**** init lost module done.\n");
174
175 return 0;
176 }
177
178 /* Child initialization function */
179 static int child_init(int rank)
180 {
181 return 0;
182 }
183
184 static void destroy(void)
185 {
186 if(held_loc_type.s != NULL && held_loc_type.len > 0) {
187 pkg_free(held_loc_type.s);
188 held_loc_type.s = NULL;
189 held_loc_type.len = 0;
190 }
191 /* do nothing */
192 }
193
194 /*
195 * Fix 4 lost_held_query params: con (string/pvar)
196 * and pidf, url, err (writable pvar).
197 */
198 static int fixup_lost_held_query(void **param, int param_no)
199 {
200 if(param_no == 1) {
201 return fixup_spve_null(param, 1);
202 }
203 if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
204 if(fixup_pvar_null(param, 1) != 0) {
205 LM_ERR("failed to fixup result pvar\n");
206 return -1;
207 }
208 if(((pv_spec_t *)(*param))->setf == NULL) {
209 LM_ERR("result pvar is not writable\n");
210 return -1;
211 }
212 return 0;
213 }
214 LM_ERR("invalid parameter number <%d>\n", param_no);
215 return -1;
216 }
217
218 /*
219 * Free lost_held_query params.
220 */
221 static int fixup_free_lost_held_query(void **param, int param_no)
222 {
223 if(param_no == 1) {
224 return fixup_spve_null(param, 1);
225 }
226 if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
227 return fixup_free_pvar_null(param, 1);
228 }
229 LM_ERR("invalid parameter number <%d>\n", param_no);
230 return -1;
231 }
232
233 /*
234 * Fix 5 lost_held_query_id params: con (string/pvar) id (string that may
235 * contain pvars) and pidf, url, err (writable pvar).
236 */
237 static int fixup_lost_held_query_id(void **param, int param_no)
238 {
239 if((param_no == 1) || (param_no == 2)) {
240 return fixup_spve_null(param, 1);
241 }
242 if((param_no == 3) || (param_no == 4) || (param_no == 5)) {
243 if(fixup_pvar_null(param, 1) != 0) {
244 LM_ERR("failed to fixup result pvar\n");
245 return -1;
246 }
247 if(((pv_spec_t *)(*param))->setf == NULL) {
248 LM_ERR("result pvar is not writable\n");
249 return -1;
250 }
251 return 0;
252 }
253 LM_ERR("invalid parameter number <%d>\n", param_no);
254 return -1;
255 }
256
257 /*
258 * Free lost_held_query_id params.
259 */
260 static int fixup_free_lost_held_query_id(void **param, int param_no)
261 {
262 if((param_no == 1) || (param_no == 2)) {
263 return fixup_free_spve_null(param, 1);
264 }
265 if((param_no == 3) || (param_no == 4) || (param_no == 5)) {
266 return fixup_free_pvar_null(param, 1);
267 }
268 LM_ERR("invalid parameter number <%d>\n", param_no);
269 return -1;
270 }
271
272 /*
273 * Fix 5 lost_held_dereference params: url (string/pvar), rtime (string/pvar),
274 * rtype (string/pvar) and pidf, err (writable pvar).
275 */
276 static int fixup_lost_held_deref(void **param, int param_no)
277 {
278 if((param_no == 1) || (param_no == 2) || (param_no == 3)) {
279 return fixup_spve_null(param, 1);
280 }
281 if((param_no == 4) || (param_no == 5)) {
282 if(fixup_pvar_null(param, 1) != 0) {
283 LM_ERR("failed to fixup result pvar\n");
284 return -1;
285 }
286 if(((pv_spec_t *)(*param))->setf == NULL) {
287 LM_ERR("result pvar is not writable\n");
288 return -1;
289 }
290 return 0;
291 }
292 LM_ERR("invalid parameter number <%d>\n", param_no);
293 return -1;
294 }
295
296 /*
297 * Free lost_held_dereference params.
298 */
299 static int fixup_free_lost_held_deref(void **param, int param_no)
300 {
301 if((param_no == 1) || (param_no == 2) || (param_no == 3)) {
302 return fixup_free_spve_null(param, 1);
303 }
304 if((param_no == 4) || (param_no == 5)) {
305 return fixup_free_pvar_null(param, 1);
306 }
307 LM_ERR("invalid parameter number <%d>\n", param_no);
308 return -1;
309 }
310
311 /*
312 * Fix 4 lost_query params: con (string/pvar)
313 * and uri, name, err (writable pvar).
314 */
315 static int fixup_lost_query(void **param, int param_no)
316 {
317 if(param_no == 1) {
318 return fixup_spve_null(param, 1);
319 }
320 if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
321 if(fixup_pvar_null(param, 1) != 0) {
322 LM_ERR("failed to fixup result pvar\n");
323 return -1;
324 }
325 if(((pv_spec_t *)(*param))->setf == NULL) {
326 LM_ERR("result pvar is not writable\n");
327 return -1;
328 }
329 return 0;
330 }
331 LM_ERR("invalid parameter number <%d>\n", param_no);
332 return -1;
333 }
334
335 /*
336 * Free lost_held_query_id params.
337 */
338 static int fixup_free_lost_query(void **param, int param_no)
339 {
340 if(param_no == 1) {
341 return fixup_free_spve_null(param, 1);
342 }
343 if((param_no == 2) || (param_no == 3) || (param_no == 4)) {
344 return fixup_free_pvar_null(param, 1);
345 }
346 LM_ERR("invalid parameter number <%d>\n", param_no);
347 return -1;
348 }
349
350 /*
351 * Fix 6 lost_query params: con (string/pvar) pidf, urn (string that may contain
352 * pvars) and uri, name, err (writable pvar).
353 */
354 static int fixup_lost_query_all(void **param, int param_no)
355 {
356 if(param_no == 1) {
357 return fixup_spve_null(param, 1);
358 }
359 if((param_no == 2) || (param_no == 3)) {
360 return fixup_spve_null(param, 1);
361 }
362 if((param_no == 4) || (param_no == 5) || (param_no == 6)) {
363 if(fixup_pvar_null(param, 1) != 0) {
364 LM_ERR("failed to fixup result pvar\n");
365 return -1;
366 }
367 if(((pv_spec_t *)(*param))->setf == NULL) {
368 LM_ERR("result pvar is not writable\n");
369 return -1;
370 }
371 return 0;
372 }
373 LM_ERR("invalid parameter number <%d>\n", param_no);
374 return -1;
375 }
376
377 /*
378 * Free lost_held_query_id params.
379 */
380 static int fixup_free_lost_query_all(void **param, int param_no)
381 {
382 if(param_no == 1) {
383 return fixup_free_spve_null(param, 1);
384 }
385 if((param_no == 2) || (param_no == 3)) {
386 return fixup_free_spve_null(param, 1);
387 }
388 if((param_no == 4) || (param_no == 5) || (param_no == 6)) {
389 return fixup_free_pvar_null(param, 1);
390 }
391 LM_ERR("invalid parameter number <%d>\n", param_no);
392 return -1;
393 }
394
395 /*
396 * Wrapper for lost_held_query w/o id
397 */
398 static int w_lost_held_query(
399 struct sip_msg *_m, char *_con, char *_pidf, char *_url, char *_err)
400 {
401 return lost_held_function(_m, _con, _pidf, _url, _err, NULL);
402 }
403
404 /*
405 * Wrapper for lost_held_query with id
406 */
407 static int w_lost_held_query_id(struct sip_msg *_m, char *_con, char *_id,
408 char *_pidf, char *_url, char *_err)
409 {
410 return lost_held_function(_m, _con, _pidf, _url, _err, _id);
411 }
412
413 /*
414 * Wrapper for lost_held_dereference
415 */
416 static int w_lost_held_deref(struct sip_msg *_m, char *_url, char *_rtime,
417 char *_rtype, char *_pidf, char *_err)
418 {
419 return lost_held_dereference(_m, _url, _pidf, _err, _rtime, _rtype);
420 }
421
422 /*
423 * Wrapper for lost_query w/o pidf, urn
424 */
425 static int w_lost_query(
426 struct sip_msg *_m, char *_con, char *_uri, char *_name, char *_err)
427 {
428 return lost_function(_m, _con, _uri, _name, _err, NULL, NULL);
429 }
430
431 /*
432 * Wrapper for lost_query with pidf, urn
433 */
434 static int w_lost_query_all(struct sip_msg *_m, char *_con, char *_pidf,
435 char *_urn, char *_uri, char *_name, char *_err)
436 {
437 return lost_function(_m, _con, _uri, _name, _err, _pidf, _urn);
438 }