pvapi.c 33 KB

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