dlg_cb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 Voice Sistem SRL
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * History:
  23. * --------
  24. * 2006-04-14 initial version (bogdan)
  25. * 2008-04-04 added direction reporting in dlg callbacks (bogdan)
  26. * 2008-04-14 DLGCB_CREATED may be registered before the module
  27. * initialization (bogdan)
  28. * 2008-04-15 added new type of callback to be triggered when dialogs are
  29. * loaded from DB (bogdan)
  30. */
  31. #include "../../mem/shm_mem.h"
  32. #include "../../dprint.h"
  33. #include "dlg_hash.h"
  34. #include "dlg_cb.h"
  35. static struct dlg_head_cbl* create_cbs = 0;
  36. static struct dlg_head_cbl* load_cbs = 0;
  37. static struct dlg_cb_params params = {NULL, NULL, DLG_DIR_NONE, NULL, NULL};
  38. #define POINTER_CLOSED_MARKER ((void *)(-1))
  39. static void run_load_callback(struct dlg_callback *cb);
  40. static struct dlg_head_cbl* init_dlg_callback(void)
  41. {
  42. struct dlg_head_cbl *new_cbs;
  43. new_cbs = (struct dlg_head_cbl*)shm_malloc(sizeof(struct dlg_head_cbl));
  44. if (new_cbs==0) {
  45. LM_ERR("no more shm mem\n");
  46. return 0;
  47. }
  48. new_cbs->first = 0;
  49. new_cbs->types = 0;
  50. return new_cbs;
  51. }
  52. void destroy_dlg_callbacks_list(struct dlg_callback *cb)
  53. {
  54. struct dlg_callback *cb_t;
  55. while(cb) {
  56. cb_t = cb;
  57. cb = cb->next;
  58. if (cb_t->callback_param_free && cb_t->param) {
  59. cb_t->callback_param_free(cb_t->param);
  60. cb_t->param = NULL;
  61. }
  62. shm_free(cb_t);
  63. }
  64. }
  65. void destroy_dlg_callbacks(unsigned int types)
  66. {
  67. if (types&DLGCB_CREATED) {
  68. if (create_cbs && create_cbs!=POINTER_CLOSED_MARKER) {
  69. destroy_dlg_callbacks_list(create_cbs->first);
  70. shm_free(create_cbs);
  71. create_cbs = POINTER_CLOSED_MARKER;
  72. }
  73. }
  74. if (types&DLGCB_LOADED) {
  75. if (load_cbs && load_cbs!=POINTER_CLOSED_MARKER) {
  76. destroy_dlg_callbacks_list(load_cbs->first);
  77. shm_free(load_cbs);
  78. load_cbs = POINTER_CLOSED_MARKER;
  79. }
  80. }
  81. }
  82. int register_dlgcb(struct dlg_cell *dlg, int types, dialog_cb f,
  83. void *param, param_free_cb ff )
  84. {
  85. struct dlg_callback *cb;
  86. if ( types&DLGCB_LOADED ) {
  87. if (types!=DLGCB_LOADED) {
  88. LM_CRIT("DLGCB_LOADED type must be register alone!\n");
  89. return -1;
  90. }
  91. } else if ( types&DLGCB_CREATED ) {
  92. if (types!=DLGCB_CREATED) {
  93. LM_CRIT("DLGCB_CREATED type must be register alone!\n");
  94. return -1;
  95. }
  96. } else {
  97. if (dlg==0) {
  98. LM_CRIT("non-DLGCB_CREATED type "
  99. "must be register to a dialog (dlg missing)!\n");
  100. return -1;
  101. }
  102. }
  103. cb = (struct dlg_callback*)shm_malloc(sizeof(struct dlg_callback));
  104. if (cb==0) {
  105. LM_ERR("no more shm mem\n");
  106. return -1;
  107. }
  108. cb->types = types;
  109. cb->callback = f;
  110. cb->param = param;
  111. cb->callback_param_free = ff;
  112. if ( types==DLGCB_CREATED ) {
  113. if (load_cbs==POINTER_CLOSED_MARKER) {
  114. LM_CRIT("DLGCB_CREATED type registered after shutdown!?!\n");
  115. goto error;
  116. }
  117. if (create_cbs==0) {
  118. /* not initialized yet */
  119. if ( (create_cbs=init_dlg_callback())==NULL ) {
  120. LM_ERR("no more shm mem\n");
  121. goto error;
  122. }
  123. }
  124. cb->next = create_cbs->first;
  125. create_cbs->first = cb;
  126. create_cbs->types |= types;
  127. } else if (types==DLGCB_LOADED) {
  128. if (load_cbs==POINTER_CLOSED_MARKER) {
  129. /* run the callback on the spot */
  130. run_load_callback(cb);
  131. destroy_dlg_callbacks_list(cb);
  132. return 0;
  133. }
  134. if (load_cbs==0) {
  135. /* not initialized yet */
  136. if ( (load_cbs=init_dlg_callback())==NULL ) {
  137. LM_ERR("no more shm mem\n");
  138. goto error;
  139. }
  140. }
  141. cb->next = load_cbs->first;
  142. load_cbs->first = cb;
  143. load_cbs->types |= types;
  144. } else {
  145. cb->next = dlg->cbs.first;
  146. dlg->cbs.first = cb;
  147. dlg->cbs.types |= types;
  148. }
  149. return 0;
  150. error:
  151. shm_free(cb);
  152. return -1;
  153. }
  154. static void run_load_callback(struct dlg_callback *cb)
  155. {
  156. struct dlg_cell *dlg;
  157. unsigned int i;
  158. params.req = NULL;
  159. params.rpl = NULL;
  160. params.direction = DLG_DIR_NONE;
  161. params.param = &cb->param;
  162. for( i=0 ; i<d_table->size ; i++ ) {
  163. for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next )
  164. cb->callback( dlg, DLGCB_LOADED, &params );
  165. }
  166. return;
  167. }
  168. void run_load_callbacks( void )
  169. {
  170. struct dlg_callback *cb;
  171. if (load_cbs && load_cbs!=POINTER_CLOSED_MARKER) {
  172. for ( cb=load_cbs->first; cb; cb=cb->next )
  173. run_load_callback( cb );
  174. }
  175. return;
  176. }
  177. void run_create_callbacks(struct dlg_cell *dlg, struct sip_msg *msg)
  178. {
  179. struct dlg_callback *cb;
  180. if (create_cbs==NULL || create_cbs->first==NULL)
  181. return;
  182. params.req = msg;
  183. params.rpl = NULL;
  184. /* initial request goes DOWNSTREAM all the time */
  185. params.direction = DLG_DIR_DOWNSTREAM;
  186. /* avoid garbage due static structure */
  187. params.param = NULL;
  188. params.dlg_data = NULL;
  189. for ( cb=create_cbs->first; cb; cb=cb->next) {
  190. LM_DBG("dialog=%p\n",dlg);
  191. params.param = &cb->param;
  192. cb->callback( dlg, DLGCB_CREATED, &params );
  193. }
  194. return;
  195. }
  196. void run_dlg_callbacks( int type ,
  197. struct dlg_cell *dlg,
  198. struct sip_msg *req,
  199. struct sip_msg *rpl,
  200. unsigned int dir, void *dlg_data)
  201. {
  202. struct dlg_callback *cb;
  203. params.req = req;
  204. params.rpl = rpl;
  205. params.direction = dir;
  206. params.dlg_data = dlg_data;
  207. if (dlg->cbs.first==0 || ((dlg->cbs.types)&type)==0 )
  208. return;
  209. for ( cb=dlg->cbs.first; cb; cb=cb->next) {
  210. if ( (cb->types)&type ) {
  211. LM_DBG("dialog=%p, type=%d\n", dlg, type);
  212. params.param = &cb->param;
  213. cb->callback( dlg, type, &params );
  214. }
  215. }
  216. return;
  217. }