Browse code

presence_reginfo: Add option for aggregating presentities

Matteo Brancaleoni authored on 16/09/2022 14:04:40 • Henning Westerholt committed on 27/09/2022 14:11:32
Showing 6 changed files
... ...
@@ -8,6 +8,25 @@ include ../../Makefile.defs
8 8
 auto_gen=
9 9
 NAME=presence_reginfo.so
10 10
 
11
+ifeq ($(CROSS_COMPILE),)
12
+XML2CFG=$(shell which xml2-config)
13
+ifeq ($(XML2CFG),)
14
+XML2CFG=$(shell \
15
+	if pkg-config --exists libxml-2.0; then \
16
+		echo 'pkg-config libxml-2.0'; \
17
+	fi)
18
+endif
19
+endif
20
+
21
+ifneq ($(XML2CFG),)
22
+	DEFS += $(shell $(XML2CFG) --cflags )
23
+	LIBS += $(shell $(XML2CFG) --libs)
24
+else
25
+	DEFS+=-I$(LOCALBASE)/include/libxml2 \
26
+		-I$(LOCALBASE)/include
27
+	LIBS+=-L$(LOCALBASE)/lib -lxml2
28
+endif
29
+
11 30
 include ../../Makefile.modules
12 31
 
13 32
 
... ...
@@ -27,6 +27,9 @@
27 27
 #include "../../core/parser/parse_content.h"
28 28
 #include "../presence/event_list.h"
29 29
 #include "presence_reginfo.h"
30
+#include "notify_body.h"
31
+
32
+extern int pres_reginfo_aggregate_presentities;
30 33
 
31 34
 int reginfo_add_events(void)
