f6c029cd306997b6b2b0eee50f3b9784d76933bc
[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 /**
320  *
321  */
322 pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
323 {
324         pv_item_t *pvi;
325         unsigned int pvid;
326
327         if(pvname==0 || e==0)
328         {
329                 LM_ERR("bad parameters\n");
330                 return NULL;
331         }
332
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];
337         while(pvi)
338         {
339                 if(pvi->pvid > pvid)
340                         break;
341
342                 if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
343                         && memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
344                 {
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;
351                         return &(pvi->pve);
352                 }
353                 pvi = pvi->next;
354         }
355
356         return NULL;
357 }
358
359 /**
360  *
361  */
362 int pv_parse_index(pv_spec_p sp, str *in)
363 {
364         char *p;
365         char *s;
366         int sign;
367         pv_spec_p nsp = 0;
368
369         if(in==NULL || in->s==NULL || sp==NULL)
370                 return -1;
371         p = in->s;
372         if(*p==PV_MARKER)
373         {
374                 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
375                 if(nsp==NULL)
376                 {
377                         LM_ERR("no more memory\n");
378                         return -1;
379                 }
380                 s = pv_parse_spec(in, nsp);
381                 if(s==NULL)
382                 {
383                         LM_ERR("invalid index [%.*s]\n", in->len, in->s);
384                         pv_spec_free(nsp);
385                         return -1;
386                 }
387                 sp->pvp.pvi.type = PV_IDX_PVAR;
388                 sp->pvp.pvi.u.dval = (void*)nsp;
389                 return 0;
390         }
391         if(*p=='*' && in->len==1)
392         {
393                 sp->pvp.pvi.type = PV_IDX_ALL;
394                 return 0;
395         }
396         sign = 1;
397         if(*p=='-')
398         {
399                 sign = -1;
400                 p++;
401         }
402         sp->pvp.pvi.u.ival = 0;
403         while(p<in->s+in->len && *p>='0' && *p<='9')
404         {
405                 sp->pvp.pvi.u.ival = sp->pvp.pvi.u.ival * 10 + *p - '0';
406                 p++;
407         }
408         if(p!=in->s+in->len)
409         {
410                 LM_ERR("invalid index [%.*s]\n", in->len, in->s);
411                 return -1;
412         }
413         sp->pvp.pvi.u.ival *= sign;
414         sp->pvp.pvi.type = PV_IDX_INT;
415         return 0;
416 }
417
418 /**
419  *
420  */
421 int pv_init_iname(pv_spec_p sp, int param)
422 {
423         if(sp==NULL)
424                 return -1;
425         sp->pvp.pvn.type = PV_NAME_INTSTR;
426         sp->pvp.pvn.u.isname.name.n = param;
427         return 0;
428 }
429
430 /**
431  *
432  */
433 char* pv_parse_spec2(str *in, pv_spec_p e, int silent)
434 {
435         char *p;
436         str s;
437         str pvname;
438         int pvstate;
439         trans_t *tr = NULL;
440         pv_export_t *pte = NULL;
441         int n=0;
442
443         if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
444         {
445                 if (!silent) LM_ERR("bad parameters\n");
446                 return NULL;
447         }
448         
449         /* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
450         tr = 0;
451         pvstate = 0;
452         memset(e, 0, sizeof(pv_spec_t));
453         p = in->s;
454         p++;
455         if(*p==PV_LNBRACKET)
456         {
457                 p++;
458                 pvstate = 1;
459         }
460         pvname.s = p;
461         if(*p == PV_MARKER) {
462                 p++;
463                 if(pvstate==1)
464                 {
465                         if(*p!=PV_RNBRACKET)
466                                 goto error;
467                         p++;
468                 }
469                 e->getf = pv_get_marker;
470                 e->type = PVT_MARKER;
471                 pvname.len = 1;
472                 goto done_all;
473         }
474         while(is_in_str(p,in) && is_pv_valid_char(*p))
475                 p++;
476         pvname.len = p - pvname.s;
477         if(pvstate==1)
478         {
479                 if(*p==PV_RNBRACKET)
480                 { /* full pv name ended here*/
481                         goto done_inm;
482                 } else if(*p==PV_LNBRACKET) {
483                         p++;
484                         pvstate = 2;
485                 } else if(*p==PV_LIBRACKET) {
486                         p++;
487                         pvstate = 3;
488                 } else if(*p==TR_LBRACKET) {
489                         p++;
490                         pvstate = 4;
491                 } else {
492                         if (!silent)
493                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
494                                                         *p, in->len, in->s, pvstate);
495                         goto error;
496                 }
497         } else { 
498                 if(!is_in_str(p, in)) {
499                         p--;
500                         goto done_inm;
501                 } else if(*p==PV_LNBRACKET) {
502                         p++;
503                         pvstate = 5;
504                 } else {
505                         /* still in input str, but end of PV */
506                         /* p is increased at the end, so decrement here */
507                         p--;
508                         goto done_inm;
509                 }
510         }
511
512 done_inm:
513         if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
514         {
515                 if (!silent) 
516                         LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
517                 goto error;
518         }
519         if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
520         {
521                 if (!silent) 
522                         LM_ERR("pvar \"%.*s\" expects an inner name\n",
523                                                 pvname.len, pvname.s);
524                 goto error;
525         }
526         if(pvstate==2 || pvstate==5)
527         {
528                 if(pte->parse_name==NULL)
529                 {
530                         if (!silent)
531                                 LM_ERR("pvar \"%.*s\" does not get name param\n",
532                                                 pvname.len, pvname.s);
533                         goto error;
534                 }
535                 s.s = p;
536                 n = 0;
537                 while(is_in_str(p, in))
538                 {
539                         if(*p==PV_RNBRACKET)
540                         {
541                                 if(n==0)
542                                         break;
543                                 n--;
544                         }
545                         if(*p == PV_LNBRACKET)
546                                 n++;
547                         p++;
548                 }
549
550                 if(!is_in_str(p, in))
551                         goto error;
552
553                 if(p==s.s)
554                 {
555                         if (!silent)
556                                 LM_ERR("pvar \"%.*s\" does not get empty name param\n",
557                                                 pvname.len, pvname.s);
558                         goto error;
559                 }
560                 s.len = p - s.s;
561                 if(pte->parse_name(e, &s)!=0)
562                 {
563                         if (!silent)
564                                 LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
565                                                 pvname.len, pvname.s, s.len, s.s);
566                         goto error;
567                 }
568                 if(pvstate==2)
569                 {
570                         p++;
571                         if(*p==PV_RNBRACKET)
572                         { /* full pv name ended here*/
573                                 goto done_vnm;
574                         } else if(*p==PV_LIBRACKET) {
575                                 p++;
576                                 pvstate = 3;
577                         } else if(*p==TR_LBRACKET) {
578                                 p++;
579                                 pvstate = 4;
580                         } else {
581                                 if (!silent)
582                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
583                                                                 *p, in->len, in->s, pvstate);
584                                 goto error;
585                         }
586                 } else {
587                         if(*p==PV_RNBRACKET)
588                         { /* full pv name ended here*/
589                                 p++;
590                                 goto done_all;
591                         } else {
592                                 if (!silent)
593                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
594                                                                 *p, in->len, in->s, pvstate);
595                                 goto error;
596                         }
597                 }
598         }
599 done_vnm:
600         if(pvstate==3)
601         {
602                 if(pte->parse_index==NULL)
603                 {
604                         if (!silent)
605                                 LM_ERR("pvar \"%.*s\" does not get index param\n",
606                                                 pvname.len, pvname.s);
607                         goto error;
608                 }
609                 s.s = p;
610                 n = 0;
611                 while(is_in_str(p, in))
612                 {
613                         if(*p==PV_RIBRACKET)
614                         {
615                                 if(n==0)
616                                         break;
617                                 n--;
618                         }
619                         if(*p == PV_LIBRACKET)
620                                 n++;
621                         p++;
622                 }
623                 if(!is_in_str(p, in))
624                         goto error;
625
626                 if(p==s.s)
627                 {
628                         if (!silent)
629                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
630                                                 pvname.len, pvname.s);
631                         goto error;
632                 }
633                 s.len = p - s.s;
634                 if(pte->parse_index(e, &s)!=0)
635                 {
636                         if (!silent)
637                                 LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
638                                                 pvname.len, pvname.s, s.len, s.s);
639                         goto error;
640                 }
641                 p++;
642                 if(*p==PV_RNBRACKET)
643                 { /* full pv name ended here*/
644                         goto done_idx;
645                 } else if(*p==TR_LBRACKET) {
646                         p++;
647                         pvstate = 4;
648                 } else {
649                         if (!silent)
650                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
651                                                         *p, in->len, in->s, pvstate);
652                         goto error;
653                 }
654         }
655 done_idx:
656         if(pvstate==4)
657         {
658                 s.s = p-1;
659                 n = 0;
660                 while(is_in_str(p, in))
661                 {
662                         if(*p==TR_RBRACKET)
663                         {
664                                 if(n==0)
665                                 {
666                                         /* yet another transformation */
667                                         p++;
668                                         while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
669
670                                         if(!is_in_str(p, in) || *p != TR_LBRACKET)
671                                         {
672                                                 p--;
673                                                 break;
674                                         }
675                                 }
676                                 n--;
677                         }
678                         if(*p == TR_LBRACKET)
679                                 n++;
680                         p++;
681                 }
682                 if(!is_in_str(p, in))
683                         goto error;
684
685                 if(p==s.s)
686                 {
687                         if (!silent)
688                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
689                                                 pvname.len, pvname.s);
690                         goto error;
691                 }
692                 s.len = p - s.s + 1;
693
694                 p = tr_lookup(&s, &tr);
695                 if(p==NULL)
696                 {
697                         if (!silent)
698                                 LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
699                         goto error;
700                 }
701                 if(*p!=PV_RNBRACKET)
702                 {
703                         if (!silent)
704                                 LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
705                         goto error;
706                 }
707                 e->trans = (void*)tr;
708         }
709         p++;
710
711 done_all:
712         if(pte!=NULL && pte->init_param)
713                 pte->init_param(e, pte->iparam);
714         return p;
715
716 error:
717         if(p!=NULL){
718                 if (!silent)
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);
721         }else{
722                 if (!silent)
723                         LM_ERR("invalid parsing in [%.*s] at (%d)\n",
724                                                 in->len, in->s, pvstate);
725         }
726         return NULL;
727
728 } /* end: pv_parse_spec */
729
730 /**
731  *
732  */
733 int pv_parse_format(str *in, pv_elem_p *el)
734 {
735         char *p, *p0;
736         int n = 0;
737         pv_elem_p e, e0;
738         str s;
739
740         if(in==NULL || in->s==NULL || el==NULL)
741                 return -1;
742
743         /*LM_DBG("parsing [%.*s]\n", in->len, in->s);*/
744         
745         if(in->len == 0)
746         {
747                 *el = pkg_malloc(sizeof(pv_elem_t));
748                 if(*el == NULL)
749                         goto error;
750                 memset(*el, 0, sizeof(pv_elem_t));
751                 (*el)->text = *in;
752                 return 0;
753         }
754
755         p = in->s;
756         *el = NULL;
757         e = e0 = NULL;
758
759         while(is_in_str(p,in))
760         {
761                 e0 = e;
762                 e = pkg_malloc(sizeof(pv_elem_t));
763                 if(!e)
764                         goto error;
765                 memset(e, 0, sizeof(pv_elem_t));
766                 n++;
767                 if(*el == NULL)
768                         *el = e;
769                 if(e0)
770                         e0->next = e;
771         
772                 e->text.s = p;
773                 while(is_in_str(p,in) && *p!=PV_MARKER)
774                         p++;
775                 e->text.len = p - e->text.s;
776                 
777                 if(*p == '\0' || !is_in_str(p,in))
778                         break;
779                 s.s = p;
780                 s.len = in->s+in->len-p;
781                 p0 = pv_parse_spec(&s, &e->spec);
782                 
783                 if(p0==NULL)
784                         goto error;
785                 if(*p0 == '\0')
786                         break;
787                 p = p0;
788         }
789         /*LM_DBG("format parsed OK: [%d] items\n", n);*/
790
791         if(*el == NULL)
792                 return -1;
793
794         return 0;
795
796 error:
797         pv_elem_free_all(*el);
798         *el = NULL;
799         return -1;
800 }
801
802 /**
803  *
804  */
805 int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name)
806 {
807         if(msg==NULL || ip==NULL || name==NULL)
808                 return -1;
809         memset(name, 0, sizeof(pv_value_t));
810
811         if(ip->pvn.type==PV_NAME_INTSTR)
812         {
813                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
814                 {
815                         name->rs = ip->pvn.u.isname.name.s;
816                         name->flags = PV_VAL_STR;
817                 } else {
818                         name->ri = ip->pvn.u.isname.name.n;
819                         name->flags = PV_VAL_INT|PV_TYPE_INT;
820                 }
821                 return 0;
822         } else if(ip->pvn.type==PV_NAME_PVAR) {
823                 /* pvar */
824                 if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), name)!=0)
825                 {
826                         LM_ERR("cannot get name value\n");
827                         return -1;
828                 }
829                 if(name->flags&PV_VAL_NULL || name->flags&PV_VAL_EMPTY)
830                 {
831                         LM_ERR("null or empty name\n");
832                         return -1;
833                 }
834                 return 0;
835         }
836         LM_ERR("name type is PV_NAME_OTHER - cannot resolve\n");
837         return -1;
838 }
839
840 /**
841  * parse AVP name
842  * @return 0 on success, -1 on error
843  */
844 int pv_parse_avp_name(pv_spec_p sp, str *in)
845 {
846         char *p;
847         char *s;
848         pv_spec_p nsp = 0;
849
850         if(in==NULL || in->s==NULL || sp==NULL)
851                 return -1;
852         p = in->s;
853         if(*p==PV_MARKER)
854         {
855                 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
856                 if(nsp==NULL)
857                 {
858                         LM_ERR("no more memory\n");
859                         return -1;
860                 }
861                 s = pv_parse_spec(in, nsp);
862                 if(s==NULL)
863                 {
864                         LM_ERR("invalid name [%.*s]\n", in->len, in->s);
865                         pv_spec_free(nsp);
866                         return -1;
867                 }
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;
872                 return 0;
873         }
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)
877         {
878                 LM_ERR("bad avp name [%.*s]\n", in->len, in->s);
879                 return -1;
880         }
881         sp->pvp.pvn.type = PV_NAME_INTSTR;
882         return 0;
883 }
884
885 /**
886  * fill avp name details (id and type)
887  * @return 0 on success, -1 on error
888  */
889 int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
890                 unsigned short *name_type)
891 {
892         pv_value_t tv;
893         if(ip==NULL || avp_name==NULL || name_type==NULL)
894                 return -1;
895         memset(avp_name, 0, sizeof(int_str));
896         *name_type = 0;
897
898         if(ip->pvn.type==PV_NAME_INTSTR)
899         {
900                 *name_type = ip->pvn.u.isname.type;
901                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
902                 {
903                         avp_name->s = ip->pvn.u.isname.name.s;
904                         *name_type |= AVP_NAME_STR;
905                 } else {
906                         avp_name->n = ip->pvn.u.isname.name.n;
907                         /* *name_type &= AVP_SCRIPT_MASK; */
908                         *name_type = 0;
909                 }
910                 return 0;
911         }
912         /* pvar */
913         if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
914         {
915                 LM_ERR("cannot get avp value\n");
916                 return -1;
917         }
918         if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
919         {
920                 LM_ERR("null or empty name\n");
921                 return -1;
922         }
923                 
924         if((tv.flags&PV_TYPE_INT) && (tv.flags&PV_VAL_INT))
925         {
926                 avp_name->n = tv.ri;
927         } else {
928                 avp_name->s = tv.rs;
929                 *name_type = AVP_NAME_STR;
930         }
931         return 0;
932 }
933
934 /**
935  *
936  */
937 int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags)
938 {
939         pv_value_t tv;
940         if(ip==NULL || idx==NULL || flags==NULL)
941                 return -1;
942
943         *idx = 0;
944         *flags = 0;
945
946         if(ip->pvi.type == PV_IDX_ALL) {
947                 *flags = PV_IDX_ALL;
948                 return 0;
949         }
950         
951         if(ip->pvi.type == PV_IDX_INT)
952         {
953                 *idx = ip->pvi.u.ival;
954                 return 0;
955         }
956
957         /* pvar */
958         if(pv_get_spec_value(msg, (pv_spec_p)ip->pvi.u.dval, &tv)!=0)
959         {
960                 LM_ERR("cannot get index value\n");
961                 return -1;
962         }
963         if(!(tv.flags&PV_VAL_INT))
964         {
965                 LM_ERR("invalid index value\n");
966                 return -1;
967         }
968         *idx = tv.ri;
969         return 0;
970 }
971
972 /**
973  *
974  */
975 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value)
976 {
977         int ret = 0;
978
979         if(msg==NULL || sp==NULL || sp->getf==NULL || value==NULL
980                         || sp->type==PVT_NONE)
981         {
982                 LM_ERR("bad parameters\n");
983                 return -1;
984         }
985         
986         memset(value, 0, sizeof(pv_value_t));
987
988         ret = (*sp->getf)(msg, &(sp->pvp), value);
989         if(ret!=0)
990                 return ret;
991                 
992         if(sp->trans)
993                 return tr_exec(msg, (trans_t*)sp->trans, value);
994         return ret;
995 }
996
997 /**
998  *
999  */
1000 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
1001                 pv_value_t *value)
1002 {
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);
1008 }
1009
1010 /**
1011  *
1012  */
1013 int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1014 {
1015         int n, h;
1016         pv_value_t tok;
1017         pv_elem_p it;
1018         char *cur;
1019         
1020         if(msg==NULL || list==NULL || buf==NULL || len==NULL)
1021                 return -1;
1022
1023         if(*len <= 0)
1024                 return -1;
1025
1026         *buf = '\0';
1027         cur = buf;
1028         
1029         h = 0;
1030         n = 0;
1031         for (it=list; it; it=it->next)
1032         {
1033                 /* put the text */
1034                 if(it->text.s && it->text.len>0)
1035                 {
1036                         if(n+it->text.len < *len)
1037                         {
1038                                 memcpy(cur, it->text.s, it->text.len);
1039                                 n += it->text.len;
1040                                 cur += it->text.len;
1041                         } else {
1042                                 LM_ERR("no more space for text [%d]\n", it->text.len);
1043                                 goto overflow;
1044                         }
1045                 }
1046                 /* put the value of the specifier */
1047                 if(it->spec.type!=PVT_NONE
1048                                 && pv_get_spec_value(msg, &(it->spec), &tok)==0)
1049                 {
1050                         if(tok.flags&PV_VAL_NULL)
1051                                 tok.rs = pv_str_null;
1052                         if(n+tok.rs.len < *len)
1053                         {
1054                                 if(tok.rs.len>0)
1055                                 {
1056                                         memcpy(cur, tok.rs.s, tok.rs.len);
1057                                         n += tok.rs.len;
1058                                         cur += tok.rs.len;
1059                                 }
1060                         } else {
1061                                 LM_ERR("no more space for spec value\n");
1062                                 goto overflow;
1063                         }
1064                 }
1065         }
1066
1067         goto done;
1068         
1069 overflow:
1070         LM_ERR("buffer overflow -- increase the buffer size...\n");
1071         return -1;
1072
1073 done:
1074 #ifdef EXTRA_DEBUG
1075         LM_DBG("final buffer length %d\n", n);
1076 #endif
1077         *cur = '\0';
1078         *len = n;
1079         return 0;
1080 }
1081
1082 /**
1083  *
1084  */
1085 pvname_list_t* parse_pvname_list(str *in, unsigned int type)
1086 {
1087         pvname_list_t* head = NULL;
1088         pvname_list_t* al = NULL;
1089         pvname_list_t* last = NULL;
1090         char *p;
1091         pv_spec_t spec;
1092         str s;
1093
1094         if(in==NULL || in->s==NULL)
1095         {
1096                 LM_ERR("bad parameters\n");
1097                 return NULL;
1098         }
1099
1100         p = in->s;
1101         while(is_in_str(p, in))
1102         {
1103                 while(is_in_str(p, in) && (*p==' '||*p=='\t'||*p==','||*p==';'))
1104                         p++;
1105                 if(!is_in_str(p, in))
1106                 {
1107                         if(head==NULL)
1108                                 LM_ERR("wrong item name list [%.*s]\n", in->len, in->s);
1109                         return head;
1110                 }
1111                 s.s=p;
1112                 s.len = in->s+in->len-p;
1113                 p = pv_parse_spec(&s, &spec);
1114                 if(p==NULL || (type && spec.type!=type))
1115                 {
1116                         LM_ERR("wrong item name list [%.*s]!\n", in->len, in->s);
1117                         goto error;
1118                 }
1119                 al = (pvname_list_t*)pkg_malloc(sizeof(pvname_list_t));
1120                 if(al==NULL)
1121                 {
1122                         LM_ERR("no more memory!\n");
1123                         goto error;
1124                 }
1125                 memset(al, 0, sizeof(pvname_list_t));
1126                 memcpy(&al->sname, &spec, sizeof(pv_spec_t));
1127
1128                 if(last==NULL)
1129                 {
1130                         head = al;
1131                         last = al;
1132                 } else {
1133                         last->next = al;
1134                         last = al;
1135                 }
1136         }
1137
1138         return head;
1139
1140 error:
1141         while(head)
1142         {
1143                 al = head;
1144                 head=head->next;
1145                 pkg_free(al);
1146         }
1147         return NULL;
1148 }
1149
1150
1151
1152 /** destroy the content of pv_spec_t structure.
1153  */
1154 void pv_spec_destroy(pv_spec_t *spec)
1155 {
1156         if(spec==0) return;
1157         /* free name if it is PV */
1158         if(spec->pvp.pvn.nfree)
1159                 spec->pvp.pvn.nfree((void*)(&spec->pvp.pvn));
1160         if(spec->trans)
1161                 tr_free((trans_t*)spec->trans);
1162 }
1163
1164 /** free the pv_spec_t structure.
1165  */
1166 void pv_spec_free(pv_spec_t *spec)
1167 {
1168         if(spec==0) return;
1169         pv_spec_destroy(spec);
1170         pkg_free(spec);
1171 }
1172
1173 /**
1174  *
1175  */
1176 int pv_elem_free_all(pv_elem_p log)
1177 {
1178         pv_elem_p t;
1179         while(log)
1180         {
1181                 t = log;
1182                 log = log->next;
1183                 pkg_free(t);
1184         }
1185         return 0;
1186 }
1187
1188 /**
1189  *
1190  */
1191 void pv_value_destroy(pv_value_t *val)
1192 {
1193         if(val==0) return;
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));
1197 }
1198
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)
1202 {
1203         static int buf_itr = 0;
1204         static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
1205
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);
1210 }
1211
1212 /********************************************************
1213  * Transformations API
1214  ********************************************************/
1215
1216 /**
1217  *
1218  */
1219 static inline char* tr_get_class(str *in, char *p, str *tclass)
1220 {
1221         tclass->s = p;
1222         while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
1223         if(*p!=TR_CLASS_MARKER || tclass->s == p)
1224         {
1225                 LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
1226                 return NULL;
1227         }
1228         tclass->len = p - tclass->s;
1229         p++;
1230
1231         return p;
1232 }
1233
1234 /**
1235  *
1236  */
1237 static inline trans_t* tr_new(void)
1238 {
1239         trans_t *t = NULL;
1240
1241         t = (trans_t*)pkg_malloc(sizeof(trans_t));
1242         if(t == NULL)
1243         {
1244                 LM_ERR("no more private memory\n");
1245                 return NULL;
1246         }
1247         memset(t, 0, sizeof(trans_t));
1248         return t;
1249 }
1250
1251 char* tr_lookup(str *in, trans_t **tr)
1252 {
1253         char *p;
1254         char *p0;
1255         str tclass;
1256         tr_export_t *te = NULL; 
1257         trans_t *t = NULL;
1258         trans_t *t0 = NULL;
1259         str s;
1260
1261         if(in==NULL || in->s==NULL || tr==NULL)
1262                 return NULL;
1263         
1264         p = in->s;
1265         do {
1266                 while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
1267                 if(*p != TR_LBRACKET)
1268                         break;
1269                 p++;
1270
1271                 if((t = tr_new())==NULL) return NULL;
1272
1273                 if(t0==NULL) *tr = t;
1274                 else t0->next = t;
1275                 t0 = t;
1276
1277                 /* find transformation class */
1278                 p = tr_get_class(in, p, &tclass);
1279                 if(p==NULL) goto error;
1280
1281                 /* locate transformation */
1282                 te = tr_lookup_class(&tclass);
1283                 if(te==NULL)
1284                 {
1285                         LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
1286                                 tclass.len, tclass.s, in->len, in->s);
1287                         goto error;
1288                 }
1289
1290                 s.s = p; s.len = in->s + in->len - p;
1291                 p0 = te->tparse(&s, t);
1292                 if(p0==NULL)
1293                         goto error;
1294                 p = p0;
1295
1296                 if(*p != TR_RBRACKET)
1297                 {
1298                         LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
1299                                         in->s, *p);
1300                         goto error;
1301                 }
1302
1303                 p++;
1304                 if(!is_in_str(p, in))
1305                         break;
1306         } while(1);
1307
1308         return p;
1309 error:
1310         LM_ERR("error parsing [%.*s]\n", in->len, in->s);
1311         t = *tr;
1312         while(t)
1313         {
1314                 t0 = t;
1315                 t = t->next;
1316                 tr_destroy(t0);
1317                 pkg_free(t0);
1318         }
1319         return NULL;
1320 }
1321
1322 /*!
1323  * \brief Destroy transformation including eventual parameter
1324  * \param t transformation
1325  */
1326 void tr_destroy(trans_t *t)
1327 {
1328         tr_param_t *tp;
1329         tr_param_t *tp0;
1330         if(t==NULL) return;
1331
1332         tp = t->params;
1333         while(tp)
1334         {
1335                 tp0 = tp;
1336                 tp = tp->next;
1337                 tr_param_free(tp0);
1338         }
1339         memset(t, 0, sizeof(trans_t));
1340 }
1341
1342 /*!
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
1348  */
1349
1350 int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v)
1351 {
1352         int r;
1353         trans_t *i;
1354
1355         if(t==NULL || v==NULL)
1356         {
1357                 LM_DBG("invalid parameters\n");
1358                 return -1;
1359         }
1360         
1361         for(i = t; i!=NULL; i=i->next)
1362         {
1363                 r = (*i->trf)(msg, i->params, i->subtype, v);
1364                 if(r!=0)
1365                         return r;
1366         }
1367         return 0;
1368 }
1369
1370 /*!
1371  * \brief Free allocated memory of transformation list
1372  * \param t transformation list
1373  */
1374 void tr_free(trans_t *t)
1375 {
1376         trans_t *t0;
1377
1378         while(t)
1379         {
1380                 t0 = t;
1381                 t = t->next;
1382                 tr_destroy(t0);
1383                 pkg_free(t0);
1384         }
1385 }
1386
1387
1388 /*!
1389  * \brief Free transformation parameter list
1390  * \param tp transformation list
1391  */
1392 void tr_param_free(tr_param_t *tp)
1393 {
1394         tr_param_t *tp0;
1395
1396         if(tp==NULL) return;
1397         while(tp)
1398         {
1399                 tp0 = tp;
1400                 tp = tp->next;
1401                 if(tp0->type==TR_PARAM_SPEC)
1402                         pv_spec_free((pv_spec_t*)tp0->v.data);
1403                 pkg_free(tp0);
1404         }
1405 }
1406
1407 typedef struct _tr_item
1408 {
1409         tr_export_t tre;
1410         unsigned int trid;
1411         struct _tr_item *next;
1412 } tr_item_t, *tr_item_p;
1413
1414 static tr_item_t* _tr_table[TR_TABLE_SIZE];
1415 static int _tr_table_set = 0;
1416
1417
1418 /**
1419  *
1420  */
1421 void tr_init_table(void)
1422 {
1423         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1424         _tr_table_set = 1;
1425 }
1426
1427 /**
1428  *
1429  */
1430 int tr_table_add(tr_export_t *e)
1431 {
1432         tr_item_t *tri = NULL;
1433         tr_item_t *trj = NULL;
1434         tr_item_t *trn = NULL;
1435         int found;
1436         unsigned int trid;
1437
1438         if(e==NULL || e->tclass.s==NULL)
1439         {
1440                 LM_ERR("invalid parameters\n");
1441                 return -1;
1442         }
1443         
1444         if(_tr_table_set==0)
1445         {
1446                 LM_DBG("TR table not initialized, doing it now\n");
1447                 tr_init_table();
1448         }
1449
1450         found = 0;
1451         // trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1452         trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1453
1454         tri = _tr_table[trid%TR_TABLE_SIZE];
1455         while(tri)
1456         {
1457                 if(tri->tre.tclass.len==e->tclass.len)
1458                 {
1459                         found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
1460                         if(found==0)
1461                         {
1462                                 LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
1463                                                 e->tclass.s);
1464                                 return -1;
1465                         }
1466                 }
1467                 trj = tri;
1468                 tri = tri->next;
1469         }
1470
1471         trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
1472         if(trn==0)
1473         {
1474                 LM_ERR("no more memory\n");
1475                 return -1;
1476         }
1477         memset(trn, 0, sizeof(tr_item_t));
1478         memcpy(&(trn->tre), e, sizeof(tr_export_t));
1479         trn->trid = trid;
1480
1481         //LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
1482         //                                      e->tclass.s, trid%TR_TABLE_SIZE);
1483         if(trj==0)
1484         {
1485                 trn->next = _tr_table[trid%TR_TABLE_SIZE];
1486                 _tr_table[trid%TR_TABLE_SIZE] = trn;
1487                 goto done;
1488         }
1489         trn->next = trj->next;
1490         trj->next = trn;
1491
1492 done:
1493         return 0;
1494 }
1495
1496 /**
1497  *
1498  */
1499 int register_trans_mod(char *mod_name, tr_export_t *items)
1500 {
1501         int ret;
1502         int i;
1503
1504         if (items==0)
1505                 return 0;
1506
1507         for ( i=0 ; items[i].tclass.s ; i++ ) {
1508                 ret = tr_table_add(&items[i]);
1509                 if (ret!=0) {
1510                         LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
1511                                         items[i].tclass.len, items[i].tclass.s, mod_name);
1512                 }
1513         }
1514         return 0;
1515 }
1516
1517 /**
1518  *
1519  */
1520 int tr_table_free(void)
1521 {
1522         tr_item_p te;
1523         tr_item_p te1;
1524         int i;
1525
1526         for(i=0; i<TR_TABLE_SIZE; i++)
1527         {
1528                 te = _tr_table[i];
1529                 while(te!=0)
1530                 {
1531                         te1 = te;
1532                         te = te->next;
1533                         pkg_free(te1);
1534                 }
1535         }
1536         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1537         _tr_table_set = 0;
1538         
1539         return 0;
1540 }
1541
1542 /**
1543  *
1544  */
1545 tr_export_t* tr_lookup_class(str *tclass)
1546 {
1547         tr_item_t *tri;
1548         unsigned int trid;
1549
1550         if(tclass==0 || tclass->s==0)
1551         {
1552                 LM_ERR("bad parameters\n");
1553                 return NULL;
1554         }
1555
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];
1560         while(tri)
1561         {
1562                 if(tri->trid==trid && tri->tre.tclass.len==tclass->len
1563                                 && memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
1564                         return &(tri->tre);
1565                 tri = tri->next;
1566         }
1567
1568         return NULL;
1569 }
1570
1571
1572 /********************************************************
1573  * core PVs, initialization and destroy APIs
1574  ********************************************************/
1575
1576 static pv_export_t _core_pvs[] = {
1577         {{"null", (sizeof("null")-1)}, /* */
1578                 PVT_NULL, pv_get_null, 0,
1579                 0, 0, 0, 0},
1580
1581         { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
1582 };
1583
1584 /** init pv api (optional).
1585  * @return 0 on success, -1 on error
1586  */
1587 int init_pv_api(void)
1588 {
1589         pv_init_table();
1590         tr_init_table();
1591         if(register_pvars_mod("core", _core_pvs)<0)
1592                 return -1;
1593         return 0;
1594 }
1595
1596
1597 /** destroy pv api. */
1598 void destroy_pv_api(void)
1599 {
1600         /* free PV and TR hash tables */
1601         pv_table_free();
1602         tr_table_free();
1603         return;
1604 }