Browse code

Merge commit 'origin/daniel/pv' into pvar

* commit 'origin/daniel/pv':
PV get null helper added
updated pvar.h file according to previous commit
pseudo-variable engine api

Andrei Pelinescu-Onciul authored on 20/11/2008 17:01:12
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,965 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2001-2003 FhG Fokus
4
+ *
5
+ * This file is part of SIP-Router, a free SIP server.
6
+ *
7
+ * SIP-Router is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * SIP-Router is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License 
18
+ * along with this program; if not, write to the Free Software 
19
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
+ */
21
+
22
+/*!
23
+ * \file
24
+ * \brief PV API specification
25
+ */
26
+
27
+#include <stdio.h>
28
+#include <string.h>
29
+#include <sys/types.h>
30
+#include <unistd.h>
31
+
32
+#include "mem/mem.h"
33
+#include "ut.h"
34
+#include "dprint.h"
35
+#include "hashes.h"
36
+#include "pvar.h"
37
+
38
+#define is_in_str(p, in) (p<in->s+in->len && *p)
39
+
40
+#define PV_TABLE_SIZE	16
41
+#define TR_TABLE_SIZE	2
42
+
43
+
44
+void tr_destroy(trans_t *t);
45
+void tr_free(trans_t *t);
46
+void tr_param_free(tr_param_t *tp);
47
+
48
+typedef struct _pv_item
49
+{
50
+	pv_export_t pve;
51
+	unsigned int pvid;
52
+	struct _pv_item *next;
53
+} pv_item_t, *pv_item_p;
54
+
55
+static pv_item_t* _pv_table[PV_TABLE_SIZE];
56
+static int _pv_table_set = 0;
57
+
58
+
59
+/**
60
+ *
61
+ */
62
+void pv_init_table()
63
+{
64
+	memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
65
+	_pv_table_set = 1;
66
+}
67
+
68
+/**
69
+ *
70
+ */
71
+static int is_pv_valid_char(char c)
72
+{
73
+	if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
74
+			|| (c=='_') || (c=='.'))
75
+		return 1;
76
+	return 0;
77
+}
78
+
79
+/**
80
+ *
81
+ */
82
+int pv_table_add(pv_export_t *e)
83
+{
84
+	char *p;
85
+	str  *in;
86
+	pv_item_t *pvi = NULL;
87
+	pv_item_t *pvj = NULL;
88
+	pv_item_t *pvn = NULL;
89
+	int found;
90
+	int pvid;
91
+
92
+	if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
93
+	{
94
+		LM_ERR("invalid parameters\n");
95
+		return -1;
96
+	}
97
+	
98
+	if(_pv_table_set==0)
99
+	{
100
+		LM_DBG("PV table not initialized, doing it now\n");
101
+		pv_init_table();
102
+	}
103
+	in = &(e->name);
104
+	p = in->s;	
105
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
106
+		p++;
107
+	if(is_in_str(p,in))
108
+	{
109
+		LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
110
+		return -1;
111
+	}
112
+	found = 0;
113
+	pvid = get_hash1_raw(in->s, in->len);
114
+
115
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
116
+	while(pvi)
117
+	{
118
+		if(pvi->pvid > pvid)
119
+			break;
120
+		if(pvi->pve.name.len > in->len)
121
+			break;
122
+		if(pvi->pve.name.len==in->len)
123
+		{
124
+			found = strncmp(pvi->pve.name.s, in->s, in->len);
125
+			if(found>0)
126
+				break;
127
+			if(found==0)
128
+			{
129
+				LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
130
+				return -1;
131
+			}
132
+		}
133
+		pvj = pvi;
134
+		pvi = pvi->next;
135
+	}
136
+
137
+	pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
138
+	if(pvn==0)
139
+	{
140
+		LM_ERR("no more memory\n");
141
+		return -1;
142
+	}
143
+	memset(pvn, 0, sizeof(pv_item_t));
144
+	memcpy(&(pvn->pve), e, sizeof(pv_export_t));
145
+	pvn->pvid = pvid;
146
+
147
+	if(pvj==0)
148
+	{
149
+		pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
150
+		_pv_table[pvid%PV_TABLE_SIZE] = pvn;
151
+		goto done;
152
+	}
153
+	pvn->next = pvj->next;
154
+	pvj->next = pvn;
155
+
156
+done:
157
+	return 0;
158
+}
159
+
160
+/**
161
+ *
162
+ */
163
+int register_pvars_mod(char *mod_name, pv_export_t *items)
164
+{
165
+	int ret;
166
+	int i;
167
+
168
+	if (items==0)
169
+		return 0;
170
+
171
+	for ( i=0 ; items[i].name.s ; i++ ) {
172
+		ret = pv_table_add(&items[i]);
173
+		if (ret!=0) {
174
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
175
+					items[i].name.len, items[i].name.s, mod_name);
176
+		}
177
+	}
178
+	return 0;
179
+}
180
+
181
+/**
182
+ *
183
+ */
184
+int pv_table_free(void)
185
+{
186
+	pv_item_p xe;
187
+	pv_item_p xe1;
188
+	int i;
189
+
190
+	for(i=0; i<PV_TABLE_SIZE; i++)
191
+	{
192
+		xe = _pv_table[i];
193
+		while(xe!=0)
194
+		{
195
+			xe1 = xe;
196
+			xe = xe->next;
197
+			pkg_free(xe1);
198
+		}
199
+		memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
200
+		_pv_table_set = 0;
201
+	}
202
+	
203
+	return 0;
204
+}
205
+
206
+/********** helper functions ********/
207
+/**
208
+ * convert unsigned int to pv_value_t
209
+ */
210
+int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
211
+		pv_value_t *res, unsigned int uival)
212
+{
213
+	int l = 0;
214
+	char *ch = NULL;
215
+
216
+	if(res==NULL)
217
+		return -1;
218
+
219
+	ch = int2str(uival, &l);
220
+	res->rs.s = ch;
221
+	res->rs.len = l;
222
+
223
+	res->ri = (int)uival;
224
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
225
+	return 0;
226
+}
227
+
228
+/**
229
+ * convert signed int to pv_value_t
230
+ */
231
+int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
232
+		pv_value_t *res, int sival)
233
+{
234
+	int l = 0;
235
+	char *ch = NULL;
236
+
237
+	if(res==NULL)
238
+		return -1;
239
+
240
+	ch = int2str(sival, &l);
241
+	res->rs.s = ch;
242
+	res->rs.len = l;
243
+
244
+	res->ri = sival;
245
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
246
+	return 0;
247
+}
248
+
249
+/**
250
+ * convert str to pv_value_t
251
+ */
252
+int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
253
+		pv_value_t *res, str *sval)
254
+{
255
+	if(res==NULL)
256
+		return -1;
257
+
258
+	res->rs = *sval;
259
+	res->flags = PV_VAL_STR;
260
+	return 0;
261
+}
262
+
263
+/**
264
+ * convert str-int to pv_value_t (type is str)
265
+ */
266
+int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
267
+		pv_value_t *res, str *sval, int ival)
268
+{
269
+	if(res==NULL)
270
+		return -1;
271
+
272
+	res->rs = *sval;
273
+	res->ri = ival;
274
+	res->flags = PV_VAL_STR|PV_VAL_INT;
275
+	return 0;
276
+}
277
+
278
+/**
279
+ * convert int-str to pv_value_t (type is int)
280
+ */
281
+int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
282
+		pv_value_t *res, int ival, str *sval)
283
+{
284
+	if(res==NULL)
285
+		return -1;
286
+
287
+	res->rs = *sval;
288
+	res->ri = ival;
289
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
290
+	return 0;
291
+}
292
+
293
+/*** ============================= ***/
294
+static str pv_str_marker = { PV_MARKER_STR, 1 };
295
+static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
296
+		pv_value_t *res)
297
+{
298
+	return pv_get_strintval(msg, param, res, &pv_str_marker,
299
+			(int)pv_str_marker.s[0]);
300
+}
301
+
302
+static str pv_str_empty  = { "", 0 };
303
+int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
304
+{
305
+	if(res==NULL)
306
+		return -1;
307
+	
308
+	res->rs = pv_str_empty;
309
+	res->ri = 0;
310
+	res->flags = PV_VAL_NULL;
311
+	return 0;
312
+}
313
+
314
+pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
315
+{
316
+	pv_item_t *pvi;
317
+	unsigned int pvid;
318
+
319
+	if(pvname==0 || e==0)
320
+	{
321
+		LM_ERR("bad parameters\n");
322
+		return NULL;
323
+	}
324
+
325
+	/* search in PV table */
326
+	pvid = get_hash1_raw(pvname->s, pvname->len);
327
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
328
+	while(pvi)
329
+	{
330
+		if(pvi->pvid > pvid)
331
+			break;
332
+		if(pvi->pve.name.len > pvname->len)
333
+			break;
334
+
335
+		if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
336
+			&& memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
337
+		{
338
+			/*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
339
+					_pv_names_table[i].type);*/
340
+			/* copy data from table to spec */
341
+			e->type = pvi->pve.type;
342
+			e->getf = pvi->pve.getf;
343
+			e->setf = pvi->pve.setf;
344
+			return &(pvi->pve);
345
+		}
346
+		pvi = pvi->next;
347
+	}
348
+
349
+	return NULL;
350
+}
351
+
352
+char* pv_parse_spec(str *in, pv_spec_p e)
353
+{
354
+	char *p;
355
+	str s;
356
+	str pvname;
357
+	int pvstate;
358
+	trans_t *tr = NULL;
359
+	pv_export_t *pte = NULL;
360
+	int n=0;
361
+
362
+	if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
363
+	{
364
+		LM_ERR("bad parameters\n");
365
+		return NULL;
366
+	}
367
+	
368
+	/* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
369
+	tr = 0;
370
+	pvstate = 0;
371
+	memset(e, 0, sizeof(pv_spec_t));
372
+	p = in->s;
373
+	p++;
374
+	if(*p==PV_LNBRACKET)
375
+	{
376
+		p++;
377
+		pvstate = 1;
378
+	}
379
+	pvname.s = p;
380
+	if(*p == PV_MARKER) {
381
+		p++;
382
+		if(pvstate==1)
383
+		{
384
+			if(*p!=PV_RNBRACKET)
385
+				goto error;
386
+			p++;
387
+		}
388
+		e->getf = pv_get_marker;
389
+		e->type = PVT_MARKER;
390
+		pvname.len = 1;
391
+		goto done_all;
392
+	}
393
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
394
+		p++;
395
+	pvname.len = p - pvname.s;
396
+	if(pvstate==1)
397
+	{
398
+		if(*p==PV_RNBRACKET)
399
+		{ /* full pv name ended here*/
400
+			goto done_inm;
401
+		} else if(*p==PV_LNBRACKET) {
402
+			p++;
403
+			pvstate = 2;
404
+		} else if(*p==PV_LIBRACKET) {
405
+			p++;
406
+			pvstate = 3;
407
+		} else if(*p==TR_LBRACKET) {
408
+			p++;
409
+			pvstate = 4;
410
+		} else {
411
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
412
+					pvstate);
413
+			goto error;
414
+		}
415
+	} else { 
416
+		if(!is_in_str(p, in)) {
417
+			p--;
418
+			goto done_inm;
419
+		} else if(*p==PV_LNBRACKET) {
420
+			p++;
421
+			pvstate = 5;
422
+		} else {
423
+			/* still in input str, but end of PV */
424
+			/* p is increased at the end, so decrement here */
425
+			p--;
426
+			goto done_inm;
427
+		}
428
+	}
429
+
430
+done_inm:
431
+	if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
432
+	{
433
+		LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
434
+		goto error;
435
+	}
436
+	if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
437
+	{
438
+		LM_ERR("pvar \"%.*s\" expects an inner name\n",
439
+				pvname.len, pvname.s);
440
+		goto error;
441
+	}
442
+	if(pvstate==2 || pvstate==5)
443
+	{
444
+		if(pte->parse_name==NULL)
445
+		{
446
+			LM_ERR("pvar \"%.*s\" does not get name param\n",
447
+					pvname.len, pvname.s);
448
+			goto error;
449
+		}
450
+		s.s = p;
451
+		n = 0;
452
+		while(is_in_str(p, in))
453
+		{
454
+			if(*p==PV_RNBRACKET)
455
+			{
456
+				if(n==0)
457
+					break;
458
+				n--;
459
+			}
460
+			if(*p == PV_LNBRACKET)
461
+				n++;
462
+			p++;
463
+		}
464
+
465
+		if(!is_in_str(p, in))
466
+			goto error;
467
+
468
+		if(p==s.s)
469
+		{
470
+			LM_ERR("pvar \"%.*s\" does not get empty name param\n",
471
+					pvname.len, pvname.s);
472
+			goto error;
473
+		}
474
+		s.len = p - s.s;
475
+		if(pte->parse_name(e, &s)!=0)
476
+		{
477
+			LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
478
+					pvname.len, pvname.s, s.len, s.s);
479
+			goto error;
480
+		}
481
+		if(pvstate==2)
482
+		{
483
+			p++;
484
+			if(*p==PV_RNBRACKET)
485
+			{ /* full pv name ended here*/
486
+				goto done_vnm;
487
+			} else if(*p==PV_LIBRACKET) {
488
+				p++;
489
+				pvstate = 3;
490
+			} else if(*p==TR_LBRACKET) {
491
+				p++;
492
+				pvstate = 4;
493
+			} else {
494
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
495
+					pvstate);
496
+				goto error;
497
+			}
498
+		} else {
499
+			if(*p==PV_RNBRACKET)
500
+			{ /* full pv name ended here*/
501
+				p++;
502
+				goto done_all;
503
+			} else {
504
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
505
+					pvstate);
506
+				goto error;
507
+			}
508
+		}
509
+	}
510
+done_vnm:
511
+	if(pvstate==3)
512
+	{
513
+		if(pte->parse_index==NULL)
514
+		{
515
+			LM_ERR("pvar \"%.*s\" does not get index param\n",
516
+					pvname.len, pvname.s);
517
+			goto error;
518
+		}
519
+		s.s = p;
520
+		n = 0;
521
+		while(is_in_str(p, in))
522
+		{
523
+			if(*p==PV_RIBRACKET)
524
+			{
525
+				if(n==0)
526
+					break;
527
+				n--;
528
+			}
529
+			if(*p == PV_LIBRACKET)
530
+				n++;
531
+			p++;
532
+		}
533
+		if(!is_in_str(p, in))
534
+			goto error;
535
+
536
+		if(p==s.s)
537
+		{
538
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
539
+					pvname.len, pvname.s);
540
+			goto error;
541
+		}
542
+		s.len = p - s.s;
543
+		if(pte->parse_index(e, &s)!=0)
544
+		{
545
+			LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
546
+					pvname.len, pvname.s, s.len, s.s);
547
+			goto error;
548
+		}
549
+		p++;
550
+		if(*p==PV_RNBRACKET)
551
+		{ /* full pv name ended here*/
552
+			goto done_idx;
553
+		} else if(*p==TR_LBRACKET) {
554
+			p++;
555
+			pvstate = 4;
556
+		} else {
557
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
558
+					pvstate);
559
+			goto error;
560
+		}
561
+	}
562
+done_idx:
563
+	if(pvstate==4)
564
+	{
565
+		s.s = p-1;
566
+		n = 0;
567
+		while(is_in_str(p, in))
568
+		{
569
+			if(*p==TR_RBRACKET)
570
+			{
571
+				if(n==0)
572
+				{
573
+					/* yet another transformation */
574
+					p++;
575
+					while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
576
+
577
+					if(!is_in_str(p, in) || *p != TR_LBRACKET)
578
+					{
579
+						p--;
580
+						break;
581
+					}
582
+				}
583
+				n--;
584
+			}
585
+			if(*p == TR_LBRACKET)
586
+				n++;
587
+			p++;
588
+		}
589
+		if(!is_in_str(p, in))
590
+			goto error;
591
+
592
+		if(p==s.s)
593
+		{
594
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
595
+					pvname.len, pvname.s);
596
+			goto error;
597
+		}
598
+		s.len = p - s.s + 1;
599
+
600
+		p = tr_lookup(&s, &tr);
601
+		if(p==NULL)
602
+		{
603
+			LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
604
+			goto error;
605
+		}
606
+		if(*p!=PV_RNBRACKET)
607
+		{
608
+			LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
609
+			goto error;
610
+		}
611
+		e->trans = (void*)tr;
612
+	}
613
+	p++;
614
+
615
+done_all:
616
+	if(pte!=NULL && pte->init_param)
617
+		pte->init_param(e, pte->iparam);
618
+	return p;
619
+
620
+error:
621
+	if(p!=NULL)
622
+		LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
623
+			in->len, in->s, (int)(p-in->s), pvstate);
624
+	else
625
+		LM_ERR("invalid parsing in [%.*s] at (%d)\n", in->len, in->s, pvstate);
626
+	return NULL;
627
+
628
+} /* end: pv_parse_spec */
629
+
630
+void pv_spec_free(pv_spec_t *spec)
631
+{
632
+	if(spec==0) return;
633
+	/* TODO: free name if it is PV */
634
+	if(spec->trans)
635
+		tr_free((trans_t*)spec->trans);
636
+	pkg_free(spec);
637
+}
638
+
639
+static inline char* tr_get_class(str *in, char *p, str *tclass)
640
+{
641
+	tclass->s = p;
642
+	while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
643
+	if(*p!=TR_CLASS_MARKER || tclass->s == p)
644
+	{
645
+		LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
646
+		return NULL;
647
+	}
648
+	tclass->len = p - tclass->s;
649
+	p++;
650
+
651
+	return p;
652
+}
653
+
654
+static inline trans_t* tr_new()
655
+{
656
+	trans_t *t = NULL;
657
+
658
+	t = (trans_t*)pkg_malloc(sizeof(trans_t));
659
+	if(t == NULL)
660
+	{
661
+		LM_ERR("no more private memory\n");
662
+		return NULL;
663
+	}
664
+	memset(t, 0, sizeof(trans_t));
665
+	return t;
666
+}
667
+
668
+char* tr_lookup(str *in, trans_t **tr)
669
+{
670
+	char *p;
671
+	char *p0;
672
+	str tclass;
673
+	tr_export_t *te = NULL; 
674
+	trans_t *t = NULL;
675
+	trans_t *t0 = NULL;
676
+	str s;
677
+
678
+	if(in==NULL || in->s==NULL || tr==NULL)
679
+		return NULL;
680
+	
681
+	p = in->s;
682
+	do {
683
+		while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
684
+		if(*p != TR_LBRACKET)
685
+			break;
686
+		p++;
687
+
688
+		if((t = tr_new())==NULL) return NULL;
689
+
690
+		if(t0==NULL) *tr = t;
691
+		else t0->next = t;
692
+		t0 = t;
693
+
694
+		/* find transformation class */
695
+		p = tr_get_class(in, p, &tclass);
696
+		if(p==NULL) goto error;
697
+
698
+		/* locate transformation */
699
+		te = tr_lookup_class(&tclass);
700
+		if(te==NULL)
701
+		{
702
+			LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
703
+				tclass.len, tclass.s, in->len, in->s);
704
+			goto error;
705
+		}
706
+
707
+		s.s = p; s.len = in->s + in->len - p;
708
+		p0 = te->tparse(&s, t);
709
+		if(p0==NULL)
710
+			goto error;
711
+		p = p0;
712
+
713
+		if(*p != TR_RBRACKET)
714
+		{
715
+			LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
716
+					in->s, *p);
717
+			goto error;
718
+		}
719
+
720
+		p++;
721
+		if(!is_in_str(p, in))
722
+			break;
723
+	} while(1);
724
+
725
+	return p;
726
+error:
727
+	LM_ERR("error parsing [%.*s]\n", in->len, in->s);
728
+	t = *tr;
729
+	while(t)
730
+	{
731
+		t0 = t;
732
+		t = t->next;
733
+		tr_destroy(t0);
734
+		pkg_free(t0);
735
+	}
736
+	return NULL;
737
+}
738
+
739
+/*!
740
+ * \brief Destroy transformation including eventual parameter
741
+ * \param t transformation
742
+ */
743
+void tr_destroy(trans_t *t)
744
+{
745
+	tr_param_t *tp;
746
+	tr_param_t *tp0;
747
+	if(t==NULL) return;
748
+
749
+	tp = t->params;
750
+	while(tp)
751
+	{
752
+		tp0 = tp;
753
+		tp = tp->next;
754
+		tr_param_free(tp0);
755
+	}
756
+	memset(t, 0, sizeof(trans_t));
757
+}
758
+
759
+
760
+/*!
761
+ * \brief Free allocated memory of transformation list
762
+ * \param t transformation list
763
+ */
764
+void tr_free(trans_t *t)
765
+{
766
+	trans_t *t0;
767
+
768
+	while(t)
769
+	{
770
+		t0 = t;
771
+		t = t->next;
772
+		tr_destroy(t0);
773
+		pkg_free(t0);
774
+	}
775
+}
776
+
777
+
778
+/*!
779
+ * \brief Free transformation parameter list
780
+ * \param tp transformation list
781
+ */
782
+void tr_param_free(tr_param_t *tp)
783
+{
784
+	tr_param_t *tp0;
785
+
786
+	if(tp==NULL) return;
787
+	while(tp)
788
+	{
789
+		tp0 = tp;
790
+		tp = tp->next;
791
+		if(tp0->type==TR_PARAM_SPEC)
792
+			pv_spec_free((pv_spec_t*)tp0->v.data);
793
+		pkg_free(tp0);
794
+	}
795
+}
796
+
797
+typedef struct _tr_item
798
+{
799
+	tr_export_t tre;
800
+	unsigned int trid;
801
+	struct _tr_item *next;
802
+} tr_item_t, *tr_item_p;
803
+
804
+static tr_item_t* _tr_table[PV_TABLE_SIZE];
805
+static int _tr_table_set = 0;
806
+
807
+
808
+/**
809
+ *
810
+ */
811
+void tr_init_table()
812
+{
813
+	memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
814
+	_tr_table_set = 1;
815
+}
816
+
817
+/**
818
+ *
819
+ */
820
+int tr_table_add(tr_export_t *e)
821
+{
822
+	tr_item_t *tri = NULL;
823
+	tr_item_t *trj = NULL;
824
+	tr_item_t *trn = NULL;
825
+	int found;
826
+	int trid;
827
+
828
+	if(e==NULL || e->tclass.s==NULL)
829
+	{
830
+		LM_ERR("invalid parameters\n");
831
+		return -1;
832
+	}
833
+	
834
+	if(_tr_table_set==0)
835
+	{
836
+		LM_DBG("TR table not initialized, doing it now\n");
837
+		tr_init_table();
838
+	}
839
+
840
+	found = 0;
841
+	trid = get_hash1_raw(e->tclass.s, e->tclass.len);
842
+
843
+	tri = _tr_table[trid%PV_TABLE_SIZE];
844
+	while(tri)
845
+	{
846
+		if(tri->trid > trid)
847
+			break;
848
+		if(tri->tre.tclass.len > e->tclass.len)
849
+			break;
850
+		if(tri->tre.tclass.len==e->tclass.len)
851
+		{
852
+			found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
853
+			if(found>0)
854
+				break;
855
+			if(found==0)
856
+			{
857
+				LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
858
+						e->tclass.s);
859
+				return -1;
860
+			}
861
+		}
862
+		trj = tri;
863
+		tri = tri->next;
864
+	}
865
+
866
+	trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
867
+	if(trn==0)
868
+	{
869
+		LM_ERR("no more memory\n");
870
+		return -1;
871
+	}
872
+	memset(trn, 0, sizeof(tr_item_t));
873
+	memcpy(&(trn->tre), e, sizeof(tr_export_t));
874
+	trn->trid = trid;
875
+
876
+	if(trj==0)
877
+	{
878
+		trn->next = _tr_table[trid%PV_TABLE_SIZE];
879
+		_tr_table[trid%PV_TABLE_SIZE] = trn;
880
+		goto done;
881
+	}
882
+	trn->next = trj->next;
883
+	trj->next = trn;
884
+
885
+done:
886
+	return 0;
887
+}
888
+
889
+/**
890
+ *
891
+ */
892
+int register_trans_mod(char *mod_name, tr_export_t *items)
893
+{
894
+	int ret;
895
+	int i;
896
+
897
+	if (items==0)
898
+		return 0;
899
+
900
+	for ( i=0 ; items[i].tclass.s ; i++ ) {
901
+		ret = tr_table_add(&items[i]);
902
+		if (ret!=0) {
903
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
904
+					items[i].tclass.len, items[i].tclass.s, mod_name);
905
+		}
906
+	}
907
+	return 0;
908
+}
909
+
910
+/**
911
+ *
912
+ */
913
+int tr_table_free(void)
914
+{
915
+	tr_item_p te;
916
+	tr_item_p te1;
917
+	int i;
918
+
919
+	for(i=0; i<TR_TABLE_SIZE; i++)
920
+	{
921
+		te = _tr_table[i];
922
+		while(te!=0)
923
+		{
924
+			te1 = te;
925
+			te = te->next;
926
+			pkg_free(te1);
927
+		}
928
+		memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
929
+		_tr_table_set = 0;
930
+	}
931
+	
932
+	return 0;
933
+}
934
+
935
+tr_export_t* tr_lookup_class(str *tclass)
936
+{
937
+	tr_item_t *tri;
938
+	unsigned int trid;
939
+
940
+	if(tclass==0 || tclass->s==0)
941
+	{
942
+		LM_ERR("bad parameters\n");
943
+		return NULL;
944
+	}
945
+
946
+	/* search in TR table */
947
+	trid = get_hash1_raw(tclass->s, tclass->len);
948
+	tri = _tr_table[trid%TR_TABLE_SIZE];
949
+	while(tri)
950
+	{
951
+		if(tri->trid > trid)
952
+			break;
953
+		if(tri->tre.tclass.len > tclass->len)
954
+			break;
955
+
956
+		if(tri->trid==trid && tri->tre.tclass.len==tclass->len
957
+				&& memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
958
+			return &(tri->tre);
959
+		tri = tri->next;
960
+	}
961
+
962
+	return NULL;
963
+}
964
+
... ...
@@ -1,74 +1,85 @@
1 1
 /*
2
- * $Id$
2
+ * $Id: items.h 2111 2007-05-01 11:18:08Z juhe $
3 3
  *
4
- * Copyright (C) 2008 
4
+ * Copyright (C) 2001-2003 FhG Fokus
5 5
  *
6
- * Permission to use, copy, modify, and distribute this software for any
7
- * purpose with or without fee is hereby granted, provided that the above
8
- * copyright notice and this permission notice appear in all copies.
6
+ * This file is part of SIP-Router, a free SIP server.
9 7
  *
10
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
- */
18
-/*
19
- * pvar compatibility wrapper for kamailio
20
- * for now it doesn't do anything (feel free to rm & replace the file)
8
+ * SIP-Router is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
21 12
  *
22
- * History:
23
- * --------
24
- *  2008-11-17  initial version compatible with kamailio pvar.h (andrei)
13
+ * SIP-Router is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License 
19
+ * along with this program; if not, write to the Free Software 
20
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
+ */
22
+
23
+/*!
24
+ * \file
25
+ * \brief Definitions for Pseudo-variable support
25 26
  */
26 27
 
27
-#ifndef _pvar_h_
28
-#define _pvar_h_
28
+
29
+#ifndef _PVAR_H_
30
+#define _PVAR_H_
29 31
 
30 32
 #include "str.h"
31 33
 #include "usr_avp.h"
32 34
 #include "parser/msg_parser.h"
33 35
 
34
-
35
-
36
+#define PV_MARKER_STR	"$"
37
+#define PV_MARKER		'$'
38
+
39
+#define PV_LNBRACKET_STR	"("
40
+#define PV_LNBRACKET		'('
41
+#define PV_RNBRACKET_STR	")"
42
+#define PV_RNBRACKET		')'
43
+
44
+#define PV_LIBRACKET_STR	"["
45
+#define PV_LIBRACKET		'['
46
+#define PV_RIBRACKET_STR	"]"
47
+#define PV_RIBRACKET		']'
48
+
49
+#define PV_VAL_NONE			0
50
+#define PV_VAL_NULL			1
51
+#define PV_VAL_EMPTY		2
52
+#define PV_VAL_STR			4
53
+#define PV_VAL_INT			8
54
+#define PV_TYPE_INT			16
55
+#define PV_VAL_PKG			32
56
+#define PV_VAL_SHM			64
57
+
58
+#define PV_NAME_INTSTR	0
59
+#define PV_NAME_PVAR	1
60
+
61
+#define PV_IDX_INT	0
62
+#define PV_IDX_PVAR	1
63
+#define PV_IDX_ALL	2
64
+
65
+/*! if PV name is dynamic, integer, or str */
66
+#define pv_has_dname(pv) ((pv)->pvp.pvn.type==PV_NAME_PVAR)
67
+#define pv_has_iname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
68
+							&& !((pv)->pvp.pvn.u.isname.type&AVP_NAME_STR))
69
+#define pv_has_sname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
70
+							&& (pv)->pvp.pvn.u.isname.type&AVP_NAME_STR)
71
+#define pv_is_w(pv)	((pv)->setf!=NULL)
36 72
 
