Debian Codegraph @

src/modules/lost/utilities.c

raw symbols SCIP
1 /*
2 * lost module utility 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 :: utilities
30 * \ingroup lost
31 * Module: \ref lost
32 */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <time.h>
39 #include <arpa/inet.h>
40
41 #include "../../core/parser/msg_parser.h"
42 #include "../../core/parser/parse_content.h"
43 #include "../../core/parser/parse_uri.h"
44 #include "../../core/parser/parse_from.h"
45 #include "../../core/parser/parse_ppi_pai.h"
46 #include "../../core/dprint.h"
47 #include "../../core/mem/mem.h"
48 #include "../../core/mem/shm_mem.h"
49 #include "../../core/rand/kam_rand.h"
50
51 #include "pidf.h"
52 #include "utilities.h"
53 #include "response.h"
54
55 extern int lost_recursion;
56 extern int lost_profile;
57 extern int lost_geoloc_3d;
58
59 /*
60 * lost_trim_content(dest, lgth)
61 * removes whitespace that my occur in a content of an xml element
62 */
63 char *lost_trim_content(char *str, int *lgth)
64 {
65 char *end;
66
67 *lgth = 0;
68
69 if(str == NULL)
70 return NULL;
71
72 while(isspace(*str))
73 str++;
74
75 if(*str == 0)
76 return NULL;
77
78 end = str + strlen(str) - 1;
79
80 while(end > str && isspace(*end))
81 end--;
82
83 *(end + 1) = '\0';
84
85 *lgth = (end + 1) - str;
86
87 return str;
88 }
89
90 /*
91 * lost_rand_str(dest, length)
92 * creates a random string used as temporary id in a findService request
93 */
94 void lost_rand_str(char *dest, size_t lgth)
95 {
96 size_t index;
97 char charset[] = "0123456789"
98 "abcdefghijklmnopqrstuvwxyz"
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
100 while(lgth-- > 0) {
101 index = (double)kam_rand() / RAND_MAX * (sizeof charset - 1);
102 *dest++ = charset[index];
103 }
104 *dest = '\0';
105 }
106
107 /*
108 * lost_new_loc(urn)
109 * creates a new location object in private memory and returns a pointer
110 */
111 p_lost_loc_t lost_new_loc(str rurn)
112 {
113 s_lost_loc_t *ptr = NULL;
114
115 char *id = NULL;
116 char *urn = NULL;
117
118 ptr = (s_lost_loc_t *)pkg_malloc(sizeof(s_lost_loc_t));
119 if(ptr == NULL) {
120 goto err;
121 }
122
123 id = (char *)pkg_malloc(RANDSTRSIZE * sizeof(char) + 1);
124 if(id == NULL) {
125 pkg_free(ptr);
126 goto err;
127 }
128
129 urn = (char *)pkg_malloc(rurn.len + 1);
130 if(urn == NULL) {
131 pkg_free(id);
132 pkg_free(ptr);
133 goto err;
134 }
135
136 memset(urn, 0, rurn.len);
137 memcpy(urn, rurn.s, rurn.len);
138 urn[rurn.len] = '\0';
139
140 lost_rand_str(id, RANDSTRSIZE);
141
142 ptr->identity = id;
143 ptr->urn = urn;
144 ptr->longitude = NULL;
145 ptr->latitude = NULL;
146 ptr->altitude = NULL;
147 ptr->geodetic = NULL;
148 ptr->xpath = NULL;
149 ptr->profile = NULL;
150 ptr->radius = 0;
151 ptr->recursive = lost_recursion; /* set recursion to param */
152 ptr->boundary = LOST_BOUNDARY_FALSE; /* set boundary to reference */
153
154 return ptr;
155
156 err:
157 PKG_MEM_ERROR;
158 return NULL;
159 }
160
161 /*
162 * lost_new_held(uri, type, time, exact)
163 * creates a new held object in private memory and returns a pointer
164 */
165 p_lost_held_t lost_new_held(str s_uri, str s_type, int time, int exact)
166 {
167 s_lost_held_t *ptr = NULL;
168
169 char *uri = NULL;
170 char *type = NULL;
171
172 ptr = (s_lost_held_t *)pkg_malloc(sizeof(s_lost_held_t));
173 if(ptr == NULL) {
174 goto err;
175 }
176
177 uri = (char *)pkg_malloc(s_uri.len + 1);
178 if(uri == NULL) {
179 pkg_free(ptr);
180 goto err;
181 }
182
183 type = (char *)pkg_malloc(s_type.len + 1);
184 if(type == NULL) {
185 pkg_free(uri);
186 pkg_free(ptr);
187 goto err;
188 }
189
190 memset(uri, 0, s_uri.len);
191 memcpy(uri, s_uri.s, s_uri.len);
192 uri[s_uri.len] = '\0';
193
194 memset(type, 0, s_type.len);
195 memcpy(type, s_type.s, s_type.len);
196 type[s_type.len] = '\0';
197
198 ptr->identity = uri;
199 ptr->type = type;
200 ptr->time = time;
201 ptr->exact = exact;
202
203 return ptr;
204
205 err:
206 PKG_MEM_ERROR;
207 return NULL;
208 }
209
210 /*
211 * lost_free_loc(ptr)
212 * frees a location object
213 */
214 void lost_free_loc(p_lost_loc_t *loc)
215 {
216 p_lost_loc_t ptr;
217
218 if(*loc == NULL)
219 return;
220
221 ptr = *loc;
222
223 if(ptr->identity)
224 pkg_free(ptr->identity);
225 if(ptr->urn)
226 pkg_free(ptr->urn);
227 if(ptr->xpath)
228 pkg_free(ptr->xpath);
229 if(ptr->geodetic)
230 pkg_free(ptr->geodetic);
231 if(ptr->longitude)
232 pkg_free(ptr->longitude);
233 if(ptr->latitude)
234 pkg_free(ptr->latitude);
235 if(ptr->altitude)
236 pkg_free(ptr->altitude);
237 if(ptr->profile)
238 pkg_free(ptr->profile);
239
240 pkg_free(ptr);
241 *loc = NULL;
242
243 LM_DBG("### location object removed\n");
244
245 return;
246 }
247
248 /*
249 * lost_free_loc(ptr)
250 * frees a held location request object
251 */
252 void lost_free_held(p_lost_held_t *held)
253 {
254 p_lost_held_t ptr;
255
256 if(*held == NULL)
257 return;
258
259 ptr = *held;
260
261 if(ptr->identity)
262 pkg_free(ptr->identity);
263 if(ptr->type)
264 pkg_free(ptr->type);
265
266 pkg_free(ptr);
267 *held = NULL;
268
269 LM_DBG("### location-request object removed\n");
270
271 return;
272 }
273
274 /*
275 * lost_copy_string(str, int*) {
276 * copies a string and returns a zero terminated string allocated
277 * in private memory
278 */
279 char *lost_copy_string(str src, int *lgth)
280 {
281 char *res = NULL;
282 *lgth = 0;
283
284 /* only copy a valid string */
285 if(src.s != NULL && src.len > 0) {
286 res = (char *)pkg_malloc((src.len + 1) * sizeof(char));
287 if(res == NULL) {
288 PKG_MEM_ERROR;
289 } else {
290 memcpy(res, src.s, src.len);
291 res[src.len] = '\0';
292 *lgth = (int)strlen(res);
293 }
294 }
295
296 return res;
297 }
298
299 /*
300 * lost_free_string(ptr)
301 * frees and resets a string
302 */
303 void lost_free_string(str *string)
304 {
305 string->len = 0;
306 if(string->s == NULL)
307 return;
308 pkg_free(string->s);
309 LM_DBG("### string object removed\n");
310 string->s = NULL;
311
312 return;
313 }
314
315 /*
316 * lost_get_content(node, name, lgth)
317 * gets a nodes "name" content, removes whitespace and returns string
318 * allocated in private memory
319 */
320 char *lost_get_content(xmlNodePtr node, const char *name, int *lgth)
321 {
322 xmlNodePtr cur = node;
323 char *content = NULL;
324 char *trimmed = NULL;
325 char *cnt = NULL;
326 int len;
327
328 *lgth = 0;
329
330 content = xmlNodeGetNodeContentByName(cur, name, NULL);
331 if(content == NULL) {
332 LM_ERR("could not get XML node content\n");
333 return cnt;
334 } else {
335 trimmed = lost_trim_content(content, &len);
336 cnt = (char *)pkg_malloc((len + 1) * sizeof(char));
337 if(cnt == NULL) {
338 PKG_MEM_ERROR;
339 xmlFree(content);
340 return cnt;
341 }
342 memset(cnt, 0, len);
343 memcpy(cnt, trimmed, len);
344 cnt[len] = '\0';
345 }
346
347 xmlFree(content);
348 *lgth = strlen(cnt);
349
350 return cnt;
351 }
352
353 /*
354 * lost_get_property(node, name, lgth)
355 * gets a nodes property "name" and returns string allocated in private memory
356 */
357 char *lost_get_property(xmlNodePtr node, const char *name, int *lgth)
358 {
359 xmlNodePtr cur = node;
360 char *content;
361 char *cnt = NULL;
362 int len;
363
364 *lgth = 0;
365
366 content = xmlNodeGetAttrContentByName(cur, name);
367 if(content == NULL) {
368 LM_ERR("could not get XML node content\n");
369 return cnt;
370 } else {
371 len = strlen(content);
372 cnt = (char *)pkg_malloc((len + 1) * sizeof(char));
373 if(cnt == NULL) {
374 PKG_MEM_ERROR;
375 xmlFree(content);
376 return cnt;
377 }
378 memset(cnt, 0, len);
379 memcpy(cnt, content, len);
380 cnt[len] = '\0';
381 }
382
383 xmlFree(content);
384 *lgth = strlen(cnt);
385
386 return cnt;
387 }
388
389 /*
390 * lost_get_childname(name, lgth)
391 * gets a nodes child name and returns string allocated in private memory
392 */
393 char *lost_get_childname(xmlNodePtr node, const char *name, int *lgth)
394 {
395 xmlNodePtr cur = node;
396 xmlNodePtr parent = NULL;
397 xmlNodePtr child = NULL;
398
399 char *cnt = NULL;
400 char *trimmed = NULL;
401
402 int len;
403
404 *lgth = 0;
405
406 parent = xmlNodeGetNodeByName(cur, name, NULL);
407 if(parent == NULL) {
408 LM_ERR("xmlNodeGetNodeByName() failed\n");
409 return cnt;
410 }
411 child = parent->children;
412 if(child == NULL) {
413 LM_ERR("%s has no children '%s'\n", parent->name, name);
414 return cnt;
415 }
416 trimmed = lost_trim_content((char *)child->name, &len);
417 cnt = (char *)pkg_malloc((len + 1) * sizeof(char));
418 if(cnt == NULL) {
419 PKG_MEM_ERROR;
420 return cnt;
421 }
422 memset(cnt, 0, len);
423 memcpy(cnt, trimmed, len);
424 cnt[len] = '\0';
425
426 *lgth = strlen(cnt);
427
428 return cnt;
429 }
430
431 /*
432 * lost_get_geolocation_header(msg, hdr)
433 * gets the Geolocation header value and returns 1 on success
434 */
435 p_lost_geolist_t lost_get_geolocation_header(struct sip_msg *msg, int *items)
436 {
437 struct hdr_field *hf;
438 str hdr = STR_NULL;
439 p_lost_geolist_t list = NULL;
440
441 *items = 0;
442
443 if(parse_headers(msg, HDR_EOH_F, 0) == -1) {
444 LM_ERR("failed to parse SIP headers\n");
445 return list;
446 }
447
448 for(hf = msg->headers; hf; hf = hf->next) {
449 if((hf->type == HDR_OTHER_T)
450 && (hf->name.len == LOST_GEOLOC_HEADER_SIZE - 2)) {
451 /* possible hit */
452 if(strncasecmp(
453 hf->name.s, LOST_GEOLOC_HEADER, LOST_GEOLOC_HEADER_SIZE)
454 == 0) {
455
456 hdr.s = hf->body.s;
457 hdr.len = hf->body.len;
458
459 LM_DBG("found geolocation header [%.*s]\n", hdr.len, hdr.s);
460
461 *items += lost_new_geoheader_list(&list, hdr);
462 }
463 }
464 }
465
466 return list;
467 }
468
469 /*
470 * lost_get_pai_header(msg, lgth)
471 * gets the P-A-I header value and returns string allocated in
472 * private memory
473 */
474 char *lost_get_pai_header(struct sip_msg *msg, int *lgth)
475 {
476 to_body_t *p_pai;
477 char *res = NULL;
478
479 *lgth = 0;
480
481 if((parse_pai_header(msg) == 0) && (msg->pai) && (msg->pai->parsed)) {
482 /* return the first header entry */
483 p_pai = get_pai(msg)->id;
484 if((p_pai->parsed_uri.user.s == NULL)
485 && (parse_uri(p_pai->uri.s, p_pai->uri.len, &p_pai->parsed_uri)
486 < 0)) {
487
488 LM_DBG("failed to parse P-A-I header\n");
489
490 return res;
491 }
492 } else {
493
494 LM_DBG("P-A-I header not found or failed to parse\n");
495
496 return res;
497 }
498
499 /* warning in case multiple P-A-I headers were found */
500 if(get_pai(msg)->num_ids > 1) {
501 LM_WARN("multiple P-A-I headers found, selected first!\n");
502 }
503
504 LM_DBG("P-A-I uri: [%.*s]\n", p_pai->uri.len, p_pai->uri.s);
505
506 /* allocate memory, copy and \0 terminate string */
507 res = (char *)pkg_malloc((p_pai->uri.len + 1) * sizeof(char));
508 if(res == NULL) {
509 PKG_MEM_ERROR;
510 return res;
511 } else {
512 memset(res, 0, p_pai->uri.len);
513 memcpy(res, p_pai->uri.s, p_pai->uri.len);
514 res[p_pai->uri.len] = '\0';
515 *lgth = strlen(res);
516 }
517
518 return res;
519 }
520
521 /*
522 * lost_parse_host(uri, string)
523 * parses host (ipv4, ipv6 or name) from uri and returns string
524 */
525 int lost_parse_host(const char *uri, str *host, int *flag)
526 {
527 char *search = (char *)uri;
528 char *end;
529
530 int len = 0;
531 int ip6 = 0;
532
533 while((len < strlen(uri)) && (*search++ != '@')) {
534 len++;
535 }
536
537 if(len == strlen(uri)) {
538 return 0;
539 }
540
541 if(*(search) == 0)
542 return 0;
543
544 if(*(search) == '[') {
545 ip6 = 1;
546 }
547
548 end = search;
549
550 if(ip6) {
551 while((len < strlen(uri)) && (*end++ != ']')) {
552 len++;
553 }
554 if(len == strlen(uri)) {
555 return 0;
556 }
557 } else {
558 while(len < strlen(uri)) {
559 if((*end == ':') || (*end == '>')) {
560 break;
561 }
562 end++;
563 len++;
564 }
565 }
566
567 if(*(search) == 0)
568 return 0;
569
570 host->s = search;
571 host->len = end - search;
572
573 if(ip6) {
574 *flag = AF_INET6;
575 } else {
576 *flag = AF_INET;
577 }
578
579 return 1;
580 }
581
582 /*
583 * lost_get_nameinfo(ip, name, flag)
584 * translates socket address to service name (flag: IPv4 or IPv6)
585 */
586 int lost_get_nameinfo(char *ip, str *name, int flag)
587 {
588 struct sockaddr_in sa4;
589 struct sockaddr_in6 sa6;
590
591 if(flag == AF_INET) {
592 bzero(&sa4, sizeof(sa4));
593 sa4.sin_family = flag;
594 if(inet_pton(flag, ip, &sa4.sin_addr) <= 0)
595 return 0;
596 if(getnameinfo((struct sockaddr *)&sa4, sizeof(sa4), name->s, name->len,
597 NULL, 0, NI_NAMEREQD))
598 return 0;
599
600 return 1;
601 }
602
603 if(flag == AF_INET6) {
604 bzero(&sa6, sizeof(sa6));
605 sa6.sin6_family = flag;
606 if(inet_pton(flag, ip, &sa6.sin6_addr) <= 0)
607 return 0;
608 if(getnameinfo((struct sockaddr *)&sa6, sizeof(sa6), name->s, name->len,
609 NULL, 0, NI_NAMEREQD))
610 return 0;
611
612 return 1;
613 }
614
615 return 0;
616 }
617
618 /*
619 * lost_get_from_header(msg, lgth)
620 * gets the From header value and returns string allocated in
621 * private memory
622 */
623 char *lost_get_from_header(struct sip_msg *msg, int *lgth)
624 {
625 to_body_t *f_body;
626 char *res = NULL;
627
628 *lgth = 0;
629
630 if(parse_from_header(msg) == -1) {
631 LM_ERR("failed to parse From header\n");
632 return res;
633 }
634
635 if(msg->from == NULL || get_from(msg) == NULL) {
636 LM_ERR("From header not found\n");
637 return res;
638 }
639 f_body = get_from(msg);
640
641 LM_DBG("From body: [%.*s]\n", f_body->body.len, f_body->body.s);
642
643 /* allocate memory, copy and \0 terminate string */
644 res = (char *)pkg_malloc((f_body->uri.len + 1) * sizeof(char));
645 if(res == NULL) {
646 PKG_MEM_ERROR;
647 return res;
648 } else {
649 memset(res, 0, f_body->uri.len);
650 memcpy(res, f_body->uri.s, f_body->uri.len);
651 res[f_body->uri.len] = '\0';
652
653 *lgth = strlen(res);
654 }
655
656 return res;
657 }
658
659 /*
660 * lost_free_geoheader_list(list)
661 * removes geoheader list from private memory
662 */
663 void lost_free_geoheader_list(p_lost_geolist_t *list)
664 {
665 p_lost_geolist_t curr;
666
667 while((curr = *list) != NULL) {
668 *list = curr->next;
669 if(curr->value != NULL) {
670 pkg_free(curr->value);
671 }
672 if(curr->param != NULL) {
673 pkg_free(curr->param);
674 }
675 pkg_free(curr);
676 }
677
678 *list = NULL;
679
680 LM_DBG("### geoheader list removed\n");
681
682 return;
683 }
684
685 /*
686 * lost_get_geoheader_value(list, type, rtype)
687 * returns geoheader value and type (rtype) of given type
688 */
689 char *lost_get_geoheader_value(
690 p_lost_geolist_t list, lost_geotype_t type, int *rtype)
691 {
692 p_lost_geolist_t head = list;
693 char *value = NULL;
694
695 if(head == NULL) {
696 return value;
697 }
698
699 /* type is not important, take first element value and type */
700 if((type == ANY) || (type == UNKNOWN)) {
701 *rtype = head->type;
702 return head->value;
703 }
704
705 /* take first element value and type of given type */
706 while(head) {
707 if(type == head->type) {
708 value = head->value;
709 *rtype = head->type;
710 break;
711 }
712 head = head->next;
713 }
714
715 return value;
716 }
717
718 /*
719 * lost_reverse_geoheader_list(list)
720 * reverses list order
721 */
722 void lost_reverse_geoheader_list(p_lost_geolist_t *head)
723 {
724 p_lost_geolist_t prev = NULL;
725 p_lost_geolist_t next = NULL;
726 p_lost_geolist_t current = *head;
727
728 while(current != NULL) {
729 next = current->next;
730 current->next = prev;
731 prev = current;
732 current = next;
733 }
734
735 *head = prev;
736 }
737
738 /*
739 * lost_copy_geoheader_value(src, len)
740 * returns a header vaule string (src to src + len) allocated in private memory
741 */
742 char *lost_copy_geoheader_value(char *src, int len)
743 {
744 char *res = NULL;
745
746 res = (char *)pkg_malloc((len + 1) * sizeof(char));
747 if(res == NULL) {
748 PKG_MEM_ERROR;
749 return res;
750 } else {
751 memset(res, 0, len);
752 memcpy(res, src, len);
753 res[len] = '\0';
754 }
755
756 return res;
757 }
758
759 /*
760 * lost_new_geoheader_list(hdr, items)
761 * searches and parses Geolocation header and returns a list
762 * allocated in private memory and an item count
763 */
764 int lost_new_geoheader_list(p_lost_geolist_t *list, str hdr)
765 {
766 char *search = NULL;
767 char *cidptr = NULL;
768 char *urlptr = NULL;
769 char *ptr = NULL;
770
771 int count = 0;
772 int len = 0;
773 int i = 0;
774
775 p_lost_geolist_t new = NULL;
776
777 LM_DBG("parsing geolocation header value ...\n");
778
779 /* search the complete header field */
780 search = hdr.s;
781 for(i = 0; i < hdr.len; i++) {
782 /* check for cid content */
783 /* <cid:x> might be the shortest */
784 if(strlen(search) > 6) {
785 if((*(search + 0) == '<')
786 && ((*(search + 1) == 'c') || (*(search + 1) == 'C'))
787 && ((*(search + 2) == 'i') || (*(search + 2) == 'I'))
788 && ((*(search + 3) == 'd') || (*(search + 3) == 'D'))
789 && (*(search + 4) == ':')) {
790 cidptr = search + 4;
791 *cidptr = LAQUOT;
792 ptr = cidptr;
793 len = 1;
794 while(*(ptr + len) != '>') {
795 if((len == strlen(ptr)) || (*(ptr + len) == '<')) {
796 LM_WARN("invalid cid: [%.*s]\n", hdr.len, hdr.s);
797 break;
798 }
799 len++;
800 }
801 if((*(ptr + len) == '>') && (len > 6)) {
802 new = (p_lost_geolist_t)pkg_malloc(
803 sizeof(s_lost_geolist_t));
804 if(new == NULL) {
805 PKG_MEM_ERROR;
806 } else {
807
808 LM_DBG("\t[%.*s]\n", len + 1, cidptr);
809
810 new->value = lost_copy_geoheader_value(cidptr, len + 1);
811 new->param = NULL;
812 new->type = CID;
813 new->next = *list;
814 *list = new;
815 count++;
816
817 LM_DBG("adding cid [%s]\n", new->value);
818 }
819 } else {
820 LM_WARN("invalid value: [%.*s]\n", hdr.len, hdr.s);
821 }
822 *cidptr = COLON;
823 }
824 }
825
826 /* check for http(s) content */
827 /* <http://goo.gl/> might be the shortest */
828 if(strlen(search) > 10) {
829 if((*(search + 0) == '<')
830 && ((*(search + 1) == 'h') || (*(search + 1) == 'H'))
831 && ((*(search + 2) == 't') || (*(search + 2) == 'T'))
832 && ((*(search + 3) == 't') || (*(search + 3) == 'T'))
833 && ((*(search + 4) == 'p') || (*(search + 4) == 'P'))) {
834 urlptr = search + 1;
835 ptr = urlptr;
836 len = 0;
837 while(*(ptr + len) != '>') {
838 if((len == strlen(ptr)) || (*(ptr + len) == '<')) {
839 LM_WARN("invalid url: [%.*s]\n", hdr.len, hdr.s);
840 break;
841 }
842 len++;
843 }
844 if((*(ptr + len) == '>') && (len > 10)) {
845 new = (p_lost_geolist_t)pkg_malloc(
846 sizeof(s_lost_geolist_t));
847 if(new == NULL) {
848 PKG_MEM_ERROR;
849 } else {
850
851 LM_DBG("\t[%.*s]\n", len, urlptr);
852
853 new->value = lost_copy_geoheader_value(urlptr, len);
854 new->param = NULL;
855 if(*(search + 5) == ':') {
856
857 LM_DBG("adding http url [%s]\n", new->value);
858
859 new->type = HTTP;
860 } else if(((*(search + 5) == 's')
861 || (*(search + 5) == 'S'))
862 && (*(search + 6) == ':')) {
863
864 LM_DBG("adding https url [%s]\n", new->value);
865
866 new->type = HTTPS;
867 }
868 new->next = *list;
869 *list = new;
870 count++;
871 }
872 } else {
873 LM_WARN("invalid value: [%.*s]\n", hdr.len, hdr.s);
874 }
875 }
876 }
877 search++;
878 }
879
880 return count;
881 }
882
883 /*
884 * lost_parse_pidf(pidf, urn)
885 * parses pidf and returns a new location object
886 */
887 p_lost_loc_t lost_parse_pidf(str pidf, str urn)
888 {
889
890 p_lost_loc_t loc = NULL;
891
892 xmlDocPtr doc = NULL;
893 xmlNodePtr root = NULL;
894
895 /* read and parse pidf-lo */
896 doc = xmlReadMemory(pidf.s, pidf.len, 0, NULL,
897 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA);
898
899 if(doc == NULL) {
900 LM_WARN("invalid xml (pidf-lo): [%.*s]\n", pidf.len, pidf.s);
901 doc = xmlReadMemory(pidf.s, pidf.len, 0, NULL,
902 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA
903 | XML_PARSE_RECOVER);
904 if(doc == NULL) {
905 LM_ERR("xml (pidf-lo) recovery failed on: [%.*s]\n", pidf.len,
906 pidf.s);
907 goto err;
908 }
909
910 LM_DBG("xml (pidf-lo) recovered\n");
911 }
912
913 root = xmlDocGetRootElement(doc);
914 if(root == NULL) {
915 LM_ERR("empty pidf-lo document\n");
916 goto err;
917 }
918 if((!xmlStrcmp(root->name, (const xmlChar *)"presence"))
919 || (!xmlStrcmp(root->name, (const xmlChar *)"locationResponse"))) {
920 /* get the geolocation: point or circle, urn, ... */
921 loc = lost_new_loc(urn);
922 if(loc == NULL) {
923 LM_ERR("location object allocation failed\n");
924 goto err;
925 }
926 if(lost_parse_location_info(root, loc) < 0) {
927 LM_ERR("location element not found\n");
928 goto err;
929 }
930 } else {
931 LM_ERR("findServiceResponse or presence element not found in "
932 "[%.*s]\n",
933 pidf.len, pidf.s);
934 goto err;
935 }
936 /* clean up */
937 xmlFreeDoc(doc);
938 doc = NULL;
939
940 return loc;
941
942 err:
943 LM_ERR("pidflo parsing error\n");
944 /* clean up */
945 lost_free_loc(&loc);
946 if(doc != NULL) {
947 xmlFreeDoc(doc);
948 doc = NULL;
949 }
950
951 return NULL;
952 }
953
954 /*
955 * lost_check_3d(node)
956 * checks if pos is 3D and returns 1 if true
957 * <gml:pos>-34.407 150.883 24.8</gml:pos>
958 */
959 int lost_check_3d(xmlNodePtr node)
960 {
961 xmlNodePtr cur = NULL;
962
963 char *content = NULL;
964 int ret = 0;
965
966 cur = node;
967 /* find <pos> element */
968 content = xmlNodeGetNodeContentByName(cur, "pos", NULL);
969
970 if(content == NULL) {
971 LM_WARN("could not find pos element\n");
972 return -1;
973 }
974
975 int len = 0;
976 char *tmp = lost_trim_content(content, &len);
977
978 if(len == 0) {
979 LM_WARN("could not find pos element\n");
980 xmlFree(content); /* clean up */
981 return -1;
982 }
983
984 int i = 0;
985 while(*tmp) {
986 if(isspace(*tmp))
987 i++;
988 tmp++;
989 }
990
991 if(i > 1) {
992 ret = 1;
993 }
994 /* clean up */
995 xmlFree(content);
996
997 return ret;
998 }
999
1000 /*
1001 * lost_parse_geo(node, loc)
1002 * parses locationResponse (pos|circle) and writes
1003 * results to location object
1004 */
1005 int lost_parse_geo(xmlNodePtr node, p_lost_loc_t loc)
1006 {
1007 xmlNodePtr cur = NULL;
1008
1009 char bufLat[BUFSIZE];
1010 char bufLon[BUFSIZE];
1011 char bufAlt[BUFSIZE];
1012 char *content = NULL;
1013
1014 char *s_profile = LOST_PRO_GEO2D;
1015
1016 int iRadius = 0;
1017 int len = 0;
1018 int scan = 0;
1019
1020 cur = node;
1021 /* find <pos> element */
1022 content = xmlNodeGetNodeContentByName(cur, "pos", NULL);
1023
1024 if(content == NULL) {
1025 LM_WARN("could not find pos element\n");
1026 return -1;
1027 }
1028
1029 scan = sscanf(content, "%127s %127s %127s", bufLat, bufLon, bufAlt);
1030 xmlFree(content);
1031
1032 if(scan < 2) {
1033 LM_WARN("invalid pos element\n");
1034 return -1;
1035 }
1036
1037 /* Check for truncation: if any field reached max width (127), it was likely truncated */
1038 if((strlen(bufLat) >= 127) || (strlen(bufLon) >= 127)
1039 || (strlen(bufAlt) >= 127)) {
1040 LM_WARN("pos element contains oversized coordinates (truncation "
1041 "detected)\n");
1042 }
1043
1044 /* latitude */
1045 len = strlen((char *)bufLat);
1046 loc->latitude = (char *)pkg_malloc(len + 1);
1047 if(loc->latitude == NULL)
1048 goto err;
1049
1050 snprintf(loc->latitude, len + 1, "%s", (char *)bufLat);
1051
1052 /* logitude */
1053 len = strlen((char *)bufLon);
1054 loc->longitude = (char *)pkg_malloc(len + 1);
1055 if(loc->longitude == NULL) {
1056 pkg_free(loc->latitude);
1057 goto err;
1058 }
1059
1060 snprintf(loc->longitude, len + 1, "%s", (char *)bufLon);
1061
1062 /* altitude */
1063 if(scan == 3) {
1064 LM_INFO("3d geolocation in pos element\n");
1065
1066 len = strlen((char *)bufAlt);
1067 loc->altitude = (char *)pkg_malloc(len + 1);
1068 if(loc->altitude == NULL) {
1069 pkg_free(loc->latitude);
1070 pkg_free(loc->longitude);
1071 goto err;
1072 }
1073
1074 snprintf(loc->altitude, len + 1, "%s", (char *)bufAlt);
1075 }
1076
1077 /* geolocation */
1078 len = strlen((char *)bufLat) + strlen((char *)bufLon) + 1;
1079 if((scan == 3) && (lost_geoloc_3d == 1)) {
1080 len += strlen((char *)bufAlt) + 1;
1081 }
1082 loc->geodetic = (char *)pkg_malloc(len + 1);
1083 if(loc->geodetic == NULL) {
1084 pkg_free(loc->latitude);
1085 pkg_free(loc->longitude);
1086 if(loc->altitude)
1087 pkg_free(loc->altitude);
1088 goto err;
1089 }
1090 if((scan == 3) && (lost_geoloc_3d == 1)) {
1091 s_profile = LOST_PRO_GEO3D;
1092 snprintf(loc->geodetic, len + 1, "%s %s %s", (char *)bufLat,
1093 (char *)bufLon, (char *)bufAlt);
1094 } else {
1095 snprintf(loc->geodetic, len + 1, "%s %s", (char *)bufLat,
1096 (char *)bufLon);
1097 }
1098
1099 /* find <radius> element */
1100 content = xmlNodeGetNodeContentByName(cur, "radius", NULL);
1101 if(content != NULL) {
1102 sscanf(content, "%d", &iRadius);
1103 xmlFree(content);
1104 }
1105
1106 /* write results */
1107 loc->radius = iRadius;
1108 loc->profile = (char *)pkg_malloc(strlen(s_profile) + 1);
1109 strcpy(loc->profile, s_profile);
1110
1111 return 0;
1112
1113 err:
1114 PKG_MEM_ERROR;
1115 return -1;
1116 }
1117
1118 /*
1119 * lost_xpath_location(doc, path, loc)
1120 * performs xpath expression on locationResponse and writes
1121 * results (location-info child element) to location object
1122 */
1123 int lost_xpath_location(xmlDocPtr doc, char *path, p_lost_loc_t loc)
1124 {
1125 xmlXPathObjectPtr result = NULL;
1126 xmlNodeSetPtr nodes = NULL;
1127 xmlNodePtr root = NULL;
1128 xmlNodePtr cur = NULL;
1129 xmlDocPtr new = NULL;
1130 xmlChar *xpath = NULL;
1131 xmlChar *xmlbuff = NULL;
1132 xmlChar *cname = NULL;
1133
1134 /* shape representation RFC 5491 */
1135 const unsigned char s_point[] = LOST_PNT;
1136 const unsigned char s_polygon[] = LOST_POL;
1137 const unsigned char s_circle[] = LOST_CIR;
1138 const unsigned char s_ellipse[] = LOST_ELL;
1139 const unsigned char s_arcband[] = LOST_ARC;
1140 const unsigned char s_sphere[] = LOST_SPH;
1141 const unsigned char s_ellipsoid[] = LOST_OID;
1142 const unsigned char s_prism[] = LOST_PSM;
1143 const unsigned char s_civic[] = LOST_CIV;
1144
1145 char *ptr = NULL;
1146 char *tmp = NULL;
1147 char *s_profile = NULL;
1148
1149 int buffersize = 0;
1150 int remove = 0;
1151 int size = 0;
1152 int len = 0;
1153 int i = 0;
1154 int nok = -1;
1155 int select = -1;
1156 int selgeo = -1;
1157 int selciv = -1;
1158
1159 xpath = (xmlChar *)path;
1160 /* get location via xpath expression */
1161 result = xmlGetNodeSet(doc, xpath, BAD_CAST XPATH_NS);
1162
1163 if(result == NULL) {
1164 LM_DBG("xmlGetNodeSet() returned no result\n");
1165 return -1;
1166 }
1167
1168 nodes = result->nodesetval;
1169 if(nodes != NULL) {
1170 size = nodes->nodeNr;
1171 for(i = 0; i < size; ++i) {
1172 if(nodes->nodeTab[i] == NULL) {
1173 LM_WARN("xpath '%s' failed\n", xpath);
1174 xmlXPathFreeObject(result);
1175 return -1;
1176 }
1177 if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
1178 cur = nodes->nodeTab[i];
1179 /* check if child element is point, circle, ... or civic */
1180 while(nok < LOST_XPATH_DPTH) {
1181 if(cur->children == NULL) {
1182 /* no additional DOM level */
1183 break;
1184 } else {
1185 /* check current DOM level */
1186 nok++;
1187 cname = BAD_CAST cur->name;
1188 if(xmlStrcasecmp(cname, s_point) == 0) {
1189 if((lost_check_3d(cur) == 1)
1190 && (lost_geoloc_3d == 1)) {
1191 s_profile = LOST_PRO_GEO3D;
1192 selgeo = i;
1193 break;
1194 }
1195 if(lost_check_3d(cur) == 0) {
1196 s_profile = LOST_PRO_GEO2D;
1197 selgeo = i;
1198 break;
1199 }
1200 }
1201 if(xmlStrcasecmp(cname, s_circle) == 0) {
1202 s_profile = LOST_PRO_GEO2D;
1203 selgeo = i;
1204 break;
1205 }
1206 if((xmlStrcasecmp(cname, s_polygon) == 0)
1207 || (xmlStrcasecmp(cname, s_ellipse) == 0)
1208 || (xmlStrcasecmp(cname, s_arcband) == 0)) {
1209 s_profile = LOST_PRO_GEO2D;
1210 selgeo = i;
1211 break;
1212 }
1213 if((xmlStrcasecmp(cname, s_sphere) == 0)
1214 || (xmlStrcasecmp(cname, s_ellipsoid) == 0)
1215 || (xmlStrcasecmp(cname, s_prism) == 0)) {
1216 if(lost_geoloc_3d == 1) {
1217 s_profile = LOST_PRO_GEO3D;
1218 selgeo = i;
1219 break;
1220 }
1221 }
1222 if(xmlStrcasecmp(cname, s_civic) == 0) {
1223 s_profile = LOST_PRO_CIVIC;
1224 selciv = i;
1225 break;
1226 }
1227 /* nothing found ... try next DOM level */
1228 cur = cur->children;
1229 }
1230 }
1231
1232 if(nok == 0) {
1233 LM_DBG("xpath '%s' returned valid element (level %d/%d)\n",
1234 xpath, nok, LOST_XPATH_DPTH);
1235 } else if(nok < LOST_XPATH_DPTH) {
1236 /* no matching location ... pidf-lo still ok */
1237 LM_WARN("xpath '%s' shape representation not found (level "
1238 "%d/%d)\n",
1239 xpath, nok, LOST_XPATH_DPTH);
1240 } else {
1241 /* really bad pidf-lo */
1242 LM_WARN("xpath '%s' failed (level %d/%d)\n", xpath, nok,
1243 LOST_XPATH_DPTH);
1244 xmlXPathFreeObject(result);
1245 return -1;
1246 }
1247
1248 if(cur == NULL) {
1249 LM_ERR("xpath xmlCopyNode() failed\n");
1250 xmlXPathFreeObject(result);
1251 return -1;
1252 }
1253
1254 root = xmlCopyNode(cur, 1);
1255 if(root == NULL) {
1256 LM_ERR("xpath xmlCopyNode() failed\n");
1257 xmlXPathFreeObject(result);
1258 return -1;
1259 }
1260
1261 new = xmlNewDoc(BAD_CAST "1.0");
1262 if(new == NULL) {
1263 LM_ERR("xpath xmlNewDoc() failed\n");
1264 xmlXPathFreeObject(result);
1265 return -1;
1266 }
1267 xmlDocSetRootElement(new, root);
1268 xmlDocDumpFormatMemory(new, &xmlbuff, &buffersize, 0);
1269 if(xmlbuff == NULL) {
1270 LM_ERR("xpath xmlDocDumpFormatMemory() failed\n");
1271 xmlFreeDoc(new);
1272 xmlXPathFreeObject(result);
1273 return -1;
1274 }
1275 /* take the first location-info element */
1276 if(lost_profile == 0) {
1277
1278 LM_DBG("first location-info element [%d]\n", i);
1279
1280 select = 0;
1281 /* take the last location-info element */
1282 } else if(lost_profile == 1) {
1283
1284 LM_DBG("last location-info element [%d]\n", i);
1285
1286 select = size - 1;
1287 /* take the first geodetic location-info element */
1288 } else if(lost_profile == 2) {
1289
1290 LM_DBG("first geodetic location-info element [%d]\n", i);
1291
1292 select = selgeo;
1293 /* take the first civic location-info element */
1294 } else if(lost_profile == 3) {
1295
1296 LM_DBG("first civic location-info element [%d]\n", i);
1297
1298 select = selciv;
1299 }
1300
1301 /* nothing found */
1302 if((select < 0) && (i == size - 1)) {
1303 LM_ERR("could not find proper location-info element\n");
1304 xmlFree(xmlbuff); /* clean up */
1305 xmlFreeDoc(new);
1306 xmlXPathFreeObject(result);
1307 return -1;
1308 }
1309
1310 if(i == select) {
1311 /* return the current profile */
1312 if(s_profile != NULL) {
1313 loc->profile =
1314 (char *)pkg_malloc(strlen(s_profile) + 1);
1315 if(loc->profile == NULL) {
1316 xmlFree(xmlbuff); /* clean up */
1317 xmlFreeDoc(new);
1318 xmlXPathFreeObject(result);
1319 goto err;
1320 }
1321 memset(loc->profile, 0, strlen(s_profile) + 1);
1322 memcpy(loc->profile, s_profile, strlen(s_profile));
1323 } else {
1324 xmlFree(xmlbuff); /* clean up */
1325 xmlFreeDoc(new);
1326 LM_DBG("xpath '%s' no valid profile found\n", xpath);
1327 xmlXPathFreeObject(result);
1328 return -1;
1329 }
1330 /* remove xml header from location element */
1331 remove = strlen("<?xml version='1.0'?>\n");
1332 buffersize = buffersize - remove;
1333 ptr = (char *)pkg_malloc((buffersize + 1) * sizeof(char));
1334 if(ptr == NULL) {
1335 xmlFree(xmlbuff); /* clean up */
1336 xmlFreeDoc(new);
1337 xmlXPathFreeObject(result);
1338 goto err;
1339 }
1340
1341 memset(ptr, 0, buffersize);
1342 memcpy(ptr, (char *)(xmlbuff + remove), buffersize);
1343 ptr[buffersize] = '\0';
1344
1345 /* trim the result */
1346 tmp = lost_trim_content(ptr, &len);
1347
1348 /* return the location DOM */
1349 loc->xpath = (char *)pkg_malloc(len + 1);
1350 if(loc->xpath == NULL) {
1351 pkg_free(ptr); /* clean up */
1352 ptr = NULL;
1353 xmlFree(xmlbuff);
1354 xmlFreeDoc(new);
1355 xmlXPathFreeObject(result);
1356 goto err;
1357 }
1358 memset(loc->xpath, 0, len + 1);
1359 memcpy(loc->xpath, tmp, len);
1360 pkg_free(ptr); /* clean up */
1361 ptr = NULL;
1362 } else {
1363 LM_WARN("location-info element[%d] dropped!\n", i + 1);
1364 }
1365 /* clean up */
1366 xmlFree(xmlbuff);
1367 xmlFreeDoc(new);
1368 /* reset level count */
1369 nok = -1;
1370 }
1371 }
1372 } else {
1373 LM_WARN("xpath '%s' error\n", xpath);
1374 xmlXPathFreeObject(result);
1375 return -1;
1376 }
1377 xmlXPathFreeObject(result);
1378
1379 return 0;
1380
1381 err:
1382 PKG_MEM_ERROR;
1383 return -1;
1384 }
1385
1386 /*
1387 * lost_parse_location_info(node, loc)
1388 * wrapper to call xpath or simple pos|circle parser (last resort)
1389 */
1390 int lost_parse_location_info(xmlNodePtr root, p_lost_loc_t loc)
1391 {
1392 if(lost_xpath_location(root->doc, LOST_XPATH_GP, loc) == 0) {
1393 return 0;
1394 }
1395
1396 LM_WARN("xpath expression failed ... trying pos|circle\n");
1397
1398 if(lost_parse_geo(root, loc) == 0) {
1399 return 0;
1400 }
1401
1402 return -1;
1403 }
1404
1405 /*
1406 * lost_held_post_request(lgth)
1407 * assembles and returns locationRequest string (allocated in private memory)
1408 */
1409 char *lost_held_post_request(int *lgth, long rtime, char *type)
1410 {
1411 int buffersize = 0;
1412
1413 char buf[BUFSIZE];
1414 char *doc = NULL;
1415
1416 xmlChar *xmlbuff = NULL;
1417 xmlDocPtr request = NULL;
1418
1419 xmlNodePtr ptrLocationRequest = NULL;
1420 xmlNodePtr ptrLocationType = NULL;
1421
1422 *lgth = 0;
1423
1424 /*
1425 https://tools.ietf.org/html/rfc6753
1426
1427 <?xml version="1.0" encoding="UTF-8"?>
1428 <locationRequest
1429 responseTime="emergencyRouting"
1430 xmlns="urn:ietf:params:xml:ns:geopriv:held">
1431 <locationType exact="false">
1432 geodetic civic
1433 </locationType>
1434 </locationRequest>
1435 */
1436
1437 /* create request */
1438 request = xmlNewDoc(BAD_CAST "1.0");
1439 if(request == NULL) {
1440 LM_ERR("locationRequest xmlNewDoc() failed\n");
1441 return doc;
1442 }
1443 /* locationRequest - element */
1444 ptrLocationRequest = xmlNewNode(NULL, BAD_CAST "locationRequest");
1445 if(ptrLocationRequest == NULL) {
1446 LM_ERR("locationRequest xmlNewNode() failed\n");
1447 xmlFreeDoc(request);
1448 return doc;
1449 }
1450
1451 xmlDocSetRootElement(request, ptrLocationRequest);
1452 /* properties */
1453 xmlNewProp(ptrLocationRequest, BAD_CAST "xmlns",
1454 BAD_CAST "urn:ietf:params:xml:ns:geopriv:held");
1455 /* responseTime - element (default: emergencyRouting) */
1456 if(rtime > 0) {
1457 /* set responseTime "<ms>") */
1458 snprintf(buf, BUFSIZE, "%ld", rtime);
1459 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST buf);
1460 } else if(rtime == -1) {
1461 /* set responseTime "emergencyDispatch" */
1462 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST HELD_ED);
1463 } else {
1464 /* set responseTime "emergencyRouting" */
1465 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST HELD_ER);
1466 }
1467 /* locationType - element (optional) */
1468 ptrLocationType =
1469 xmlNewChild(ptrLocationRequest, NULL, BAD_CAST "locationType",
1470 type == NULL ? BAD_CAST "geodetic civic" : BAD_CAST type);
1471 /* properties */
1472 xmlNewProp(ptrLocationType, BAD_CAST "exact", BAD_CAST "false");
1473
1474 xmlDocDumpFormatMemory(request, &xmlbuff, &buffersize, 0);
1475 if(xmlbuff == NULL) {
1476 LM_ERR("locationRequest xmlDocDumpFormatMemory() failed\n");
1477 xmlFreeDoc(request);
1478 return doc;
1479 }
1480
1481 doc = (char *)pkg_malloc((buffersize + 1) * sizeof(char));
1482 if(doc == NULL) {
1483 PKG_MEM_ERROR;
1484 xmlFree(xmlbuff);
1485 xmlFreeDoc(request);
1486 return doc;
1487 }
1488
1489 memset(doc, 0, buffersize);
1490 memcpy(doc, (char *)xmlbuff, buffersize);
1491 doc[buffersize] = '\0';
1492
1493 *lgth = strlen(doc);
1494
1495 xmlFree(xmlbuff);
1496 xmlFreeDoc(request);
1497
1498 return doc;
1499 }
1500
1501 /*
1502 * lost_held_location_request(held, lgth)
1503 * assembles and returns locationRequest string (allocated in private memory)
1504 */
1505 char *lost_held_location_request(p_lost_held_t held, int *lgth)
1506 {
1507 int buffersize = 0;
1508
1509 char buf[BUFSIZE];
1510 char *doc = NULL;
1511
1512 xmlChar *xmlbuff = NULL;
1513 xmlDocPtr request = NULL;
1514
1515 xmlNodePtr ptrLocationRequest = NULL;
1516 xmlNodePtr ptrLocationType = NULL;
1517 xmlNodePtr ptrDevice = NULL;
1518
1519 *lgth = 0;
1520
1521 /*
1522 https://tools.ietf.org/html/rfc6155
1523 https://tools.ietf.org/html/rfc5985
1524
1525 <?xml version="1.0" encoding="UTF-8"?>
1526 <locationRequest xmlns="urn:ietf:params:xml:ns:geopriv:held"
1527 responseTime="emergencyRouting">
1528 <locationType exact="true">geodetic locationURI</locationType>
1529 <device xmlns="urn:ietf:params:xml:ns:geopriv:held:id">
1530 <uri>sip:[email protected]</uri>
1531 </device>
1532 </locationRequest>
1533 */
1534
1535 /* create request */
1536 request = xmlNewDoc(BAD_CAST "1.0");
1537 if(request == NULL) {
1538 LM_ERR("locationRequest xmlNewDoc() failed\n");
1539 return doc;
1540 }
1541 /* locationRequest - element */
1542 ptrLocationRequest = xmlNewNode(NULL, BAD_CAST HELD_LR);
1543 if(ptrLocationRequest == NULL) {
1544 LM_ERR("locationRequest xmlNewNode() failed\n");
1545 xmlFreeDoc(request);
1546 return doc;
1547 }
1548 xmlDocSetRootElement(request, ptrLocationRequest);
1549 /* properties */
1550 xmlNewProp(ptrLocationRequest, BAD_CAST "xmlns",
1551 BAD_CAST "urn:ietf:params:xml:ns:geopriv:held");
1552 /* responseTime - element (default: emergencyRouting) */
1553 if(held->time > 0) {
1554 /* set responseTime "<ms>") */
1555 snprintf(buf, BUFSIZE, "%d", held->time);
1556 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST buf);
1557 } else if(held->time == -1) {
1558 /* set responseTime "emergencyDispatch" */
1559 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST HELD_ED);
1560 } else {
1561 /* set responseTime "emergencyRouting" */
1562 xmlNewProp(ptrLocationRequest, BAD_CAST HELD_RT, BAD_CAST HELD_ER);
1563 }
1564 /* locationType - element (optional) */
1565 ptrLocationType = xmlNewChild(
1566 ptrLocationRequest, NULL, BAD_CAST HELD_LT, BAD_CAST held->type);
1567 /* properties */
1568 xmlNewProp(ptrLocationType, BAD_CAST "exact",
1569 (held->exact == HELD_EXACT_TRUE) ? BAD_CAST "true"
1570 : BAD_CAST "false");
1571 /* device - element */
1572 ptrDevice = xmlNewChild(ptrLocationRequest, NULL, BAD_CAST "device", NULL);
1573 if(ptrDevice == NULL) {
1574 LM_ERR("locationRequest xmlNewChild() failed\n");
1575 xmlFreeDoc(request);
1576 return doc;
1577 }
1578 /* properties */
1579 xmlNewProp(ptrDevice, BAD_CAST "xmlns",
1580 BAD_CAST "urn:ietf:params:xml:ns:geopriv:held:id");
1581 /* uri - element */
1582 xmlNewChild(ptrDevice, NULL, BAD_CAST "uri", BAD_CAST held->identity);
1583
1584 xmlDocDumpFormatMemory(request, &xmlbuff, &buffersize, 0);
1585 if(xmlbuff == NULL) {
1586 LM_ERR("locationRequest xmlDocDumpFormatMemory() failed\n");
1587 xmlFreeDoc(request);
1588 return doc;
1589 }
1590
1591 doc = (char *)pkg_malloc((buffersize + 1) * sizeof(char));
1592 if(doc == NULL) {
1593 PKG_MEM_ERROR;
1594 xmlFree(xmlbuff);
1595 xmlFreeDoc(request);
1596 return doc;
1597 }
1598
1599 memset(doc, 0, buffersize);
1600 memcpy(doc, (char *)xmlbuff, buffersize);
1601 doc[buffersize] = '\0';
1602
1603 *lgth = strlen(doc);
1604
1605 xmlFree(xmlbuff);
1606 xmlFreeDoc(request);
1607
1608 return doc;
1609 }
1610
1611 /*
1612 * lost_append_via_element(head, parent)
1613 * appends via elements and returns the number of via elements added
1614 */
1615 int lost_append_via_element(p_lost_list_t *head, xmlNodePtr *parent)
1616 {
1617 int cnt = 0;
1618 int i;
1619 p_lost_list_t current = NULL;
1620
1621 if(head == NULL) {
1622 return 0;
1623 }
1624
1625 /* at least one <via> element to add */
1626 current = *head;
1627 cnt++;
1628
1629 /* check for more <via> elements to add */
1630 while(current->next != NULL) {
1631 cnt++;
1632 current = current->next;
1633 }
1634
1635 current = *head;
1636 xmlNodePtr ptrVia[cnt];
1637
1638 /* ad <via> elements to <path> element */
1639 for(i = 0; i < cnt; i++) {
1640 ptrVia[i] = xmlNewChild(*parent, NULL, BAD_CAST "via", NULL);
1641 xmlNewProp(ptrVia[i], BAD_CAST "source", BAD_CAST current->value);
1642 current = current->next;
1643 }
1644
1645 return cnt;
1646 }
1647
1648
1649 /*
1650 * lost_find_service_request(loc, path, lgth)
1651 * assembles and returns findService request string (allocated in private memory)
1652 */
1653 char *lost_find_service_request(p_lost_loc_t loc, p_lost_list_t path, int *lgth)
1654 {
1655 int buffersize = 0;
1656
1657 char buf[BUFSIZE];
1658 char *doc = NULL;
1659
1660 xmlChar *xmlbuff = NULL;
1661 xmlDocPtr request = NULL;
1662
1663 xmlNodePtr ptrFindService = NULL;
1664 xmlNodePtr ptrLocation = NULL;
1665 xmlNodePtr ptrPoint = NULL;
1666 xmlNodePtr ptrCircle = NULL;
1667 xmlNodePtr ptrRadius = NULL;
1668 xmlNodePtr ptrNode = NULL;
1669 xmlNodePtr ptrPath = NULL;
1670
1671 *lgth = 0;
1672
1673 /*
1674 https://tools.ietf.org/html/rfc5222
1675
1676 <?xml version="1.0" encoding="UTF-8"?>
1677 <findService
1678 xmlns="urn:ietf:params:xml:ns:lost1"
1679 xmlns:p2="http://www.opengis.net/gml"
1680 serviceBoundary="value"
1681 recursive="true">
1682 <location id="6020688f1ce1896d" profile="geodetic-2d">
1683 <p2:Point id="point1" srsName="urn:ogc:def:crs:EPSG::4326">
1684 <p2:pos>37.775 -122.422</p2:pos>
1685 </p2:Point>
1686 </location>
1687 <service>urn:service:sos.police</service>
1688 <path>
1689 <via source="resolver.example"/>
1690 <via source="authoritative.example"/>
1691 </path>
1692 </findService>
1693 */
1694 /* create request */
1695 request = xmlNewDoc(BAD_CAST "1.0");
1696 if(request == NULL) {
1697 LM_ERR("findService request xmlNewDoc() failed\n");
1698 return doc;
1699 }
1700 /* findService - element */
1701 ptrFindService = xmlNewNode(NULL, BAD_CAST "findService");
1702 if(ptrFindService == NULL) {
1703 LM_ERR("findService xmlNewNode() failed\n");
1704 xmlFreeDoc(request);
1705 return doc;
1706 }
1707 xmlDocSetRootElement(request, ptrFindService);
1708 /* set properties */
1709 xmlNewProp(ptrFindService, BAD_CAST "xmlns",
1710 BAD_CAST "urn:ietf:params:xml:ns:lost1");
1711 xmlNewProp(ptrFindService, BAD_CAST "serviceBoundary",
1712 (loc->boundary == 1) ? BAD_CAST "value" : BAD_CAST "reference");
1713 xmlNewProp(ptrFindService, BAD_CAST "recursive",
1714 (loc->recursive == 1) ? BAD_CAST "true" : BAD_CAST "false");
1715 if(loc->xpath == NULL) {
1716 xmlNewProp(ptrFindService, BAD_CAST "xmlns:p2",
1717 BAD_CAST "http://www.opengis.net/gml");
1718 }
1719
1720 /* location - element */
1721 ptrLocation = xmlNewChild(ptrFindService, NULL, BAD_CAST "location", NULL);
1722 xmlNewProp(ptrLocation, BAD_CAST "id", BAD_CAST loc->identity);
1723 xmlNewProp(ptrLocation, BAD_CAST "profile", BAD_CAST loc->profile);
1724 /* set pos */
1725 snprintf(buf, BUFSIZE, "%s %s", loc->latitude, loc->longitude);
1726 /* xpath result */
1727 if(loc->xpath != NULL) {
1728 xmlParseInNodeContext(
1729 ptrLocation, loc->xpath, strlen(loc->xpath), 0, &ptrNode);
1730 if(ptrNode == NULL) {
1731 LM_ERR("locationRequest xmlParseInNodeContext() failed\n");
1732 xmlFreeDoc(request);
1733 return doc;
1734 } else {
1735 xmlAddChild(ptrLocation, ptrNode);
1736 }
1737 }
1738 /* Point */
1739 else if(loc->radius == 0) {
1740 ptrPoint = xmlNewChild(ptrLocation, NULL, BAD_CAST "Point", NULL);
1741 if(ptrPoint == NULL) {
1742 LM_ERR("locationRequest xmlNewChild() failed\n");
1743 xmlFreeDoc(request);
1744 return doc;
1745 }
1746 xmlNewProp(ptrPoint, BAD_CAST "xmlns",
1747 BAD_CAST "http://www.opengis.net/gml");
1748 xmlNewProp(ptrPoint, BAD_CAST "srsName",
1749 BAD_CAST "urn:ogc:def:crs:EPSG::4326");
1750 /* pos */
1751 xmlNewChild(ptrPoint, NULL, BAD_CAST "pos", BAD_CAST buf);
1752 }
1753 /* circle - Point */
1754 else {
1755 ptrCircle = xmlNewChild(ptrLocation, NULL, BAD_CAST "gs:Circle", NULL);
1756 if(ptrCircle == NULL) {
1757 LM_ERR("locationRequest xmlNewChild() failed\n");
1758 xmlFreeDoc(request);
1759 return doc;
1760 }
1761 xmlNewProp(ptrCircle, BAD_CAST "xmlns:gml",
1762 BAD_CAST "http://www.opengis.net/gml");
1763 xmlNewProp(ptrCircle, BAD_CAST "xmlns:gs",
1764 BAD_CAST "http://www.opengis.net/pidflo/1.0");
1765 xmlNewProp(ptrCircle, BAD_CAST "srsName",
1766 BAD_CAST "urn:ogc:def:crs:EPSG::4326");
1767 /* pos */
1768 xmlNewChild(ptrCircle, NULL, BAD_CAST "gml:pos", BAD_CAST buf);
1769 /* circle - radius */
1770 snprintf(buf, BUFSIZE, "%d", loc->radius);
1771 ptrRadius = xmlNewChild(
1772 ptrCircle, NULL, BAD_CAST "gs:radius", BAD_CAST buf);
1773 if(ptrRadius == NULL) {
1774 LM_ERR("locationRequest xmlNewChild() failed\n");
1775 xmlFreeDoc(request);
1776 return doc;
1777 }
1778 xmlNewProp(ptrRadius, BAD_CAST "uom",
1779 BAD_CAST "urn:ogc:def:uom:EPSG::9001");
1780 }
1781 /* service - element */
1782 snprintf(buf, BUFSIZE, "%s", loc->urn);
1783 xmlNewChild(ptrFindService, NULL, BAD_CAST "service", BAD_CAST buf);
1784 /* service - element */
1785 if(path != NULL) {
1786 ptrPath = xmlNewChild(ptrFindService, NULL, BAD_CAST "path", NULL);
1787 if(ptrPath == NULL) {
1788 LM_ERR("locationRequest xmlNewChild() failed\n");
1789 xmlFreeDoc(request);
1790 return doc;
1791 }
1792 if(lost_append_via_element(&path, &ptrPath) == 0) {
1793 LM_ERR("appending <via> elements to <path> failed\n");
1794 xmlFreeDoc(request);
1795 return doc;
1796 }
1797 }
1798
1799 xmlDocDumpFormatMemory(request, &xmlbuff, &buffersize, 0);
1800 if(xmlbuff == NULL) {
1801 LM_ERR("findService request xmlDocDumpFormatMemory() failed\n");
1802 xmlFreeDoc(request);
1803 return doc;
1804 }
1805
1806 doc = (char *)pkg_malloc((buffersize + 1) * sizeof(char));
1807 if(doc == NULL) {
1808 PKG_MEM_ERROR;
1809 xmlFree(xmlbuff);
1810 xmlFreeDoc(request);
1811 return doc;
1812 }
1813
1814 memset(doc, 0, buffersize);
1815 memcpy(doc, (char *)xmlbuff, buffersize);
1816 doc[buffersize] = '\0';
1817
1818 *lgth = strlen(doc);
1819
1820 xmlFree(xmlbuff);
1821 xmlFreeDoc(request);
1822
1823 return doc;
1824 }