Debian Codegraph @

src/modules/lost/response.c

raw symbols SCIP
1 /*
2 * lost module LoST response parsing 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 :: response
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
40 #include "../../core/parser/msg_parser.h"
41 #include "../../core/parser/parse_content.h"
42 #include "../../core/parser/parse_uri.h"
43 #include "../../core/parser/parse_from.h"
44 #include "../../core/parser/parse_ppi_pai.h"
45 #include "../../core/dprint.h"
46 #include "../../core/mem/mem.h"
47 #include "../../core/mem/shm_mem.h"
48 #include "../../core/rand/kam_rand.h"
49
50 #include <libxml/parser.h>
51 #include <libxml/xmlmemory.h>
52 #include <libxml/xpath.h>
53 #include <libxml/xpathInternals.h>
54
55 #include "pidf.h"
56 #include "utilities.h"
57 #include "response.h"
58
59 /*
60 * is_http_laquot(search)
61 * return 1 if true else 0
62 */
63 int is_http_laquot(char *search)
64 {
65 if(search == NULL) {
66 return 0;
67 }
68 if(strlen(search) < strlen("<http:")) {
69 return 0;
70 }
71 if((*(search + 0) == '<')
72 && ((*(search + 1) == 'h') || (*(search + 1) == 'H'))
73 && ((*(search + 2) == 't') || (*(search + 2) == 'T'))
74 && ((*(search + 3) == 't') || (*(search + 3) == 'T'))
75 && ((*(search + 4) == 'p') || (*(search + 4) == 'P'))
76 && ((*(search + 5) == ':'))) {
77 return 1;
78 }
79 return 0;
80 }
81
82 /*
83 * is_https_laquot(search)
84 * return 1 if true else 0
85 */
86 int is_https_laquot(char *search)
87 {
88 if(search == NULL) {
89 return 0;
90 }
91 if(strlen(search) < strlen("<https:")) {
92 return 0;
93 }
94 if((*(search + 0) == '<')
95 && ((*(search + 1) == 'h') || (*(search + 1) == 'H'))
96 && ((*(search + 2) == 't') || (*(search + 2) == 'T'))
97 && ((*(search + 3) == 't') || (*(search + 3) == 'T'))
98 && ((*(search + 4) == 'p') || (*(search + 4) == 'P'))
99 && ((*(search + 5) == 's') || (*(search + 5) == 'S'))
100 && ((*(search + 6) == ':'))) {
101 return 1;
102 }
103 return 0;
104 }
105
106 /*
107 * is_http(search)
108 * return 1 if true else 0
109 */
110 int is_http(char *search)
111 {
112 if(search == NULL) {
113 return 0;
114 }
115 if(strlen(search) < strlen("http:")) {
116 return 0;
117 }
118 if(((*(search + 0) == 'h') || (*(search + 0) == 'H'))
119 && ((*(search + 1) == 't') || (*(search + 1) == 'T'))
120 && ((*(search + 2) == 't') || (*(search + 2) == 'T'))
121 && ((*(search + 3) == 'p') || (*(search + 3) == 'P'))
122 && ((*(search + 4) == ':'))) {
123 return 1;
124 }
125 return 0;
126 }
127
128 /*
129 * is_https(search)
130 * return 1 if true else 0
131 */
132 int is_https(char *search)
133 {
134 if(search == NULL) {
135 return 0;
136 }
137 if(strlen(search) < strlen("https:")) {
138 return 0;
139 }
140 if(((*(search + 0) == 'h') || (*(search + 0) == 'H'))
141 && ((*(search + 1) == 't') || (*(search + 1) == 'T'))
142 && ((*(search + 2) == 't') || (*(search + 2) == 'T'))
143 && ((*(search + 3) == 'p') || (*(search + 3) == 'P'))
144 && ((*(search + 4) == 's') || (*(search + 4) == 'S'))
145 && ((*(search + 5) == ':'))) {
146 return 1;
147 }
148 return 0;
149 }
150
151 /*
152 * is_cid_laquot(search)
153 * return 1 if true else 0
154 */
155 int is_cid_laquot(char *search)
156 {
157 if(search == NULL) {
158 return 0;
159 }
160 if(strlen(search) < strlen("<cid:")) {
161 return 0;
162 }
163 if((*(search + 0) == '<')
164 && ((*(search + 1) == 'c') || (*(search + 1) == 'C'))
165 && ((*(search + 2) == 'i') || (*(search + 2) == 'I'))
166 && ((*(search + 3) == 'd') || (*(search + 3) == 'D'))
167 && (*(search + 4) == ':')) {
168 return 1;
169 }
170 return 0;
171 }
172
173 /*
174 * is_cid(search)
175 * return 1 if true else 0
176 */
177 int is_cid(char *search)
178 {
179 if(search == NULL) {
180 return 0;
181 }
182 if(strlen(search) < strlen("cid:")) {
183 return 0;
184 }
185 if(((*(search + 0) == 'c') || (*(search + 0) == 'C'))
186 && ((*(search + 1) == 'i') || (*(search + 1) == 'I'))
187 && ((*(search + 2) == 'd') || (*(search + 2) == 'D'))
188 && (*(search + 3) == ':')) {
189 return 1;
190 }
191 return 0;
192 }
193
194 /*
195 * is_urn(search)
196 * return 1 if true else 0
197 */
198 int is_urn(char *search)
199 {
200 if(search == NULL) {
201 return 0;
202 }
203 if(strlen(search) < strlen("urn:")) {
204 return 0;
205 }
206 if(((*(search + 0) == 'u') || (*(search + 0) == 'U'))
207 && ((*(search + 1) == 'r') || (*(search + 1) == 'R'))
208 && ((*(search + 2) == 'n') || (*(search + 2) == 'N'))
209 && (*(search + 3) == ':')) {
210 return 1;
211 }
212 return 0;
213 }
214
215 /*
216 * lost_new_response(void)
217 * creates a new response object in private memory and returns a pointer
218 */
219 p_lost_fsr_t lost_new_response(void)
220 {
221 p_lost_fsr_t res;
222
223 res = (p_lost_fsr_t)pkg_malloc(sizeof(s_lost_fsr_t));
224 if(res == NULL) {
225 return NULL;
226 }
227 res->category = OTHER;
228 res->mapping = NULL;
229 res->path = NULL;
230 res->warnings = NULL;
231 res->errors = NULL;
232 res->redirect = NULL;
233 res->uri = NULL;
234
235 LM_DBG("### reponse data initialized\n");
236
237 return res;
238 }
239
240 /*
241 * lost_new_response_type(void)
242 * creates a new response type object in private memory and returns a pointer
243 */
244 p_lost_type_t lost_new_response_type(void)
245 {
246 p_lost_type_t res;
247
248 res = (p_lost_type_t)pkg_malloc(sizeof(s_lost_type_t));
249 if(res == NULL) {
250 PKG_MEM_ERROR;
251 return NULL;
252 }
253 res->type = NULL;
254 res->target = NULL;
255 res->source = NULL;
256
257 res->info = (p_lost_info_t)pkg_malloc(sizeof(s_lost_info_t));
258 if(res->info == NULL) {
259 PKG_MEM_ERROR;
260 } else {
261 res->info->text = NULL;
262 res->info->lang = NULL;
263 }
264
265 LM_DBG("### type data initialized\n");
266
267 return res;
268 }
269
270 /*
271 * lost_new_response_issues(void)
272 * creates a new issues object in private memory and returns a pointer
273 */
274 p_lost_issue_t lost_new_response_issues(void)
275 {
276 p_lost_issue_t res = NULL;
277
278 res = (p_lost_issue_t)pkg_malloc(sizeof(s_lost_issue_t));
279 if(res == NULL) {
280 PKG_MEM_ERROR;
281 return NULL;
282 }
283 res->issue = NULL;
284 res->next = NULL;
285
286 LM_DBG("### issues data initialized\n");
287
288 return res;
289 }
290
291 /*
292 * lost_new_response_data(void)
293 * creates a new response data object in private memory and returns a pointer
294 */
295 p_lost_data_t lost_new_response_data(void)
296 {
297 p_lost_data_t res;
298
299 res = (p_lost_data_t)pkg_malloc(sizeof(s_lost_data_t));
300 if(res == NULL) {
301 PKG_MEM_ERROR;
302 return NULL;
303 }
304 res->expires = NULL;
305 res->updated = NULL;
306 res->source = NULL;
307 res->sourceid = NULL;
308 res->urn = NULL;
309 res->name = NULL;
310 res->number = NULL;
311
312 LM_DBG("### mapping data initialized\n");
313
314 return res;
315 }
316
317 /*
318 * lost_new_response_list(void)
319 * creates a new response list object in private memory and returns a pointer
320 */
321 p_lost_list_t lost_new_response_list(void)
322 {
323 p_lost_list_t list;
324
325 list = (p_lost_list_t)pkg_malloc(sizeof(s_lost_list_t));
326 if(list == NULL) {
327 PKG_MEM_ERROR;
328 return NULL;
329 }
330
331 list->value = NULL;
332 list->next = NULL;
333
334 LM_DBG("### list data initialized\n");
335
336 return list;
337 }
338
339 /*
340 * lost_reverse_response_list(list)
341 * reverses list order of a list object
342 */
343 void lost_reverse_response_list(p_lost_list_t *head)
344 {
345 p_lost_list_t prev = NULL;
346 p_lost_list_t next = NULL;
347 p_lost_list_t current = *head;
348
349 while(current != NULL) {
350 next = current->next;
351 current->next = prev;
352 prev = current;
353 current = next;
354 }
355 *head = prev;
356 }
357
358 /*
359 * lost_append_response_list(list, str)
360 * appends str value to list object and returns str len
361 */
362 int lost_append_response_list(p_lost_list_t *head, str val)
363 {
364 int len = 0;
365 p_lost_list_t new = NULL;
366 p_lost_list_t current = *head;
367
368 new = lost_new_response_list();
369 if(new != NULL) {
370 new->value = lost_copy_string(val, &len);
371 new->next = NULL;
372
373 LM_DBG("### new list data [%.*s]\n", val.len, val.s);
374
375 if(current == NULL) {
376 *head = new;
377 return len;
378 }
379 while(current->next != NULL) {
380 current = current->next;
381 }
382 current->next = new;
383 }
384 return len;
385 }
386
387 /*
388 * lost_search_response_list(list, value, search)
389 * looks for search string in list object and returns pointer if found
390 */
391 int lost_search_response_list(p_lost_list_t *list, char **val, const char *str)
392 {
393 p_lost_list_t cur;
394 p_lost_list_t next;
395
396 if(*list == NULL)
397 return 0;
398
399 if(str == NULL)
400 return 0;
401
402 LM_DBG("### list data search [%s]\n", str);
403
404 next = *list;
405 while((cur = next) != NULL) {
406 next = cur->next;
407 if(cur->value != NULL) {
408 if(strncasecmp(cur->value, str, strlen(str)) == 0) {
409 *val = cur->value;
410
411 LM_DBG("###\t[%s] found\n", cur->value);
412
413 return 1;
414 }
415 }
416 }
417
418 return 0;
419 }
420
421 /*
422 * lost_delete_response_list(list)
423 * removes response list from private memory
424 */
425 void lost_delete_response_list(p_lost_list_t *list)
426 {
427 p_lost_list_t cur;
428
429 if(*list == NULL)
430 return;
431
432 while((cur = *list) != NULL) {
433 *list = cur->next;
434 if(cur->value != NULL) {
435 pkg_free(cur->value);
436 }
437 pkg_free(cur);
438 }
439
440 *list = NULL;
441
442 LM_DBG("### list data deleted\n");
443
444 return;
445 }
446
447 /*
448 * lost_delete_response_msg(msg)
449 * removes response info from private memory
450 */
451 void lost_delete_response_info(p_lost_info_t *info)
452 {
453 p_lost_info_t ptr;
454
455 if(*info == NULL)
456 return;
457
458 ptr = *info;
459
460 if(ptr->text != NULL) {
461 pkg_free(ptr->text);
462 }
463 if(ptr->lang != NULL) {
464 pkg_free(ptr->lang);
465 }
466
467 pkg_free(ptr);
468 *info = NULL;
469
470 LM_DBG("### info data deleted\n");
471
472 return;
473 }
474
475 /*
476 * lost_delete_response_msg(type)
477 * removes response type from private memory
478 */
479 void lost_delete_response_type(p_lost_type_t *type)
480 {
481 p_lost_type_t ptr;
482
483 if(*type == NULL)
484 return;
485
486 ptr = *type;
487
488 if(ptr->type != NULL) {
489 pkg_free(ptr->type);
490 }
491 if(ptr->target != NULL) {
492 pkg_free(ptr->target);
493 }
494 if(ptr->source != NULL) {
495 pkg_free(ptr->source);
496 }
497 if(ptr->info != NULL) {
498 lost_delete_response_info(&ptr->info);
499 }
500
501 pkg_free(ptr);
502 *type = NULL;
503
504 LM_DBG("### type data deleted\n");
505
506 return;
507 }
508
509 /*
510 * lost_delete_response_issue(list)
511 * removes response issue object from private memory
512 */
513 void lost_delete_response_issues(p_lost_issue_t *list)
514 {
515 p_lost_issue_t cur;
516
517 while((cur = *list) != NULL) {
518 *list = cur->next;
519 if(cur->issue != NULL) {
520 lost_delete_response_type(&cur->issue);
521 }
522 pkg_free(cur);
523 }
524
525 *list = NULL;
526
527 LM_DBG("### issue data deleted\n");
528
529 return;
530 }
531
532 /*
533 * lost_delete_response_issue(mapping)
534 * removes response data object from private memory
535 */
536 void lost_delete_response_data(p_lost_data_t *m)
537 {
538 p_lost_data_t ptr;
539
540 if(*m == NULL)
541 return;
542
543 ptr = *m;
544
545 if(ptr->expires != NULL) {
546 pkg_free(ptr->expires);
547 }
548 if(ptr->updated != NULL) {
549 pkg_free(ptr->updated);
550 }
551 if(ptr->source != NULL) {
552 pkg_free(ptr->source);
553 }
554 if(ptr->sourceid != NULL) {
555 pkg_free(ptr->sourceid);
556 }
557 if(ptr->urn != NULL) {
558 pkg_free(ptr->urn);
559 }
560 if(ptr->name != NULL) {
561 lost_delete_response_info(&ptr->name);
562 }
563 if(ptr->number != NULL) {
564 pkg_free(ptr->number);
565 }
566
567 pkg_free(ptr);
568 *m = NULL;
569
570 LM_DBG("### mapping data deleted\n");
571
572 return;
573 }
574
575 /*
576 * lost_free_findServiceResponse(response)
577 * removes findServiceResponse object from private memory
578 */
579 void lost_free_findServiceResponse(p_lost_fsr_t *res)
580 {
581 p_lost_fsr_t ptr;
582
583 if(*res == NULL)
584 return;
585
586 ptr = *res;
587
588 if(ptr->mapping != NULL) {
589 lost_delete_response_data(&ptr->mapping);
590 }
591 if(ptr->path != NULL) {
592 lost_delete_response_list(&ptr->path);
593 }
594 if(ptr->warnings != NULL) {
595 lost_delete_response_issues(&ptr->warnings);
596 }
597 if(ptr->errors != NULL) {
598 lost_delete_response_issues(&ptr->errors);
599 }
600 if(ptr->redirect != NULL) {
601 lost_delete_response_type(&ptr->redirect);
602 }
603 if(ptr->uri != NULL) {
604 lost_delete_response_list(&ptr->uri);
605 }
606
607 pkg_free(ptr);
608 *res = NULL;
609
610 LM_DBG("### findServiceResponse deleted\n");
611
612 return;
613 }
614
615 /*
616 * lost_get_response_issue(node)
617 * parses response issue (errors, warnings) and writes
618 * results to issue object
619 */
620 p_lost_issue_t lost_get_response_issues(xmlNodePtr node)
621 {
622 xmlNodePtr cur = NULL;
623
624 p_lost_issue_t list = NULL;
625 p_lost_issue_t new = NULL;
626 p_lost_type_t issue = NULL;
627
628 str tmp = STR_NULL;
629
630 int len = 0;
631
632 if(node == NULL) {
633 return NULL;
634 }
635
636 LM_DBG("### LOST\t%s\n", node->name);
637
638 cur = node->children;
639 while(cur) {
640 if(cur->type == XML_ELEMENT_NODE) {
641 /* get a new response type object */
642 issue = lost_new_response_type();
643 if(issue == NULL) {
644 /* didn't get it ... return */
645 break;
646 }
647 /* get issue type */
648 tmp.s = (char *)cur->name;
649 tmp.len = strlen((char *)cur->name);
650 /* copy issue type to object */
651 len = 0;
652 if(tmp.len > 0 && tmp.s != NULL) {
653 issue->type = lost_copy_string(tmp, &len);
654 }
655 if(len == 0) {
656 /* issue type not found, clean up and return */
657 lost_delete_response_type(&issue); /* clean up */
658 break;
659 }
660 /* parse source property */
661 len = 0;
662 issue->source = lost_get_property(cur->parent, MAPP_PROP_SRC, &len);
663 if(len == 0) {
664 /* source property not found, clean up and return */
665 lost_delete_response_type(&issue); /* clean up */
666 break;
667 }
668
669 LM_DBG("###\t[%s]\n", issue->type);
670
671 /* type and source property found ... parse text and copy */
672 if(issue->info != NULL) {
673 issue->info->text = lost_get_property(cur, PROP_MSG, &len);
674 issue->info->lang = lost_get_property(cur, PROP_LANG, &len);
675 }
676 /* get a new list element */
677 new = lost_new_response_issues();
678 if(new == NULL) {
679 /* didn't get it, clean up and return */
680 lost_delete_response_type(&issue); /* clean up */
681 break;
682 }
683 /* parsing done, append object to list */
684 new->issue = issue;
685 new->next = list;
686 list = new;
687
688 /* get next element */
689 cur = cur->next;
690 }
691 }
692
693 return list;
694 }
695
696 /*
697 * lost_get_response_list(node, name, property)
698 * parses response list and writes results to list object
699 */
700 p_lost_list_t lost_get_response_list(
701 xmlNodePtr node, const char *name, const char *prop)
702 {
703 xmlNodePtr cur = NULL;
704
705 p_lost_list_t list = NULL;
706 p_lost_list_t new = NULL;
707
708 str tmp = STR_NULL;
709 int len = 0;
710
711 if(node == NULL) {
712 return list;
713 }
714
715 LM_DBG("### LOST\t%s\n", node->name);
716
717 for(cur = node; cur; cur = cur->next) {
718 if(cur->type == XML_ELEMENT_NODE) {
719 if(!xmlStrcasecmp(cur->name, (unsigned char *)name)) {
720 new = lost_new_response_list();
721 if(new != NULL) {
722 if(prop) {
723 tmp.s = lost_get_property(cur, prop, &tmp.len);
724 } else {
725 tmp.s = lost_get_content(cur, name, &tmp.len);
726 }
727 if(tmp.len > 0 && tmp.s != NULL) {
728 new->value = lost_copy_string(tmp, &len);
729
730 LM_DBG("###\t[%s]\n", new->value);
731
732 new->next = list;
733 list = new;
734 } else {
735 lost_delete_response_list(&new); /* clean up */
736 }
737 lost_free_string(&tmp); /* clean up */
738 }
739 }
740 } else {
741 /* not an uri element */
742 break;
743 }
744 }
745
746 return list;
747 }
748
749 /*
750 * lost_get_response_element(node, name)
751 * parses response element and returns a char pointer
752 */
753 char *lost_get_response_element(xmlNodePtr node, const char *name)
754 {
755 char *ret = NULL;
756 int len = 0;
757
758 if(node == NULL) {
759 return ret;
760 }
761
762 LM_DBG("### LOST %s\n", node->name);
763
764 ret = lost_get_content(node, name, &len);
765
766 LM_DBG("###\t[%.*s]\n", len, ret);
767
768 return ret;
769 }
770
771 /*
772 * lost_get_response_type(node, name)
773 * parses response type and writes results to type object
774 */
775 p_lost_type_t lost_get_response_type(xmlNodePtr node, const char *name)
776 {
777 p_lost_type_t res = NULL;
778
779 str tmp = STR_NULL;
780
781 int len = 0;
782
783 if(node == NULL) {
784 return res;
785 }
786
787 LM_DBG("### LOST %s\n", node->name);
788
789 tmp.s = lost_get_childname(node, name, &tmp.len);
790 if(tmp.len > 0 && tmp.s != NULL) {
791 res = lost_new_response_type();
792 if(res != NULL) {
793 res->type = lost_copy_string(tmp, &len);
794 if(len > 0) {
795
796 LM_DBG("###\t[%s]\n", res->type);
797 }
798 if(res->info != NULL) {
799 res->info->text =
800 lost_get_property(node->children, PROP_MSG, &len);
801 res->info->lang =
802 lost_get_property(node->children, PROP_LANG, &len);
803 }
804 }
805 lost_free_string(&tmp); /* clean up */
806 }
807
808 return res;
809 }
810
811 /*
812 * lost_get_response_info(node, name, property)
813 * parses response info (text, language) and writes results to info object
814 */
815 p_lost_info_t lost_get_response_info(
816 xmlNodePtr node, const char *name, const char *prop)
817 {
818 p_lost_info_t res = NULL;
819
820 str tmp = STR_NULL;
821
822 int len = 0;
823
824 if(node == NULL) {
825 return res;
826 }
827
828 LM_DBG("### LOST %s\n", node->name);
829
830 res = (p_lost_info_t)pkg_malloc(sizeof(s_lost_info_t));
831 if(res == NULL) {
832 PKG_MEM_ERROR;
833 return NULL;
834 } else {
835 res->text = NULL;
836 res->lang = NULL;
837 if(prop) {
838 tmp.s = lost_get_property(node, PROP_MSG, &tmp.len);
839 } else {
840 tmp.s = lost_get_content(node, name, &tmp.len);
841 }
842 if(tmp.s != NULL) {
843 res->text = lost_copy_string(tmp, &len);
844 if(res->text != NULL) {
845 LM_DBG("###\t\t[%s]\n", res->text);
846 }
847 lost_free_string(&tmp); /* clean up */
848 }
849 res->lang = lost_get_property(node, PROP_LANG, &len);
850
851 LM_DBG("###\t\t[%s]\n", res->lang);
852 }
853
854 return res;
855 }
856
857 /*
858 * lost_print_findServiceResponse(response)
859 * prints/logs response elements
860 */
861 void lost_print_findServiceResponse(p_lost_fsr_t res)
862 {
863 if(res == NULL) {
864 return;
865 }
866 p_lost_data_t m = NULL;
867 p_lost_type_t r = NULL;
868 p_lost_issue_t e = NULL;
869 p_lost_issue_t w = NULL;
870 p_lost_type_t t = NULL;
871 p_lost_list_t list = NULL;
872
873 switch(res->category) {
874 case RESPONSE:
875 if((m = res->mapping) != NULL) {
876 LM_INFO("### LOST %s:\t[%s]\n", MAPP_PROP_EXP, m->expires);
877 LM_INFO("### LOST %s:\t[%s]\n", MAPP_PROP_LUP, m->updated);
878 LM_INFO("### LOST %s:\t[%s]\n", MAPP_PROP_SRC, m->source);
879 LM_INFO("### LOST %s:\t[%s]\n", MAPP_PROP_SID, m->sourceid);
880 if(m->name) {
881 LM_INFO("### LOST %s:\t[%s (%s)]\n", DISPNAME_NODE,
882 m->name->text, m->name->lang);
883 }
884 LM_INFO("### LOST %s:\t[%s]\n", SERVICENR_NODE, m->number);
885 LM_INFO("### LOST %s:\t[%s]\n", SERVICE_NODE, m->urn);
886 if((list = res->uri) != NULL) {
887 while(list) {
888 LM_INFO("### LOST %s:\t[%s]\n", MAPP_NODE_URI,
889 list->value);
890 list = list->next;
891 }
892 }
893 if((list = res->path) != NULL) {
894 while(list) {
895 LM_INFO("### LOST %s:\t[%s]\n", PATH_NODE_VIA,
896 list->value);
897 list = list->next;
898 }
899 }
900 if((w = res->warnings) != NULL) {
901 while(w) {
902 t = w->issue;
903 LM_INFO("###! LOST %s reported ...\n", t->source);
904 LM_INFO("###! LOST %s:\n", t->type);
905 if(t->info) {
906 LM_INFO("###! LOST\t[%s (%s)]\n", t->info->text,
907 t->info->lang);
908 }
909 w = w->next;
910 }
911 }
912 }
913 break;
914 case REDIRECT:
915 if((r = res->redirect) != NULL) {
916 LM_INFO("### LOST %s:\t[%s]\n", RED_PROP_TAR, r->target);
917 LM_INFO("### LOST %s:\t[%s]\n", RED_PROP_SRC, r->source);
918 if(r->info) {
919 LM_INFO("### LOST %s:\t[%s (%s)]\n", PROP_MSG,
920 r->info->text, r->info->lang);
921 }
922 }
923 break;
924 case ERROR:
925 if((e = res->errors) != NULL) {
926 while(e) {
927 t = e->issue;
928 LM_INFO("###! LOST %s reported ...\n", t->source);
929 LM_INFO("###! LOST %s:\n", t->type);
930 if(t->info) {
931 LM_INFO("###! LOST\t[%s (%s)]\n", t->info->text,
932 t->info->lang);
933 }
934 e = e->next;
935 }
936 }
937 break;
938 case OTHER:
939 default:
940 break;
941 }
942
943 return;
944 }
945
946 /*
947 * lost_parse_findServiceResponse(str)
948 * read and parse the findServiceResponse xml string
949 */
950 p_lost_fsr_t lost_parse_findServiceResponse(str ret)
951 {
952 xmlDocPtr doc = NULL;
953 xmlNodePtr root = NULL;
954 xmlNodePtr node = NULL;
955
956 p_lost_fsr_t res = NULL;
957 p_lost_data_t m = NULL;
958 p_lost_type_t r = NULL;
959
960 int len = 0;
961
962 doc = xmlReadMemory(ret.s, ret.len, 0, 0,
963 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA);
964
965 if(doc == NULL) {
966 LM_ERR("invalid xml document: [%.*s]\n", ret.len, ret.s);
967 doc = xmlReadMemory(ret.s, ret.len, 0, NULL,
968 XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_NOCDATA
969 | XML_PARSE_RECOVER);
970 if(doc == NULL) {
971 LM_ERR("xml document recovery failed on: [%.*s]\n", ret.len, ret.s);
972 return NULL;
973 }
974
975 LM_DBG("xml document recovered\n");
976 }
977 root = xmlDocGetRootElement(doc);
978 if(root == NULL) {
979 LM_ERR("empty xml document: [%.*s]\n", ret.len, ret.s);
980 xmlFreeDoc(doc); /* clean up */
981 return NULL;
982 }
983
984 LM_DBG("### %s received\n", root->name);
985
986 res = lost_new_response();
987 if(res == NULL) {
988 PKG_MEM_ERROR;
989 xmlFreeDoc(doc); /* clean up */
990 return NULL;
991 }
992 /* check the root element ... */
993 /* findServiceResponse */
994 if(!xmlStrcmp(root->name, (unsigned char *)ROOT_NODE)) {
995 m = lost_new_response_data();
996 if(m == NULL) {
997 PKG_MEM_ERROR;
998 /* clean up */
999 xmlFreeDoc(doc);
1000 lost_free_findServiceResponse(&res);
1001 return NULL;
1002 }
1003 /* get mapping properties */
1004 node = xmlDocGetNodeByName(doc, MAPP_NODE, NULL);
1005 m->expires = lost_get_property(node, MAPP_PROP_EXP, &len);
1006 m->updated = lost_get_property(node, MAPP_PROP_LUP, &len);
1007 m->source = lost_get_property(node, MAPP_PROP_SRC, &len);
1008 m->sourceid = lost_get_property(node, MAPP_PROP_SID, &len);
1009 node = NULL;
1010 /* get the displayName element */
1011 node = xmlDocGetNodeByName(doc, DISPNAME_NODE, NULL);
1012 m->name = lost_get_response_info(node, DISPNAME_NODE, NULL);
1013 /* get the serviceNumber element */
1014 m->number = lost_get_response_element(root, SERVICENR_NODE);
1015 /* get the service element */
1016 m->urn = lost_get_response_element(root, SERVICE_NODE);
1017 node = NULL;
1018 /* get the via path (list) */
1019 node = xmlDocGetNodeByName(doc, PATH_NODE_VIA, NULL);
1020 res->path = lost_get_response_list(node, PATH_NODE_VIA, MAPP_PROP_SRC);
1021 lost_reverse_response_list(&res->path);
1022 node = NULL;
1023 /* get the uri element list */
1024 node = xmlDocGetNodeByName(doc, MAPP_NODE_URI, NULL);
1025 res->uri = lost_get_response_list(node, MAPP_NODE_URI, NULL);
1026 lost_reverse_response_list(&res->uri);
1027 node = NULL;
1028 /* get the warnings element */
1029 node = xmlDocGetNodeByName(doc, WARNINGS_NODE, NULL);
1030 res->warnings = lost_get_response_issues(node);
1031 /* return */
1032 res->mapping = m;
1033 res->category = RESPONSE;
1034 /* check the root element ... */
1035 /* errors */
1036 } else if(!xmlStrcmp(root->name, (unsigned char *)ERRORS_NODE)) {
1037 res->errors = lost_get_response_issues(root);
1038 /* return */
1039 res->category = ERROR;
1040 /* check the root element ... */
1041 /* redirect */
1042 } else if(!xmlStrcmp(root->name, (unsigned char *)RED_NODE)) {
1043 r = lost_new_response_type();
1044 if(r == NULL) {
1045 PKG_MEM_ERROR;
1046 xmlFreeDoc(doc); /* clean up */
1047 lost_free_findServiceResponse(&res);
1048 return NULL;
1049 }
1050 r->source = lost_get_property(root, RED_PROP_SRC, &len);
1051 r->target = lost_get_property(root, RED_PROP_TAR, &len);
1052 if(r->info != NULL) {
1053 r->info->text = lost_get_property(root, RED_PROP_MSG, &len);
1054 r->info->lang = lost_get_property(root, PROP_LANG, &len);
1055 }
1056 /* return */
1057 res->redirect = r;
1058 res->category = REDIRECT;
1059 /* check the root element ... */
1060 /* unknown */
1061 } else {
1062 LM_ERR("root element is not valid: [%.*s]\n", ret.len, ret.s);
1063 res->category = OTHER;
1064 }
1065
1066 xmlFreeDoc(doc); /* clean up */
1067
1068 return res;
1069 }
1070
1071 /*
1072 * lost_check_HeldResponse(node)
1073 * does a quick check of HELD dereference response and returns ...
1074 * 0: neither location value nor reference found
1075 * 1: location reference found
1076 * 2: location value found
1077 * 3: location value and reference found
1078 * multiple occurrences are ignored
1079 */
1080 int lost_check_HeldResponse(xmlNodePtr node)
1081 {
1082 char *tmp = NULL;
1083
1084 int ret = 0; /* response error */
1085
1086 tmp = xmlNodeGetNodeContentByName(node, "location-info", NULL);
1087 if(tmp != NULL) {
1088 ret += HELD_RESPONSE_VALUE; /* LocByVal: civic or geodetic */
1089 }
1090 xmlFree(tmp); /* clean up */
1091
1092 tmp = xmlNodeGetNodeContentByName(node, "locationURI", NULL);
1093 if(tmp != NULL) {
1094 ret += HELD_RESPONSE_REFERENCE; /* LocByRef: reference */
1095 }
1096 xmlFree(tmp); /* clean up */
1097
1098 return ret;
1099 }