37 73
 enum _pv_type { 
38 74
 	PVT_NONE=0,           PVT_EMPTY,             PVT_NULL, 
39
-	PVT_MARKER,           PVT_AVP,               PVT_HDR,
40
-	PVT_PID,              PVT_RETURN_CODE,       PVT_TIMES,
41
-	PVT_TIMEF,            PVT_MSGID,             PVT_METHOD,
42
-	PVT_STATUS,           PVT_REASON,            PVT_RURI,
43
-	PVT_RURI_USERNAME,    PVT_RURI_DOMAIN,       PVT_RURI_PORT,
44
-	PVT_FROM,             PVT_FROM_USERNAME,     PVT_FROM_DOMAIN,
45
-	PVT_FROM_TAG,         PVT_TO,                PVT_TO_USERNAME,
46
-	PVT_TO_DOMAIN,        PVT_TO_TAG,            PVT_CSEQ,
47
-	PVT_CONTACT,          PVT_CALLID,            PVT_USERAGENT,
48
-	PVT_MSG_BUF,          PVT_MSG_LEN,           PVT_FLAGS,
49
-	PVT_HEXFLAGS,         PVT_SRCIP,             PVT_SRCPORT,
50
-	PVT_RCVIP,            PVT_RCVPORT,           PVT_REFER_TO,
51
-	PVT_DSET,             PVT_DSTURI,            PVT_COLOR,
52
-	PVT_BRANCH,           PVT_BRANCHES,          PVT_CONTENT_TYPE,
53
-	PVT_CONTENT_LENGTH,   PVT_MSG_BODY,          PVT_AUTH_USERNAME,
54
-	PVT_AUTH_REALM,       PVT_RURI_PROTOCOL,     PVT_DSTURI_DOMAIN,
55
-	PVT_DSTURI_PORT,      PVT_DSTURI_PROTOCOL,   PVT_FROM_DISPLAYNAME,
56
-	PVT_TO_DISPLAYNAME,   PVT_OURI,              PVT_OURI_USERNAME,
57
-	PVT_OURI_DOMAIN,      PVT_OURI_PORT,         PVT_OURI_PROTOCOL,
58
-	PVT_FORCE_SOCK,       PVT_RPID_URI,          PVT_DIVERSION_URI,
59
-	PVT_ACC_USERNAME,     PVT_PPI,               PVT_PPI_DISPLAYNAME,
60
-	PVT_PPI_DOMAIN,       PVT_PPI_USERNAME,      PVT_PAI_URI,
61
-	PVT_BFLAGS,           PVT_HEXBFLAGS,         PVT_SFLAGS,
62
-	PVT_HEXSFLAGS,        PVT_ERR_CLASS,         PVT_ERR_LEVEL,
63
-	PVT_ERR_INFO,         PVT_ERR_RCODE,         PVT_ERR_RREASON,
64
-	PVT_SCRIPTVAR,        PVT_PROTO,             PVT_AUTH_USERNAME_WHOLE,
65
-	PVT_AUTH_DURI,        PVT_DIV_REASON,        PVT_DIV_PRIVACY,
66
-	PVT_AUTH_DOMAIN,      PVT_EXTRA /* keep it last */
75
+	PVT_MARKER,           PVT_AVP,               PVT_OTHER,
76
+	PVT_EXTRA /* keep it last */
67 77
 };
