periodic.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (c) 2015-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_PERIODIC_H
  4. #define TOR_PERIODIC_H
  5. #define PERIODIC_EVENT_NO_UPDATE (-1)
  6. /* Tor roles for which a periodic event item is for. An event can be for
  7. * multiple roles, they can be combined. */
  8. #define PERIODIC_EVENT_ROLE_CLIENT (1U << 0)
  9. #define PERIODIC_EVENT_ROLE_RELAY (1U << 1)
  10. #define PERIODIC_EVENT_ROLE_BRIDGE (1U << 2)
  11. #define PERIODIC_EVENT_ROLE_DIRAUTH (1U << 3)
  12. #define PERIODIC_EVENT_ROLE_BRIDGEAUTH (1U << 4)
  13. #define PERIODIC_EVENT_ROLE_HS_SERVICE (1U << 5)
  14. /* Helper macro to make it a bit less annoying to defined groups of roles that
  15. * are often used. */
  16. /* Router that is a Bridge or Relay. */
  17. #define PERIODIC_EVENT_ROLE_ROUTER \
  18. (PERIODIC_EVENT_ROLE_BRIDGE | PERIODIC_EVENT_ROLE_RELAY)
  19. /* Authorities that is both bridge and directory. */
  20. #define PERIODIC_EVENT_ROLE_AUTHORITIES \
  21. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_DIRAUTH)
  22. /* All roles. */
  23. #define PERIODIC_EVENT_ROLE_ALL \
  24. (PERIODIC_EVENT_ROLE_AUTHORITIES | PERIODIC_EVENT_ROLE_CLIENT | \
  25. PERIODIC_EVENT_ROLE_HS_SERVICE | PERIODIC_EVENT_ROLE_ROUTER)
  26. /** Callback function for a periodic event to take action. The return value
  27. * influences the next time the function will get called. Return
  28. * PERIODIC_EVENT_NO_UPDATE to not update <b>last_action_time</b> and be polled
  29. * again in the next second. If a positive value is returned it will update the
  30. * interval time. */
  31. typedef int (*periodic_event_helper_t)(time_t now,
  32. const or_options_t *options);
  33. struct mainloop_event_t;
  34. /** A single item for the periodic-events-function table. */
  35. typedef struct periodic_event_item_t {
  36. periodic_event_helper_t fn; /**< The function to run the event */
  37. time_t last_action_time; /**< The last time the function did something */
  38. struct mainloop_event_t *ev; /**< Libevent callback we're using to implement
  39. * this */
  40. const char *name; /**< Name of the function -- for debug */
  41. /* Bitmask of roles define above for which this event applies. */
  42. uint32_t roles;
  43. /* Indicate that this event has been enabled that is scheduled. */
  44. unsigned int enabled : 1;
  45. } periodic_event_item_t;
  46. /** events will get their interval from first execution */
  47. #define PERIODIC_EVENT(fn, r) { fn##_callback, 0, NULL, #fn, r, 0 }
  48. #define END_OF_PERIODIC_EVENTS { NULL, 0, NULL, NULL, 0, 0 }
  49. /* Return true iff the given event was setup before thus is enabled to be
  50. * scheduled. */
  51. static inline int
  52. periodic_event_is_enabled(const periodic_event_item_t *item)
  53. {
  54. return item->enabled;
  55. }
  56. void periodic_event_launch(periodic_event_item_t *event);
  57. void periodic_event_setup(periodic_event_item_t *event);
  58. void periodic_event_destroy(periodic_event_item_t *event);
  59. void periodic_event_reschedule(periodic_event_item_t *event);
  60. void periodic_event_enable(periodic_event_item_t *event);
  61. void periodic_event_disable(periodic_event_item_t *event);
  62. #endif /* !defined(TOR_PERIODIC_H) */