File: notify_body.c

package info (click to toggle)
opensips 2.2.2-3.1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 31,160 kB
  • sloc: ansic: 334,348; xml: 91,231; perl: 6,659; sh: 5,148; sql: 4,175; makefile: 3,167; yacc: 2,499; python: 1,197; cpp: 611; php: 573
file content (511 lines) | stat: -rw-r--r-- 13,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 * presence_dialoginfo module -
 *
 * Copyright (C) 2006 Voice Sistem S.R.L.
 * Copyright (C) 2008 Klaus Darilion, IPCom
 *
 * This file is part of opensips, a free SIP server.
 *
 * opensips is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * opensips is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * History:
 * --------
 *  2008-08-25  initial version (kd)
 */

#define MAX_INT_LEN 11 /* 2^32: 10 chars + 1 char sign */

#include <string.h>
#include <stdlib.h>
#include <libxml/parser.h>

#include "../../mem/mem.h"
#include "../presence/utils_func.h"
#include "../presence/hash.h"
#include "../presence/event_list.h"
#include "../presence/presence.h"
#include "../presence/presentity.h"
#include "presence_dialoginfo.h"
#include "notify_body.h"
#include "pidf.h"

str* agregate_xmls(str* pres_user, str* pres_domain, str** body_array, int n, int partial);
str* build_dialoginfo(str* pres_user, str* pres_domain);
extern int force_single_dialog;

static str* _build_empty_dialoginfo(const char* pres_uri_char, str* extra_hdrs);

#define VERSION_HOLDER "00000000000"

void free_xml_body(char* body)
{
	if(body)
		xmlFree(body);
}

/* Joins user and domain into "sip:USER@DOMAIN".
 * dst must fit at least MAX_URI_SIZE+1 characters! */
static inline int sipuri_cat(char* dst, const str* user, const str* domain) {
	if ((4 + user->len + 1 + domain->len) > MAX_URI_SIZE) {
	        LM_ERR("entity URI too long, maximum=%d\n", MAX_URI_SIZE);
		return -1;
	}
	memcpy(dst, "sip:", 4);
	memcpy(dst + 4, user->s, user->len);
	dst[user->len + 4] = '@';
	memcpy(dst + user->len + 5, domain->s, domain->len);
	dst[user->len + 5 + domain->len] = '\0';
	return 0;
}

str* dlginfo_agg_nbody(str* pres_user, str* pres_domain, str** body_array, int n, int off_index)
{
	str* n_body= NULL;
	char pres_uri_char[MAX_URI_SIZE+1];

	if (sipuri_cat(pres_uri_char, pres_user, pres_domain) != 0)
		return NULL;
	LM_DBG("[pres_uri] %s (%d), [n]=%d\n", pres_uri_char,
		pres_user->len + 5 + pres_domain->len, n);

	if(body_array == NULL)
		return _build_empty_dialoginfo(pres_uri_char, NULL);

	if (n == -2)
		n_body= agregate_xmls(pres_user, pres_domain, body_array, 1, 1);
	else
		n_body= agregate_xmls(pres_user, pres_domain, body_array, n, 0);

	LM_DBG("[n_body]=%p\n", n_body);
	if(n_body) {
		LM_DBG("[*n_body]=%.*s\n",
			n_body->len, n_body->s);
	}
	if(n_body== NULL && n!= 0)
	{
		LM_ERR("while aggregating body\n");
	}

	xmlCleanupParser();
	xmlMemoryDump();

	if (n_body== NULL)
		n_body = _build_empty_dialoginfo(pres_uri_char, NULL);
	return n_body;
}

