dst_blacklist.h
dcb59e67
 /*
  * Copyright (C) 2006 iptelorg GmbH
  *
02ca141b
  * This file is part of Kamailio, a free SIP server.
dcb59e67
  *
02ca141b
  * Kamailio is free software; you can redistribute it and/or modify
dcb59e67
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
02ca141b
  * Kamailio is distributed in the hope that it will be useful,
dcb59e67
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
5744b6f8
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
dcb59e67
  */
1ff47a5c
 
6806e46a
 /** Kamailio core :: Destination blacklists.
1ff47a5c
  * @file
02ca141b
  * @author andrei, Gergo
1ff47a5c
  * @ingroup core
  * Module: @ref core
  */
 
dcb59e67
 #ifndef dst_black_list_h
 #define dst_black_list_h
 
 #include "ip_addr.h"
7a64325c
 #include "parser/msg_parser.h"
5bd736c7
 #include "timer_ticks.h"
a4d17f82
 #include "cfg_core.h"
5bd736c7
 
1ff47a5c
 #define DEFAULT_BLST_TIMEOUT		60  /**< 1 min. */
 #define DEFAULT_BLST_MAX_MEM		250 /**< 250 KB */
 
 /** @name flags: */
 /*@{ */
dcb59e67
 
1ff47a5c
 #define BLST_IS_IPV6		1		/**< set if the address is ipv6 */
 #define BLST_ERR_SEND		(1<<1)	/**< set if  send is denied/failed */
 #define BLST_ERR_CONNECT	(1<<2)	/**< set if connect failed (tcp/tls) */
 #define BLST_ICMP_RCVD		(1<<3)	/**< set if icmp error */
 #define BLST_ERR_TIMEOUT	(1<<4)	/**< set if sip timeout */
 #define BLST_503			(1<<5)	/**< set for 503 replies */
 #define BLST_ADM_PROHIBITED	(1<<6)	/**< administratively prohibited */
 #define BLST_PERMANENT		(1<<7)  /**< never deleted, never expires */
 /*@} */
dcb59e67
 
2b813af1
 /* uncomment the define above to enable blacklist callbacks support */
304eb781
 /*#define DST_BLACKLIST_HOOKS*/
 
1ff47a5c
 #define DST_BLACKLIST_CONTINUE 0 /**< add: do nothing/ignore, search: ignore */
 #define DST_BLACKLIST_ACCEPT 1   /**< add: force accept, search: force match */
 #define DST_BLACKLIST_DENY  -1   /**< add: deny, search: force no match */
2b813af1
 
 #define DST_BLACKLIST_ADD_CB 1
 #define DST_BLACKLIST_SEARCH_CB 2
304eb781
 
0f9e9908
 
 extern unsigned blst_proto_imask[PROTO_LAST+1];
 
304eb781
 #ifdef DST_BLACKLIST_HOOKS
 struct blacklist_hook{
7a64325c
 	/* WARNING: msg might be NULL, and it might point to shared memory
 	 * without locking, do not modify it! msg can be used typically for checking
 	 * the message flags with isflagset() */
5bd736c7
 	int (*on_blst_action)(struct dest_info* si, unsigned char* err_flags,
 							struct sip_msg* msg);
304eb781
 	/* called before ser shutdown */
 	void (*destroy)(void);
 };
 
2b813af1
 int register_blacklist_hook(struct blacklist_hook *h, int type);
304eb781
 #endif /* DST_BLACKLIST_HOOKS */
 
abb01fb4
 int init_dst_blacklist(void);
5744b6f8
 #ifdef USE_DST_BLACKLIST_STATS
 int init_dst_blacklist_stats(int iproc_num);
 #define DST_BLACKLIST_ALL_STATS "bkl_all_stats"
 #endif
abb01fb4
 void destroy_dst_blacklist(void);
dcb59e67
 
5bd736c7
 
5d6752dc
 /** force add to the blacklist.
7af69774
  * like function dst_blacklist_add_to, but no ignore mask or
5d6752dc
  * blacklist enabled checks are made.
  * @see dst_blacklist_add_to for the parameters and return value.
  */
 int dst_blacklist_force_add_to(unsigned char err_flags, struct dest_info* si,
 								struct sip_msg* msg, ticks_t timeout);
5bd736c7
 
5d6752dc
 /** force add to the blacklist, long version.
7af69774
  * like function dst_blacklist_su_to, but no ignore mask or
5d6752dc
  * blacklist enabled checks are made.
  * @see dst_blacklist_su_to for the parameters and return value.
6d91574d
  */
5d6752dc
 int dst_blacklist_force_su_to(	unsigned char err_flags,
 								unsigned char proto,
 								union sockaddr_union* dst,
 								struct sip_msg* msg,
 								ticks_t timeout);
 
 
 /** checks if blacklist should be used.
   * @param  err_flags - blacklist reason
0f9e9908
   * @param si - filled dest_info structure pointer.
5d6752dc
   * @return 1 if blacklist is enabled (core_cfg) and the event/error
   *           is not in the ignore list.
   *         0 otherwise
   */
 #define should_blacklist(err_flags, si) \
 	(cfg_get(core, core_cfg, use_dst_blacklist) && \
0f9e9908
 		((err_flags) & ~blst_proto_imask[(unsigned)((si)->proto)] & \
 		 			   ~(si)->send_flags.blst_imask ))
