pvapi.c 34 KB

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