Debian Codegraph @

src/modules/lost/pidf.c

raw symbols SCIP
1 /*
2 * $Id: pidf.c 1953 2007-04-04 08:50:33Z anca_vamanu $
3 *
4 * presence module - presence server implementation
5 *
6 * Copyright (C) 2006 Voice Sistem S.R.L.
7 *
8 * This file is part of Kamailio, a free SIP server.
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 *
12 * Kamailio is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version
16 *
17 * Kamailio is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 */
27
28 /*! \file
29 * \brief Kamailio lost :: PIDF handling
30 * \ingroup lost
31 */
32
33
34 /**
35 * make strptime available
36 * use 600 for 'Single UNIX Specification, Version 3'
37 * _XOPEN_SOURCE creates conflict in header definitions in Solaris
38 */
39 #ifndef __OS_solaris
40 #define _XOPEN_SOURCE 600 /* glibc2 on linux, bsd */
41 #define _BSD_SOURCE \
42 1 /* needed on linux to "fix" the effect
43 of the above define on
44 features.h/unistd.h syscall() */
45 #define _DEFAULT_SOURCE 1 /* _BSD_SOURCE is deprecated */
46 #define _DARWIN_C_SOURCE 1
47 #else
48 #define _XOPEN_SOURCE_EXTENDED 1 /* solaris */
49 #endif
50
51 #include <time.h>
52
53 #undef _XOPEN_SOURCE
54 #undef _XOPEN_SOURCE_EXTENDED
55
56 #include <string.h>
57 #include <stdlib.h>
58 #include <libxml/parser.h>
59
60 #include "../../core/mem/mem.h"
61 #include "../../core/dprint.h"
62
63 #include "pidf.h"
64
65 xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
66 {
67 xmlAttrPtr attr = node->properties;
68 while(attr) {
69 if(xmlStrcasecmp(attr->name, (unsigned char *)name) == 0)
70 return attr;
71 attr = attr->next;
72 }
73 return NULL;
74 }
75
76 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
77 {
78 xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
79 if(attr)
80 return (char *)xmlNodeGetContent(attr->children);
81 else
82 return NULL;
83 }
84
85 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
86 {
87 xmlNodePtr cur = node->children;
88 while(cur) {
89 if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0)
90 return cur;
91 cur = cur->next;
92 }
93 return NULL;
94 }
95
96 xmlNodePtr xmlNodeGetNodeByName(
97 xmlNodePtr node, const char *name, const char *ns)
98 {
99 xmlNodePtr cur = node;
100 while(cur) {
101 xmlNodePtr match = NULL;
102 if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) {
103 if(!ns
104 || (cur->ns
105 && xmlStrcasecmp(
106 cur->ns->prefix, (unsigned char *)ns)
107 == 0))
108 return cur;
109 }
110 match = xmlNodeGetNodeByName(cur->children, name, ns);
111 if(match)
112 return match;
113 cur = cur->next;
114 }
115 return NULL;
116 }
117
118 char *xmlNodeGetNodeContentByName(
119 xmlNodePtr root, const char *name, const char *ns)
120 {
121 xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
122 if(node)
123 return (char *)xmlNodeGetContent(node->children);
124 else
125 return NULL;
126 }
127
128 xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
129 {
130 xmlNodePtr cur = doc->children;
131 if(cur)
132 return xmlNodeGetNodeByName(cur, name, ns);
133 else
134 return NULL;
135 }
136
137 char *xmlDocGetNodeContentByName(
138 xmlDocPtr doc, const char *name, const char *ns)
139 {
140 xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
141 if(node)
142 return (char *)xmlNodeGetContent(node->children);
143 else
144 return NULL;
145 }
146
147 xmlXPathObjectPtr xmlGetNodeSet(xmlDocPtr doc, xmlChar *xpath, xmlChar *ns)
148 {
149
150 xmlXPathContextPtr context = NULL;
151 xmlXPathObjectPtr result = NULL;
152
153 context = xmlXPathNewContext(doc);
154 if(context == NULL) {
155 return NULL;
156 }
157
158 if((ns != NULL) && (xmlRegisterNamespaces(context, ns) < 0)) {
159 xmlXPathFreeContext(context);
160 return NULL;
161 }
162
163 result = xmlXPathEvalExpression(xpath, context);
164 xmlXPathFreeContext(context);
165
166 if(result == NULL) {
167 LM_ERR("xmlXPathEvalExpression() failed\n");
168 return NULL;
169 }
170 if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
171 xmlXPathFreeObject(result);
172 LM_DBG("xmlXPathEvalExpression() returned no result\n");
173 return NULL;
174 }
175
176 return result;
177 }
178
179 int xmlRegisterNamespaces(xmlXPathContextPtr context, const xmlChar *ns)
180 {
181 xmlChar *nsListDup;
182 xmlChar *prefix;
183 xmlChar *href;
184 xmlChar *next;
185
186 nsListDup = xmlStrdup(ns);
187 if(nsListDup == NULL) {
188 return -1;
189 }
190
191 next = nsListDup;
192 while(next != NULL) {
193 /* skip spaces */
194 while((*next) == ' ')
195 next++;
196 if((*next) == '\0')
197 break;
198
199 /* find prefix */
200 prefix = next;
201 next = (xmlChar *)xmlStrchr(next, '=');
202 if(next == NULL) {
203 xmlFree(nsListDup);
204 return -1;
205 }
206 *(next++) = '\0';
207
208 /* find href */
209 href = next;
210 next = (xmlChar *)xmlStrchr(next, ' ');
211 if(next != NULL) {
212 *(next++) = '\0';
213 }
214
215 /* register namespace */
216 if(xmlXPathRegisterNs(context, prefix, href) != 0) {
217 xmlFree(nsListDup);
218 return -1;
219 }
220 }
221
222 xmlFree(nsListDup);
223 return 0;
224 }