str* agregate_xmls(str* pres_user, str* pres_domain, str** body_array, int n, int partial)
{
	int i, j= 0;

	xmlDocPtr  doc = NULL;
	xmlNodePtr root_node = NULL;
	xmlNsPtr   namespace = NULL;

	xmlNodePtr p_root= NULL;
	xmlDocPtr* xml_array ;
	xmlNodePtr node = NULL;
	char *state;
	int winner_priority = -1, priority ;
	xmlNodePtr winner_dialog_node = NULL ;
	str *body= NULL;
	char buf[MAX_URI_SIZE+1];

	LM_DBG("[pres_user]=%.*s [pres_domain]= %.*s, [n]=%d\n",
			pres_user->len, pres_user->s, pres_domain->len, pres_domain->s, n);

	xml_array = (xmlDocPtr*)pkg_malloc( n*sizeof(xmlDocPtr));
	if(xml_array== NULL)
	{
		LM_ERR("while allocating memory");
		return NULL;
	}
	memset(xml_array, 0, n*sizeof(xmlDocPtr)) ;

	/* parse all the XML documents */
	for(i=0; i<n; i++)
	{
		if(body_array[i] == NULL )
			continue;

		xml_array[j] = NULL;
		xml_array[j] = xmlParseMemory( body_array[i]->s, body_array[i]->len );

		/* LM_DBG("parsing XML body: [n]=%d, [i]=%d, [j]=%d xml_array[j]=%p\n", n, i, j, xml_array[j] ); */

		if( xml_array[j]== NULL)
		{
			LM_ERR("while parsing xml body message\n");
			goto error;
		}
		j++;

	}

	if(j== 0)  /* no body */
	{
		if(xml_array)
			pkg_free(xml_array);
		return NULL;
	}

	/* n: number of bodies in total */
	/* j: number of useful bodies; created XML structures */
	/* i: loop counter */
	/* LM_DBG("number of bodies in total [n]=%d, number of useful bodies [j]=%d\n", n, j ); */

	/* create the new NOTIFY body  */
	if (sipuri_cat(buf, pres_user, pres_domain) != 0)
		goto error;

    doc = xmlNewDoc(BAD_CAST "1.0");
    if(doc==0)
        return NULL;

    root_node = xmlNewNode(NULL, BAD_CAST "dialog-info");
    if(root_node==0)
        goto error;

    xmlDocSetRootElement(doc, root_node);
	namespace = xmlNewNs(root_node, BAD_CAST "urn:ietf:params:xml:ns:dialog-info", NULL);
	if (!namespace) {
		LM_ERR("creating namespace failed\n");
	}
	xmlSetNs(root_node, namespace);
	/* The version must be increased for each new document and is a 32bit int.
       As the version is different for each watcher, we can not set here the
       correct value. Thus, we just put here a placeholder which will be
	   replaced by the correct value in the aux_body_processing callback.
	   Thus we have CPU intensive XML aggregation only once and can use
	   quick search&replace in the per-watcher aux_body_processing callback.
	   We use 11 chracters as an signed int (although RFC says unsigned int we
	   use signed int as presence module stores "version" in DB as
	   signed int) has max. 10 characters + 1 character for the sign
	*/
    xmlNewProp(root_node, BAD_CAST "version", BAD_CAST VERSION_HOLDER);
    xmlNewProp(root_node, BAD_CAST "entity",  BAD_CAST buf);
	if (!partial)
		xmlNewProp(root_node, BAD_CAST "state",  BAD_CAST "full" );
	else
		xmlNewProp(root_node, BAD_CAST "state",  BAD_CAST "partial" );

	/* loop over all bodies and create the aggregated body */
	for(i=0; i<j; i++)
	{
		/* LM_DBG("[n]=%d, [i]=%d, [j]=%d xml_array[i]=%p\n", n, i, j, xml_array[j] ); */
		p_root= xmlDocGetRootElement(xml_array[i]);
			if(p_root ==NULL) {
				LM_ERR("while getting the xml_tree root element\n");
				goto error;
			}
			if (p_root->children) {
			for (node = p_root->children; node; node = node->next) {
				if (node->type == XML_ELEMENT_NODE) {
					LM_DBG("node type: Element, name: %s\n", node->name);
					/* we do not copy the node, but unlink it and then add it ot the new node
					 * this destroys the original document but we do not need it anyway.
					 * using "copy" instead of "unlink" would also copy the namespace which
					 * would then be declared redundant (libxml unfortunately can not remove
					 * namespaces)
					 */
					if (!force_single_dialog || (j==1)) {
						xmlUnlinkNode(node);
						if(xmlAddChild(root_node, node)== NULL) {
							LM_ERR("while adding child\n");
							goto error;
						}
					} else {
						/* try to put only the most important into the XML document
						 * order of importance: terminated->trying->proceeding->confirmed->early
						 */
						state = xmlNodeGetNodeContentByName(node, "state", NULL);
						if (state) {
							LM_DBG("state element content = %s\n", state);
							priority = get_dialog_state_priority(state);
							if (priority > winner_priority) {
								winner_priority = priority;
								LM_DBG("new winner priority = %s (%d)\n", state, winner_priority);
								winner_dialog_node = node;
							}
							xmlFree(state);
						}
					}
				}
			}
		}
	}

	if (force_single_dialog && (j!=1)) {
		xmlUnlinkNode(winner_dialog_node);
		if(xmlAddChild(root_node, winner_dialog_node)== NULL) {
			LM_ERR("while adding winner-child\n");
			goto error;
		}
	}

	body = (str*)pkg_malloc(sizeof(str));
	if(body == NULL) {
		ERR_MEM(PKG_MEM_STR);
	}

	xmlDocDumpMemory(doc,(xmlChar**)(void*)&body->s,
			&body->len);

  	for(i=0; i<j; i++)
	{
		if(xml_array[i]!=NULL)
			xmlFreeDoc( xml_array[i]);
	}
	if (doc)
		xmlFreeDoc(doc);
	if(xml_array!=NULL)
		pkg_free(xml_array);

	xmlCleanupParser();
    xmlMemoryDump();

	return body;

error:
	if(xml_array!=NULL)
	{
		for(i=0; i<=j; i++)
		{
			if(xml_array[i]!=NULL)
				xmlFreeDoc( xml_array[i]);
		}
		pkg_free(xml_array);
	}
	if(body)
		pkg_free(body);

	return NULL;
}


