Debian Codegraph @

src/modules/lost/functions.c

raw symbols SCIP
1 /*
2 * lost module functions
3 *
4 * Copyright (C) 2023 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 :: functions
30 * \ingroup lost
31 * Module: \ref lost
32 */
33 /*****************/
34
35 #include "../../modules/http_client/curl_api.h"
36
37 #include "../../core/mod_fix.h"
38 #include "../../core/pvar.h"
39 #include "../../core/route_struct.h"
40 #include "../../core/ut.h"
41 #include "../../core/trim.h"
42 #include "../../core/mem/mem.h"
43 #include "../../core/parser/msg_parser.h"
44 #include "../../core/parser/parse_body.h"
45 #include "../../core/lvalue.h"
46
47 #include "pidf.h"
48 #include "utilities.h"
49 #include "response.h"
50 #include "naptr.h"
51
52 #define LOST_SUCCESS 200
53 #define LOST_CLIENT_ERROR 400
54 #define LOST_SERVER_ERROR 500
55
56 #define HELD_DEFAULT_TYPE "geodetic locationURI"
57 #define HELD_DEFAULT_TYPE_LEN (sizeof(HELD_DEFAULT_TYPE) - 1)
58
59 #define NAPTR_LOST_SERVICE_HTTP "LoST:http"
60 #define NAPTR_LOST_SERVICE_HTTPS "LoST:https"
61 #define NAPTR_LIS_SERVICE_HELD "LIS:HELD"
62
63 #define ACCEPT_HDR \
64 "Accept: " \
65 "application/pidf+xml,application/" \
66 "held+xml;q=0.5"
67
68 extern httpc_api_t httpapi;
69
70 extern int lost_geoloc_type;
71 extern int lost_geoloc_order;
72 extern int lost_geoloc_3d;
73 extern int lost_verbose;
74 extern int held_resp_time;
75 extern int held_exact_type;
76 extern int held_post_req;
77 extern str held_loc_type;
78
79 char mtheld[] = "application/held+xml;charset=utf-8";
80 char mtlost[] = "application/lost+xml;charset=utf-8";
81
82 char uri_element[] = "uri";
83 char name_element[] = "displayName";
84 char errors_element[] = "errors";
85
86 /*
87 * lost_held_type(type, exact, lgth)
88 * verifies module params and returns valid HELD loaction type
89 * allocated in private memory
90 */
91 char *lost_held_type(char *type, int *exact, int *lgth)
92 {
93 char *ret = NULL;
94 char *tmp = NULL;
95 int len = 0;
96
97 ret = (char *)pkg_malloc(1);
98 if(ret == NULL)
99 goto err;
100
101 memset(ret, 0, 1);
102 *lgth = 0;
103
104 if(strstr(type, HELD_TYPE_ANY)) {
105 len = strlen(ret) + strlen(HELD_TYPE_ANY) + 1;
106 tmp = pkg_realloc(ret, len);
107 if(tmp == NULL)
108 goto err;
109 ret = tmp;
110 strcat(ret, HELD_TYPE_ANY);
111 *exact = 0;
112 } else {
113 if(strstr(type, HELD_TYPE_CIV)) {
114 len = strlen(ret) + strlen(HELD_TYPE_CIV) + 1;
115 tmp = pkg_realloc(ret, len);
116 if(tmp == NULL)
117 goto err;
118 ret = tmp;
119 strcat(ret, HELD_TYPE_CIV);
120 }
121 if(strstr(type, HELD_TYPE_GEO)) {
122 if(strlen(ret) > 1) {
123 len = strlen(ret) + strlen(HELD_TYPE_SEP) + 1;
124 tmp = pkg_realloc(ret, len);
125 if(tmp == NULL)
126 goto err;
127 ret = tmp;
128 strcat(ret, HELD_TYPE_SEP);
129 }
130 len = strlen(ret) + strlen(HELD_TYPE_GEO) + 1;
131 tmp = pkg_realloc(ret, len);
132 if(tmp == NULL)
133 goto err;
134 ret = tmp;
135 strcat(ret, HELD_TYPE_GEO);
136 }
137 if(strstr(type, HELD_TYPE_URI)) {
138 if(strlen(ret) > 1) {
139 len = strlen(ret) + strlen(HELD_TYPE_SEP) + 1;
140 tmp = pkg_realloc(ret, len);
141 if(tmp == NULL)
142 goto err;
143 ret = tmp;
144 strcat(ret, HELD_TYPE_SEP);
145 }
146 len = strlen(ret) + strlen(HELD_TYPE_URI) + 1;
147 tmp = pkg_realloc(ret, len);
148 if(tmp == NULL)
149 goto err;
150 ret = tmp;
151 strcat(ret, HELD_TYPE_URI);
152 }
153 }
154
155 *lgth = strlen(ret);
156 return ret;
157
158 err:
159 PKG_MEM_ERROR;
160 /* clean up */
161 if(ret != NULL) {
162 pkg_free(ret);
163 ret = NULL;
164 }
165 *lgth = 0;
166 return NULL;
167 }
168
169 /*
170 * lost_function_held(msg, con, pidf, url, err, id)
171 * assembles and runs HELD locationRequest, parses results
172 */
173 int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url,
174 char *_err, char *_id)
175 {
176 pv_spec_t *pspidf = NULL;
177 pv_spec_t *psurl = NULL;
178 pv_spec_t *pserr = NULL;
179
180 pv_value_t pvpidf;
181 pv_value_t pvurl;
182 pv_value_t pverr;
183
184 p_lost_held_t held = NULL;
185
186 xmlDocPtr doc = NULL;
187 xmlNodePtr root = NULL;
188 xmlNodePtr cur_node = NULL;
189
190 str geo = STR_NULL; /* return value geolocation uri */
191 str egeo = STR_NULL; /* entire value geolocation uri */
192 str res = STR_NULL; /* return value pidf */
193 str err = STR_NULL; /* return value error */
194
195 str url = STR_NULL;
196 str did = STR_NULL;
197 str que = STR_NULL;
198 str con = STR_NULL;
199 str host = STR_NULL;
200 str name = STR_NULL;
201 str idhdr = STR_NULL;
202 str pidfurl = STR_NULL;
203
204 static str rtype = STR_STATIC_INIT(HELD_DEFAULT_TYPE);
205 static str sheld = STR_STATIC_INIT(NAPTR_LIS_SERVICE_HELD);
206
207 char ustr[MAX_URI_SIZE];
208 char istr[NI_MAXHOST];
209 char *ipstr = NULL;
210 char *lisurl = NULL;
211 char *heldreq = NULL;
212
213 int len = 0;
214 int curl = 0;
215 int flag = 0;
216 int naptr = 0;
217 int presence = 0;
218 int res_error = 0;
219
220 if(_pidf == NULL || _url == NULL || _err == NULL) {
221 LM_ERR("invalid parameter\n");
222 goto err;
223 }
224
225 /* module parameter */
226 if(held_loc_type.len > 0) {
227 rtype.s = held_loc_type.s;
228 rtype.len = held_loc_type.len;
229 }
230 /* connection from parameter */
231 if(_con) {
232 if(get_str_fparam(&con, _m, (gparam_p)_con) != 0) {
233 LM_ERR("cannot get connection string\n");
234 goto err;
235 }
236 /* check if connection exists */
237 if(con.s != NULL && con.len > 0) {
238 if(httpapi.http_connection_exists(&con) == 0) {
239 LM_ERR("connection: [%s] does not exist\n", con.s);
240 goto err;
241 }
242 }
243 }
244 /* id from parameter */
245 if(_id) {
246 if(get_str_fparam(&did, _m, (gparam_p)_id) != 0) {
247 LM_ERR("cannot get device id\n");
248 goto err;
249 }
250 if(did.len == 0) {
251 LM_ERR("no device id found\n");
252 goto err;
253 }
254 } else {
255
256 LM_DBG("parsing P-A-I header\n");
257
258 /* id from P-A-I header */
259 idhdr.s = lost_get_pai_header(_m, &idhdr.len);
260 if(idhdr.len == 0) {
261 LM_WARN("P-A-I header not found, trying From header ...\n");
262
263 LM_DBG("parsing From header\n");
264
265 lost_free_string(&idhdr);
266 /* id from From header */
267 idhdr.s = lost_get_from_header(_m, &idhdr.len);
268 if(idhdr.len == 0) {
269 LM_ERR("no device id found\n");
270 goto err;
271 }
272 }
273 did.s = idhdr.s;
274 did.len = idhdr.len;
275 }
276 LM_INFO("### HELD id [%.*s]\n", did.len, did.s);
277 /* assemble locationRequest */
278 held = lost_new_held(did, rtype, held_resp_time, held_exact_type);
279 if(held == NULL) {
280 LM_ERR("held object allocation failed\n");
281 goto err;
282 }
283 que.s = lost_held_location_request(held, &que.len);
284 lost_free_held(&held); /* clean up */
285 if(que.len == 0) {
286 LM_ERR("held request document error\n");
287 goto err;
288 }
289
290 LM_DBG("held location request: [%s]\n", que.s);
291
292 /* send locationRequest to location server - HTTP POST */
293 if(con.s != NULL && con.len > 0) {
294
295 LM_DBG("using connection [%.*s]\n", con.len, con.s);
296
297 /* send via connection */
298 curl = httpapi.http_connect(_m, &con, NULL, &res, mtheld, &que);
299 } else {
300 /* we have no connection ... do a NAPTR lookup */
301 if(lost_parse_host(did.s, &host, &flag) > 0) {
302
303 LM_DBG("no conn. trying NAPTR lookup [%.*s]\n", host.len, host.s);
304
305 /* remove '[' and ']' from string (IPv6) */
306 if(flag == AF_INET6) {
307 host.s++;
308 host.len = host.len - 2;
309 }
310 /* is it a name or ip ... check nameinfo (reverse lookup) */
311 len = 0;
312 ipstr = lost_copy_string(host, &len);
313 if(ipstr != NULL) {
314 name.s = &(istr[0]);
315 name.len = NI_MAXHOST;
316 if(lost_get_nameinfo(ipstr, &name, flag) > 0) {
317
318 LM_DBG("ip [%s] to name [%.*s]\n", ipstr, name.len, name.s);
319
320 /* change ip string to name */
321 host.s = name.s;
322 host.len = name.len;
323 } else {
324
325 /* keep string */
326 LM_DBG("no nameinfo for [%s]\n", ipstr);
327 }
328 pkg_free(ipstr); /* clean up */
329 ipstr = NULL;
330 } else {
331 LM_ERR("could not copy host info\n");
332 }
333 url.s = &(ustr[0]);
334 url.len = MAX_URI_SIZE;
335 if((naptr = lost_naptr_lookup(host, &sheld, &url)) == 0) {
336 LM_ERR("NAPTR failed on [%.*s]\n", host.len, host.s);
337 goto err;
338 }
339 } else {
340 LM_ERR("failed to get location service for [%.*s]\n", did.len,
341 did.s);
342 goto err;
343 }
344
345 LM_DBG("NAPTR lookup returned [%.*s]\n", url.len, url.s);
346
347 /* curl doesn't like str */
348 len = 0;
349 lisurl = lost_copy_string(url, &len);
350 if(lisurl == NULL) {
351 LM_ERR("could not copy POST url\n");
352 goto err;
353 }
354 /* send to service */
355 curl = httpapi.http_client_query_c(
356 _m, lisurl, &res, que.s, mtheld, ACCEPT_HDR);
357 pkg_free(lisurl); /*clean up */
358 lisurl = NULL;
359 }
360 /* only HTTP 2xx responses are accepted */
361 if(curl >= 300 || curl < 100) {
362 if(con.s != NULL && con.len > 0) {
363 LM_ERR("[%.*s] failed with error: %d\n", con.len, con.s, curl);
364 } else {
365 LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl);
366 }
367 goto err;
368 }
369 if(con.s != NULL && con.len > 0) {
370
371 LM_DBG("[%.*s] returned: %d\n", con.len, con.s, curl);
372
373 } else {
374
375 LM_DBG("[%.*s] returned: %d\n", url.len, url.s, curl);
376 }
377 did.s = NULL;
378 did.len = 0;
379 /* clean up */
380 lost_free_string(&idhdr);
381 lost_free_string(&que);
382 /* read and parse the returned xml */
383 doc = xmlReadMemory(res.s, res.len, 0, NULL,
384 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA);
385 if(doc == NULL) {
386 LM_WARN("invalid xml document: [%.*s]\n", res.len, res.s);
387 doc = xmlReadMemory(res.s, res.len, 0, NULL,
388 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA
389 | XML_PARSE_RECOVER);
390 if(doc == NULL) {
391 LM_ERR("xml document recovery failed on: [%.*s]\n", res.len, res.s);
392 goto err;
393 }
394
395 LM_DBG("xml document recovered\n");
396 }
397 root = xmlDocGetRootElement(doc);
398 if(root == NULL) {
399 LM_ERR("empty xml document\n");
400 goto err;
401 }
402 /* check the root element ... shall be locationResponse, or error */
403 if(xmlStrcmp(root->name, (const xmlChar *)"locationResponse") == 0) {
404
405 LM_DBG("HELD location response [%.*s]\n", res.len, res.s);
406
407 for(cur_node = root->children; cur_node; cur_node = cur_node->next) {
408 if(cur_node->type == XML_ELEMENT_NODE) {
409 if(xmlStrcmp(cur_node->name, (const xmlChar *)"locationUriSet")
410 == 0) {
411
412 LM_DBG("*** node '%s' found\n", cur_node->name);
413
414 lost_free_string(&egeo);
415 /* get the locationUri element */
416 egeo.s = lost_get_content(
417 root, (char *)HELD_TYPE_URI, &egeo.len);
418 if(egeo.len == 0) {
419 LM_WARN("%s element not found\n", HELD_TYPE_URI);
420 lost_free_string(&egeo);
421 } else {
422 geo.len = egeo.len;
423 geo.s = lost_trim_content(egeo.s, &geo.len);
424 }
425 }
426 if(xmlStrcmp(cur_node->name, (const xmlChar *)"presence")
427 == 0) {
428
429 LM_DBG("*** node '%s' found\n", cur_node->name);
430
431 /* response contains presence node */
432 presence = 1;
433 }
434 }
435 }
436 /* if we do not have a presence node but a location URI */
437 /* dereference pidf.lo at location server via HTTP GET */
438 if((presence == 0) && (geo.s != NULL && geo.len > 0)) {
439 LM_INFO("presence node not found in HELD response, trying URI "
440 "...\n");
441 if(held_post_req == 0) {
442 curl = httpapi.http_client_query_c(
443 _m, geo.s, &pidfurl, NULL, mtheld, ACCEPT_HDR);
444 } else {
445 len = 0;
446 heldreq = lost_held_post_request(&len, 0, NULL);
447 if(heldreq == NULL) {
448 LM_ERR("could not create POST request\n");
449 goto err;
450 }
451
452 LM_DBG("held POST request: [%.*s]\n", len, heldreq);
453
454 curl = httpapi.http_client_query_c(
455 _m, geo.s, &pidfurl, heldreq, mtheld, ACCEPT_HDR);
456 pkg_free(heldreq); /* clean up */
457 heldreq = NULL;
458 }
459 /* only HTTP 2xx responses are accepted */
460 if(curl >= 300 || curl < 100) {
461 LM_ERR("GET [%.*s] failed with error: %d\n", pidfurl.len,
462 pidfurl.s, curl);
463 goto err;
464 }
465 if(pidfurl.len == 0) {
466 LM_WARN("HELD location request failed [%.*s]\n", geo.len,
467 geo.s);
468 } else {
469
470 LM_DBG("HELD location response [%.*s]\n", pidfurl.len,
471 pidfurl.s);
472
473 res.s = pidfurl.s;
474 res.len = pidfurl.len;
475 }
476 }
477 /* error received */
478 } else if(xmlStrcmp(root->name, (const xmlChar *)"error") == 0) {
479
480 LM_DBG("HELD error response [%.*s]\n", res.len, res.s);
481
482 /* get the error property */
483 err.s = lost_get_property(root, (char *)"code", &err.len);
484 if(err.len == 0) {
485 LM_ERR("error - property not found: [%.*s]\n", res.len, res.s);
486 goto err;
487 }
488 LM_WARN("locationRequest error response: [%.*s]\n", err.len, err.s);
489 } else {
490 LM_ERR("root element is not valid: [%.*s]\n", res.len, res.s);
491 goto err;
492 }
493
494 /* clean up */
495 xmlFreeDoc(doc);
496 doc = NULL;
497
498 /* set writeable pvars */
499 pvpidf.rs = res;
500 pvpidf.rs.s = res.s;
501 pvpidf.rs.len = res.len;
502
503 pvpidf.flags = PV_VAL_STR;
504 pspidf = (pv_spec_t *)_pidf;
505 pspidf->setf(_m, &pspidf->pvp, (int)EQ_T, &pvpidf);
506 lost_free_string(&res); /* clean up */
507
508 pvurl.rs = geo;
509 pvurl.rs.s = geo.s;
510 pvurl.rs.len = geo.len;
511
512 pvurl.flags = PV_VAL_STR;
513 psurl = (pv_spec_t *)_url;
514 psurl->setf(_m, &psurl->pvp, (int)EQ_T, &pvurl);
515 lost_free_string(&egeo); /* clean up */
516
517 /* return error code in case of response error */
518 if(err.len > 0) {
519 res_error = 1;
520 }
521 pverr.rs = err;
522 pverr.rs.s = err.s;
523 pverr.rs.len = err.len;
524
525 pverr.flags = PV_VAL_STR;
526 pserr = (pv_spec_t *)_err;
527 pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
528 lost_free_string(&err); /* clean up */
529
530 return (res_error > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
531
532 err:
533 /* clean up pointer */
534 lost_free_string(&que);
535 lost_free_string(&idhdr);
536 lost_free_string(&pidfurl);
537 /* clean up xml */
538 if(doc != NULL) {
539 xmlFreeDoc(doc);
540 }
541 /* clean up string */
542 lost_free_string(&res);
543 lost_free_string(&egeo);
544 lost_free_string(&err);
545
546 return LOST_CLIENT_ERROR;
547 }
548
549
550 /*
551 * lost_held_dereference(msg, url, pidf, err, rtime, rtype)
552 * assembles and runs HELD locationRequest (POST), returns result as pidf
553 */
554 int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf,
555 char *_err, char *_rtime, char *_rtype)
556 {
557 pv_spec_t *pspidf;
558 pv_spec_t *pserr;
559
560 pv_value_t pvpidf;
561 pv_value_t pverr;
562
563 xmlDocPtr doc = NULL;
564 xmlNodePtr root = NULL;
565
566 str url = STR_NULL;
567 str rtm = STR_NULL;
568 str rtp = STR_NULL;
569
570 str res = STR_NULL; /* return value location response */
571 str err = STR_NULL; /* return value error */
572
573 char *ptr = NULL;
574 char *lisurl = NULL;
575 char *heldreq = NULL;
576 char *rtype = NULL;
577
578 long ltime = 0;
579 long rtime = 0;
580
581 int len = 0;
582 int curl = 0;
583 int exact = 0;
584 int ret = LOST_SUCCESS;
585
586 if(_url == NULL || _rtime == NULL || _pidf == NULL || _rtype == NULL
587 || _err == NULL) {
588 LM_ERR("invalid parameter\n");
589 goto err;
590 }
591
592 /* dereference url from parameter */
593 if(_url) {
594 if(get_str_fparam(&url, _m, (gparam_p)_url) != 0) {
595 LM_ERR("cannot get dereference url\n");
596 goto err;
597 }
598 if(url.len == 0) {
599 LM_ERR("no dereference url found\n");
600 goto err;
601 }
602 }
603
604 /* response time from parameter */
605 if(_rtime) {
606 if(get_str_fparam(&rtm, _m, (gparam_p)_rtime) != 0) {
607 LM_ERR("cannot get response time\n");
608 goto err;
609 }
610 if(rtm.len == 0) {
611 /* default: rtime = 0 */
612 LM_WARN("no response time found\n");
613 } else {
614 ltime = strtol(rtm.s, &ptr, 10);
615 /* look for a number ... */
616 if((ltime > 0) && (strlen(ptr) == 0)) {
617 /* responseTime: milliseconds */
618 rtime = ltime;
619 /* or a string */
620 } else if((ltime == 0) && (strlen(ptr) > 0)) {
621 if(strncasecmp(ptr, HELD_ED, strlen(HELD_ED)) == 0) {
622 /* responseTime: emergencyDispatch */
623 rtime = -1;
624 } else if(strncasecmp(ptr, HELD_ER, strlen(HELD_ER)) == 0) {
625 /* responseTime: emergencyRouting */
626 rtime = 0;
627 }
628 }
629 }
630 }
631
632 /* response type from parameter */
633 if(_rtype) {
634 if(get_str_fparam(&rtp, _m, (gparam_p)_rtype) != 0) {
635 LM_ERR("cannot get response type\n");
636 goto err;
637 }
638 if(rtp.len == 0) {
639 LM_WARN("no response type found\n");
640 rtype = NULL;
641 } else {
642 len = 0;
643 /* response type string sanity check */
644 rtype = lost_held_type(rtp.s, &exact, &len);
645 /* default value will be used if nothing was returned */
646 if(rtype == NULL) {
647 LM_WARN("cannot normalize [%.*s]\n", rtp.len, rtp.s);
648 }
649 }
650 }
651
652 /* get the HELD request body */
653 heldreq = lost_held_post_request(&len, rtime, rtype);
654
655 /* clean up */
656 if(rtype != NULL) {
657 pkg_free(rtype);
658 rtype = NULL;
659 }
660
661 if(heldreq == NULL) {
662 LM_ERR("could not create POST request\n");
663 goto err;
664 }
665
666 LM_DBG("POST request: [%.*s]\n", len, heldreq);
667
668 /* curl doesn't like str */
669 len = 0;
670 lisurl = lost_copy_string(url, &len);
671 if(lisurl == NULL) {
672 LM_ERR("could not copy POST url\n");
673 pkg_free(heldreq); /* clean up */
674 heldreq = NULL;
675 goto err;
676 }
677
678 LM_DBG("POST url: [%.*s]\n", len, lisurl);
679
680 curl = httpapi.http_client_query_c(
681 _m, lisurl, &res, heldreq, mtheld, ACCEPT_HDR);
682 pkg_free(lisurl); /* clean up */
683 lisurl = NULL;
684 pkg_free(heldreq);
685 heldreq = NULL;
686
687 /* only HTTP 2xx responses are accepted */
688 if(curl >= 300 || curl < 100) {
689 LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl);
690 goto err;
691 }
692 if(res.s != NULL && res.len > 0) {
693
694 LM_DBG("LbR pidf-lo: [%.*s]\n", res.len, res.s);
695
696 } else {
697 LM_ERR("dereferencing location failed\n");
698 goto err;
699 }
700
701 /* read and parse the returned xml */
702 doc = xmlReadMemory(res.s, res.len, 0, NULL,
703 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA);
704 if(doc == NULL) {
705 LM_WARN("invalid xml document: [%.*s]\n", res.len, res.s);
706 doc = xmlReadMemory(res.s, res.len, 0, NULL,
707 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA
708 | XML_PARSE_RECOVER);
709 if(doc == NULL) {
710 LM_ERR("xml document recovery failed on: [%.*s]\n", res.len, res.s);
711 goto err;
712 }
713
714 LM_DBG("xml document recovered\n");
715 }
716 root = xmlDocGetRootElement(doc);
717 if(root == NULL) {
718 LM_ERR("empty xml document\n");
719 goto err;
720 }
721
722 /* check root element ... shall be presence|locationResponse, or error */
723 if((!xmlStrcmp(root->name, (const xmlChar *)"presence"))
724 || (!xmlStrcmp(root->name, (const xmlChar *)"locationResponse"))) {
725
726 LM_DBG("HELD location response [%.*s]\n", res.len, res.s);
727
728 /* check content and set response code
729 * + 0 nothing found: return 200
730 * + 1 reference found: return 201
731 * + 2 value found: return 202
732 * + 3 value and reference found: return 203
733 */
734 ret += lost_check_HeldResponse(root);
735 /* error received */
736 } else if(xmlStrcmp(root->name, (const xmlChar *)"error") == 0) {
737
738 LM_DBG("HELD error response [%.*s]\n", res.len, res.s);
739
740 /* get the error property */
741 err.s = lost_get_property(root, (char *)"code", &err.len);
742 if(err.len == 0) {
743 LM_ERR("error - property not found: [%.*s]\n", res.len, res.s);
744 goto err;
745 }
746 LM_WARN("locationRequest error response: [%.*s]\n", err.len, err.s);
747 } else {
748 LM_ERR("root element is not valid: [%.*s]\n", res.len, res.s);
749 goto err;
750 }
751
752 /* clean up */
753 xmlFreeDoc(doc);
754 doc = NULL;
755
756 /* set writeable pvars */
757 pvpidf.rs = res;
758 pvpidf.rs.s = res.s;
759 pvpidf.rs.len = res.len;
760
761 pvpidf.flags = PV_VAL_STR;
762 pspidf = (pv_spec_t *)_pidf;
763 pspidf->setf(_m, &pspidf->pvp, (int)EQ_T, &pvpidf);
764 lost_free_string(&res); /* clean up */
765
766 /* return error code in case of response error */
767 if(err.len > 0) {
768 ret = LOST_SERVER_ERROR;
769 }
770 pverr.rs = err;
771 pverr.rs.s = err.s;
772 pverr.rs.len = err.len;
773
774 pverr.flags = PV_VAL_STR;
775 pserr = (pv_spec_t *)_err;
776 pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
777 lost_free_string(&err); /* clean up */
778
779 return ret;
780
781 err:
782 /* clean up xml */
783 if(doc != NULL) {
784 xmlFreeDoc(doc);
785 }
786 /* clean up string */
787 lost_free_string(&res);
788 lost_free_string(&err);
789
790 return LOST_CLIENT_ERROR;
791 }
792
793 /*
794 * lost_function(msg, con, pidf, uri, name, err, pidf, urn)
795 * assembles and runs LOST findService request, parses results
796 */
797 int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name,
798 char *_err, char *_pidf, char *_urn)
799 {
800 pv_spec_t *psname;
801 pv_spec_t *psuri;
802 pv_spec_t *pserr;
803
804 pv_value_t pvname;
805 pv_value_t pvuri;
806 pv_value_t pverr;
807
808 p_lost_loc_t loc = NULL;
809 p_lost_geolist_t geolist = NULL;
810 p_lost_fsr_t fsrdata = NULL;
811
812 str name = STR_NULL; /* return value displayName */
813 str uri = STR_NULL; /* return value uri */
814 str err = STR_NULL; /* return value error */
815
816 str tmp = STR_NULL;
817 str url = STR_NULL;
818 str urn = STR_NULL;
819 str req = STR_NULL;
820 str con = STR_NULL;
821 str ret = STR_NULL;
822 str src = STR_NULL;
823 str pidf = STR_NULL;
824 str rereq = STR_NULL;
825 str oldurl = STR_NULL;
826 str losturl = STR_NULL;
827
828 static str shttp = STR_STATIC_INIT(NAPTR_LOST_SERVICE_HTTP);
829 static str shttps = STR_STATIC_INIT(NAPTR_LOST_SERVICE_HTTPS);
830
831 struct msg_start *fl;
832
833 char ustr[MAX_URI_SIZE];
834 char *search = NULL;
835 char *geoval = NULL;
836 char *urlrep = NULL;
837 char *heldreq = NULL;
838
839 int geotype = 0;
840 int redirect = 0;
841 int curl = 0;
842 int len = 0;
843 int naptr = 0;
844 int geoitems = 0;
845 int res_error = 0;
846
847 if(_con == NULL || _uri == NULL || _name == NULL || _err == NULL) {
848 LM_ERR("invalid parameter\n");
849 goto err;
850 }
851 /* connection from parameter */
852 if(_con) {
853 if(get_str_fparam(&con, _m, (gparam_p)_con) != 0) {
854 LM_ERR("cannot get connection string\n");
855 goto err;
856 }
857 /* check if connection exists */
858 if(con.s != NULL && con.len > 0) {
859 if(httpapi.http_connection_exists(&con) == 0) {
860 LM_WARN("connection: [%.*s] does not exist\n", con.len, con.s);
861 /* check if NAPTR lookup works with connection parameter */
862 losturl.s = &(ustr[0]);
863 losturl.len = MAX_URI_SIZE;
864 if((naptr = lost_naptr_lookup(con, &shttps, &losturl)) == 0) {
865 naptr = lost_naptr_lookup(con, &shttp, &losturl);
866 }
867 if(naptr == 0) {
868 LM_ERR("NAPTR failed on [%.*s]\n", con.len, con.s);
869 goto err;
870 }
871 }
872 }
873 }
874 /* urn from parameter */
875 if(_urn) {
876 if(get_str_fparam(&urn, _m, (gparam_p)_urn) != 0) {
877 LM_ERR("cannot get service urn parameter\n");
878 goto err;
879 }
880 }
881 /* urn from request line */
882 if(urn.len == 0) {
883
884 LM_DBG("no service urn parameter, trying request line ...\n");
885
886 fl = &(_m->first_line);
887 urn.len = fl->u.request.uri.len;
888 urn.s = fl->u.request.uri.s;
889 }
890 /* check urn scheme */
891 search = urn.s;
892 if(is_urn(search) > 0) {
893 LM_INFO("### LOST urn\t[%.*s]\n", urn.len, urn.s);
894 } else {
895 LM_ERR("service urn not found\n");
896 goto err;
897 }
898 /* pidf from parameter */
899 if(_pidf) {
900 if(get_str_fparam(&pidf, _m, (gparam_p)_pidf) != 0) {
901 LM_ERR("cannot get pidf parameter\n");
902 } else {
903
904 LM_DBG("parsing pidf parameter ...\n");
905
906 if(pidf.s != NULL && pidf.len > 0) {
907
908 LM_DBG("pidf: [%.*s]\n", pidf.len, pidf.s);
909
910 /* parse the pidf and get loc object */
911 loc = lost_parse_pidf(pidf, urn);
912 }
913 }
914 }
915 /* neither valid pidf parameter nor loc ... check geolocation header */
916 if(loc == NULL) {
917
918 /* parse Geolocation header */
919
920 LM_DBG("parsing geolocation header ...\n");
921
922 geolist = lost_get_geolocation_header(_m, &geoitems);
923
924 if(geoitems == 0) {
925 LM_ERR("geolocation header not found\n");
926 goto err;
927 }
928
929 LM_DBG("number of location URIs: %d\n", geoitems);
930
931 if(lost_geoloc_order == 0) {
932
933 LM_DBG("reversing location URI sequence\n");
934
935 lost_reverse_geoheader_list(&geolist);
936 }
937 switch(lost_geoloc_type) {
938 case ANY: /* type: 0 */
939 geoval = lost_get_geoheader_value(geolist, ANY, &geotype);
940
941 LM_DBG("geolocation header field (any): %s\n", geoval);
942
943 break;
944 case CID: /* type: 1 */
945 geoval = lost_get_geoheader_value(geolist, CID, &geotype);
946
947 LM_DBG("geolocation header field (LbV): %s\n", geoval);
948
949 break;
950 case HTTP: /* type: 2 */
951 geoval = lost_get_geoheader_value(geolist, HTTP, &geotype);
952 /* fallback to https */
953 if(geoval == NULL) {
954 LM_WARN("no valid http URL ... trying https\n");
955 geoval = lost_get_geoheader_value(geolist, HTTPS, &geotype);
956 }
957
958 LM_DBG("geolocation header field (LbR): %s\n", geoval);
959
960 break;
961 case HTTPS: /* type: 3 */
962 /* prefer https */
963 geoval = lost_get_geoheader_value(geolist, HTTPS, &geotype);
964 /* fallback to http */
965 if(geoval == NULL) {
966 LM_WARN("no valid https URL ... trying http\n");
967 geoval = lost_get_geoheader_value(geolist, HTTP, &geotype);
968 }
969
970 LM_DBG("geolocation header field (LbR): %s\n", geoval);
971
972 break;
973 default:
974 LM_WARN("unknown module parameter value\n");
975 geoval = lost_get_geoheader_value(geolist, UNKNOWN, &geotype);
976
977 LM_DBG("geolocation header field (any): %s\n", geoval);
978
979 break;
980 }
981 if(geoval == NULL) {
982 LM_ERR("invalid geolocation header\n");
983 goto err;
984 }
985 LM_INFO("### LOST loc\t[%s]\n", geoval);
986 /* clean up */
987 pidf.s = NULL;
988 pidf.len = 0;
989 /* use location by value */
990 if(geotype == CID) {
991 /* get body part - filter=>content-indirection */
992 pidf.s = get_body_part_by_filter(_m, 0, 0, geoval, NULL, &pidf.len);
993 if(pidf.s != NULL && pidf.len > 0) {
994
995 LM_DBG("LbV pidf-lo: [%.*s]\n", pidf.len, pidf.s);
996
997 } else {
998 LM_WARN("no multipart body found\n");
999 }
1000 }
1001 /* use location by reference */
1002 if((geotype == HTTPS) || (geotype == HTTP)) {
1003 url.s = geoval;
1004 url.len = strlen(geoval);
1005 /* ! dereference pidf.lo at location server - HTTP GET */
1006 /* ! requires hack in http_client module */
1007 /* ! functions.c => http_client_query => query_params.oneline = 0; */
1008 if(held_post_req == 0) {
1009 curl = httpapi.http_client_query_c(
1010 _m, url.s, &ret, NULL, mtheld, ACCEPT_HDR);
1011 } else {
1012 len = 0;
1013 heldreq = lost_held_post_request(&len, 0, NULL);
1014 if(heldreq == NULL) {
1015 LM_ERR("could not create POST request\n");
1016 goto err;
1017 }
1018
1019 LM_DBG("POST request: [%.*s]\n", len, heldreq);
1020
1021 curl = httpapi.http_client_query_c(
1022 _m, url.s, &ret, heldreq, mtheld, ACCEPT_HDR);
1023 pkg_free(heldreq); /* clean up */
1024 heldreq = NULL;
1025 }
1026 /* only HTTP 2xx responses are accepted */
1027 if(curl >= 300 || curl < 100) {
1028 if(held_post_req == 0) {
1029 LM_ERR("GET [%.*s] failed with error: %d\n", url.len, url.s,
1030 curl);
1031 } else {
1032 LM_ERR("POST [%.*s] failed with error: %d\n", url.len,
1033 url.s, curl);
1034 }
1035 /* clean up */
1036 lost_free_string(&ret);
1037 goto err;
1038 }
1039 url.s = NULL;
1040 url.len = 0;
1041 pidf.s = ret.s;
1042 pidf.len = ret.len;
1043 if(pidf.s != NULL && pidf.len > 0) {
1044
1045 LM_DBG("LbR pidf-lo: [%.*s]\n", pidf.len, pidf.s);
1046
1047 } else {
1048 LM_WARN("dereferencing location failed\n");
1049 }
1050 }
1051 /* clean up */
1052 lost_free_geoheader_list(&geolist);
1053 lost_free_string(&ret);
1054
1055 if(pidf.s == NULL && pidf.len == 0) {
1056 LM_ERR("location object not found\n");
1057 goto err;
1058 }
1059 /* parse the pidf and get loc object */
1060 loc = lost_parse_pidf(pidf, urn);
1061 }
1062 /* pidf parsing failed ... return */
1063 if(loc == NULL) {
1064 LM_ERR("parsing pidf failed\n");
1065 goto err;
1066 }
1067 /* assemble findService request */
1068 req.s = lost_find_service_request(loc, NULL, &req.len);
1069
1070 if(req.s == NULL && req.len == 0) {
1071 LM_ERR("lost request failed\n");
1072 goto err;
1073 }
1074
1075 LM_DBG("findService request: [%.*s]\n", req.len, req.s);
1076
1077 /* send findService request to mapping server - HTTP POST */
1078 if(naptr) {
1079 /* copy url */
1080 len = 0;
1081 urlrep = lost_copy_string(url, &len);
1082 if(urlrep == NULL) {
1083 LM_ERR("could not copy POST url\n");
1084 goto err;
1085 }
1086 /* send request */
1087 curl = httpapi.http_client_query(_m, urlrep, &ret, req.s, mtlost);
1088 pkg_free(urlrep); /* clean up */
1089 urlrep = NULL;
1090 } else {
1091 curl = httpapi.http_connect(_m, &con, NULL, &ret, mtlost, &req);
1092 }
1093 /* only HTTP 2xx responses are accepted */
1094 if(curl >= 300 || curl < 100) {
1095 if(naptr) {
1096 LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl);
1097 } else {
1098 LM_ERR("[%.*s] failed with error: %d\n", con.len, con.s, curl);
1099 }
1100 lost_free_string(&ret);
1101 goto err;
1102 }
1103
1104 if(naptr) {
1105 LM_DBG("[%.*s] returned: %d\n", url.len, url.s, curl);
1106 } else {
1107 LM_DBG("[%.*s] returned: %d\n", con.len, con.s, curl);
1108 }
1109
1110 if(ret.len == 0) {
1111 LM_ERR("findService request failed\n");
1112 goto err;
1113 }
1114
1115 LM_DBG("findService response: [%.*s]\n", ret.len, ret.s);
1116
1117 /* at least parse one response */
1118 redirect = 1;
1119 while(redirect) {
1120 fsrdata = lost_parse_findServiceResponse(ret);
1121 if(lost_verbose == 1) {
1122 lost_print_findServiceResponse(fsrdata);
1123 }
1124 switch(fsrdata->category) {
1125 case RESPONSE:
1126 if(fsrdata->uri != NULL) {
1127 /* get the first sips uri element ... */
1128 if(lost_search_response_list(&fsrdata->uri, &tmp.s, SIPS_S)
1129 > 0) {
1130 tmp.len = strlen(tmp.s);
1131 /* or the first sip uri element ... */
1132 } else if(lost_search_response_list(
1133 &fsrdata->uri, &tmp.s, SIP_S)
1134 > 0) {
1135 tmp.len = strlen(tmp.s);
1136 /* or return error if nothing found */
1137 } else {
1138 LM_ERR("sip/sips uri not found: [%.*s]\n", ret.len,
1139 ret.s);
1140 goto err;
1141 }
1142 /* copy uri string */
1143 if(pkg_str_dup(&uri, &tmp) < 0) {
1144 LM_ERR("could not copy: [%.*s]\n", tmp.len, tmp.s);
1145 goto err;
1146 }
1147 } else {
1148 LM_ERR("uri element not found: [%.*s]\n", ret.len, ret.s);
1149 goto err;
1150 }
1151 if(fsrdata->mapping != NULL) {
1152 /* get the displayName element */
1153 if((tmp.s = fsrdata->mapping->name->text) != NULL) {
1154 tmp.len = strlen(fsrdata->mapping->name->text);
1155 if(pkg_str_dup(&name, &tmp) < 0) {
1156 LM_ERR("could not copy: [%.*s]\n", tmp.len, tmp.s);
1157 goto err;
1158 }
1159 }
1160 } else {
1161 LM_ERR("name not found: [%.*s]\n", ret.len, ret.s);
1162 goto err;
1163 }
1164 /* we are done */
1165 redirect = 0;
1166 break;
1167 case ERROR:
1168 /* get the errors element */
1169 if(fsrdata->errors != NULL) {
1170 if((tmp.s = fsrdata->errors->issue->type) != NULL) {
1171 tmp.len = strlen(fsrdata->errors->issue->type);
1172 if(pkg_str_dup(&err, &tmp) < 0) {
1173 LM_ERR("could not copy: [%.*s]\n", tmp.len, tmp.s);
1174 goto err;
1175 }
1176 }
1177 /* clean up */
1178 tmp.s = NULL;
1179 tmp.len = 0;
1180 } else {
1181 LM_ERR("errors not found: [%.*s]\n", ret.len, ret.s);
1182 goto err;
1183 }
1184 /* we are done */
1185 redirect = 0;
1186 break;
1187 case REDIRECT:
1188 /* get the target element */
1189 if(fsrdata->redirect != NULL) {
1190 if((tmp.s = fsrdata->redirect->target) != NULL) {
1191 tmp.len = strlen(fsrdata->redirect->target);
1192 url.s = &(ustr[0]);
1193 url.len = MAX_URI_SIZE;
1194 /* check loop ... current response */
1195 if(oldurl.s != NULL && oldurl.len > 0) {
1196 if(str_strcasecmp(&tmp, &oldurl) == 0) {
1197 LM_ERR("loop detected: "
1198 "[%.*s]<-->[%.*s]\n",
1199 oldurl.len, oldurl.s, tmp.len, tmp.s);
1200 goto err;
1201 }
1202 }
1203 /* add redirecting source to path list */
1204 if((src.s = fsrdata->redirect->source) != NULL) {
1205 src.len = strlen(fsrdata->redirect->source);
1206 if(lost_append_response_list(&fsrdata->path, src)
1207 == 0) {
1208 LM_ERR("could not append server to path "
1209 "elememt\n");
1210 goto err;
1211 }
1212 }
1213 /* clean up */
1214 src.s = NULL;
1215 src.len = 0;
1216 /* check loop ... path elements */
1217 char *via = NULL;
1218 if(lost_search_response_list(
1219 &fsrdata->path, &via, tmp.s)
1220 > 0) {
1221 LM_ERR("loop detected: "
1222 "[%s]<-->[%.*s]\n",
1223 via, tmp.len, tmp.s);
1224 goto err;
1225 }
1226 /* remember the redirect target */
1227 if(pkg_str_dup(&oldurl, &tmp) < 0) {
1228 LM_ERR("could not copy: [%.*s]\n", tmp.len, tmp.s);
1229 goto err;
1230 }
1231 /* get url string via NAPTR */
1232 naptr = lost_naptr_lookup(tmp, &shttps, &url);
1233 if(naptr == 0) {
1234 /* fallback to http */
1235 naptr = lost_naptr_lookup(tmp, &shttp, &url);
1236 }
1237 /* nothing found ... return */
1238 if(naptr == 0) {
1239 LM_ERR("NAPTR failed on [%.*s]\n", tmp.len, tmp.s);
1240 goto err;
1241 }
1242 /* clean up */
1243 tmp.s = NULL;
1244 tmp.len = 0;
1245
1246 /* assemble new findService request including path element */
1247 rereq.s = lost_find_service_request(
1248 loc, fsrdata->path, &rereq.len);
1249 /* clean up */
1250 lost_free_findServiceResponse(&fsrdata);
1251 lost_free_string(&ret);
1252
1253 LM_DBG("findService request: [%.*s]\n", rereq.len,
1254 rereq.s);
1255
1256 /* copy url */
1257 len = 0;
1258 urlrep = lost_copy_string(url, &len);
1259 if(urlrep == NULL) {
1260 LM_ERR("could not copy POST url\n");
1261 goto err;
1262 }
1263 /* send request */
1264 curl = httpapi.http_client_query(
1265 _m, urlrep, &ret, rereq.s, mtlost);
1266 /*clean up */
1267 pkg_free(urlrep);
1268 urlrep = NULL;
1269 lost_free_string(&rereq);
1270
1271 /* only HTTP 2xx responses are accepted */
1272 if(curl >= 300 || curl < 100) {
1273 LM_ERR("POST [%.*s] failed with error: %d\n",
1274 url.len, url.s, curl);
1275 goto err;
1276 }
1277 /* reset url string */
1278 url.s = NULL;
1279 url.len = 0;
1280 /* once more ... we got a redirect */
1281 redirect = 1;
1282 }
1283 } else {
1284 LM_ERR("redirect element not found: [%.*s]\n", ret.len,
1285 ret.s);
1286 goto err;
1287 }
1288 break;
1289 case OTHER:
1290 default:
1291 LM_ERR("pidf is not valid: [%.*s]\n", ret.len, ret.s);
1292 goto err;
1293 break;
1294 }
1295 }
1296
1297 /* clean up */
1298 lost_free_findServiceResponse(&fsrdata);
1299 lost_free_string(&ret);
1300 lost_free_string(&req);
1301 lost_free_string(&oldurl);
1302 lost_free_loc(&loc);
1303
1304 /* set writable pvars */
1305 pvname.rs = name;
1306 pvname.rs.s = name.s;
1307 pvname.rs.len = name.len;
1308
1309 pvname.flags = PV_VAL_STR;
1310 psname = (pv_spec_t *)_name;
1311 psname->setf(_m, &psname->pvp, (int)EQ_T, &pvname);
1312 lost_free_string(&name); /* clean up */
1313
1314 pvuri.rs = uri;
1315 pvuri.rs.s = uri.s;
1316 pvuri.rs.len = uri.len;
1317
1318 pvuri.flags = PV_VAL_STR;
1319 psuri = (pv_spec_t *)_uri;
1320 psuri->setf(_m, &psuri->pvp, (int)EQ_T, &pvuri);
1321 lost_free_string(&uri); /* clean up */
1322
1323 /* return error code in case of response error */
1324 if(err.len > 0) {
1325 res_error = 1;
1326 }
1327 pverr.rs = err;
1328 pverr.rs.s = err.s;
1329 pverr.rs.len = err.len;
1330
1331 pverr.flags = PV_VAL_STR;
1332 pserr = (pv_spec_t *)_err;
1333 pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
1334 lost_free_string(&err); /* clean up */
1335
1336 return (res_error > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
1337
1338 err:
1339 /* clean up */
1340 lost_free_findServiceResponse(&fsrdata);
1341 lost_free_geoheader_list(&geolist);
1342 lost_free_loc(&loc);
1343 /* clean up string */
1344 lost_free_string(&oldurl);
1345 lost_free_string(&ret);
1346 lost_free_string(&req);
1347 lost_free_string(&rereq);
1348 lost_free_string(&name);
1349 lost_free_string(&uri);
1350 lost_free_string(&err);
1351
1352 return LOST_CLIENT_ERROR;
1353 }