nonsip_hooks.c 2.7 KB

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