script_cb.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. #include <stdlib.h>
  41. #include "script_cb.h"
  42. #include "dprint.h"
  43. #include "error.h"
  44. #include "mem/mem.h"
  45. /* Number of cb types = last cb type */
  46. #define SCRIPT_CB_NUM EVENT_CB_TYPE
  47. static struct script_cb *pre_script_cb[SCRIPT_CB_NUM];
  48. static struct script_cb *post_script_cb[SCRIPT_CB_NUM];
  49. /* Add a script callback to the beginning of the linked list.
  50. * Returns -1 on error
  51. */
  52. static inline int add_callback( struct script_cb **list,
  53. cb_function f, void *param)
  54. {
  55. struct script_cb *new_cb;
  56. new_cb=pkg_malloc(sizeof(struct script_cb));
  57. if (new_cb==0) {
  58. LOG(L_CRIT, "add_script_callback: out of memory\n");
  59. return -1;
  60. }
  61. new_cb->cbf = f;
  62. new_cb->param = param;
  63. /* link at the beginning of the list */
  64. new_cb->next = *list;
  65. *list = new_cb;
  66. return 0;
  67. }
  68. /* Register pre- or post-script callbacks.
  69. * Returns -1 on error, 0 on success
  70. */
  71. int register_script_cb( cb_function f, unsigned int flags, void *param )
  72. {
  73. struct script_cb **cb_array;
  74. int i;
  75. /* type checkings */
  76. if ( (flags&((1<<SCRIPT_CB_NUM)-1))==0 ) {
  77. LOG(L_BUG, "register_script_cb: callback flag not specified\n");
  78. return -1;
  79. }
  80. if ( (flags&(~(PRE_SCRIPT_CB|POST_SCRIPT_CB))) >= 1<<SCRIPT_CB_NUM ) {
  81. LOG(L_BUG, "register_script_cb: unsupported callback flags: %u\n",
  82. flags);
  83. return -1;
  84. }
  85. if ( (flags&(PRE_SCRIPT_CB|POST_SCRIPT_CB))==0 ||
  86. (flags&PRE_SCRIPT_CB && flags&POST_SCRIPT_CB) ) {
  87. LOG(L_BUG, "register_script_cb: callback POST or PRE type must "
  88. "be exactly one\n");
  89. return -1;
  90. }
  91. if (flags&PRE_SCRIPT_CB)
  92. cb_array = pre_script_cb;
  93. else
  94. cb_array = post_script_cb;
  95. /* Add the callback to the lists.
  96. * (as many times as many flags are set)
  97. */
  98. for (i=0; i<SCRIPT_CB_NUM; i++) {
  99. if ((flags&(1<<i)) == 0)
  100. continue;
  101. if (add_callback(&cb_array[i], f, param) < 0)
  102. goto add_error;
  103. }
  104. return 0;
  105. add_error:
  106. LOG(L_ERR,"register_script_cb: failed to add callback\n");
  107. return -1;
  108. }
  109. int init_script_cb()
  110. {
  111. memset(pre_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
  112. memset(post_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
  113. return 0;
  114. }
  115. static inline void destroy_cb_list(struct script_cb **list)
  116. {
  117. struct script_cb *foo;
  118. while( *list ) {
  119. foo = *list;
  120. *list = (*list)->next;
  121. pkg_free( foo );
  122. }
  123. }
  124. void destroy_script_cb()
  125. {
  126. int i;
  127. for (i=0; i<SCRIPT_CB_NUM; i++) {
  128. destroy_cb_list(&pre_script_cb[i]);
  129. destroy_cb_list(&post_script_cb[i]);
  130. }
  131. }
  132. /* Execute pre-script callbacks of a given type.
  133. * Returns 0 on error, 1 on success
  134. */
  135. int exec_pre_script_cb( struct sip_msg *msg, enum script_cb_type type)
  136. {
  137. struct script_cb *cb;
  138. unsigned int flags;
  139. #ifdef EXTRA_DEBUG
  140. if (type >= SCRIPT_CB_NUM) {
  141. LOG(L_BUG, "exec_pre_script_cb: Uknown callback type\n");
  142. abort();
  143. }
  144. #endif
  145. flags = PRE_SCRIPT_CB | (1<<(type-1));
  146. for (cb=pre_script_cb[type-1]; cb ; cb=cb->next ) {
  147. /* stop on error */
  148. if (cb->cbf(msg, flags, cb->param)==0)
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. /* Execute post-script callbacks of a given type.
  154. * Always returns 1, success.
  155. */
  156. int exec_post_script_cb( struct sip_msg *msg, enum script_cb_type type)
  157. {
  158. struct script_cb *cb;
  159. unsigned int flags;
  160. #ifdef EXTRA_DEBUG
  161. if (type >= SCRIPT_CB_NUM) {
  162. LOG(L_BUG, "exec_pre_script_cb: Uknown callback type\n");
  163. abort();
  164. }
  165. #endif
  166. flags = POST_SCRIPT_CB | (1<<(type-1));
  167. for (cb=post_script_cb[type-1]; cb ; cb=cb->next){
  168. cb->cbf(msg, flags, cb->param);
  169. }
  170. return 1;
  171. }