urecord.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * $Id$
  3. *
  4. * Usrloc record structure
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History:
  30. * ---------
  31. * 2003-03-12 added replication mark and zombie state support (nils)
  32. * 2004-03-17 generic callbacks added (bogdan)
  33. * 2004-06-07 updated to the new DB api (andrei)
  34. */
  35. #include "urecord.h"
  36. #include <string.h>
  37. #include "../../mem/shm_mem.h"
  38. #include "../../dprint.h"
  39. #include "../../ut.h"
  40. #include "ul_mod.h"
  41. #include "utime.h"
  42. /* #include "del_list.h" */
  43. /* #include "ins_list.h" */
  44. #include "notify.h"
  45. #include "ul_callback.h"
  46. #include "reg_avps.h"
  47. /*
  48. * Create and initialize new record structure
  49. */
  50. int new_urecord(str* _dom, str* _uid, urecord_t** _r)
  51. {
  52. *_r = (urecord_t*)shm_malloc(sizeof(urecord_t));
  53. if (*_r == 0) {
  54. LOG(L_ERR, "new_urecord(): No memory left\n");
  55. return -1;
  56. }
  57. memset(*_r, 0, sizeof(urecord_t));
  58. (*_r)->uid.s = (char*)shm_malloc(_uid->len);
  59. if ((*_r)->uid.s == 0) {
  60. LOG(L_ERR, "new_urecord(): No memory left\n");
  61. shm_free(*_r);
  62. return -2;
  63. }
  64. memcpy((*_r)->uid.s, _uid->s, _uid->len);
  65. (*_r)->uid.len = _uid->len;
  66. (*_r)->domain = _dom;
  67. return 0;
  68. }
  69. /*
  70. * Free all memory used by the given structure
  71. * The structure must be removed from all linked
  72. * lists first
  73. */
  74. void free_urecord(urecord_t* _r)
  75. {
  76. notify_cb_t* watcher;
  77. ucontact_t* ptr;
  78. while(_r->watchers) {
  79. watcher = _r->watchers;
  80. _r->watchers = watcher->next;
  81. shm_free(watcher);
  82. }
  83. while(_r->contacts) {
  84. ptr = _r->contacts;
  85. _r->contacts = _r->contacts->next;
  86. free_ucontact(ptr);
  87. }
  88. if (_r->uid.s) shm_free(_r->uid.s);
  89. shm_free(_r);
  90. }
  91. /*
  92. * Print a record
  93. */
  94. void print_urecord(FILE* _f, urecord_t* _r)
  95. {
  96. ucontact_t* ptr;
  97. fprintf(_f, "...Record(%p)...\n", _r);
  98. fprintf(_f, "domain: '%.*s'\n", _r->domain->len, ZSW(_r->domain->s));
  99. fprintf(_f, "uid : '%.*s'\n", _r->uid.len, ZSW(_r->uid.s));
  100. if (_r->contacts) {
  101. ptr = _r->contacts;
  102. while(ptr) {
  103. print_ucontact(_f, ptr);
  104. ptr = ptr->next;
  105. }
  106. }
  107. fprintf(_f, ".../Record...\n");
  108. }
  109. /*
  110. * Add a new contact
  111. * Contacts are ordered by: 1) q
  112. * 2) descending modification time
  113. */
  114. int mem_insert_ucontact(urecord_t* _r, str* aor, str* _c, time_t _e, qvalue_t _q, str* _cid, int _cs,
  115. unsigned int _flags, struct ucontact** _con, str* _ua, str* _recv,
  116. struct socket_info* sock, str* _inst, int sid)
  117. {
  118. ucontact_t* ptr, *prev = 0;
  119. if (new_ucontact(_r->domain, &_r->uid, aor, _c, _e, _q, _cid, _cs, _flags, _con, _ua, _recv, sock, _inst, sid) < 0) {
  120. LOG(L_ERR, "mem_insert_ucontact(): Can't create new contact\n");
  121. return -1;
  122. }
  123. ptr = _r->contacts;
  124. if (!desc_time_order) {
  125. while(ptr) {
  126. if (ptr->q < _q) break;
  127. prev = ptr;
  128. ptr = ptr->next;
  129. }
  130. }
  131. if (ptr) {
  132. if (!ptr->prev) {
  133. ptr->prev = *_con;
  134. (*_con)->next = ptr;
  135. _r->contacts = *_con;
  136. } else {
  137. (*_con)->next = ptr;
  138. (*_con)->prev = ptr->prev;
  139. ptr->prev->next = *_con;
  140. ptr->prev = *_con;
  141. }
  142. } else if (prev) {
  143. prev->next = *_con;
  144. (*_con)->prev = prev;
  145. } else {
  146. _r->contacts = *_con;
  147. }
  148. return 0;
  149. }
  150. /*
  151. * Remove the contact from lists
  152. */
  153. void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c)
  154. {
  155. if (_c->prev) {
  156. _c->prev->next = _c->next;
  157. if (_c->next) {
  158. _c->next->prev = _c->prev;
  159. }
  160. } else {
  161. _r->contacts = _c->next;
  162. if (_c->next) {
  163. _c->next->prev = 0;
  164. }
  165. }
  166. }
  167. /*
  168. * Remove contact from the list and delete
  169. */
  170. void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c)
  171. {
  172. mem_remove_ucontact(_r, _c);
  173. free_ucontact(_c);
  174. }
  175. /*
  176. * This timer routine is used when
  177. * db_mode is set to NO_DB or READONLY
  178. */
  179. static inline int nodb_timer(urecord_t* _r)
  180. {
  181. ucontact_t* ptr, *t;
  182. int not = 0;
  183. ptr = _r->contacts;
  184. while(ptr) {
  185. if (!VALID_CONTACT(ptr, act_time)) {
  186. /* run callbacks for EXPIRE event */
  187. if (exists_ulcb_type(UL_CONTACT_EXPIRE))
  188. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  189. notify_watchers(_r, ptr, PRES_OFFLINE);
  190. LOG(L_NOTICE, "Binding '%.*s','%.*s' has expired\n",
  191. ptr->uid->len, ZSW(ptr->uid->s),
  192. ptr->c.len, ZSW(ptr->c.s));
  193. t = ptr;
  194. ptr = ptr->next;
  195. /* it was the last contact and it was in normal
  196. * state, so notify */
  197. if (!ptr && t->state == CS_NEW) not=1;
  198. delete_reg_avps(t);
  199. mem_delete_ucontact(_r, t);
  200. _r->slot->d->expired++;
  201. } else {
  202. ptr = ptr->next;
  203. }
  204. }
  205. return 0;
  206. }
  207. /*
  208. * This routine is used when db_mode is
  209. * set to WRITE_THROUGH
  210. */
  211. static inline int wt_timer(urecord_t* _r)
  212. {
  213. ucontact_t* ptr, *t;
  214. int not = 0;
  215. ptr = _r->contacts;
  216. while(ptr) {
  217. if (!VALID_CONTACT(ptr, act_time)) {
  218. /* run callbacks for EXPIRE event */
  219. if (exists_ulcb_type(UL_CONTACT_EXPIRE)) {
  220. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  221. }
  222. notify_watchers(_r, ptr, PRES_OFFLINE);
  223. LOG(L_NOTICE, "Binding '%.*s','%.*s' has expired\n",
  224. ptr->uid->len, ZSW(ptr->uid->s),
  225. ptr->c.len, ZSW(ptr->c.s));
  226. t = ptr;
  227. ptr = ptr->next;
  228. /* it was the last contact and it was in normal
  229. * state, so notify */
  230. if (!ptr && t->state == CS_SYNC) not=1;
  231. if (db_delete_ucontact(t) < 0) {
  232. LOG(L_ERR, "wt_timer(): Error while deleting contact from "
  233. "database\n");
  234. }
  235. delete_reg_avps(t);
  236. mem_delete_ucontact(_r, t);
  237. _r->slot->d->expired++;
  238. } else {
  239. /* the contact was unregistered and is not marked
  240. * for replication so remove it, but the notify was
  241. * already done during unregister */
  242. ptr = ptr->next;
  243. }
  244. }
  245. return 0;
  246. }
  247. /*
  248. * Write-back timer
  249. */
  250. static inline int wb_timer(urecord_t* _r)
  251. {
  252. ucontact_t* ptr, *t;
  253. int op;
  254. int not = 0;
  255. ptr = _r->contacts;
  256. while(ptr) {
  257. if (!VALID_CONTACT(ptr, act_time)) {
  258. /* run callbacks for EXPIRE event */
  259. if (exists_ulcb_type(UL_CONTACT_EXPIRE)) {
  260. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  261. }
  262. notify_watchers(_r, ptr, PRES_OFFLINE);
  263. LOG(L_NOTICE, "Binding '%.*s','%.*s' has expired\n",
  264. ptr->uid->len, ZSW(ptr->uid->s),
  265. ptr->c.len, ZSW(ptr->c.s));
  266. if (ptr->next == 0) not=1;
  267. _r->slot->d->expired++;
  268. t = ptr;
  269. ptr = ptr->next;
  270. /* Should we remove the contact from the database ? */
  271. if (st_expired_ucontact(t) == 1) {
  272. if (db_delete_ucontact(t) < 0) {
  273. LOG(L_ERR, "wb_timer(): Can't delete contact from the database\n");
  274. }
  275. }
  276. delete_reg_avps(t);
  277. mem_delete_ucontact(_r, t);
  278. } else {
  279. /* Determine the operation we have to do */
  280. op = st_flush_ucontact(ptr);
  281. switch(op) {
  282. case 0: /* do nothing, contact is synchronized */
  283. break;
  284. case 1: /* insert */
  285. if (db_store_ucontact(ptr) < 0) {
  286. LOG(L_ERR, "wb_timer(): Error while inserting contact into database\n");
  287. }
  288. break;
  289. case 2: /* update */
  290. if (db_store_ucontact(ptr) < 0) {
  291. LOG(L_ERR, "wb_timer(): Error while updating contact in db\n");
  292. }
  293. break;
  294. case 4: /* delete */
  295. if (db_delete_ucontact(ptr) < 0) {
  296. LOG(L_ERR, "wb_timer(): Can't delete contact from database\n");
  297. }
  298. /* fall through to the next case statement */
  299. case 3: /* delete from memory */
  300. delete_reg_avps(ptr);
  301. mem_delete_ucontact(_r, ptr);
  302. break;
  303. }
  304. ptr = ptr->next;
  305. }
  306. }
  307. return 0;
  308. }
  309. int timer_urecord(urecord_t* _r)
  310. {
  311. switch(db_mode) {
  312. case NO_DB: return nodb_timer(_r);
  313. case WRITE_THROUGH: return wt_timer(_r);
  314. case WRITE_BACK: return wb_timer(_r);
  315. case READONLY: return nodb_timer(_r);
  316. }
  317. return 0; /* Makes gcc happy */
  318. }
  319. /*
  320. * Release urecord previously obtained
  321. * through get_urecord
  322. */
  323. void release_urecord(urecord_t* _r)
  324. {
  325. if (_r->contacts == 0) {
  326. mem_delete_urecord(_r->slot->d, _r);
  327. }
  328. }
  329. /*
  330. * Create and insert new contact
  331. * into urecord
  332. */
  333. int insert_ucontact(urecord_t* _r, str* aor, str* _c, time_t _e, qvalue_t _q, str* _cid,
  334. int _cs, unsigned int _flags, struct ucontact** _con, str* _ua, str* _recv,
  335. struct socket_info* sock, str* _inst, int sid)
  336. {
  337. if (mem_insert_ucontact(_r, aor, _c, _e, _q, _cid, _cs, _flags, _con, _ua, _recv, sock, _inst, sid) < 0) {
  338. LOG(L_ERR, "insert_ucontact(): Error while inserting contact\n");
  339. return -1;
  340. }
  341. notify_watchers(_r, *_con, (_e > 0) ? PRES_ONLINE : PRES_OFFLINE);
  342. if (exists_ulcb_type(UL_CONTACT_INSERT)) {
  343. run_ul_callbacks( UL_CONTACT_INSERT, *_con);
  344. }
  345. save_reg_avps(*_con);
  346. if (db_mode == WRITE_THROUGH) {
  347. if (db_store_ucontact(*_con) < 0) {
  348. LOG(L_ERR, "insert_ucontact(): Error while inserting in database\n");
  349. }
  350. (*_con)->state = CS_SYNC;
  351. }
  352. return 0;
  353. }
  354. /*
  355. * Delete ucontact from urecord
  356. */
  357. int delete_ucontact(urecord_t* _r, struct ucontact* _c)
  358. {
  359. if (exists_ulcb_type(UL_CONTACT_DELETE)) {
  360. run_ul_callbacks( UL_CONTACT_DELETE, _c);
  361. }
  362. notify_watchers(_r, _c, PRES_OFFLINE);
  363. if (st_delete_ucontact(_c) > 0) {
  364. if (db_mode == WRITE_THROUGH) {
  365. if (db_delete_ucontact(_c) < 0) {
  366. LOG(L_ERR, "delete_ucontact(): Can't remove contact from "
  367. "database\n");
  368. }
  369. }
  370. delete_reg_avps(_c);
  371. mem_delete_ucontact(_r, _c);
  372. }
  373. return 0;
  374. }
  375. /*
  376. * Get pointer to ucontact with given contact
  377. */
  378. int get_ucontact(urecord_t* _r, str* _c, struct ucontact** _co)
  379. {
  380. ucontact_t* ptr;
  381. ptr = _r->contacts;
  382. while(ptr) {
  383. if ((_c->len == ptr->c.len &&
  384. !memcmp(_c->s, ptr->c.s, _c->len))) {
  385. *_co = ptr;
  386. return 0;
  387. }
  388. ptr = ptr->next;
  389. }
  390. return 1;
  391. }
  392. /*
  393. * Get pointer to ucontact with given contact and given sip.instance
  394. */
  395. int get_ucontact_by_instance(urecord_t* _r, str* _c, str* _i, struct ucontact** _co)
  396. {
  397. ucontact_t* ptr;
  398. if (_i == NULL) {
  399. return get_ucontact(_r, _c, _co);
  400. }
  401. ptr = _r->contacts;
  402. while(ptr) {
  403. if ((_i->len == ptr->instance.len &&
  404. !memcmp(_i->s, ptr->instance.s, _i->len)) ||
  405. (_c->len == ptr->c.len &&
  406. !memcmp(_c->s, ptr->c.s, _c->len))) {
  407. *_co = ptr;
  408. return 0;
  409. }
  410. ptr = ptr->next;
  411. }
  412. return 1;
  413. }