pvapi.c 35 KB

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