script_cb.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * $Id$
  3. *
  4. * Script callbacks -- they add the ability to register callback
  5. * functions which are always called when script for request
  6. * processing is entered or left
  7. *
  8. *
  9. * Copyright (C) 2001-2003 FhG Fokus
  10. *
  11. * This file is part of ser, a free SIP server.
  12. *
  13. * ser is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version
  17. *
  18. * For a license to use the ser software under conditions
  19. * other than those described here, or to purchase support for this
  20. * software, please contact iptel.org by e-mail at the following addresses:
  21. * [email protected]
  22. *
  23. * ser is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. *
  32. * History:
  33. * --------
  34. * 2003-03-29 cleaning pkg allocation introduced (jiri)
  35. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  36. * 2005-02-13 script callbacks devided into request and reply types (bogdan)
  37. * 2009-06-01 Added pre- and post-script callback support for all types
  38. * of route blocks. (Miklos)
  39. */
  40. /*!
  41. * \file
  42. * \brief SIP-router core ::
  43. * \ingroup core
  44. * Module: \ref core
  45. */
  46. #include <stdlib.h>
  47. #include "script_cb.h"
  48. #include "dprint.h"
  49. #include "error.h"
  50. #include "mem/mem.h"
  51. /* Number of cb types = last cb type */
  52. #define SCRIPT_CB_NUM (MAX_CB_TYPE-1)
  53. static struct script_cb *pre_script_cb[SCRIPT_CB_NUM];
  54. static struct script_cb *post_script_cb[SCRIPT_CB_NUM];
  55. /* Add a script callback to the beginning of the linked list.
  56. * Returns -1 on error
  57. */
  58. static inline int add_callback( struct script_cb **list,
  59. cb_function f, void *param)
  60. {
  61. struct script_cb *new_cb;
  62. new_cb=pkg_malloc(sizeof(struct script_cb));
  63. if (new_cb==0) {
  64. LM_CRIT("out of memory\n");
  65. return -1;
  66. }
  67. new_cb->cbf = f;
  68. new_cb->param = param;
  69. /* link at the beginning of the list */
  70. new_cb->next = *list;
  71. *list = new_cb;
  72. return 0;
  73. }
  74. /* Register pre- or post-script callbacks.
  75. * Returns -1 on error, 0 on success
  76. */
  77. int register_script_cb( cb_function f, unsigned int flags, void *param )
  78. {
  79. struct script_cb **cb_array;
  80. int i;
  81. /* type checkings */
  82. if ( (flags&((1<<SCRIPT_CB_NUM)-1))==0 ) {
  83. LOG(L_BUG, "register_script_cb: callback flag not specified\n");
  84. return -1;
  85. }
  86. if ( (flags&(~(PRE_SCRIPT_CB|POST_SCRIPT_CB))) >= 1<<SCRIPT_CB_NUM ) {
  87. LOG(L_BUG, "register_script_cb: unsupported callback flags: %u\n",
  88. flags);
  89. return -1;
  90. }
  91. if ( (flags&(PRE_SCRIPT_CB|POST_SCRIPT_CB))==0 ||
  92. (flags&PRE_SCRIPT_CB && flags&POST_SCRIPT_CB) ) {
  93. LOG(L_BUG, "register_script_cb: callback POST or PRE type must "
  94. "be exactly one\n");
  95. return -1;
  96. }
  97. if (flags&PRE_SCRIPT_CB)
  98. cb_array = pre_script_cb;
  99. else
  100. cb_array = post_script_cb;
  101. /* Add the callback to the lists.
  102. * (as many times as many flags are set)
  103. */
  104. for (i=0; i<SCRIPT_CB_NUM; i++) {
  105. if ((flags&(1<<i)) == 0)
  106. continue;
  107. if (add_callback(&cb_array[i], f, param) < 0)
  108. goto add_error;
  109. }
  110. return 0;
  111. add_error:
  112. LM_ERR("failed to add callback\n");
  113. return -1;
  114. }
  115. int init_script_cb()
  116. {
  117. memset(pre_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
  118. memset(post_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
  119. return 0;
  120. }
  121. static inline void destroy_cb_list(struct script_cb **list)
  122. {
  123. struct script_cb *foo;
  124. while( *list ) {
  125. foo = *list;
  126. *list = (*list)->next;
  127. pkg_free( foo );
  128. }
  129. }
  130. void destroy_script_cb()
  131. {
  132. int i;
  133. for (i=0; i<SCRIPT_CB_NUM; i++) {
  134. destroy_cb_list(&pre_script_cb[i]);
  135. destroy_cb_list(&post_script_cb[i]);
  136. }
  137. }
  138. /* Execute pre-script callbacks of a given type.
  139. * Returns 0 on error, 1 on success
  140. */
  141. int exec_pre_script_cb( struct sip_msg *msg, enum script_cb_type type)
  142. {
  143. struct script_cb *cb;
  144. unsigned int flags;
  145. #ifdef EXTRA_DEBUG
  146. if (type > SCRIPT_CB_NUM) {
  147. LOG(L_BUG, "Uknown callback type\n");
  148. abort();
  149. }
  150. #endif
  151. flags = PRE_SCRIPT_CB | (1<<(type-1));
  152. for (cb=pre_script_cb[type-1]; cb ; cb=cb->next ) {
  153. /* stop on error */
  154. if (cb->cbf(msg, flags, cb->param)==0)
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. /* Execute post-script callbacks of a given type.
  160. * Always returns 1, success.
  161. */
  162. int exec_post_script_cb( struct sip_msg *msg, enum script_cb_type type)
  163. {
  164. struct script_cb *cb;
  165. unsigned int flags;
  166. #ifdef EXTRA_DEBUG
  167. if (type > SCRIPT_CB_NUM) {
  168. LOG(L_BUG, "exec_pre_script_cb: Uknown callback type\n");
  169. abort();
  170. }
  171. #endif
  172. flags = POST_SCRIPT_CB | (1<<(type-1));
  173. for (cb=post_script_cb[type-1]; cb ; cb=cb->next){
  174. cb->cbf(msg, flags, cb->param);
  175. }
  176. return 1;
  177. }