2
0

dlg_cb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_nodlg(str *callid, str *ftag, str *ttag,
  83. int types, dialog_cb f,
  84. void *param, param_free_cb ff )
  85. {
  86. struct dlg_cell *dlg;
  87. unsigned int dir = DLG_DIR_NONE;
  88. dlg = get_dlg(callid, ftag, ttag, &dir); //increments ref count!
  89. if (!dlg) {
  90. LM_ERR("Can't find dialog to register callback\n");
  91. return -1;
  92. }
  93. int ret = register_dlgcb(dlg, types, f, param, ff);
  94. unref_dlg(dlg, 1);
  95. return ret;
  96. }
  97. int register_dlgcb(struct dlg_cell *dlg, int types, dialog_cb f,
  98. void *param, param_free_cb ff )
  99. {
  100. struct dlg_callback *cb;
  101. if ( types&DLGCB_LOADED ) {
  102. if (types!=DLGCB_LOADED) {
  103. LM_CRIT("DLGCB_LOADED type must be register alone!\n");
  104. return -1;
  105. }
  106. } else if ( types&DLGCB_CREATED ) {
  107. if (types!=DLGCB_CREATED) {
  108. LM_CRIT("DLGCB_CREATED type must be register alone!\n");
  109. return -1;
  110. }
  111. } else {
  112. if (dlg==0) {
  113. LM_CRIT("non-DLGCB_CREATED type "
  114. "must be register to a dialog (dlg missing)!\n");
  115. return -1;
  116. }
  117. }
  118. cb = (struct dlg_callback*)shm_malloc(sizeof(struct dlg_callback));
  119. if (cb==0) {
  120. LM_ERR("no more shm mem\n");
  121. return -1;
  122. }
  123. cb->types = types;
  124. cb->callback = f;
  125. cb->param = param;
  126. cb->callback_param_free = ff;
  127. if ( types==DLGCB_CREATED ) {
  128. if (load_cbs==POINTER_CLOSED_MARKER) {
  129. LM_CRIT("DLGCB_CREATED type registered after shutdown!?!\n");
  130. goto error;
  131. }
  132. if (create_cbs==0) {
  133. /* not initialized yet */
  134. if ( (create_cbs=init_dlg_callback())==NULL ) {
  135. LM_ERR("no more shm mem\n");
  136. goto error;
  137. }
  138. }
  139. cb->next = create_cbs->first;
  140. create_cbs->first = cb;
  141. create_cbs->types |= types;
  142. } else if (types==DLGCB_LOADED) {
  143. if (load_cbs==POINTER_CLOSED_MARKER) {
  144. /* run the callback on the spot */
  145. run_load_callback(cb);
  146. destroy_dlg_callbacks_list(cb);
  147. return 0;
  148. }
  149. if (load_cbs==0) {
  150. /* not initialized yet */
  151. if ( (load_cbs=init_dlg_callback())==NULL ) {
  152. LM_ERR("no more shm mem\n");
  153. goto error;
  154. }
  155. }
  156. cb->next = load_cbs->first;
  157. load_cbs->first = cb;
  158. load_cbs->types |= types;
  159. } else {
  160. cb->next = dlg->cbs.first;
  161. dlg->cbs.first = cb;
  162. dlg->cbs.types |= types;
  163. }
  164. return 0;
  165. error:
  166. shm_free(cb);
  167. return -1;
  168. }
  169. static void run_load_callback(struct dlg_callback *cb)
  170. {
  171. struct dlg_cell *dlg;
  172. unsigned int i;
  173. params.req = NULL;
  174. params.rpl = NULL;
  175. params.direction = DLG_DIR_NONE;
  176. params.param = &cb->param;
  177. for( i=0 ; i<d_table->size ; i++ ) {
  178. for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next )
  179. cb->callback( dlg, DLGCB_LOADED, &params );
  180. }
  181. return;
  182. }
  183. void run_load_callbacks( void )
  184. {
  185. struct dlg_callback *cb;
  186. if (load_cbs && load_cbs!=POINTER_CLOSED_MARKER) {
  187. for ( cb=load_cbs->first; cb; cb=cb->next )
  188. run_load_callback( cb );
  189. }
  190. return;
  191. }
  192. void run_create_callbacks(struct dlg_cell *dlg, struct sip_msg *msg)
  193. {
  194. struct dlg_callback *cb;
  195. if (create_cbs==NULL || create_cbs->first==NULL)
  196. return;
  197. params.req = msg;
  198. params.rpl = NULL;
  199. /* initial request goes DOWNSTREAM all the time */
  200. params.direction = DLG_DIR_DOWNSTREAM;
  201. /* avoid garbage due static structure */
  202. params.param = NULL;
  203. params.dlg_data = NULL;
  204. for ( cb=create_cbs->first; cb; cb=cb->next) {
  205. LM_DBG("dialog=%p\n",dlg);
  206. params.param = &cb->param;
  207. cb->callback( dlg, DLGCB_CREATED, &params );
  208. }
  209. return;
  210. }
  211. void run_dlg_callbacks( int type ,
  212. struct dlg_cell *dlg,
  213. struct sip_msg *req,
  214. struct sip_msg *rpl,
  215. unsigned int dir, void *dlg_data)
  216. {
  217. struct dlg_callback *cb;
  218. params.req = req;
  219. params.rpl = rpl;
  220. params.direction = dir;
  221. params.dlg_data = dlg_data;
  222. if (dlg->cbs.first==0 || ((dlg->cbs.types)&type)==0 )
  223. return;
  224. for ( cb=dlg->cbs.first; cb; cb=cb->next) {
  225. if ( (cb->types)&type ) {
  226. LM_DBG("dialog=%p, type=%d\n", dlg, type);
  227. params.param = &cb->param;
  228. cb->callback( dlg, type, &params );
  229. }
  230. }
  231. return;
  232. }