pvapi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of SIP-Router, a free SIP server.
  7. *
  8. * SIP-Router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * SIP-Router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*!
  23. * \file
  24. * \brief PV API specification
  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_init.h"
  37. #include "pvar.h"
  38. #define is_in_str(p, in) (p<in->s+in->len && *p)
  39. #define PV_TABLE_SIZE 16
  40. #define TR_TABLE_SIZE 4
  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. /**
  52. *
  53. */
  54. void pv_init_table(void)
  55. {
  56. memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
  57. _pv_table_set = 1;
  58. }
  59. /**
  60. *
  61. */
  62. static int is_pv_valid_char(char c)
  63. {
  64. if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
  65. || (c=='_') || (c=='.') || (c=='?') /* ser $? */)
  66. return 1;
  67. return 0;
  68. }
  69. /**
  70. *
  71. */
  72. int pv_table_add(pv_export_t *e)
  73. {
  74. char *p;
  75. str *in;
  76. pv_item_t *pvi = NULL;
  77. pv_item_t *pvj = NULL;
  78. pv_item_t *pvn = NULL;
  79. int found;
  80. unsigned int pvid;
  81. if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
  82. {
  83. LM_ERR("invalid parameters\n");
  84. return -1;
  85. }
  86. if(_pv_table_set==0)
  87. {
  88. LM_DBG("PV table not initialized, doing it now\n");
  89. pv_init_table();
  90. }
  91. in = &(e->name);
  92. p = in->s;
  93. while(is_in_str(p,in) && is_pv_valid_char(*p))
  94. p++;
  95. if(is_in_str(p,in))
  96. {
  97. LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
  98. return -1;
  99. }
  100. found = 0;
  101. //pvid = get_hash1_raw(in->s, in->len);
  102. pvid = get_hash1_raw(in->s, in->len);
  103. pvi = _pv_table[pvid%PV_TABLE_SIZE];
  104. while(pvi)
  105. {
  106. if(pvi->pvid > pvid)
  107. break;
  108. if(pvi->pve.name.len==in->len)
  109. {
  110. found = strncmp(pvi->pve.name.s, in->s, in->len);
  111. if(found==0)
  112. {
  113. LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
  114. return -1;
  115. }
  116. }
  117. pvj = pvi;
  118. pvi = pvi->next;
  119. }
  120. pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
  121. if(pvn==0)
  122. {
  123. LM_ERR("no more memory\n");
  124. return -1;
  125. }
  126. memset(pvn, 0, sizeof(pv_item_t));
  127. memcpy(&(pvn->pve), e, sizeof(pv_export_t));
  128. pvn->pvid = pvid;
  129. if(pvj==0)
  130. {
  131. pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
  132. _pv_table[pvid%PV_TABLE_SIZE] = pvn;
  133. goto done;
  134. }
  135. pvn->next = pvj->next;
  136. pvj->next = pvn;
  137. done:
  138. return 0;
  139. }
  140. /**
  141. *
  142. */
  143. int register_pvars_mod(char *mod_name, pv_export_t *items)
  144. {
  145. int ret;
  146. int i;
  147. if (items==0)
  148. return 0;
  149. for ( i=0 ; items[i].name.s ; i++ ) {
  150. ret = pv_table_add(&items[i]);
  151. if (ret!=0) {
  152. LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
  153. items[i].name.len, items[i].name.s, mod_name);
  154. }
  155. }
  156. return 0;
  157. }
  158. /**
  159. *
  160. */
  161. int pv_table_free(void)
  162. {
  163. pv_item_p xe;
  164. pv_item_p xe1;
  165. int i;
  166. for(i=0; i<PV_TABLE_SIZE; i++)
  167. {
  168. xe = _pv_table[i];
  169. while(xe!=0)
  170. {
  171. xe1 = xe;
  172. xe = xe->next;
  173. pkg_free(xe1);
  174. }
  175. }
  176. memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
  177. _pv_table_set = 0;
  178. return 0;
  179. }
  180. /********** helper functions ********/
  181. /**
  182. * convert unsigned int to pv_value_t
  183. */
  184. int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
  185. pv_value_t *res, unsigned int uival)
  186. {
  187. int l = 0;
  188. char *ch = NULL;
  189. if(res==NULL)
  190. return -1;
  191. ch = int2str(uival, &l);
  192. res->rs.s = ch;
  193. res->rs.len = l;
  194. res->ri = (int)uival;
  195. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  196. return 0;
  197. }
  198. /**
  199. * convert signed int to pv_value_t
  200. */
  201. int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
  202. pv_value_t *res, int sival)
  203. {
  204. int l = 0;
  205. char *ch = NULL;
  206. if(res==NULL)
  207. return -1;
  208. ch = sint2str(sival, &l);
  209. res->rs.s = ch;
  210. res->rs.len = l;
  211. res->ri = sival;
  212. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  213. return 0;
  214. }
  215. /**
  216. * convert str to pv_value_t
  217. */
  218. int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
  219. pv_value_t *res, str *sval)
  220. {
  221. if(res==NULL)
  222. return -1;
  223. res->rs = *sval;
  224. res->flags = PV_VAL_STR;
  225. return 0;
  226. }
  227. /**
  228. * convert str-int to pv_value_t (type is str)
  229. */
  230. int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
  231. pv_value_t *res, str *sval, int ival)
  232. {
  233. if(res==NULL)
  234. return -1;
  235. res->rs = *sval;
  236. res->ri = ival;
  237. res->flags = PV_VAL_STR|PV_VAL_INT;
  238. return 0;
  239. }
  240. /**
  241. * convert int-str to pv_value_t (type is int)
  242. */
  243. int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
  244. pv_value_t *res, int ival, str *sval)
  245. {
  246. if(res==NULL)
  247. return -1;
  248. res->rs = *sval;
  249. res->ri = ival;
  250. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  251. return 0;
  252. }
  253. /*** ============================= ***/
  254. static str pv_str_marker = { PV_MARKER_STR, 1 };
  255. static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
  256. pv_value_t *res)
  257. {
  258. return pv_get_strintval(msg, param, res, &pv_str_marker,
  259. (int)pv_str_marker.s[0]);
  260. }
  261. static str pv_str_empty = { "", 0 };
  262. static str pv_str_null = { "<null>", 6 };
  263. int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
  264. {
  265. if(res==NULL)
  266. return -1;
  267. res->rs = pv_str_empty;
  268. res->ri = 0;
  269. res->flags = PV_VAL_NULL;
  270. return 0;
  271. }
  272. pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
  273. {
  274. pv_item_t *pvi;
  275. unsigned int pvid;
  276. if(pvname==0 || e==0)
  277. {
  278. LM_ERR("bad parameters\n");
  279. return NULL;
  280. }
  281. /* search in PV table */
  282. // pvid = get_hash1_raw(pvname->s, pvname->len);
  283. pvid = get_hash1_raw(pvname->s, pvname->len);
  284. pvi = _pv_table[pvid%PV_TABLE_SIZE];
  285. while(pvi)
  286. {
  287. if(pvi->pvid > pvid)
  288. break;
  289. if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
  290. && memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
  291. {
  292. /*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
  293. _pv_names_table[i].type);*/
  294. /* copy data from table to spec */
  295. e->type = pvi->pve.type;
  296. e->getf = pvi->pve.getf;
  297. e->setf = pvi->pve.setf;
  298. return &(pvi->pve);
  299. }
  300. pvi = pvi->next;
  301. }
  302. return NULL;
  303. }
  304. int pv_parse_index(pv_spec_p sp, str *in)
  305. {
  306. char *p;
  307. char *s;
  308. int sign;
  309. pv_spec_p nsp = 0;
  310. if(in==NULL || in->s==NULL || sp==NULL)
  311. return -1;
  312. p = in->s;
  313. if(*p==PV_MARKER)
  314. {
  315. nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
  316. if(nsp==NULL)
  317. {
  318. LM_ERR("no more memory\n");
  319. return -1;
  320. }
  321. s = pv_parse_spec(in, nsp);
  322. if(s==NULL)
  323. {
  324. LM_ERR("invalid index [%.*s]\n", in->len, in->s);
  325. pv_spec_free(nsp);
  326. return -1;
  327. }
  328. sp->pvp.pvi.type = PV_IDX_PVAR;
  329. sp->pvp.pvi.u.dval = (void*)nsp;
  330. return 0;
  331. }
  332. if(*p=='*' && in->len==1)
  333. {
  334. sp->pvp.pvi.type = PV_IDX_ALL;
  335. return 0;
  336. }
  337. sign = 1;
  338. if(*p=='-')
  339. {
  340. sign = -1;
  341. p++;
  342. }
  343. sp->pvp.pvi.u.ival = 0;
  344. while(p<in->s+in->len && *p>='0' && *p<='9')
  345. {
  346. sp->pvp.pvi.u.ival = sp->pvp.pvi.u.ival * 10 + *p - '0';
  347. p++;
  348. }
  349. if(p!=in->s+in->len)
  350. {
  351. LM_ERR("invalid index [%.*s]\n", in->len, in->s);
  352. return -1;
  353. }
  354. sp->pvp.pvi.u.ival *= sign;
  355. sp->pvp.pvi.type = PV_IDX_INT;
  356. return 0;
  357. }
  358. int pv_init_iname(pv_spec_p sp, int param)
  359. {
  360. if(sp==NULL)
  361. return -1;
  362. sp->pvp.pvn.type = PV_NAME_INTSTR;
  363. sp->pvp.pvn.u.isname.name.n = param;
  364. return 0;
  365. }
  366. char* pv_parse_spec2(str *in, pv_spec_p e, int silent)
  367. {
  368. char *p;
  369. str s;
  370. str pvname;
  371. int pvstate;
  372. trans_t *tr = NULL;
  373. pv_export_t *pte = NULL;
  374. int n=0;
  375. if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
  376. {
  377. if (!silent) LM_ERR("bad parameters\n");
  378. return NULL;
  379. }
  380. /* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
  381. tr = 0;
  382. pvstate = 0;
  383. memset(e, 0, sizeof(pv_spec_t));
  384. p = in->s;
  385. p++;
  386. if(*p==PV_LNBRACKET)
  387. {
  388. p++;
  389. pvstate = 1;
  390. }
  391. pvname.s = p;
  392. if(*p == PV_MARKER) {
  393. p++;
  394. if(pvstate==1)
  395. {
  396. if(*p!=PV_RNBRACKET)
  397. goto error;
  398. p++;
  399. }
  400. e->getf = pv_get_marker;
  401. e->type = PVT_MARKER;
  402. pvname.len = 1;
  403. goto done_all;
  404. }
  405. while(is_in_str(p,in) && is_pv_valid_char(*p))
  406. p++;
  407. pvname.len = p - pvname.s;
  408. if(pvstate==1)
  409. {
  410. if(*p==PV_RNBRACKET)
  411. { /* full pv name ended here*/
  412. goto done_inm;
  413. } else if(*p==PV_LNBRACKET) {
  414. p++;
  415. pvstate = 2;
  416. } else if(*p==PV_LIBRACKET) {
  417. p++;
  418. pvstate = 3;
  419. } else if(*p==TR_LBRACKET) {
  420. p++;
  421. pvstate = 4;
  422. } else {
  423. if (!silent)
  424. LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
  425. *p, in->len, in->s, pvstate);
  426. goto error;
  427. }
  428. } else {
  429. if(!is_in_str(p, in)) {
  430. p--;
  431. goto done_inm;
  432. } else if(*p==PV_LNBRACKET) {
  433. p++;
  434. pvstate = 5;
  435. } else {
  436. /* still in input str, but end of PV */
  437. /* p is increased at the end, so decrement here */
  438. p--;
  439. goto done_inm;
  440. }
  441. }
  442. done_inm:
  443. if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
  444. {
  445. if (!silent)
  446. LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
  447. goto error;
  448. }
  449. if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
  450. {
  451. if (!silent)
  452. LM_ERR("pvar \"%.*s\" expects an inner name\n",
  453. pvname.len, pvname.s);
  454. goto error;
  455. }
  456. if(pvstate==2 || pvstate==5)
  457. {
  458. if(pte->parse_name==NULL)
  459. {
  460. if (!silent)
  461. LM_ERR("pvar \"%.*s\" does not get name param\n",
  462. pvname.len, pvname.s);
  463. goto error;
  464. }
  465. s.s = p;
  466. n = 0;
  467. while(is_in_str(p, in))
  468. {
  469. if(*p==PV_RNBRACKET)
  470. {
  471. if(n==0)
  472. break;
  473. n--;
  474. }
  475. if(*p == PV_LNBRACKET)
  476. n++;
  477. p++;
  478. }
  479. if(!is_in_str(p, in))
  480. goto error;
  481. if(p==s.s)
  482. {
  483. if (!silent)
  484. LM_ERR("pvar \"%.*s\" does not get empty name param\n",
  485. pvname.len, pvname.s);
  486. goto error;
  487. }
  488. s.len = p - s.s;
  489. if(pte->parse_name(e, &s)!=0)
  490. {
  491. if (!silent)
  492. LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
  493. pvname.len, pvname.s, s.len, s.s);
  494. goto error;
  495. }
  496. if(pvstate==2)
  497. {
  498. p++;
  499. if(*p==PV_RNBRACKET)
  500. { /* full pv name ended here*/
  501. goto done_vnm;
  502. } else if(*p==PV_LIBRACKET) {
  503. p++;
  504. pvstate = 3;
  505. } else if(*p==TR_LBRACKET) {
  506. p++;
  507. pvstate = 4;
  508. } else {
  509. if (!silent)
  510. LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
  511. *p, in->len, in->s, pvstate);
  512. goto error;
  513. }
  514. } else {
  515. if(*p==PV_RNBRACKET)
  516. { /* full pv name ended here*/
  517. p++;
  518. goto done_all;
  519. } else {
  520. if (!silent)
  521. LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
  522. *p, in->len, in->s, pvstate);
  523. goto error;
  524. }
  525. }
  526. }
  527. done_vnm:
  528. if(pvstate==3)
  529. {
  530. if(pte->parse_index==NULL)
  531. {
  532. if (!silent)
  533. LM_ERR("pvar \"%.*s\" does not get index param\n",
  534. pvname.len, pvname.s);
  535. goto error;
  536. }
  537. s.s = p;
  538. n = 0;
  539. while(is_in_str(p, in))
  540. {
  541. if(*p==PV_RIBRACKET)
  542. {
  543. if(n==0)
  544. break;
  545. n--;
  546. }
  547. if(*p == PV_LIBRACKET)
  548. n++;
  549. p++;
  550. }
  551. if(!is_in_str(p, in))
  552. goto error;
  553. if(p==s.s)
  554. {
  555. if (!silent)
  556. LM_ERR("pvar \"%.*s\" does not get empty index param\n",
  557. pvname.len, pvname.s);
  558. goto error;
  559. }
  560. s.len = p - s.s;
  561. if(pte->parse_index(e, &s)!=0)
  562. {
  563. if (!silent)
  564. LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
  565. pvname.len, pvname.s, s.len, s.s);
  566. goto error;
  567. }
  568. p++;
  569. if(*p==PV_RNBRACKET)
  570. { /* full pv name ended here*/
  571. goto done_idx;
  572. } else if(*p==TR_LBRACKET) {
  573. p++;
  574. pvstate = 4;
  575. } else {
  576. if (!silent)
  577. LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
  578. *p, in->len, in->s, pvstate);
  579. goto error;
  580. }
  581. }
  582. done_idx:
  583. if(pvstate==4)
  584. {
  585. s.s = p-1;
  586. n = 0;
  587. while(is_in_str(p, in))
  588. {
  589. if(*p==TR_RBRACKET)
  590. {
  591. if(n==0)
  592. {
  593. /* yet another transformation */
  594. p++;
  595. while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
  596. if(!is_in_str(p, in) || *p != TR_LBRACKET)
  597. {
  598. p--;
  599. break;
  600. }
  601. }
  602. n--;
  603. }
  604. if(*p == TR_LBRACKET)
  605. n++;
  606. p++;
  607. }
  608. if(!is_in_str(p, in))
  609. goto error;
  610. if(p==s.s)
  611. {
  612. if (!silent)
  613. LM_ERR("pvar \"%.*s\" does not get empty index param\n",
  614. pvname.len, pvname.s);
  615. goto error;
  616. }
  617. s.len = p - s.s + 1;
  618. p = tr_lookup(&s, &tr);
  619. if(p==NULL)
  620. {
  621. if (!silent)
  622. LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
  623. goto error;
  624. }
  625. if(*p!=PV_RNBRACKET)
  626. {
  627. if (!silent)
  628. LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
  629. goto error;
  630. }
  631. e->trans = (void*)tr;
  632. }
  633. p++;
  634. done_all:
  635. if(pte!=NULL && pte->init_param)
  636. pte->init_param(e, pte->iparam);
  637. return p;
  638. error:
  639. if(p!=NULL){
  640. if (!silent)
  641. LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
  642. in->len, in->s, (int)(p-in->s), pvstate);
  643. }else{
  644. if (!silent)
  645. LM_ERR("invalid parsing in [%.*s] at (%d)\n",
  646. in->len, in->s, pvstate);
  647. }
  648. return NULL;
  649. } /* end: pv_parse_spec */
  650. /**
  651. *
  652. */
  653. int pv_parse_format(str *in, pv_elem_p *el)
  654. {
  655. char *p, *p0;
  656. int n = 0;
  657. pv_elem_p e, e0;
  658. str s;
  659. if(in==NULL || in->s==NULL || el==NULL)
  660. return -1;
  661. /*LM_DBG("parsing [%.*s]\n", in->len, in->s);*/
  662. if(in->len == 0)
  663. {
  664. *el = pkg_malloc(sizeof(pv_elem_t));
  665. if(*el == NULL)
  666. goto error;
  667. memset(*el, 0, sizeof(pv_elem_t));
  668. (*el)->text = *in;
  669. return 0;
  670. }
  671. p = in->s;
  672. *el = NULL;
  673. e = e0 = NULL;
  674. while(is_in_str(p,in))
  675. {
  676. e0 = e;
  677. e = pkg_malloc(sizeof(pv_elem_t));
  678. if(!e)
  679. goto error;
  680. memset(e, 0, sizeof(pv_elem_t));
  681. n++;
  682. if(*el == NULL)
  683. *el = e;
  684. if(e0)
  685. e0->next = e;
  686. e->text.s = p;
  687. while(is_in_str(p,in) && *p!=PV_MARKER)
  688. p++;
  689. e->text.len = p - e->text.s;
  690. if(*p == '\0' || !is_in_str(p,in))
  691. break;
  692. s.s = p;
  693. s.len = in->s+in->len-p;
  694. p0 = pv_parse_spec(&s, &e->spec);
  695. if(p0==NULL)
  696. goto error;
  697. if(*p0 == '\0')
  698. break;
  699. p = p0;
  700. }
  701. /*LM_DBG("format parsed OK: [%d] items\n", n);*/
  702. if(*el == NULL)
  703. return -1;
  704. return 0;
  705. error:
  706. pv_elem_free_all(*el);
  707. *el = NULL;
  708. return -1;
  709. }
  710. int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name)
  711. {
  712. if(msg==NULL || ip==NULL || name==NULL)
  713. return -1;
  714. memset(name, 0, sizeof(pv_value_t));
  715. if(ip->pvn.type==PV_NAME_INTSTR)
  716. {
  717. if(ip->pvn.u.isname.type&AVP_NAME_STR)
  718. {
  719. name->rs = ip->pvn.u.isname.name.s;
  720. name->flags = PV_VAL_STR;
  721. } else {
  722. name->ri = ip->pvn.u.isname.name.n;
  723. name->flags = PV_VAL_INT|PV_TYPE_INT;
  724. }
  725. return 0;
  726. } else if(ip->pvn.type==PV_NAME_PVAR) {
  727. /* pvar */
  728. if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), name)!=0)
  729. {
  730. LM_ERR("cannot get name value\n");
  731. return -1;
  732. }
  733. if(name->flags&PV_VAL_NULL || name->flags&PV_VAL_EMPTY)
  734. {
  735. LM_ERR("null or empty name\n");
  736. return -1;
  737. }
  738. return 0;
  739. }
  740. LM_ERR("name type is PV_NAME_OTHER - cannot resolve\n");
  741. return -1;
  742. }
  743. int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
  744. unsigned short *name_type)
  745. {
  746. pv_value_t tv;
  747. if(ip==NULL || avp_name==NULL || name_type==NULL)
  748. return -1;
  749. memset(avp_name, 0, sizeof(int_str));
  750. *name_type = 0;
  751. if(ip->pvn.type==PV_NAME_INTSTR)
  752. {
  753. *name_type = ip->pvn.u.isname.type;
  754. if(ip->pvn.u.isname.type&AVP_NAME_STR)
  755. {
  756. avp_name->s = ip->pvn.u.isname.name.s;
  757. *name_type |= AVP_NAME_STR;
  758. } else {
  759. avp_name->n = ip->pvn.u.isname.name.n;
  760. /* *name_type &= AVP_SCRIPT_MASK; */
  761. *name_type = 0;
  762. }
  763. return 0;
  764. }
  765. /* pvar */
  766. if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
  767. {
  768. LM_ERR("cannot get avp value\n");
  769. return -1;
  770. }
  771. if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
  772. {
  773. LM_ERR("null or empty name\n");
  774. return -1;
  775. }
  776. if((tv.flags&PV_TYPE_INT) && (tv.flags&PV_VAL_INT))
  777. {
  778. avp_name->n = tv.ri;
  779. } else {
  780. avp_name->s = tv.rs;
  781. *name_type = AVP_NAME_STR;
  782. }
  783. return 0;
  784. }
  785. int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags)
  786. {
  787. pv_value_t tv;
  788. if(ip==NULL || idx==NULL || flags==NULL)
  789. return -1;
  790. *idx = 0;
  791. *flags = 0;
  792. if(ip->pvi.type == PV_IDX_ALL) {
  793. *flags = PV_IDX_ALL;
  794. return 0;
  795. }
  796. if(ip->pvi.type == PV_IDX_INT)
  797. {
  798. *idx = ip->pvi.u.ival;
  799. return 0;
  800. }
  801. /* pvar */
  802. if(pv_get_spec_value(msg, (pv_spec_p)ip->pvi.u.dval, &tv)!=0)
  803. {
  804. LM_ERR("cannot get index value\n");
  805. return -1;
  806. }
  807. if(!(tv.flags&PV_VAL_INT))
  808. {
  809. LM_ERR("invalid index value\n");
  810. return -1;
  811. }
  812. *idx = tv.ri;
  813. return 0;
  814. }
  815. int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value)
  816. {
  817. int ret = 0;
  818. if(msg==NULL || sp==NULL || sp->getf==NULL || value==NULL
  819. || sp->type==PVT_NONE)
  820. {
  821. LM_ERR("bad parameters\n");
  822. return -1;
  823. }
  824. memset(value, 0, sizeof(pv_value_t));
  825. ret = (*sp->getf)(msg, &(sp->pvp), value);
  826. if(ret!=0)
  827. return ret;
  828. if(sp->trans)
  829. return tr_exec(msg, (trans_t*)sp->trans, value);
  830. return ret;
  831. }
  832. int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
  833. pv_value_t *value)
  834. {
  835. if(sp==NULL || !pv_is_w(sp))
  836. return 0; /* no op */
  837. if(pv_alter_context(sp) && is_route_type(LOCAL_ROUTE))
  838. return 0; /* no op */
  839. return sp->setf(msg, &sp->pvp, op, value);
  840. }
  841. /**
  842. *
  843. */
  844. int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
  845. {
  846. int n, h;
  847. pv_value_t tok;
  848. pv_elem_p it;
  849. char *cur;
  850. if(msg==NULL || list==NULL || buf==NULL || len==NULL)
  851. return -1;
  852. if(*len <= 0)
  853. return -1;
  854. *buf = '\0';
  855. cur = buf;
  856. h = 0;
  857. n = 0;
  858. for (it=list; it; it=it->next)
  859. {
  860. /* put the text */
  861. if(it->text.s && it->text.len>0)
  862. {
  863. if(n+it->text.len < *len)
  864. {
  865. memcpy(cur, it->text.s, it->text.len);
  866. n += it->text.len;
  867. cur += it->text.len;
  868. } else {
  869. LM_ERR("no more space for text [%d]\n", it->text.len);
  870. goto overflow;
  871. }
  872. }
  873. /* put the value of the specifier */
  874. if(it->spec.type!=PVT_NONE
  875. && pv_get_spec_value(msg, &(it->spec), &tok)==0)
  876. {
  877. if(tok.flags&PV_VAL_NULL)
  878. tok.rs = pv_str_null;
  879. if(n+tok.rs.len < *len)
  880. {
  881. if(tok.rs.len>0)
  882. {
  883. memcpy(cur, tok.rs.s, tok.rs.len);
  884. n += tok.rs.len;
  885. cur += tok.rs.len;
  886. }
  887. } else {
  888. LM_ERR("no more space for spec value\n");
  889. goto overflow;
  890. }
  891. }
  892. }
  893. goto done;
  894. overflow:
  895. LM_ERR("buffer overflow -- increase the buffer size...\n");
  896. return -1;
  897. done:
  898. #ifdef EXTRA_DEBUG
  899. LM_DBG("final buffer length %d\n", n);
  900. #endif
  901. *cur = '\0';
  902. *len = n;
  903. return 0;
  904. }
  905. /**
  906. *
  907. */
  908. pvname_list_t* parse_pvname_list(str *in, unsigned int type)
  909. {
  910. pvname_list_t* head = NULL;
  911. pvname_list_t* al = NULL;
  912. pvname_list_t* last = NULL;
  913. char *p;
  914. pv_spec_t spec;
  915. str s;
  916. if(in==NULL || in->s==NULL)
  917. {
  918. LM_ERR("bad parameters\n");
  919. return NULL;
  920. }
  921. p = in->s;
  922. while(is_in_str(p, in))
  923. {
  924. while(is_in_str(p, in) && (*p==' '||*p=='\t'||*p==','||*p==';'))
  925. p++;
  926. if(!is_in_str(p, in))
  927. {
  928. if(head==NULL)
  929. LM_ERR("wrong item name list [%.*s]\n", in->len, in->s);
  930. return head;
  931. }
  932. s.s=p;
  933. s.len = in->s+in->len-p;
  934. p = pv_parse_spec(&s, &spec);
  935. if(p==NULL || (type && spec.type!=type))
  936. {
  937. LM_ERR("wrong item name list [%.*s]!\n", in->len, in->s);
  938. goto error;
  939. }
  940. al = (pvname_list_t*)pkg_malloc(sizeof(pvname_list_t));
  941. if(al==NULL)
  942. {
  943. LM_ERR("no more memory!\n");
  944. goto error;
  945. }
  946. memset(al, 0, sizeof(pvname_list_t));
  947. memcpy(&al->sname, &spec, sizeof(pv_spec_t));
  948. if(last==NULL)
  949. {
  950. head = al;
  951. last = al;
  952. } else {
  953. last->next = al;
  954. last = al;
  955. }
  956. }
  957. return head;
  958. error:
  959. while(head)
  960. {
  961. al = head;
  962. head=head->next;
  963. pkg_free(al);
  964. }
  965. return NULL;
  966. }
  967. /**
  968. *
  969. */
  970. void pv_spec_free(pv_spec_t *spec)
  971. {
  972. if(spec==0) return;
  973. /* TODO: free name if it is PV */
  974. if(spec->trans)
  975. tr_free((trans_t*)spec->trans);
  976. pkg_free(spec);
  977. }
  978. /**
  979. *
  980. */
  981. int pv_elem_free_all(pv_elem_p log)
  982. {
  983. pv_elem_p t;
  984. while(log)
  985. {
  986. t = log;
  987. log = log->next;
  988. pkg_free(t);
  989. }
  990. return 0;
  991. }
  992. /**
  993. *
  994. */
  995. void pv_value_destroy(pv_value_t *val)
  996. {
  997. if(val==0) return;
  998. if(val->flags&PV_VAL_PKG) pkg_free(val->rs.s);
  999. if(val->flags&PV_VAL_SHM) shm_free(val->rs.s);
  1000. memset(val, 0, sizeof(pv_value_t));
  1001. }
  1002. #define PV_PRINT_BUF_SIZE 1024
  1003. #define PV_PRINT_BUF_NO 3
  1004. int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
  1005. {
  1006. static int buf_itr = 0;
  1007. static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
  1008. s->s = buf[buf_itr];
  1009. s->len = PV_PRINT_BUF_SIZE;
  1010. buf_itr = (buf_itr+1)%PV_PRINT_BUF_NO;
  1011. return pv_printf( msg, list, s->s, &s->len);
  1012. }
  1013. /********************************************************
  1014. * Transformations API
  1015. ********************************************************/
  1016. /**
  1017. *
  1018. */
  1019. static inline char* tr_get_class(str *in, char *p, str *tclass)
  1020. {
  1021. tclass->s = p;
  1022. while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
  1023. if(*p!=TR_CLASS_MARKER || tclass->s == p)
  1024. {
  1025. LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
  1026. return NULL;
  1027. }
  1028. tclass->len = p - tclass->s;
  1029. p++;
  1030. return p;
  1031. }
  1032. /**
  1033. *
  1034. */
  1035. static inline trans_t* tr_new(void)
  1036. {
  1037. trans_t *t = NULL;
  1038. t = (trans_t*)pkg_malloc(sizeof(trans_t));
  1039. if(t == NULL)
  1040. {
  1041. LM_ERR("no more private memory\n");
  1042. return NULL;
  1043. }
  1044. memset(t, 0, sizeof(trans_t));
  1045. return t;
  1046. }
  1047. char* tr_lookup(str *in, trans_t **tr)
  1048. {
  1049. char *p;
  1050. char *p0;
  1051. str tclass;
  1052. tr_export_t *te = NULL;
  1053. trans_t *t = NULL;
  1054. trans_t *t0 = NULL;
  1055. str s;
  1056. if(in==NULL || in->s==NULL || tr==NULL)
  1057. return NULL;
  1058. p = in->s;
  1059. do {
  1060. while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
  1061. if(*p != TR_LBRACKET)
  1062. break;
  1063. p++;
  1064. if((t = tr_new())==NULL) return NULL;
  1065. if(t0==NULL) *tr = t;
  1066. else t0->next = t;
  1067. t0 = t;
  1068. /* find transformation class */
  1069. p = tr_get_class(in, p, &tclass);
  1070. if(p==NULL) goto error;
  1071. /* locate transformation */
  1072. te = tr_lookup_class(&tclass);
  1073. if(te==NULL)
  1074. {
  1075. LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
  1076. tclass.len, tclass.s, in->len, in->s);
  1077. goto error;
  1078. }
  1079. s.s = p; s.len = in->s + in->len - p;
  1080. p0 = te->tparse(&s, t);
  1081. if(p0==NULL)
  1082. goto error;
  1083. p = p0;
  1084. if(*p != TR_RBRACKET)
  1085. {
  1086. LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
  1087. in->s, *p);
  1088. goto error;
  1089. }
  1090. p++;
  1091. if(!is_in_str(p, in))
  1092. break;
  1093. } while(1);
  1094. return p;
  1095. error:
  1096. LM_ERR("error parsing [%.*s]\n", in->len, in->s);
  1097. t = *tr;
  1098. while(t)
  1099. {
  1100. t0 = t;
  1101. t = t->next;
  1102. tr_destroy(t0);
  1103. pkg_free(t0);
  1104. }
  1105. return NULL;
  1106. }
  1107. /*!
  1108. * \brief Destroy transformation including eventual parameter
  1109. * \param t transformation
  1110. */
  1111. void tr_destroy(trans_t *t)
  1112. {
  1113. tr_param_t *tp;
  1114. tr_param_t *tp0;
  1115. if(t==NULL) return;
  1116. tp = t->params;
  1117. while(tp)
  1118. {
  1119. tp0 = tp;
  1120. tp = tp->next;
  1121. tr_param_free(tp0);
  1122. }
  1123. memset(t, 0, sizeof(trans_t));
  1124. }
  1125. /*!
  1126. * \brief Exec transformation on a pseudo-variable value
  1127. * \param msg SIP message
  1128. * \param tr one or more transformations
  1129. * \param val pseudo-variable value
  1130. * \return 0 on success, -1 on error
  1131. */
  1132. int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v)
  1133. {
  1134. int r;
  1135. trans_t *i;
  1136. if(t==NULL || v==NULL)
  1137. {
  1138. LM_DBG("invalid parameters\n");
  1139. return -1;
  1140. }
  1141. for(i = t; i!=NULL; i=i->next)
  1142. {
  1143. r = (*i->trf)(msg, i->params, i->subtype, v);
  1144. if(r!=0)
  1145. return r;
  1146. }
  1147. return 0;
  1148. }
  1149. /*!
  1150. * \brief Free allocated memory of transformation list
  1151. * \param t transformation list
  1152. */
  1153. void tr_free(trans_t *t)
  1154. {
  1155. trans_t *t0;
  1156. while(t)
  1157. {
  1158. t0 = t;
  1159. t = t->next;
  1160. tr_destroy(t0);
  1161. pkg_free(t0);
  1162. }
  1163. }
  1164. /*!
  1165. * \brief Free transformation parameter list
  1166. * \param tp transformation list
  1167. */
  1168. void tr_param_free(tr_param_t *tp)
  1169. {
  1170. tr_param_t *tp0;
  1171. if(tp==NULL) return;
  1172. while(tp)
  1173. {
  1174. tp0 = tp;
  1175. tp = tp->next;
  1176. if(tp0->type==TR_PARAM_SPEC)
  1177. pv_spec_free((pv_spec_t*)tp0->v.data);
  1178. pkg_free(tp0);
  1179. }
  1180. }
  1181. typedef struct _tr_item
  1182. {
  1183. tr_export_t tre;
  1184. unsigned int trid;
  1185. struct _tr_item *next;
  1186. } tr_item_t, *tr_item_p;
  1187. static tr_item_t* _tr_table[TR_TABLE_SIZE];
  1188. static int _tr_table_set = 0;
  1189. /**
  1190. *
  1191. */
  1192. void tr_init_table(void)
  1193. {
  1194. memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
  1195. _tr_table_set = 1;
  1196. }
  1197. /**
  1198. *
  1199. */
  1200. int tr_table_add(tr_export_t *e)
  1201. {
  1202. tr_item_t *tri = NULL;
  1203. tr_item_t *trj = NULL;
  1204. tr_item_t *trn = NULL;
  1205. int found;
  1206. unsigned int trid;
  1207. if(e==NULL || e->tclass.s==NULL)
  1208. {
  1209. LM_ERR("invalid parameters\n");
  1210. return -1;
  1211. }
  1212. if(_tr_table_set==0)
  1213. {
  1214. LM_DBG("TR table not initialized, doing it now\n");
  1215. tr_init_table();
  1216. }
  1217. found = 0;
  1218. // trid = get_hash1_raw(e->tclass.s, e->tclass.len);
  1219. trid = get_hash1_raw(e->tclass.s, e->tclass.len);
  1220. tri = _tr_table[trid%TR_TABLE_SIZE];
  1221. while(tri)
  1222. {
  1223. if(tri->tre.tclass.len==e->tclass.len)
  1224. {
  1225. found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
  1226. if(found==0)
  1227. {
  1228. LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
  1229. e->tclass.s);
  1230. return -1;
  1231. }
  1232. }
  1233. trj = tri;
  1234. tri = tri->next;
  1235. }
  1236. trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
  1237. if(trn==0)
  1238. {
  1239. LM_ERR("no more memory\n");
  1240. return -1;
  1241. }
  1242. memset(trn, 0, sizeof(tr_item_t));
  1243. memcpy(&(trn->tre), e, sizeof(tr_export_t));
  1244. trn->trid = trid;
  1245. //LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
  1246. // e->tclass.s, trid%TR_TABLE_SIZE);
  1247. if(trj==0)
  1248. {
  1249. trn->next = _tr_table[trid%TR_TABLE_SIZE];
  1250. _tr_table[trid%TR_TABLE_SIZE] = trn;
  1251. goto done;
  1252. }
  1253. trn->next = trj->next;
  1254. trj->next = trn;
  1255. done:
  1256. return 0;
  1257. }
  1258. /**
  1259. *
  1260. */
  1261. int register_trans_mod(char *mod_name, tr_export_t *items)
  1262. {
  1263. int ret;
  1264. int i;
  1265. if (items==0)
  1266. return 0;
  1267. for ( i=0 ; items[i].tclass.s ; i++ ) {
  1268. ret = tr_table_add(&items[i]);
  1269. if (ret!=0) {
  1270. LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
  1271. items[i].tclass.len, items[i].tclass.s, mod_name);
  1272. }
  1273. }
  1274. return 0;
  1275. }
  1276. /**
  1277. *
  1278. */
  1279. int tr_table_free(void)
  1280. {
  1281. tr_item_p te;
  1282. tr_item_p te1;
  1283. int i;
  1284. for(i=0; i<TR_TABLE_SIZE; i++)
  1285. {
  1286. te = _tr_table[i];
  1287. while(te!=0)
  1288. {
  1289. te1 = te;
  1290. te = te->next;
  1291. pkg_free(te1);
  1292. }
  1293. }
  1294. memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
  1295. _tr_table_set = 0;
  1296. return 0;
  1297. }
  1298. tr_export_t* tr_lookup_class(str *tclass)
  1299. {
  1300. tr_item_t *tri;
  1301. unsigned int trid;
  1302. if(tclass==0 || tclass->s==0)
  1303. {
  1304. LM_ERR("bad parameters\n");
  1305. return NULL;
  1306. }
  1307. /* search in TR table */
  1308. // trid = get_hash1_raw(tclass->s, tclass->len);
  1309. trid = get_hash1_raw(tclass->s, tclass->len);
  1310. tri = _tr_table[trid%TR_TABLE_SIZE];
  1311. while(tri)
  1312. {
  1313. if(tri->trid==trid && tri->tre.tclass.len==tclass->len
  1314. && memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
  1315. return &(tri->tre);
  1316. tri = tri->next;
  1317. }
  1318. return NULL;
  1319. }
  1320. /** init pv api (optional).
  1321. * @return 0 on success, -1 on error
  1322. */
  1323. int init_pv_api(void)
  1324. {
  1325. pv_init_table();
  1326. tr_init_table();
  1327. return 0;
  1328. }
  1329. /** destroy pv api. */
  1330. void destroy_pv_api(void)
  1331. {
  1332. /* free PV and TR hash tables */
  1333. pv_table_free();
  1334. tr_table_free();
  1335. return;
  1336. }