68 78
 
69 79
 typedef enum _pv_type pv_type_t;
70 80
 typedef int pv_flags_t;
71 81
 
82
+
72 83
 typedef struct _pv_value
73 84
 {
74 85
 	str rs;    /*!< string value */
... ...
@@ -114,8 +125,6 @@ typedef struct _pv_spec {
114 114
 	void         *trans; /*!< transformations */
115 115
 } pv_spec_t, *pv_spec_p;
116 116
 
117
-
118
-
119 117
 typedef int (*pv_parse_name_f)(pv_spec_p sp, str *in);
120 118
 typedef int (*pv_parse_index_f)(pv_spec_p sp, str *in);
121 119
 typedef int (*pv_init_param_f)(pv_spec_p sp, int param);
... ...
@@ -142,4 +151,103 @@ typedef struct _pv_export {
142 142
 	int iparam;                    /*!< parameter for the init function */
143 143
 } pv_export_t;
144 144
 
145
-#endif /* _pvar_h_ */
145
+typedef struct _pv_elem
146
+{
147
+	str text;
148
+	pv_spec_t spec;
149
+	struct _pv_elem *next;
150
+} pv_elem_t, *pv_elem_p;
151
+
152
+char* pv_parse_spec(str *in, pv_spec_p sp);
153
+int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
154
+int pv_print_spec(struct sip_msg* msg, pv_spec_p sp, char *buf, int *len);
155
+int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len);
156
+int pv_elem_free_all(pv_elem_p log);
157
+void pv_value_destroy(pv_value_t *val);
158
+void pv_spec_free(pv_spec_t *spec);
159
+int pv_spec_dbg(pv_spec_p sp);
160
+int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags);
161
+int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
162
+		unsigned short *name_type);
163
+int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name);
164
+int pv_parse_format(str *in, pv_elem_p *el);
165
+int pv_init_iname(pv_spec_p sp, int param);
166
+int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s);
167
+
168
+typedef struct _pvname_list {
169
+	pv_spec_t sname;
170
+	struct _pvname_list *next;
171
+} pvname_list_t, *pvname_list_p;
172
+
173
+typedef struct pv_spec_list {
174
+	pv_spec_p spec;
175
+	struct pv_spec_list *next;
176
+} pv_spec_list_t, *pv_spec_list_p;
177
+
178
+pvname_list_t* parse_pvname_list(str *in, unsigned int type);
179
+
180
+int register_pvars_mod(char *mod_name, pv_export_t *items);
181
+int pv_free_extra_list(void);
182
+
183
+/*! \brief PV helper functions */
184
+int pv_parse_index(pv_spec_p sp, str *in);
185
+
186
+int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
187
+int pv_update_time(struct sip_msg *msg, time_t *t);
188
+
189
+int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
190
+		pv_value_t *res, unsigned int uival);
191
+int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
192
+		pv_value_t *res, int sival);
193
+int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
194
+		pv_value_t *res, str *sval);
195
+int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
196
+		pv_value_t *res, str *sval, int ival);
197
+int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
198
+		pv_value_t *res, int ival, str *sval);
199
+
200
+/**
201
+ * Transformations
202
+ */
203
+#define TR_LBRACKET_STR		"{"
204
+#define TR_LBRACKET		'{'
205
+#define TR_RBRACKET_STR		"}"
206
+#define TR_RBRACKET		'}'
207
+#define TR_CLASS_MARKER		'.'
208
+#define TR_PARAM_MARKER		','
209
+
210
+enum _tr_param_type { TR_PARAM_NONE=0, TR_PARAM_STRING, TR_PARAM_NUMBER,
211
+	TR_PARAM_SPEC };
212
+
213
+typedef struct _tr_param {
214
+	int type;
215
+	union {
216
+		int n;
217
+		str s;
218
+		void *data;
219
+	} v;
220
+	struct _tr_param *next;
221
+} tr_param_t, *tr_param_p;
222
+
223
+typedef int (*tr_func_t) (struct sip_msg *, tr_param_t*, int, pv_value_t*);
224
+
225
+typedef struct _trans {
226
+	str name;
227
+	int type;
228
+	int subtype;
229
+	tr_func_t trf;
230
+	tr_param_t *params;
231
+	struct _trans *next;
232
+} trans_t, *trans_p;
233
+
234
+typedef char* (*tr_parsef_t)(str *, trans_t *);
235
+typedef struct _tr_export {
236
+	str tclass;
237
+	tr_parsef_t tparse; 
238
+} tr_export_t, *tr_export_p;
239
+
240
+char* tr_lookup(str *in, trans_t **tr);
241
+tr_export_t* tr_lookup_class(str *tclass);
242
+
243
+#endif
244
+