tls_hooks.h
6c53d41a
 /*
  * Copyright (C) 2007 iptelorg GmbH 
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
  *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
7fa7e9bb
 /**
  * @file
6806e46a
  * @brief Kamailio TLS support :: TLS hooks for modules
7fa7e9bb
  * @ingroup tls
  * Module: @ref tls
  */
 
 
6c53d41a
 #ifndef _tls_hooks_h
 #define _tls_hooks_h
 
 #ifdef TLS_HOOKS
 
 #ifndef USE_TLS
 #error "USE_TLS required and not defined (please compile with make \
 	TLS_HOOKS=1)"
 #endif
 
 #ifdef CORE_TLS
 #error "Conflict: CORE_TLS and TLS_HOOKS cannot be defined in the same time"
 #endif
 
 #include "tcp_conn.h"
 
 
 
 struct tls_hooks{
5f653089
 	/* read using tls (should use tcp internal read functions to
 	   get the data from the connection) */
ce51fbb8
 	int  (*read)(struct tcp_connection* c, int* flags);
5f653089
 	/* process data for sending. Should replace pbuf & plen with
e3163126
 	   an internal buffer containing the tls records. If it was not able
 	   to process the whole pbuf, it should set (rest_buf, rest_len) to
 	   the remaining unprocessed part, else they must be set to 0.
 	   send_flags are passed as a pointer and they can also be changed
 	   (e.g. reset a FORCE_CLOSE flag if there is internal queued data
 	    waiting to be written).
 	   If rest_len or rest_buf are not 0 the call will be repeated after the
 	   contents of pbuf is sent, with (rest_buf, rest_len) as input.
5f653089
 	   Should return *plen (if >=0).
 	   If it returns < 0 => error (tcp connection will be closed).
 	*/
e3163126
 	int (*encode)(struct tcp_connection* c,
 					const char** pbuf, unsigned int* plen,
 					const char** rest_buf, unsigned int* rest_len,
 					snd_flags_t* send_flags);
6c53d41a
 	int  (*on_tcpconn_init)(struct tcp_connection *c, int sock);
 	void (*tcpconn_clean)(struct tcp_connection* c);
 	void (*tcpconn_close)(struct tcp_connection*c , int fd);
 	
4700831f
 	/* per listening socket init, called on kamailio startup (after modules,
6c53d41a
 	 *  process table, init() and udp socket initialization)*/
 	int (*init_si)(struct socket_info* si);
4700831f
 	/* generic init function (called at kamailio init, after module initialization
6c53d41a
 	 *  and process table creation)*/
abb01fb4
 	int (*init)(void);
6c53d41a
 	/* destroy function, called after the modules are destroyed, and 
 	 * after  destroy_tcp() */
abb01fb4
 	void (*destroy)(void);
4700831f
 	/* generic pre-init function (called at kamailio start, before module
 	 * initialization (after modparams) */
 	int (*pre_init)(void);
6c53d41a
 };
 
 
ee7f9cb4
 extern struct tls_hooks tls_hook;
6c53d41a
 
 #ifdef __SUNPRO_C
 	#define tls_hook_call(name, ret_not_set, ...) \
 		((tls_hook.name)?(tls_hook.name(__VA_ARGS__)): (ret_not_set))
 	#define tls_hook_call_v(name, __VA_ARGS__) \
 		do{ \
 			if (tls_hook.name) tls_hook.name(__VA_ARGS__); \
 		}while(0)
 #else
 	#define tls_hook_call(name, ret_not_set, args...) \
 		((tls_hook.name)?(tls_hook.name(args)): (ret_not_set))
 	#define tls_hook_call_v(name, args...) \
 		do{ \
 			if (tls_hook.name) tls_hook.name(args); \
 		}while(0)
 #endif
 
 /* hooks */
 
 #define tls_tcpconn_init(c, s)	tls_hook_call(on_tcpconn_init, 0, (c), (s))
 #define tls_tcpconn_clean(c)	tls_hook_call_v(tcpconn_clean, (c))
e3163126
 #define tls_encode(c, pbuf, plen, rbuf, rlen, sflags) \
 	tls_hook_call(encode, -1, (c), (pbuf), (plen), (rbuf), (rlen), (sflags))
6c53d41a
 #define tls_close(conn, fd)		tls_hook_call_v(tcpconn_close, (conn), (fd))
ce51fbb8
 #define tls_read(c, flags)				tls_hook_call(read, -1, (c), (flags))
6c53d41a
 
 int register_tls_hooks(struct tls_hooks* h);
 
 #endif /* TLS_HOOKS */
 #endif