pvapi.c 35 KB

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