Browse code

xcap_server: components of xcap uri available in cfg

- the user id is embedded in xcap uri and is needed to decide what is
the sip uri associated with it
- $xcapuri(name=>key) PV exported to config

Daniel-Constantin Mierla authored on 01/09/2010 10:49:01
Showing 3 changed files
... ...
@@ -30,15 +30,19 @@
30 30
 #include <libxml/xpathInternals.h>
31 31
 
32 32
 #include "../../dprint.h"
33
-#include "../../sr_module.h"
34 33
 #include "../../mem/mem.h"
35 34
 #include "../../parser/parse_param.h"
36 35
 #include "../../modules_k/xcap_client/xcap_callbacks.h"
37 36
 
38 37
 #include "xcap_misc.h"
39 38
 
39
+extern str xcaps_root;
40
+
40 41
 static param_t *_xcaps_xpath_ns_root = NULL;
41 42
 
43
+/**
44
+ * parse xcap uri
45
+ */
42 46
 int xcap_parse_uri(str *huri, str *xroot, xcap_uri_t *xuri)
43 47
 {
44 48
 	str s;
... ...
@@ -213,6 +217,7 @@ int xcap_parse_uri(str *huri, str *xroot, xcap_uri_t *xuri)
213 213
 		xuri->node.len = xuri->uri.s + xuri->uri.len - xuri->node.s;
214 214
 	}
215 215
 
216
+#if 0
216 217
 	LM_DBG("----- uri: [%.*s]\n", xuri->uri.len, xuri->uri.s);
217 218
 	LM_DBG("----- root: [%.*s]\n", xuri->root.len, xuri->root.s);
218 219
 	LM_DBG("----- auid: [%.*s] (%d)\n", xuri->auid.len, xuri->auid.s,
... ...
@@ -225,9 +230,13 @@ int xcap_parse_uri(str *huri, str *xroot, xcap_uri_t *xuri)
225 225
 	LM_DBG("----- rdoc: [%.*s]\n", xuri->rdoc.len, xuri->rdoc.s);
226 226
 	if(xuri->nss!=NULL)
227 227
 		LM_DBG("----- node: [%.*s]\n", xuri->node.len, xuri->node.s);
228
+#endif
228 229
 	return 0;
229 230
 }
230 231
 
232
+/**
233
+ * get content of xpath pointer
234
+ */
231 235
 int xcaps_xpath_get(str *inbuf, str *xpaths, str *outbuf)
232 236
 {
233 237
 	xmlDocPtr doc = NULL;
... ...
@@ -357,7 +366,9 @@ error:
357 357
 	return -1;
358 358
 }
359 359
 
360
-
360
+/**
361
+ * set content of xpath pointer
362
+ */
361 363
 int xcaps_xpath_set(str *inbuf, str *xpaths, str *val, str *outbuf)
362 364
 {
363 365
 	xmlDocPtr doc = NULL;
... ...
@@ -499,7 +510,9 @@ error:
499 499
 	return -1;
500 500
 }
501 501
 
502
-
502
+/**
503
+ * register extra xml name spaces
504
+ */
503 505
 void xcaps_xpath_register_ns(xmlXPathContextPtr xpathCtx)
504 506
 {
505 507
 	param_t *ns;
... ...
@@ -511,6 +524,9 @@ void xcaps_xpath_register_ns(xmlXPathContextPtr xpathCtx)
511 511
 	}
512 512
 }
513 513
 
514
+/**
515
+ * parse xml ns parameter
516
+ */
514 517
 int xcaps_xpath_ns_param(modparam_t type, void *val)
515 518
 {
516 519
 	char *p;
... ...
@@ -549,3 +565,228 @@ error:
549 549
 
550 550
 }
551 551
 
