Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,286 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser 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
+#include <stdio.h>
27
+#include <string.h>
28
+#include <time.h>
29
+
30
+#include <xcap/rls_services_parser.h>
31
+#include <xcap/xml_utils.h>
32
+
33
+#include <libxml/parser.h>
34
+#include <libxml/tree.h>
35
+#include <cds/sstr.h>
36
+#include <cds/logger.h>
37
+
38
+static char rls_namespace[] = "urn:ietf:params:xml:ns:rls-services";
39
+/*
40
+static int read_entry(xmlNode *entry_node, entry_t **dst)
41
+{
42
+	xmlAttr *a;
43
+	const char *a_val;
44
+	
45
+	/ * allocate memory and prepare empty node * /
46
+	if (!dst) return -1;
47
+	*dst = (entry_t*)cds_malloc(sizeof(entry_t));
48
+	if (!(*dst)) return -2;
49
+	memset(*dst, 0, sizeof(entry_t));
50
+
51
+	/ * get attributes * /
52
+	a = find_attr(entry_node->properties, "uri");
53
+	if (a) {
54
+		a_val = get_attr_value(a);
55
+		if (a_val) (*dst)->uri = zt_strdup(a_val);
56
+	}
57
+	return 0;
58
+}*/
59
+
60
+static int read_package(xmlNode *n, package_t **dst)
61
+{
62
+	const char *name;
63
+	if (!dst) return -1;
64
+	
65
+	*dst = (package_t*)cds_malloc(sizeof(package_t));
66
+	if (!(*dst)) return -2;
67
+	memset(*dst, 0, sizeof(package_t));
68
+
69
+	name = get_node_value(n);
70
+	if (name) (*dst)->name = zt_strdup(name);
71
+	return 0;
72
+}
73
+
74
+static int read_packages(xmlNode *list_node, packages_t **dst)
75
+{
76
+	int res = 0;
77
+	xmlNode *n;
78
+	package_t *p, *last;
79
+	
80
+	/* allocate memory and prepare empty node */
81
+	if (!dst) return -1;
82
+	*dst = (packages_t*)cds_malloc(sizeof(packages_t));
83
+	if (!(*dst)) return -2;
84
+	memset(*dst, 0, sizeof(packages_t));
85
+
86
+	/* read packages */
87
+	n = list_node->children;
88
+	last = NULL;
89
+	while (n) {
90
+		if (n->type == XML_ELEMENT_NODE) {
91
+			if (cmp_node(n, "package", rls_namespace) >= 0) {
92
+				res = read_package(n, &p);
93
+				if ((res == 0) && p) {
94
+					SEQUENCE_ADD((*dst)->package, last, p);
95
+				}
96
+			}
97
+		}
98
+		n = n->next;
99
+	}
100
+	
101
+	return 0;
102
+}
103
+
104
+int read_service(xmlNode *list_node, service_t **dst)
105
+{
106
+	int res = 0;
107
+	xmlAttr *a;
108
+	const char *a_val;
109
+	xmlNode *n;
110
+	int first_node;
111
+
112
+	DEBUG_LOG("read_service(): called\n");
113
+	/* allocate memory and prepare empty node */
114
+	if (!dst) return -1;
115
+	*dst = (service_t*)cds_malloc(sizeof(service_t));
116
+	if (!(*dst)) return -2;
117
+	memset(*dst, 0, sizeof(service_t));
118
+
119
+	/* get attributes */
120
+	a = find_attr(list_node->properties, "uri");
121
+	if (a) {
122
+		a_val = get_attr_value(a);
123
+		if (a_val) (*dst)->uri = zt_strdup(a_val);
124
+	}
125
+
126
+	/* read child nodes */
127
+	n = list_node->children;
128
+	first_node = 1;
129
+	while (n) {
130
+		if (n->type == XML_ELEMENT_NODE) {
131
+			if (first_node) {
132
+				/* element must be list or resource-list */
133
+				if (cmp_node(n, "list", rls_namespace) >= 0) {
134
+					res = read_list(n, &(*dst)->content.list, 0);
135
+					if ( (res == 0) && ((*dst)->content.list) ) {
136
+						(*dst)->content_type = stc_list;
137
+					}
138
+					else return -1;
139
+				}
140
+				else if (cmp_node(n, "resource-list", rls_namespace) >= 0) {
141
+					a_val = get_node_value(n);
142
+					if (a_val)
143
+						(*dst)->content.resource_list = zt_strdup(a_val);
144
+					else
145
+						(*dst)->content.resource_list = NULL;
146
+					(*dst)->content_type = stc_resource_list;
147
+				}
148
+				else return -1;
149
+
150
+				first_node = 0;
151
+			}
152
+			else { /* packages node */
153
+				if (cmp_node(n, "packages", rls_namespace) >= 0) {
154
+					res = read_packages(n, &(*dst)->packages);
155
+				}
156
+				break;
157
+			}
158
+		}
159
+		n = n->next;
160
+	}
161
+	
162
+	return 0;
163
+}
164
+
165
+static int read_rls_services(xmlNode *root, rls_services_t **dst)
166
+{
167
+	/* xmlAttr *a; */
168
+	xmlNode *n;
169
+	service_t *l, *last_l;
170
+	int res = 0;
171
+	
172
+	if (!root) return -1;
173
+	if (!dst) return -1;
174
+	
175
+	if (cmp_node(root, "rls-services", rls_namespace) < 0) {
176
+		ERROR_LOG("document is not a rls-services\n");
177
+		return -1;
178
+	}
179
+
180
+	*dst = (rls_services_t*)cds_malloc(sizeof(rls_services_t));
181
+	if (!(*dst)) return -2;
182
+	(*dst)->rls_services = NULL;
183
+	
184
+	last_l = NULL;
185
+	n = root->children;
186
+	while (n) {
187
+		if (n->type == XML_ELEMENT_NODE) {
188
+			if (cmp_node(n, "service", rls_namespace) >= 0) {
189
+				res = read_service(n, &l);
190
+				if (res == 0) {
191
+					if (l) SEQUENCE_ADD((*dst)->rls_services, last_l, l);
192
+				}
193
+				else break;
194
+			}
195
+		}
196
+		n = n->next;
197
+	}
198
+
199
+	return res;
200
+}
201
+
202
+int parse_rls_services_xml(const char *data, int data_len, rls_services_t **dst)
203
+{
204
+	int res = 0;
205
+	xmlDocPtr doc; /* the resulting document tree */
206
+
207
+	doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
208
+	if (doc == NULL) {
209
+		ERROR_LOG("can't parse document\n");
210
+		return -1;
211
+	}
212
+	
213
+	res = read_rls_services(xmlDocGetRootElement(doc), dst);
214
+
215
+	xmlFreeDoc(doc);
216
+	return res;
217
+}
218
+
219
+int parse_service(const char *data, int data_len, service_t **dst)
220
+{
221
+	int res = 0;
222
+	xmlDocPtr doc; /* the resulting document tree */
223
+
224
+	doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
225
+	if (doc == NULL) {
226
+		ERROR_LOG("can't parse document\n");
227
+		return -1;
228
+	}
229
+	
230
+	res = read_service(xmlDocGetRootElement(doc), dst);
231
+
232
+	xmlFreeDoc(doc);
233
+	return res;
234
+}
235
+
236
+static void free_package(package_t *p)
237
+{
238
+	if (!p) return;
239
+	if (p->name) cds_free(p->name);
240
+	cds_free(p);
241
+}
242
+
243
+static void free_packages(packages_t *p)
244
+{
245
+	package_t *e, *f;
246
+	if (!p) return;
247
+	
248
+	e = SEQUENCE_FIRST(p->package);
249
+	while (e) {
250
+		f = SEQUENCE_NEXT(e);
251
+		free_package(e);
252
+		e = f;
253
+	}
254
+	cds_free(p);
255
+}
256
+
257
+void free_service(service_t *s)
258
+{
259
+	if (!s) return;
260
+	
261
+	if (s->uri) cds_free(s->uri);
262
+
263
+	switch (s->content_type) {
264
+		case stc_list: free_list(s->content.list); break;
265
+		case stc_resource_list: cds_free(s->content.resource_list); break;
266
+	}
267
+	
268
+	free_packages(s->packages);
269
+
270
+	cds_free(s);
271
+}
272
+
273
+void free_rls_services(rls_services_t *rls)
274
+{
275
+	service_t *e, *f;
276
+	if (!rls) return;
277
+	
278
+	e = SEQUENCE_FIRST(rls->rls_services);
279
+	while (e) {
280
+		f = SEQUENCE_NEXT(e);
281
+		free_service(e);
282
+		e = f;
283
+	}
284
+	cds_free(rls);
285
+}
286
+