compat_libevent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* Copyright (c) 2009-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file compat_libevent.c
  5. * \brief Wrappers to handle porting between different versions of libevent.
  6. *
  7. * In an ideal world, we'd just use Libevent 2.0 from now on. But as of June
  8. * 2012, Libevent 1.4 is still all over, and some poor souls are stuck on
  9. * Libevent 1.3e. */
  10. #include "orconfig.h"
  11. #include "compat.h"
  12. #include "compat_libevent.h"
  13. #include "util.h"
  14. #include "torlog.h"
  15. #ifdef HAVE_EVENT2_EVENT_H
  16. #include <event2/event.h>
  17. #include <event2/thread.h>
  18. #ifdef USE_BUFFEREVENTS
  19. #include <event2/bufferevent.h>
  20. #endif
  21. #else
  22. #include <event.h>
  23. #endif
  24. /** A number representing a version of Libevent.
  25. This is a 4-byte number, with the first three bytes representing the
  26. major, minor, and patchlevel respectively of the library. The fourth
  27. byte is unused.
  28. This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
  29. 2.0.1 or later. For versions of Libevent before 1.4.0, which followed the
  30. format of "1.0, 1.0a, 1.0b", we define 1.0 to be equivalent to 1.0.0, 1.0a
  31. to be equivalent to 1.0.1, and so on.
  32. */
  33. typedef uint32_t le_version_t;
  34. /** @{ */
  35. /** Macros: returns the number of a libevent version as a le_version_t */
  36. #define V(major, minor, patch) \
  37. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  38. #define V_OLD(major, minor, patch) \
  39. V((major), (minor), (patch)-'a'+1)
  40. /** @} */
  41. /** Represetns a version of libevent so old we can't figure out what version
  42. * it is. */
  43. #define LE_OLD V(0,0,0)
  44. /** Represents a version of libevent so weird we can't figure out what version
  45. * it is. */
  46. #define LE_OTHER V(0,0,99)
  47. #if 0
  48. static le_version_t tor_get_libevent_version(const char **v_out);
  49. #endif
  50. #if defined(HAVE_EVENT_SET_LOG_CALLBACK) || defined(RUNNING_DOXYGEN)
  51. /** A string which, if it appears in a libevent log, should be ignored. */
  52. static const char *suppress_msg = NULL;
  53. /** Callback function passed to event_set_log() so we can intercept
  54. * log messages from libevent. */
  55. static void
  56. libevent_logging_callback(int severity, const char *msg)
  57. {
  58. char buf[1024];
  59. size_t n;
  60. if (suppress_msg && strstr(msg, suppress_msg))
  61. return;
  62. n = strlcpy(buf, msg, sizeof(buf));
  63. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  64. buf[n-1] = '\0';
  65. }
  66. switch (severity) {
  67. case _EVENT_LOG_DEBUG:
  68. log_debug(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  69. break;
  70. case _EVENT_LOG_MSG:
  71. log_info(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  72. break;
  73. case _EVENT_LOG_WARN:
  74. log_warn(LD_NOCB|LD_GENERAL, "Warning from libevent: %s", buf);
  75. break;
  76. case _EVENT_LOG_ERR:
  77. log_err(LD_NOCB|LD_GENERAL, "Error from libevent: %s", buf);
  78. break;
  79. default:
  80. log_warn(LD_NOCB|LD_GENERAL, "Message [%d] from libevent: %s",
  81. severity, buf);
  82. break;
  83. }
  84. }
  85. /** Set hook to intercept log messages from libevent. */
  86. void
  87. configure_libevent_logging(void)
  88. {
  89. event_set_log_callback(libevent_logging_callback);
  90. }
  91. /** Ignore any libevent log message that contains <b>msg</b>. */
  92. void
  93. suppress_libevent_log_msg(const char *msg)
  94. {
  95. suppress_msg = msg;
  96. }
  97. #else
  98. void
  99. configure_libevent_logging(void)
  100. {
  101. }
  102. void
  103. suppress_libevent_log_msg(const char *msg)
  104. {
  105. (void)msg;
  106. }
  107. #endif
  108. #ifndef HAVE_EVENT2_EVENT_H
  109. /** Work-alike replacement for event_new() on pre-Libevent-2.0 systems. */
  110. struct event *
  111. tor_event_new(struct event_base *base, int sock, short what,
  112. void (*cb)(int, short, void *), void *arg)
  113. {
  114. struct event *e = tor_malloc_zero(sizeof(struct event));
  115. event_set(e, sock, what, cb, arg);
  116. if (! base)
  117. base = tor_libevent_get_base();
  118. event_base_set(base, e);
  119. return e;
  120. }
  121. /** Work-alike replacement for evtimer_new() on pre-Libevent-2.0 systems. */
  122. struct event *
  123. tor_evtimer_new(struct event_base *base,
  124. void (*cb)(int, short, void *), void *arg)
  125. {
  126. return tor_event_new(base, -1, 0, cb, arg);
  127. }
  128. /** Work-alike replacement for evsignal_new() on pre-Libevent-2.0 systems. */
  129. struct event *
  130. tor_evsignal_new(struct event_base * base, int sig,
  131. void (*cb)(int, short, void *), void *arg)
  132. {
  133. return tor_event_new(base, sig, EV_SIGNAL|EV_PERSIST, cb, arg);
  134. }
  135. /** Work-alike replacement for event_free() on pre-Libevent-2.0 systems,
  136. * except tolerate tor_event_free(NULL). */
  137. void
  138. tor_event_free(struct event *ev)
  139. {
  140. if (ev == NULL)
  141. return;
  142. event_del(ev);
  143. tor_free(ev);
  144. }
  145. #else
  146. /* Wrapper for event_free() that tolerates tor_event_free(NULL) */
  147. void
  148. tor_event_free(struct event *ev)
  149. {
  150. if (ev == NULL)
  151. return;
  152. event_free(ev);
  153. }
  154. #endif
  155. /** Global event base for use by the main thread. */
  156. struct event_base *the_event_base = NULL;
  157. /* This is what passes for version detection on OSX. We set
  158. * MACOSX_KQUEUE_IS_BROKEN to true iff we're on a version of OSX before
  159. * 10.4.0 (aka 1040). */
  160. #ifdef __APPLE__
  161. #ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
  162. #define MACOSX_KQUEUE_IS_BROKEN \
  163. (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1040)
  164. #else
  165. #define MACOSX_KQUEUE_IS_BROKEN 0
  166. #endif
  167. #endif
  168. #ifdef USE_BUFFEREVENTS
  169. static int using_iocp_bufferevents = 0;
  170. static void tor_libevent_set_tick_timeout(int msec_per_tick);
  171. int
  172. tor_libevent_using_iocp_bufferevents(void)
  173. {
  174. return using_iocp_bufferevents;
  175. }
  176. #endif
  177. /** Initialize the Libevent library and set up the event base. */
  178. void
  179. tor_libevent_initialize(tor_libevent_cfg *torcfg)
  180. {
  181. tor_assert(the_event_base == NULL);
  182. /* some paths below don't use torcfg, so avoid unused variable warnings */
  183. (void)torcfg;
  184. #ifdef HAVE_EVENT2_EVENT_H
  185. {
  186. int attempts = 0;
  187. int using_threads;
  188. struct event_config *cfg;
  189. retry:
  190. ++attempts;
  191. using_threads = 0;
  192. cfg = event_config_new();
  193. tor_assert(cfg);
  194. #if defined(_WIN32) && defined(USE_BUFFEREVENTS)
  195. if (! torcfg->disable_iocp) {
  196. evthread_use_windows_threads();
  197. event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
  198. using_iocp_bufferevents = 1;
  199. using_threads = 1;
  200. } else {
  201. using_iocp_bufferevents = 0;
  202. }
  203. #endif
  204. if (!using_threads) {
  205. /* Telling Libevent not to try to turn locking on can avoid a needless
  206. * socketpair() attempt. */
  207. event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK);
  208. }
  209. #if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,0,7)
  210. if (torcfg->num_cpus > 0)
  211. event_config_set_num_cpus_hint(cfg, torcfg->num_cpus);
  212. #endif
  213. #if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
  214. /* We can enable changelist support with epoll, since we don't give
  215. * Libevent any dup'd fds. This lets us avoid some syscalls. */
  216. event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
  217. #endif
  218. the_event_base = event_base_new_with_config(cfg);
  219. event_config_free(cfg);
  220. if (using_threads && the_event_base == NULL && attempts < 2) {
  221. /* This could be a socketpair() failure, which can happen sometimes on
  222. * windows boxes with obnoxious firewall rules. Downgrade and try
  223. * again. */
  224. #if defined(_WIN32) && defined(USE_BUFFEREVENTS)
  225. if (torcfg->disable_iocp == 0) {
  226. log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again "
  227. "with IOCP disabled.");
  228. } else
  229. #endif
  230. {
  231. log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again.");
  232. }
  233. torcfg->disable_iocp = 1;
  234. goto retry;
  235. }
  236. }
  237. #else
  238. the_event_base = event_init();
  239. #endif
  240. if (!the_event_base) {
  241. log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue.");
  242. exit(1);
  243. }
  244. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  245. /* Making this a NOTICE for now so we can link bugs to a libevent versions
  246. * or methods better. */
  247. log_info(LD_GENERAL,
  248. "Initialized libevent version %s using method %s. Good.",
  249. event_get_version(), tor_libevent_get_method());
  250. #else
  251. log_notice(LD_GENERAL,
  252. "Initialized old libevent (version 1.0b or earlier).");
  253. log_warn(LD_GENERAL,
  254. "You have a *VERY* old version of libevent. It is likely to be buggy; "
  255. "please build Tor with a more recent version.");
  256. #endif
  257. #ifdef USE_BUFFEREVENTS
  258. tor_libevent_set_tick_timeout(torcfg->msec_per_tick);
  259. #endif
  260. }
  261. /** Return the current Libevent event base that we're set up to use. */
  262. struct event_base *
  263. tor_libevent_get_base(void)
  264. {
  265. return the_event_base;
  266. }
  267. #ifndef HAVE_EVENT_BASE_LOOPEXIT
  268. /** Replacement for event_base_loopexit on some very old versions of Libevent
  269. * that we are not yet brave enough to deprecate. */
  270. int
  271. tor_event_base_loopexit(struct event_base *base, struct timeval *tv)
  272. {
  273. tor_assert(base == the_event_base);
  274. return event_loopexit(tv);
  275. }
  276. #endif
  277. /** Return the name of the Libevent backend we're using. */
  278. const char *
  279. tor_libevent_get_method(void)
  280. {
  281. #ifdef HAVE_EVENT2_EVENT_H
  282. return event_base_get_method(the_event_base);
  283. #elif defined(HAVE_EVENT_GET_METHOD)
  284. return event_get_method();
  285. #else
  286. return "<unknown>";
  287. #endif
  288. }
  289. /** Return the le_version_t for the version of libevent specified in the
  290. * string <b>v</b>. If the version is very new or uses an unrecognized
  291. * version, format, return LE_OTHER. */
  292. static le_version_t
  293. tor_decode_libevent_version(const char *v)
  294. {
  295. unsigned major, minor, patchlevel;
  296. char c, e, extra;
  297. int fields;
  298. /* Try the new preferred "1.4.11-stable" format.
  299. * Also accept "1.4.14b-stable". */
  300. fields = tor_sscanf(v, "%u.%u.%u%c%c", &major, &minor, &patchlevel, &c, &e);
  301. if (fields == 3 ||
  302. ((fields == 4 || fields == 5 ) && (c == '-' || c == '_')) ||
  303. (fields == 5 && TOR_ISALPHA(c) && (e == '-' || e == '_'))) {
  304. return V(major,minor,patchlevel);
  305. }
  306. /* Try the old "1.3e" format. */
  307. fields = tor_sscanf(v, "%u.%u%c%c", &major, &minor, &c, &extra);
  308. if (fields == 3 && TOR_ISALPHA(c)) {
  309. return V_OLD(major, minor, c);
  310. } else if (fields == 2) {
  311. return V(major, minor, 0);
  312. }
  313. return LE_OTHER;
  314. }
  315. /** Return an integer representing the binary interface of a Libevent library.
  316. * Two different versions with different numbers are sure not to be binary
  317. * compatible. Two different versions with the same numbers have a decent
  318. * chance of binary compatibility.*/
  319. static int
  320. le_versions_compatibility(le_version_t v)
  321. {
  322. if (v == LE_OTHER)
  323. return 0;
  324. if (v < V_OLD(1,0,'c'))
  325. return 1;
  326. else if (v < V(1,4,0))
  327. return 2;
  328. else if (v < V(1,4,99))
  329. return 3;
  330. else if (v < V(2,0,1))
  331. return 4;
  332. else /* Everything 2.0 and later should be compatible. */
  333. return 5;
  334. }
  335. #if 0
  336. /** Return the version number of the currently running version of Libevent.
  337. * See le_version_t for info on the format.
  338. */
  339. static le_version_t
  340. tor_get_libevent_version(const char **v_out)
  341. {
  342. const char *v;
  343. le_version_t r;
  344. #if defined(HAVE_EVENT_GET_VERSION_NUMBER)
  345. v = event_get_version();
  346. r = event_get_version_number();
  347. #elif defined (HAVE_EVENT_GET_VERSION)
  348. v = event_get_version();
  349. r = tor_decode_libevent_version(v);
  350. #else
  351. v = "pre-1.0c";
  352. r = LE_OLD;
  353. #endif
  354. if (v_out)
  355. *v_out = v;
  356. return r;
  357. }
  358. #endif
  359. /** Return a string representation of the version of the currently running
  360. * version of Libevent. */
  361. const char *
  362. tor_libevent_get_version_str(void)
  363. {
  364. #ifdef HAVE_EVENT_GET_VERSION
  365. return event_get_version();
  366. #else
  367. return "pre-1.0c";
  368. #endif
  369. }
  370. /**
  371. * Compare the current Libevent method and version to a list of versions
  372. * which are known not to work. Warn the user as appropriate.
  373. */
  374. void
  375. tor_check_libevent_version(const char *m, int server,
  376. const char **badness_out)
  377. {
  378. (void) m;
  379. (void) server;
  380. *badness_out = NULL;
  381. }
  382. #if defined(LIBEVENT_VERSION)
  383. #define HEADER_VERSION LIBEVENT_VERSION
  384. #elif defined(_EVENT_VERSION)
  385. #define HEADER_VERSION _EVENT_VERSION
  386. #endif
  387. /** See whether the headers we were built against differ from the library we
  388. * linked against so much that we're likely to crash. If so, warn the
  389. * user. */
  390. void
  391. tor_check_libevent_header_compatibility(void)
  392. {
  393. (void) le_versions_compatibility;
  394. (void) tor_decode_libevent_version;
  395. /* In libevent versions before 2.0, it's hard to keep binary compatibility
  396. * between upgrades, and unpleasant to detect when the version we compiled
  397. * against is unlike the version we have linked against. Here's how. */
  398. #if defined(HEADER_VERSION) && defined(HAVE_EVENT_GET_VERSION)
  399. /* We have a header-file version and a function-call version. Easy. */
  400. if (strcmp(HEADER_VERSION, event_get_version())) {
  401. le_version_t v1, v2;
  402. int compat1 = -1, compat2 = -1;
  403. int verybad;
  404. v1 = tor_decode_libevent_version(HEADER_VERSION);
  405. v2 = tor_decode_libevent_version(event_get_version());
  406. compat1 = le_versions_compatibility(v1);
  407. compat2 = le_versions_compatibility(v2);
  408. verybad = compat1 != compat2;
  409. tor_log(verybad ? LOG_WARN : LOG_NOTICE,
  410. LD_GENERAL, "We were compiled with headers from version %s "
  411. "of Libevent, but we're using a Libevent library that says it's "
  412. "version %s.", HEADER_VERSION, event_get_version());
  413. if (verybad)
  414. log_warn(LD_GENERAL, "This will almost certainly make Tor crash.");
  415. else
  416. log_info(LD_GENERAL, "I think these versions are binary-compatible.");
  417. }
  418. #elif defined(HAVE_EVENT_GET_VERSION)
  419. /* event_get_version but no _EVENT_VERSION. We might be in 1.4.0-beta or
  420. earlier, where that's normal. To see whether we were compiled with an
  421. earlier version, let's see whether the struct event defines MIN_HEAP_IDX.
  422. */
  423. #ifdef HAVE_STRUCT_EVENT_MIN_HEAP_IDX
  424. /* The header files are 1.4.0-beta or later. If the version is not
  425. * 1.4.0-beta, we are incompatible. */
  426. {
  427. if (strcmp(event_get_version(), "1.4.0-beta")) {
  428. log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
  429. "Libevent 1.4.0-beta header files, whereas you have linked "
  430. "against Libevent %s. This will probably make Tor crash.",
  431. event_get_version());
  432. }
  433. }
  434. #else
  435. /* Our headers are 1.3e or earlier. If the library version is not 1.4.x or
  436. later, we're probably fine. */
  437. {
  438. const char *v = event_get_version();
  439. if ((v[0] == '1' && v[2] == '.' && v[3] > '3') || v[0] > '1') {
  440. log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
  441. "Libevent header file from 1.3e or earlier, whereas you have "
  442. "linked against Libevent %s. This will probably make Tor "
  443. "crash.", event_get_version());
  444. }
  445. }
  446. #endif
  447. #elif defined(HEADER_VERSION)
  448. #warn "_EVENT_VERSION is defined but not get_event_version(): Libevent is odd."
  449. #else
  450. /* Your libevent is ancient. */
  451. #endif
  452. }
  453. /*
  454. If possible, we're going to try to use Libevent's periodic timer support,
  455. since it does a pretty good job of making sure that periodic events get
  456. called exactly M seconds apart, rather than starting each one exactly M
  457. seconds after the time that the last one was run.
  458. */
  459. #ifdef HAVE_EVENT2_EVENT_H
  460. #define HAVE_PERIODIC
  461. #define PERIODIC_FLAGS EV_PERSIST
  462. #else
  463. #define PERIODIC_FLAGS 0
  464. #endif
  465. /** Represents a timer that's run every N microseconds by Libevent. */
  466. struct periodic_timer_t {
  467. /** Underlying event used to implement this periodic event. */
  468. struct event *ev;
  469. /** The callback we'll be invoking whenever the event triggers */
  470. void (*cb)(struct periodic_timer_t *, void *);
  471. /** User-supplied data for the callback */
  472. void *data;
  473. #ifndef HAVE_PERIODIC
  474. /** If Libevent doesn't know how to invoke events every N microseconds,
  475. * we'll need to remember the timeout interval here. */
  476. struct timeval tv;
  477. #endif
  478. };
  479. /** Libevent callback to implement a periodic event. */
  480. static void
  481. periodic_timer_cb(evutil_socket_t fd, short what, void *arg)
  482. {
  483. periodic_timer_t *timer = arg;
  484. (void) what;
  485. (void) fd;
  486. #ifndef HAVE_PERIODIC
  487. /** reschedule the event as needed. */
  488. event_add(timer->ev, &timer->tv);
  489. #endif
  490. timer->cb(timer, timer->data);
  491. }
  492. /** Create and schedule a new timer that will run every <b>tv</b> in
  493. * the event loop of <b>base</b>. When the timer fires, it will
  494. * run the timer in <b>cb</b> with the user-supplied data in <b>data</b>. */
  495. periodic_timer_t *
  496. periodic_timer_new(struct event_base *base,
  497. const struct timeval *tv,
  498. void (*cb)(periodic_timer_t *timer, void *data),
  499. void *data)
  500. {
  501. periodic_timer_t *timer;
  502. tor_assert(base);
  503. tor_assert(tv);
  504. tor_assert(cb);
  505. timer = tor_malloc_zero(sizeof(periodic_timer_t));
  506. if (!(timer->ev = tor_event_new(base, -1, PERIODIC_FLAGS,
  507. periodic_timer_cb, timer))) {
  508. tor_free(timer);
  509. return NULL;
  510. }
  511. timer->cb = cb;
  512. timer->data = data;
  513. #ifndef HAVE_PERIODIC
  514. memcpy(&timer->tv, tv, sizeof(struct timeval));
  515. #endif
  516. event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/
  517. return timer;
  518. }
  519. /** Stop and free a periodic timer */
  520. void
  521. periodic_timer_free(periodic_timer_t *timer)
  522. {
  523. if (!timer)
  524. return;
  525. tor_event_free(timer->ev);
  526. tor_free(timer);
  527. }
  528. #ifdef USE_BUFFEREVENTS
  529. static const struct timeval *one_tick = NULL;
  530. /**
  531. * Return a special timeout to be passed whenever libevent's O(1) timeout
  532. * implementation should be used. Only use this when the timer is supposed
  533. * to fire after msec_per_tick ticks have elapsed.
  534. */
  535. const struct timeval *
  536. tor_libevent_get_one_tick_timeout(void)
  537. {
  538. tor_assert(one_tick);
  539. return one_tick;
  540. }
  541. /** Initialize the common timeout that we'll use to refill the buckets every
  542. * time a tick elapses. */
  543. static void
  544. tor_libevent_set_tick_timeout(int msec_per_tick)
  545. {
  546. struct event_base *base = tor_libevent_get_base();
  547. struct timeval tv;
  548. tor_assert(! one_tick);
  549. tv.tv_sec = msec_per_tick / 1000;
  550. tv.tv_usec = (msec_per_tick % 1000) * 1000;
  551. one_tick = event_base_init_common_timeout(base, &tv);
  552. }
  553. static struct bufferevent *
  554. tor_get_root_bufferevent(struct bufferevent *bev)
  555. {
  556. struct bufferevent *u;
  557. while ((u = bufferevent_get_underlying(bev)) != NULL)
  558. bev = u;
  559. return bev;
  560. }
  561. int
  562. tor_set_bufferevent_rate_limit(struct bufferevent *bev,
  563. struct ev_token_bucket_cfg *cfg)
  564. {
  565. return bufferevent_set_rate_limit(tor_get_root_bufferevent(bev), cfg);
  566. }
  567. int
  568. tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
  569. struct bufferevent_rate_limit_group *g)
  570. {
  571. return bufferevent_add_to_rate_limit_group(tor_get_root_bufferevent(bev), g);
  572. }
  573. #endif
  574. #if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,1,1)
  575. void
  576. tor_gettimeofday_cached(struct timeval *tv)
  577. {
  578. event_base_gettimeofday_cached(the_event_base, tv);
  579. }
  580. void
  581. tor_gettimeofday_cache_clear(void)
  582. {
  583. event_base_update_cache_time(the_event_base);
  584. }
  585. #else
  586. /** Cache the current hi-res time; the cache gets reset when libevent
  587. * calls us. */
  588. static struct timeval cached_time_hires = {0, 0};
  589. /** Return a fairly recent view of the current time. */
  590. void
  591. tor_gettimeofday_cached(struct timeval *tv)
  592. {
  593. if (cached_time_hires.tv_sec == 0) {
  594. tor_gettimeofday(&cached_time_hires);
  595. }
  596. *tv = cached_time_hires;
  597. }
  598. /** Reset the cached view of the current time, so that the next time we try
  599. * to learn it, we will get an up-to-date value. */
  600. void
  601. tor_gettimeofday_cache_clear(void)
  602. {
  603. cached_time_hires.tv_sec = 0;
  604. }
  605. #endif