orconn_event.c (2328B)
1 /* Copyright (c) 2007-2021, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 /** 5 * \file orconn_event.c 6 * \brief Publish state change messages for OR connections 7 * 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. 11 * 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 **/ 18 19 #include "core/or/or.h" 20 #include "lib/pubsub/pubsub.h" 21 #include "lib/subsys/subsys.h" 22 23 #define ORCONN_EVENT_PRIVATE 24 #include "core/or/orconn_event.h" 25 #include "core/or/or_sys.h" 26 27 DECLARE_PUBLISH(orconn_state); 28 DECLARE_PUBLISH(orconn_status); 29 30 static void 31 orconn_event_free(msg_aux_data_t u) 32 { 33 tor_free_(u.ptr); 34 } 35 36 static char * 37 orconn_state_fmt(msg_aux_data_t u) 38 { 39 orconn_state_msg_t *msg = (orconn_state_msg_t *)u.ptr; 40 char *s = NULL; 41 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; 45 } 46 47 static char * 48 orconn_status_fmt(msg_aux_data_t u) 49 { 50 orconn_status_msg_t *msg = (orconn_status_msg_t *)u.ptr; 51 char *s = NULL; 52 53 tor_asprintf(&s, "<gid=%"PRIu64" status=%d reason=%d>", 54 msg->gid, msg->status, msg->reason); 55 return s; 56 } 57 58 static dispatch_typefns_t orconn_state_fns = { 59 .free_fn = orconn_event_free, 60 .fmt_fn = orconn_state_fmt, 61 }; 62 63 static dispatch_typefns_t orconn_status_fns = { 64 .free_fn = orconn_event_free, 65 .fmt_fn = orconn_status_fmt, 66 }; 67 68 int 69 orconn_add_pubsub(struct pubsub_connector_t *connector) 70 { 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; 80 } 81 82 void 83 orconn_state_publish(orconn_state_msg_t *msg) 84 { 85 PUBLISH(orconn_state, msg); 86 } 87 88 void 89 orconn_status_publish(orconn_status_msg_t *msg) 90 { 91 PUBLISH(orconn_status, msg); 92 }