5d6752dc
 
 
 /** checks if blacklist should be used, long version.
7af69774
   * @param err_flags - blacklist reason
5d6752dc
   * @param snd_flags - snd_flags pointer, can be 0.
   * @param proto - protocol, can be 0 (PROTO_NONE).
7af69774
   * @param su  - sockaddr_union pointer, can be 0.
5d6752dc
   * @return 1 if blacklist is enabled (core_cfg) and the event/error
7af69774
   *           is not in the ignore list. 0 otherwise
5d6752dc
   */
 #define should_blacklist_su(err_flags, snd_flags, proto, su) \
 	(cfg_get(core, core_cfg, use_dst_blacklist) && \
0f9e9908
 		((err_flags) & ~blst_proto_imask[(unsigned)(proto)] & \
5d6752dc
 		 			~((snd_flags)?((snd_flags_t*)(snd_flags))->blst_imask:0)))
 
 
 /** adds a dst to the blacklist.
  *
  * @param  err_flags - blacklist reason
  * @param si  - dest_info structure (dst).
  * @param msg - sip msg struct. pointer if known, 0 otherwise.
  * @param timeout - timeout in ticks.
  * @return >=0 on success, -1 on error.
  */
 #define dst_blacklist_add_to(err_flags, si, msg, timeout) \
 	(should_blacklist(err_flags, si)? \
 		dst_blacklist_force_add_to((err_flags), (si), (msg), (timeout))\
 		: 0)
 
 
 /** adds a dst to the blacklist, long version.
  * Similar to dst_blacklist_add_to, but uses "unpacked" parameters.
  * @param  err_flags - blacklist reason
  * @param proto - protocol.
  * @param dst  - sockaddr_union pointer.
  * @param snd_flags - snd_flags pointer, can be 0.
  * @param msg - sip msg struct. pointer if known, 0 otherwise.
  * @param timeout - timeout in ticks.
  * @return >=0 on success, -1 on error.
  */
 #define dst_blacklist_su_to(err_flags, proto, dst, snd_flags, msg, timeout) \
 	(should_blacklist_su(err_flags, snd_flags, proto, dst) ? \
 		dst_blacklist_force_su_to((err_flags), (proto), (dst), (msg), \
 									(timeout))\
 		: 0)
 
dcb59e67
 
6d91574d
 /** adds a dst to the blacklist with default timeout.
5d6752dc
  *
  * @param  err_flags - blacklist reason
  * @param si  - dest_info structure (dst).
  * @param msg - sip msg struct. pointer if known, 0 otherwise.
  * @return >=0 on success, -1 on error.
  * @see dst_blacklist_add_to.
6d91574d
  */
5d6752dc
 #define dst_blacklist_add(err_flags, si, msg) \
 	dst_blacklist_add_to(err_flags, si, msg, \
 							S_TO_TICKS(cfg_get(core, core_cfg, blst_timeout)))
 
 
 /** adds a dst to the blacklist with default timeout, long version.
  * Similar to dst_blacklist_add_to, but uses "unpacked" parameters.
  * @param  err_flags - blacklist reason
  * @param proto - protocol.
  * @param dst  - sockaddr_union pointer.
  * @param snd_flags - snd_flags pointer, can be 0.
  * @param msg - sip msg struct. pointer if known, 0 otherwise.
  * @return >=0 on success, -1 on error.
  * @see dst_blacklist_su_to.
6d91574d
  */
5d6752dc
 #define dst_blacklist_su(err_flags, proto, dst, snd_flags, msg) \
 	dst_blacklist_su_to(err_flags, proto, dst, snd_flags, msg, \
 							S_TO_TICKS(cfg_get(core, core_cfg, blst_timeout)))
6d91574d
 
7a64325c
 int dst_is_blacklisted(struct dest_info* si, struct sip_msg* msg);
1ff47a5c
 
04f55d6c
 /** delete an entry from the blacklist. */
5bd736c7
 int dst_blacklist_del(struct dest_info* si, struct sip_msg* msg);
dfcbd08b
 
04f55d6c
 /** deletes all the entries from the blacklist except the permanent ones.
dfcbd08b
  * (which are marked with BLST_PERMANENT)
  */
 void dst_blst_flush(void);
 
33bfeb9d
 int use_dst_blacklist_fixup(void *handle, str *gname, str *name, void **val);
1ff47a5c
 
04f55d6c
 /** KByte to Byte conversion. */
33bfeb9d
 int blst_max_mem_fixup(void *handle, str *gname, str *name, void **val);
a4d17f82
 
0f9e9908
 void blst_reinit_ign_masks(str* gname, str* name);
 
dcb59e67
 #endif