pdomain.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Presence Agent, domain support
  3. *
  4. * $Id$
  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. /*
  30. * History:
  31. * --------
  32. * 2003-03-11 converted to the new locking scheme: locking.h (andrei)
  33. */
  34. #include "pdomain.h"
  35. #include "paerrno.h"
  36. #include "presentity.h"
  37. #include "../../ut.h"
  38. #include "../../dprint.h"
  39. #include "../../mem/shm_mem.h"
  40. #include <cds/logger.h>
  41. #include "pa_mod.h"
  42. #include <time.h>
  43. /*
  44. * Hash function
  45. */
  46. static inline unsigned int hash_func(pdomain_t* _d, unsigned char* _s, int _l)
  47. {
  48. unsigned int res = 0, i;
  49. for(i = 0; i < _l; i++) {
  50. res += _s[i];
  51. }
  52. return res % _d->size;
  53. }
  54. /*
  55. * Create a new domain structure
  56. * _n is pointer to str representing
  57. * name of the domain, the string is
  58. * not copied, it should point to str
  59. * structure stored in domain list
  60. * _s is hash table size
  61. */
  62. int new_pdomain(str* _n, int _s, pdomain_t** _d, register_watcher_t _r, unregister_watcher_t _u)
  63. {
  64. int i;
  65. pdomain_t* ptr;
  66. ptr = (pdomain_t*)mem_alloc(sizeof(pdomain_t));
  67. if (!ptr) {
  68. paerrno = PA_NO_MEMORY;
  69. LOG(L_ERR, "new_pdomain(): No memory left\n");
  70. return -1;
  71. }
  72. memset(ptr, 0, sizeof(pdomain_t));
  73. ptr->table = (hslot_t*)mem_alloc(sizeof(hslot_t) * _s);
  74. if (!ptr->table) {
  75. paerrno = PA_NO_MEMORY;
  76. LOG(L_ERR, "new_pdomain(): No memory left 2\n");
  77. mem_free(ptr);
  78. return -2;
  79. }
  80. ptr->name = _n;
  81. for(i = 0; i < _s; i++) {
  82. init_slot(ptr, &ptr->table[i]);
  83. }
  84. ptr->size = _s;
  85. lock_init(&ptr->lock);
  86. ptr->users = 0;
  87. ptr->expired = 0;
  88. ptr->reg = _r;
  89. ptr->unreg = _u;
  90. *_d = ptr;
  91. return 0;
  92. }
  93. /*
  94. * Free all memory allocated for
  95. * the domain
  96. */
  97. void free_pdomain(pdomain_t* _d)
  98. {
  99. int i;
  100. lock_pdomain(_d);
  101. if (_d->table) {
  102. for(i = 0; i < _d->size; i++) {
  103. deinit_slot(_d->table + i);
  104. }
  105. mem_free(_d->table);
  106. }
  107. unlock_pdomain(_d);
  108. mem_free(_d);
  109. }
  110. int timer_pdomain(pdomain_t* _d)
  111. {
  112. struct presentity* presentity, *t;
  113. time_t start, stop;
  114. PROF_START(pa_timer_pdomain)
  115. lock_pdomain(_d);
  116. start = time(NULL);
  117. presentity = _d->first;
  118. while(presentity) {
  119. if (timer_presentity(presentity) < 0) {
  120. LOG(L_ERR, "timer_pdomain(): Error in timer_pdomain\n");
  121. unlock_pdomain(_d);
  122. PROF_STOP(pa_timer_pdomain)
  123. return -1;
  124. }
  125. /* Remove the entire record
  126. * if it is empty
  127. */
  128. if ( (!presentity->first_watcher) &&
  129. (!presentity->first_winfo_watcher) &&
  130. (!presentity->data.first_tuple) &&
  131. (!presentity->data.first_note) &&
  132. (!presentity->data.first_unknown_element) &&
  133. (!presentity->first_qsa_subscription) &&
  134. (presentity->ref_cnt == 0)) {
  135. LOG(L_DBG, "timer_pdomain(): removing empty presentity\n");
  136. t = presentity;
  137. presentity = presentity->next;
  138. release_presentity(t);
  139. } else {
  140. presentity = presentity->next;
  141. }
  142. }
  143. stop = time(NULL);
  144. if (stop - start > 1) WARN("timer_pdomain took %d seconds\n", (int) (stop - start));
  145. unlock_pdomain(_d);
  146. PROF_STOP(pa_timer_pdomain)
  147. return 0;
  148. }
  149. static int in_pdomain = 0; /* this only works with single or multiprocess execution model, but not multi-threaded */
  150. /*
  151. * Get lock if this process does not already have it
  152. */
  153. void lock_pdomain(pdomain_t* _d)
  154. {
  155. DBG("lock_pdomain\n");
  156. if (!in_pdomain++)
  157. lock_get(&_d->lock);
  158. }
  159. /*
  160. * Release lock
  161. */
  162. void unlock_pdomain(pdomain_t* _d)
  163. {
  164. DBG("unlock_pdomain\n");
  165. in_pdomain--;
  166. if (!in_pdomain)
  167. lock_release(&_d->lock);
  168. }
  169. /*
  170. * Find a presentity in domain according to uid
  171. */
  172. int find_presentity_uid(pdomain_t* _d, str* uid, struct presentity** _p)
  173. {
  174. unsigned int sl, i;
  175. struct presentity* p;
  176. int res = 1;
  177. if ((!uid) || (!_p)) return -1;
  178. sl = hash_func(_d, (unsigned char *)uid->s, uid->len);
  179. p = _d->table[sl].first;
  180. for(i = 0; i < _d->table[sl].n; i++) {
  181. if ((p->uuid.len == uid->len) && !memcmp(p->uuid.s, uid->s, uid->len)) {
  182. *_p = p;
  183. res = 0;
  184. break;
  185. }
  186. p = p->next;
  187. }
  188. return res; /* Nothing found */
  189. }
  190. /*
  191. * contact will be NULL if user is offline
  192. */
  193. static void callback(str* _user, str *_contact, int state, void* data)
  194. {
  195. mq_message_t *msg;
  196. tuple_change_info_t *info;
  197. if ((!_user) || (!_contact) || (!data)) {
  198. ERROR_LOG("callback(): error!\n");
  199. }
  200. /* asynchronous processing */
  201. msg = create_message_ex(sizeof(tuple_change_info_t));
  202. if (!msg) {
  203. LOG(L_ERR, "can't create message with tuple status change\n");
  204. return;
  205. }
  206. set_data_destroy_function(msg, (destroy_function_f)free_tuple_change_info_content);
  207. info = get_message_data(msg);
  208. if (state == 0) info->state = presence_tuple_closed;
  209. else info->state = presence_tuple_open;
  210. str_dup(&info->user, _user);
  211. str_dup(&info->contact, _contact);
  212. if (data) push_message(&((struct presentity*)data)->mq, msg);
  213. }
  214. void add_presentity(pdomain_t* _d, struct presentity* _p)
  215. {
  216. unsigned int sl;
  217. sl = hash_func(_d, (unsigned char *)_p->uuid.s, _p->uuid.len);
  218. slot_add(&_d->table[sl], _p, &_d->first, &_d->last);
  219. if (use_callbacks) {
  220. DBG("! registering callback to %.*s, %p\n", _p->uuid.len, _p->uuid.s,_p);
  221. _d->reg(&_p->data.uri, &_p->uuid, (void*)callback, _p);
  222. }
  223. if (subscribe_to_users) {
  224. TRACE("! subscribing to %.*s, %p\n", _p->uuid.len, _p->uuid.s,_p);
  225. subscribe_to_user(_p);
  226. }
  227. }
  228. void remove_presentity(pdomain_t* _d, struct presentity* _p)
  229. {
  230. if (use_callbacks) {
  231. DBG("! unregistering callback to %.*s, %p\n", _p->uuid.len, _p->uuid.s,_p);
  232. _d->unreg(&_p->data.uri, &_p->uuid, (void*)callback, _p);
  233. DBG("! unregistered callback to %.*s, %p\n", _p->uuid.len, _p->uuid.s,_p);
  234. }
  235. if (subscribe_to_users) {
  236. DBG("! unsubscribing from %.*s, %p\n", _p->uuid.len, _p->uuid.s,_p);
  237. unsubscribe_to_user(_p);
  238. }
  239. LOG(L_DBG, "remove_presentity _p=%p p_uri=%.*s\n", _p, _p->data.uri.len,
  240. _p->data.uri.s);
  241. slot_rem(_p->slot, _p, &_d->first, &_d->last);
  242. /* remove presentity from database */
  243. }