nonsip_hooks.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * non-sip callbacks, called whenever a message with protocol != SIP/2.0
  29. * is received (the message must have at least a sip like first line or
  30. * else they will be dropped before this callbacks are called
  31. */
  32. /*
  33. * History:
  34. * --------
  35. * 2006-11-29 created by andrei
  36. */
  37. /*!
  38. * \file
  39. * \brief SIP-router core ::
  40. * \ingroup core
  41. * Module: \ref core
  42. */
  43. #include "nonsip_hooks.h"
  44. #include "mem/mem.h"
  45. static struct nonsip_hook* nonsip_hooks;
  46. static unsigned int nonsip_max_hooks=MAX_NONSIP_HOOKS;
  47. static int last_hook_idx=0;
  48. int init_nonsip_hooks()
  49. {
  50. nonsip_hooks=pkg_malloc(nonsip_max_hooks*
  51. sizeof(struct nonsip_hook));
  52. if (nonsip_hooks==0){
  53. goto error;
  54. }
  55. memset(nonsip_hooks, 0, nonsip_max_hooks*sizeof(struct nonsip_hook));
  56. return 0;
  57. error:
  58. LM_ERR("memory allocation failure\n");
  59. return -1;
  60. }
  61. void destroy_nonsip_hooks()
  62. {
  63. int r;
  64. if (nonsip_hooks){
  65. for (r=0; r<last_hook_idx; r++){
  66. if (nonsip_hooks[r].destroy)
  67. nonsip_hooks[r].destroy();
  68. }
  69. pkg_free(nonsip_hooks);
  70. nonsip_hooks=0;
  71. }
  72. }
  73. /* allocates a new hook
  74. * returns 0 on success and -1 on error */
  75. int register_nonsip_msg_hook(struct nonsip_hook *h)
  76. {
  77. struct nonsip_hook* tmp;
  78. int new_max_hooks;
  79. if (nonsip_max_hooks==0)
  80. goto error;
  81. if (last_hook_idx >= nonsip_max_hooks){
  82. new_max_hooks=2*nonsip_max_hooks;
  83. tmp=pkg_realloc(nonsip_hooks,
  84. new_max_hooks*sizeof(struct nonsip_hook));
  85. if (tmp==0){
  86. goto error;
  87. }
  88. nonsip_hooks=tmp;
  89. /* init the new chunk */
  90. memset(&nonsip_hooks[last_hook_idx+1], 0,
  91. (new_max_hooks-nonsip_max_hooks-1)*
  92. sizeof(struct nonsip_hook));
  93. nonsip_max_hooks=new_max_hooks;
  94. }
  95. nonsip_hooks[last_hook_idx]=*h;
  96. last_hook_idx++;
  97. return 0;
  98. error:
  99. return -1;
  100. }
  101. int nonsip_msg_run_hooks(struct sip_msg* msg)
  102. {
  103. int r;
  104. int ret;
  105. ret=NONSIP_MSG_DROP; /* default, if no hook installed, drop */
  106. for (r=0; r<last_hook_idx; r++){
  107. ret=nonsip_hooks[r].on_nonsip_req(msg);
  108. if (ret!=NONSIP_MSG_PASS) break;
  109. }
  110. return ret;
  111. }