cc: Export sendme_inc validation into public function
[tor.git] / src / core / or / orconn_event.c
blobad2dac02ae3773f9e146d41bea7f68c7a9d01f7d
1 /* Copyright (c) 2007-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file orconn_event.c
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
16 * frameworks.
17 **/
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);
30 static void
31 orconn_event_free(msg_aux_data_t u)
33 tor_free_(u.ptr);
36 static char *
37 orconn_state_fmt(msg_aux_data_t u)
39 orconn_state_msg_t *msg = (orconn_state_msg_t *)u.ptr;
40 char *s = NULL;
42 tor_asprintf(&s, "<gid=%"PRIu64" chan=%"PRIu64" proxy_type=%d state=%d>",
43 msg->gid, msg->chan, msg->proxy_type, msg->state);
44 return s;
47 static char *
48 orconn_status_fmt(msg_aux_data_t u)
50 orconn_status_msg_t *msg = (orconn_status_msg_t *)u.ptr;
51 char *s = NULL;
53 tor_asprintf(&s, "<gid=%"PRIu64" status=%d reason=%d>",
54 msg->gid, msg->status, msg->reason);
55 return s;
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,
68 int
69 orconn_add_pubsub(struct pubsub_connector_t *connector)
71 if (DISPATCH_REGISTER_TYPE(connector, orconn_state, &orconn_state_fns))
72 return -1;
73 if (DISPATCH_REGISTER_TYPE(connector, orconn_status, &orconn_status_fns))
74 return -1;
75 if (DISPATCH_ADD_PUB(connector, orconn, orconn_state) != 0)
76 return -1;
77 if (DISPATCH_ADD_PUB(connector, orconn, orconn_status) != 0)
78 return -1;
79 return 0;
82 void
83 orconn_state_publish(orconn_state_msg_t *msg)
85 PUBLISH(orconn_state, msg);
88 void
89 orconn_status_publish(orconn_status_msg_t *msg)
91 PUBLISH(orconn_status, msg);