552
+/**
553
+ * xcapuri PV export
554
+ */
555
+typedef struct _pv_xcap_uri {
556
+	str name;
557
+	unsigned int id;
558
+	xcap_uri_t xuri;
559
+	struct _pv_xcap_uri *next;
560
+} pv_xcap_uri_t;
561
+
562
+typedef struct _pv_xcap_uri_spec {
563
+	str name;
564
+	str key;
565
+	int ktype;
566
+	pv_xcap_uri_t *xus;
567
+} pv_xcap_uri_spec_t;
568
+
569
+
570
+pv_xcap_uri_t *_pv_xcap_uri_root = NULL;
571
+
572
+/**
573
+ *
574
+ */
575
+pv_xcap_uri_t *pv_xcap_uri_get_struct(str *name)
576
+{
577
+	unsigned int id;
578
+	pv_xcap_uri_t *it;
579
+
580
+	id = get_hash1_raw(name->s, name->len);
581
+	it = _pv_xcap_uri_root;
582
+
583
+	while(it!=NULL)
584
+	{
585
+		if(id == it->id && name->len==it->name.len
586
+				&& strncmp(name->s, it->name.s, name->len)==0)
587
+		{
588
+			LM_DBG("uri found [%.*s]\n", name->len, name->s);
589
+			return it;
590
+		}
591
+		it = it->next;
592
+	}
593
+
594
+	it = (pv_xcap_uri_t*)pkg_malloc(sizeof(pv_xcap_uri_t));
595
+	if(it==NULL)
596
+	{
597
+		LM_ERR("no more pkg\n");
598
+		return NULL;
599
+	}
600
+	memset(it, 0, sizeof(pv_xcap_uri_t));
601
+
602
+	it->id = id;
603
+	it->name = *name;
604
+
605
+	it->next = _pv_xcap_uri_root;
606
+	_pv_xcap_uri_root = it;
607
+	return it;
608
+}
609
+
610
+
611
+/**
612
+ *
613
+ */
614
+int pv_parse_xcap_uri_name(pv_spec_p sp, str *in)
615
+{
616
+	pv_xcap_uri_spec_t *pxs = NULL;
617
+	char *p;
618
+
619
+	if(in->s==NULL || in->len<=0)
620
+		return -1;
621
+
622
+	pxs = (pv_xcap_uri_spec_t*)pkg_malloc(sizeof(pv_xcap_uri_spec_t));
623
+	if(pxs==NULL)
624
+		return -1;
625
+
626
+	memset(pxs, 0, sizeof(pv_xcap_uri_spec_t));
627
+
628
+	p = in->s;
629
+
630
+	while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
631
+		p++;
632
+	if(p>in->s+in->len || *p=='\0')
633
+		goto error;
634
+	pxs->name.s = p;
635
+	while(p < in->s + in->len)
636
+	{
637
+		if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
638
+			break;
639
+		p++;
640
+	}
641
+	if(p>in->s+in->len || *p=='\0')
642
+		goto error;
643
+	pxs->name.len = p - pxs->name.s;
644
+	if(*p!='=')
645
+	{
646
+		while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
647
+			p++;
648
+		if(p>in->s+in->len || *p=='\0' || *p!='=')
649
+			goto error;
650
+	}
651
+	p++;
652
+	if(*p!='>')
653
+		goto error;
654
+	p++;
655
+
656
+	pxs->key.len = in->len - (int)(p - in->s);
657
+	pxs->key.s = p;
658
+	LM_DBG("uri name [%.*s] - key [%.*s]\n", pxs->name.len, pxs->name.s,
659
+			pxs->key.len, pxs->key.s);
660
+	if(pxs->key.len==4 && strncmp(pxs->key.s, "data", 4)==0) {
661
+		pxs->ktype = 0;
662
+	} else if(pxs->key.len==3 && strncmp(pxs->key.s, "uri", 4)==0) {
663
+		pxs->ktype = 1;
664
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "root", 4)==0) {
665
+		pxs->ktype = 2;
666
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "auid", 4)==0) {
667
+		pxs->ktype = 3;
668
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "type", 4)==0) {
669
+		pxs->ktype = 4;
670
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "tree", 4)==0) {
671
+		pxs->ktype = 5;
672
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "xuid", 4)==0) {
673
+		pxs->ktype = 6;
674
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "file", 4)==0) {
675
+		pxs->ktype = 7;
676
+	} else if(pxs->key.len==4 && strncmp(pxs->key.s, "node", 4)==0) {
677
+		pxs->ktype = 8;
678
+	} else {
679
+		LM_ERR("unknown key type [%.*s]\n", in->len, in->s);
680
+		goto error;
681
+	}
682
+	pxs->xus = pv_xcap_uri_get_struct(&pxs->name);
683
+	sp->pvp.pvn.u.dname = (void*)pxs;
684
+	sp->pvp.pvn.type = PV_NAME_OTHER;
685
+	return 0;
686
+
687
+error:
688
+	if(pxs!=NULL)
689
+		pkg_free(pxs);
690
+	return -1;
691
+}
692
+
693
+/**
694
+ *
695
+ */
696
+int pv_set_xcap_uri(struct sip_msg* msg, pv_param_t *param,
697
+		int op, pv_value_t *val)
698
+{
699
+	pv_xcap_uri_spec_t *pxs = NULL;
700
+
701
+	pxs = (pv_xcap_uri_spec_t*)param->pvn.u.dname;
702
+	if(pxs->xus==NULL)
703
+		return -1;
704
+	if(!(val->flags&PV_VAL_STR))
705
+		return -1;
706
+	if(pxs->ktype!=0)
707
+		return -1;
708
+	/* set uri data */
709
+	if(xcap_parse_uri(&val->rs, &xcaps_root, &pxs->xus->xuri)<0)
710
+	{
711
+		LM_ERR("error setting xcap uri data [%.*s]\n",
712
+				val->rs.len, val->rs.s);
713
+		return -1;
714
+	}
715
+
716
+	return 0;
717
+}
718
+
719
+/**
720
+ *
721
+ */
722
+int pv_get_xcap_uri(struct sip_msg *msg,  pv_param_t *param,
723
+		pv_value_t *res)
724
+{
725
+	pv_xcap_uri_spec_t *pxs = NULL;
726
+
727
+	pxs = (pv_xcap_uri_spec_t*)param->pvn.u.dname;
728
+	if(pxs->xus==NULL)
729
+		return -1;
730
+
731
+	switch(pxs->ktype) {
732
+		case 0:
733
+		case 1:
734
+			/* get uri */
735
+			if(pxs->xus->xuri.uri.len>0)
736
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.uri);
737
+		break;
738
+		case 2:
739
+			/* get root */
740
+			if(pxs->xus->xuri.root.len>0)
741
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.root);
742
+		break;
743
+		case 3:
744
+			/* get auid */
745
+			if(pxs->xus->xuri.auid.len>0)
746
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.auid);
747
+		break;
748
+		case 4:
749
+			/* get type */
750
+			return pv_get_sintval(msg, param, res, pxs->xus->xuri.type);
751
+		break;
752
+		case 5:
753
+			/* get tree */
754
+			if(pxs->xus->xuri.tree.len>0)
755
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.tree);
756
+		break;
757
+		case 6:
758
+			/* get xuid */
759
+			if(pxs->xus->xuri.xuid.len>0)
760
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.xuid);
761
+		break;
762
+		case 7:
763
+			/* get file */
764
+			if(pxs->xus->xuri.file.len>0)
765
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.file);
766
+		break;
767
+		case 8:
768
+			/* get node */
769
+			if(pxs->xus->xuri.node.len>0)
770
+				return pv_get_strval(msg, param, res, &pxs->xus->xuri.node);
771
+		break;
772
+		default:
773
+			return pv_get_null(msg, param, res);
774
+	}
775
+	return pv_get_null(msg, param, res);
776
+}
... ...
@@ -26,6 +26,8 @@
26 26
 #define _XCAP_MISC_H_
