dst_blacklist.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * $Id$
  3. *
  4. * resolver related functions
  5. *
  6. * Copyright (C) 2006 iptelorg GmbH
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2006-07-29 created by andrei
  32. * 2007-07-30 dst blacklist measurements added (Gergo)
  33. * 2009-12-22 blacklist ignore mask support and dst_blacklist_{add,su}
  34. * switched to macros (andrei)
  35. */
  36. #ifndef dst_black_list_h
  37. #define dst_black_list_h
  38. #include "ip_addr.h"
  39. #include "parser/msg_parser.h"
  40. #include "timer_ticks.h"
  41. #include "cfg_core.h"
  42. #define DEFAULT_BLST_TIMEOUT 60 /* 1 min. */
  43. #define DEFAULT_BLST_MAX_MEM 250 /* 250 KB */
  44. /* flags: */
  45. #define BLST_IS_IPV6 1 /* set if the address is ipv6 */
  46. #define BLST_ERR_SEND (1<<1) /* set if send is denied/failed */
  47. #define BLST_ERR_CONNECT (1<<2) /* set if connect failed (tcp/tls) */
  48. #define BLST_ICMP_RCVD (1<<3) /* set if icmp error */
  49. #define BLST_ERR_TIMEOUT (1<<4) /* set if sip timeout */
  50. #define BLST_503 (1<<5) /* set for 503 replies */
  51. #define BLST_ADM_PROHIBITED (1<<6) /* administratively prohibited */
  52. #define BLST_PERMANENT (1<<7) /* never deleted, never expires */
  53. /* uncomment the define above to enable blacklist callbacks support */
  54. /*#define DST_BLACKLIST_HOOKS*/
  55. #define DST_BLACKLIST_CONTINUE 0 /* add: do nothing/ignore, search: ignore */
  56. #define DST_BLACKLIST_ACCEPT 1 /* add: force accept, search: force match */
  57. #define DST_BLACKLIST_DENY -1 /* add: deny, search: force no match */
  58. #define DST_BLACKLIST_ADD_CB 1
  59. #define DST_BLACKLIST_SEARCH_CB 2
  60. #ifdef DST_BLACKLIST_HOOKS
  61. struct blacklist_hook{
  62. /* WARNING: msg might be NULL, and it might point to shared memory
  63. * without locking, do not modify it! msg can be used typically for checking
  64. * the message flags with isflagset() */
  65. int (*on_blst_action)(struct dest_info* si, unsigned char* err_flags,
  66. struct sip_msg* msg);
  67. /* called before ser shutdown */
  68. void (*destroy)(void);
  69. };
  70. int register_blacklist_hook(struct blacklist_hook *h, int type);
  71. #endif /* DST_BLACKLIST_HOOKS */
  72. int init_dst_blacklist();
  73. #ifdef USE_DST_BLACKLIST_STATS
  74. int init_dst_blacklist_stats(int iproc_num);
  75. #define DST_BLACKLIST_ALL_STATS "bkl_all_stats"
  76. #endif
  77. void destroy_dst_blacklist();
  78. /** force add to the blacklist.
  79. * like @function dst_blacklist_add_to, but no ignore mask or
  80. * blacklist enabled checks are made.
  81. * @see dst_blacklist_add_to for the parameters and return value.
  82. */
  83. int dst_blacklist_force_add_to(unsigned char err_flags, struct dest_info* si,
  84. struct sip_msg* msg, ticks_t timeout);
  85. /** force add to the blacklist, long version.
  86. * like @function dst_blacklist_su_to, but no ignore mask or
  87. * blacklist enabled checks are made.
  88. * @see dst_blacklist_su_to for the parameters and return value.
  89. */
  90. int dst_blacklist_force_su_to( unsigned char err_flags,
  91. unsigned char proto,
  92. union sockaddr_union* dst,
  93. struct sip_msg* msg,
  94. ticks_t timeout);
  95. /** checks if blacklist should be used.
  96. * @param err_flags - blacklist reason
  97. * @param si - filled dst_info structure pointer.
  98. * @return 1 if blacklist is enabled (core_cfg) and the event/error
  99. * is not in the ignore list.
  100. * 0 otherwise
  101. */
  102. #define should_blacklist(err_flags, si) \
  103. (cfg_get(core, core_cfg, use_dst_blacklist) && \
  104. ((err_flags) & (si)->send_flags.blst_imask))
  105. /** checks if blacklist should be used, long version.
  106. * @param err_flags - blacklist reason
  107. * @param snd_flags - snd_flags pointer, can be 0.
  108. * @param proto - protocol, can be 0 (PROTO_NONE).
  109. * @param si - sockaddr_union pointer, can be 0.
  110. * @return 1 if blacklist is enabled (core_cfg) and the event/error
  111. * is not in the ignore list.
  112. * 0 otherwise
  113. */
  114. #define should_blacklist_su(err_flags, snd_flags, proto, su) \
  115. (cfg_get(core, core_cfg, use_dst_blacklist) && \
  116. ((err_flags) & \
  117. ~((snd_flags)?((snd_flags_t*)(snd_flags))->blst_imask:0)))
  118. /** adds a dst to the blacklist.
  119. *
  120. * @param err_flags - blacklist reason
  121. * @param si - dest_info structure (dst).
  122. * @param msg - sip msg struct. pointer if known, 0 otherwise.
  123. * @param timeout - timeout in ticks.
  124. * @return >=0 on success, -1 on error.
  125. */
  126. #define dst_blacklist_add_to(err_flags, si, msg, timeout) \
  127. (should_blacklist(err_flags, si)? \
  128. dst_blacklist_force_add_to((err_flags), (si), (msg), (timeout))\
  129. : 0)
  130. /** adds a dst to the blacklist, long version.
  131. * Similar to dst_blacklist_add_to, but uses "unpacked" parameters.
  132. * @param err_flags - blacklist reason
  133. * @param proto - protocol.
  134. * @param dst - sockaddr_union pointer.
  135. * @param snd_flags - snd_flags pointer, can be 0.
  136. * @param msg - sip msg struct. pointer if known, 0 otherwise.
  137. * @param timeout - timeout in ticks.
  138. * @return >=0 on success, -1 on error.
  139. */
  140. #define dst_blacklist_su_to(err_flags, proto, dst, snd_flags, msg, timeout) \
  141. (should_blacklist_su(err_flags, snd_flags, proto, dst) ? \
  142. dst_blacklist_force_su_to((err_flags), (proto), (dst), (msg), \
  143. (timeout))\
  144. : 0)
  145. /** adds a dst to the blacklist with default timeout.
  146. *
  147. * @param err_flags - blacklist reason
  148. * @param si - dest_info structure (dst).
  149. * @param msg - sip msg struct. pointer if known, 0 otherwise.
  150. * @return >=0 on success, -1 on error.
  151. * @see dst_blacklist_add_to.
  152. */
  153. #define dst_blacklist_add(err_flags, si, msg) \
  154. dst_blacklist_add_to(err_flags, si, msg, \
  155. S_TO_TICKS(cfg_get(core, core_cfg, blst_timeout)))
  156. /** adds a dst to the blacklist with default timeout, long version.
  157. * Similar to dst_blacklist_add_to, but uses "unpacked" parameters.
  158. * @param err_flags - blacklist reason
  159. * @param proto - protocol.
  160. * @param dst - sockaddr_union pointer.
  161. * @param snd_flags - snd_flags pointer, can be 0.
  162. * @param msg - sip msg struct. pointer if known, 0 otherwise.
  163. * @return >=0 on success, -1 on error.
  164. * @see dst_blacklist_su_to.
  165. */
  166. #define dst_blacklist_su(err_flags, proto, dst, snd_flags, msg) \
  167. dst_blacklist_su_to(err_flags, proto, dst, snd_flags, msg, \
  168. S_TO_TICKS(cfg_get(core, core_cfg, blst_timeout)))
  169. int dst_is_blacklisted(struct dest_info* si, struct sip_msg* msg);
  170. /* delete an entry from the blacklist */
  171. int dst_blacklist_del(struct dest_info* si, struct sip_msg* msg);
  172. /* deletes all the entries from the blacklist except the permanent ones
  173. * (which are marked with BLST_PERMANENT)
  174. */
  175. void dst_blst_flush(void);
  176. int use_dst_blacklist_fixup(void *handle, str *gname, str *name, void **val);
  177. /* KByte to Byte conversion */
  178. int blst_max_mem_fixup(void *handle, str *gname, str *name, void **val);
  179. #endif