tls_hooks.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * tls hooks for modules
  20. *
  21. * History:
  22. * --------
  23. * 2007-02-09 created by andrei
  24. * 2010-05-14 new hook interface (better suited for async. tcp) (andrei)
  25. */
  26. /**
  27. * @file
  28. * @brief SIP-router TLS support :: TLS hooks for modules
  29. * @ingroup tls
  30. * Module: @ref tls
  31. */
  32. #ifndef _tls_hooks_h
  33. #define _tls_hooks_h
  34. #ifdef TLS_HOOKS
  35. #ifndef USE_TLS
  36. #error "USE_TLS required and not defined (please compile with make \
  37. TLS_HOOKS=1)"
  38. #endif
  39. #ifdef CORE_TLS
  40. #error "Conflict: CORE_TLS and TLS_HOOKS cannot be defined in the same time"
  41. #endif
  42. #include "tcp_conn.h"
  43. struct tls_hooks{
  44. /* read using tls (should use tcp internal read functions to
  45. get the data from the connection) */
  46. int (*read)(struct tcp_connection* c, int* flags);
  47. /* process data for sending. Should replace pbuf & plen with
  48. an internal buffer containing the tls records. If it was not able
  49. to process the whole pbuf, it should set (rest_buf, rest_len) to
  50. the remaining unprocessed part, else they must be set to 0.
  51. send_flags are passed as a pointer and they can also be changed
  52. (e.g. reset a FORCE_CLOSE flag if there is internal queued data
  53. waiting to be written).
  54. If rest_len or rest_buf are not 0 the call will be repeated after the
  55. contents of pbuf is sent, with (rest_buf, rest_len) as input.
  56. Should return *plen (if >=0).
  57. If it returns < 0 => error (tcp connection will be closed).
  58. */
  59. int (*encode)(struct tcp_connection* c,
  60. const char** pbuf, unsigned int* plen,
  61. const char** rest_buf, unsigned int* rest_len,
  62. snd_flags_t* send_flags);
  63. int (*on_tcpconn_init)(struct tcp_connection *c, int sock);
  64. void (*tcpconn_clean)(struct tcp_connection* c);
  65. void (*tcpconn_close)(struct tcp_connection*c , int fd);
  66. /* per listening socket init, called on ser startup (after modules,
  67. * process table, init() and udp socket initialization)*/
  68. int (*init_si)(struct socket_info* si);
  69. /* generic init function (called at ser init, after module initialization
  70. * and process table creation)*/
  71. int (*init)(void);
  72. /* destroy function, called after the modules are destroyed, and
  73. * after destroy_tcp() */
  74. void (*destroy)(void);
  75. };
  76. extern struct tls_hooks tls_hook;
  77. #ifdef __SUNPRO_C
  78. #define tls_hook_call(name, ret_not_set, ...) \
  79. ((tls_hook.name)?(tls_hook.name(__VA_ARGS__)): (ret_not_set))
  80. #define tls_hook_call_v(name, __VA_ARGS__) \
  81. do{ \
  82. if (tls_hook.name) tls_hook.name(__VA_ARGS__); \
  83. }while(0)
  84. #else
  85. #define tls_hook_call(name, ret_not_set, args...) \
  86. ((tls_hook.name)?(tls_hook.name(args)): (ret_not_set))
  87. #define tls_hook_call_v(name, args...) \
  88. do{ \
  89. if (tls_hook.name) tls_hook.name(args); \
  90. }while(0)
  91. #endif
  92. /* hooks */
  93. #define tls_tcpconn_init(c, s) tls_hook_call(on_tcpconn_init, 0, (c), (s))
  94. #define tls_tcpconn_clean(c) tls_hook_call_v(tcpconn_clean, (c))
  95. #define tls_encode(c, pbuf, plen, rbuf, rlen, sflags) \
  96. tls_hook_call(encode, -1, (c), (pbuf), (plen), (rbuf), (rlen), (sflags))
  97. #define tls_close(conn, fd) tls_hook_call_v(tcpconn_close, (conn), (fd))
  98. #define tls_read(c, flags) tls_hook_call(read, -1, (c), (flags))
  99. int register_tls_hooks(struct tls_hooks* h);
  100. #endif /* TLS_HOOKS */
  101. #endif