mtree.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file 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. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include "../../dprint.h"
  30. #include "../../mem/shm_mem.h"
  31. #include "../../parser/parse_param.h"
  32. #include "../../ut.h"
  33. #include "../../pvar.h"
  34. #include "../../lvalue.h"
  35. #include "../../shm_init.h"
  36. #include "mtree.h"
  37. //extern str mt_char_list = {"1234567890*",11};
  38. extern str mt_char_list;
  39. extern pv_spec_t pv_value;
  40. extern pv_spec_t pv_values;
  41. extern pv_spec_t pv_dstid;
  42. extern pv_spec_t pv_weight;
  43. extern int _mt_tree_type;
  44. extern int _mt_ignore_duplicates;
  45. extern int _mt_allow_duplicates;
  46. /** structures containing prefix-value pairs */
  47. static m_tree_t **_ptree = NULL;
  48. /* quick transaltion table */
  49. unsigned char _mt_char_table[256];
  50. /**
  51. *
  52. */
  53. void mt_char_table_init(void)
  54. {
  55. unsigned int i;
  56. for(i=0; i<=255; i++)
  57. _mt_char_table[i] = 255;
  58. for(i=0; i<mt_char_list.len; i++)
  59. _mt_char_table[(unsigned int)mt_char_list.s[i]] = (unsigned char)i;
  60. }
  61. /**
  62. *
  63. */
  64. m_tree_t *mt_swap_list_head(m_tree_t *ntree)
  65. {
  66. m_tree_t *otree;
  67. otree = *_ptree;
  68. *_ptree = ntree;
  69. return otree;
  70. }
  71. /**
  72. *
  73. */
  74. int mt_init_list_head(void)
  75. {
  76. if(_ptree!=NULL)
  77. return 0;
  78. _ptree = (m_tree_t**)shm_malloc( sizeof(m_tree_t*) );
  79. if (_ptree==0) {
  80. LM_ERR("out of shm mem for pdtree\n");
  81. return -1;
  82. }
  83. *_ptree=0;
  84. return 0;
  85. }
  86. /**
  87. *
  88. */
  89. m_tree_t* mt_init_tree(str* tname, str *dbtable, int type, int multi)
  90. {
  91. m_tree_t *pt = NULL;
  92. pt = (m_tree_t*)shm_malloc(sizeof(m_tree_t));
  93. if(pt==NULL)
  94. {
  95. LM_ERR("no more shm memory\n");
  96. return NULL;
  97. }
  98. memset(pt, 0, sizeof(m_tree_t));
  99. pt->type = type;
  100. pt->multi = multi;
  101. pt->reload_time = (unsigned int)time(NULL);
  102. pt->tname.s = (char*)shm_malloc((1+tname->len)*sizeof(char));
  103. if(pt->tname.s==NULL)
  104. {
  105. shm_free(pt);
  106. LM_ERR("no more shm memory\n");
  107. return NULL;
  108. }
  109. memset(pt->tname.s, 0, 1+tname->len);
  110. memcpy(pt->tname.s, tname->s, tname->len);
  111. pt->tname.len = tname->len;
  112. pt->dbtable.s = (char*)shm_malloc((1+dbtable->len)*sizeof(char));
  113. if(pt->dbtable.s==NULL)
  114. {
  115. shm_free(pt->tname.s);
  116. shm_free(pt);
  117. LM_ERR("no more shm memory\n");
  118. return NULL;
  119. }
  120. memset(pt->dbtable.s, 0, 1+dbtable->len);
  121. memcpy(pt->dbtable.s, dbtable->s, dbtable->len);
  122. pt->dbtable.len = dbtable->len;
  123. return pt;
  124. }
  125. int mt_add_to_tree(m_tree_t *pt, str *sp, str *svalue)
  126. {
  127. int l, ivalue = 0;
  128. mt_node_t *itn, *itn0;
  129. mt_is_t *tvalues;
  130. if(pt==NULL || sp==NULL || sp->s==NULL
  131. || svalue==NULL || svalue->s==NULL)
  132. {
  133. LM_ERR("bad parameters\n");
  134. return -1;
  135. }
  136. if(sp->len>=MT_MAX_DEPTH)
  137. {
  138. LM_ERR("max prefix len exceeded\n");
  139. return -1;
  140. }
  141. LM_DBG("adding to tree <%.*s> of type <%d>\n", pt->tname.len,
  142. pt->tname.s, pt->type);
  143. if ((pt->type == MT_TREE_IVAL) && (str2sint(svalue, &ivalue) != 0)) {
  144. LM_ERR("bad integer string <%.*s>\n", svalue->len, svalue->s);
  145. return -1;
  146. }
  147. l = 0;
  148. if(pt->head == NULL)
  149. {
  150. pt->head = (mt_node_t*)shm_malloc(MT_NODE_SIZE*sizeof(mt_node_t));
  151. if(pt->head == NULL)
  152. {
  153. LM_ERR("no more shm memory for tree head\n");
  154. return -1;
  155. }
  156. memset(pt->head, 0, MT_NODE_SIZE*sizeof(mt_node_t));
  157. pt->nrnodes++;
  158. pt->memsize += MT_NODE_SIZE*sizeof(mt_node_t);
  159. }
  160. itn0 = pt->head;
  161. if(_mt_char_table[(unsigned int)sp->s[l]]==255)
  162. {
  163. LM_ERR("invalid char %d in prefix [%c (0x%x)]\n",
  164. l, sp->s[l], sp->s[l]);
  165. return -1;
  166. }
  167. itn = itn0[_mt_char_table[(unsigned int)sp->s[l]]].child;
  168. while(l < sp->len-1)
  169. {
  170. if(itn == NULL)
  171. {
  172. itn = (mt_node_t*)shm_malloc(MT_NODE_SIZE*sizeof(mt_node_t));
  173. if(itn == NULL)
  174. {
  175. LM_ERR("no more shm mem\n");
  176. return -1;
  177. }
  178. memset(itn, 0, MT_NODE_SIZE*sizeof(mt_node_t));
  179. pt->nrnodes++;
  180. pt->memsize += MT_NODE_SIZE*sizeof(mt_node_t);
  181. itn0[_mt_char_table[(unsigned int)sp->s[l]]].child = itn;
  182. }
  183. l++;
  184. if(_mt_char_table[(unsigned int)sp->s[l]]==255)
  185. {
  186. LM_ERR("invalid char %d in prefix [%c (0x%x)]\n",
  187. l, sp->s[l], sp->s[l]);
  188. return -1;
  189. }
  190. itn0 = itn;
  191. itn = itn0[_mt_char_table[(unsigned int)sp->s[l]]].child;
  192. }
  193. if(itn0[_mt_char_table[(unsigned int)sp->s[l]]].tvalues != NULL) {
  194. if(_mt_ignore_duplicates != 0) {
  195. LM_NOTICE("prefix already allocated [%.*s/%.*s]\n",
  196. sp->len, sp->s, svalue->len, svalue->s);
  197. return 1;
  198. } else if (_mt_allow_duplicates == 0) {
  199. LM_ERR("prefix already allocated [%.*s/%.*s]\n",
  200. sp->len, sp->s, svalue->len, svalue->s);
  201. return -1;
  202. }
  203. }
  204. tvalues = (mt_is_t *)shm_malloc(sizeof(mt_is_t));
  205. if (tvalues == NULL) {
  206. LM_ERR("no more shm mem for tvalue\n");
  207. return -1;
  208. }
  209. memset(tvalues, 0, sizeof(mt_is_t));
  210. if (pt->type == MT_TREE_IVAL) {
  211. tvalues->tvalue.n = ivalue;
  212. } else { /* pt->type == MT_TREE_SVAL or MT_TREE_DW */
  213. tvalues->tvalue.s.s = (char*)shm_malloc((svalue->len+1)*sizeof(char));
  214. if (tvalues->tvalue.s.s == NULL) {
  215. LM_ERR("no more shm mem for string\n");
  216. return -1;
  217. }
  218. tvalues->tvalue.s.len = svalue->len;
  219. pt->memsize += (svalue->len+1)*sizeof(char);
  220. pt->nritems++;
  221. strncpy(tvalues->tvalue.s.s, svalue->s, svalue->len);
  222. tvalues->tvalue.s.s[svalue->len] = '\0';
  223. }
  224. tvalues->next = itn0[_mt_char_table[(unsigned int)sp->s[l]]].tvalues;
  225. itn0[_mt_char_table[(unsigned int)sp->s[l]]].tvalues = tvalues;
  226. mt_node_set_payload(&itn0[_mt_char_table[(unsigned int)sp->s[l]]],
  227. pt->type);
  228. return 0;
  229. }
  230. m_tree_t* mt_get_tree(str *tname)
  231. {
  232. m_tree_t *it;
  233. int ret;
  234. if(_ptree==NULL || *_ptree==NULL)
  235. return NULL;
  236. if( tname==NULL || tname->s==NULL)
  237. {
  238. LM_ERR("bad parameters\n");
  239. return NULL;
  240. }
  241. it = *_ptree;
  242. /* search the tree for the asked tname */
  243. while(it!=NULL)
  244. {
  245. ret = str_strcmp(&it->tname, tname);
  246. if(ret>0)
  247. return NULL;
  248. if(ret==0)
  249. return it;
  250. it = it->next;
  251. }
  252. return it;
  253. }
  254. m_tree_t* mt_get_first_tree()
  255. {
  256. if(_ptree==NULL || *_ptree==NULL)
  257. return NULL;
  258. return *_ptree;
  259. }
  260. is_t* mt_get_tvalue(m_tree_t *pt, str *tomatch, int *len)
  261. {
  262. int l;
  263. mt_node_t *itn;
  264. is_t *tvalue;
  265. if(pt==NULL || tomatch==NULL || tomatch->s==NULL || len == NULL)
  266. {
  267. LM_ERR("bad parameters\n");
  268. return NULL;
  269. }
  270. l = 0;
  271. itn = pt->head;
  272. tvalue = NULL;
  273. while(itn!=NULL && l < tomatch->len && l < MT_MAX_DEPTH)
  274. {
  275. /* check validity */
  276. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255)
  277. {
  278. LM_DBG("not matching char at %d in [%.*s]\n",
  279. l, tomatch->len, tomatch->s);
  280. return NULL;
  281. }
  282. if(itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues!=NULL)
  283. {
  284. tvalue = &itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues->tvalue;
  285. }
  286. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  287. l++;
  288. }
  289. *len = l;
  290. return tvalue;
  291. }
  292. int mt_add_tvalues(struct sip_msg *msg, m_tree_t *pt, str *tomatch)
  293. {
  294. int l, n;
  295. mt_node_t *itn;
  296. int_str val, values_avp_name;
  297. unsigned short values_name_type;
  298. mt_is_t *tvalues;
  299. if (pt == NULL || tomatch == NULL || tomatch->s == NULL) {
  300. LM_ERR("bad parameters\n");
  301. return -1;
  302. }
  303. if (pv_get_avp_name(msg, &pv_values.pvp, &values_avp_name,
  304. &values_name_type) < 0) {
  305. LM_ERR("cannot get values avp name\n");
  306. return -1;
  307. }
  308. destroy_avps(values_name_type, values_avp_name, 1);
  309. l = n = 0;
  310. itn = pt->head;
  311. while (itn != NULL && l < tomatch->len && l < MT_MAX_DEPTH) {
  312. /* check validity */
  313. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) {
  314. LM_ERR("invalid char at %d in [%.*s]\n",
  315. l, tomatch->len, tomatch->s);
  316. return -1;
  317. }
  318. tvalues = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues;
  319. while (tvalues != NULL) {
  320. if (pt->type == MT_TREE_IVAL) {
  321. val.n = tvalues->tvalue.n;
  322. LM_DBG("adding avp <%.*s> with value <i:%d>\n",
  323. values_avp_name.s.len, values_avp_name.s.s, val.n);
  324. add_avp(values_name_type, values_avp_name, val);
  325. } else { /* pt->type == MT_TREE_SVAL */
  326. val.s = tvalues->tvalue.s;
  327. LM_DBG("adding avp <%.*s> with value <s:%.*s>\n",
  328. values_avp_name.s.len, values_avp_name.s.s, val.s.len,
  329. val.s.s);
  330. add_avp(values_name_type|AVP_VAL_STR, values_avp_name, val);
  331. }
  332. n++;
  333. tvalues = tvalues->next;
  334. }
  335. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  336. l++;
  337. }
  338. if (n > 0)
  339. return 0;
  340. else
  341. return -1;
  342. }
  343. int mt_match_prefix(struct sip_msg *msg, m_tree_t *it,
  344. str *tomatch, int mode)
  345. {
  346. int l, len, n;
  347. int i, j, k = 0;
  348. mt_node_t *itn;
  349. is_t *tvalue;
  350. int_str dstid_avp_name;
  351. unsigned short dstid_name_type;
  352. int_str weight_avp_name;
  353. unsigned short weight_name_type;
  354. int_str avp_value;
  355. mt_dw_t *dw;
  356. pv_value_t val;
  357. #define MT_MAX_DST_LIST 64
  358. unsigned int tmp_list[2*(MT_MAX_DST_LIST+1)];
  359. if(it==NULL || tomatch == NULL
  360. || tomatch->s == NULL)
  361. {
  362. LM_ERR("bad parameters\n");
  363. return -1;
  364. }
  365. l = len = 0;
  366. n = 0;
  367. if ((it->type==MT_TREE_SVAL) || (it->type==MT_TREE_IVAL)) {
  368. if (mode == 2)
  369. return mt_add_tvalues(msg, it, tomatch);
  370. tvalue = mt_get_tvalue(it, tomatch, &k);
  371. if (tvalue == NULL) {
  372. LM_DBG("no match for: %.*s\n", tomatch->len, tomatch->s);
  373. return -1;
  374. }
  375. memset(&val, 0, sizeof(pv_value_t));
  376. if (it->type==MT_TREE_SVAL) {
  377. val.flags = PV_VAL_STR;
  378. val.rs = tvalue->s;
  379. if(pv_value.setf(msg, &pv_value.pvp, (int)EQ_T, &val)<0) {
  380. LM_ERR("setting PV failed\n");
  381. return -2;
  382. }
  383. } else {
  384. val.flags = PV_VAL_INT;
  385. val.ri = tvalue->n;
  386. if(pv_value.setf(msg, &pv_value.pvp, (int)EQ_T, &val)<0) {
  387. LM_ERR("setting PV failed\n");
  388. return -2;
  389. }
  390. }
  391. return 0;
  392. }
  393. if(it->type!=MT_TREE_DW)
  394. return -1; /* wrong tree type */
  395. if(pv_get_avp_name(msg, &pv_dstid.pvp, &dstid_avp_name,
  396. &dstid_name_type)<0)
  397. {
  398. LM_ERR("cannot get dstid avp name\n");
  399. return -1;
  400. }
  401. if(pv_get_avp_name(msg, &pv_weight.pvp, &weight_avp_name,
  402. &weight_name_type)<0)
  403. {
  404. LM_ERR("cannot get weight avp name\n");
  405. return -1;
  406. }
  407. itn = it->head;
  408. memset(tmp_list, 0, sizeof(unsigned int)*2*(MT_MAX_DST_LIST+1));
  409. while(itn!=NULL && l < tomatch->len && l < MT_MAX_DEPTH)
  410. {
  411. /* check validity */
  412. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255)
  413. {
  414. LM_ERR("invalid char at %d in [%.*s]\n",
  415. l, tomatch->len, tomatch->s);
  416. return -1;
  417. }
  418. if(itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues!=NULL)
  419. {
  420. dw = (mt_dw_t*)itn[_mt_char_table[(unsigned int)tomatch->s[l]]].data;
  421. while(dw) {
  422. tmp_list[2*n]=dw->dstid;
  423. tmp_list[2*n+1]=dw->weight;
  424. n++;
  425. if(n==MT_MAX_DST_LIST)
  426. break;
  427. dw = dw->next;
  428. }
  429. len = l+1;
  430. }
  431. if(n==MT_MAX_DST_LIST)
  432. break;
  433. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  434. l++;
  435. }
  436. if(n==0)
  437. return -1; /* no match */
  438. /* invalidate duplicated dstid, keeping longest match */
  439. for(i=(n-1); i>0; i--)
  440. {
  441. if (tmp_list[2*i]!=0)
  442. {
  443. for(j=0; j<i; j++)
  444. if(tmp_list[2*i]==tmp_list[2*j])
  445. tmp_list[2*j] = 0;
  446. }
  447. }
  448. /* sort the table -- bubble sort -- reverse order */
  449. for (i = (n - 1); i >= 0; i--)
  450. {
  451. for (j = 1; j <= i; j++)
  452. {
  453. if (tmp_list[2*(j-1)+1] < tmp_list[2*j+1])
  454. {
  455. tmp_list[2*MT_MAX_DST_LIST] = tmp_list[2*(j-1)];
  456. tmp_list[2*MT_MAX_DST_LIST+1] = tmp_list[2*(j-1)+1];
  457. tmp_list[2*(j-1)] = tmp_list[2*j];
  458. tmp_list[2*(j-1)+1] = tmp_list[2*j+1];
  459. tmp_list[2*j] = tmp_list[2*MT_MAX_DST_LIST];
  460. tmp_list[2*j+1] = tmp_list[2*MT_MAX_DST_LIST+1];
  461. }
  462. }
  463. }
  464. /* add as avp */
  465. for(i=0; i<n; i++)
  466. {
  467. if(tmp_list[2*i]!=0)
  468. {
  469. avp_value.n = (int)tmp_list[2*i+1];
  470. add_avp(weight_name_type, weight_avp_name, avp_value);
  471. avp_value.n = (int)tmp_list[2*i];
  472. add_avp(dstid_name_type, dstid_avp_name, avp_value);
  473. }
  474. }
  475. return 0;
  476. }
  477. void mt_free_node(mt_node_t *pn, int type)
  478. {
  479. int i;
  480. mt_is_t *tvalues, *next;
  481. if(pn==NULL)
  482. return;
  483. for(i=0; i<MT_NODE_SIZE; i++) {
  484. tvalues = pn[i].tvalues;
  485. while (tvalues != NULL) {
  486. if ((type == MT_TREE_SVAL) && (tvalues->tvalue.s.s != NULL)) {
  487. shm_free(tvalues->tvalue.s.s);
  488. tvalues->tvalue.s.s = NULL;
  489. tvalues->tvalue.s.len = 0;
  490. }
  491. next = tvalues->next;
  492. shm_free(tvalues);
  493. tvalues = next;
  494. }
  495. if(type==MT_TREE_DW)
  496. mt_node_unset_payload(&pn[i], type);
  497. if(pn[i].child!=NULL) {
  498. mt_free_node(pn[i].child, type);
  499. pn[i].child = NULL;
  500. }
  501. }
  502. shm_free(pn);
  503. pn = NULL;
  504. return;
  505. }
  506. void mt_free_tree(m_tree_t *pt)
  507. {
  508. if(pt == NULL)
  509. return;
  510. if(pt->head!=NULL)
  511. mt_free_node(pt->head, pt->type);
  512. if(pt->next!=NULL)
  513. mt_free_tree(pt->next);
  514. if(pt->dbtable.s!=NULL)
  515. shm_free(pt->dbtable.s);
  516. if(pt->tname.s!=NULL)
  517. shm_free(pt->tname.s);
  518. shm_free(pt);
  519. pt = NULL;
  520. return;
  521. }
  522. int mt_print_node(mt_node_t *pn, char *code, int len, int type)
  523. {
  524. int i;
  525. mt_is_t *tvalues;
  526. if(pn==NULL || code==NULL || len>=MT_MAX_DEPTH)
  527. return 0;
  528. for(i=0; i<MT_NODE_SIZE; i++)
  529. {
  530. code[len]=mt_char_list.s[i];
  531. tvalues = pn[i].tvalues;
  532. while (tvalues != NULL) {
  533. if (type == MT_TREE_IVAL) {
  534. LM_INFO("[%.*s] [i:%d]\n", len+1, code, tvalues->tvalue.n);
  535. } else if (tvalues->tvalue.s.s != NULL) {
  536. LM_INFO("[%.*s] [s:%.*s]\n",
  537. len+1, code, tvalues->tvalue.s.len, tvalues->tvalue.s.s);
  538. }
  539. tvalues = tvalues->next;
  540. }
  541. mt_print_node(pn[i].child, code, len+1, type);
  542. }
  543. return 0;
  544. }
  545. static char mt_code_buf[MT_MAX_DEPTH+1];
  546. int mt_print_tree(m_tree_t *pt)
  547. {
  548. int len;
  549. if(pt == NULL)
  550. {
  551. LM_DBG("tree is empty\n");
  552. return 0;
  553. }
  554. LM_INFO("[%.*s]\n", pt->tname.len, pt->tname.s);
  555. len = 0;
  556. mt_print_node(pt->head, mt_code_buf, len, pt->type);
  557. return mt_print_tree(pt->next);
  558. }
  559. int mt_node_set_payload(mt_node_t *node, int type)
  560. {
  561. param_t *list;
  562. param_t *it;
  563. param_hooks_t hooks;
  564. str s;
  565. mt_dw_t *dwl;
  566. mt_dw_t *dw;
  567. if(type!=MT_TREE_DW)
  568. return 0;
  569. s = node->tvalues->tvalue.s;
  570. if(s.s[s.len-1]==';')
  571. s.len--;
  572. if(parse_params(&s, CLASS_ANY, &hooks, &list)<0)
  573. {
  574. LM_ERR("cannot parse tvalue payload [%.*s]\n", s.len, s.s);
  575. return -1;
  576. }
  577. dwl = NULL;
  578. for(it=list; it; it=it->next)
  579. {
  580. dw = (mt_dw_t*)shm_malloc(sizeof(mt_dw_t));
  581. if(dw==NULL)
  582. {
  583. LM_ERR("no more shm\n");
  584. goto error;
  585. }
  586. memset(dw, 0, sizeof(mt_dw_t));
  587. str2int(&it->name, &dw->dstid);
  588. str2int(&it->body, &dw->weight);
  589. dw->next = dwl;
  590. dwl = dw;
  591. }
  592. node->data = (void*)dwl;
  593. free_params(list);
  594. return 0;
  595. error:
  596. while(dwl)
  597. {
  598. dw=dwl;
  599. dwl=dwl->next;
  600. shm_free(dwl);
  601. }
  602. free_params(list);
  603. return -1;
  604. }
  605. int mt_node_unset_payload(mt_node_t *node, int type)
  606. {
  607. mt_dw_t *dwl;
  608. mt_dw_t *dw;
  609. if(type!=MT_TREE_DW)
  610. return 0;
  611. dwl = (mt_dw_t*)node->data;
  612. while(dwl)
  613. {
  614. dw=dwl;
  615. dwl=dwl->next;
  616. shm_free(dw);
  617. }
  618. node->data = NULL;
  619. return 0;
  620. }
  621. int mt_table_spec(char* val)
  622. {
  623. param_t* params_list = NULL;
  624. param_hooks_t phooks;
  625. param_t *pit=NULL;
  626. m_tree_t tmp;
  627. m_tree_t *it, *prev, *ndl;
  628. str s;
  629. if(val==NULL)
  630. return -1;
  631. if(!shm_initialized())
  632. {
  633. LM_ERR("shm not intialized - cannot define mtree now\n");
  634. return 0;
  635. }
  636. s.s = val;
  637. s.len = strlen(s.s);
  638. if(s.s[s.len-1]==';')
  639. s.len--;
  640. if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0)
  641. return -1;
  642. memset(&tmp, 0, sizeof(m_tree_t));
  643. for (pit = params_list; pit; pit=pit->next)
  644. {
  645. if (pit->name.len==4
  646. && strncasecmp(pit->name.s, "name", 4)==0) {
  647. tmp.tname = pit->body;
  648. } else if(pit->name.len==4
  649. && strncasecmp(pit->name.s, "type", 4)==0) {
  650. str2sint(&pit->body, &tmp.type);
  651. } else if(pit->name.len==5
  652. && strncasecmp(pit->name.s, "multi", 5)==0) {
  653. str2sint(&pit->body, &tmp.multi);
  654. } else if(pit->name.len==7
  655. && strncasecmp(pit->name.s, "dbtable", 7)==0) {
  656. tmp.dbtable = pit->body;
  657. }
  658. }
  659. if(tmp.tname.s==NULL)
  660. {
  661. LM_ERR("invalid mtree name\n");
  662. goto error;
  663. }
  664. if(tmp.dbtable.s==NULL)
  665. {
  666. LM_INFO("no table name - default mtree\n");
  667. tmp.dbtable.s = "mtree";
  668. tmp.dbtable.len = 5;
  669. }
  670. if ((tmp.type != 0) && (tmp.type != 1) && (tmp.type != 2)) {
  671. LM_ERR("unknown tree type <%d>\n", tmp.type);
  672. goto error;
  673. }
  674. if ((tmp.multi != 0) && (tmp.multi != 1)) {
  675. LM_ERR("unknown multi value <%d>\n", tmp.multi);
  676. goto error;
  677. }
  678. /* check for same tree */
  679. if(_ptree == 0)
  680. {
  681. /* tree list head in shm */
  682. _ptree = (m_tree_t**)shm_malloc( sizeof(m_tree_t*) );
  683. if (_ptree==0)
  684. {
  685. LM_ERR("out of shm mem for ptree\n");
  686. goto error;
  687. }
  688. *_ptree=0;
  689. }
  690. it = *_ptree;
  691. prev = NULL;
  692. /* search the it position before which to insert new tvalue */
  693. while(it!=NULL && str_strcmp(&it->tname, &tmp.tname)<0)
  694. {
  695. prev = it;
  696. it = it->next;
  697. }
  698. /* found */
  699. if(it!=NULL && str_strcmp(&it->tname, &tmp.tname)==0)
  700. {
  701. LM_ERR("duplicate tree with name [%s]\n", tmp.tname.s);
  702. goto error;
  703. }
  704. /* add new tname*/
  705. if(it==NULL || str_strcmp(&it->tname, &tmp.tname)>0)
  706. {
  707. LM_DBG("adding new tname [%s]\n", tmp.tname.s);
  708. ndl = mt_init_tree(&tmp.tname, &tmp.dbtable, tmp.type,
  709. tmp.multi);
  710. if(ndl==NULL)
  711. {
  712. LM_ERR("no more shm memory\n");
  713. goto error;
  714. }
  715. ndl->next = it;
  716. /* new tvalue must be added as first element */
  717. if(prev==NULL)
  718. *_ptree = ndl;
  719. else
  720. prev->next=ndl;
  721. }
  722. free_params(params_list);
  723. return 0;
  724. error:
  725. free_params(params_list);
  726. return -1;
  727. }
  728. m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, int type,
  729. int multi)
  730. {
  731. m_tree_t *it = NULL;
  732. m_tree_t *prev = NULL;
  733. m_tree_t *ndl = NULL;
  734. if(dpt==NULL)
  735. return NULL;
  736. it = *dpt;
  737. prev = NULL;
  738. /* search the it position before which to insert new tvalue */
  739. while(it!=NULL && str_strcmp(&it->tname, tname)<0)
  740. {
  741. prev = it;
  742. it = it->next;
  743. }
  744. if(it!=NULL && str_strcmp(&it->tname, tname)==0)
  745. {
  746. return it;
  747. }
  748. /* add new tname*/
  749. if(it==NULL || str_strcmp(&it->tname, tname)>0)
  750. {
  751. LM_DBG("adding new tname [%s]\n", tname->s);
  752. ndl = mt_init_tree(tname, dbtable, type, multi);
  753. if(ndl==NULL)
  754. {
  755. LM_ERR("no more shm memory\n");
  756. return NULL;
  757. }
  758. ndl->next = it;
  759. /* new tvalue must be added as first element */
  760. if(prev==NULL)
  761. *dpt = ndl;
  762. else
  763. prev->next=ndl;
  764. }
  765. return ndl;
  766. }
  767. void mt_destroy_trees(void)
  768. {
  769. if (_ptree!=NULL)
  770. {
  771. if (*_ptree!=NULL)
  772. mt_free_tree(*_ptree);
  773. shm_free(_ptree);
  774. _ptree = NULL;
  775. }
  776. }
  777. int mt_defined_trees(void)
  778. {
  779. if (_ptree!=NULL && *_ptree!=NULL)
  780. return 1;
  781. return 0;
  782. }
  783. int mt_mi_add_tvalues(struct mi_node *rpl, m_tree_t *pt, str *tomatch)
  784. {
  785. int l;
  786. mt_node_t *itn;
  787. mt_is_t *tvalues;
  788. struct mi_attr* attr= NULL;
  789. struct mi_node *node = NULL;
  790. if (pt == NULL || tomatch == NULL || tomatch->s == NULL) {
  791. LM_ERR("bad parameters\n");
  792. return -1;
  793. }
  794. l = 0;
  795. itn = pt->head;
  796. while (itn != NULL && l < tomatch->len && l < MT_MAX_DEPTH) {
  797. /* check validity */
  798. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) {
  799. LM_ERR("invalid char at %d in [%.*s]\n",
  800. l, tomatch->len, tomatch->s);
  801. return -1;
  802. }
  803. tvalues = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues;
  804. while (tvalues != NULL) {
  805. node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0);
  806. if(node == NULL)
  807. return -1;
  808. if( add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
  809. pt->tname.s, pt->tname.len) == NULL)
  810. return -1;
  811. if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7,
  812. tomatch->s, l+1) == NULL)
  813. return -1;
  814. if (pt->type == MT_TREE_IVAL) {
  815. attr = addf_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
  816. "%d", tvalues->tvalue.n);
  817. } else { /* pt->type == MT_TREE_SVAL */
  818. attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
  819. tvalues->tvalue.s.s, tvalues->tvalue.s.len);
  820. }
  821. tvalues = tvalues->next;
  822. if (attr == NULL)
  823. return -1;
  824. }
  825. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  826. l++;
  827. }
  828. if (node == NULL) return -1;
  829. return 0;
  830. }
  831. int mt_mi_match_prefix(struct mi_node *rpl, m_tree_t *it,
  832. str *tomatch, int mode)
  833. {
  834. int l, len, n;
  835. int i, j;
  836. mt_node_t *itn;
  837. is_t *tvalue;
  838. mt_dw_t *dw;
  839. int tprefix_len = 0;
  840. struct mi_node *node;
  841. struct mi_attr* attr= NULL;
  842. #define MT_MAX_DST_LIST 64
  843. unsigned int tmp_list[2*(MT_MAX_DST_LIST+1)];
  844. if(it==NULL || tomatch == NULL
  845. || tomatch->s == NULL)
  846. {
  847. LM_ERR("bad parameters\n");
  848. return -1;
  849. }
  850. l = len = 0;
  851. n = 0;
  852. if ((it->type==MT_TREE_SVAL) || (it->type==MT_TREE_IVAL)) {
  853. if (mode == 2)
  854. return mt_mi_add_tvalues(rpl, it, tomatch);
  855. tvalue = mt_get_tvalue(it, tomatch, &tprefix_len);
  856. if (tvalue == NULL) {
  857. LM_DBG("no match for: %.*s\n", tomatch->len, tomatch->s);
  858. return -1;
  859. }
  860. if (tvalue) {
  861. node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0);
  862. if(node == NULL)
  863. return -1;
  864. if( add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
  865. it->tname.s, it->tname.len) == NULL)
  866. return -1;
  867. if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7,
  868. tomatch->s, tprefix_len) == NULL)
  869. return -1;
  870. if (it->type==MT_TREE_SVAL) {
  871. attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
  872. tvalue->s.s, tvalue->s.len);
  873. } else {
  874. attr = addf_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
  875. "%d", tvalue->n);
  876. }
  877. if (attr == NULL)
  878. return -1;
  879. }
  880. return 0;
  881. }
  882. if(it->type!=MT_TREE_DW)
  883. return -1; /* wrong tree type */
  884. itn = it->head;
  885. memset(tmp_list, 0, sizeof(unsigned int)*2*(MT_MAX_DST_LIST+1));
  886. while(itn!=NULL && l < tomatch->len && l < MT_MAX_DEPTH)
  887. {
  888. /* check validity */
  889. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255)
  890. {
  891. LM_ERR("invalid char at %d in [%.*s]\n",
  892. l, tomatch->len, tomatch->s);
  893. return -1;
  894. }
  895. if(itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues!=NULL)
  896. {
  897. dw = (mt_dw_t*)itn[_mt_char_table[(unsigned int)tomatch->s[l]]].data;
  898. while(dw) {
  899. tmp_list[2*n]=dw->dstid;
  900. tmp_list[2*n+1]=dw->weight;
  901. n++;
  902. if(n==MT_MAX_DST_LIST)
  903. break;
  904. dw = dw->next;
  905. }
  906. len = l+1;
  907. }
  908. if(n==MT_MAX_DST_LIST)
  909. break;
  910. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  911. l++;
  912. }
  913. if(n==0)
  914. return -1; /* no match */
  915. /* invalidate duplicated dstid, keeping longest match */
  916. for(i=(n-1); i>0; i--)
  917. {
  918. if (tmp_list[2*i]!=0)
  919. {
  920. for(j=0; j<i; j++)
  921. if(tmp_list[2*i]==tmp_list[2*j])
  922. tmp_list[2*j] = 0;
  923. }
  924. }
  925. /* sort the table -- bubble sort -- reverse order */
  926. for (i = (n - 1); i >= 0; i--)
  927. {
  928. for (j = 1; j <= i; j++)
  929. {
  930. if (tmp_list[2*(j-1)+1] < tmp_list[2*j+1])
  931. {
  932. tmp_list[2*MT_MAX_DST_LIST] = tmp_list[2*(j-1)];
  933. tmp_list[2*MT_MAX_DST_LIST+1] = tmp_list[2*(j-1)+1];
  934. tmp_list[2*(j-1)] = tmp_list[2*j];
  935. tmp_list[2*(j-1)+1] = tmp_list[2*j+1];
  936. tmp_list[2*j] = tmp_list[2*MT_MAX_DST_LIST];
  937. tmp_list[2*j+1] = tmp_list[2*MT_MAX_DST_LIST+1];
  938. }
  939. }
  940. }
  941. /* add as attributes */
  942. for(i=0; i<n; i++)
  943. {
  944. if(tmp_list[2*i]!=0)
  945. {
  946. node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0);
  947. if(node == NULL)
  948. return -1;
  949. if( add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
  950. it->tname.s, it->tname.len) == NULL)
  951. return -1;
  952. if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7,
  953. tomatch->s, len) == NULL)
  954. return -1;
  955. if(addf_mi_attr(node, MI_DUP_VALUE, "WEIGHT", 6,
  956. "%d", (int)tmp_list[2*i+1]) == NULL)
  957. return -1;
  958. if (addf_mi_attr(node, MI_DUP_VALUE, "DSTID", 5,
  959. "%d", (int)tmp_list[2*i]) == NULL)
  960. return -1;
  961. }
  962. }
  963. return 0;
  964. }
  965. int mt_rpc_add_tvalues(rpc_t* rpc, void* ctx, m_tree_t *pt, str *tomatch)
  966. {
  967. int l;
  968. mt_node_t *itn;
  969. mt_is_t *tvalues;
  970. void *vstruct = NULL;
  971. str prefix = *tomatch;
  972. if (pt == NULL || tomatch == NULL || tomatch->s == NULL) {
  973. LM_ERR("bad parameters\n");
  974. return -1;
  975. }
  976. l = 0;
  977. itn = pt->head;
  978. while (itn != NULL && l < tomatch->len && l < MT_MAX_DEPTH) {
  979. /* check validity */
  980. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) {
  981. LM_ERR("invalid char at %d in [%.*s]\n",
  982. l, tomatch->len, tomatch->s);
  983. return -1;
  984. }
  985. tvalues = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues;
  986. while (tvalues != NULL) {
  987. prefix.len = l+1;
  988. if (rpc->add(ctx, "{", &vstruct) < 0) {
  989. rpc->fault(ctx, 500, "Internal error adding struct");
  990. return -1;
  991. }
  992. if(rpc->struct_add(vstruct, "S", "PREFIX", &prefix) < 0) {
  993. rpc->fault(ctx, 500, "Internal error adding prefix");
  994. return -1;
  995. }
  996. if (pt->type == MT_TREE_IVAL) {
  997. if(rpc->struct_add(vstruct, "d", "TVALUE", tvalues->tvalue.n) < 0 ) {
  998. rpc->fault(ctx, 500, "Internal error adding tvalue");
  999. return -1;
  1000. }
  1001. } else { /* pt->type == MT_TREE_SVAL */
  1002. if(rpc->struct_add(vstruct, "S", "TVALUE", &tvalues->tvalue.s) < 0 ) {
  1003. rpc->fault(ctx, 500, "Internal error adding tvalue");
  1004. return -1;
  1005. }
  1006. }
  1007. tvalues = tvalues->next;
  1008. }
  1009. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  1010. l++;
  1011. }
  1012. if (vstruct == NULL) return -1;
  1013. return 0;
  1014. }
  1015. int mt_rpc_match_prefix(rpc_t* rpc, void* ctx, m_tree_t *it,
  1016. str *tomatch, int mode)
  1017. {
  1018. int l, len, n;
  1019. int i, j;
  1020. mt_node_t *itn;
  1021. is_t *tvalue;
  1022. mt_dw_t *dw;
  1023. int tprefix_len = 0;
  1024. str prefix = *tomatch;
  1025. void *vstruct = NULL;
  1026. #define MT_MAX_DST_LIST 64
  1027. unsigned int tmp_list[2*(MT_MAX_DST_LIST+1)];
  1028. if(it==NULL || tomatch == NULL
  1029. || tomatch->s == NULL)
  1030. {
  1031. LM_ERR("bad parameters\n");
  1032. return -1;
  1033. }
  1034. if (rpc->add(ctx, "S", &it->tname) < 0) {
  1035. rpc->fault(ctx, 500, "Internal error adding tname");
  1036. return -1;
  1037. }
  1038. l = len = 0;
  1039. n = 0;
  1040. if ((it->type==MT_TREE_SVAL) || (it->type==MT_TREE_IVAL)) {
  1041. if (mode == 2)
  1042. return mt_rpc_add_tvalues(rpc, ctx, it, tomatch);
  1043. tvalue = mt_get_tvalue(it, tomatch, &tprefix_len);
  1044. if (tvalue == NULL) {
  1045. LM_DBG("no match for: %.*s\n", tomatch->len, tomatch->s);
  1046. return -1;
  1047. }
  1048. if (tvalue) {
  1049. prefix.len = tprefix_len;
  1050. if (rpc->add(ctx, "{", &vstruct) < 0) {
  1051. rpc->fault(ctx, 500, "Internal error adding struct");
  1052. return -1;
  1053. }
  1054. if (rpc->struct_add(vstruct, "S", "PREFIX", &prefix) < 0) {
  1055. rpc->fault(ctx, 500, "Internal error adding prefix");
  1056. return -1;
  1057. }
  1058. if (it->type==MT_TREE_SVAL) {
  1059. if(rpc->struct_add(vstruct, "S", "TVALUE", &tvalue->s) < 0 ) {
  1060. rpc->fault(ctx, 500, "Internal error adding tvalue");
  1061. return -1;
  1062. }
  1063. } else {
  1064. if(rpc->struct_add(vstruct, "d", "TVALUE", tvalue->n) < 0 ) {
  1065. rpc->fault(ctx, 500, "Internal error adding tvalue");
  1066. return -1;
  1067. }
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. if(it->type!=MT_TREE_DW)
  1073. return -1; /* wrong tree type */
  1074. itn = it->head;
  1075. memset(tmp_list, 0, sizeof(unsigned int)*2*(MT_MAX_DST_LIST+1));
  1076. while(itn!=NULL && l < tomatch->len && l < MT_MAX_DEPTH)
  1077. {
  1078. /* check validity */
  1079. if(_mt_char_table[(unsigned int)tomatch->s[l]]==255)
  1080. {
  1081. LM_ERR("invalid char at %d in [%.*s]\n",
  1082. l, tomatch->len, tomatch->s);
  1083. return -1;
  1084. }
  1085. if(itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues!=NULL)
  1086. {
  1087. dw = (mt_dw_t*)itn[_mt_char_table[(unsigned int)tomatch->s[l]]].data;
  1088. while(dw) {
  1089. tmp_list[2*n]=dw->dstid;
  1090. tmp_list[2*n+1]=dw->weight;
  1091. n++;
  1092. if(n==MT_MAX_DST_LIST)
  1093. break;
  1094. dw = dw->next;
  1095. }
  1096. len = l+1;
  1097. }
  1098. if(n==MT_MAX_DST_LIST)
  1099. break;
  1100. itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
  1101. l++;
  1102. }
  1103. if(n==0)
  1104. return -1; /* no match */
  1105. /* invalidate duplicated dstid, keeping longest match */
  1106. for(i=(n-1); i>0; i--)
  1107. {
  1108. if (tmp_list[2*i]!=0)
  1109. {
  1110. for(j=0; j<i; j++)
  1111. if(tmp_list[2*i]==tmp_list[2*j])
  1112. tmp_list[2*j] = 0;
  1113. }
  1114. }
  1115. /* sort the table -- bubble sort -- reverse order */
  1116. for (i = (n - 1); i >= 0; i--)
  1117. {
  1118. for (j = 1; j <= i; j++)
  1119. {
  1120. if (tmp_list[2*(j-1)+1] < tmp_list[2*j+1])
  1121. {
  1122. tmp_list[2*MT_MAX_DST_LIST] = tmp_list[2*(j-1)];
  1123. tmp_list[2*MT_MAX_DST_LIST+1] = tmp_list[2*(j-1)+1];
  1124. tmp_list[2*(j-1)] = tmp_list[2*j];
  1125. tmp_list[2*(j-1)+1] = tmp_list[2*j+1];
  1126. tmp_list[2*j] = tmp_list[2*MT_MAX_DST_LIST];
  1127. tmp_list[2*j+1] = tmp_list[2*MT_MAX_DST_LIST+1];
  1128. }
  1129. }
  1130. }
  1131. prefix.len = len;
  1132. /* add as attributes */
  1133. for(i=0; i<n; i++)
  1134. {
  1135. if(tmp_list[2*i]!=0)
  1136. {
  1137. if (rpc->add(ctx, "{", &vstruct) < 0) {
  1138. rpc->fault(ctx, 500, "Internal error adding struct");
  1139. return -1;
  1140. }
  1141. if (rpc->struct_add(vstruct, "S", "PREFIX", &prefix) < 0) {
  1142. rpc->fault(ctx, 500, "Internal error adding prefix");
  1143. return -1;
  1144. }
  1145. if(rpc->add(vstruct, "dd",
  1146. "WEIGHT", tmp_list[2*i+1],
  1147. "DSTID", tmp_list[2*i]) < 0 ) {
  1148. rpc->fault(ctx, 500, "Internal error adding weight/dstid");
  1149. return -1;
  1150. }
  1151. }
  1152. }
  1153. return 0;
  1154. }