htable.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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. * Kamailio 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. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <sys/ipc.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include "../../sr_module.h"
  30. #include "../../timer.h"
  31. #include "../../route.h"
  32. #include "../../dprint.h"
  33. #include "../../hashes.h"
  34. #include "../../ut.h"
  35. #include "../../rpc.h"
  36. #include "../../rpc_lookup.h"
  37. #include "../../lib/kmi/mi.h"
  38. #include "../../lib/kcore/faked_msg.h"
  39. #include "../../pvar.h"
  40. #include "ht_api.h"
  41. #include "ht_db.h"
  42. #include "ht_var.h"
  43. #include "api.h"
  44. MODULE_VERSION
  45. int ht_timer_interval = 20;
  46. int ht_db_expires_flag = 0;
  47. static int htable_init_rpc(void);
  48. /** module functions */
  49. static int ht_print(struct sip_msg*, char*, char*);
  50. static int mod_init(void);
  51. static int child_init(int rank);
  52. static void destroy(void);
  53. static int fixup_ht_key(void** param, int param_no);
  54. static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo);
  55. static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo);
  56. static int ht_slot_lock(struct sip_msg* msg, char* key, char* foo);
  57. static int ht_slot_unlock(struct sip_msg* msg, char* key, char* foo);
  58. int ht_param(modparam_t type, void* val);
  59. static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param);
  60. static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param);
  61. static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param);
  62. static pv_export_t mod_pvs[] = {
  63. { {"sht", sizeof("sht")-1}, PVT_OTHER, pv_get_ht_cell, pv_set_ht_cell,
  64. pv_parse_ht_name, 0, 0, 0 },
  65. { {"shtex", sizeof("shtex")-1}, PVT_OTHER, pv_get_ht_cell_expire,
  66. pv_set_ht_cell_expire,
  67. pv_parse_ht_name, 0, 0, 0 },
  68. { {"shtcn", sizeof("shtcn")-1}, PVT_OTHER, pv_get_ht_cn, 0,
  69. pv_parse_ht_name, 0, 0, 0 },
  70. { {"shtcv", sizeof("shtcv")-1}, PVT_OTHER, pv_get_ht_cv, 0,
  71. pv_parse_ht_name, 0, 0, 0 },
  72. { {"shtinc", sizeof("shtinc")-1}, PVT_OTHER, pv_get_ht_inc, 0,
  73. pv_parse_ht_name, 0, 0, 0 },
  74. { {"shtdec", sizeof("shtdec")-1}, PVT_OTHER, pv_get_ht_dec, 0,
  75. pv_parse_ht_name, 0, 0, 0 },
  76. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  77. };
  78. static mi_export_t mi_cmds[] = {
  79. { "sht_reload", ht_mi_reload, 0, 0, 0},
  80. { "sht_dump", ht_mi_dump, 0, 0, 0},
  81. { "sht_delete", ht_mi_delete, 0, 0, 0},
  82. { 0, 0, 0, 0, 0}
  83. };
  84. static cmd_export_t cmds[]={
  85. {"sht_print", (cmd_function)ht_print, 0, 0, 0,
  86. ANY_ROUTE},
  87. {"sht_rm_name_re", (cmd_function)ht_rm_name_re, 1, fixup_ht_key, 0,
  88. ANY_ROUTE},
  89. {"sht_rm_value_re", (cmd_function)ht_rm_value_re, 1, fixup_ht_key, 0,
  90. ANY_ROUTE},
  91. {"sht_lock", (cmd_function)ht_slot_lock, 1, fixup_ht_key, 0,
  92. ANY_ROUTE},
  93. {"sht_unlock", (cmd_function)ht_slot_unlock, 1, fixup_ht_key, 0,
  94. ANY_ROUTE},
  95. {"bind_htable", (cmd_function)bind_htable, 0, 0, 0,
  96. ANY_ROUTE},
  97. {0,0,0,0,0,0}
  98. };
  99. static param_export_t params[]={
  100. {"htable", STR_PARAM|USE_FUNC_PARAM, (void*)ht_param},
  101. {"db_url", STR_PARAM, &ht_db_url.s},
  102. {"key_name_column", STR_PARAM, &ht_db_name_column.s},
  103. {"key_type_column", STR_PARAM, &ht_db_ktype_column.s},
  104. {"value_type_column", STR_PARAM, &ht_db_vtype_column.s},
  105. {"key_value_column", STR_PARAM, &ht_db_value_column.s},
  106. {"expires_column", STR_PARAM, &ht_db_expires_column.s},
  107. {"array_size_suffix", STR_PARAM, &ht_array_size_suffix.s},
  108. {"fetch_rows", INT_PARAM, &ht_fetch_rows},
  109. {"timer_interval", INT_PARAM, &ht_timer_interval},
  110. {"db_expires", INT_PARAM, &ht_db_expires_flag},
  111. {0,0,0}
  112. };
  113. /** module exports */
  114. struct module_exports exports= {
  115. "htable",
  116. DEFAULT_DLFLAGS, /* dlopen flags */
  117. cmds,
  118. params,
  119. 0, /* exported statistics */
  120. mi_cmds, /* exported MI functions */
  121. mod_pvs, /* exported pseudo-variables */
  122. 0, /* extra processes */
  123. mod_init, /* module initialization function */
  124. 0,
  125. (destroy_function) destroy,
  126. child_init /* per-child init function */
  127. };
  128. /**
  129. * init module function
  130. */
  131. static int mod_init(void)
  132. {
  133. if(register_mi_mod(exports.name, mi_cmds)!=0)
  134. {
  135. LM_ERR("failed to register MI commands\n");
  136. return -1;
  137. }
  138. if(htable_init_rpc()!=0)
  139. {
  140. LM_ERR("failed to register RPC commands\n");
  141. return -1;
  142. }
  143. if(ht_init_tables()!=0)
  144. return -1;
  145. ht_db_init_params();
  146. if(ht_db_url.len>0)
  147. {
  148. if(ht_db_init_con()!=0)
  149. return -1;
  150. if(ht_db_open_con()!=0)
  151. return -1;
  152. if(ht_db_load_tables()!=0)
  153. {
  154. ht_db_close_con();
  155. return -1;
  156. }
  157. ht_db_close_con();
  158. }
  159. if(ht_has_autoexpire())
  160. {
  161. LM_DBG("starting auto-expire timer\n");
  162. if(ht_timer_interval<=0)
  163. ht_timer_interval = 20;
  164. if(register_timer(ht_timer, 0, ht_timer_interval)<0)
  165. {
  166. LM_ERR("failed to register timer function\n");
  167. return -1;
  168. }
  169. }
  170. return 0;
  171. }
  172. static int child_init(int rank)
  173. {
  174. struct sip_msg *fmsg;
  175. struct run_act_ctx ctx;
  176. int rtb, rt;
  177. LM_DBG("rank is (%d)\n", rank);
  178. if (rank!=PROC_INIT)
  179. return 0;
  180. rt = route_get(&event_rt, "htable:mod-init");
  181. if(rt>=0 && event_rt.rlist[rt]!=NULL) {
  182. LM_DBG("executing event_route[htable:mod-init] (%d)\n", rt);
  183. if(faked_msg_init()<0)
  184. return -1;
  185. fmsg = faked_msg_next();
  186. rtb = get_route_type();
  187. set_route_type(REQUEST_ROUTE);
  188. init_run_actions_ctx(&ctx);
  189. run_top_route(event_rt.rlist[rt], fmsg, &ctx);
  190. if(ctx.run_flags&DROP_R_F)
  191. {
  192. LM_ERR("exit due to 'drop' in event route\n");
  193. return -1;
  194. }
  195. set_route_type(rtb);
  196. }
  197. return 0;
  198. }
  199. /**
  200. * destroy function
  201. */
  202. static void destroy(void)
  203. {
  204. /* sync back to db */
  205. if(ht_db_url.len>0)
  206. {
  207. if(ht_db_init_con()==0)
  208. {
  209. if(ht_db_open_con()==0)
  210. {
  211. ht_db_sync_tables();
  212. ht_db_close_con();
  213. }
  214. }
  215. }
  216. ht_destroy();
  217. }
  218. /**
  219. * print hash table content
  220. */
  221. static int ht_print(struct sip_msg *msg, char *s1, char *s2)
  222. {
  223. ht_dbg();
  224. return 1;
  225. }
  226. static int fixup_ht_key(void** param, int param_no)
  227. {
  228. pv_spec_t *sp;
  229. str s;
  230. sp = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
  231. if(param_no != 1)
  232. {
  233. LM_ERR("invalid parameter number %d\n", param_no);
  234. return -1;
  235. }
  236. if (sp == 0)
  237. {
  238. LM_ERR("no pkg memory left\n");
  239. return -1;
  240. }
  241. memset(sp, 0, sizeof(pv_spec_t));
  242. s.s = (char*)*param; s.len = strlen(s.s);
  243. if(pv_parse_ht_name(sp, &s)<0)
  244. {
  245. pkg_free(sp);
  246. LM_ERR("invalid parameter %d\n", param_no);
  247. return -1;
  248. }
  249. *param = (void*)sp;
  250. return 0;
  251. }
  252. static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo)
  253. {
  254. ht_pv_t *hpv;
  255. str sre;
  256. pv_spec_t *sp;
  257. sp = (pv_spec_t*)key;
  258. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  259. if(hpv->ht==NULL)
  260. {
  261. hpv->ht = ht_get_table(&hpv->htname);
  262. if(hpv->ht==NULL)
  263. return 1;
  264. }
  265. if(pv_printf_s(msg, hpv->pve, &sre)!=0)
  266. {
  267. LM_ERR("cannot get $ht expression\n");
  268. return -1;
  269. }
  270. if(ht_rm_cell_re(&sre, hpv->ht, 0)<0)
  271. return -1;
  272. return 1;
  273. }
  274. static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo)
  275. {
  276. ht_pv_t *hpv;
  277. str sre;
  278. pv_spec_t *sp;
  279. sp = (pv_spec_t*)key;
  280. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  281. if(hpv->ht==NULL)
  282. {
  283. hpv->ht = ht_get_table(&hpv->htname);
  284. if(hpv->ht==NULL)
  285. return 1;
  286. }
  287. if(pv_printf_s(msg, hpv->pve, &sre)!=0)
  288. {
  289. LM_ERR("cannot get $ht expression\n");
  290. return -1;
  291. }
  292. if(ht_rm_cell_re(&sre, hpv->ht, 1)<0)
  293. return -1;
  294. return 1;
  295. }
  296. /**
  297. * lock the slot for a given key in a hash table
  298. */
  299. static int ht_slot_lock(struct sip_msg* msg, char* key, char* foo)
  300. {
  301. ht_pv_t *hpv;
  302. str skey;
  303. pv_spec_t *sp;
  304. unsigned int hid;
  305. unsigned int idx;
  306. sp = (pv_spec_t*)key;
  307. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  308. if(hpv->ht==NULL)
  309. {
  310. hpv->ht = ht_get_table(&hpv->htname);
  311. if(hpv->ht==NULL) {
  312. LM_ERR("cannot get $ht root\n");
  313. return -11;
  314. }
  315. }
  316. if(pv_printf_s(msg, hpv->pve, &skey)!=0)
  317. {
  318. LM_ERR("cannot get $ht key\n");
  319. return -1;
  320. }
  321. hid = ht_compute_hash(&skey);
  322. idx = ht_get_entry(hid, hpv->ht->htsize);
  323. LM_DBG("unlocking slot %.*s[%u] for key %.*s\n",
  324. hpv->htname.len, hpv->htname.s,
  325. idx, skey.len, skey.s);
  326. lock_get(&hpv->ht->entries[idx].lock);
  327. return 1;
  328. }
  329. /**
  330. * unlock the slot for a given key in a hash table
  331. */
  332. static int ht_slot_unlock(struct sip_msg* msg, char* key, char* foo)
  333. {
  334. ht_pv_t *hpv;
  335. str skey;
  336. pv_spec_t *sp;
  337. unsigned int hid;
  338. unsigned int idx;
  339. sp = (pv_spec_t*)key;
  340. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  341. if(hpv->ht==NULL)
  342. {
  343. hpv->ht = ht_get_table(&hpv->htname);
  344. if(hpv->ht==NULL) {
  345. LM_ERR("cannot get $ht root\n");
  346. return -11;
  347. }
  348. }
  349. if(pv_printf_s(msg, hpv->pve, &skey)!=0)
  350. {
  351. LM_ERR("cannot get $ht key\n");
  352. return -1;
  353. }
  354. hid = ht_compute_hash(&skey);
  355. idx = ht_get_entry(hid, hpv->ht->htsize);
  356. LM_DBG("unlocking slot %.*s[%u] for key %.*s\n",
  357. hpv->htname.len, hpv->htname.s,
  358. idx, skey.len, skey.s);
  359. lock_release(&hpv->ht->entries[idx].lock);
  360. return 1;
  361. }
  362. int ht_param(modparam_t type, void *val)
  363. {
  364. if(val==NULL)
  365. goto error;
  366. return ht_table_spec((char*)val);
  367. error:
  368. return -1;
  369. }
  370. #define MI_ERR_RELOAD "ERROR Reloading data"
  371. #define MI_ERR_RELOAD_LEN (sizeof(MI_ERR_RELOAD)-1)
  372. static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param)
  373. {
  374. struct mi_node* node;
  375. str htname;
  376. ht_t *ht;
  377. ht_t nht;
  378. ht_cell_t *first;
  379. ht_cell_t *it;
  380. int i;
  381. if(ht_db_url.len<=0)
  382. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  383. if(ht_db_init_con()!=0)
  384. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  385. if(ht_db_open_con()!=0)
  386. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  387. node = cmd_tree->node.kids;
  388. if(node == NULL)
  389. {
  390. ht_db_close_con();
  391. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  392. }
  393. htname = node->value;
  394. if(htname.len<=0 || htname.s==NULL)
  395. {
  396. LM_ERR("bad hash table name\n");
  397. ht_db_close_con();
  398. return init_mi_tree( 500, "bad hash table name", 19);
  399. }
  400. ht = ht_get_table(&htname);
  401. if(ht==NULL || ht->dbtable.len<=0)
  402. {
  403. LM_ERR("bad hash table name\n");
  404. ht_db_close_con();
  405. return init_mi_tree( 500, "no such hash table", 18);
  406. }
  407. memcpy(&nht, ht, sizeof(ht_t));
  408. nht.entries = (ht_entry_t*)shm_malloc(nht.htsize*sizeof(ht_entry_t));
  409. if(nht.entries == NULL)
  410. {
  411. ht_db_close_con();
  412. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  413. }
  414. memset(nht.entries, 0, nht.htsize*sizeof(ht_entry_t));
  415. if(ht_db_load_table(&nht, &ht->dbtable, 0)<0)
  416. {
  417. ht_db_close_con();
  418. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  419. }
  420. /* replace old entries */
  421. for(i=0; i<nht.htsize; i++)
  422. {
  423. lock_get(&ht->entries[i].lock);
  424. first = ht->entries[i].first;
  425. ht->entries[i].first = nht.entries[i].first;
  426. ht->entries[i].esize = nht.entries[i].esize;
  427. lock_release(&ht->entries[i].lock);
  428. nht.entries[i].first = first;
  429. }
  430. /* free old entries */
  431. for(i=0; i<nht.htsize; i++)
  432. {
  433. first = nht.entries[i].first;
  434. while(first)
  435. {
  436. it = first;
  437. first = first->next;
  438. ht_cell_free(it);
  439. }
  440. }
  441. ht_db_close_con();
  442. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  443. }
  444. static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param) {
  445. struct mi_node *node;
  446. str *htname, *key;
  447. ht_t *ht;
  448. node = cmd_tree->node.kids;
  449. if (!node)
  450. goto param_err;
  451. htname = &node->value;
  452. if (!htname->len)
  453. goto param_err;
  454. node = node->next;
  455. if (!node)
  456. goto param_err;
  457. key = &node->value;
  458. if (!key->len)
  459. goto param_err;
  460. ht = ht_get_table(htname);
  461. if (!ht)
  462. return init_mi_tree(404, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  463. ht_del_cell(ht, key);
  464. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  465. param_err:
  466. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  467. }
  468. static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param)
  469. {
  470. struct mi_node* node;
  471. struct mi_node* node2;
  472. struct mi_root *rpl_tree;
  473. struct mi_node *rpl;
  474. str htname;
  475. ht_t *ht;
  476. ht_cell_t *it;
  477. int i;
  478. int len;
  479. char *p;
  480. node = cmd_tree->node.kids;
  481. if(node == NULL)
  482. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  483. htname = node->value;
  484. if(htname.len<=0 || htname.s==NULL)
  485. {
  486. LM_ERR("bad hash table name\n");
  487. return init_mi_tree( 500, "bad hash table name", 19);
  488. }
  489. ht = ht_get_table(&htname);
  490. if(ht==NULL)
  491. {
  492. LM_ERR("bad hash table name\n");
  493. return init_mi_tree( 500, "no such hash table", 18);
  494. }
  495. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  496. if (rpl_tree==NULL)
  497. return 0;
  498. rpl = &rpl_tree->node;
  499. for(i=0; i<ht->htsize; i++)
  500. {
  501. lock_get(&ht->entries[i].lock);
  502. it = ht->entries[i].first;
  503. if(it)
  504. {
  505. /* add entry node */
  506. p = int2str((unsigned long)i, &len);
  507. node = add_mi_node_child(rpl, MI_DUP_VALUE, "Entry", 5, p, len);
  508. if (node==0)
  509. goto error;
  510. while(it)
  511. {
  512. if(it->flags&AVP_VAL_STR) {
  513. node2 = add_mi_node_child(node, MI_DUP_VALUE, it->name.s, it->name.len,
  514. it->value.s.s, it->value.s.len);
  515. } else {
  516. p = sint2str((long)it->value.n, &len);
  517. node2 = add_mi_node_child(node, MI_DUP_VALUE, it->name.s, it->name.len,
  518. p, len);
  519. }
  520. if (node2==0)
  521. goto error;
  522. it = it->next;
  523. }
  524. }
  525. lock_release(&ht->entries[i].lock);
  526. }
  527. return rpl_tree;
  528. error:
  529. free_mi_tree(rpl_tree);
  530. return 0;
  531. }
  532. static const char* htable_dump_doc[2] = {
  533. "Dump the contents of hash table.",
  534. 0
  535. };
  536. static const char* htable_delete_doc[2] = {
  537. "Delete one key from a hash table.",
  538. 0
  539. };
  540. static const char* htable_get_doc[2] = {
  541. "Get one key from a hash table.",
  542. 0
  543. };
  544. static const char* htable_sets_doc[2] = {
  545. "Set one key in a hash table to a string value.",
  546. 0
  547. };
  548. static const char* htable_seti_doc[2] = {
  549. "Set one key in a hash table to an integer value.",
  550. 0
  551. };
  552. static const char* htable_list_doc[2] = {
  553. "List all htables.",
  554. 0
  555. };
  556. static const char* htable_reload_doc[2] = {
  557. "Reload hash table.",
  558. 0
  559. };
  560. static void htable_rpc_delete(rpc_t* rpc, void* c) {
  561. str htname, keyname;
  562. ht_t *ht;
  563. if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
  564. rpc->fault(c, 500, "Not enough parameters (htable name & key name");
  565. return;
  566. }
  567. ht = ht_get_table(&htname);
  568. if (!ht) {
  569. rpc->fault(c, 500, "No such htable");
  570. return;
  571. }
  572. ht_del_cell(ht, &keyname);
  573. }
  574. /*! \brief RPC htable.get command to get one item */
  575. static void htable_rpc_get(rpc_t* rpc, void* c) {
  576. str htname, keyname;
  577. ht_t *ht;
  578. ht_cell_t *htc; /*!< One HT cell */
  579. void* th;
  580. void* vh;
  581. if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
  582. rpc->fault(c, 500, "Not enough parameters (htable name and key name)");
  583. return;
  584. }
  585. /* Find the htable */
  586. ht = ht_get_table(&htname);
  587. if (!ht) {
  588. rpc->fault(c, 500, "No such htable");
  589. return;
  590. }
  591. /* Find the cell */
  592. htc = ht_cell_pkg_copy(ht, &keyname, NULL);
  593. if(htc == NULL) {
  594. /* Print error message */
  595. rpc->fault(c, 500, "Key name doesn't exist in htable.");
  596. return;
  597. }
  598. /* add entry node */
  599. if (rpc->add(c, "{", &th) < 0) {
  600. rpc->fault(c, 500, "Internal error creating rpc");
  601. goto error;
  602. }
  603. if(rpc->struct_add(th, "{", "item", &vh)<0) {
  604. rpc->fault(c, 500, "Internal error creating rpc");
  605. goto error;
  606. }
  607. if(htc->flags&AVP_VAL_STR) {
  608. if(rpc->struct_add(vh, "SS", "name", &htc->name.s, "value", &htc->value.s)<0)
  609. {
  610. rpc->fault(c, 500, "Internal error adding item");
  611. goto error;
  612. }
  613. } else {
  614. if(rpc->struct_add(vh, "Sd", "name", &htc->name.s, "value", (int)htc->value.n))
  615. {
  616. rpc->fault(c, 500, "Internal error adding item");
  617. goto error;
  618. }
  619. }
  620. error:
  621. /* Release the allocated memory */
  622. ht_cell_pkg_free(htc);
  623. return;
  624. }
  625. /*! \brief RPC htable.sets command to set one item to string value */
  626. static void htable_rpc_sets(rpc_t* rpc, void* c) {
  627. str htname, keyname;
  628. int_str keyvalue;
  629. ht_t *ht;
  630. if (rpc->scan(c, "SS.S", &htname, &keyname, &keyvalue.s) < 3) {
  631. rpc->fault(c, 500,
  632. "Not enough parameters (htable name, key name and value)");
  633. return;
  634. }
  635. /* Find the htable */
  636. ht = ht_get_table(&htname);
  637. if (!ht) {
  638. rpc->fault(c, 500, "No such htable");
  639. return;
  640. }
  641. if(ht_set_cell(ht, &keyname, AVP_VAL_STR, &keyvalue, 1)!=0)
  642. {
  643. LM_ERR("cannot set $ht(%.*s=>%.*s)\n", htname.len, htname.s,
  644. keyname.len, keyname.s);
  645. rpc->fault(c, 500, "Failed to set the item");
  646. return;
  647. }
  648. return;
  649. }
  650. /*! \brief RPC htable.seti command to set one item to integer value */
  651. static void htable_rpc_seti(rpc_t* rpc, void* c) {
  652. str htname, keyname;
  653. int_str keyvalue;
  654. ht_t *ht;
  655. if (rpc->scan(c, "SS.d", &htname, &keyname, &keyvalue.n) < 3) {
  656. rpc->fault(c, 500,
  657. "Not enough parameters (htable name, key name and value)");
  658. return;
  659. }
  660. /* Find the htable */
  661. ht = ht_get_table(&htname);
  662. if (!ht) {
  663. rpc->fault(c, 500, "No such htable");
  664. return;
  665. }
  666. if(ht_set_cell(ht, &keyname, 0, &keyvalue, 1)!=0)
  667. {
  668. LM_ERR("cannot set $ht(%.*s=>%.*s)\n", htname.len, htname.s,
  669. keyname.len, keyname.s);
  670. rpc->fault(c, 500, "Failed to set the item");
  671. return;
  672. }
  673. return;
  674. }
  675. /*! \brief RPC htable.dump command to print content of a hash table */
  676. static void htable_rpc_dump(rpc_t* rpc, void* c)
  677. {
  678. str htname;
  679. ht_t *ht;
  680. ht_cell_t *it;
  681. int i;
  682. void* th;
  683. void* ih;
  684. void* vh;
  685. if (rpc->scan(c, "S", &htname) < 1)
  686. {
  687. rpc->fault(c, 500, "No htable name given");
  688. return;
  689. }
  690. ht = ht_get_table(&htname);
  691. if(ht==NULL)
  692. {
  693. rpc->fault(c, 500, "No such htable");
  694. return;
  695. }
  696. for(i=0; i<ht->htsize; i++)
  697. {
  698. lock_get(&ht->entries[i].lock);
  699. it = ht->entries[i].first;
  700. if(it)
  701. {
  702. /* add entry node */
  703. if (rpc->add(c, "{", &th) < 0)
  704. {
  705. rpc->fault(c, 500, "Internal error creating rpc");
  706. goto error;
  707. }
  708. if(rpc->struct_add(th, "dd{",
  709. "entry", i,
  710. "size", (int)ht->entries[i].esize,
  711. "slot", &ih)<0)
  712. {
  713. rpc->fault(c, 500, "Internal error creating rpc");
  714. goto error;
  715. }
  716. while(it)
  717. {
  718. if(rpc->struct_add(ih, "{",
  719. "item", &vh)<0)
  720. {
  721. rpc->fault(c, 500, "Internal error creating rpc");
  722. goto error;
  723. }
  724. if(it->flags&AVP_VAL_STR) {
  725. if(rpc->struct_add(vh, "SS",
  726. "name", &it->name.s,
  727. "value", &it->value.s)<0)
  728. {
  729. rpc->fault(c, 500, "Internal error adding item");
  730. goto error;
  731. }
  732. } else {
  733. if(rpc->struct_add(vh, "Sd",
  734. "name", &it->name.s,
  735. "value", (int)it->value.n))
  736. {
  737. rpc->fault(c, 500, "Internal error adding item");
  738. goto error;
  739. }
  740. }
  741. it = it->next;
  742. }
  743. }
  744. lock_release(&ht->entries[i].lock);
  745. }
  746. return;
  747. error:
  748. lock_release(&ht->entries[i].lock);
  749. }
  750. static void htable_rpc_list(rpc_t* rpc, void* c)
  751. {
  752. ht_t *ht;
  753. void* th;
  754. char dbname[128];
  755. ht = ht_get_root();
  756. if(ht==NULL)
  757. {
  758. rpc->fault(c, 500, "No htables");
  759. return;
  760. }
  761. while (ht != NULL)
  762. {
  763. int len = 0;
  764. /* add entry node */
  765. if (rpc->add(c, "{", &th) < 0)
  766. {
  767. rpc->fault(c, 500, "Internal error creating structure rpc");
  768. goto error;
  769. }
  770. if (ht->dbtable.len > 0) {
  771. len = ht->dbtable.len > 127 ? 127 : ht->dbtable.len;
  772. memcpy(dbname, ht->dbtable.s, len);
  773. dbname[ht->dbtable.len] = '\0';
  774. } else {
  775. dbname[0] = '\0';
  776. }
  777. if(rpc->struct_add(th, "Ssdddd",
  778. "name", &ht->name, /* String */
  779. "dbtable", &dbname , /* Char * */
  780. "dbmode", (int) ht->dbmode, /* u int */
  781. "expire", (int) ht->htexpire, /* u int */
  782. "updateexpire", ht->updateexpire, /* int */
  783. "size", (int) ht->htsize /* u int */
  784. ) < 0) {
  785. rpc->fault(c, 500, "Internal error creating data rpc");
  786. goto error;
  787. }
  788. ht = ht->next;
  789. }
  790. error:
  791. return;
  792. }
  793. /*! \brief RPC htable.reload command to reload content of a hash table */
  794. static void htable_rpc_reload(rpc_t* rpc, void* c)
  795. {
  796. str htname;
  797. ht_t *ht;
  798. ht_t nht;
  799. ht_cell_t *first;
  800. ht_cell_t *it;
  801. int i;
  802. if(ht_db_url.len<=0) {
  803. rpc->fault(c, 500, "No htable db_url");
  804. return;
  805. }
  806. if(ht_db_init_con()!=0) {
  807. rpc->fault(c, 500, "Failed to init htable db connection");
  808. return;
  809. }
  810. if(ht_db_open_con()!=0) {
  811. rpc->fault(c, 500, "Failed to open htable db connection");
  812. return;
  813. }
  814. if (rpc->scan(c, "S", &htname) < 1)
  815. {
  816. rpc->fault(c, 500, "No htable name given");
  817. return;
  818. }
  819. ht = ht_get_table(&htname);
  820. if(ht==NULL)
  821. {
  822. rpc->fault(c, 500, "No such htable");
  823. return;
  824. }
  825. memcpy(&nht, ht, sizeof(ht_t));
  826. nht.entries = (ht_entry_t*)shm_malloc(nht.htsize*sizeof(ht_entry_t));
  827. if(nht.entries == NULL)
  828. {
  829. ht_db_close_con();
  830. rpc->fault(c, 500, "Mtree reload failed");
  831. return;
  832. }
  833. memset(nht.entries, 0, nht.htsize*sizeof(ht_entry_t));
  834. if(ht_db_load_table(&nht, &ht->dbtable, 0)<0)
  835. {
  836. ht_db_close_con();
  837. rpc->fault(c, 500, "Mtree reload failed");
  838. return;
  839. }
  840. /* replace old entries */
  841. for(i=0; i<nht.htsize; i++)
  842. {
  843. lock_get(&ht->entries[i].lock);
  844. first = ht->entries[i].first;
  845. ht->entries[i].first = nht.entries[i].first;
  846. ht->entries[i].esize = nht.entries[i].esize;
  847. lock_release(&ht->entries[i].lock);
  848. nht.entries[i].first = first;
  849. }
  850. /* free old entries */
  851. for(i=0; i<nht.htsize; i++)
  852. {
  853. first = nht.entries[i].first;
  854. while(first)
  855. {
  856. it = first;
  857. first = first->next;
  858. ht_cell_free(it);
  859. }
  860. }
  861. ht_db_close_con();
  862. return;
  863. }
  864. rpc_export_t htable_rpc[] = {
  865. {"htable.dump", htable_rpc_dump, htable_dump_doc, 0},
  866. {"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
  867. {"htable.get", htable_rpc_get, htable_get_doc, 0},
  868. {"htable.sets", htable_rpc_sets, htable_sets_doc, 0},
  869. {"htable.seti", htable_rpc_seti, htable_seti_doc, 0},
  870. {"htable.listTables", htable_rpc_list, htable_list_doc, 0},
  871. {"htable.reload", htable_rpc_reload, htable_reload_doc, 0},
  872. {0, 0, 0, 0}
  873. };
  874. static int htable_init_rpc(void)
  875. {
  876. if (rpc_register_array(htable_rpc)!=0)
  877. {
  878. LM_ERR("failed to register RPC commands\n");
  879. return -1;
  880. }
  881. return 0;
  882. }