script_cb.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <stdlib.h>
  33. #include "script_cb.h"
  34. #include "dprint.h"
  35. #include "error.h"
  36. static struct script_cb *pre_cb=0;
  37. static struct script_cb *post_cb=0;
  38. static unsigned int cb_id=0;
  39. int register_script_cb( cb_function f, callback_t t, void *param )
  40. {
  41. struct script_cb *new_cb;
  42. new_cb=malloc(sizeof(struct script_cb));
  43. if (new_cb==0) {
  44. LOG(L_ERR, "ERROR: register_script_cb: out of memory\n");
  45. return E_OUT_OF_MEM;
  46. }
  47. new_cb->cbf=f;
  48. new_cb->id=cb_id++;
  49. new_cb->param=param;
  50. /* insert into appropriate list */
  51. if (t==PRE_SCRIPT_CB) {
  52. new_cb->next=pre_cb;
  53. pre_cb=new_cb;
  54. } else if (t==POST_SCRIPT_CB) {
  55. new_cb->next=post_cb;
  56. post_cb=new_cb;
  57. } else {
  58. LOG(L_CRIT, "ERROR: register_script_cb: unknown CB type\n");
  59. return E_BUG;
  60. }
  61. /* ok, callback installed */
  62. return 1;
  63. }
  64. int exec_pre_cb( struct sip_msg *msg)
  65. {
  66. struct script_cb *i;
  67. for (i=pre_cb; i; i=i->next) {
  68. /* stop on error */
  69. if (i->cbf(msg, i->param)==0)
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. void exec_post_cb( struct sip_msg *msg)
  75. {
  76. struct script_cb *i;
  77. for (i=post_cb; i; i=i->next) i->cbf(msg, i->param);
  78. }