compat_libevent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2009-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_COMPAT_LIBEVENT_H
  4. #define TOR_COMPAT_LIBEVENT_H
  5. #include "orconfig.h"
  6. #include "testsupport.h"
  7. struct event;
  8. struct event_base;
  9. #ifdef USE_BUFFEREVENTS
  10. struct bufferevent;
  11. struct ev_token_bucket_cfg;
  12. struct bufferevent_rate_limit_group;
  13. #endif
  14. #ifdef HAVE_EVENT2_EVENT_H
  15. #include <event2/util.h>
  16. #elif !defined(EVUTIL_SOCKET_DEFINED)
  17. #define EVUTIL_SOCKET_DEFINED
  18. #define evutil_socket_t int
  19. #endif
  20. void configure_libevent_logging(void);
  21. void suppress_libevent_log_msg(const char *msg);
  22. #ifdef HAVE_EVENT2_EVENT_H
  23. #define tor_event_new event_new
  24. #define tor_evtimer_new evtimer_new
  25. #define tor_evsignal_new evsignal_new
  26. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  27. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  28. (sock),(tcp),(cb),(data));
  29. #else
  30. struct event *tor_event_new(struct event_base * base, evutil_socket_t sock,
  31. short what, void (*cb)(evutil_socket_t, short, void *), void *arg);
  32. struct event *tor_evtimer_new(struct event_base * base,
  33. void (*cb)(evutil_socket_t, short, void *), void *arg);
  34. struct event *tor_evsignal_new(struct event_base * base, int sig,
  35. void (*cb)(evutil_socket_t, short, void *), void *arg);
  36. #define tor_evdns_add_server_port evdns_add_server_port
  37. #endif
  38. void tor_event_free(struct event *ev);
  39. typedef struct periodic_timer_t periodic_timer_t;
  40. periodic_timer_t *periodic_timer_new(struct event_base *base,
  41. const struct timeval *tv,
  42. void (*cb)(periodic_timer_t *timer, void *data),
  43. void *data);
  44. void periodic_timer_free(periodic_timer_t *);
  45. #define tor_event_base_loopexit event_base_loopexit
  46. /** Defines a configuration for using libevent with Tor: passed as an argument
  47. * to tor_libevent_initialize() to describe how we want to set up. */
  48. typedef struct tor_libevent_cfg {
  49. /** Flag: if true, disable IOCP (assuming that it could be enabled). */
  50. int disable_iocp;
  51. /** How many CPUs should we use (relevant only with IOCP). */
  52. int num_cpus;
  53. /** How many milliseconds should we allow between updating bandwidth limits?
  54. * (relevant only with bufferevents). */
  55. int msec_per_tick;
  56. } tor_libevent_cfg;
  57. void tor_libevent_initialize(tor_libevent_cfg *cfg);
  58. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  59. const char *tor_libevent_get_method(void);
  60. void tor_check_libevent_header_compatibility(void);
  61. const char *tor_libevent_get_version_str(void);
  62. const char *tor_libevent_get_header_version_str(void);
  63. #ifdef USE_BUFFEREVENTS
  64. const struct timeval *tor_libevent_get_one_tick_timeout(void);
  65. int tor_libevent_using_iocp_bufferevents(void);
  66. int tor_set_bufferevent_rate_limit(struct bufferevent *bev,
  67. struct ev_token_bucket_cfg *cfg);
  68. int tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
  69. struct bufferevent_rate_limit_group *g);
  70. #endif
  71. int tor_init_libevent_rng(void);
  72. void tor_gettimeofday_cached(struct timeval *tv);
  73. void tor_gettimeofday_cache_clear(void);
  74. #ifdef TOR_UNIT_TESTS
  75. void tor_gettimeofday_cache_set(const struct timeval *tv);
  76. #endif
  77. void tor_gettimeofday_cached_monotonic(struct timeval *tv);
  78. #ifdef COMPAT_LIBEVENT_PRIVATE
  79. /** A number representing a version of Libevent.
  80. This is a 4-byte number, with the first three bytes representing the
  81. major, minor, and patchlevel respectively of the library. The fourth
  82. byte is unused.
  83. This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
  84. 2.0.1 or later. For versions of Libevent before 1.4.0, which followed the
  85. format of "1.0, 1.0a, 1.0b", we define 1.0 to be equivalent to 1.0.0, 1.0a
  86. to be equivalent to 1.0.1, and so on.
  87. */
  88. typedef uint32_t le_version_t;
  89. /** @{ */
  90. /** Macros: returns the number of a libevent version as a le_version_t */
  91. #define V(major, minor, patch) \
  92. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  93. #define V_OLD(major, minor, patch) \
  94. V((major), (minor), (patch)-'a'+1)
  95. /** @} */
  96. /** Represetns a version of libevent so old we can't figure out what version
  97. * it is. */
  98. #define LE_OLD V(0,0,0)
  99. /** Represents a version of libevent so weird we can't figure out what version
  100. * it is. */
  101. #define LE_OTHER V(0,0,99)
  102. STATIC void
  103. libevent_logging_callback(int severity, const char *msg);
  104. STATIC le_version_t
  105. tor_decode_libevent_version(const char *v);
  106. STATIC int
  107. le_versions_compatibility(le_version_t v);
  108. #endif
  109. #endif