Browse Source

Merge branch 'pvar' into fixups

* pvar:
  PV get null helper added
  updated pvar.h file according to previous commit
  pseudo-variable engine api
Andrei Pelinescu-Onciul 17 years ago
parent
commit
d4dac38e4d
2 changed files with 1127 additions and 54 deletions
  1. 965 0
      pvapi.c
  2. 162 54
      pvar.h

+ 965 - 0
pvapi.c

@@ -0,0 +1,965 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ *
+ * This file is part of SIP-Router, a free SIP server.
+ *
+ * SIP-Router 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
+ *
+ * SIP-Router 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*!
+ * \file
+ * \brief PV API specification
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "mem/mem.h"
+#include "ut.h"
+#include "dprint.h"
+#include "hashes.h"
+#include "pvar.h"
+
+#define is_in_str(p, in) (p<in->s+in->len && *p)
+
+#define PV_TABLE_SIZE	16
+#define TR_TABLE_SIZE	2
+
+
+void tr_destroy(trans_t *t);
+void tr_free(trans_t *t);
+void tr_param_free(tr_param_t *tp);
+
+typedef struct _pv_item
+{
+	pv_export_t pve;
+	unsigned int pvid;
+	struct _pv_item *next;
+} pv_item_t, *pv_item_p;
+
+static pv_item_t* _pv_table[PV_TABLE_SIZE];
+static int _pv_table_set = 0;
+
+
+/**
+ *
+ */
+void pv_init_table()
+{
+	memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
+	_pv_table_set = 1;
+}
+
+/**
+ *
+ */
+static int is_pv_valid_char(char c)
+{
+	if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
+			|| (c=='_') || (c=='.'))
+		return 1;
+	return 0;
+}
+
+/**
+ *
+ */
+int pv_table_add(pv_export_t *e)
+{
+	char *p;
+	str  *in;
+	pv_item_t *pvi = NULL;
+	pv_item_t *pvj = NULL;
+	pv_item_t *pvn = NULL;
+	int found;
+	int pvid;
+
+	if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
+	{
+		LM_ERR("invalid parameters\n");
+		return -1;
+	}
+	
+	if(_pv_table_set==0)
+	{
+		LM_DBG("PV table not initialized, doing it now\n");
+		pv_init_table();
+	}
+	in = &(e->name);
+	p = in->s;	
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
+		p++;
+	if(is_in_str(p,in))
+	{
+		LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
+		return -1;
+	}
+	found = 0;
+	pvid = get_hash1_raw(in->s, in->len);
+
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
+	while(pvi)
+	{
+		if(pvi->pvid > pvid)
+			break;
+		if(pvi->pve.name.len > in->len)
+			break;
+		if(pvi->pve.name.len==in->len)
+		{
+			found = strncmp(pvi->pve.name.s, in->s, in->len);
+			if(found>0)
+				break;
+			if(found==0)
+			{
+				LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
+				return -1;
+			}
+		}
+		pvj = pvi;
+		pvi = pvi->next;
+	}
+
+	pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
+	if(pvn==0)
+	{
+		LM_ERR("no more memory\n");
+		return -1;
+	}
+	memset(pvn, 0, sizeof(pv_item_t));
+	memcpy(&(pvn->pve), e, sizeof(pv_export_t));
+	pvn->pvid = pvid;
+
+	if(pvj==0)
+	{
+		pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
+		_pv_table[pvid%PV_TABLE_SIZE] = pvn;
+		goto done;
+	}
+	pvn->next = pvj->next;
+	pvj->next = pvn;
+
+done:
+	return 0;
+}
+
+/**
+ *
+ */
+int register_pvars_mod(char *mod_name, pv_export_t *items)
+{
+	int ret;
+	int i;
+
+	if (items==0)
+		return 0;
+
+	for ( i=0 ; items[i].name.s ; i++ ) {
+		ret = pv_table_add(&items[i]);
+		if (ret!=0) {
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
+					items[i].name.len, items[i].name.s, mod_name);
+		}
+	}
+	return 0;
+}
+
+/**
+ *
+ */
+int pv_table_free(void)
+{
+	pv_item_p xe;
+	pv_item_p xe1;
+	int i;
+
+	for(i=0; i<PV_TABLE_SIZE; i++)
+	{
+		xe = _pv_table[i];
+		while(xe!=0)
+		{
+			xe1 = xe;
+			xe = xe->next;
+			pkg_free(xe1);
+		}
+		memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
+		_pv_table_set = 0;
+	}
+	
+	return 0;
+}
+
+/********** helper functions ********/
+/**
+ * convert unsigned int to pv_value_t
+ */
+int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, unsigned int uival)
+{
+	int l = 0;
+	char *ch = NULL;
+
+	if(res==NULL)
+		return -1;
+
+	ch = int2str(uival, &l);
+	res->rs.s = ch;
+	res->rs.len = l;
+
+	res->ri = (int)uival;
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
+	return 0;
+}
+
+/**
+ * convert signed int to pv_value_t
+ */
+int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, int sival)
+{
+	int l = 0;
+	char *ch = NULL;
+
+	if(res==NULL)
+		return -1;
+
+	ch = int2str(sival, &l);
+	res->rs.s = ch;
+	res->rs.len = l;
+
+	res->ri = sival;
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
+	return 0;
+}
+
+/**
+ * convert str to pv_value_t
+ */
+int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, str *sval)
+{
+	if(res==NULL)
+		return -1;
+
+	res->rs = *sval;
+	res->flags = PV_VAL_STR;
+	return 0;
+}
+
+/**
+ * convert str-int to pv_value_t (type is str)
+ */
+int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, str *sval, int ival)
+{
+	if(res==NULL)
+		return -1;
+
+	res->rs = *sval;
+	res->ri = ival;
+	res->flags = PV_VAL_STR|PV_VAL_INT;
+	return 0;
+}
+
+/**
+ * convert int-str to pv_value_t (type is int)
+ */
+int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, int ival, str *sval)
+{
+	if(res==NULL)
+		return -1;
+
+	res->rs = *sval;
+	res->ri = ival;
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
+	return 0;
+}
+
+/*** ============================= ***/
+static str pv_str_marker = { PV_MARKER_STR, 1 };
+static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res)
+{
+	return pv_get_strintval(msg, param, res, &pv_str_marker,
+			(int)pv_str_marker.s[0]);
+}
+
+static str pv_str_empty  = { "", 0 };
+int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
+{
+	if(res==NULL)
+		return -1;
+	
+	res->rs = pv_str_empty;
+	res->ri = 0;
+	res->flags = PV_VAL_NULL;
+	return 0;
+}
+
+pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
+{
+	pv_item_t *pvi;
+	unsigned int pvid;
+
+	if(pvname==0 || e==0)
+	{
+		LM_ERR("bad parameters\n");
+		return NULL;
+	}
+
+	/* search in PV table */
+	pvid = get_hash1_raw(pvname->s, pvname->len);
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
+	while(pvi)
+	{
+		if(pvi->pvid > pvid)
+			break;
+		if(pvi->pve.name.len > pvname->len)
+			break;
+
+		if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
+			&& memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
+		{
+			/*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
+					_pv_names_table[i].type);*/
+			/* copy data from table to spec */
+			e->type = pvi->pve.type;
+			e->getf = pvi->pve.getf;
+			e->setf = pvi->pve.setf;
+			return &(pvi->pve);
+		}
+		pvi = pvi->next;
+	}
+
+	return NULL;
+}
+
+char* pv_parse_spec(str *in, pv_spec_p e)
+{
+	char *p;
+	str s;
+	str pvname;
+	int pvstate;
+	trans_t *tr = NULL;
+	pv_export_t *pte = NULL;
+	int n=0;
+
+	if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
+	{
+		LM_ERR("bad parameters\n");
+		return NULL;
+	}
+	
+	/* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
+	tr = 0;
+	pvstate = 0;
+	memset(e, 0, sizeof(pv_spec_t));
+	p = in->s;
+	p++;
+	if(*p==PV_LNBRACKET)
+	{
+		p++;
+		pvstate = 1;
+	}
+	pvname.s = p;
+	if(*p == PV_MARKER) {
+		p++;
+		if(pvstate==1)
+		{
+			if(*p!=PV_RNBRACKET)
+				goto error;
+			p++;
+		}
+		e->getf = pv_get_marker;
+		e->type = PVT_MARKER;
+		pvname.len = 1;
+		goto done_all;
+	}
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
+		p++;
+	pvname.len = p - pvname.s;
+	if(pvstate==1)
+	{
+		if(*p==PV_RNBRACKET)
+		{ /* full pv name ended here*/
+			goto done_inm;
+		} else if(*p==PV_LNBRACKET) {
+			p++;
+			pvstate = 2;
+		} else if(*p==PV_LIBRACKET) {
+			p++;
+			pvstate = 3;
+		} else if(*p==TR_LBRACKET) {
+			p++;
+			pvstate = 4;
+		} else {
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
+					pvstate);
+			goto error;
+		}
+	} else { 
+		if(!is_in_str(p, in)) {
+			p--;
+			goto done_inm;
+		} else if(*p==PV_LNBRACKET) {
+			p++;
+			pvstate = 5;
+		} else {
+			/* still in input str, but end of PV */
+			/* p is increased at the end, so decrement here */
+			p--;
+			goto done_inm;
+		}
+	}
+
+done_inm:
+	if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
+	{
+		LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
+		goto error;
+	}
+	if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
+	{
+		LM_ERR("pvar \"%.*s\" expects an inner name\n",
+				pvname.len, pvname.s);
+		goto error;
+	}
+	if(pvstate==2 || pvstate==5)
+	{
+		if(pte->parse_name==NULL)
+		{
+			LM_ERR("pvar \"%.*s\" does not get name param\n",
+					pvname.len, pvname.s);
+			goto error;
+		}
+		s.s = p;
+		n = 0;
+		while(is_in_str(p, in))
+		{
+			if(*p==PV_RNBRACKET)
+			{
+				if(n==0)
+					break;
+				n--;
+			}
+			if(*p == PV_LNBRACKET)
+				n++;
+			p++;
+		}
+
+		if(!is_in_str(p, in))
+			goto error;
+
+		if(p==s.s)
+		{
+			LM_ERR("pvar \"%.*s\" does not get empty name param\n",
+					pvname.len, pvname.s);
+			goto error;
+		}
+		s.len = p - s.s;
+		if(pte->parse_name(e, &s)!=0)
+		{
+			LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
+					pvname.len, pvname.s, s.len, s.s);
+			goto error;
+		}
+		if(pvstate==2)
+		{
+			p++;
+			if(*p==PV_RNBRACKET)
+			{ /* full pv name ended here*/
+				goto done_vnm;
+			} else if(*p==PV_LIBRACKET) {
+				p++;
+				pvstate = 3;
+			} else if(*p==TR_LBRACKET) {
+				p++;
+				pvstate = 4;
+			} else {
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
+					pvstate);
+				goto error;
+			}
+		} else {
+			if(*p==PV_RNBRACKET)
+			{ /* full pv name ended here*/
+				p++;
+				goto done_all;
+			} else {
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
+					pvstate);
+				goto error;
+			}
+		}
+	}
+done_vnm:
+	if(pvstate==3)
+	{
+		if(pte->parse_index==NULL)
+		{
+			LM_ERR("pvar \"%.*s\" does not get index param\n",
+					pvname.len, pvname.s);
+			goto error;
+		}
+		s.s = p;
+		n = 0;
+		while(is_in_str(p, in))
+		{
+			if(*p==PV_RIBRACKET)
+			{
+				if(n==0)
+					break;
+				n--;
+			}
+			if(*p == PV_LIBRACKET)
+				n++;
+			p++;
+		}
+		if(!is_in_str(p, in))
+			goto error;
+
+		if(p==s.s)
+		{
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
+					pvname.len, pvname.s);
+			goto error;
+		}
+		s.len = p - s.s;
+		if(pte->parse_index(e, &s)!=0)
+		{
+			LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
+					pvname.len, pvname.s, s.len, s.s);
+			goto error;
+		}
+		p++;
+		if(*p==PV_RNBRACKET)
+		{ /* full pv name ended here*/
+			goto done_idx;
+		} else if(*p==TR_LBRACKET) {
+			p++;
+			pvstate = 4;
+		} else {
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
+					pvstate);
+			goto error;
+		}
+	}
+done_idx:
+	if(pvstate==4)
+	{
+		s.s = p-1;
+		n = 0;
+		while(is_in_str(p, in))
+		{
+			if(*p==TR_RBRACKET)
+			{
+				if(n==0)
+				{
+					/* yet another transformation */
+					p++;
+					while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
+
+					if(!is_in_str(p, in) || *p != TR_LBRACKET)
+					{
+						p--;
+						break;
+					}
+				}
+				n--;
+			}
+			if(*p == TR_LBRACKET)
+				n++;
+			p++;
+		}
+		if(!is_in_str(p, in))
+			goto error;
+
+		if(p==s.s)
+		{
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
+					pvname.len, pvname.s);
+			goto error;
+		}
+		s.len = p - s.s + 1;
+
+		p = tr_lookup(&s, &tr);
+		if(p==NULL)
+		{
+			LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
+			goto error;
+		}
+		if(*p!=PV_RNBRACKET)
+		{
+			LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
+			goto error;
+		}
+		e->trans = (void*)tr;
+	}
+	p++;
+
+done_all:
+	if(pte!=NULL && pte->init_param)
+		pte->init_param(e, pte->iparam);
+	return p;
+
+error:
+	if(p!=NULL)
+		LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
+			in->len, in->s, (int)(p-in->s), pvstate);
+	else
+		LM_ERR("invalid parsing in [%.*s] at (%d)\n", in->len, in->s, pvstate);
+	return NULL;
+
+} /* end: pv_parse_spec */
+
+void pv_spec_free(pv_spec_t *spec)
+{
+	if(spec==0) return;
+	/* TODO: free name if it is PV */
+	if(spec->trans)
+		tr_free((trans_t*)spec->trans);
+	pkg_free(spec);
+}
+
+static inline char* tr_get_class(str *in, char *p, str *tclass)
+{
+	tclass->s = p;
+	while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
+	if(*p!=TR_CLASS_MARKER || tclass->s == p)
+	{
+		LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
+		return NULL;
+	}
+	tclass->len = p - tclass->s;
+	p++;
+
+	return p;
+}
+
+static inline trans_t* tr_new()
+{
+	trans_t *t = NULL;
+
+	t = (trans_t*)pkg_malloc(sizeof(trans_t));
+	if(t == NULL)
+	{
+		LM_ERR("no more private memory\n");
+		return NULL;
+	}
+	memset(t, 0, sizeof(trans_t));
+	return t;
+}
+
+char* tr_lookup(str *in, trans_t **tr)
+{
+	char *p;
+	char *p0;
+	str tclass;
+	tr_export_t *te = NULL; 
+	trans_t *t = NULL;
+	trans_t *t0 = NULL;
+	str s;
+
+	if(in==NULL || in->s==NULL || tr==NULL)
+		return NULL;
+	
+	p = in->s;
+	do {
+		while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
+		if(*p != TR_LBRACKET)
+			break;
+		p++;
+
+		if((t = tr_new())==NULL) return NULL;
+
+		if(t0==NULL) *tr = t;
+		else t0->next = t;
+		t0 = t;
+
+		/* find transformation class */
+		p = tr_get_class(in, p, &tclass);
+		if(p==NULL) goto error;
+
+		/* locate transformation */
+		te = tr_lookup_class(&tclass);
+		if(te==NULL)
+		{
+			LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
+				tclass.len, tclass.s, in->len, in->s);
+			goto error;
+		}
+
+		s.s = p; s.len = in->s + in->len - p;
+		p0 = te->tparse(&s, t);
+		if(p0==NULL)
+			goto error;
+		p = p0;
+
+		if(*p != TR_RBRACKET)
+		{
+			LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
+					in->s, *p);
+			goto error;
+		}
+
+		p++;
+		if(!is_in_str(p, in))
+			break;
+	} while(1);
+
+	return p;
+error:
+	LM_ERR("error parsing [%.*s]\n", in->len, in->s);
+	t = *tr;
+	while(t)
+	{
+		t0 = t;
+		t = t->next;
+		tr_destroy(t0);
+		pkg_free(t0);
+	}
+	return NULL;
+}
+
+/*!
+ * \brief Destroy transformation including eventual parameter
+ * \param t transformation
+ */
+void tr_destroy(trans_t *t)
+{
+	tr_param_t *tp;
+	tr_param_t *tp0;
+	if(t==NULL) return;
+
+	tp = t->params;
+	while(tp)
+	{
+		tp0 = tp;
+		tp = tp->next;
+		tr_param_free(tp0);
+	}
+	memset(t, 0, sizeof(trans_t));
+}
+
+
+/*!
+ * \brief Free allocated memory of transformation list
+ * \param t transformation list
+ */
+void tr_free(trans_t *t)
+{
+	trans_t *t0;
+
+	while(t)
+	{
+		t0 = t;
+		t = t->next;
+		tr_destroy(t0);
+		pkg_free(t0);
+	}
+}
+
+
+/*!
+ * \brief Free transformation parameter list
+ * \param tp transformation list
+ */
+void tr_param_free(tr_param_t *tp)
+{
+	tr_param_t *tp0;
+
+	if(tp==NULL) return;
+	while(tp)
+	{
+		tp0 = tp;
+		tp = tp->next;
+		if(tp0->type==TR_PARAM_SPEC)
+			pv_spec_free((pv_spec_t*)tp0->v.data);
+		pkg_free(tp0);
+	}
+}
+
+typedef struct _tr_item
+{
+	tr_export_t tre;
+	unsigned int trid;
+	struct _tr_item *next;
+} tr_item_t, *tr_item_p;
+
+static tr_item_t* _tr_table[PV_TABLE_SIZE];
+static int _tr_table_set = 0;
+
+
+/**
+ *
+ */
+void tr_init_table()
+{
+	memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
+	_tr_table_set = 1;
+}
+
+/**
+ *
+ */
+int tr_table_add(tr_export_t *e)
+{
+	tr_item_t *tri = NULL;
+	tr_item_t *trj = NULL;
+	tr_item_t *trn = NULL;
+	int found;
+	int trid;
+
+	if(e==NULL || e->tclass.s==NULL)
+	{
+		LM_ERR("invalid parameters\n");
+		return -1;
+	}
+	
+	if(_tr_table_set==0)
+	{
+		LM_DBG("TR table not initialized, doing it now\n");
+		tr_init_table();
+	}
+
+	found = 0;
+	trid = get_hash1_raw(e->tclass.s, e->tclass.len);
+
+	tri = _tr_table[trid%PV_TABLE_SIZE];
+	while(tri)
+	{
+		if(tri->trid > trid)
+			break;
+		if(tri->tre.tclass.len > e->tclass.len)
+			break;
+		if(tri->tre.tclass.len==e->tclass.len)
+		{
+			found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
+			if(found>0)
+				break;
+			if(found==0)
+			{
+				LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
+						e->tclass.s);
+				return -1;
+			}
+		}
+		trj = tri;
+		tri = tri->next;
+	}
+
+	trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
+	if(trn==0)
+	{
+		LM_ERR("no more memory\n");
+		return -1;
+	}
+	memset(trn, 0, sizeof(tr_item_t));
+	memcpy(&(trn->tre), e, sizeof(tr_export_t));
+	trn->trid = trid;
+
+	if(trj==0)
+	{
+		trn->next = _tr_table[trid%PV_TABLE_SIZE];
+		_tr_table[trid%PV_TABLE_SIZE] = trn;
+		goto done;
+	}
+	trn->next = trj->next;
+	trj->next = trn;
+
+done:
+	return 0;
+}
+
+/**
+ *
+ */
+int register_trans_mod(char *mod_name, tr_export_t *items)
+{
+	int ret;
+	int i;
+
+	if (items==0)
+		return 0;
+
+	for ( i=0 ; items[i].tclass.s ; i++ ) {
+		ret = tr_table_add(&items[i]);
+		if (ret!=0) {
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
+					items[i].tclass.len, items[i].tclass.s, mod_name);
+		}
+	}
+	return 0;
+}
+
+/**
+ *
+ */
+int tr_table_free(void)
+{
+	tr_item_p te;
+	tr_item_p te1;
+	int i;
+
+	for(i=0; i<TR_TABLE_SIZE; i++)
+	{
+		te = _tr_table[i];
+		while(te!=0)
+		{
+			te1 = te;
+			te = te->next;
+			pkg_free(te1);
+		}
+		memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
+		_tr_table_set = 0;
+	}
+	
+	return 0;
+}
+
+tr_export_t* tr_lookup_class(str *tclass)
+{
+	tr_item_t *tri;
+	unsigned int trid;
+
+	if(tclass==0 || tclass->s==0)
+	{
+		LM_ERR("bad parameters\n");
+		return NULL;
+	}
+
+	/* search in TR table */
+	trid = get_hash1_raw(tclass->s, tclass->len);
+	tri = _tr_table[trid%TR_TABLE_SIZE];
+	while(tri)
+	{
+		if(tri->trid > trid)
+			break;
+		if(tri->tre.tclass.len > tclass->len)
+			break;
+
+		if(tri->trid==trid && tri->tre.tclass.len==tclass->len
+				&& memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
+			return &(tri->tre);
+		tri = tri->next;
+	}
+
+	return NULL;
+}
+

+ 162 - 54
pvar.h

@@ -1,74 +1,85 @@
 /*
- * $Id$
+ * $Id: items.h 2111 2007-05-01 11:18:08Z juhe $
  *
- * Copyright (C) 2008 
+ * Copyright (C) 2001-2003 FhG Fokus
  *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
+ * This file is part of SIP-Router, a free SIP server.
  *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-/*
- * pvar compatibility wrapper for kamailio
- * for now it doesn't do anything (feel free to rm & replace the file)
+ * SIP-Router 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
  *
- * History:
- * --------
- *  2008-11-17  initial version compatible with kamailio pvar.h (andrei)
+ * SIP-Router 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*!
+ * \file
+ * \brief Definitions for Pseudo-variable support
  */
 
-#ifndef _pvar_h_
-#define _pvar_h_
+
+#ifndef _PVAR_H_
+#define _PVAR_H_
 
 #include "str.h"
 #include "usr_avp.h"
 #include "parser/msg_parser.h"
 
-
-
+#define PV_MARKER_STR	"$"
+#define PV_MARKER		'$'
+
+#define PV_LNBRACKET_STR	"("
+#define PV_LNBRACKET		'('
+#define PV_RNBRACKET_STR	")"
+#define PV_RNBRACKET		')'
+
+#define PV_LIBRACKET_STR	"["
+#define PV_LIBRACKET		'['
+#define PV_RIBRACKET_STR	"]"
+#define PV_RIBRACKET		']'
+
+#define PV_VAL_NONE			0
+#define PV_VAL_NULL			1
+#define PV_VAL_EMPTY		2
+#define PV_VAL_STR			4
+#define PV_VAL_INT			8
+#define PV_TYPE_INT			16
+#define PV_VAL_PKG			32
+#define PV_VAL_SHM			64
+
+#define PV_NAME_INTSTR	0
+#define PV_NAME_PVAR	1
+
+#define PV_IDX_INT	0
+#define PV_IDX_PVAR	1
+#define PV_IDX_ALL	2
+
+/*! if PV name is dynamic, integer, or str */
+#define pv_has_dname(pv) ((pv)->pvp.pvn.type==PV_NAME_PVAR)
+#define pv_has_iname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
+							&& !((pv)->pvp.pvn.u.isname.type&AVP_NAME_STR))
+#define pv_has_sname(pv) ((pv)->pvp.pvn.type==PV_NAME_INTSTR \
+							&& (pv)->pvp.pvn.u.isname.type&AVP_NAME_STR)
+#define pv_is_w(pv)	((pv)->setf!=NULL)
 
 enum _pv_type { 
 	PVT_NONE=0,           PVT_EMPTY,             PVT_NULL, 
-	PVT_MARKER,           PVT_AVP,               PVT_HDR,
-	PVT_PID,              PVT_RETURN_CODE,       PVT_TIMES,
-	PVT_TIMEF,            PVT_MSGID,             PVT_METHOD,
-	PVT_STATUS,           PVT_REASON,            PVT_RURI,
-	PVT_RURI_USERNAME,    PVT_RURI_DOMAIN,       PVT_RURI_PORT,
-	PVT_FROM,             PVT_FROM_USERNAME,     PVT_FROM_DOMAIN,
-	PVT_FROM_TAG,         PVT_TO,                PVT_TO_USERNAME,
-	PVT_TO_DOMAIN,        PVT_TO_TAG,            PVT_CSEQ,
-	PVT_CONTACT,          PVT_CALLID,            PVT_USERAGENT,
-	PVT_MSG_BUF,          PVT_MSG_LEN,           PVT_FLAGS,
-	PVT_HEXFLAGS,         PVT_SRCIP,             PVT_SRCPORT,
-	PVT_RCVIP,            PVT_RCVPORT,           PVT_REFER_TO,
-	PVT_DSET,             PVT_DSTURI,            PVT_COLOR,
-	PVT_BRANCH,           PVT_BRANCHES,          PVT_CONTENT_TYPE,
-	PVT_CONTENT_LENGTH,   PVT_MSG_BODY,          PVT_AUTH_USERNAME,
-	PVT_AUTH_REALM,       PVT_RURI_PROTOCOL,     PVT_DSTURI_DOMAIN,
-	PVT_DSTURI_PORT,      PVT_DSTURI_PROTOCOL,   PVT_FROM_DISPLAYNAME,
-	PVT_TO_DISPLAYNAME,   PVT_OURI,              PVT_OURI_USERNAME,
-	PVT_OURI_DOMAIN,      PVT_OURI_PORT,         PVT_OURI_PROTOCOL,
-	PVT_FORCE_SOCK,       PVT_RPID_URI,          PVT_DIVERSION_URI,
-	PVT_ACC_USERNAME,     PVT_PPI,               PVT_PPI_DISPLAYNAME,
-	PVT_PPI_DOMAIN,       PVT_PPI_USERNAME,      PVT_PAI_URI,
-	PVT_BFLAGS,           PVT_HEXBFLAGS,         PVT_SFLAGS,
-	PVT_HEXSFLAGS,        PVT_ERR_CLASS,         PVT_ERR_LEVEL,
-	PVT_ERR_INFO,         PVT_ERR_RCODE,         PVT_ERR_RREASON,
-	PVT_SCRIPTVAR,        PVT_PROTO,             PVT_AUTH_USERNAME_WHOLE,
-	PVT_AUTH_DURI,        PVT_DIV_REASON,        PVT_DIV_PRIVACY,
-	PVT_AUTH_DOMAIN,      PVT_EXTRA /* keep it last */
+	PVT_MARKER,           PVT_AVP,               PVT_OTHER,
+	PVT_EXTRA /* keep it last */
 };
 
 typedef enum _pv_type pv_type_t;
 typedef int pv_flags_t;
 
