1 /* Copyright (c) 2007-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Publish state change messages for OR connections
8 * Implements a basic publish-subscribe framework for messages about
9 * the state of OR connections. The publisher calls the subscriber
10 * callback functions synchronously.
12 * Although the synchronous calls might not simplify the call graph,
13 * this approach improves data isolation because the publisher doesn't
14 * need knowledge about the internals of subscribing subsystems. It
15 * also avoids race conditions that might occur in asynchronous
19 #include "core/or/or.h"
20 #include "lib/pubsub/pubsub.h"
21 #include "lib/subsys/subsys.h"
23 #define ORCONN_EVENT_PRIVATE
24 #include "core/or/orconn_event.h"
25 #include "core/or/or_sys.h"
27 DECLARE_PUBLISH(orconn_state
);
28 DECLARE_PUBLISH(orconn_status
);
31 orconn_event_free(msg_aux_data_t u
)
37 orconn_state_fmt(msg_aux_data_t u
)
39 orconn_state_msg_t
*msg
= (orconn_state_msg_t
*)u
.ptr
;
42 tor_asprintf(&s
, "<gid=%"PRIu64
" chan=%"PRIu64
" proxy_type=%d state=%d>",
43 msg
->gid
, msg
->chan
, msg
->proxy_type
, msg
->state
);
48 orconn_status_fmt(msg_aux_data_t u
)
50 orconn_status_msg_t
*msg
= (orconn_status_msg_t
*)u
.ptr
;
53 tor_asprintf(&s
, "<gid=%"PRIu64
" status=%d reason=%d>",
54 msg
->gid
, msg
->status
, msg
->reason
);
58 static dispatch_typefns_t orconn_state_fns
= {
59 .free_fn
= orconn_event_free
,
60 .fmt_fn
= orconn_state_fmt
,
63 static dispatch_typefns_t orconn_status_fns
= {
64 .free_fn
= orconn_event_free
,
65 .fmt_fn
= orconn_status_fmt
,
69 orconn_add_pubsub(struct pubsub_connector_t
*connector
)
71 if (DISPATCH_REGISTER_TYPE(connector
, orconn_state
, &orconn_state_fns
))
73 if (DISPATCH_REGISTER_TYPE(connector
, orconn_status
, &orconn_status_fns
))
75 if (DISPATCH_ADD_PUB(connector
, orconn
, orconn_state
) != 0)
77 if (DISPATCH_ADD_PUB(connector
, orconn
, orconn_status
) != 0)
83 orconn_state_publish(orconn_state_msg_t
*msg
)
85 PUBLISH(orconn_state
, msg
);
89 orconn_status_publish(orconn_status_msg_t
*msg
)
91 PUBLISH(orconn_status
, msg
);