ht_api.c 31 KB

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