int get_dialog_state_priority(char *state) {
	if (strcasecmp(state,"terminated") == 0)
		return 0;
	if (strcasecmp(state,"trying") == 0)
		return 1;
	if (strcasecmp(state,"proceeding") == 0)
		return 2;
	if (strcasecmp(state,"confirmed") == 0)
		return 3;
	if (strcasecmp(state,"early") == 0)
		return 4;

	return 0;
}


str *dlginfo_body_setversion(subs_t *subs, str *body) {
	char *version_start=0;
	char version[MAX_INT_LEN + 2]; /* +2 becasue of trailing " and \0 */
	int version_len;

	if (!body) {
		return NULL;
	}

	LM_DBG("set version\n");
	/* xmlDocDumpFormatMemory creates \0 terminated string */
	/* version parameters starts at minimum at character 34 */
	if (body->len < 41) {
		LM_ERR("body string too short!\n");
		return NULL;
	}
	version_start = strstr(body->s + 34, "version=");
	if (!version_start) {
	    LM_ERR("version string not found!\n");
		return NULL;
	}
	version_start += 9;

	version_len = snprintf(version, MAX_INT_LEN + 2,"%d\"", subs->version);
	if (version_len >= MAX_INT_LEN + 2) {
		LM_ERR("failed to convert 'version' to string\n");
		return NULL;
	}
	/* Replace the placeholder 00000000000 with the version.
	 * Put the padding behind the ""
	 */
	LM_DBG("replace version with \"%s\n",version);
	memcpy(version_start, version, version_len);
	memset(version_start + version_len, ' ', MAX_INT_LEN + 2 - version_len);

	return NULL;
}

