periodic.h 3.6 KB

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