4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of SIP-Router, a free SIP server.
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
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.
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
25 * \brief SIP-router core :: PV API specification
33 #include <sys/types.h>
37 #include "mem/shm_mem.h"
42 #include "pvapi_init.h"
45 #define is_in_str(p, in) (p<in->s+in->len && *p)
47 #define PV_TABLE_SIZE 16
48 #define TR_TABLE_SIZE 4
51 void tr_destroy(trans_t *t);
52 void tr_free(trans_t *t);
54 typedef struct _pv_item
58 struct _pv_item *next;
59 } pv_item_t, *pv_item_p;
61 static pv_item_t* _pv_table[PV_TABLE_SIZE];
62 static int _pv_table_set = 0;
68 void pv_init_table(void)
70 memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
77 static int is_pv_valid_char(char c)
79 if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
80 || (c=='_') || (c=='.') || (c=='?') /* ser $? */)
88 int pv_table_add(pv_export_t *e)
92 pv_item_t *pvi = NULL;
93 pv_item_t *pvj = NULL;
94 pv_item_t *pvn = NULL;
98 if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
100 LM_ERR("invalid parameters\n");
106 LM_DBG("PV table not initialized, doing it now\n");
111 while(is_in_str(p,in) && is_pv_valid_char(*p))
115 LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
119 //pvid = get_hash1_raw(in->s, in->len);
120 pvid = get_hash1_raw(in->s, in->len);
122 pvi = _pv_table[pvid%PV_TABLE_SIZE];
127 if(pvi->pve.name.len==in->len)
129 found = strncmp(pvi->pve.name.s, in->s, in->len);
133 LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
141 pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
144 LM_ERR("no more memory\n");
147 memset(pvn, 0, sizeof(pv_item_t));
148 memcpy(&(pvn->pve), e, sizeof(pv_export_t));
153 pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
154 _pv_table[pvid%PV_TABLE_SIZE] = pvn;
157 pvn->next = pvj->next;
167 int register_pvars_mod(char *mod_name, pv_export_t *items)
175 for ( i=0 ; items[i].name.s ; i++ ) {
176 ret = pv_table_add(&items[i]);
178 LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
179 items[i].name.len, items[i].name.s, mod_name);
188 int pv_table_free(void)
194 for(i=0; i<PV_TABLE_SIZE; i++)
204 memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
210 /********** helper functions ********/
212 * convert unsigned int to pv_value_t
214 int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
215 pv_value_t *res, unsigned int uival)
223 ch = int2str(uival, &l);
227 res->ri = (int)uival;
228 res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
233 * convert signed int to pv_value_t
235 int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
236 pv_value_t *res, int sival)
244 ch = sint2str(sival, &l);
249 res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
254 * convert str to pv_value_t
256 int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
257 pv_value_t *res, str *sval)
263 res->flags = PV_VAL_STR;
268 * convert str-int to pv_value_t (type is str)
270 int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
271 pv_value_t *res, str *sval, int ival)
278 res->flags = PV_VAL_STR|PV_VAL_INT;
283 * convert int-str to pv_value_t (type is int)
285 int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
286 pv_value_t *res, int ival, str *sval)
293 res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
297 /*** ============================= ***/
298 static str pv_str_marker = { PV_MARKER_STR, 1 };
299 static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
302 return pv_get_strintval(msg, param, res, &pv_str_marker,
303 (int)pv_str_marker.s[0]);
306 static str pv_str_empty = { "", 0 };
307 static str pv_str_null = { "<null>", 6 };
308 int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
313 res->rs = pv_str_empty;
315 res->flags = PV_VAL_NULL;
322 pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
327 if(pvname==0 || e==0)
329 LM_ERR("bad parameters\n");
333 /* search in PV table */
334 // pvid = get_hash1_raw(pvname->s, pvname->len);
335 pvid = get_hash1_raw(pvname->s, pvname->len);
336 pvi = _pv_table[pvid%PV_TABLE_SIZE];
342 if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
343 && memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
345 /*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
346 _pv_names_table[i].type);*/
347 /* copy data from table to spec */
348 e->type = pvi->pve.type;
349 e->getf = pvi->pve.getf;
350 e->setf = pvi->pve.setf;
362 int pv_parse_index(pv_spec_p sp, str *in)
369 if(in==NULL || in->s==NULL || sp==NULL)
374 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
377 LM_ERR("no more memory\n");
380 s = pv_parse_spec(in, nsp);
383 LM_ERR("invalid index [%.*s]\n", in->len, in->s);
387 sp->pvp.pvi.type = PV_IDX_PVAR;
388 sp->pvp.pvi.u.dval = (void*)nsp;
391 if(*p=='*' && in->len==1)
393 sp->pvp.pvi.type = PV_IDX_ALL;
402 sp->pvp.pvi.u.ival = 0;
403 while(p<in->s+in->len && *p>='0' && *p<='9')
405 sp->pvp.pvi.u.ival = sp->pvp.pvi.u.ival * 10 + *p - '0';
410 LM_ERR("invalid index [%.*s]\n", in->len, in->s);
413 sp->pvp.pvi.u.ival *= sign;
414 sp->pvp.pvi.type = PV_IDX_INT;
421 int pv_init_iname(pv_spec_p sp, int param)
425 sp->pvp.pvn.type = PV_NAME_INTSTR;
426 sp->pvp.pvn.u.isname.name.n = param;
433 char* pv_parse_spec2(str *in, pv_spec_p e, int silent)
440 pv_export_t *pte = NULL;
443 if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
445 if (!silent) LM_ERR("bad parameters\n");
449 /* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
452 memset(e, 0, sizeof(pv_spec_t));
461 if(*p == PV_MARKER) {
469 e->getf = pv_get_marker;
470 e->type = PVT_MARKER;
474 while(is_in_str(p,in) && is_pv_valid_char(*p))
476 pvname.len = p - pvname.s;
480 { /* full pv name ended here*/
482 } else if(*p==PV_LNBRACKET) {
485 } else if(*p==PV_LIBRACKET) {
488 } else if(*p==TR_LBRACKET) {
493 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
494 *p, in->len, in->s, pvstate);
498 if(!is_in_str(p, in)) {
501 } else if(*p==PV_LNBRACKET) {
505 /* still in input str, but end of PV */
506 /* p is increased at the end, so decrement here */
513 if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
516 LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
519 if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
522 LM_ERR("pvar \"%.*s\" expects an inner name\n",
523 pvname.len, pvname.s);
526 if(pvstate==2 || pvstate==5)
528 if(pte->parse_name==NULL)
531 LM_ERR("pvar \"%.*s\" does not get name param\n",
532 pvname.len, pvname.s);
537 while(is_in_str(p, in))
545 if(*p == PV_LNBRACKET)
550 if(!is_in_str(p, in))
556 LM_ERR("pvar \"%.*s\" does not get empty name param\n",
557 pvname.len, pvname.s);
561 if(pte->parse_name(e, &s)!=0)
564 LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
565 pvname.len, pvname.s, s.len, s.s);
572 { /* full pv name ended here*/
574 } else if(*p==PV_LIBRACKET) {
577 } else if(*p==TR_LBRACKET) {
582 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
583 *p, in->len, in->s, pvstate);
588 { /* full pv name ended here*/
593 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
594 *p, in->len, in->s, pvstate);
602 if(pte->parse_index==NULL)
605 LM_ERR("pvar \"%.*s\" does not get index param\n",
606 pvname.len, pvname.s);
611 while(is_in_str(p, in))
619 if(*p == PV_LIBRACKET)
623 if(!is_in_str(p, in))
629 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
630 pvname.len, pvname.s);
634 if(pte->parse_index(e, &s)!=0)
637 LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
638 pvname.len, pvname.s, s.len, s.s);
643 { /* full pv name ended here*/
645 } else if(*p==TR_LBRACKET) {
650 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
651 *p, in->len, in->s, pvstate);
660 while(is_in_str(p, in))
666 /* yet another transformation */
668 while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
670 if(!is_in_str(p, in) || *p != TR_LBRACKET)
678 if(*p == TR_LBRACKET)
682 if(!is_in_str(p, in))
688 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
689 pvname.len, pvname.s);
694 p = tr_lookup(&s, &tr);
698 LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
704 LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
707 e->trans = (void*)tr;
712 if(pte!=NULL && pte->init_param)
713 pte->init_param(e, pte->iparam);
719 LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
720 in->len, in->s, (int)(p-in->s), pvstate);
723 LM_ERR("invalid parsing in [%.*s] at (%d)\n",
724 in->len, in->s, pvstate);
728 } /* end: pv_parse_spec */
733 int pv_parse_format(str *in, pv_elem_p *el)
740 if(in==NULL || in->s==NULL || el==NULL)
743 /*LM_DBG("parsing [%.*s]\n", in->len, in->s);*/
747 *el = pkg_malloc(sizeof(pv_elem_t));
750 memset(*el, 0, sizeof(pv_elem_t));
759 while(is_in_str(p,in))
762 e = pkg_malloc(sizeof(pv_elem_t));
765 memset(e, 0, sizeof(pv_elem_t));
773 while(is_in_str(p,in) && *p!=PV_MARKER)
775 e->text.len = p - e->text.s;
777 if(*p == '\0' || !is_in_str(p,in))
780 s.len = in->s+in->len-p;
781 p0 = pv_parse_spec(&s, &e->spec);
789 /*LM_DBG("format parsed OK: [%d] items\n", n);*/
797 pv_elem_free_all(*el);
805 int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name)
807 if(msg==NULL || ip==NULL || name==NULL)
809 memset(name, 0, sizeof(pv_value_t));
811 if(ip->pvn.type==PV_NAME_INTSTR)
813 if(ip->pvn.u.isname.type&AVP_NAME_STR)
815 name->rs = ip->pvn.u.isname.name.s;
816 name->flags = PV_VAL_STR;
818 name->ri = ip->pvn.u.isname.name.n;
819 name->flags = PV_VAL_INT|PV_TYPE_INT;
822 } else if(ip->pvn.type==PV_NAME_PVAR) {
824 if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), name)!=0)
826 LM_ERR("cannot get name value\n");
829 if(name->flags&PV_VAL_NULL || name->flags&PV_VAL_EMPTY)
831 LM_ERR("null or empty name\n");
836 LM_ERR("name type is PV_NAME_OTHER - cannot resolve\n");
842 * @return 0 on success, -1 on error
844 int pv_parse_avp_name(pv_spec_p sp, str *in)
850 if(in==NULL || in->s==NULL || sp==NULL)
855 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
858 LM_ERR("no more memory\n");
861 s = pv_parse_spec(in, nsp);
864 LM_ERR("invalid name [%.*s]\n", in->len, in->s);
868 //LM_ERR("dynamic name [%.*s]\n", in->len, in->s);
869 //pv_print_spec(nsp);
870 sp->pvp.pvn.type = PV_NAME_PVAR;
871 sp->pvp.pvn.u.dname = (void*)nsp;
874 /*LM_DBG("static name [%.*s]\n", in->len, in->s);*/
875 if(km_parse_avp_spec(in, &sp->pvp.pvn.u.isname.type,
876 &sp->pvp.pvn.u.isname.name)!=0)
878 LM_ERR("bad avp name [%.*s]\n", in->len, in->s);
881 sp->pvp.pvn.type = PV_NAME_INTSTR;
886 * fill avp name details (id and type)
887 * @return 0 on success, -1 on error
889 int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
890 unsigned short *name_type)
893 if(ip==NULL || avp_name==NULL || name_type==NULL)
895 memset(avp_name, 0, sizeof(int_str));
898 if(ip->pvn.type==PV_NAME_INTSTR)
900 *name_type = ip->pvn.u.isname.type;
901 if(ip->pvn.u.isname.type&AVP_NAME_STR)
903 avp_name->s = ip->pvn.u.isname.name.s;
904 *name_type |= AVP_NAME_STR;
906 avp_name->n = ip->pvn.u.isname.name.n;
907 /* *name_type &= AVP_SCRIPT_MASK; */
913 if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
915 LM_ERR("cannot get avp value\n");
918 if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
920 LM_ERR("null or empty name\n");
924 if((tv.flags&PV_TYPE_INT) && (tv.flags&PV_VAL_INT))
929 *name_type = AVP_NAME_STR;
937 int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags)
940 if(ip==NULL || idx==NULL || flags==NULL)
946 if(ip->pvi.type == PV_IDX_ALL) {
951 if(ip->pvi.type == PV_IDX_INT)
953 *idx = ip->pvi.u.ival;
958 if(pv_get_spec_value(msg, (pv_spec_p)ip->pvi.u.dval, &tv)!=0)
960 LM_ERR("cannot get index value\n");
963 if(!(tv.flags&PV_VAL_INT))
965 LM_ERR("invalid index value\n");
975 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value)
979 if(msg==NULL || sp==NULL || sp->getf==NULL || value==NULL
980 || sp->type==PVT_NONE)
982 LM_ERR("bad parameters\n");
986 memset(value, 0, sizeof(pv_value_t));
988 ret = (*sp->getf)(msg, &(sp->pvp), value);
993 return tr_exec(msg, (trans_t*)sp->trans, value);
1000 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
1003 if(sp==NULL || !pv_is_w(sp))
1004 return 0; /* no op */
1005 if(pv_alter_context(sp) && is_route_type(LOCAL_ROUTE))
1006 return 0; /* no op */
1007 return sp->setf(msg, &sp->pvp, op, value);
1013 int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1020 if(msg==NULL || list==NULL || buf==NULL || len==NULL)
1031 for (it=list; it; it=it->next)
1034 if(it->text.s && it->text.len>0)
1036 if(n+it->text.len < *len)
1038 memcpy(cur, it->text.s, it->text.len);
1040 cur += it->text.len;
1042 LM_ERR("no more space for text [%d]\n", it->text.len);
1046 /* put the value of the specifier */
1047 if(it->spec.type!=PVT_NONE
1048 && pv_get_spec_value(msg, &(it->spec), &tok)==0)
1050 if(tok.flags&PV_VAL_NULL)
1051 tok.rs = pv_str_null;
1052 if(n+tok.rs.len < *len)
1056 memcpy(cur, tok.rs.s, tok.rs.len);
1061 LM_ERR("no more space for spec value\n");
1070 LM_ERR("buffer overflow -- increase the buffer size...\n");
1075 LM_DBG("final buffer length %d\n", n);
1085 pvname_list_t* parse_pvname_list(str *in, unsigned int type)
1087 pvname_list_t* head = NULL;
1088 pvname_list_t* al = NULL;
1089 pvname_list_t* last = NULL;
1094 if(in==NULL || in->s==NULL)
1096 LM_ERR("bad parameters\n");
1101 while(is_in_str(p, in))
1103 while(is_in_str(p, in) && (*p==' '||*p=='\t'||*p==','||*p==';'))
1105 if(!is_in_str(p, in))
1108 LM_ERR("wrong item name list [%.*s]\n", in->len, in->s);
1112 s.len = in->s+in->len-p;
1113 p = pv_parse_spec(&s, &spec);
1114 if(p==NULL || (type && spec.type!=type))
1116 LM_ERR("wrong item name list [%.*s]!\n", in->len, in->s);
1119 al = (pvname_list_t*)pkg_malloc(sizeof(pvname_list_t));
1122 LM_ERR("no more memory!\n");
1125 memset(al, 0, sizeof(pvname_list_t));
1126 memcpy(&al->sname, &spec, sizeof(pv_spec_t));
1152 /** destroy the content of pv_spec_t structure.
1154 void pv_spec_destroy(pv_spec_t *spec)
1157 /* free name if it is PV */
1158 if(spec->pvp.pvn.nfree)
1159 spec->pvp.pvn.nfree((void*)(&spec->pvp.pvn));
1161 tr_free((trans_t*)spec->trans);
1164 /** free the pv_spec_t structure.
1166 void pv_spec_free(pv_spec_t *spec)
1169 pv_spec_destroy(spec);
1176 int pv_elem_free_all(pv_elem_p log)
1191 void pv_value_destroy(pv_value_t *val)
1194 if(val->flags&PV_VAL_PKG) pkg_free(val->rs.s);
1195 if(val->flags&PV_VAL_SHM) shm_free(val->rs.s);
1196 memset(val, 0, sizeof(pv_value_t));
1199 #define PV_PRINT_BUF_SIZE 1024
1200 #define PV_PRINT_BUF_NO 3
1201 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
1203 static int buf_itr = 0;
1204 static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
1206 s->s = buf[buf_itr];
1207 s->len = PV_PRINT_BUF_SIZE;
1208 buf_itr = (buf_itr+1)%PV_PRINT_BUF_NO;
1209 return pv_printf( msg, list, s->s, &s->len);
1212 /********************************************************
1213 * Transformations API
1214 ********************************************************/
1219 static inline char* tr_get_class(str *in, char *p, str *tclass)
1222 while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
1223 if(*p!=TR_CLASS_MARKER || tclass->s == p)
1225 LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
1228 tclass->len = p - tclass->s;
1237 static inline trans_t* tr_new(void)
1241 t = (trans_t*)pkg_malloc(sizeof(trans_t));
1244 LM_ERR("no more private memory\n");
1247 memset(t, 0, sizeof(trans_t));
1251 char* tr_lookup(str *in, trans_t **tr)
1256 tr_export_t *te = NULL;
1261 if(in==NULL || in->s==NULL || tr==NULL)
1266 while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
1267 if(*p != TR_LBRACKET)
1271 if((t = tr_new())==NULL) return NULL;
1273 if(t0==NULL) *tr = t;
1277 /* find transformation class */
1278 p = tr_get_class(in, p, &tclass);
1279 if(p==NULL) goto error;
1281 /* locate transformation */
1282 te = tr_lookup_class(&tclass);
1285 LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
1286 tclass.len, tclass.s, in->len, in->s);
1290 s.s = p; s.len = in->s + in->len - p;
1291 p0 = te->tparse(&s, t);
1296 if(*p != TR_RBRACKET)
1298 LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
1304 if(!is_in_str(p, in))
1310 LM_ERR("error parsing [%.*s]\n", in->len, in->s);
1323 * \brief Destroy transformation including eventual parameter
1324 * \param t transformation
1326 void tr_destroy(trans_t *t)
1339 memset(t, 0, sizeof(trans_t));
1343 * \brief Exec transformation on a pseudo-variable value
1344 * \param msg SIP message
1345 * \param tr one or more transformations
1346 * \param val pseudo-variable value
1347 * \return 0 on success, -1 on error
1350 int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v)
1355 if(t==NULL || v==NULL)
1357 LM_DBG("invalid parameters\n");
1361 for(i = t; i!=NULL; i=i->next)
1363 r = (*i->trf)(msg, i->params, i->subtype, v);
1371 * \brief Free allocated memory of transformation list
1372 * \param t transformation list
1374 void tr_free(trans_t *t)
1389 * \brief Free transformation parameter list
1390 * \param tp transformation list
1392 void tr_param_free(tr_param_t *tp)
1396 if(tp==NULL) return;
1401 if(tp0->type==TR_PARAM_SPEC)
1402 pv_spec_free((pv_spec_t*)tp0->v.data);
1407 typedef struct _tr_item
1411 struct _tr_item *next;
1412 } tr_item_t, *tr_item_p;
1414 static tr_item_t* _tr_table[TR_TABLE_SIZE];
1415 static int _tr_table_set = 0;
1421 void tr_init_table(void)
1423 memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1430 int tr_table_add(tr_export_t *e)
1432 tr_item_t *tri = NULL;
1433 tr_item_t *trj = NULL;
1434 tr_item_t *trn = NULL;
1438 if(e==NULL || e->tclass.s==NULL)
1440 LM_ERR("invalid parameters\n");
1444 if(_tr_table_set==0)
1446 LM_DBG("TR table not initialized, doing it now\n");
1451 // trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1452 trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1454 tri = _tr_table[trid%TR_TABLE_SIZE];
1457 if(tri->tre.tclass.len==e->tclass.len)
1459 found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
1462 LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
1471 trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
1474 LM_ERR("no more memory\n");
1477 memset(trn, 0, sizeof(tr_item_t));
1478 memcpy(&(trn->tre), e, sizeof(tr_export_t));
1481 //LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
1482 // e->tclass.s, trid%TR_TABLE_SIZE);
1485 trn->next = _tr_table[trid%TR_TABLE_SIZE];
1486 _tr_table[trid%TR_TABLE_SIZE] = trn;
1489 trn->next = trj->next;
1499 int register_trans_mod(char *mod_name, tr_export_t *items)
1507 for ( i=0 ; items[i].tclass.s ; i++ ) {
1508 ret = tr_table_add(&items[i]);
1510 LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
1511 items[i].tclass.len, items[i].tclass.s, mod_name);
1520 int tr_table_free(void)
1526 for(i=0; i<TR_TABLE_SIZE; i++)
1536 memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1545 tr_export_t* tr_lookup_class(str *tclass)
1550 if(tclass==0 || tclass->s==0)
1552 LM_ERR("bad parameters\n");
1556 /* search in TR table */
1557 // trid = get_hash1_raw(tclass->s, tclass->len);
1558 trid = get_hash1_raw(tclass->s, tclass->len);
1559 tri = _tr_table[trid%TR_TABLE_SIZE];
1562 if(tri->trid==trid && tri->tre.tclass.len==tclass->len
1563 && memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
1572 /********************************************************
1573 * core PVs, initialization and destroy APIs
1574 ********************************************************/
1576 static pv_export_t _core_pvs[] = {
1577 {{"null", (sizeof("null")-1)}, /* */
1578 PVT_NULL, pv_get_null, 0,
1581 { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
1584 /** init pv api (optional).
1585 * @return 0 on success, -1 on error
1587 int init_pv_api(void)
1591 if(register_pvars_mod("core", _core_pvs)<0)
1597 /** destroy pv api. */
1598 void destroy_pv_api(void)
1600 /* free PV and TR hash tables */