str* build_dialoginfo(str* pres_user, str* pres_domain)
{
	xmlDocPtr  doc = NULL;
	xmlNodePtr root_node = NULL;
	xmlNodePtr dialog_node = NULL;
	xmlNodePtr state_node = NULL;

	str *body= NULL;
	str pres_uri;
	char buf[MAX_URI_SIZE+1];

	if (sipuri_cat(buf, pres_user, pres_domain) != 0)
		return NULL;
	pres_uri.s = buf;
	pres_uri.len = 4 + pres_user->len + 1 + pres_domain->len;
	LM_DBG("[pres_uri] %.*s\n", pres_uri.len, pres_uri.s);

	if (pres_contains_presence(&pres_uri) < 0) {
		LM_DBG("No record exists in hash_table\n");
		goto error;
	}

	/* create the Publish body */
	doc = xmlNewDoc(BAD_CAST "1.0");
	if(doc==0)
		goto error;

	root_node = xmlNewNode(NULL, BAD_CAST "dialog-info");
	if(root_node==0)
		goto error;

	xmlDocSetRootElement(doc, root_node);

	xmlNewProp(root_node, BAD_CAST "xmlns",
			BAD_CAST "urn:ietf:params:xml:ns:dialog-info");
	/* we set the version to 0 but it should be set to the correct value
       in the pua module */
	xmlNewProp(root_node, BAD_CAST "version", BAD_CAST  VERSION_HOLDER);
	xmlNewProp(root_node, BAD_CAST "state",   BAD_CAST "partial" );
	xmlNewProp(root_node, BAD_CAST "entity",  BAD_CAST buf);
	/* dialog tag */
	dialog_node =xmlNewChild(root_node, NULL, BAD_CAST "dialog", NULL) ;
	if( dialog_node ==NULL)
	{
		LM_ERR("while adding child [dialog]\n");
		goto error;
	}

	/* reuse buf for user-part only */
	memcpy(buf, pres_user->s, pres_user->len);
	buf[pres_user->len] = '\0';

	xmlNewProp(dialog_node, BAD_CAST "id", BAD_CAST buf);

	/* state tag */
	state_node = xmlNewChild(dialog_node, NULL, BAD_CAST "state", BAD_CAST "terminated");
	if( state_node ==NULL)
	{
		LM_ERR("while adding child [state]\n");
		goto error;
	}
	/* create the body */
	body = (str*)pkg_malloc(sizeof(str));
	if(body == NULL)
	{
		LM_ERR("while allocating memory\n");
		goto error;
	}
	memset(body, 0, sizeof(str));

	xmlDocDumpMemory(doc,(unsigned char**)(void*)&body->s,&body->len);

	LM_DBG("new_body:\n%.*s\n",body->len, body->s);
	/*free the document */
	xmlFreeDoc(doc);
	xmlCleanupParser();
	return body;
error:
	if(body)
	{
		if(body->s)
			xmlFree(body->s);
		pkg_free(body);
	}
	if(doc)
		xmlFreeDoc(doc);
	return NULL;
}

static str* _build_empty_dialoginfo(const char* pres_uri_char, str* extra_hdrs)
{
	str* nbody= 0;
	xmlDocPtr doc = NULL;
	xmlNodePtr node;

	nbody= (str*) pkg_malloc(sizeof(str));
	if(nbody== NULL)
	{
		LM_ERR("No more memory\n");
		return 0;
	}

	doc = xmlNewDoc(BAD_CAST "1.0");
	if(doc == NULL)
	{
		LM_ERR("Failed to create new xml document\n");
		goto error;
	}

	node = xmlNewNode(0, BAD_CAST "dialog-info");
	if(node == NULL)
	{
		LM_ERR("Failed to create new xml node\n");
		goto error;
	}
	xmlDocSetRootElement(doc, node);
	xmlNewProp(node, BAD_CAST "xmlns",
			BAD_CAST "urn:ietf:params:xml:ns:dialog-info");
	xmlNewProp(node, BAD_CAST "version", BAD_CAST VERSION_HOLDER);
	xmlNewProp(node, BAD_CAST "state",   BAD_CAST "full");

	xmlNewProp(node, BAD_CAST "entity", BAD_CAST pres_uri_char);

	xmlDocDumpMemory(doc,(xmlChar**)(void*)&nbody->s,
		&nbody->len);

	xmlFreeDoc(doc);
	xmlCleanupParser();
	xmlMemoryDump();

	return nbody;
error:
	if(doc)
		xmlFreeDoc(doc);
	if(nbody)
		pkg_free(nbody);
	return 0;
}

str* build_empty_dialoginfo(str* pres_uri, str* extra_hdrs)
{
	char* pres_uri_char;
	str* ret;

	pres_uri_char = (char*)pkg_malloc(pres_uri->len + 1);
	if(pres_uri_char == NULL)
	{
		LM_ERR("No more memory\n");
		return NULL;
	}
	memcpy(pres_uri_char, pres_uri->s, pres_uri->len);
	pres_uri_char[pres_uri->len] = '\0';

	/* do the call with a null-terminated pres_uri */
	ret = _build_empty_dialoginfo(pres_uri_char, extra_hdrs);
	
	pkg_free(pres_uri_char);

	return ret;
}