32 35
 {
... ...
@@ -39,12 +42,22 @@ int reginfo_add_events(void)
39 42
 
40 43
 	event.content_type.s = "application/reginfo+xml";
41 44
 	event.content_type.len = 23;
42
-	event.default_expires= pres_reginfo_default_expires;
45
+	event.default_expires = pres_reginfo_default_expires;
43 46
 	event.type = PUBL_TYPE;
44 47
 	event.req_auth = 0;
45 48
 	event.evs_publ_handl = 0;
46 49
 
47
-	if (pres_add_event(&event) < 0) {
50
+
51
+	if(pres_reginfo_aggregate_presentities) {
52
+		/* aggregate XML body and free() fuction */
53
+		event.agg_nbody = reginfo_agg_nbody;
54
+		event.free_body = free_xml_body;
55
+		/* modify XML body for each watcher to set the correct "version" */
56
+		event.aux_body_processing = reginfo_body_setversion;
57
+		event.aux_free_body = free_xml_body;
58
+	}
59
+
60
+	if(pres_add_event(&event) < 0) {
48 61
 		LM_ERR("failed to add event \"reginfo\"\n");
49 62
 		return -1;
50 63
 	}
... ...
@@ -86,6 +86,27 @@
86 86
             </example>
87 87
         </section>
88 88
 
89
+				<section id="presence_reginfo.p.aggregate_presentities">
90
+							<title><varname>aggregate_presentities</varname> (int)</title>
91
+							<para>
92
+							Whether to aggregate in a single notify body all registration 
93
+							presentities. Useful to have all registrations on first NOTIFY
94
+							following initial SUBSCRIBE.
95
+							</para>
96
+							<para>
97
+							<emphasis>Default value is <quote>0</quote> (disabled).
98
+							</emphasis>
99
+							</para>
100
+							<example>
101
+							<title>Set <varname>aggregate_presentities</varname> parameter</title>
102
+							<programlisting format="linespecific">
103
+					...
104
+					modparam("presence_reginfo", "aggregate_presentities", 1)
105
+					...
106
+					</programlisting>
107
+							</example>
108
+					</section>
109
+
89 110
 	</section>
90 111
 
91 112
 
92 113
new file mode 100644
... ...
@@ -0,0 +1,314 @@
1
+/*
2
+ * presence_dialoginfo module
3
+ *
4
+ * Copyright (C) 2006 Voice Sistem S.R.L.
5
+ * Copyright (C) 2008 Klaus Darilion, IPCom
6
+ * Copyright (C) 2022 Matteo Brancaleoni, VoiSmart
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
+ *
24
+ */
25
+
26
+/*! \file
27
+ * \brief Kamailio Presence_reginfo :: Notify BODY handling
28
+ * \ingroup presence_reginfo
29
+ */
30
+
31
+#define MAX_INT_LEN 11 /* 2^32: 10 chars + 1 char sign */
32
+
33
+#include <string.h>
34
+#include <stdlib.h>
35
+#include <libxml/parser.h>
36
+
37
+#include "../../core/mem/mem.h"
38
+#include "../presence/hash.h"
39
+#include "../presence/presence.h"
40
+
41
+
42
+str *agregate_xmls(str *pres_user, str *pres_domain, str **body_array, int n);
43
+
44
+void free_xml_body(char *body)
45
+{
46
+	if(body == NULL)
47
+		return;
48
+
49
+	xmlFree(body);
50
+	body = NULL;
51
+}
52
+
53
+str *reginfo_agg_nbody(str *pres_user, str *pres_domain, str **body_array,
54
+		int n, int off_index)
55
+{
56
+	str *n_body = NULL;
57
+
58
+	LM_DBG("[pres_user]=%.*s [pres_domain]= %.*s, [n]=%d\n", pres_user->len,
59
+			pres_user->s, pres_domain->len, pres_domain->s, n);
60
+
61
+	if(body_array == NULL) {
62
+		return NULL;
63
+	}
64
+
65
+	n_body = agregate_xmls(pres_user, pres_domain, body_array, n);
66
+	LM_DBG("[n_body]=%p\n", n_body);
67
+	if(n_body) {
68
+		LM_DBG("[*n_body]=%.*s\n", n_body->len, n_body->s);
69
+	}
70
+	if(n_body == NULL && n != 0) {
71
+		LM_ERR("while aggregating body\n");
72
+	}
73
+
74
+	xmlCleanupParser();
75
+	xmlMemoryDump();
76
+
77
+	return n_body;
78
+}
79
+
80
+str *agregate_xmls(str *pres_user, str *pres_domain, str **body_array, int n)
81
+{
82
+	int i, j = 0;
83
+
84
+	str *body = NULL;
85
+
86
+	xmlDocPtr doc = NULL;
87
+	xmlNodePtr root_node = NULL;
88
+	xmlNsPtr namespace = NULL;
89
+	xmlNodePtr p_root = NULL;
90
+	xmlDocPtr *xml_array;
91
+	xmlNodePtr node = NULL;
92
+	xmlNodePtr next_node = NULL;
93
+
94
+	LM_DBG("[pres_user]=%.*s [pres_domain]= %.*s, [n]=%d\n", pres_user->len,
95
+			pres_user->s, pres_domain->len, pres_domain->s, n);
96
+
97
+	xml_array = (xmlDocPtr *)pkg_malloc(n * sizeof(xmlDocPtr));
98
+	if(xml_array == NULL) {
99
+		PKG_MEM_ERROR;
100
+		return NULL;
101
+	}
102
+	memset(xml_array, 0, n * sizeof(xmlDocPtr));
103
+
104
+	/* parse all the XML documents into xml_array[] */
105
+	for(i = 0; i < n; i++) {
106
+		if(body_array[i] == NULL)
107
+			continue;
108
+
109
+		xml_array[j] = NULL;
110
+		xml_array[j] = xmlParseMemory(body_array[i]->s, body_array[i]->len);
111
+		if(xml_array[j] == NULL) {
112
+			LM_ERR("while parsing xml body message\n");
113
+			goto error;
114
+		}
115
+		j++;
116
+	}
117
+
118
+	if(j == 0) /* no body */
119
+	{
120
+		LM_DBG("no body to be built\n");
121
+		goto error;
122
+	}
123
+
124
+	/* n: number of bodies in total */
125
+	/* j: number of useful bodies; created XML structures */
126
+	/* i: loop counter */
127
+
128
+	/* create the new NOTIFY body  */
129
+	doc = xmlNewDoc(BAD_CAST "1.0");
130
+	if(doc == 0) {
131
+		LM_ERR("unable to create xml document\n");
132
+		goto error;
133
+	}
134
+
135
+	root_node = xmlNewNode(NULL, BAD_CAST "reginfo");
136
+	if(root_node == 0) {
137
+		goto error;
138
+	}
139
+
140
+	xmlDocSetRootElement(doc, root_node);
141
+	namespace = xmlNewNs(
142
+			root_node, BAD_CAST "urn:ietf:params:xml:ns:reginfo", NULL);
143
+	if(!namespace) {
144
+		LM_ERR("creating namespace failed\n");
145
+	}
146
+	xmlSetNs(root_node, namespace);
147
+
148
+	/* The version must be increased for each new document and is a 32bit int.
149
+	 * As the version is different for each watcher, we can not set here the
150
+	 * correct value. Thus, we just put here a placeholder which will be
151
+	 * replaced by the correct value in the aux_body_processing callback.
152
+	 * Thus we have CPU intensive XML aggregation only once and can use
153
+	 * quick search&replace in the per-watcher aux_body_processing callback.
154
+	 * We use 11 chracters as an signed int (although RFC says unsigned int we
155
+	 * use signed int as presence module stores "version" in DB as
156
+	 * signed int) has max. 10 characters + 1 character for the sign
157
+	 */
158
+	xmlNewProp(root_node, BAD_CAST "version", BAD_CAST "00000000000");
159
+	xmlNewProp(root_node, BAD_CAST "state", BAD_CAST "full");
160
+
161
+	/* loop over all bodies and create the aggregated body */
162
+	for(i = 0; i < j; i++) {
163
+		// get the root reginfo element
164
+		p_root = xmlDocGetRootElement(xml_array[i]);
165
+		if(p_root == NULL) {
166
+			LM_ERR("the xml_tree root element is null\n");
167
+			goto error;
168
+		}
169
+
170
+		// get the children registration elements
171
+		if(p_root->children) {
172
+			// loop over registration elements
173
+			for(node = p_root->children; node; node = next_node) {
174
+				next_node = node->next;
175
+				if(node->type != XML_ELEMENT_NODE) {
176
+					continue;
177
+				}
178
+				LM_DBG("node type: Element, name: %s\n", node->name);
179
+
180
+				/* we do not copy the node, but unlink it and then add it ot the new node
181
+				 * this destroys the original document but we do not need it anyway.
182
+				 */
183
+				xmlUnlinkNode(node);
184
+				if(xmlAddChild(root_node, node) == NULL) {
185
+					xmlFreeNode(node);
186
+					LM_ERR("while adding child\n");
187
+					goto error;
188
+				}
189
+			} // end of loop over registration elements
190
+		}
191
+	} // end of loop over all bodies
192
+
193
+	// convert to string & cleanup
194
+	body = (str *)pkg_malloc(sizeof(str));
195
+	if(body == NULL) {
196
+		ERR_MEM(PKG_MEM_STR);
197
+	}
198
+
199
+	xmlDocDumpFormatMemory(doc, (xmlChar **)(void *)&body->s, &body->len, 1);
200
+
201
+	for(i = 0; i < j; i++) {
202
+		if(xml_array[i] != NULL)
203
+			xmlFreeDoc(xml_array[i]);
204
+	}
205
+	if(doc) {
206
+		xmlFreeDoc(doc);
207
+	}
208
+	if(xml_array)
209
+		pkg_free(xml_array);
210
+
211
+	xmlCleanupParser();
212
+	xmlMemoryDump();
213
+
214
+	return body;
215
+
216
+	// error handling, cleanup and return NULL
217
+error:
218
+	if(xml_array != NULL) {
219
+		for(i = 0; i < j; i++) {
220
+			if(xml_array[i] != NULL)
221
+				xmlFreeDoc(xml_array[i]);
222
+		}
223
+		pkg_free(xml_array);
224
+	}
225
+	if(body) {
226
+		pkg_free(body);
227
+	}
228
+	if(doc) {
229
+		xmlFreeDoc(doc);
230
+	}
231
+
232
+	return NULL;
233
+}
234
+
235
+str *reginfo_body_setversion(subs_t *subs, str *body)
236
+{
237
+	char *version_start = 0;
238
+	char version[MAX_INT_LEN + 2]; /* +2 becasue of trailing " and \0 */
239
+	int version_len;
240
+	str *aux_body = NULL;
241
+
242
+	if(!body) {
243
+		return NULL;
244
+	}
245
+
246
+	/* xmlDocDumpFormatMemory creates \0 terminated string */
247
+	/* version parameters starts at minimum at character 30 */
248
+	if(body->len < 37) {
249
+		LM_ERR("body string too short!\n");
250
+		return NULL;
251
+	}
252
+	version_start = strstr(body->s + 30, "version=");
253
+	if(!version_start) {
254
+		LM_ERR("version string not found!\n");
255
+		return NULL;
256
+	}
257
+	version_start += 9;
258
+
259
+	/* safety check for placeholder - if it is body not set by the module,
260
+	 * don't update the version */
261
+	if(strncmp(version_start, "00000000000\"", 12) != 0)
262
+		return NULL;
263
+
264
+	version_len = snprintf(version, MAX_INT_LEN + 2, "%d\"", subs->version);
265
+	if(version_len >= MAX_INT_LEN + 2) {
266
+		LM_ERR("failed to convert 'version' to string (truncated output)\n");
267
+		return NULL;
268
+	} else if(version_len < 0) {
269
+		LM_ERR("failed to convert 'version' to string (encoding error)\n");
270
+		return NULL;
271
+	}
272
+
273
+	aux_body = (str *)pkg_malloc(sizeof(str));
274
+	if(aux_body == NULL) {
275
+		PKG_MEM_ERROR_FMT("error allocating memory for aux body str\n");
276
+		return NULL;
277
+	}
278
+	memset(aux_body, 0, sizeof(str));
279
+	aux_body->s = (char *)pkg_malloc(body->len * sizeof(char));
280
+	if(aux_body->s == NULL) {
281
+		pkg_free(aux_body);
282
+		PKG_MEM_ERROR_FMT("error allocating memory for aux body buffer\n");
283
+		return NULL;
284
+	}
285
+	memcpy(aux_body->s, body->s, body->len);
286
+	aux_body->len = body->len;
287
+
288
+	/* again but on the copied str, no checks needed */
289
+	version_start = strstr(aux_body->s + 30, "version=");
290
+	version_start += 9;
291
+	/* Replace the placeholder 00000000000 with the version.
292
+	 * Put the padding behind the ""
293
+	 */
294
+	LM_DBG("replace version with \"%s\n", version);
295
+	memcpy(version_start, version, version_len);
296
+	memset(version_start + version_len, ' ', 12 - version_len);
297
+
298
+	xmlDocPtr doc =
299
+			xmlReadMemory(aux_body->s, aux_body->len, "noname.xml", NULL, 0);
300
+	if(doc == NULL) {
301
+		LM_ERR("error allocation xmldoc\n");
302
+		pkg_free(aux_body->s);
303
+		pkg_free(aux_body);
304
+		return NULL;
305
+	}
306
+	pkg_free(aux_body->s);
307
+	xmlDocDumpFormatMemory(
308
+			doc, (xmlChar **)(void *)&aux_body->s, &aux_body->len, 1);
309
+	xmlFreeDoc(doc);
310
+	xmlCleanupParser();
311
+	xmlMemoryDump();
312
+
313
+	return aux_body;
314
+}
0 315
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+/*
2
+ * presence_dialoginfo module
3
+ *
4
+ * Copyright (C) 2006 Voice Sistem S.R.L.
5
+ * Copyright (C) 2008 Klaus Darilion, IPCom
6
+ * Copyright (C) 2022 Matteo Brancaleoni, VoiSmart
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
+ *
24
+ */
25
+
26
+/*! \file
27
+ * \brief Kamailio presence reginfo  ::
28
+ * \ref notify_body.c
29
+ * \ingroup presence_reginfo
30
+ */
31
+
32
+
33
+#ifndef _NBODY_H_
34
+#define _NBODY_H_
35
+
36
+str *reginfo_agg_nbody(str *pres_user, str *pres_domain, str **body_array,
37
+		int n, int off_index);
38
+
39
+str *reginfo_body_setversion(subs_t *subs, str *body);
40
+
41
+void free_xml_body(char *body);
42
+
43
+#endif
... ...
@@ -47,11 +47,13 @@ static int mod_init(void);
47 47
 add_event_t pres_add_event;
48 48
 
49 49
 /* module parameters */
50
+int pres_reginfo_aggregate_presentities = 0;
50 51
 unsigned int pres_reginfo_default_expires = 3600;
51 52
 
52 53
 /* module exported paramaters */
53 54
 static param_export_t params[] = {
54 55
 	{ "default_expires", INT_PARAM, &pres_reginfo_default_expires },
56
+	{ "aggregate_presentities", INT_PARAM, &pres_reginfo_aggregate_presentities },
55 57
 	{0, 0, 0}
56 58
 };
57 59
 
... ...
@@ -89,6 +91,12 @@ static int mod_init(void)
89 91
 		return -1;
90 92
 	}
91 93
 
94
+	if (pres_reginfo_aggregate_presentities != 0
95
+			&& pres_reginfo_aggregate_presentities != 1) {
96
+		LM_ERR("invalid aggregate_presentities param value, should be 0 or 1\n");
97
+		return -1;
98
+	}
99
+
92 100
 	pres_add_event = pres.add_event;
93 101
 	if (pres_add_event == NULL) {
94 102
 		LM_ERR("could not import add_event\n");