ocirc_event.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file ocirc_event.c
  5. * \brief Publish state change messages for origin circuits
  6. *
  7. * Implements a basic publish-subscribe framework for messages about
  8. * the state of origin circuits. The publisher calls the subscriber
  9. * callback functions synchronously.
  10. *
  11. * Although the synchronous calls might not simplify the call graph,
  12. * this approach improves data isolation because the publisher doesn't
  13. * need knowledge about the internals of subscribing subsystems. It
  14. * also avoids race conditions that might occur in asynchronous
  15. * frameworks.
  16. **/
  17. #include "core/or/or.h"
  18. #define OCIRC_EVENT_PRIVATE
  19. #include "core/or/cpath_build_state_st.h"
  20. #include "core/or/ocirc_event.h"
  21. #include "core/or/ocirc_event_sys.h"
  22. #include "core/or/origin_circuit_st.h"
  23. #include "lib/subsys/subsys.h"
  24. DECLARE_PUBLISH(ocirc_state);
  25. DECLARE_PUBLISH(ocirc_chan);
  26. DECLARE_PUBLISH(ocirc_cevent);
  27. static void
  28. ocirc_event_free(msg_aux_data_t u)
  29. {
  30. tor_free_(u.ptr);
  31. }
  32. static char *
  33. ocirc_state_fmt(msg_aux_data_t u)
  34. {
  35. ocirc_state_msg_t *msg = (ocirc_state_msg_t *)u.ptr;
  36. char *s = NULL;
  37. tor_asprintf(&s, "<gid=%"PRIu32" state=%d onehop=%d>",
  38. msg->gid, msg->state, msg->onehop);
  39. return s;
  40. }
  41. static char *
  42. ocirc_chan_fmt(msg_aux_data_t u)
  43. {
  44. ocirc_chan_msg_t *msg = (ocirc_chan_msg_t *)u.ptr;
  45. char *s = NULL;
  46. tor_asprintf(&s, "<gid=%"PRIu32" chan=%"PRIu64" onehop=%d>",
  47. msg->gid, msg->chan, msg->onehop);
  48. return s;
  49. }
  50. static char *
  51. ocirc_cevent_fmt(msg_aux_data_t u)
  52. {
  53. ocirc_cevent_msg_t *msg = (ocirc_cevent_msg_t *)u.ptr;
  54. char *s = NULL;
  55. tor_asprintf(&s, "<gid=%"PRIu32" evtype=%d reason=%d onehop=%d>",
  56. msg->gid, msg->evtype, msg->reason, msg->onehop);
  57. return s;
  58. }
  59. static dispatch_typefns_t ocirc_state_fns = {
  60. .free_fn = ocirc_event_free,
  61. .fmt_fn = ocirc_state_fmt,
  62. };
  63. static dispatch_typefns_t ocirc_chan_fns = {
  64. .free_fn = ocirc_event_free,
  65. .fmt_fn = ocirc_chan_fmt,
  66. };
  67. static dispatch_typefns_t ocirc_cevent_fns = {
  68. .free_fn = ocirc_event_free,
  69. .fmt_fn = ocirc_cevent_fmt,
  70. };
  71. static int
  72. ocirc_add_pubsub(struct pubsub_connector_t *connector)
  73. {
  74. if (DISPATCH_REGISTER_TYPE(connector, ocirc_state, &ocirc_state_fns))
  75. return -1;
  76. if (DISPATCH_REGISTER_TYPE(connector, ocirc_chan, &ocirc_chan_fns))
  77. return -1;
  78. if (DISPATCH_REGISTER_TYPE(connector, ocirc_cevent, &ocirc_cevent_fns))
  79. return -1;
  80. if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_state))
  81. return -1;
  82. if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_chan))
  83. return -1;
  84. if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_cevent))
  85. return -1;
  86. return 0;
  87. }
  88. void
  89. ocirc_state_publish(ocirc_state_msg_t *msg)
  90. {
  91. PUBLISH(ocirc_state, msg);
  92. }
  93. void
  94. ocirc_chan_publish(ocirc_chan_msg_t *msg)
  95. {
  96. PUBLISH(ocirc_chan, msg);
  97. }
  98. void
  99. ocirc_cevent_publish(ocirc_cevent_msg_t *msg)
  100. {
  101. PUBLISH(ocirc_cevent, msg);
  102. }
  103. const subsys_fns_t sys_ocirc_event = {
  104. .name = "ocirc_event",
  105. .supported = true,
  106. .level = -32,
  107. .add_pubsub = ocirc_add_pubsub,
  108. };