+
 typedef struct _pv_value
 {
 	str rs;    /*!< string value */
@@ -114,8 +125,6 @@ typedef struct _pv_spec {
 	void         *trans; /*!< transformations */
 } pv_spec_t, *pv_spec_p;
 
-
-
 typedef int (*pv_parse_name_f)(pv_spec_p sp, str *in);
 typedef int (*pv_parse_index_f)(pv_spec_p sp, str *in);
 typedef int (*pv_init_param_f)(pv_spec_p sp, int param);
@@ -142,4 +151,103 @@ typedef struct _pv_export {
 	int iparam;                    /*!< parameter for the init function */
 } pv_export_t;
 
-#endif /* _pvar_h_ */
+typedef struct _pv_elem
+{
+	str text;
+	pv_spec_t spec;
+	struct _pv_elem *next;
+} pv_elem_t, *pv_elem_p;
+
+char* pv_parse_spec(str *in, pv_spec_p sp);
+int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
+int pv_print_spec(struct sip_msg* msg, pv_spec_p sp, char *buf, int *len);
+int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len);
+int pv_elem_free_all(pv_elem_p log);
+void pv_value_destroy(pv_value_t *val);
+void pv_spec_free(pv_spec_t *spec);
+int pv_spec_dbg(pv_spec_p sp);
+int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags);
+int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
+		unsigned short *name_type);
+int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name);
+int pv_parse_format(str *in, pv_elem_p *el);
+int pv_init_iname(pv_spec_p sp, int param);
+int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s);
+
+typedef struct _pvname_list {
+	pv_spec_t sname;
+	struct _pvname_list *next;
+} pvname_list_t, *pvname_list_p;
+
+typedef struct pv_spec_list {
+	pv_spec_p spec;
+	struct pv_spec_list *next;
+} pv_spec_list_t, *pv_spec_list_p;
+
+pvname_list_t* parse_pvname_list(str *in, unsigned int type);
+
+int register_pvars_mod(char *mod_name, pv_export_t *items);
+int pv_free_extra_list(void);
+
+/*! \brief PV helper functions */
+int pv_parse_index(pv_spec_p sp, str *in);
+
+int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
+int pv_update_time(struct sip_msg *msg, time_t *t);
+
+int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, unsigned int uival);
+int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, int sival);
+int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, str *sval);
+int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, str *sval, int ival);
+int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, int ival, str *sval);
+
+/**
+ * Transformations
+ */
+#define TR_LBRACKET_STR		"{"
+#define TR_LBRACKET		'{'
+#define TR_RBRACKET_STR		"}"
+#define TR_RBRACKET		'}'
+#define TR_CLASS_MARKER		'.'
+#define TR_PARAM_MARKER		','
+
+enum _tr_param_type { TR_PARAM_NONE=0, TR_PARAM_STRING, TR_PARAM_NUMBER,
+	TR_PARAM_SPEC };
+
+typedef struct _tr_param {
+	int type;
+	union {
+		int n;
+		str s;
+		void *data;
+	} v;
+	struct _tr_param *next;
+} tr_param_t, *tr_param_p;
+
+typedef int (*tr_func_t) (struct sip_msg *, tr_param_t*, int, pv_value_t*);
+
+typedef struct _trans {
+	str name;
+	int type;
+	int subtype;
+	tr_func_t trf;
+	tr_param_t *params;
+	struct _trans *next;
+} trans_t, *trans_p;
+
+typedef char* (*tr_parsef_t)(str *, trans_t *);
+typedef struct _tr_export {
+	str tclass;
+	tr_parsef_t tparse; 
+} tr_export_t, *tr_export_p;
+
+char* tr_lookup(str *in, trans_t **tr);
+tr_export_t* tr_lookup_class(str *tclass);
+
+#endif
+