periodic.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #define PERIODIC_EVENT_ROLE_DIRSERVER (1U << 6)
  15. /* Helper macro to make it a bit less annoying to defined groups of roles that
  16. * are often used. */
  17. /* Router that is a Bridge or Relay. */
  18. #define PERIODIC_EVENT_ROLE_ROUTER \
  19. (PERIODIC_EVENT_ROLE_BRIDGE | PERIODIC_EVENT_ROLE_RELAY)
  20. /* Authorities that is both bridge and directory. */
  21. #define PERIODIC_EVENT_ROLE_AUTHORITIES \
  22. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_DIRAUTH)
  23. /* All roles. */
  24. #define PERIODIC_EVENT_ROLE_ALL \
  25. (PERIODIC_EVENT_ROLE_AUTHORITIES | PERIODIC_EVENT_ROLE_CLIENT | \
  26. PERIODIC_EVENT_ROLE_HS_SERVICE | PERIODIC_EVENT_ROLE_ROUTER)
  27. /*
  28. * Event flags which can change the behavior of an event.
  29. */
  30. /* Indicate that the event needs the network meaning that if we are in
  31. * DisableNetwork or hibernation mode, the event won't be enabled. This obey
  32. * the net_is_disabled() check. */
  33. #define PERIODIC_EVENT_FLAG_NEED_NET (1U << 0)
  34. /** Callback function for a periodic event to take action. The return value
  35. * influences the next time the function will get called. Return
  36. * PERIODIC_EVENT_NO_UPDATE to not update <b>last_action_time</b> and be polled
  37. * again in the next second. If a positive value is returned it will update the
  38. * interval time. */
  39. typedef int (*periodic_event_helper_t)(time_t now,
  40. const or_options_t *options);
  41. struct mainloop_event_t;
  42. /** A single item for the periodic-events-function table. */
  43. typedef struct periodic_event_item_t {
  44. periodic_event_helper_t fn; /**< The function to run the event */
  45. time_t last_action_time; /**< The last time the function did something */
  46. struct mainloop_event_t *ev; /**< Libevent callback we're using to implement
  47. * this */
  48. const char *name; /**< Name of the function -- for debug */
  49. /* Bitmask of roles define above for which this event applies. */
  50. uint32_t roles;
  51. /* Bitmask of flags which can change the behavior of the event. */
  52. uint32_t flags;
  53. /* Indicate that this event has been enabled that is scheduled. */
  54. unsigned int enabled : 1;
  55. } periodic_event_item_t;
  56. /** events will get their interval from first execution */
  57. #define PERIODIC_EVENT(fn, r, f) { fn##_callback, 0, NULL, #fn, r, f, 0 }
  58. #define END_OF_PERIODIC_EVENTS { NULL, 0, NULL, NULL, 0, 0, 0 }
  59. /* Return true iff the given event was setup before thus is enabled to be
  60. * scheduled. */
  61. static inline int
  62. periodic_event_is_enabled(const periodic_event_item_t *item)
  63. {
  64. return item->enabled;
  65. }
  66. void periodic_event_launch(periodic_event_item_t *event);
  67. void periodic_event_setup(periodic_event_item_t *event);
  68. void periodic_event_destroy(periodic_event_item_t *event);
  69. void periodic_event_reschedule(periodic_event_item_t *event);
  70. void periodic_event_enable(periodic_event_item_t *event);
  71. void periodic_event_disable(periodic_event_item_t *event);
  72. #endif /* !defined(TOR_PERIODIC_H) */