tls_hooks.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * returns 1 if the read can continue, 0 if the connection is not yet
  48. * ready for the read and fix_read_con() should be attempted at a latter
  49. * time and <0 on error.
  50. */
  51. int (*fix_read_con)(struct tcp_connection* c);
  52. /* per listening socket init, called on ser startup (after modules,
  53. * process table, init() and udp socket initialization)*/
  54. int (*init_si)(struct socket_info* si);
  55. /* generic init function (called at ser init, after module initialization
  56. * and process table creation)*/
  57. int (*init)();
  58. /* destroy function, called after the modules are destroyed, and
  59. * after destroy_tcp() */
  60. void (*destroy)();
  61. };
  62. extern struct tls_hooks tls_hook;
  63. #ifdef __SUNPRO_C
  64. #define tls_hook_call(name, ret_not_set, ...) \
  65. ((tls_hook.name)?(tls_hook.name(__VA_ARGS__)): (ret_not_set))
  66. #define tls_hook_call_v(name, __VA_ARGS__) \
  67. do{ \
  68. if (tls_hook.name) tls_hook.name(__VA_ARGS__); \
  69. }while(0)
  70. #else
  71. #define tls_hook_call(name, ret_not_set, args...) \
  72. ((tls_hook.name)?(tls_hook.name(args)): (ret_not_set))
  73. #define tls_hook_call_v(name, args...) \
  74. do{ \
  75. if (tls_hook.name) tls_hook.name(args); \
  76. }while(0)
  77. #endif
  78. /* hooks */
  79. #define tls_tcpconn_init(c, s) tls_hook_call(on_tcpconn_init, 0, (c), (s))
  80. #define tls_tcpconn_clean(c) tls_hook_call_v(tcpconn_clean, (c))
  81. #define tls_blocking_write(c, fd, buf, len) \
  82. tls_hook_call(blocking_write, -1, (c), (fd), (buf), (len))
  83. #define tls_close(conn, fd) tls_hook_call_v(tcpconn_close, (conn), (fd))
  84. #define tls_read(c) tls_hook_call(read, -1, (c))
  85. #define tls_fix_read_conn(c) tls_hook_call(fix_read_con, -1, (c))
  86. int register_tls_hooks(struct tls_hooks* h);
  87. #endif /* TLS_HOOKS */
  88. #endif