core: pvapi: added pv_spec_free_contents()
[sip-router] / pvapi.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of SIP-Router, a free SIP server.
7  *
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
12  *
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 SIP-router core ::  PV API specification
26  * \ingroup core
27  * Module: \ref core
28  */
29
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "mem/mem.h"
37 #include "mem/shm_mem.h"
38 #include "ut.h"
39 #include "dprint.h"
40 #include "hashes.h"
41 #include "route.h"
42 #include "pvapi_init.h"
43 #include "pvar.h"
44
45 #define is_in_str(p, in) (p<in->s+in->len && *p)
46
47 #define PV_TABLE_SIZE   16
48 #define TR_TABLE_SIZE   4
49
50
51 void tr_destroy(trans_t *t);
52 void tr_free(trans_t *t);
53
54 typedef struct _pv_item
55 {
56         pv_export_t pve;
57         unsigned int pvid;
58         struct _pv_item *next;
59 } pv_item_t, *pv_item_p;
60
61 static pv_item_t* _pv_table[PV_TABLE_SIZE];
62 static int _pv_table_set = 0;
63
64
65 /**
66  *
67  */
68 void pv_init_table(void)
69 {
70         memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
71         _pv_table_set = 1;
72 }
73
74 /**
75  *
76  */
77 static int is_pv_valid_char(char c)
78 {
79         if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
80                         || (c=='_') || (c=='.') || (c=='?') /* ser $? */)
81                 return 1;
82         return 0;
83 }
84
85 /**
86  *
87  */
88 int pv_table_add(pv_export_t *e)
89 {
90         char *p;
91         str  *in;
92         pv_item_t *pvi = NULL;
93         pv_item_t *pvj = NULL;
94         pv_item_t *pvn = NULL;
95         int found;
96         unsigned int pvid;
97
98         if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
99         {
100                 LM_ERR("invalid parameters\n");
101                 return -1;
102         }
103         
104         if(_pv_table_set==0)
105         {
106                 LM_DBG("PV table not initialized, doing it now\n");
107                 pv_init_table();
108         }
109         in = &(e->name);
110         p = in->s;      
111         while(is_in_str(p,in) && is_pv_valid_char(*p))
112                 p++;
113         if(is_in_str(p,in))
114         {
115                 LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
116                 return -1;
117         }
118         found = 0;
119         //pvid = get_hash1_raw(in->s, in->len);
120         pvid = get_hash1_raw(in->s, in->len);
121
122         pvi = _pv_table[pvid%PV_TABLE_SIZE];
123         while(pvi)
124         {
125                 if(pvi->pvid > pvid)
126                         break;
127                 if(pvi->pve.name.len==in->len)
128                 {
129                         found = strncmp(pvi->pve.name.s, in->s, in->len);
130
131                         if(found==0)
132                         {
133                                 LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
134                                 return -1;
135                         }
136                 }
137                 pvj = pvi;
138                 pvi = pvi->next;
139         }
140
141         pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
142         if(pvn==0)
143         {
144                 LM_ERR("no more memory\n");
145                 return -1;
146         }
147         memset(pvn, 0, sizeof(pv_item_t));
148         memcpy(&(pvn->pve), e, sizeof(pv_export_t));
149         pvn->pvid = pvid;
150
151         if(pvj==0)
152         {
153                 pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
154                 _pv_table[pvid%PV_TABLE_SIZE] = pvn;
155                 goto done;
156         }
157         pvn->next = pvj->next;
158         pvj->next = pvn;
159
160 done:
161         return 0;
162 }
163
164 /**
165  *
166  */
167 int register_pvars_mod(char *mod_name, pv_export_t *items)
168 {
169         int ret;
170         int i;
171
172         if (items==0)
173                 return 0;
174
175         for ( i=0 ; items[i].name.s ; i++ ) {
176                 ret = pv_table_add(&items[i]);
177                 if (ret!=0) {
178                         LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
179                                         items[i].name.len, items[i].name.s, mod_name);
180                 }
181         }
182         return 0;
183 }
184
185 /**
186  *
187  */
188 int pv_table_free(void)
189 {
190         pv_item_p xe;
191         pv_item_p xe1;
192         int i;
193
194         for(i=0; i<PV_TABLE_SIZE; i++)
195         {
196                 xe = _pv_table[i];
197                 while(xe!=0)
198                 {
199                         xe1 = xe;
200                         xe = xe->next;
201                         pkg_free(xe1);
202                 }
203         }
204         memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
205         _pv_table_set = 0;
206         
207         return 0;
208 }
209
210 /********** helper functions ********/
211 /**
212  * convert unsigned int to pv_value_t
213  */
214 int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
215                 pv_value_t *res, unsigned int uival)
216 {
217         int l = 0;
218         char *ch = NULL;
219
220         if(res==NULL)
221                 return -1;
222
223         ch = int2str(uival, &l);
224         res->rs.s = ch;
225         res->rs.len = l;
226
227         res->ri = (int)uival;
228         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
229         return 0;
230 }
231
232 /**
233  * convert signed int to pv_value_t
234  */
235 int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
236                 pv_value_t *res, int sival)
237 {
238         int l = 0;
239         char *ch = NULL;
240
241         if(res==NULL)
242                 return -1;
243
244         ch = sint2str(sival, &l);
245         res->rs.s = ch;
246         res->rs.len = l;
247
248         res->ri = sival;
249         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
250         return 0;
251 }
252
253 /**
254  * convert str to pv_value_t
255  */
256 int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
257                 pv_value_t *res, str *sval)
258 {
259         if(res==NULL)
260                 return -1;
261
262         res->rs = *sval;
263         res->flags = PV_VAL_STR;
264         return 0;
265 }
266
267 /**
268  * convert str-int to pv_value_t (type is str)
269  */
270 int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
271                 pv_value_t *res, str *sval, int ival)
272 {
273         if(res==NULL)
274                 return -1;
275
276         res->rs = *sval;
277         res->ri = ival;
278         res->flags = PV_VAL_STR|PV_VAL_INT;
279         return 0;
280 }
281
282 /**
283  * convert int-str to pv_value_t (type is int)
284  */
285 int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
286                 pv_value_t *res, int ival, str *sval)
287 {
288         if(res==NULL)
289                 return -1;
290
291         res->rs = *sval;
292         res->ri = ival;
293         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
294         return 0;
295 }
296
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,
300                 pv_value_t *res)
301 {
302         return pv_get_strintval(msg, param, res, &pv_str_marker,
303                         (int)pv_str_marker.s[0]);
304 }
305
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)
309 {
310         if(res==NULL)
311                 return -1;
312         
313         res->rs = pv_str_empty;
314         res->ri = 0;
315         res->flags = PV_VAL_NULL;
316         return 0;
317 }
318
319 pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
320 {
321         pv_item_t *pvi;
322         unsigned int pvid;
323
324         if(pvname==0 || e==0)
325         {
326                 LM_ERR("bad parameters\n");
327                 return NULL;
328         }
329
330         /* search in PV table */
331         // pvid = get_hash1_raw(pvname->s, pvname->len);
332         pvid = get_hash1_raw(pvname->s, pvname->len);
333         pvi = _pv_table[pvid%PV_TABLE_SIZE];
334         while(pvi)
335         {
336                 if(pvi->pvid > pvid)
337                         break;
338
339                 if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
340                         && memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
341                 {
342                         /*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
343                                         _pv_names_table[i].type);*/
344                         /* copy data from table to spec */
345                         e->type = pvi->pve.type;
346                         e->getf = pvi->pve.getf;
347                         e->setf = pvi->pve.setf;
348                         return &(pvi->pve);
349                 }
350                 pvi = pvi->next;
351         }
352
353         return NULL;
354 }
355
356 int pv_parse_index(pv_spec_p sp, str *in)
357 {
358         char *p;
359         char *s;
360         int sign;
361         pv_spec_p nsp = 0;
362
363         if(in==NULL || in->s==NULL || sp==NULL)
364                 return -1;
365         p = in->s;
366         if(*p==PV_MARKER)
367         {
368                 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
369                 if(nsp==NULL)
370                 {
371                         LM_ERR("no more memory\n");
372                         return -1;
373                 }
374                 s = pv_parse_spec(in, nsp);
375                 if(s==NULL)
376                 {
377                         LM_ERR("invalid index [%.*s]\n", in->len, in->s);
378                         pv_spec_free(nsp);
379                         return -1;
380                 }
381                 sp->pvp.pvi.type = PV_IDX_PVAR;
382                 sp->pvp.pvi.u.dval = (void*)nsp;
383                 return 0;
384         }
385         if(*p=='*' && in->len==1)
386         {
387                 sp->pvp.pvi.type = PV_IDX_ALL;
388                 return 0;
389         }
390         sign = 1;
391         if(*p=='-')
392         {
393                 sign = -1;
394                 p++;
395         }
396         sp->pvp.pvi.u.ival = 0;
397         while(p<in->s+in->len && *p>='0' && *p<='9')
398         {
399                 sp->pvp.pvi.u.ival = sp->pvp.pvi.u.ival * 10 + *p - '0';
400                 p++;
401         }
402         if(p!=in->s+in->len)
403         {
404                 LM_ERR("invalid index [%.*s]\n", in->len, in->s);
405                 return -1;
406         }
407         sp->pvp.pvi.u.ival *= sign;
408         sp->pvp.pvi.type = PV_IDX_INT;
409         return 0;
410 }
411
412 int pv_init_iname(pv_spec_p sp, int param)
413 {
414         if(sp==NULL)
415                 return -1;
416         sp->pvp.pvn.type = PV_NAME_INTSTR;
417         sp->pvp.pvn.u.isname.name.n = param;
418         return 0;
419 }
420
421 char* pv_parse_spec2(str *in, pv_spec_p e, int silent)
422 {
423         char *p;
424         str s;
425         str pvname;
426         int pvstate;
427         trans_t *tr = NULL;
428         pv_export_t *pte = NULL;
429         int n=0;
430
431         if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
432         {
433                 if (!silent) LM_ERR("bad parameters\n");
434                 return NULL;
435         }
436         
437         /* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
438         tr = 0;
439         pvstate = 0;
440         memset(e, 0, sizeof(pv_spec_t));
441         p = in->s;
442         p++;
443         if(*p==PV_LNBRACKET)
444         {
445                 p++;
446                 pvstate = 1;
447         }
448         pvname.s = p;
449         if(*p == PV_MARKER) {
450                 p++;
451                 if(pvstate==1)
452                 {
453                         if(*p!=PV_RNBRACKET)
454                                 goto error;
455                         p++;
456                 }
457                 e->getf = pv_get_marker;
458                 e->type = PVT_MARKER;
459                 pvname.len = 1;
460                 goto done_all;
461         }
462         while(is_in_str(p,in) && is_pv_valid_char(*p))
463                 p++;
464         pvname.len = p - pvname.s;
465         if(pvstate==1)
466         {
467                 if(*p==PV_RNBRACKET)
468                 { /* full pv name ended here*/
469                         goto done_inm;
470                 } else if(*p==PV_LNBRACKET) {
471                         p++;
472                         pvstate = 2;
473                 } else if(*p==PV_LIBRACKET) {
474                         p++;
475                         pvstate = 3;
476                 } else if(*p==TR_LBRACKET) {
477                         p++;
478                         pvstate = 4;
479                 } else {
480                         if (!silent)
481                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
482                                                         *p, in->len, in->s, pvstate);
483                         goto error;
484                 }
485         } else { 
486                 if(!is_in_str(p, in)) {
487                         p--;
488                         goto done_inm;
489                 } else if(*p==PV_LNBRACKET) {
490                         p++;
491                         pvstate = 5;
492                 } else {
493                         /* still in input str, but end of PV */
494                         /* p is increased at the end, so decrement here */
495                         p--;
496                         goto done_inm;
497                 }
498         }
499
500 done_inm:
501         if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
502         {
503                 if (!silent) 
504                         LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
505                 goto error;
506         }
507         if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
508         {
509                 if (!silent) 
510                         LM_ERR("pvar \"%.*s\" expects an inner name\n",
511                                                 pvname.len, pvname.s);
512                 goto error;
513         }
514         if(pvstate==2 || pvstate==5)
515         {
516                 if(pte->parse_name==NULL)
517                 {
518                         if (!silent)
519                                 LM_ERR("pvar \"%.*s\" does not get name param\n",
520                                                 pvname.len, pvname.s);
521                         goto error;
522                 }
523                 s.s = p;
524                 n = 0;
525                 while(is_in_str(p, in))
526                 {
527                         if(*p==PV_RNBRACKET)
528                         {
529                                 if(n==0)
530                                         break;
531                                 n--;
532                         }
533                         if(*p == PV_LNBRACKET)
534                                 n++;
535                         p++;
536                 }
537
538                 if(!is_in_str(p, in))
539                         goto error;
540
541                 if(p==s.s)
542                 {
543                         if (!silent)
544                                 LM_ERR("pvar \"%.*s\" does not get empty name param\n",
545                                                 pvname.len, pvname.s);
546                         goto error;
547                 }
548                 s.len = p - s.s;
549                 if(pte->parse_name(e, &s)!=0)
550                 {
551                         if (!silent)
552                                 LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
553                                                 pvname.len, pvname.s, s.len, s.s);
554                         goto error;
555                 }
556                 if(pvstate==2)
557                 {
558                         p++;
559                         if(*p==PV_RNBRACKET)
560                         { /* full pv name ended here*/
561                                 goto done_vnm;
562                         } else if(*p==PV_LIBRACKET) {
563                                 p++;
564                                 pvstate = 3;
565                         } else if(*p==TR_LBRACKET) {
566                                 p++;
567                                 pvstate = 4;
568                         } else {
569                                 if (!silent)
570                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
571                                                                 *p, in->len, in->s, pvstate);
572                                 goto error;
573                         }
574                 } else {
575                         if(*p==PV_RNBRACKET)
576                         { /* full pv name ended here*/
577                                 p++;
578                                 goto done_all;
579                         } else {
580                                 if (!silent)
581                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
582                                                                 *p, in->len, in->s, pvstate);
583                                 goto error;
584                         }
585                 }
586         }
587 done_vnm:
588         if(pvstate==3)
589         {
590                 if(pte->parse_index==NULL)
591                 {
592                         if (!silent)
593                                 LM_ERR("pvar \"%.*s\" does not get index param\n",
594                                                 pvname.len, pvname.s);
595                         goto error;
596                 }
597                 s.s = p;
598                 n = 0;
599                 while(is_in_str(p, in))
600                 {
601                         if(*p==PV_RIBRACKET)
602                         {
603                                 if(n==0)
604                                         break;
605                                 n--;
606                         }
607                         if(*p == PV_LIBRACKET)
608                                 n++;
609                         p++;
610                 }
611                 if(!is_in_str(p, in))
612                         goto error;
613
614                 if(p==s.s)
615                 {
616                         if (!silent)
617                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
618                                                 pvname.len, pvname.s);
619                         goto error;
620                 }
621                 s.len = p - s.s;
622                 if(pte->parse_index(e, &s)!=0)
623                 {
624                         if (!silent)
625                                 LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
626                                                 pvname.len, pvname.s, s.len, s.s);
627                         goto error;
628                 }
629                 p++;
630                 if(*p==PV_RNBRACKET)
631                 { /* full pv name ended here*/
632                         goto done_idx;
633                 } else if(*p==TR_LBRACKET) {
634                         p++;
635                         pvstate = 4;
636                 } else {
637                         if (!silent)
638                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
639                                                         *p, in->len, in->s, pvstate);
640                         goto error;
641                 }
642         }
643 done_idx:
644         if(pvstate==4)
645         {
646                 s.s = p-1;
647                 n = 0;
648                 while(is_in_str(p, in))
649                 {
650                         if(*p==TR_RBRACKET)
651                         {
652                                 if(n==0)
653                                 {
654                                         /* yet another transformation */
655                                         p++;
656                                         while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
657
658                                         if(!is_in_str(p, in) || *p != TR_LBRACKET)
659                                         {
660                                                 p--;
661                                                 break;
662                                         }
663                                 }
664                                 n--;
665                         }
666                         if(*p == TR_LBRACKET)
667                                 n++;
668                         p++;
669                 }
670                 if(!is_in_str(p, in))
671                         goto error;
672
673                 if(p==s.s)
674                 {
675                         if (!silent)
676                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
677                                                 pvname.len, pvname.s);
678                         goto error;
679                 }
680                 s.len = p - s.s + 1;
681
682                 p = tr_lookup(&s, &tr);
683                 if(p==NULL)
684                 {
685                         if (!silent)
686                                 LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
687                         goto error;
688                 }
689                 if(*p!=PV_RNBRACKET)
690                 {
691                         if (!silent)
692                                 LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
693                         goto error;
694                 }
695                 e->trans = (void*)tr;
696         }
697         p++;
698
699 done_all:
700         if(pte!=NULL && pte->init_param)
701                 pte->init_param(e, pte->iparam);
702         return p;
703
704 error:
705         if(p!=NULL){
706                 if (!silent)
707                         LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
708                                         in->len, in->s, (int)(p-in->s), pvstate);
709         }else{
710                 if (!silent)
711                         LM_ERR("invalid parsing in [%.*s] at (%d)\n",
712                                                 in->len, in->s, pvstate);
713         }
714         return NULL;
715
716 } /* end: pv_parse_spec */
717
718 /**
719  *
720  */
721 int pv_parse_format(str *in, pv_elem_p *el)
722 {
723         char *p, *p0;
724         int n = 0;
725         pv_elem_p e, e0;
726         str s;
727
728         if(in==NULL || in->s==NULL || el==NULL)
729                 return -1;
730
731         /*LM_DBG("parsing [%.*s]\n", in->len, in->s);*/
732         
733         if(in->len == 0)
734         {
735                 *el = pkg_malloc(sizeof(pv_elem_t));
736                 if(*el == NULL)
737                         goto error;
738                 memset(*el, 0, sizeof(pv_elem_t));
739                 (*el)->text = *in;
740                 return 0;
741         }
742
743         p = in->s;
744         *el = NULL;
745         e = e0 = NULL;
746
747         while(is_in_str(p,in))
748         {
749                 e0 = e;
750                 e = pkg_malloc(sizeof(pv_elem_t));
751                 if(!e)
752                         goto error;
753                 memset(e, 0, sizeof(pv_elem_t));
754                 n++;
755                 if(*el == NULL)
756                         *el = e;
757                 if(e0)
758                         e0->next = e;
759         
760                 e->text.s = p;
761                 while(is_in_str(p,in) && *p!=PV_MARKER)
762                         p++;
763                 e->text.len = p - e->text.s;
764                 
765                 if(*p == '\0' || !is_in_str(p,in))
766                         break;
767                 s.s = p;
768                 s.len = in->s+in->len-p;
769                 p0 = pv_parse_spec(&s, &e->spec);
770                 
771                 if(p0==NULL)
772                         goto error;
773                 if(*p0 == '\0')
774                         break;
775                 p = p0;
776         }
777         /*LM_DBG("format parsed OK: [%d] items\n", n);*/
778
779         if(*el == NULL)
780                 return -1;
781
782         return 0;
783
784 error:
785         pv_elem_free_all(*el);
786         *el = NULL;
787         return -1;
788 }
789
790 int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name)
791 {
792         if(msg==NULL || ip==NULL || name==NULL)
793                 return -1;
794         memset(name, 0, sizeof(pv_value_t));
795
796         if(ip->pvn.type==PV_NAME_INTSTR)
797         {
798                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
799                 {
800                         name->rs = ip->pvn.u.isname.name.s;
801                         name->flags = PV_VAL_STR;
802                 } else {
803                         name->ri = ip->pvn.u.isname.name.n;
804                         name->flags = PV_VAL_INT|PV_TYPE_INT;
805                 }
806                 return 0;
807         } else if(ip->pvn.type==PV_NAME_PVAR) {
808                 /* pvar */
809                 if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), name)!=0)
810                 {
811                         LM_ERR("cannot get name value\n");
812                         return -1;
813                 }
814                 if(name->flags&PV_VAL_NULL || name->flags&PV_VAL_EMPTY)
815                 {
816                         LM_ERR("null or empty name\n");
817                         return -1;
818                 }
819                 return 0;
820         }
821         LM_ERR("name type is PV_NAME_OTHER - cannot resolve\n");
822         return -1;
823 }
824
825 int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
826                 unsigned short *name_type)
827 {
828         pv_value_t tv;
829         if(ip==NULL || avp_name==NULL || name_type==NULL)
830                 return -1;
831         memset(avp_name, 0, sizeof(int_str));
832         *name_type = 0;
833
834         if(ip->pvn.type==PV_NAME_INTSTR)
835         {
836                 *name_type = ip->pvn.u.isname.type;
837                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
838                 {
839                         avp_name->s = ip->pvn.u.isname.name.s;
840                         *name_type |= AVP_NAME_STR;
841                 } else {
842                         avp_name->n = ip->pvn.u.isname.name.n;
843                         /* *name_type &= AVP_SCRIPT_MASK; */
844                         *name_type = 0;
845                 }
846                 return 0;
847         }
848         /* pvar */
849         if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
850         {
851                 LM_ERR("cannot get avp value\n");
852                 return -1;
853         }
854         if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
855         {
856                 LM_ERR("null or empty name\n");
857                 return -1;
858         }
859                 
860         if((tv.flags&PV_TYPE_INT) && (tv.flags&PV_VAL_INT))
861         {
862                 avp_name->n = tv.ri;
863         } else {
864                 avp_name->s = tv.rs;
865                 *name_type = AVP_NAME_STR;
866         }
867         return 0;
868 }
869
870
871 int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags)
872 {
873         pv_value_t tv;
874         if(ip==NULL || idx==NULL || flags==NULL)
875                 return -1;
876
877         *idx = 0;
878         *flags = 0;
879
880         if(ip->pvi.type == PV_IDX_ALL) {
881                 *flags = PV_IDX_ALL;
882                 return 0;
883         }
884         
885         if(ip->pvi.type == PV_IDX_INT)
886         {
887                 *idx = ip->pvi.u.ival;
888                 return 0;
889         }
890
891         /* pvar */
892         if(pv_get_spec_value(msg, (pv_spec_p)ip->pvi.u.dval, &tv)!=0)
893         {
894                 LM_ERR("cannot get index value\n");
895                 return -1;
896         }
897         if(!(tv.flags&PV_VAL_INT))
898         {
899                 LM_ERR("invalid index value\n");
900                 return -1;
901         }
902         *idx = tv.ri;
903         return 0;
904 }
905
906 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value)
907 {
908         int ret = 0;
909
910         if(msg==NULL || sp==NULL || sp->getf==NULL || value==NULL
911                         || sp->type==PVT_NONE)
912         {
913                 LM_ERR("bad parameters\n");
914                 return -1;
915         }
916         
917         memset(value, 0, sizeof(pv_value_t));
918
919         ret = (*sp->getf)(msg, &(sp->pvp), value);
920         if(ret!=0)
921                 return ret;
922                 
923         if(sp->trans)
924                 return tr_exec(msg, (trans_t*)sp->trans, value);
925         return ret;
926 }
927
928 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
929                 pv_value_t *value)
930 {
931         if(sp==NULL || !pv_is_w(sp))
932                 return 0; /* no op */
933         if(pv_alter_context(sp) && is_route_type(LOCAL_ROUTE))
934                 return 0; /* no op */
935         return sp->setf(msg, &sp->pvp, op, value);
936 }
937
938 /**
939  *
940  */
941 int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
942 {
943         int n, h;
944         pv_value_t tok;
945         pv_elem_p it;
946         char *cur;
947         
948         if(msg==NULL || list==NULL || buf==NULL || len==NULL)
949                 return -1;
950
951         if(*len <= 0)
952                 return -1;
953
954         *buf = '\0';
955         cur = buf;
956         
957         h = 0;
958         n = 0;
959         for (it=list; it; it=it->next)
960         {
961                 /* put the text */
962                 if(it->text.s && it->text.len>0)
963                 {
964                         if(n+it->text.len < *len)
965                         {
966                                 memcpy(cur, it->text.s, it->text.len);
967                                 n += it->text.len;
968                                 cur += it->text.len;
969                         } else {
970                                 LM_ERR("no more space for text [%d]\n", it->text.len);
971                                 goto overflow;
972                         }
973                 }
974                 /* put the value of the specifier */
975                 if(it->spec.type!=PVT_NONE
976                                 && pv_get_spec_value(msg, &(it->spec), &tok)==0)
977                 {
978                         if(tok.flags&PV_VAL_NULL)
979                                 tok.rs = pv_str_null;
980                         if(n+tok.rs.len < *len)
981                         {
982                                 if(tok.rs.len>0)
983                                 {
984                                         memcpy(cur, tok.rs.s, tok.rs.len);
985                                         n += tok.rs.len;
986                                         cur += tok.rs.len;
987                                 }
988                         } else {
989                                 LM_ERR("no more space for spec value\n");
990                                 goto overflow;
991                         }
992                 }
993         }
994
995         goto done;
996         
997 overflow:
998         LM_ERR("buffer overflow -- increase the buffer size...\n");
999         return -1;
1000
1001 done:
1002 #ifdef EXTRA_DEBUG
1003         LM_DBG("final buffer length %d\n", n);
1004 #endif
1005         *cur = '\0';
1006         *len = n;
1007         return 0;
1008 }
1009
1010 /**
1011  *
1012  */
1013 pvname_list_t* parse_pvname_list(str *in, unsigned int type)
1014 {
1015         pvname_list_t* head = NULL;
1016         pvname_list_t* al = NULL;
1017         pvname_list_t* last = NULL;
1018         char *p;
1019         pv_spec_t spec;
1020         str s;
1021
1022         if(in==NULL || in->s==NULL)
1023         {
1024                 LM_ERR("bad parameters\n");
1025                 return NULL;
1026         }
1027
1028         p = in->s;
1029         while(is_in_str(p, in))
1030         {
1031                 while(is_in_str(p, in) && (*p==' '||*p=='\t'||*p==','||*p==';'))
1032                         p++;
1033                 if(!is_in_str(p, in))
1034                 {
1035                         if(head==NULL)
1036                                 LM_ERR("wrong item name list [%.*s]\n", in->len, in->s);
1037                         return head;
1038                 }
1039                 s.s=p;
1040                 s.len = in->s+in->len-p;
1041                 p = pv_parse_spec(&s, &spec);
1042                 if(p==NULL || (type && spec.type!=type))
1043                 {
1044                         LM_ERR("wrong item name list [%.*s]!\n", in->len, in->s);
1045                         goto error;
1046                 }
1047                 al = (pvname_list_t*)pkg_malloc(sizeof(pvname_list_t));
1048                 if(al==NULL)
1049                 {
1050                         LM_ERR("no more memory!\n");
1051                         goto error;
1052                 }
1053                 memset(al, 0, sizeof(pvname_list_t));
1054                 memcpy(&al->sname, &spec, sizeof(pv_spec_t));
1055
1056                 if(last==NULL)
1057                 {
1058                         head = al;
1059                         last = al;
1060                 } else {
1061                         last->next = al;
1062                         last = al;
1063                 }
1064         }
1065
1066         return head;
1067
1068 error:
1069         while(head)
1070         {
1071                 al = head;
1072                 head=head->next;
1073                 pkg_free(al);
1074         }
1075         return NULL;
1076 }
1077
1078
1079
1080 /** frees only the contests of a pv_spec_t. */
1081 void pv_spec_free_contents(pv_spec_t *spec)
1082 {
1083         /* TODO: free name if it is PV */
1084         if(spec->trans)
1085                 tr_free((trans_t*)spec->trans);
1086 }
1087
1088
1089
1090 /**
1091  *
1092  */
1093 void pv_spec_free(pv_spec_t *spec)
1094 {
1095         if(spec==0) return;
1096         pv_spec_free_contents(spec);
1097         pkg_free(spec);
1098 }
1099
1100 /**
1101  *
1102  */
1103 int pv_elem_free_all(pv_elem_p log)
1104 {
1105         pv_elem_p t;
1106         while(log)
1107         {
1108                 t = log;
1109                 log = log->next;
1110                 pkg_free(t);
1111         }
1112         return 0;
1113 }
1114
1115 /**
1116  *
1117  */
1118 void pv_value_destroy(pv_value_t *val)
1119 {
1120         if(val==0) return;
1121         if(val->flags&PV_VAL_PKG) pkg_free(val->rs.s);
1122         if(val->flags&PV_VAL_SHM) shm_free(val->rs.s);
1123         memset(val, 0, sizeof(pv_value_t));
1124 }
1125
1126 #define PV_PRINT_BUF_SIZE  1024
1127 #define PV_PRINT_BUF_NO    3
1128 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
1129 {
1130         static int buf_itr = 0;
1131         static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
1132
1133         s->s = buf[buf_itr];
1134         s->len = PV_PRINT_BUF_SIZE;
1135         buf_itr = (buf_itr+1)%PV_PRINT_BUF_NO;
1136         return pv_printf( msg, list, s->s, &s->len);
1137 }
1138
1139 /********************************************************
1140  * Transformations API
1141  ********************************************************/
1142
1143 /**
1144  *
1145  */
1146 static inline char* tr_get_class(str *in, char *p, str *tclass)
1147 {
1148         tclass->s = p;
1149         while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
1150         if(*p!=TR_CLASS_MARKER || tclass->s == p)
1151         {
1152                 LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
1153                 return NULL;
1154         }
1155         tclass->len = p - tclass->s;
1156         p++;
1157
1158         return p;
1159 }
1160
1161 /**
1162  *
1163  */
1164 static inline trans_t* tr_new(void)
1165 {
1166         trans_t *t = NULL;
1167
1168         t = (trans_t*)pkg_malloc(sizeof(trans_t));
1169         if(t == NULL)
1170         {
1171                 LM_ERR("no more private memory\n");
1172                 return NULL;
1173         }
1174         memset(t, 0, sizeof(trans_t));
1175         return t;
1176 }
1177
1178 char* tr_lookup(str *in, trans_t **tr)
1179 {
1180         char *p;
1181         char *p0;
1182         str tclass;
1183         tr_export_t *te = NULL; 
1184         trans_t *t = NULL;
1185         trans_t *t0 = NULL;
1186         str s;
1187
1188         if(in==NULL || in->s==NULL || tr==NULL)
1189                 return NULL;
1190         
1191         p = in->s;
1192         do {
1193                 while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
1194                 if(*p != TR_LBRACKET)
1195                         break;
1196                 p++;
1197
1198                 if((t = tr_new())==NULL) return NULL;
1199
1200                 if(t0==NULL) *tr = t;
1201                 else t0->next = t;
1202                 t0 = t;
1203
1204                 /* find transformation class */
1205                 p = tr_get_class(in, p, &tclass);
1206                 if(p==NULL) goto error;
1207
1208                 /* locate transformation */
1209                 te = tr_lookup_class(&tclass);
1210                 if(te==NULL)
1211                 {
1212                         LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
1213                                 tclass.len, tclass.s, in->len, in->s);
1214                         goto error;
1215                 }
1216
1217                 s.s = p; s.len = in->s + in->len - p;
1218                 p0 = te->tparse(&s, t);
1219                 if(p0==NULL)
1220                         goto error;
1221                 p = p0;
1222
1223                 if(*p != TR_RBRACKET)
1224                 {
1225                         LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
1226                                         in->s, *p);
1227                         goto error;
1228                 }
1229
1230                 p++;
1231                 if(!is_in_str(p, in))
1232                         break;
1233         } while(1);
1234
1235         return p;
1236 error:
1237         LM_ERR("error parsing [%.*s]\n", in->len, in->s);
1238         t = *tr;
1239         while(t)
1240         {
1241                 t0 = t;
1242                 t = t->next;
1243                 tr_destroy(t0);
1244                 pkg_free(t0);
1245         }
1246         return NULL;
1247 }
1248
1249 /*!
1250  * \brief Destroy transformation including eventual parameter
1251  * \param t transformation
1252  */
1253 void tr_destroy(trans_t *t)
1254 {
1255         tr_param_t *tp;
1256         tr_param_t *tp0;
1257         if(t==NULL) return;
1258
1259         tp = t->params;
1260         while(tp)
1261         {
1262                 tp0 = tp;
1263                 tp = tp->next;
1264                 tr_param_free(tp0);
1265         }
1266         memset(t, 0, sizeof(trans_t));
1267 }
1268
1269 /*!
1270  * \brief Exec transformation on a pseudo-variable value
1271  * \param msg SIP message
1272  * \param tr one or more transformations
1273  * \param val pseudo-variable value
1274  * \return 0 on success, -1 on error
1275  */
1276
1277 int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v)
1278 {
1279         int r;
1280         trans_t *i;
1281
1282         if(t==NULL || v==NULL)
1283         {
1284                 LM_DBG("invalid parameters\n");
1285                 return -1;
1286         }
1287         
1288         for(i = t; i!=NULL; i=i->next)
1289         {
1290                 r = (*i->trf)(msg, i->params, i->subtype, v);
1291                 if(r!=0)
1292                         return r;
1293         }
1294         return 0;
1295 }
1296
1297 /*!
1298  * \brief Free allocated memory of transformation list
1299  * \param t transformation list
1300  */
1301 void tr_free(trans_t *t)
1302 {
1303         trans_t *t0;
1304
1305         while(t)
1306         {
1307                 t0 = t;
1308                 t = t->next;
1309                 tr_destroy(t0);
1310                 pkg_free(t0);
1311         }
1312 }
1313
1314
1315 /*!
1316  * \brief Free transformation parameter list
1317  * \param tp transformation list
1318  */
1319 void tr_param_free(tr_param_t *tp)
1320 {
1321         tr_param_t *tp0;
1322
1323         if(tp==NULL) return;
1324         while(tp)
1325         {
1326                 tp0 = tp;
1327                 tp = tp->next;
1328                 if(tp0->type==TR_PARAM_SPEC)
1329                         pv_spec_free((pv_spec_t*)tp0->v.data);
1330                 pkg_free(tp0);
1331         }
1332 }
1333
1334 typedef struct _tr_item
1335 {
1336         tr_export_t tre;
1337         unsigned int trid;
1338         struct _tr_item *next;
1339 } tr_item_t, *tr_item_p;
1340
1341 static tr_item_t* _tr_table[TR_TABLE_SIZE];
1342 static int _tr_table_set = 0;
1343
1344
1345 /**
1346  *
1347  */
1348 void tr_init_table(void)
1349 {
1350         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1351         _tr_table_set = 1;
1352 }
1353
1354 /**
1355  *
1356  */
1357 int tr_table_add(tr_export_t *e)
1358 {
1359         tr_item_t *tri = NULL;
1360         tr_item_t *trj = NULL;
1361         tr_item_t *trn = NULL;
1362         int found;
1363         unsigned int trid;
1364
1365         if(e==NULL || e->tclass.s==NULL)
1366         {
1367                 LM_ERR("invalid parameters\n");
1368                 return -1;
1369         }
1370         
1371         if(_tr_table_set==0)
1372         {
1373                 LM_DBG("TR table not initialized, doing it now\n");
1374                 tr_init_table();
1375         }
1376
1377         found = 0;
1378         // trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1379         trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1380
1381         tri = _tr_table[trid%TR_TABLE_SIZE];
1382         while(tri)
1383         {
1384                 if(tri->tre.tclass.len==e->tclass.len)
1385                 {
1386                         found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
1387                         if(found==0)
1388                         {
1389                                 LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
1390                                                 e->tclass.s);
1391                                 return -1;
1392                         }
1393                 }
1394                 trj = tri;
1395                 tri = tri->next;
1396         }
1397
1398         trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
1399         if(trn==0)
1400         {
1401                 LM_ERR("no more memory\n");
1402                 return -1;
1403         }
1404         memset(trn, 0, sizeof(tr_item_t));
1405         memcpy(&(trn->tre), e, sizeof(tr_export_t));
1406         trn->trid = trid;
1407
1408         //LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
1409         //                                      e->tclass.s, trid%TR_TABLE_SIZE);
1410         if(trj==0)
1411         {
1412                 trn->next = _tr_table[trid%TR_TABLE_SIZE];
1413                 _tr_table[trid%TR_TABLE_SIZE] = trn;
1414                 goto done;
1415         }
1416         trn->next = trj->next;
1417         trj->next = trn;
1418
1419 done:
1420         return 0;
1421 }
1422
1423 /**
1424  *
1425  */
1426 int register_trans_mod(char *mod_name, tr_export_t *items)
1427 {
1428         int ret;
1429         int i;
1430
1431         if (items==0)
1432                 return 0;
1433
1434         for ( i=0 ; items[i].tclass.s ; i++ ) {
1435                 ret = tr_table_add(&items[i]);
1436                 if (ret!=0) {
1437                         LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
1438                                         items[i].tclass.len, items[i].tclass.s, mod_name);
1439                 }
1440         }
1441         return 0;
1442 }
1443
1444 /**
1445  *
1446  */
1447 int tr_table_free(void)
1448 {
1449         tr_item_p te;
1450         tr_item_p te1;
1451         int i;
1452
1453         for(i=0; i<TR_TABLE_SIZE; i++)
1454         {
1455                 te = _tr_table[i];
1456                 while(te!=0)
1457                 {
1458                         te1 = te;
1459                         te = te->next;
1460                         pkg_free(te1);
1461                 }
1462         }
1463         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1464         _tr_table_set = 0;
1465         
1466         return 0;
1467 }
1468
1469 tr_export_t* tr_lookup_class(str *tclass)
1470 {
1471         tr_item_t *tri;
1472         unsigned int trid;
1473
1474         if(tclass==0 || tclass->s==0)
1475         {
1476                 LM_ERR("bad parameters\n");
1477                 return NULL;
1478         }
1479
1480         /* search in TR table */
1481         // trid = get_hash1_raw(tclass->s, tclass->len);
1482         trid = get_hash1_raw(tclass->s, tclass->len);
1483         tri = _tr_table[trid%TR_TABLE_SIZE];
1484         while(tri)
1485         {
1486                 if(tri->trid==trid && tri->tre.tclass.len==tclass->len
1487                                 && memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
1488                         return &(tri->tre);
1489                 tri = tri->next;
1490         }
1491
1492         return NULL;
1493 }
1494
1495
1496 /** init pv api (optional).
1497  * @return 0 on success, -1 on error
1498  */
1499 int init_pv_api(void)
1500 {
1501         pv_init_table();
1502         tr_init_table();
1503         return 0;
1504 }
1505
1506
1507 /** destroy pv api. */
1508 void destroy_pv_api(void)
1509 {
1510         /* free PV and TR hash tables */
1511         pv_table_free();
1512         tr_table_free();
1513         return;
1514 }