domains.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG FOKUS
  5. * Copyright (C) 2008 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /**
  29. * History
  30. * -------
  31. * 2003-04-07: a structure for both hashes introduced (ramona)
  32. * 2005-01-26: double hash removed (ramona)
  33. * FIFO operations are kept as a diff list (ramona)
  34. * domain hash kept in share memory along with FIFO ops (ramona)
  35. *
  36. */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include "../../sr_module.h"
  40. #include "../../parser/parse_fline.h"
  41. #include "../../lib/srdb2/db.h"
  42. #include "../../mem/shm_mem.h"
  43. #include "../../mem/mem.h"
  44. #include "../../dprint.h"
  45. #include "domains.h"
  46. pd_op_t* new_pd_op(pd_t *cell, int id, int op)
  47. {
  48. pd_op_t *pdo;
  49. if(cell==NULL)
  50. {
  51. LOG(L_ERR, "PDT:new_pd_op: bad parameters\n");
  52. return NULL;
  53. }
  54. pdo = (pd_op_t*)shm_malloc(sizeof(pd_op_t));
  55. if(pdo==NULL)
  56. {
  57. LOG(L_ERR, "PDT:new_pd_op: no more shm\n");
  58. return NULL;
  59. }
  60. memset(pdo, 0, sizeof(pd_op_t));
  61. pdo->cell = cell;
  62. pdo->id = id;
  63. pdo->op = op;
  64. return pdo;
  65. }
  66. void free_pd_op(pd_op_t *pdo)
  67. {
  68. if(pdo==NULL)
  69. return;
  70. free_cell(pdo->cell);
  71. shm_free(pdo);
  72. pdo = NULL;
  73. return;
  74. }
  75. pd_t* new_cell(str* p, str *d)
  76. {
  77. pd_t* cell = NULL;
  78. if(p==NULL || p->s==NULL || d==NULL || d->s==NULL)
  79. {
  80. LOG(L_ERR, "PDT:new_cell: bad parameters\n");
  81. return NULL;
  82. }
  83. /* the cell is in share memory */
  84. cell = (pd_t*)shm_malloc(sizeof(pd_t));
  85. /* if there is no space return just NULL */
  86. if(cell==NULL)
  87. {
  88. LOG(L_ERR, "PDT:new_cell: no more shm memory.\n");
  89. return NULL;
  90. }
  91. memset(cell, 0, sizeof(pd_t));
  92. cell->prefix.s = (char*)shm_malloc((1+p->len)*sizeof(char));
  93. if(cell->prefix.s==NULL)
  94. {
  95. shm_free(cell);
  96. LOG(L_ERR, "PDT:new_cell: no more shm memory\n");
  97. return NULL;
  98. }
  99. strncpy(cell->prefix.s, p->s, p->len);
  100. cell->prefix.len = p->len;
  101. cell->prefix.s[p->len] = '\0';
  102. cell->domain.s = (char*)shm_malloc((1+d->len)*sizeof(char));
  103. if(cell->domain.s==NULL)
  104. {
  105. shm_free(cell->prefix.s);
  106. shm_free(cell);
  107. LOG(L_ERR, "PDT:new_cell: no more shm memory!\n");
  108. return NULL;
  109. }
  110. strncpy(cell->domain.s, d->s, d->len);
  111. cell->domain.len = d->len;
  112. cell->domain.s[d->len] = '\0';
  113. cell->dhash = pdt_compute_hash(cell->domain.s);
  114. /* return the newly allocated in share memory cell */
  115. return cell;
  116. }
  117. void free_cell(pd_t* cell)
  118. {
  119. if(cell==NULL)
  120. return;
  121. if(cell->prefix.s)
  122. shm_free(cell->prefix.s);
  123. if(cell->domain.s)
  124. shm_free(cell->domain.s);
  125. shm_free(cell);
  126. }
  127. /* returns a pointer to a hashtable */
  128. pd_entry_t* init_hash(unsigned int hash_size)
  129. {
  130. int i, j;
  131. pd_entry_t *hash = NULL;
  132. /* space for the hash is allocated in share memory */
  133. hash = (pd_entry_t*)shm_malloc(hash_size*sizeof(pd_entry_t));
  134. if(hash == NULL)
  135. {
  136. LOG(L_ERR, "PDT:init_hash: no more shm\n");
  137. return NULL;
  138. }
  139. memset(hash, 0, hash_size*sizeof(pd_entry_t));
  140. /* create mutex semaphores for each entry of the hash */
  141. for(i=0; i<hash_size; i++)
  142. {
  143. if(lock_init(&hash[i].lock) == 0)
  144. {
  145. LOG(L_ERR, "PDT:init_hash: cannot init the lock\n");
  146. goto error;
  147. }
  148. hash[i].e = NULL;
  149. }
  150. /* the allocated hash */
  151. return hash;
  152. error:
  153. for(j=0; j<i; j++)
  154. lock_destroy(&hash[j].lock);
  155. shm_free(hash);
  156. return NULL;
  157. }
  158. pdt_hash_t* pdt_init_hash(int hs_two_pow)
  159. {
  160. pdt_hash_t* hash = NULL;
  161. int hash_size;
  162. if(hs_two_pow>MAX_HSIZE_TWO_POW || hs_two_pow<0)
  163. hash_size = MAX_HASH_SIZE;
  164. else
  165. hash_size = 1<<hs_two_pow;
  166. /* space for the double_hash is allocated in share memory */
  167. hash = (pdt_hash_t*)shm_malloc(sizeof(pdt_hash_t));
  168. if(hash == NULL)
  169. {
  170. LOG(L_ERR, "PDT:pdt_init_hash: no more shm\n");
  171. return NULL;
  172. }
  173. if(lock_init(&hash->diff_lock) == 0)
  174. {
  175. shm_free(hash);
  176. LOG(L_ERR, "PDT:pdt_init_hash: cannot init the diff lock\n");
  177. return NULL;
  178. }
  179. if( (hash->dhash = init_hash(hash_size)) == NULL )
  180. {
  181. lock_destroy(&hash->diff_lock);
  182. shm_free(hash);
  183. LOG(L_ERR, "PDT:pdt_init_hash: no more shm!\n");
  184. return NULL;
  185. }
  186. hash->hash_size = hash_size;
  187. return hash;
  188. }
  189. void free_hash(pd_entry_t* hash, unsigned int hash_size)
  190. {
  191. int i;
  192. pd_t *tmp, *it;
  193. if(hash==NULL || hash_size<=0)
  194. return;
  195. for(i=0; i<hash_size; i++)
  196. {
  197. it = hash[i].e;
  198. while(it != NULL)
  199. {
  200. tmp = it->n;
  201. free_cell(it);
  202. it = tmp;
  203. }
  204. lock_destroy(&hash[i].lock);
  205. }
  206. shm_free(hash);
  207. }
  208. void pdt_free_hash(pdt_hash_t* hash)
  209. {
  210. free_hash(hash->dhash, hash->hash_size);
  211. lock_destroy(&hash->diff_lock);
  212. /* todo: destroy diff list */
  213. shm_free(hash);
  214. }
  215. int pdt_add_to_hash(pdt_hash_t *hash, str *sp, str *sd)
  216. {
  217. int hash_entry=0;
  218. unsigned int dhash;
  219. pd_t *it, *tmp;
  220. pd_t *cell;
  221. if(hash==NULL || sp==NULL || sd==NULL)
  222. {
  223. LOG(L_ERR, "PDT:pdt_add_to_hash: bad parameters\n");
  224. return -1;
  225. }
  226. dhash = pdt_compute_hash(sd->s);
  227. hash_entry = get_hash_entry(dhash, hash->hash_size);
  228. lock_get(&hash->dhash[hash_entry].lock);
  229. it = hash->dhash[hash_entry].e;
  230. tmp = NULL;
  231. while(it!=NULL && it->dhash < dhash)
  232. {
  233. tmp = it;
  234. it = it->n;
  235. }
  236. /* we need a new entry for this cell */
  237. cell = new_cell(sp, sd);
  238. if(cell == NULL)
  239. {
  240. lock_release(&hash->dhash[hash_entry].lock);
  241. return -1;
  242. }
  243. if(tmp)
  244. tmp->n=cell;
  245. else
  246. hash->dhash[hash_entry].e = cell;
  247. cell->p=tmp;
  248. cell->n=it;
  249. if(it)
  250. it->p=cell;
  251. lock_release(&hash->dhash[hash_entry].lock);
  252. return 0;
  253. }
  254. int pdt_remove_from_hash(pdt_hash_t *hash, str *sd)
  255. {
  256. int hash_entry=0;
  257. unsigned int dhash;
  258. pd_t *it, *tmp;
  259. if(sd==NULL)
  260. return 0;
  261. if(hash==NULL)
  262. {
  263. LOG(L_ERR, "PDT:pdt_remove_from_hash: bad parameters\n");
  264. return -1;
  265. }
  266. /* find the list where the cell must be */
  267. dhash = pdt_compute_hash(sd->s);
  268. hash_entry = get_hash_entry(dhash, hash->hash_size);
  269. lock_get(&hash->dhash[hash_entry].lock);
  270. /* first element of the list */
  271. it = hash->dhash[hash_entry].e;
  272. /* find the cell in the list */
  273. /* a double linked list in the hash is kept alphabetically
  274. * or numerical ordered */
  275. tmp = NULL;
  276. while(it!=NULL)
  277. {
  278. if( it->dhash==dhash && it->domain.len==sd->len
  279. && strncasecmp(it->domain.s, sd->s, sd->len)==0)
  280. break;
  281. tmp = it;
  282. it = it->n;
  283. }
  284. if(it!=NULL)
  285. {
  286. if(tmp!=NULL)
  287. tmp->n = it->n;
  288. else
  289. hash->dhash[hash_entry].e = it->n;
  290. if(it->n)
  291. it->n->p = it->p;
  292. free_cell(it);
  293. }
  294. lock_release(&hash->dhash[hash_entry].lock);
  295. return 0;
  296. }
  297. str* pdt_get_prefix(pdt_hash_t *ph, str* sd)
  298. {
  299. int hash_entry;
  300. unsigned int dhash;
  301. pd_t* it;
  302. if(ph==NULL || ph->dhash==NULL || ph->hash_size>MAX_HASH_SIZE)
  303. {
  304. LOG(L_ERR, "PDT:pdt_get_prefix: bad parameters\n");
  305. return NULL;
  306. }
  307. dhash = pdt_compute_hash(sd->s);
  308. hash_entry = get_hash_entry(dhash, ph->hash_size);
  309. lock_get(&ph->dhash[hash_entry].lock);
  310. it = ph->dhash[hash_entry].e;
  311. while(it!=NULL && it->dhash<=dhash)
  312. {
  313. if(it->dhash==dhash && it->domain.len==sd->len
  314. && strncasecmp(it->domain.s, sd->s, sd->len)==0)
  315. {
  316. lock_release(&ph->dhash[hash_entry].lock);
  317. return &it->prefix;
  318. }
  319. it = it->n;
  320. }
  321. lock_release(&ph->dhash[hash_entry].lock);
  322. return NULL;
  323. }
  324. int pdt_check_pd(pdt_hash_t *ph, str *sp, str *sd)
  325. {
  326. int i;
  327. unsigned int dhash;
  328. pd_t* it;
  329. if(ph==NULL || sp==NULL || sd==NULL)
  330. {
  331. LOG(L_ERR, "PDT:pdt_check_pd: bad parameters\n");
  332. return -1;
  333. }
  334. dhash = pdt_compute_hash(sd->s);
  335. for(i=0; i<ph->hash_size; i++)
  336. {
  337. lock_get(&ph->dhash[i].lock);
  338. it = ph->dhash[i].e;
  339. while(it != NULL)
  340. {
  341. if((it->domain.len==sd->len
  342. && strncasecmp(it->domain.s, sd->s, sd->len)==0)
  343. || (it->prefix.len==sp->len
  344. && strncasecmp(it->prefix.s, sp->s, sp->len)==0))
  345. {
  346. lock_release(&ph->dhash[i].lock);
  347. return 1;
  348. }
  349. it = it->n;
  350. }
  351. lock_release(&ph->dhash[i].lock);
  352. }
  353. return 0;
  354. }
  355. void pdt_print_hash(pdt_hash_t* hash)
  356. {
  357. int i, count;
  358. pd_t *it;
  359. if(hash==NULL)
  360. {
  361. DBG("PDT:pdt_print_hash: empty...\n");
  362. return;
  363. }
  364. for(i=0; i<hash->hash_size; i++)
  365. {
  366. lock_get(&hash->dhash[i].lock);
  367. it = hash->dhash[i].e;
  368. DBG("PDT:pdt_print_hash: entry<%d>:\n", i);
  369. count = 0;
  370. while(it!=NULL)
  371. {
  372. DBG("PDT:pdt_print_hash: |Domain: %.*s |Code: %.*s | DHash:%u \n",
  373. it->domain.len, it->domain.s,
  374. it->prefix.len, it->prefix.s, it->dhash);
  375. it = it->n;
  376. count++;
  377. }
  378. lock_release(&hash->dhash[i].lock);
  379. DBG("PDT:pdt_print_hash: ---- has %d records\n\n", count);
  380. }
  381. }
  382. /* be sure s!=NULL */
  383. /* compute a hash value for a string, knowing also the hash dimension */
  384. unsigned int pdt_compute_hash(char* s)
  385. {
  386. #define h_inc h+=v^(v>>3);
  387. char* p;
  388. register unsigned v;
  389. register unsigned h;
  390. int len;
  391. len = strlen(s);
  392. h=0;
  393. for(p=s; p<=(s+len-4); p+=4)
  394. {
  395. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  396. h_inc;
  397. }
  398. v=0;
  399. for(;p<(s+len); p++)
  400. {
  401. v<<=8;
  402. v+=*p;
  403. }
  404. h_inc;
  405. return h;
  406. }