script_cb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. */
  38. #include <stdlib.h>
  39. #include "script_cb.h"
  40. #include "dprint.h"
  41. #include "error.h"
  42. #include "mem/mem.h"
  43. static struct script_cb *pre_req_cb=0;
  44. static struct script_cb *post_req_cb=0;
  45. static struct script_cb *pre_rpl_cb=0;
  46. static struct script_cb *post_rpl_cb=0;
  47. static unsigned int cb_id=0;
  48. static inline int add_callback( struct script_cb **list,
  49. cb_function f, void *param)
  50. {
  51. struct script_cb *new_cb;
  52. new_cb=pkg_malloc(sizeof(struct script_cb));
  53. if (new_cb==0) {
  54. LOG(L_ERR, "ERROR:add_script_callback: out of memory\n");
  55. return -1;
  56. }
  57. new_cb->cbf = f;
  58. new_cb->id = cb_id++;
  59. new_cb->param = param;
  60. /* link at the beginning of the list */
  61. new_cb->next = *list;
  62. *list = new_cb;
  63. return 0;
  64. }
  65. int register_script_cb( cb_function f, int type, void *param )
  66. {
  67. /* type checkings */
  68. if ( (type&(REQ_TYPE_CB|RPL_TYPE_CB))==0 ) {
  69. LOG(L_CRIT,"BUG:register_script_cb: REQUEST or REPLY "
  70. "type not specified\n");
  71. goto error;
  72. }
  73. if ( (type&(PRE_SCRIPT_CB|POST_SCRIPT_CB))==0 ||
  74. (type&PRE_SCRIPT_CB && type&POST_SCRIPT_CB) ) {
  75. LOG(L_CRIT,"BUG:register_script_cb: callback POST or PRE type must "
  76. "be exactly one\n");
  77. goto error;
  78. }
  79. if (type&REQ_TYPE_CB) {
  80. /* callback for request script */
  81. if (type&PRE_SCRIPT_CB) {
  82. if (add_callback( &pre_req_cb, f, param)<0)
  83. goto add_error;
  84. } else if (type&POST_SCRIPT_CB) {
  85. if (add_callback( &post_req_cb, f, param)<0)
  86. goto add_error;
  87. }
  88. }
  89. if (type&REQ_TYPE_CB) {
  90. /* callback (also) for reply script */
  91. if (type&PRE_SCRIPT_CB) {
  92. if (add_callback( &pre_rpl_cb, f, param)<0)
  93. goto add_error;
  94. } else if (type&POST_SCRIPT_CB) {
  95. if (add_callback( &post_rpl_cb, f, param)<0)
  96. goto add_error;
  97. }
  98. }
  99. return 0;
  100. add_error:
  101. LOG(L_ERR,"ERROR:register_script_cb: failed to add callback\n");
  102. error:
  103. return -1;
  104. }
  105. static inline void destroy_cb_list(struct script_cb **list)
  106. {
  107. struct script_cb *foo;
  108. while( *list ) {
  109. foo = *list;
  110. *list = (*list)->next;
  111. pkg_free( foo );
  112. }
  113. }
  114. void destroy_script_cb()
  115. {
  116. destroy_cb_list( &pre_req_cb );
  117. destroy_cb_list( &post_req_cb );
  118. destroy_cb_list( &pre_rpl_cb );
  119. destroy_cb_list( &post_req_cb );
  120. }
  121. static inline int exec_pre_cb( struct sip_msg *msg, struct script_cb *cb)
  122. {
  123. for ( ; cb ; cb=cb->next ) {
  124. /* stop on error */
  125. if (cb->cbf(msg, cb->param)==0)
  126. return 0;
  127. }
  128. return 1;
  129. }
  130. static inline int exec_post_cb( struct sip_msg *msg, struct script_cb *cb)
  131. {
  132. for ( ; cb ; cb=cb->next){
  133. cb->cbf( msg, cb->param);
  134. }
  135. return 1;
  136. }
  137. int exec_pre_req_cb( struct sip_msg *msg)
  138. {
  139. return exec_pre_cb( msg, pre_req_cb);
  140. }
  141. int exec_pre_rpl_cb( struct sip_msg *msg)
  142. {
  143. return exec_pre_cb( msg, pre_rpl_cb);
  144. }
  145. int exec_post_req_cb( struct sip_msg *msg)
  146. {
  147. return exec_post_cb( msg, post_req_cb);
  148. }
  149. int exec_post_rpl_cb( struct sip_msg *msg)
  150. {
  151. return exec_post_cb( msg, post_rpl_cb);
  152. }