nonsip_hooks.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. #include "nonsip_hooks.h"
  38. #include "mem/mem.h"
  39. static struct nonsip_hook* nonsip_hooks;
  40. static unsigned int nonsip_max_hooks=MAX_NONSIP_HOOKS;
  41. static int last_hook_idx=0;
  42. int init_nonsip_hooks()
  43. {
  44. nonsip_hooks=pkg_malloc(nonsip_max_hooks*
  45. sizeof(struct nonsip_hook));
  46. if (nonsip_hooks==0){
  47. goto error;
  48. }
  49. memset(nonsip_hooks, 0, nonsip_max_hooks*sizeof(struct nonsip_hook));
  50. return 0;
  51. error:
  52. LOG(L_ERR, "nonsip_hooks: memory allocation failure\n");
  53. return -1;
  54. }
  55. void destroy_nonsip_hooks()
  56. {
  57. int r;
  58. if (nonsip_hooks){
  59. for (r=0; r<last_hook_idx; r++){
  60. if (nonsip_hooks[r].destroy)
  61. nonsip_hooks[r].destroy();
  62. }
  63. pkg_free(nonsip_hooks);
  64. nonsip_hooks=0;
  65. }
  66. }
  67. /* allocates a new hook
  68. * returns 0 on success and -1 on error */
  69. int register_nonsip_msg_hook(struct nonsip_hook *h)
  70. {
  71. struct nonsip_hook* tmp;
  72. int new_max_hooks;
  73. if (nonsip_max_hooks==0)
  74. goto error;
  75. if (last_hook_idx >= nonsip_max_hooks){
  76. new_max_hooks=2*nonsip_max_hooks;
  77. tmp=pkg_realloc(nonsip_hooks,
  78. new_max_hooks*sizeof(struct nonsip_hook));
  79. if (tmp==0){
  80. goto error;
  81. }
  82. nonsip_hooks=tmp;
  83. /* init the new chunk */
  84. memset(&nonsip_hooks[last_hook_idx+1], 0,
  85. (new_max_hooks-nonsip_max_hooks-1)*
  86. sizeof(struct nonsip_hook));
  87. nonsip_max_hooks=new_max_hooks;
  88. }
  89. nonsip_hooks[last_hook_idx]=*h;
  90. last_hook_idx++;
  91. return 0;
  92. error:
  93. return -1;
  94. }
  95. int nonsip_msg_run_hooks(struct sip_msg* msg)
  96. {
  97. int r;
  98. int ret;
  99. ret=NONSIP_MSG_DROP; /* default, if no hook installed, drop */
  100. for (r=0; r<last_hook_idx; r++){
  101. ret=nonsip_hooks[r].on_nonsip_req(msg);
  102. if (ret!=NONSIP_MSG_PASS) break;
  103. }
  104. return ret;
  105. }