27 27
 
28 28
 #include "../../str.h"
29
+#include "../../sr_module.h"
30
+#include "../../pvar.h"
29 31
 
30 32
 #define XCAP_MAX_URI_SIZE	127
31 33
 /* Node Selector Separator */
... ...
@@ -50,4 +52,10 @@ int xcap_parse_uri(str *huri, str *xroot, xcap_uri_t *xuri);
50 50
 int xcaps_xpath_set(str *inbuf, str *xpaths, str *val, str *outbuf);
51 51
 int xcaps_xpath_get(str *inbuf, str *xpaths, str *outbuf);
52 52
 
53
+int pv_get_xcap_uri(struct sip_msg *msg,  pv_param_t *param,
54
+		pv_value_t *res);
55
+int pv_set_xcap_uri(struct sip_msg* msg, pv_param_t *param,
56
+		int op, pv_value_t *val);
57
+int pv_parse_xcap_uri_name(pv_spec_p sp, str *in);
58
+
53 59
 #endif
... ...
@@ -75,9 +75,9 @@ int xcaps_generate_etag_hdr(str *etag);
75 75
 
76 76
 static str xcaps_db_table = str_init("xcap");
77 77
 static str xcaps_db_url = str_init(DEFAULT_DB_URL);
78
-static str xcaps_root = str_init("/xcap-root/");
79 78
 static int xcaps_init_time = 0;
80 79
 static int xcaps_etag_counter = 1;
80
+str xcaps_root = str_init("/xcap-root/");
81 81
 
82 82
 static str xcaps_buf = {0, 8192};
83 83
 #define XCAPS_ETAG_SIZE	128
... ...
@@ -100,6 +100,12 @@ db_func_t xcaps_dbf;
100 100
 /** SL API structure */
101 101
 sl_api_t slb;
102 102
 
103
+static pv_export_t mod_pvs[] = {
104
+	{ {"xcapuri", sizeof("xcapuri")-1}, PVT_OTHER, pv_get_xcap_uri,
105
+		pv_set_xcap_uri, pv_parse_xcap_uri_name, 0, 0, 0 },
106
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
107
+};
108
+
103 109
 static param_export_t params[] = {
104 110
 	{ "db_url",		STR_PARAM, &xcaps_db_url.s    },
105 111
 	{ "xcap_table",	STR_PARAM, &xcaps_db_table.s  },
... ...
@@ -128,7 +134,7 @@ struct module_exports exports= {
128 128
 	params,						/* exported parameters */
129 129
 	0,      					/* exported statistics */
130 130
 	0,							/* exported MI functions */
131
-	0,							/* exported pseudo-variables */
131
+	mod_pvs,					/* exported pseudo-variables */
132 132
 	0,							/* extra processes */
133 133
 	mod_init,					/* module initialization function */
134 134
 	0,							/* response handling function */