tls_hooks.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. */
  25. #ifndef _tls_hooks_h
  26. #define _tls_hooks_h
  27. #ifdef TLS_HOOKS
  28. #ifndef USE_TLS
  29. #error "USE_TLS required and not defined (please compile with make \
  30. TLS_HOOKS=1)"
  31. #endif
  32. #ifdef CORE_TLS
  33. #error "Conflict: CORE_TLS and TLS_HOOKS cannot be defined in the same time"
  34. #endif
  35. #include "tcp_conn.h"
  36. struct tls_hooks{
  37. int (*read)(struct tcp_connection* c);
  38. int (*blocking_write)(struct tcp_connection* c, int fd, const char* buf,
  39. unsigned int len);
  40. int (*on_tcpconn_init)(struct tcp_connection *c, int sock);
  41. void (*tcpconn_clean)(struct tcp_connection* c);
  42. void (*tcpconn_close)(struct tcp_connection*c , int fd);
  43. /* checks if a tls connection is fully established before a read, and if
  44. * not it runs tls_accept() or tls_connect() as needed
  45. * (tls_accept and tls_connect are deferred to the "reader" process for
  46. * performance reasons) */
  47. int (*fix_read_con)(struct tcp_connection* c);
  48. /* per listening socket init, called on ser startup (after modules,
  49. * process table, init() and udp socket initialization)*/
  50. int (*init_si)(struct socket_info* si);
  51. /* generic init function (called at ser init, after module initialization
  52. * and process table creation)*/
  53. int (*init)();
  54. /* destroy function, called after the modules are destroyed, and
  55. * after destroy_tcp() */
  56. void (*destroy)();
  57. };
  58. extern struct tls_hooks tls_hook;
  59. #ifdef __SUNPRO_C
  60. #define tls_hook_call(name, ret_not_set, ...) \
  61. ((tls_hook.name)?(tls_hook.name(__VA_ARGS__)): (ret_not_set))
  62. #define tls_hook_call_v(name, __VA_ARGS__) \
  63. do{ \
  64. if (tls_hook.name) tls_hook.name(__VA_ARGS__); \
  65. }while(0)
  66. #else
  67. #define tls_hook_call(name, ret_not_set, args...) \
  68. ((tls_hook.name)?(tls_hook.name(args)): (ret_not_set))
  69. #define tls_hook_call_v(name, args...) \
  70. do{ \
  71. if (tls_hook.name) tls_hook.name(args); \
  72. }while(0)
  73. #endif
  74. /* hooks */
  75. #define tls_tcpconn_init(c, s) tls_hook_call(on_tcpconn_init, 0, (c), (s))
  76. #define tls_tcpconn_clean(c) tls_hook_call_v(tcpconn_clean, (c))
  77. #define tls_blocking_write(c, fd, buf, len) \
  78. tls_hook_call(blocking_write, -1, (c), (fd), (buf), (len))
  79. #define tls_close(conn, fd) tls_hook_call_v(tcpconn_close, (conn), (fd))
  80. #define tls_read(c) tls_hook_call(read, -1, (c))
  81. #define tls_fix_read_conn(c) tls_hook_call(fix_read_con, -1, (c))
  82. int register_tls_hooks(struct tls_hooks* h);
  83. #endif /* TLS_HOOKS */
  84. #endif