transaction.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Smile Communications, [email protected]
  5. * Copyright (C) 2012 Smile Communications, [email protected]
  6. *
  7. * The initial version of this code was written by Dragos Vingarzan
  8. * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
  9. * Fruanhofer Institute. It was and still is maintained in a separate
  10. * branch of the original SER. We are therefore migrating it to
  11. * Kamailio/SR and look forward to maintaining it from here on out.
  12. * 2011/2012 Smile Communications, Pty. Ltd.
  13. * ported/maintained/improved by
  14. * Jason Penton (jason(dot)penton(at)smilecoms.com and
  15. * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
  16. * effort to add full IMS support to Kamailio/SR using a new and
  17. * improved architecture
  18. *
  19. * NB: Alot of this code was originally part of OpenIMSCore,
  20. * FhG Fokus.
  21. * Copyright (C) 2004-2006 FhG Fokus
  22. * Thanks for great work! This is an effort to
  23. * break apart the various CSCF functions into logically separate
  24. * components. We hope this will drive wider use. We also feel
  25. * that in this way the architecture is more complete and thereby easier
  26. * to manage in the Kamailio/SR environment
  27. *
  28. * This file is part of Kamailio, a free SIP server.
  29. *
  30. * Kamailio is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version
  34. *
  35. * Kamailio is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  43. *
  44. */
  45. #include "transaction.h"
  46. #include "timer.h"
  47. #include "globals.h"
  48. cdp_trans_list_t *trans_list=0; /**< list of transactions */
  49. /**
  50. * Initializes the transaction structure.
  51. * Also adds a timer callback for checking the transaction statuses
  52. * @returns 1 if success or 0 on error
  53. */
  54. int cdp_trans_init()
  55. {
  56. trans_list = shm_malloc(sizeof(cdp_trans_list_t));
  57. if (!trans_list){
  58. LOG_NO_MEM("shm",sizeof(cdp_trans_list_t));
  59. return 0;
  60. }
  61. trans_list->head = 0;
  62. trans_list->tail = 0;
  63. trans_list->lock = lock_alloc();
  64. trans_list->lock = lock_init(trans_list->lock);
  65. add_timer(1,0,cdp_trans_timer,0);
  66. return 1;
  67. }
  68. int cdp_trans_destroy()
  69. {
  70. cdp_trans_t *t=0;
  71. if (trans_list){
  72. lock_get(trans_list->lock);
  73. while(trans_list->head){
  74. t = trans_list->head;
  75. trans_list->head = t->next;
  76. cdp_free_trans(t);
  77. }
  78. lock_destroy(trans_list->lock);
  79. lock_dealloc((void*)trans_list->lock);
  80. shm_free(trans_list);
  81. trans_list = 0;
  82. }
  83. return 1;
  84. }
  85. /**
  86. * Create and add a transaction to the transaction list.
  87. * @param msg - the message that this related to
  88. * @param cb - callback to be called on response or time-out
  89. * @param ptr - generic pointer to pass to the callback on call
  90. * @param timeout - timeout time in seconds
  91. * @param auto_drop - whether to auto drop the transaction on event, or let the application do it later
  92. * @returns the created cdp_trans_t* or NULL on error
  93. */
  94. inline cdp_trans_t* cdp_add_trans(AAAMessage *msg,AAATransactionCallback_f *cb, void *ptr,int timeout,int auto_drop)
  95. {
  96. cdp_trans_t *x;
  97. x = shm_malloc(sizeof(cdp_trans_t));
  98. if (!x) {
  99. LOG_NO_MEM("shm",sizeof(cdp_trans_t));
  100. return 0;
  101. }
  102. x->ptr = shm_malloc(sizeof(void*));
  103. if (!x->ptr) {
  104. LOG_NO_MEM("shm",sizeof(void*));
  105. shm_free(x);
  106. return 0;
  107. }
  108. gettimeofday(&x->started, NULL);
  109. x->endtoendid = msg->endtoendId;
  110. x->hopbyhopid = msg->hopbyhopId;
  111. x->cb = cb;
  112. *(x->ptr) = ptr;
  113. x->expires = timeout + time(0);
  114. x->auto_drop = auto_drop;
  115. x->next = 0;
  116. lock_get(trans_list->lock);
  117. x->prev = trans_list->tail;
  118. if (trans_list->tail) trans_list->tail->next = x;
  119. trans_list->tail = x;
  120. if (!trans_list->head) trans_list->head = x;
  121. lock_release(trans_list->lock);
  122. return x;
  123. }
  124. /**
  125. * Remove from the list and deallocate a transaction.
  126. * @param msg - the message that relates to that particular transaction
  127. */
  128. inline void del_trans(AAAMessage *msg)
  129. {
  130. cdp_trans_t *x;
  131. lock_get(trans_list->lock);
  132. x = trans_list->head;
  133. while(x&& x->endtoendid!=msg->endtoendId && x->hopbyhopid!=msg->hopbyhopId) x = x->next;
  134. if (x){
  135. if (x->prev) x->prev->next = x->next;
  136. else trans_list->head = x->next;
  137. if (x->next) x->next->prev = x->prev;
  138. else trans_list->tail = x->prev;
  139. cdp_free_trans(x);
  140. }
  141. lock_release(trans_list->lock);
  142. }
  143. /**
  144. * Return and remove the transaction from the transaction list.
  145. * @param msg - the message that this transaction relates to
  146. * @returns the cdp_trans_t* if found or NULL if not
  147. */
  148. inline cdp_trans_t* cdp_take_trans(AAAMessage *msg)
  149. {
  150. cdp_trans_t *x;
  151. lock_get(trans_list->lock);
  152. x = trans_list->head;
  153. while(x&& x->endtoendid!=msg->endtoendId && x->hopbyhopid!=msg->hopbyhopId) x = x->next;
  154. if (x){
  155. if (x->prev) x->prev->next = x->next;
  156. else trans_list->head = x->next;
  157. if (x->next) x->next->prev = x->prev;
  158. else trans_list->tail = x->prev;
  159. }
  160. lock_release(trans_list->lock);
  161. return x;
  162. }
  163. /**
  164. * Deallocate the memory taken by a transaction.
  165. * @param x - the transaction to deallocate
  166. */
  167. inline void cdp_free_trans(cdp_trans_t *x)
  168. {
  169. if (x->ptr) shm_free(x->ptr);
  170. shm_free(x);
  171. }
  172. /**
  173. * Timer callback for checking the transaction status.
  174. * @param now - time of call
  175. * @param ptr - generic pointer, passed to the transactional callbacks
  176. */
  177. int cdp_trans_timer(time_t now, void* ptr)
  178. {
  179. cdp_trans_t *x,*n;
  180. LM_DBG("trans_timer(): taking care of diameter transactions...\n");
  181. lock_get(trans_list->lock);
  182. x = trans_list->head;
  183. while(x)
  184. {
  185. if (now>x->expires){
  186. update_stat(stat_cdp_timeouts, 1); //Transaction has timed out waiting for response
  187. x->ans = 0;
  188. if (x->cb){
  189. (x->cb)(1,*(x->ptr),0, (now - x->expires));
  190. }
  191. n = x->next;
  192. if (x->prev) x->prev->next = x->next;
  193. else trans_list->head = x->next;
  194. if (x->next) x->next->prev = x->prev;
  195. else trans_list->tail = x->prev;
  196. if (x->auto_drop) cdp_free_trans(x);
  197. x = n;
  198. } else
  199. x = x->next;
  200. }
  201. lock_release(trans_list->lock);
  202. return 1;
  203. }
  204. /* TRANSACTIONS */
  205. /**
  206. * Create a AAATransaction for the given request.
  207. * @param app_id - id of the request's application
  208. * @param cmd_code - request's code
  209. * @returns the AAATransaction*
  210. */
  211. AAATransaction *AAACreateTransaction(AAAApplicationId app_id,AAACommandCode cmd_code)
  212. {
  213. AAATransaction *t;
  214. t = shm_malloc(sizeof(AAATransaction));
  215. if (!t) return 0;
  216. memset(t,0,sizeof(AAATransaction));
  217. t->application_id=app_id;
  218. t->command_code=cmd_code;
  219. return t;
  220. }
  221. /**
  222. * Free the memory allocated for the AAATransaction.
  223. * @param trans - the AAATransaction to be deallocated
  224. * @returns 1 on success, 0 on failure
  225. */
  226. int AAADropTransaction(AAATransaction *trans)
  227. {
  228. if (!trans) return 0;
  229. shm_free(trans);
  230. return 1;
  231. }