htable.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /**
  2. * Copyright (C) 2008-2014 Elena-Ramona Modroiu (asipto.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #include <sys/ipc.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include "../../sr_module.h"
  28. #include "../../timer.h"
  29. #include "../../route.h"
  30. #include "../../dprint.h"
  31. #include "../../hashes.h"
  32. #include "../../mod_fix.h"
  33. #include "../../ut.h"
  34. #include "../../rpc.h"
  35. #include "../../rpc_lookup.h"
  36. #include "../../lib/kmi/mi.h"
  37. #include "../../lib/kcore/faked_msg.h"
  38. #include "../../pvar.h"
  39. #include "ht_api.h"
  40. #include "ht_db.h"
  41. #include "ht_var.h"
  42. #include "api.h"
  43. #include "ht_dmq.h"
  44. MODULE_VERSION
  45. int ht_timer_interval = 20;
  46. int ht_db_expires_flag = 0;
  47. int ht_enable_dmq = 0;
  48. static int htable_init_rpc(void);
  49. /** module functions */
  50. static int ht_print(struct sip_msg*, char*, char*);
  51. static int mod_init(void);
  52. static int child_init(int rank);
  53. static void destroy(void);
  54. static int fixup_ht_key(void** param, int param_no);
  55. static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo);
  56. static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo);
  57. static int w_ht_slot_lock(struct sip_msg* msg, char* key, char* foo);
  58. static int w_ht_slot_unlock(struct sip_msg* msg, char* key, char* foo);
  59. static int ht_reset(struct sip_msg* msg, char* htname, char* foo);
  60. static int w_ht_iterator_start(struct sip_msg* msg, char* iname, char* hname);
  61. static int w_ht_iterator_next(struct sip_msg* msg, char* iname, char* foo);
  62. static int w_ht_iterator_end(struct sip_msg* msg, char* iname, char* foo);
  63. int ht_param(modparam_t type, void* val);
  64. static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param);
  65. static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param);
  66. static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param);
  67. static pv_export_t mod_pvs[] = {
  68. { {"sht", sizeof("sht")-1}, PVT_OTHER, pv_get_ht_cell, pv_set_ht_cell,
  69. pv_parse_ht_name, 0, 0, 0 },
  70. { {"shtex", sizeof("shtex")-1}, PVT_OTHER, pv_get_ht_cell_expire,
  71. pv_set_ht_cell_expire,
  72. pv_parse_ht_name, 0, 0, 0 },
  73. { {"shtcn", sizeof("shtcn")-1}, PVT_OTHER, pv_get_ht_cn, 0,
  74. pv_parse_ht_name, 0, 0, 0 },
  75. { {"shtcv", sizeof("shtcv")-1}, PVT_OTHER, pv_get_ht_cv, 0,
  76. pv_parse_ht_name, 0, 0, 0 },
  77. { {"shtinc", sizeof("shtinc")-1}, PVT_OTHER, pv_get_ht_inc, 0,
  78. pv_parse_ht_name, 0, 0, 0 },
  79. { {"shtdec", sizeof("shtdec")-1}, PVT_OTHER, pv_get_ht_dec, 0,
  80. pv_parse_ht_name, 0, 0, 0 },
  81. { {"shtrecord", sizeof("shtrecord")-1}, PVT_OTHER, pv_get_ht_expired_cell, 0,
  82. pv_parse_ht_expired_cell, 0, 0, 0 },
  83. { {"shtitkey", sizeof("shtitkey")-1}, PVT_OTHER, pv_get_iterator_key, 0,
  84. pv_parse_iterator_name, 0, 0, 0 },
  85. { {"shtitval", sizeof("shtitval")-1}, PVT_OTHER, pv_get_iterator_val, 0,
  86. pv_parse_iterator_name, 0, 0, 0 },
  87. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  88. };
  89. static mi_export_t mi_cmds[] = {
  90. { "sht_reload", ht_mi_reload, 0, 0, 0},
  91. { "sht_dump", ht_mi_dump, 0, 0, 0},
  92. { "sht_delete", ht_mi_delete, 0, 0, 0},
  93. { 0, 0, 0, 0, 0}
  94. };
  95. static cmd_export_t cmds[]={
  96. {"sht_print", (cmd_function)ht_print, 0, 0, 0,
  97. ANY_ROUTE},
  98. {"sht_rm_name_re", (cmd_function)ht_rm_name_re, 1, fixup_ht_key, 0,
  99. ANY_ROUTE},
  100. {"sht_rm_value_re", (cmd_function)ht_rm_value_re, 1, fixup_ht_key, 0,
  101. ANY_ROUTE},
  102. {"sht_lock", (cmd_function)w_ht_slot_lock, 1, fixup_ht_key, 0,
  103. ANY_ROUTE},
  104. {"sht_unlock", (cmd_function)w_ht_slot_unlock, 1, fixup_ht_key, 0,
  105. ANY_ROUTE},
  106. {"sht_reset", (cmd_function)ht_reset, 1, fixup_spve_null, 0,
  107. ANY_ROUTE},
  108. {"sht_iterator_start", (cmd_function)w_ht_iterator_start, 2, fixup_spve_spve, 0,
  109. ANY_ROUTE},
  110. {"sht_iterator_next", (cmd_function)w_ht_iterator_next, 1, fixup_spve_null, 0,
  111. ANY_ROUTE},
  112. {"sht_iterator_end", (cmd_function)w_ht_iterator_end, 1, fixup_spve_null, 0,
  113. ANY_ROUTE},
  114. {"bind_htable", (cmd_function)bind_htable, 0, 0, 0,
  115. ANY_ROUTE},
  116. {0,0,0,0,0,0}
  117. };
  118. static param_export_t params[]={
  119. {"htable", PARAM_STRING|USE_FUNC_PARAM, (void*)ht_param},
  120. {"db_url", PARAM_STR, &ht_db_url},
  121. {"key_name_column", PARAM_STR, &ht_db_name_column},
  122. {"key_type_column", PARAM_STR, &ht_db_ktype_column},
  123. {"value_type_column", PARAM_STR, &ht_db_vtype_column},
  124. {"key_value_column", PARAM_STR, &ht_db_value_column},
  125. {"expires_column", PARAM_STR, &ht_db_expires_column},
  126. {"array_size_suffix", PARAM_STR, &ht_array_size_suffix},
  127. {"fetch_rows", INT_PARAM, &ht_fetch_rows},
  128. {"timer_interval", INT_PARAM, &ht_timer_interval},
  129. {"db_expires", INT_PARAM, &ht_db_expires_flag},
  130. {"enable_dmq", INT_PARAM, &ht_enable_dmq},
  131. {0,0,0}
  132. };
  133. /** module exports */
  134. struct module_exports exports= {
  135. "htable",
  136. DEFAULT_DLFLAGS, /* dlopen flags */
  137. cmds,
  138. params,
  139. 0, /* exported statistics */
  140. mi_cmds, /* exported MI functions */
  141. mod_pvs, /* exported pseudo-variables */
  142. 0, /* extra processes */
  143. mod_init, /* module initialization function */
  144. 0,
  145. (destroy_function) destroy,
  146. child_init /* per-child init function */
  147. };
  148. /**
  149. * init module function
  150. */
  151. static int mod_init(void)
  152. {
  153. if(register_mi_mod(exports.name, mi_cmds)!=0)
  154. {
  155. LM_ERR("failed to register MI commands\n");
  156. return -1;
  157. }
  158. if(htable_init_rpc()!=0)
  159. {
  160. LM_ERR("failed to register RPC commands\n");
  161. return -1;
  162. }
  163. if(ht_init_tables()!=0)
  164. return -1;
  165. ht_db_init_params();
  166. if(ht_db_url.len>0)
  167. {
  168. if(ht_db_init_con()!=0)
  169. return -1;
  170. if(ht_db_open_con()!=0)
  171. return -1;
  172. if(ht_db_load_tables()!=0)
  173. {
  174. ht_db_close_con();
  175. return -1;
  176. }
  177. ht_db_close_con();
  178. }
  179. if(ht_has_autoexpire())
  180. {
  181. LM_DBG("starting auto-expire timer\n");
  182. if(ht_timer_interval<=0)
  183. ht_timer_interval = 20;
  184. if(register_timer(ht_timer, 0, ht_timer_interval)<0)
  185. {
  186. LM_ERR("failed to register timer function\n");
  187. return -1;
  188. }
  189. }
  190. if (ht_enable_dmq>0 && ht_dmq_initialize()!=0) {
  191. LM_ERR("failed to initialize dmq integration\n");
  192. return -1;
  193. }
  194. ht_iterator_init();
  195. return 0;
  196. }
  197. static int child_init(int rank)
  198. {
  199. struct sip_msg *fmsg;
  200. struct run_act_ctx ctx;
  201. int rtb, rt;
  202. LM_DBG("rank is (%d)\n", rank);
  203. if (rank!=PROC_INIT)
  204. return 0;
  205. rt = route_get(&event_rt, "htable:mod-init");
  206. if(rt>=0 && event_rt.rlist[rt]!=NULL) {
  207. LM_DBG("executing event_route[htable:mod-init] (%d)\n", rt);
  208. if(faked_msg_init()<0)
  209. return -1;
  210. fmsg = faked_msg_next();
  211. rtb = get_route_type();
  212. set_route_type(REQUEST_ROUTE);
  213. init_run_actions_ctx(&ctx);
  214. run_top_route(event_rt.rlist[rt], fmsg, &ctx);
  215. if(ctx.run_flags&DROP_R_F)
  216. {
  217. LM_ERR("exit due to 'drop' in event route\n");
  218. return -1;
  219. }
  220. set_route_type(rtb);
  221. }
  222. return 0;
  223. }
  224. /**
  225. * destroy function
  226. */
  227. static void destroy(void)
  228. {
  229. /* sync back to db */
  230. if(ht_db_url.len>0)
  231. {
  232. if(ht_db_init_con()==0)
  233. {
  234. if(ht_db_open_con()==0)
  235. {
  236. ht_db_sync_tables();
  237. ht_db_close_con();
  238. }
  239. }
  240. }
  241. ht_destroy();
  242. }
  243. /**
  244. * print hash table content
  245. */
  246. static int ht_print(struct sip_msg *msg, char *s1, char *s2)
  247. {
  248. ht_dbg();
  249. return 1;
  250. }
  251. static int fixup_ht_key(void** param, int param_no)
  252. {
  253. pv_spec_t *sp;
  254. str s;
  255. if(param_no != 1)
  256. {
  257. LM_ERR("invalid parameter number %d\n", param_no);
  258. return -1;
  259. }
  260. sp = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
  261. if (sp == 0)
  262. {
  263. LM_ERR("no pkg memory left\n");
  264. return -1;
  265. }
  266. memset(sp, 0, sizeof(pv_spec_t));
  267. s.s = (char*)*param; s.len = strlen(s.s);
  268. if(pv_parse_ht_name(sp, &s)<0)
  269. {
  270. pkg_free(sp);
  271. LM_ERR("invalid parameter %d\n", param_no);
  272. return -1;
  273. }
  274. *param = (void*)sp;
  275. return 0;
  276. }
  277. static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo)
  278. {
  279. ht_pv_t *hpv;
  280. str sre;
  281. pv_spec_t *sp;
  282. sp = (pv_spec_t*)key;
  283. int_str isval;
  284. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  285. if(hpv->ht==NULL)
  286. {
  287. hpv->ht = ht_get_table(&hpv->htname);
  288. if(hpv->ht==NULL)
  289. return 1;
  290. }
  291. if(pv_printf_s(msg, hpv->pve, &sre)!=0)
  292. {
  293. LM_ERR("cannot get $ht expression\n");
  294. return -1;
  295. }
  296. if (hpv->ht->dmqreplicate>0) {
  297. isval.s = sre;
  298. if (ht_dmq_replicate_action(HT_DMQ_RM_CELL_RE, &hpv->htname, NULL, AVP_VAL_STR, &isval, 0)!=0) {
  299. LM_ERR("dmq relication failed\n");
  300. }
  301. }
  302. if(ht_rm_cell_re(&sre, hpv->ht, 0)<0)
  303. return -1;
  304. return 1;
  305. }
  306. static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo)
  307. {
  308. ht_pv_t *hpv;
  309. str sre;
  310. pv_spec_t *sp;
  311. sp = (pv_spec_t*)key;
  312. int_str isval;
  313. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  314. if(hpv->ht==NULL)
  315. {
  316. hpv->ht = ht_get_table(&hpv->htname);
  317. if(hpv->ht==NULL)
  318. return 1;
  319. }
  320. if(pv_printf_s(msg, hpv->pve, &sre)!=0)
  321. {
  322. LM_ERR("cannot get $ht expression\n");
  323. return -1;
  324. }
  325. if (hpv->ht->dmqreplicate>0) {
  326. isval.s = sre;
  327. if (ht_dmq_replicate_action(HT_DMQ_RM_CELL_RE, &hpv->htname, NULL, AVP_VAL_STR, &isval, 1)!=0) {
  328. LM_ERR("dmq relication failed\n");
  329. }
  330. }
  331. if(ht_rm_cell_re(&sre, hpv->ht, 1)<0)
  332. return -1;
  333. return 1;
  334. }
  335. static int ht_reset(struct sip_msg* msg, char* htname, char* foo)
  336. {
  337. ht_t *ht;
  338. str sname;
  339. if(fixup_get_svalue(msg, (gparam_t*)htname, &sname)<0 || sname.len<=0)
  340. {
  341. LM_ERR("cannot get hash table name\n");
  342. return -1;
  343. }
  344. ht = ht_get_table(&sname);
  345. if(ht==NULL)
  346. {
  347. LM_ERR("cannot get hash table [%.*s]\n", sname.len, sname.s);
  348. return -1;
  349. }
  350. if(ht_reset_content(ht)<0)
  351. return -1;
  352. return 1;
  353. }
  354. static int w_ht_iterator_start(struct sip_msg* msg, char* iname, char* hname)
  355. {
  356. str siname;
  357. str shname;
  358. if(fixup_get_svalue(msg, (gparam_t*)iname, &siname)<0 || siname.len<=0)
  359. {
  360. LM_ERR("cannot get iterator name\n");
  361. return -1;
  362. }
  363. if(fixup_get_svalue(msg, (gparam_t*)hname, &shname)<0 || shname.len<=0)
  364. {
  365. LM_ERR("cannot get hash table name\n");
  366. return -1;
  367. }
  368. if(ht_iterator_start(&siname, &shname)<0)
  369. return -1;
  370. return 1;
  371. }
  372. static int w_ht_iterator_next(struct sip_msg* msg, char* iname, char* foo)
  373. {
  374. str siname;
  375. if(fixup_get_svalue(msg, (gparam_t*)iname, &siname)<0 || siname.len<=0)
  376. {
  377. LM_ERR("cannot get iterator name\n");
  378. return -1;
  379. }
  380. if(ht_iterator_next(&siname)<0)
  381. return -1;
  382. return 1;
  383. }
  384. static int w_ht_iterator_end(struct sip_msg* msg, char* iname, char* foo)
  385. {
  386. str siname;
  387. if(fixup_get_svalue(msg, (gparam_t*)iname, &siname)<0 || siname.len<=0)
  388. {
  389. LM_ERR("cannot get iterator name\n");
  390. return -1;
  391. }
  392. if(ht_iterator_end(&siname)<0)
  393. return -1;
  394. return 1;
  395. }
  396. /**
  397. * lock the slot for a given key in a hash table
  398. */
  399. static int w_ht_slot_lock(struct sip_msg* msg, char* key, char* foo)
  400. {
  401. ht_pv_t *hpv;
  402. str skey;
  403. pv_spec_t *sp;
  404. unsigned int hid;
  405. unsigned int idx;
  406. sp = (pv_spec_t*)key;
  407. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  408. if(hpv->ht==NULL)
  409. {
  410. hpv->ht = ht_get_table(&hpv->htname);
  411. if(hpv->ht==NULL) {
  412. LM_ERR("cannot get $ht root\n");
  413. return -11;
  414. }
  415. }
  416. if(pv_printf_s(msg, hpv->pve, &skey)!=0)
  417. {
  418. LM_ERR("cannot get $ht key\n");
  419. return -1;
  420. }
  421. hid = ht_compute_hash(&skey);
  422. idx = ht_get_entry(hid, hpv->ht->htsize);
  423. LM_DBG("unlocking slot %.*s[%u] for key %.*s\n",
  424. hpv->htname.len, hpv->htname.s,
  425. idx, skey.len, skey.s);
  426. ht_slot_lock(hpv->ht, idx);
  427. return 1;
  428. }
  429. /**
  430. * unlock the slot for a given key in a hash table
  431. */
  432. static int w_ht_slot_unlock(struct sip_msg* msg, char* key, char* foo)
  433. {
  434. ht_pv_t *hpv;
  435. str skey;
  436. pv_spec_t *sp;
  437. unsigned int hid;
  438. unsigned int idx;
  439. sp = (pv_spec_t*)key;
  440. hpv = (ht_pv_t*)sp->pvp.pvn.u.dname;
  441. if(hpv->ht==NULL)
  442. {
  443. hpv->ht = ht_get_table(&hpv->htname);
  444. if(hpv->ht==NULL) {
  445. LM_ERR("cannot get $ht root\n");
  446. return -11;
  447. }
  448. }
  449. if(pv_printf_s(msg, hpv->pve, &skey)!=0)
  450. {
  451. LM_ERR("cannot get $ht key\n");
  452. return -1;
  453. }
  454. hid = ht_compute_hash(&skey);
  455. idx = ht_get_entry(hid, hpv->ht->htsize);
  456. LM_DBG("unlocking slot %.*s[%u] for key %.*s\n",
  457. hpv->htname.len, hpv->htname.s,
  458. idx, skey.len, skey.s);
  459. ht_slot_unlock(hpv->ht, idx);
  460. return 1;
  461. }
  462. int ht_param(modparam_t type, void *val)
  463. {
  464. if(val==NULL)
  465. goto error;
  466. return ht_table_spec((char*)val);
  467. error:
  468. return -1;
  469. }
  470. #define MI_ERR_RELOAD "ERROR Reloading data"
  471. #define MI_ERR_RELOAD_LEN (sizeof(MI_ERR_RELOAD)-1)
  472. static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param)
  473. {
  474. struct mi_node* node;
  475. str htname;
  476. ht_t *ht;
  477. ht_t nht;
  478. ht_cell_t *first;
  479. ht_cell_t *it;
  480. int i;
  481. if(ht_db_url.len<=0)
  482. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  483. if(ht_db_init_con()!=0)
  484. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  485. if(ht_db_open_con()!=0)
  486. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  487. node = cmd_tree->node.kids;
  488. if(node == NULL)
  489. {
  490. ht_db_close_con();
  491. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  492. }
  493. htname = node->value;
  494. if(htname.len<=0 || htname.s==NULL)
  495. {
  496. LM_ERR("bad hash table name\n");
  497. ht_db_close_con();
  498. return init_mi_tree( 500, "bad hash table name", 19);
  499. }
  500. ht = ht_get_table(&htname);
  501. if(ht==NULL || ht->dbtable.len<=0)
  502. {
  503. LM_ERR("bad hash table name\n");
  504. ht_db_close_con();
  505. return init_mi_tree( 500, "no such hash table", 18);
  506. }
  507. memcpy(&nht, ht, sizeof(ht_t));
  508. /* it's temporary operation - use system malloc */
  509. nht.entries = (ht_entry_t*)malloc(nht.htsize*sizeof(ht_entry_t));
  510. if(nht.entries == NULL)
  511. {
  512. ht_db_close_con();
  513. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  514. }
  515. memset(nht.entries, 0, nht.htsize*sizeof(ht_entry_t));
  516. if(ht_db_load_table(&nht, &ht->dbtable, 0)<0)
  517. {
  518. /* free any entry set if it was a partial load */
  519. for(i=0; i<nht.htsize; i++)
  520. {
  521. first = nht.entries[i].first;
  522. while(first)
  523. {
  524. it = first;
  525. first = first->next;
  526. ht_cell_free(it);
  527. }
  528. }
  529. free(nht.entries);
  530. ht_db_close_con();
  531. return init_mi_tree(500, MI_ERR_RELOAD, MI_ERR_RELOAD_LEN);
  532. }
  533. /* replace old entries */
  534. for(i=0; i<nht.htsize; i++)
  535. {
  536. ht_slot_lock(ht, i);
  537. first = ht->entries[i].first;
  538. ht->entries[i].first = nht.entries[i].first;
  539. ht->entries[i].esize = nht.entries[i].esize;
  540. ht_slot_unlock(ht, i);
  541. nht.entries[i].first = first;
  542. }
  543. /* free old entries */
  544. for(i=0; i<nht.htsize; i++)
  545. {
  546. first = nht.entries[i].first;
  547. while(first)
  548. {
  549. it = first;
  550. first = first->next;
  551. ht_cell_free(it);
  552. }
  553. }
  554. free(nht.entries);
  555. ht_db_close_con();
  556. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  557. }
  558. static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param) {
  559. struct mi_node *node;
  560. str *htname, *key;
  561. ht_t *ht;
  562. node = cmd_tree->node.kids;
  563. if (!node)
  564. goto param_err;
  565. htname = &node->value;
  566. if (!htname->len)
  567. goto param_err;
  568. node = node->next;
  569. if (!node)
  570. goto param_err;
  571. key = &node->value;
  572. if (!key->len)
  573. goto param_err;
  574. ht = ht_get_table(htname);
  575. if (!ht)
  576. return init_mi_tree(404, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  577. if (ht->dmqreplicate>0 && ht_dmq_replicate_action(HT_DMQ_DEL_CELL, &ht->name, key, 0, NULL, 0)!=0) {
  578. LM_ERR("dmq relication failed\n");
  579. }
  580. LM_DBG("deleting key [%.*s] from [%.*s]\n",
  581. key->len, key->s, htname->len, htname->s);
  582. ht_del_cell(ht, key);
  583. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  584. param_err:
  585. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  586. }
  587. static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param)
  588. {
  589. struct mi_node* node;
  590. struct mi_node* node2;
  591. struct mi_root *rpl_tree;
  592. struct mi_node *rpl;
  593. str htname;
  594. ht_t *ht;
  595. ht_cell_t *it;
  596. int i;
  597. int len;
  598. char *p;
  599. node = cmd_tree->node.kids;
  600. if(node == NULL)
  601. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  602. htname = node->value;
  603. if(htname.len<=0 || htname.s==NULL)
  604. {
  605. LM_ERR("bad hash table name\n");
  606. return init_mi_tree( 500, "bad hash table name", 19);
  607. }
  608. ht = ht_get_table(&htname);
  609. if(ht==NULL)
  610. {
  611. LM_ERR("bad hash table name\n");
  612. return init_mi_tree( 500, "no such hash table", 18);
  613. }
  614. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  615. if (rpl_tree==NULL)
  616. return 0;
  617. rpl = &rpl_tree->node;
  618. for(i=0; i<ht->htsize; i++)
  619. {
  620. ht_slot_lock(ht, i);
  621. it = ht->entries[i].first;
  622. if(it)
  623. {
  624. /* add entry node */
  625. p = int2str((unsigned long)i, &len);
  626. node = add_mi_node_child(rpl, MI_DUP_VALUE, "Entry", 5, p, len);
  627. if (node==0)
  628. goto error;
  629. while(it)
  630. {
  631. if(it->flags&AVP_VAL_STR) {
  632. node2 = add_mi_node_child(node, MI_DUP_VALUE, it->name.s, it->name.len,
  633. it->value.s.s, it->value.s.len);
  634. } else {
  635. p = sint2str((long)it->value.n, &len);
  636. node2 = add_mi_node_child(node, MI_DUP_VALUE, it->name.s, it->name.len,
  637. p, len);
  638. }
  639. if (node2==0)
  640. goto error;
  641. it = it->next;
  642. }
  643. }
  644. ht_slot_unlock(ht, i);
  645. }
  646. return rpl_tree;
  647. error:
  648. free_mi_tree(rpl_tree);
  649. return 0;
  650. }
  651. #define RPC_DATE_BUF_LEN 21
  652. static const char* htable_dump_doc[2] = {
  653. "Dump the contents of hash table.",
  654. 0
  655. };
  656. static const char* htable_delete_doc[2] = {
  657. "Delete one key from a hash table.",
  658. 0
  659. };
  660. static const char* htable_get_doc[2] = {
  661. "Get one key from a hash table.",
  662. 0
  663. };
  664. static const char* htable_sets_doc[2] = {
  665. "Set one key in a hash table to a string value.",
  666. 0
  667. };
  668. static const char* htable_seti_doc[2] = {
  669. "Set one key in a hash table to an integer value.",
  670. 0
  671. };
  672. static const char* htable_list_doc[2] = {
  673. "List all htables.",
  674. 0
  675. };
  676. static const char* htable_stats_doc[2] = {
  677. "Statistics about htables.",
  678. 0
  679. };
  680. static const char* htable_reload_doc[2] = {
  681. "Reload hash table.",
  682. 0
  683. };
  684. static void htable_rpc_delete(rpc_t* rpc, void* c) {
  685. str htname, keyname;
  686. ht_t *ht;
  687. if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
  688. rpc->fault(c, 500, "Not enough parameters (htable name & key name");
  689. return;
  690. }
  691. ht = ht_get_table(&htname);
  692. if (!ht) {
  693. rpc->fault(c, 500, "No such htable");
  694. return;
  695. }
  696. if (ht->dmqreplicate>0 && ht_dmq_replicate_action(HT_DMQ_DEL_CELL, &ht->name, &keyname, 0, NULL, 0)!=0) {
  697. LM_ERR("dmq relication failed\n");
  698. }
  699. ht_del_cell(ht, &keyname);
  700. }
  701. /*! \brief RPC htable.get command to get one item */
  702. static void htable_rpc_get(rpc_t* rpc, void* c) {
  703. str htname, keyname;
  704. ht_t *ht;
  705. ht_cell_t *htc; /*!< One HT cell */
  706. void* th;
  707. void* vh;
  708. struct tm *_expire_t;
  709. char expire_buf[RPC_DATE_BUF_LEN]="NEVER";
  710. if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
  711. rpc->fault(c, 500, "Not enough parameters (htable name and key name)");
  712. return;
  713. }
  714. /* Find the htable */
  715. ht = ht_get_table(&htname);
  716. if (!ht) {
  717. rpc->fault(c, 500, "No such htable");
  718. return;
  719. }
  720. /* Find the cell */
  721. htc = ht_cell_pkg_copy(ht, &keyname, NULL);
  722. if(htc == NULL) {
  723. /* Print error message */
  724. rpc->fault(c, 500, "Key name doesn't exist in htable.");
  725. return;
  726. }
  727. /* add entry node */
  728. if (rpc->add(c, "{", &th) < 0) {
  729. rpc->fault(c, 500, "Internal error creating rpc");
  730. goto error;
  731. }
  732. if(rpc->struct_add(th, "{", "item", &vh)<0) {
  733. rpc->fault(c, 500, "Internal error creating rpc");
  734. goto error;
  735. }
  736. if (htc->expire) {
  737. _expire_t = localtime(&htc->expire);
  738. strftime(expire_buf, RPC_DATE_BUF_LEN - 1,
  739. "%Y-%m-%d %H:%M:%S", _expire_t);
  740. }
  741. if(htc->flags&AVP_VAL_STR) {
  742. if(rpc->struct_add(vh, "SSds", "name", &htc->name.s,
  743. "value", &htc->value.s,
  744. "flags", htc->flags,
  745. "expire", expire_buf)<0)
  746. {
  747. rpc->fault(c, 500, "Internal error adding item");
  748. goto error;
  749. }
  750. } else {
  751. if(rpc->struct_add(vh, "Sdds", "name", &htc->name.s,
  752. "value", (int)htc->value.n,
  753. "flags", htc->flags,
  754. "expire", expire_buf)<0)
  755. {
  756. rpc->fault(c, 500, "Internal error adding item");
  757. goto error;
  758. }
  759. }
  760. error:
  761. /* Release the allocated memory */
  762. ht_cell_pkg_free(htc);
  763. return;
  764. }
  765. /*! \brief RPC htable.sets command to set one item to string value */
  766. static void htable_rpc_sets(rpc_t* rpc, void* c) {
  767. str htname, keyname;
  768. int_str keyvalue;
  769. ht_t *ht;
  770. if (rpc->scan(c, "SS.S", &htname, &keyname, &keyvalue.s) < 3) {
  771. rpc->fault(c, 500,
  772. "Not enough parameters (htable name, key name and value)");
  773. return;
  774. }
  775. /* Find the htable */
  776. ht = ht_get_table(&htname);
  777. if (!ht) {
  778. rpc->fault(c, 500, "No such htable");
  779. return;
  780. }
  781. if (ht->dmqreplicate>0 && ht_dmq_replicate_action(HT_DMQ_SET_CELL, &ht->name, &keyname, AVP_VAL_STR, &keyvalue, 1)!=0) {
  782. LM_ERR("dmq relication failed\n");
  783. }
  784. if(ht_set_cell(ht, &keyname, AVP_VAL_STR, &keyvalue, 1)!=0)
  785. {
  786. LM_ERR("cannot set $ht(%.*s=>%.*s)\n", htname.len, htname.s,
  787. keyname.len, keyname.s);
  788. rpc->fault(c, 500, "Failed to set the item");
  789. return;
  790. }
  791. return;
  792. }
  793. /*! \brief RPC htable.seti command to set one item to integer value */
  794. static void htable_rpc_seti(rpc_t* rpc, void* c) {
  795. str htname, keyname;
  796. int_str keyvalue;
  797. ht_t *ht;
  798. if (rpc->scan(c, "SS.d", &htname, &keyname, &keyvalue.n) < 3) {
  799. rpc->fault(c, 500,
  800. "Not enough parameters (htable name, key name and value)");
  801. return;
  802. }
  803. /* Find the htable */
  804. ht = ht_get_table(&htname);
  805. if (!ht) {
  806. rpc->fault(c, 500, "No such htable");
  807. return;
  808. }
  809. if (ht->dmqreplicate>0 && ht_dmq_replicate_action(HT_DMQ_SET_CELL, &ht->name, &keyname, 0, &keyvalue, 1)!=0) {
  810. LM_ERR("dmq relication failed\n");
  811. }
  812. if(ht_set_cell(ht, &keyname, 0, &keyvalue, 1)!=0)
  813. {
  814. LM_ERR("cannot set $ht(%.*s=>%.*s)\n", htname.len, htname.s,
  815. keyname.len, keyname.s);
  816. rpc->fault(c, 500, "Failed to set the item");
  817. return;
  818. }
  819. return;
  820. }
  821. /*! \brief RPC htable.dump command to print content of a hash table */
  822. static void htable_rpc_dump(rpc_t* rpc, void* c)
  823. {
  824. str htname;
  825. ht_t *ht;
  826. ht_cell_t *it;
  827. int i;
  828. void* th;
  829. void* ih;
  830. void* vh;
  831. if (rpc->scan(c, "S", &htname) < 1)
  832. {
  833. rpc->fault(c, 500, "No htable name given");
  834. return;
  835. }
  836. ht = ht_get_table(&htname);
  837. if(ht==NULL)
  838. {
  839. rpc->fault(c, 500, "No such htable");
  840. return;
  841. }
  842. for(i=0; i<ht->htsize; i++)
  843. {
  844. ht_slot_lock(ht, i);
  845. it = ht->entries[i].first;
  846. if(it)
  847. {
  848. /* add entry node */
  849. if (rpc->add(c, "{", &th) < 0)
  850. {
  851. rpc->fault(c, 500, "Internal error creating rpc");
  852. goto error;
  853. }
  854. if(rpc->struct_add(th, "dd{",
  855. "entry", i,
  856. "size", (int)ht->entries[i].esize,
  857. "slot", &ih)<0)
  858. {
  859. rpc->fault(c, 500, "Internal error creating rpc");
  860. goto error;
  861. }
  862. while(it)
  863. {
  864. if(rpc->struct_add(ih, "{",
  865. "item", &vh)<0)
  866. {
  867. rpc->fault(c, 500, "Internal error creating rpc");
  868. goto error;
  869. }
  870. if(it->flags&AVP_VAL_STR) {
  871. if(rpc->struct_add(vh, "SS",
  872. "name", &it->name.s,
  873. "value", &it->value.s)<0)
  874. {
  875. rpc->fault(c, 500, "Internal error adding item");
  876. goto error;
  877. }
  878. } else {
  879. if(rpc->struct_add(vh, "Sd",
  880. "name", &it->name.s,
  881. "value", (int)it->value.n)<0)
  882. {
  883. rpc->fault(c, 500, "Internal error adding item");
  884. goto error;
  885. }
  886. }
  887. it = it->next;
  888. }
  889. }
  890. ht_slot_unlock(ht, i);
  891. }
  892. return;
  893. error:
  894. ht_slot_unlock(ht, i);
  895. }
  896. static void htable_rpc_list(rpc_t* rpc, void* c)
  897. {
  898. ht_t *ht;
  899. void* th;
  900. char dbname[128];
  901. ht = ht_get_root();
  902. if(ht==NULL)
  903. {
  904. rpc->fault(c, 500, "No htables");
  905. return;
  906. }
  907. while (ht != NULL)
  908. {
  909. int len = 0;
  910. /* add entry node */
  911. if (rpc->add(c, "{", &th) < 0)
  912. {
  913. rpc->fault(c, 500, "Internal error creating structure rpc");
  914. goto error;
  915. }
  916. if (ht->dbtable.len > 0) {
  917. len = ht->dbtable.len > 127 ? 127 : ht->dbtable.len;
  918. memcpy(dbname, ht->dbtable.s, len);
  919. dbname[ht->dbtable.len] = '\0';
  920. } else {
  921. dbname[0] = '\0';
  922. }
  923. if(rpc->struct_add(th, "Ssddddd",
  924. "name", &ht->name, /* String */
  925. "dbtable", &dbname , /* Char * */
  926. "dbmode", (int) ht->dbmode, /* u int */
  927. "expire", (int) ht->htexpire, /* u int */
  928. "updateexpire", ht->updateexpire, /* int */
  929. "size", (int) ht->htsize, /* u int */
  930. "dmqreplicate", ht->dmqreplicate /* int */
  931. ) < 0) {
  932. rpc->fault(c, 500, "Internal error creating data rpc");
  933. goto error;
  934. }
  935. ht = ht->next;
  936. }
  937. error:
  938. return;
  939. }
  940. static void htable_rpc_stats(rpc_t* rpc, void* c)
  941. {
  942. ht_t *ht;
  943. void* th;
  944. unsigned int min;
  945. unsigned int max;
  946. unsigned int all;
  947. unsigned int i;
  948. ht = ht_get_root();
  949. if(ht==NULL)
  950. {
  951. rpc->fault(c, 500, "No htables");
  952. return;
  953. }
  954. while (ht != NULL)
  955. {
  956. /* add entry node */
  957. if (rpc->add(c, "{", &th) < 0)
  958. {
  959. rpc->fault(c, 500, "Internal error creating structure rpc");
  960. goto error;
  961. }
  962. all = 0;
  963. max = 0;
  964. min = 4294967295U;
  965. for(i=0; i<ht->htsize; i++) {
  966. ht_slot_lock(ht, i);
  967. if(ht->entries[i].esize<min)
  968. min = ht->entries[i].esize;
  969. if(ht->entries[i].esize>max)
  970. max = ht->entries[i].esize;
  971. all += ht->entries[i].esize;
  972. ht_slot_unlock(ht, i);
  973. }
  974. if(rpc->struct_add(th, "Sddd",
  975. "name", &ht->name, /* str */
  976. "slots", (int)ht->htsize, /* uint */
  977. "all", (int)all, /* uint */
  978. "min", (int)min, /* uint */
  979. "max", (int)max /* uint */
  980. ) < 0) {
  981. rpc->fault(c, 500, "Internal error creating rpc structure");
  982. goto error;
  983. }
  984. ht = ht->next;
  985. }
  986. error:
  987. return;
  988. }
  989. /*! \brief RPC htable.reload command to reload content of a hash table */
  990. static void htable_rpc_reload(rpc_t* rpc, void* c)
  991. {
  992. str htname;
  993. ht_t *ht;
  994. ht_t nht;
  995. ht_cell_t *first;
  996. ht_cell_t *it;
  997. int i;
  998. if(ht_db_url.len<=0) {
  999. rpc->fault(c, 500, "No htable db_url");
  1000. return;
  1001. }
  1002. if(ht_db_init_con()!=0) {
  1003. rpc->fault(c, 500, "Failed to init htable db connection");
  1004. return;
  1005. }
  1006. if(ht_db_open_con()!=0) {
  1007. rpc->fault(c, 500, "Failed to open htable db connection");
  1008. return;
  1009. }
  1010. if (rpc->scan(c, "S", &htname) < 1)
  1011. {
  1012. rpc->fault(c, 500, "No htable name given");
  1013. return;
  1014. }
  1015. ht = ht_get_table(&htname);
  1016. if(ht==NULL)
  1017. {
  1018. rpc->fault(c, 500, "No such htable");
  1019. return;
  1020. }
  1021. memcpy(&nht, ht, sizeof(ht_t));
  1022. /* it's temporary operation - use system malloc */
  1023. nht.entries = (ht_entry_t*)malloc(nht.htsize*sizeof(ht_entry_t));
  1024. if(nht.entries == NULL)
  1025. {
  1026. ht_db_close_con();
  1027. rpc->fault(c, 500, "Mtree reload failed");
  1028. return;
  1029. }
  1030. memset(nht.entries, 0, nht.htsize*sizeof(ht_entry_t));
  1031. if(ht_db_load_table(&nht, &ht->dbtable, 0)<0)
  1032. {
  1033. /* free any entry set if it was a partial load */
  1034. for(i=0; i<nht.htsize; i++)
  1035. {
  1036. first = nht.entries[i].first;
  1037. while(first)
  1038. {
  1039. it = first;
  1040. first = first->next;
  1041. ht_cell_free(it);
  1042. }
  1043. }
  1044. free(nht.entries);
  1045. ht_db_close_con();
  1046. rpc->fault(c, 500, "Mtree reload failed");
  1047. return;
  1048. }
  1049. /* replace old entries */
  1050. for(i=0; i<nht.htsize; i++)
  1051. {
  1052. ht_slot_lock(ht, i);
  1053. first = ht->entries[i].first;
  1054. ht->entries[i].first = nht.entries[i].first;
  1055. ht->entries[i].esize = nht.entries[i].esize;
  1056. ht_slot_unlock(ht, i);
  1057. nht.entries[i].first = first;
  1058. }
  1059. /* free old entries */
  1060. for(i=0; i<nht.htsize; i++)
  1061. {
  1062. first = nht.entries[i].first;
  1063. while(first)
  1064. {
  1065. it = first;
  1066. first = first->next;
  1067. ht_cell_free(it);
  1068. }
  1069. }
  1070. free(nht.entries);
  1071. ht_db_close_con();
  1072. return;
  1073. }
  1074. rpc_export_t htable_rpc[] = {
  1075. {"htable.dump", htable_rpc_dump, htable_dump_doc, RET_ARRAY},
  1076. {"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
  1077. {"htable.get", htable_rpc_get, htable_get_doc, 0},
  1078. {"htable.sets", htable_rpc_sets, htable_sets_doc, 0},
  1079. {"htable.seti", htable_rpc_seti, htable_seti_doc, 0},
  1080. {"htable.listTables", htable_rpc_list, htable_list_doc, RET_ARRAY},
  1081. {"htable.reload", htable_rpc_reload, htable_reload_doc, 0},
  1082. {"htable.stats", htable_rpc_stats, htable_stats_doc, RET_ARRAY},
  1083. {0, 0, 0, 0}
  1084. };
  1085. static int htable_init_rpc(void)
  1086. {
  1087. if (rpc_register_array(htable_rpc)!=0)
  1088. {
  1089. LM_ERR("failed to register RPC commands\n");
  1090. return -1;
  1091. }
  1092. return 0;
  1093. }