2
0

ht_api.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /**
  2. *
  3. * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
  4. *
  5. * This file is part of kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stddef.h>
  22. #include <regex.h>
  23. #include "../../mem/shm_mem.h"
  24. #include "../../mem/mem.h"
  25. #include "../../shm_init.h"
  26. #include "../../dprint.h"
  27. #include "../../parser/parse_param.h"
  28. #include "../../hashes.h"
  29. #include "../../ut.h"
  30. #include "../../re.h"
  31. #include "../../lib/kcore/faked_msg.h"
  32. #include "../../action.h"
  33. #include "../../route.h"
  34. #include "ht_api.h"
  35. #include "ht_db.h"
  36. ht_t *_ht_root = NULL;
  37. ht_cell_t *ht_expired_cell;
  38. typedef struct _keyvalue {
  39. str key;
  40. str value;
  41. int type;
  42. union {
  43. param_t *params;
  44. } u;
  45. } keyvalue_t;
  46. #define KEYVALUE_TYPE_NONE 0
  47. #define KEYVALUE_TYPE_PARAMS 1
  48. /**
  49. * parse a string like: 'key=>value'
  50. * - the value can be parameter list: 'name1=value1;...;nameX=valueX'
  51. */
  52. int keyvalue_parse_str(str *data, int type, keyvalue_t *res)
  53. {
  54. char *p;
  55. str s;
  56. str in;
  57. param_hooks_t phooks;
  58. if(data==NULL || data->s==NULL || data->len<=0 || res==NULL)
  59. {
  60. LM_ERR("invalid parameters\n");
  61. return -1;
  62. }
  63. memset(res, 0, sizeof(keyvalue_t));
  64. in.s = data->s;
  65. in.len = data->len;
  66. p = in.s;
  67. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  68. p++;
  69. if(p>in.s+in.len || *p=='\0')
  70. goto error;
  71. res->key.s = p;
  72. while(p < in.s + in.len)
  73. {
  74. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  75. break;
  76. p++;
  77. }
  78. if(p>in.s+in.len || *p=='\0')
  79. goto error;
  80. res->key.len = (int)(p - res->key.s);
  81. if(*p!='=')
  82. {
  83. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  84. p++;
  85. if(p>in.s+in.len || *p=='\0' || *p!='=')
  86. goto error;
  87. }
  88. p++;
  89. if(*p!='>')
  90. goto error;
  91. p++;
  92. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  93. p++;
  94. s.s = p;
  95. s.len = in.s + in.len - p;
  96. res->value.s = s.s;
  97. res->value.len = s.len;
  98. res->type = type;
  99. if(type==KEYVALUE_TYPE_PARAMS)
  100. {
  101. if(s.s[s.len-1]==';')
  102. s.len--;
  103. if (parse_params(&s, CLASS_ANY, &phooks, &res->u.params)<0)
  104. {
  105. LM_ERR("failed parsing params value\n");
  106. goto error;
  107. }
  108. }
  109. return 0;
  110. error:
  111. LM_ERR("invalid input parameter [%.*s] at [%d]\n", in.len, in.s,
  112. (int)(p-in.s));
  113. return -1;
  114. }
  115. void keyvalue_destroy(keyvalue_t *res)
  116. {
  117. if(res==NULL)
  118. return;
  119. if(res->type==KEYVALUE_TYPE_PARAMS)
  120. {
  121. if(res->u.params!=NULL)
  122. free_params(res->u.params);
  123. }
  124. memset(res, 0, sizeof(keyvalue_t));
  125. }
  126. /**
  127. * recursive/re-entrant lock of the slot in hash table
  128. */
  129. void ht_slot_lock(ht_t *ht, int idx)
  130. {
  131. int mypid;
  132. mypid = my_pid();
  133. if (likely(atomic_get(&ht->entries[idx].locker_pid) != mypid)) {
  134. lock_get(&ht->entries[idx].lock);
  135. atomic_set(&ht->entries[idx].locker_pid, mypid);
  136. } else {
  137. /* locked within the same process that executed us */
  138. ht->entries[idx].rec_lock_level++;
  139. }
  140. }
  141. /**
  142. * recursive/re-entrant unlock of the slot in hash table
  143. */
  144. void ht_slot_unlock(ht_t *ht, int idx)
  145. {
  146. if (likely(ht->entries[idx].rec_lock_level == 0)) {
  147. atomic_set(&ht->entries[idx].locker_pid, 0);
  148. lock_release(&ht->entries[idx].lock);
  149. } else {
  150. /* recursive locked => decrease lock count */
  151. ht->entries[idx].rec_lock_level--;
  152. }
  153. }
  154. ht_cell_t* ht_cell_new(str *name, int type, int_str *val, unsigned int cellid)
  155. {
  156. ht_cell_t *cell;
  157. unsigned int msize;
  158. msize = sizeof(ht_cell_t) + (name->len + 1)*sizeof(char);
  159. if(type&AVP_VAL_STR)
  160. msize += (val->s.len + 1)*sizeof(char);
  161. cell = (ht_cell_t*)shm_malloc(msize);
  162. if(cell==NULL)
  163. {
  164. LM_ERR("no more shm\n");
  165. return NULL;
  166. }
  167. memset(cell, 0, msize);
  168. cell->msize = msize;
  169. cell->cellid = cellid;
  170. cell->flags = type;
  171. cell->name.len = name->len;
  172. cell->name.s = (char*)cell + sizeof(ht_cell_t);
  173. memcpy(cell->name.s, name->s, name->len);
  174. cell->name.s[name->len] = '\0';
  175. if(type&AVP_VAL_STR)
  176. {
  177. cell->value.s.s = (char*)cell->name.s + name->len + 1;
  178. cell->value.s.len = val->s.len;
  179. memcpy(cell->value.s.s, val->s.s, val->s.len);
  180. cell->value.s.s[val->s.len] = '\0';
  181. } else {
  182. cell->value.n = val->n;
  183. }
  184. return cell;
  185. }
  186. int ht_cell_free(ht_cell_t *cell)
  187. {
  188. if(cell==NULL)
  189. return -1;
  190. shm_free(cell);
  191. return 0;
  192. }
  193. int ht_cell_pkg_free(ht_cell_t *cell)
  194. {
  195. if(cell==NULL)
  196. return -1;
  197. pkg_free(cell);
  198. return 0;
  199. }
  200. ht_t *ht_get_root(void)
  201. {
  202. return _ht_root;
  203. }
  204. ht_t* ht_get_table(str *name)
  205. {
  206. unsigned int htid;
  207. ht_t *ht;
  208. htid = ht_compute_hash(name);
  209. /* does it exist */
  210. ht = _ht_root;
  211. while(ht!=NULL)
  212. {
  213. if(htid == ht->htid && name->len==ht->name.len
  214. && strncmp(name->s, ht->name.s, name->len)==0)
  215. {
  216. LM_DBG("htable found [%.*s]\n", name->len, name->s);
  217. return ht;
  218. }
  219. ht = ht->next;
  220. }
  221. return NULL;
  222. }
  223. int ht_add_table(str *name, int autoexp, str *dbtable, int size, int dbmode,
  224. int itype, int_str *ival, int updateexpire, int dmqreplicate)
  225. {
  226. unsigned int htid;
  227. ht_t *ht;
  228. htid = ht_compute_hash(name);
  229. /* does it exist */
  230. ht = _ht_root;
  231. while(ht!=NULL)
  232. {
  233. if(htid == ht->htid && name->len==ht->name.len
  234. && strncmp(name->s, ht->name.s, name->len)==0)
  235. {
  236. LM_ERR("htable already configured [%.*s]\n", name->len, name->s);
  237. return -1;
  238. }
  239. ht = ht->next;
  240. }
  241. ht = (ht_t*)shm_malloc(sizeof(ht_t));
  242. if(ht==NULL)
  243. {
  244. LM_ERR("no more shared memory\n");
  245. return -1;
  246. }
  247. memset(ht, 0, sizeof(ht_t));
  248. if(size<=1)
  249. ht->htsize = 8;
  250. else if(size>31)
  251. ht->htsize = 1<<14;
  252. else ht->htsize = 1<<size;
  253. ht->htid = htid;
  254. ht->htexpire = autoexp;
  255. ht->updateexpire = updateexpire;
  256. ht->name = *name;
  257. if(dbtable!=NULL && dbtable->len>0)
  258. ht->dbtable = *dbtable;
  259. ht->dbmode = dbmode;
  260. ht->flags = itype;
  261. if(ival!=NULL)
  262. ht->initval = *ival;
  263. ht->dmqreplicate = dmqreplicate;
  264. ht->next = _ht_root;
  265. _ht_root = ht;
  266. return 0;
  267. }
  268. int ht_init_tables(void)
  269. {
  270. ht_t *ht;
  271. int i;
  272. char route_name[64];
  273. ht = _ht_root;
  274. while(ht)
  275. {
  276. LM_DBG("initializing htable [%.*s] with nr. of slots: %d\n",
  277. ht->name.len, ht->name.s, ht->htsize);
  278. if(ht->name.len + sizeof("htable:expired:") < 64)
  279. {
  280. strcpy(route_name, "htable:expired:");
  281. strncat(route_name, ht->name.s, ht->name.len);
  282. ht->evrt_expired = route_get(&event_rt, route_name);
  283. if (ht->evrt_expired < 0
  284. || event_rt.rlist[ht->evrt_expired] == NULL)
  285. {
  286. ht->evrt_expired = -1;
  287. LM_DBG("event route for expired items in [%.*s] does not exist\n",
  288. ht->name.len, ht->name.s);
  289. } else {
  290. LM_DBG("event route for expired items in [%.*s] exists\n",
  291. ht->name.len, ht->name.s);
  292. }
  293. } else {
  294. LM_WARN("event route name for expired items in htable [%.*s]"
  295. " is too long\n", ht->name.len, ht->name.s);
  296. }
  297. ht->entries = (ht_entry_t*)shm_malloc(ht->htsize*sizeof(ht_entry_t));
  298. if(ht->entries==NULL)
  299. {
  300. LM_ERR("no more shm for [%.*s]\n", ht->name.len, ht->name.s);
  301. shm_free(ht);
  302. return -1;
  303. }
  304. memset(ht->entries, 0, ht->htsize*sizeof(ht_entry_t));
  305. for(i=0; i<ht->htsize; i++)
  306. {
  307. if(lock_init(&ht->entries[i].lock)==0)
  308. {
  309. LM_ERR("cannot initalize lock[%d] in [%.*s]\n", i,
  310. ht->name.len, ht->name.s);
  311. i--;
  312. while(i>=0)
  313. {
  314. lock_destroy(&ht->entries[i].lock);
  315. i--;
  316. }
  317. shm_free(ht->entries);
  318. shm_free(ht);
  319. return -1;
  320. }
  321. }
  322. ht = ht->next;
  323. }
  324. return 0;
  325. }
  326. int ht_destroy(void)
  327. {
  328. int i;
  329. ht_cell_t *it, *it0;
  330. ht_t *ht;
  331. ht_t *ht0;
  332. if(_ht_root==NULL)
  333. return -1;
  334. ht = _ht_root;
  335. while(ht)
  336. {
  337. ht0 = ht->next;
  338. if(ht->entries!=NULL)
  339. {
  340. for(i=0; i<ht->htsize; i++)
  341. {
  342. /* free entries */
  343. it = ht->entries[i].first;
  344. while(it)
  345. {
  346. it0 = it;
  347. it = it->next;
  348. ht_cell_free(it0);
  349. }
  350. /* free locks */
  351. lock_destroy(&ht->entries[i].lock);
  352. }
  353. shm_free(ht->entries);
  354. }
  355. shm_free(ht);
  356. ht = ht0;
  357. }
  358. _ht_root = NULL;
  359. return 0;
  360. }
  361. int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
  362. {
  363. unsigned int idx;
  364. unsigned int hid;
  365. ht_cell_t *it, *prev, *cell;
  366. time_t now;
  367. if(ht==NULL || ht->entries==NULL)
  368. return -1;
  369. hid = ht_compute_hash(name);
  370. idx = ht_get_entry(hid, ht->htsize);
  371. now = 0;
  372. if(ht->htexpire>0)
  373. now = time(NULL);
  374. prev = NULL;
  375. if(mode) ht_slot_lock(ht, idx);
  376. it = ht->entries[idx].first;
  377. while(it!=NULL && it->cellid < hid)
  378. {
  379. prev = it;
  380. it = it->next;
  381. }
  382. while(it!=NULL && it->cellid == hid)
  383. {
  384. if(name->len==it->name.len
  385. && strncmp(name->s, it->name.s, name->len)==0)
  386. {
  387. /* update value */
  388. if(it->flags&AVP_VAL_STR)
  389. {
  390. if(type&AVP_VAL_STR)
  391. {
  392. if(it->value.s.len >= val->s.len)
  393. {
  394. /* copy */
  395. it->value.s.len = val->s.len;
  396. memcpy(it->value.s.s, val->s.s, val->s.len);
  397. it->value.s.s[it->value.s.len] = '\0';
  398. if(ht->updateexpire)
  399. it->expire = now + ht->htexpire;
  400. } else {
  401. /* new */
  402. cell = ht_cell_new(name, type, val, hid);
  403. if(cell == NULL)
  404. {
  405. LM_ERR("cannot create new cell\n");
  406. if(mode) ht_slot_unlock(ht, idx);
  407. return -1;
  408. }
  409. cell->next = it->next;
  410. cell->prev = it->prev;
  411. cell->expire = now + ht->htexpire;
  412. if(it->prev)
  413. it->prev->next = cell;
  414. else
  415. ht->entries[idx].first = cell;
  416. if(it->next)
  417. it->next->prev = cell;
  418. ht_cell_free(it);
  419. }
  420. } else {
  421. it->flags &= ~AVP_VAL_STR;
  422. it->value.n = val->n;
  423. if(ht->updateexpire)
  424. it->expire = now + ht->htexpire;
  425. }
  426. if(mode) ht_slot_unlock(ht, idx);
  427. return 0;
  428. } else {
  429. if(type&AVP_VAL_STR)
  430. {
  431. /* new */
  432. cell = ht_cell_new(name, type, val, hid);
  433. if(cell == NULL)
  434. {
  435. LM_ERR("cannot create new cell.\n");
  436. if(mode) ht_slot_unlock(ht, idx);
  437. return -1;
  438. }
  439. cell->expire = now + ht->htexpire;
  440. cell->next = it->next;
  441. cell->prev = it->prev;
  442. if(it->prev)
  443. it->prev->next = cell;
  444. else
  445. ht->entries[idx].first = cell;
  446. if(it->next)
  447. it->next->prev = cell;
  448. ht_cell_free(it);
  449. } else {
  450. it->value.n = val->n;
  451. if(ht->updateexpire)
  452. it->expire = now + ht->htexpire;
  453. }
  454. if(mode) ht_slot_unlock(ht, idx);
  455. return 0;
  456. }
  457. }
  458. prev = it;
  459. it = it->next;
  460. }
  461. /* add */
  462. cell = ht_cell_new(name, type, val, hid);
  463. if(cell == NULL)
  464. {
  465. LM_ERR("cannot create new cell.\n");
  466. if(mode) ht_slot_unlock(ht, idx);
  467. return -1;
  468. }
  469. cell->expire = now + ht->htexpire;
  470. if(prev==NULL)
  471. {
  472. if(ht->entries[idx].first!=NULL)
  473. {
  474. cell->next = ht->entries[idx].first;
  475. ht->entries[idx].first->prev = cell;
  476. }
  477. ht->entries[idx].first = cell;
  478. } else {
  479. cell->next = prev->next;
  480. cell->prev = prev;
  481. if(prev->next)
  482. prev->next->prev = cell;
  483. prev->next = cell;
  484. }
  485. ht->entries[idx].esize++;
  486. if(mode) ht_slot_unlock(ht, idx);
  487. return 0;
  488. }
  489. int ht_del_cell(ht_t *ht, str *name)
  490. {
  491. unsigned int idx;
  492. unsigned int hid;
  493. ht_cell_t *it;
  494. if(ht==NULL || ht->entries==NULL)
  495. return -1;
  496. hid = ht_compute_hash(name);
  497. idx = ht_get_entry(hid, ht->htsize);
  498. /* head test and return */
  499. if(ht->entries[idx].first==NULL)
  500. return 0;
  501. ht_slot_lock(ht, idx);
  502. it = ht->entries[idx].first;
  503. while(it!=NULL && it->cellid < hid)
  504. it = it->next;
  505. while(it!=NULL && it->cellid == hid)
  506. {
  507. if(name->len==it->name.len
  508. && strncmp(name->s, it->name.s, name->len)==0)
  509. {
  510. /* found */
  511. if(it->prev==NULL)
  512. ht->entries[idx].first = it->next;
  513. else
  514. it->prev->next = it->next;
  515. if(it->next)
  516. it->next->prev = it->prev;
  517. ht->entries[idx].esize--;
  518. ht_slot_unlock(ht, idx);
  519. ht_cell_free(it);
  520. return 0;
  521. }
  522. it = it->next;
  523. }
  524. ht_slot_unlock(ht, idx);
  525. return 0;
  526. }
  527. ht_cell_t* ht_cell_value_add(ht_t *ht, str *name, int val, int mode,
  528. ht_cell_t *old)
  529. {
  530. unsigned int idx;
  531. unsigned int hid;
  532. ht_cell_t *it, *prev, *cell;
  533. time_t now;
  534. int_str isval;
  535. if(ht==NULL || ht->entries==NULL)
  536. return NULL;
  537. hid = ht_compute_hash(name);
  538. idx = ht_get_entry(hid, ht->htsize);
  539. now = 0;
  540. if(ht->htexpire>0)
  541. now = time(NULL);
  542. prev = NULL;
  543. if(mode) ht_slot_lock(ht, idx);
  544. it = ht->entries[idx].first;
  545. while(it!=NULL && it->cellid < hid)
  546. {
  547. prev = it;
  548. it = it->next;
  549. }
  550. while(it!=NULL && it->cellid == hid)
  551. {
  552. if(name->len==it->name.len
  553. && strncmp(name->s, it->name.s, name->len)==0)
  554. {
  555. /* found */
  556. if(now>0 && it->expire!=0 && it->expire<now) {
  557. /* entry has expired */
  558. ht_handle_expired_record(ht, it);
  559. if(ht->flags==PV_VAL_INT) {
  560. /* initval is integer, use it to create a fresh entry */
  561. it->flags &= ~AVP_VAL_STR;
  562. it->value.n = ht->initval.n;
  563. /* increment will be done below */
  564. } else {
  565. /* delete expired entry */
  566. if(it->prev==NULL)
  567. ht->entries[idx].first = it->next;
  568. else
  569. it->prev->next = it->next;
  570. if(it->next)
  571. it->next->prev = it->prev;
  572. ht->entries[idx].esize--;
  573. if(mode) ht_slot_unlock(ht, idx);
  574. ht_cell_free(it);
  575. return NULL;
  576. }
  577. }
  578. /* update value */
  579. if(it->flags&AVP_VAL_STR)
  580. {
  581. /* string value cannot be incremented */
  582. if(mode) ht_slot_unlock(ht, idx);
  583. return NULL;
  584. } else {
  585. it->value.n += val;
  586. it->expire = now + ht->htexpire;
  587. if(old!=NULL)
  588. {
  589. if(old->msize>=it->msize)
  590. {
  591. memcpy(old, it, it->msize);
  592. if(mode) ht_slot_unlock(ht, idx);
  593. return old;
  594. }
  595. }
  596. cell = (ht_cell_t*)pkg_malloc(it->msize);
  597. if(cell!=NULL)
  598. memcpy(cell, it, it->msize);
  599. if(mode) ht_slot_unlock(ht, idx);
  600. return cell;
  601. }
  602. }
  603. prev = it;
  604. it = it->next;
  605. }
  606. /* add val if htable has an integer init value */
  607. if(ht->flags!=PV_VAL_INT)
  608. {
  609. if(mode) ht_slot_unlock(ht, idx);
  610. return NULL;
  611. }
  612. isval.n = ht->initval.n + val;
  613. it = ht_cell_new(name, 0, &isval, hid);
  614. if(it == NULL)
  615. {
  616. LM_ERR("cannot create new cell.\n");
  617. if(mode) ht_slot_unlock(ht, idx);
  618. return NULL;
  619. }
  620. it->expire = now + ht->htexpire;
  621. if(prev==NULL)
  622. {
  623. if(ht->entries[idx].first!=NULL)
  624. {
  625. it->next = ht->entries[idx].first;
  626. ht->entries[idx].first->prev = it;
  627. }
  628. ht->entries[idx].first = it;
  629. } else {
  630. it->next = prev->next;
  631. it->prev = prev;
  632. if(prev->next)
  633. prev->next->prev = it;
  634. prev->next = it;
  635. }
  636. ht->entries[idx].esize++;
  637. if(old!=NULL)
  638. {
  639. if(old->msize>=it->msize)
  640. {
  641. memcpy(old, it, it->msize);
  642. if(mode) ht_slot_unlock(ht, idx);
  643. return old;
  644. }
  645. }
  646. cell = (ht_cell_t*)pkg_malloc(it->msize);
  647. if(cell!=NULL)
  648. memcpy(cell, it, it->msize);
  649. if(mode) ht_slot_unlock(ht, idx);
  650. return cell;
  651. }
  652. ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old)
  653. {
  654. unsigned int idx;
  655. unsigned int hid;
  656. ht_cell_t *it, *cell;
  657. if(ht==NULL || ht->entries==NULL)
  658. return NULL;
  659. hid = ht_compute_hash(name);
  660. idx = ht_get_entry(hid, ht->htsize);
  661. /* head test and return */
  662. if(ht->entries[idx].first==NULL)
  663. return NULL;
  664. ht_slot_lock(ht, idx);
  665. it = ht->entries[idx].first;
  666. while(it!=NULL && it->cellid < hid)
  667. it = it->next;
  668. while(it!=NULL && it->cellid == hid)
  669. {
  670. if(name->len==it->name.len
  671. && strncmp(name->s, it->name.s, name->len)==0)
  672. {
  673. /* found */
  674. if(ht->htexpire>0 && it->expire!=0 && it->expire<time(NULL)) {
  675. /* entry has expired, delete it and return NULL */
  676. ht_handle_expired_record(ht, it);
  677. if(it->prev==NULL)
  678. ht->entries[idx].first = it->next;
  679. else
  680. it->prev->next = it->next;
  681. if(it->next)
  682. it->next->prev = it->prev;
  683. ht->entries[idx].esize--;
  684. ht_slot_unlock(ht, idx);
  685. ht_cell_free(it);
  686. return NULL;
  687. }
  688. if(old!=NULL)
  689. {
  690. if(old->msize>=it->msize)
  691. {
  692. memcpy(old, it, it->msize);
  693. ht_slot_unlock(ht, idx);
  694. return old;
  695. }
  696. }
  697. cell = (ht_cell_t*)pkg_malloc(it->msize);
  698. if(cell!=NULL)
  699. memcpy(cell, it, it->msize);
  700. ht_slot_unlock(ht, idx);
  701. return cell;
  702. }
  703. it = it->next;
  704. }
  705. ht_slot_unlock(ht, idx);
  706. return NULL;
  707. }
  708. int ht_dbg(void)
  709. {
  710. int i;
  711. ht_cell_t *it;
  712. ht_t *ht;
  713. ht = _ht_root;
  714. while(ht)
  715. {
  716. LM_ERR("===== htable[%.*s] hid: %u exp: %u>\n", ht->name.len,
  717. ht->name.s, ht->htid, ht->htexpire);
  718. for(i=0; i<ht->htsize; i++)
  719. {
  720. ht_slot_lock(ht, i);
  721. LM_ERR("htable[%d] -- <%d>\n", i, ht->entries[i].esize);
  722. it = ht->entries[i].first;
  723. while(it)
  724. {
  725. LM_ERR("\tcell: %.*s\n", it->name.len, it->name.s);
  726. LM_ERR("\thid: %u msize: %u flags: %d expire: %u\n", it->cellid,
  727. it->msize, it->flags, (unsigned int)it->expire);
  728. if(it->flags&AVP_VAL_STR)
  729. LM_ERR("\tv-s:%.*s\n", it->value.s.len, it->value.s.s);
  730. else
  731. LM_ERR("\tv-i:%d\n", it->value.n);
  732. it = it->next;
  733. }
  734. ht_slot_unlock(ht, i);
  735. }
  736. ht = ht->next;
  737. }
  738. return 0;
  739. }
  740. int ht_table_spec(char *spec)
  741. {
  742. keyvalue_t kval;
  743. str name;
  744. str dbtable = {0, 0};
  745. unsigned int autoexpire = 0;
  746. unsigned int size = 4;
  747. unsigned int dbmode = 0;
  748. unsigned int updateexpire = 1;
  749. unsigned int dmqreplicate = 0;
  750. str in;
  751. str tok;
  752. param_t *pit=NULL;
  753. int_str ival;
  754. int itype;
  755. if(!shm_initialized())
  756. {
  757. LM_ERR("shared memory was not initialized\n");
  758. return -1;
  759. }
  760. /* parse: name=>dbtable=abc;autoexpire=123;size=123 */
  761. in.s = spec;
  762. in.len = strlen(in.s);
  763. if(keyvalue_parse_str(&in, KEYVALUE_TYPE_PARAMS, &kval)<0)
  764. {
  765. LM_ERR("failed parsing: %.*s\n", in.len, in.s);
  766. return -1;
  767. }
  768. name = kval.key;
  769. itype = PV_VAL_NONE;
  770. memset(&ival, 0, sizeof(int_str));
  771. for (pit = kval.u.params; pit; pit=pit->next)
  772. {
  773. tok = pit->body;
  774. if(pit->name.len==7 && strncmp(pit->name.s, "dbtable", 7)==0) {
  775. dbtable = tok;
  776. LM_DBG("htable [%.*s] - dbtable [%.*s]\n", name.len, name.s,
  777. dbtable.len, dbtable.s);
  778. } else if(pit->name.len==10 && strncmp(pit->name.s, "autoexpire", 10)==0) {
  779. if(str2int(&tok, &autoexpire)!=0)
  780. goto error;
  781. LM_DBG("htable [%.*s] - expire [%u]\n", name.len, name.s,
  782. autoexpire);
  783. } else if(pit->name.len==4 && strncmp(pit->name.s, "size", 4)==0) {
  784. if(str2int(&tok, &size)!=0)
  785. goto error;
  786. LM_DBG("htable [%.*s] - size [%u]\n", name.len, name.s,
  787. size);
  788. } else if(pit->name.len==6 && strncmp(pit->name.s, "dbmode", 6)==0) {
  789. if(str2int(&tok, &dbmode)!=0)
  790. goto error;
  791. LM_DBG("htable [%.*s] - dbmode [%u]\n", name.len, name.s,
  792. dbmode);
  793. } else if(pit->name.len==7 && strncmp(pit->name.s, "initval", 7)==0) {
  794. if(str2sint(&tok, &ival.n)!=0)
  795. goto error;
  796. itype = PV_VAL_INT;
  797. LM_DBG("htable [%.*s] - initval [%d]\n", name.len, name.s,
  798. ival.n);
  799. } else if(pit->name.len == 12 && strncmp(pit->name.s, "updateexpire", 12) == 0) {
  800. if(str2int(&tok, &updateexpire) != 0)
  801. goto error;
  802. LM_DBG("htable [%.*s] - updateexpire [%u]\n", name.len, name.s, updateexpire);
  803. } else if(pit->name.len == 12 && strncmp(pit->name.s, "dmqreplicate", 12) == 0) {
  804. if(str2int(&tok, &dmqreplicate) != 0)
  805. goto error;
  806. LM_DBG("htable [%.*s] - dmqreplicate [%u]\n", name.len, name.s, dmqreplicate);
  807. } else { goto error; }
  808. }
  809. return ht_add_table(&name, autoexpire, &dbtable, size, dbmode,
  810. itype, &ival, updateexpire, dmqreplicate);
  811. error:
  812. LM_ERR("invalid htable parameter [%.*s]\n", in.len, in.s);
  813. return -1;
  814. }
  815. int ht_db_load_tables(void)
  816. {
  817. ht_t *ht;
  818. ht = _ht_root;
  819. while(ht)
  820. {
  821. if(ht->dbtable.len>0)
  822. {
  823. LM_DBG("loading db table [%.*s] in ht [%.*s]\n",
  824. ht->dbtable.len, ht->dbtable.s,
  825. ht->name.len, ht->name.s);
  826. if(ht_db_load_table(ht, &ht->dbtable, 0)!=0)
  827. return -1;
  828. }
  829. ht = ht->next;
  830. }
  831. return 0;
  832. }
  833. int ht_db_sync_tables(void)
  834. {
  835. ht_t *ht;
  836. ht = _ht_root;
  837. while(ht)
  838. {
  839. if(ht->dbtable.len>0 && ht->dbmode!=0)
  840. {
  841. LM_DBG("sync db table [%.*s] from ht [%.*s]\n",
  842. ht->dbtable.len, ht->dbtable.s,
  843. ht->name.len, ht->name.s);
  844. ht_db_delete_records(&ht->dbtable);
  845. if(ht_db_save_table(ht, &ht->dbtable)!=0)
  846. LM_ERR("failed sync'ing hash table [%.*s] to db\n",
  847. ht->name.len, ht->name.s);
  848. }
  849. ht = ht->next;
  850. }
  851. return 0;
  852. }
  853. int ht_has_autoexpire(void)
  854. {
  855. ht_t *ht;
  856. if(_ht_root==NULL)
  857. return 0;
  858. ht = _ht_root;
  859. while(ht)
  860. {
  861. if(ht->htexpire>0)
  862. return 1;
  863. ht = ht->next;
  864. }
  865. return 0;
  866. }
  867. void ht_timer(unsigned int ticks, void *param)
  868. {
  869. ht_t *ht;
  870. ht_cell_t *it;
  871. ht_cell_t *it0;
  872. time_t now;
  873. int i;
  874. if(_ht_root==NULL)
  875. return;
  876. now = time(NULL);
  877. ht = _ht_root;
  878. while(ht)
  879. {
  880. if(ht->htexpire>0)
  881. {
  882. for(i=0; i<ht->htsize; i++)
  883. {
  884. /* free entries */
  885. ht_slot_lock(ht, i);
  886. it = ht->entries[i].first;
  887. while(it)
  888. {
  889. it0 = it->next;
  890. if(it->expire!=0 && it->expire<now)
  891. {
  892. /* expired */
  893. ht_handle_expired_record(ht, it);
  894. if(it->prev==NULL)
  895. ht->entries[i].first = it->next;
  896. else
  897. it->prev->next = it->next;
  898. if(it->next)
  899. it->next->prev = it->prev;
  900. ht->entries[i].esize--;
  901. ht_cell_free(it);
  902. }
  903. it = it0;
  904. }
  905. ht_slot_unlock(ht, i);
  906. }
  907. }
  908. ht = ht->next;
  909. }
  910. return;
  911. }
  912. void ht_handle_expired_record(ht_t *ht, ht_cell_t *cell)
  913. {
  914. if(ht->evrt_expired<0)
  915. return;
  916. ht_expired_cell = cell;
  917. LM_DBG("running event_route[htable:expired:%.*s]\n",
  918. ht->name.len, ht->name.s);
  919. ht_expired_run_event_route(ht->evrt_expired);
  920. ht_expired_cell = NULL;
  921. }
  922. void ht_expired_run_event_route(int routeid)
  923. {
  924. int backup_rt;
  925. sip_msg_t *fmsg;
  926. if (routeid < 0 || event_rt.rlist[routeid] == NULL)
  927. {
  928. LM_DBG("route does not exist\n");
  929. return;
  930. }
  931. if (faked_msg_init() < 0)
  932. {
  933. LM_ERR("faked_msg_init() failed\n");
  934. return;
  935. }
  936. fmsg = faked_msg_next();
  937. fmsg->parsed_orig_ruri_ok = 0;
  938. backup_rt = get_route_type();
  939. set_route_type(EVENT_ROUTE);
  940. run_top_route(event_rt.rlist[routeid], fmsg, 0);
  941. set_route_type(backup_rt);
  942. }
  943. int ht_set_cell_expire(ht_t *ht, str *name, int type, int_str *val)
  944. {
  945. unsigned int idx;
  946. unsigned int hid;
  947. ht_cell_t *it;
  948. time_t now;
  949. if(ht==NULL || ht->entries==NULL)
  950. return -1;
  951. /* str value - ignore */
  952. if(type&AVP_VAL_STR)
  953. return 0;
  954. /* not auto-expire htable */
  955. if(ht->htexpire==0)
  956. return 0;
  957. hid = ht_compute_hash(name);
  958. idx = ht_get_entry(hid, ht->htsize);
  959. now = 0;
  960. if(val->n>0)
  961. now = time(NULL) + val->n;
  962. LM_DBG("set auto-expire to %u (%d)\n", (unsigned int)now,
  963. val->n);
  964. ht_slot_lock(ht, idx);
  965. it = ht->entries[idx].first;
  966. while(it!=NULL && it->cellid < hid)
  967. it = it->next;
  968. while(it!=NULL && it->cellid == hid)
  969. {
  970. if(name->len==it->name.len
  971. && strncmp(name->s, it->name.s, name->len)==0)
  972. {
  973. /* update value */
  974. it->expire = now;
  975. ht_slot_unlock(ht, idx);
  976. return 0;
  977. }
  978. it = it->next;
  979. }
  980. ht_slot_unlock(ht, idx);
  981. return 0;
  982. }
  983. int ht_get_cell_expire(ht_t *ht, str *name, unsigned int *val)
  984. {
  985. unsigned int idx;
  986. unsigned int hid;
  987. ht_cell_t *it;
  988. time_t now;
  989. if(ht==NULL || ht->entries==NULL)
  990. return -1;
  991. *val = 0;
  992. /* not auto-expire htable */
  993. if(ht->htexpire==0)
  994. return 0;
  995. hid = ht_compute_hash(name);
  996. idx = ht_get_entry(hid, ht->htsize);
  997. now = time(NULL);
  998. ht_slot_lock(ht, idx);
  999. it = ht->entries[idx].first;
  1000. while(it!=NULL && it->cellid < hid)
  1001. it = it->next;
  1002. while(it!=NULL && it->cellid == hid)
  1003. {
  1004. if(name->len==it->name.len
  1005. && strncmp(name->s, it->name.s, name->len)==0)
  1006. {
  1007. /* update value */
  1008. *val = (unsigned int)(it->expire - now);
  1009. ht_slot_unlock(ht, idx);
  1010. return 0;
  1011. }
  1012. it = it->next;
  1013. }
  1014. ht_slot_unlock(ht, idx);
  1015. return 0;
  1016. }
  1017. int ht_rm_cell_re(str *sre, ht_t *ht, int mode)
  1018. {
  1019. ht_cell_t *it;
  1020. ht_cell_t *it0;
  1021. int i;
  1022. regex_t re;
  1023. int match;
  1024. regmatch_t pmatch;
  1025. if(sre==NULL || sre->len<=0 || ht==NULL)
  1026. return -1;
  1027. if (regcomp(&re, sre->s, REG_EXTENDED|REG_ICASE|REG_NEWLINE))
  1028. {
  1029. LM_ERR("bad re %s\n", sre->s);
  1030. return -1;
  1031. }
  1032. for(i=0; i<ht->htsize; i++)
  1033. {
  1034. /* free entries */
  1035. ht_slot_lock(ht, i);
  1036. it = ht->entries[i].first;
  1037. while(it)
  1038. {
  1039. it0 = it->next;
  1040. match = 0;
  1041. if(mode==0)
  1042. {
  1043. if (regexec(&re, it->name.s, 1, &pmatch, 0)==0)
  1044. match = 1;
  1045. } else {
  1046. if(it->flags&AVP_VAL_STR)
  1047. if (regexec(&re, it->value.s.s, 1, &pmatch, 0)==0)
  1048. match = 1;
  1049. }
  1050. if(match==1)
  1051. {
  1052. if(it->prev==NULL)
  1053. ht->entries[i].first = it->next;
  1054. else
  1055. it->prev->next = it->next;
  1056. if(it->next)
  1057. it->next->prev = it->prev;
  1058. ht->entries[i].esize--;
  1059. ht_cell_free(it);
  1060. }
  1061. it = it0;
  1062. }
  1063. ht_slot_unlock(ht, i);
  1064. }
  1065. regfree(&re);
  1066. return 0;
  1067. }
  1068. int ht_reset_content(ht_t *ht)
  1069. {
  1070. ht_cell_t *it;
  1071. ht_cell_t *it0;
  1072. int i;
  1073. if(ht==NULL)
  1074. return -1;
  1075. for(i=0; i<ht->htsize; i++)
  1076. {
  1077. /* free entries */
  1078. ht_slot_lock(ht, i);
  1079. it = ht->entries[i].first;
  1080. while(it)
  1081. {
  1082. it0 = it->next;
  1083. if(it->prev==NULL)
  1084. ht->entries[i].first = it->next;
  1085. else
  1086. it->prev->next = it->next;
  1087. if(it->next)
  1088. it->next->prev = it->prev;
  1089. ht->entries[i].esize--;
  1090. ht_cell_free(it);
  1091. it = it0;
  1092. }
  1093. ht_slot_unlock(ht, i);
  1094. }
  1095. return 0;
  1096. }
  1097. int ht_count_cells_re(str *sre, ht_t *ht, int mode)
  1098. {
  1099. ht_cell_t *it;
  1100. ht_cell_t *it0;
  1101. int i;
  1102. regex_t re;
  1103. regmatch_t pmatch;
  1104. int cnt = 0;
  1105. int op = 0;
  1106. str sval;
  1107. str tval;
  1108. int ival = 0;
  1109. if(sre==NULL || sre->len<=0 || ht==NULL)
  1110. return 0;
  1111. if(sre->len>=2)
  1112. {
  1113. switch(sre->s[0]) {
  1114. case '~':
  1115. switch(sre->s[1]) {
  1116. case '~':
  1117. op = 1; /* regexp */
  1118. break;
  1119. case '%':
  1120. op = 2; /* rlike */
  1121. break;
  1122. }
  1123. break;
  1124. case '%':
  1125. switch(sre->s[1]) {
  1126. case '~':
  1127. op = 3; /* llike */
  1128. break;
  1129. }
  1130. break;
  1131. case '=':
  1132. switch(sre->s[1]) {
  1133. case '=':
  1134. op = 4; /* str eq */
  1135. break;
  1136. }
  1137. break;
  1138. case 'e':
  1139. switch(sre->s[1]) {
  1140. case 'q':
  1141. op = 5; /* int eq */
  1142. break;
  1143. }
  1144. break;
  1145. case '*':
  1146. switch(sre->s[1]) {
  1147. case '*':
  1148. op = 6; /* int eq */
  1149. break;
  1150. }
  1151. break;
  1152. }
  1153. }
  1154. if(op==6) {
  1155. /* count all */
  1156. for(i=0; i<ht->htsize; i++)
  1157. cnt += ht->entries[i].esize;
  1158. return cnt;
  1159. }
  1160. if(op > 0) {
  1161. if(sre->len<=2)
  1162. return 0;
  1163. sval = *sre;
  1164. sval.s += 2;
  1165. sval.len -= 2;
  1166. if(op==5) {
  1167. if(mode==0)
  1168. {
  1169. /* match by name */
  1170. return 0;
  1171. }
  1172. str2sint(&sval, &ival);
  1173. }
  1174. } else {
  1175. sval = *sre;
  1176. op = 1;
  1177. }
  1178. if(op==1)
  1179. {
  1180. if (regcomp(&re, sval.s, REG_EXTENDED|REG_ICASE|REG_NEWLINE))
  1181. {
  1182. LM_ERR("bad re %s\n", sre->s);
  1183. return 0;
  1184. }
  1185. }
  1186. for(i=0; i<ht->htsize; i++)
  1187. {
  1188. /* free entries */
  1189. ht_slot_lock(ht, i);
  1190. it = ht->entries[i].first;
  1191. while(it)
  1192. {
  1193. it0 = it->next;
  1194. if(op==5)
  1195. {
  1196. if(!(it->flags&AVP_VAL_STR))
  1197. if( it->value.n==ival)
  1198. cnt++;
  1199. } else {
  1200. tval.len = -1;
  1201. if(mode==0)
  1202. {
  1203. /* match by name */
  1204. tval = it->name;
  1205. } else {
  1206. if(it->flags&AVP_VAL_STR)
  1207. tval = it->value.s;
  1208. }
  1209. if(tval.len>-1) {
  1210. switch(op) {
  1211. case 1: /* regexp */
  1212. if (regexec(&re, tval.s, 1, &pmatch, 0)==0)
  1213. cnt++;
  1214. break;
  1215. case 2: /* rlike */
  1216. if(sval.len<=tval.len
  1217. && strncmp(sval.s,
  1218. tval.s+tval.len-sval.len, sval.len)==0)
  1219. cnt++;
  1220. break;
  1221. case 3: /* llike */
  1222. if(sval.len<=tval.len
  1223. && strncmp(sval.s, tval.s, sval.len)==0)
  1224. cnt++;
  1225. break;
  1226. case 4: /* str eq */
  1227. if(sval.len==tval.len
  1228. && strncmp(sval.s, tval.s, sval.len)==0)
  1229. cnt++;
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. it = it0;
  1235. }
  1236. ht_slot_unlock(ht, i);
  1237. }
  1238. if(op==1)
  1239. regfree(&re);
  1240. return cnt;
  1241. }
  1242. #define HT_ITERATOR_SIZE 4
  1243. #define HT_ITERATOR_NAME_SIZE 32
  1244. typedef struct ht_iterator {
  1245. str name;
  1246. char bname[HT_ITERATOR_NAME_SIZE];
  1247. ht_t *ht;
  1248. int slot;
  1249. ht_cell_t *it;
  1250. } ht_iterator_t;
  1251. static ht_iterator_t _ht_iterators[HT_ITERATOR_SIZE];
  1252. void ht_iterator_init(void)
  1253. {
  1254. memset(_ht_iterators, 0, HT_ITERATOR_SIZE*sizeof(ht_iterator_t));
  1255. }
  1256. int ht_iterator_start(str *iname, str *hname)
  1257. {
  1258. int i;
  1259. int k;
  1260. k = -1;
  1261. for(i=0; i<HT_ITERATOR_SIZE; i++)
  1262. {
  1263. if(_ht_iterators[i].name.len>0)
  1264. {
  1265. if(_ht_iterators[i].name.len==iname->len
  1266. && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
  1267. {
  1268. k = i;
  1269. break;
  1270. }
  1271. } else {
  1272. if(k==-1) k = i;
  1273. }
  1274. }
  1275. if(k==-1)
  1276. {
  1277. LM_ERR("no iterator available - max number is %d\n", HT_ITERATOR_SIZE);
  1278. return -1;
  1279. }
  1280. if(_ht_iterators[k].name.len>0)
  1281. {
  1282. if(_ht_iterators[k].ht!=NULL && _ht_iterators[k].it!=NULL)
  1283. {
  1284. if(_ht_iterators[k].slot>=0 && _ht_iterators[k].slot<_ht_iterators[k].ht->htsize)
  1285. {
  1286. ht_slot_unlock(_ht_iterators[k].ht, _ht_iterators[k].slot);
  1287. }
  1288. }
  1289. } else {
  1290. if(iname->len>=HT_ITERATOR_NAME_SIZE)
  1291. {
  1292. LM_ERR("iterator name is too big [%.*s] (max %d)\n",
  1293. iname->len, iname->s, HT_ITERATOR_NAME_SIZE);
  1294. return -1;
  1295. }
  1296. strncpy(_ht_iterators[k].bname, iname->s, iname->len);
  1297. _ht_iterators[k].bname[iname->len] = '\0';
  1298. _ht_iterators[k].name.len = iname->len;
  1299. _ht_iterators[k].name.s = _ht_iterators[k].bname;
  1300. }
  1301. _ht_iterators[k].it = NULL;
  1302. _ht_iterators[k].slot = 0;
  1303. _ht_iterators[k].ht = ht_get_table(hname);
  1304. if(_ht_iterators[k].ht==NULL)
  1305. {
  1306. LM_ERR("cannot get hash table [%.*s]\n", hname->len, hname->s);
  1307. return -1;
  1308. }
  1309. return 0;
  1310. }
  1311. int ht_iterator_next(str *iname)
  1312. {
  1313. int i;
  1314. int k;
  1315. k = -1;
  1316. for(i=0; i<HT_ITERATOR_SIZE; i++)
  1317. {
  1318. if(_ht_iterators[i].name.len>0)
  1319. {
  1320. if(_ht_iterators[i].name.len==iname->len
  1321. && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
  1322. {
  1323. k = i;
  1324. break;
  1325. }
  1326. } else {
  1327. if(k==-1) k = i;
  1328. }
  1329. }
  1330. if(k==-1)
  1331. {
  1332. LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
  1333. return -1;
  1334. }
  1335. if(_ht_iterators[k].ht==NULL)
  1336. {
  1337. LM_ERR("iterator not initialized [%.*s]\n", iname->len, iname->s);
  1338. return -1;
  1339. }
  1340. if(_ht_iterators[k].it==NULL)
  1341. {
  1342. /* first execution - start from first slot */
  1343. _ht_iterators[k].slot=0;
  1344. } else {
  1345. _ht_iterators[k].it = _ht_iterators[k].it->next;
  1346. if(_ht_iterators[k].it!=NULL)
  1347. {
  1348. /* next item is in the same slot */
  1349. return 0;
  1350. }
  1351. /* next is not in the same slot - release and try next one */
  1352. _ht_iterators[k].it = NULL;
  1353. ht_slot_unlock(_ht_iterators[k].ht, _ht_iterators[k].slot);
  1354. _ht_iterators[k].slot++;
  1355. }
  1356. for( ; _ht_iterators[k].slot<_ht_iterators[k].ht->htsize; _ht_iterators[k].slot++)
  1357. {
  1358. ht_slot_lock(_ht_iterators[k].ht, _ht_iterators[k].slot);
  1359. if(_ht_iterators[k].ht->entries[_ht_iterators[k].slot].first!=NULL)
  1360. {
  1361. _ht_iterators[k].it = _ht_iterators[k].ht->entries[_ht_iterators[k].slot].first;
  1362. return 0;
  1363. }
  1364. ht_slot_unlock(_ht_iterators[k].ht, _ht_iterators[k].slot);
  1365. }
  1366. return -1;
  1367. }
  1368. int ht_iterator_end(str *iname)
  1369. {
  1370. int i;
  1371. for(i=0; i<HT_ITERATOR_SIZE; i++)
  1372. {
  1373. if(_ht_iterators[i].name.len>0)
  1374. {
  1375. if(_ht_iterators[i].name.len==iname->len
  1376. && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
  1377. {
  1378. if(_ht_iterators[i].ht!=NULL && _ht_iterators[i].it!=NULL)
  1379. {
  1380. if(_ht_iterators[i].slot>=0 && _ht_iterators[i].slot<_ht_iterators[i].ht->htsize)
  1381. {
  1382. ht_slot_unlock(_ht_iterators[i].ht, _ht_iterators[i].slot);
  1383. }
  1384. }
  1385. memset(&_ht_iterators[i], 0, sizeof(ht_iterator_t));
  1386. return 0;
  1387. }
  1388. }
  1389. }
  1390. return -1;
  1391. }
  1392. ht_cell_t* ht_iterator_get_current(str *iname)
  1393. {
  1394. int i;
  1395. if(iname==NULL || iname->len<=0)
  1396. return NULL;
  1397. for(i=0; i<HT_ITERATOR_SIZE; i++)
  1398. {
  1399. if(_ht_iterators[i].name.len>0)
  1400. {
  1401. if(_ht_iterators[i].name.len==iname->len
  1402. && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
  1403. {
  1404. return _ht_iterators[i].it;
  1405. }
  1406. }
  1407. }
  1408. return NULL;
  1409. }