Tor  0.4.6.0-alpha-dev
ocirc_event.c
Go to the documentation of this file.
1 /* Copyright (c) 2007-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file ocirc_event.c
6  * \brief Publish state change messages for origin circuits
7  *
8  * Implements a basic publish-subscribe framework for messages about
9  * the state of origin circuits. 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 
21 #define OCIRC_EVENT_PRIVATE
22 
24 #include "core/or/ocirc_event.h"
25 #include "core/or/or_sys.h"
27 #include "lib/subsys/subsys.h"
28 
29 DECLARE_PUBLISH(ocirc_state);
30 DECLARE_PUBLISH(ocirc_chan);
31 DECLARE_PUBLISH(ocirc_cevent);
32 
33 static void
34 ocirc_event_free(msg_aux_data_t u)
35 {
36  tor_free_(u.ptr);
37 }
38 
39 static char *
40 ocirc_state_fmt(msg_aux_data_t u)
41 {
42  ocirc_state_msg_t *msg = (ocirc_state_msg_t *)u.ptr;
43  char *s = NULL;
44 
45  tor_asprintf(&s, "<gid=%"PRIu32" state=%d onehop=%d>",
46  msg->gid, msg->state, msg->onehop);
47  return s;
48 }
49 
50 static char *
51 ocirc_chan_fmt(msg_aux_data_t u)
52 {
53  ocirc_chan_msg_t *msg = (ocirc_chan_msg_t *)u.ptr;
54  char *s = NULL;
55 
56  tor_asprintf(&s, "<gid=%"PRIu32" chan=%"PRIu64" onehop=%d>",
57  msg->gid, msg->chan, msg->onehop);
58  return s;
59 }
60 
61 static char *
62 ocirc_cevent_fmt(msg_aux_data_t u)
63 {
64  ocirc_cevent_msg_t *msg = (ocirc_cevent_msg_t *)u.ptr;
65  char *s = NULL;
66 
67  tor_asprintf(&s, "<gid=%"PRIu32" evtype=%d reason=%d onehop=%d>",
68  msg->gid, msg->evtype, msg->reason, msg->onehop);
69  return s;
70 }
71 
72 static dispatch_typefns_t ocirc_state_fns = {
73  .free_fn = ocirc_event_free,
74  .fmt_fn = ocirc_state_fmt,
75 };
76 
77 static dispatch_typefns_t ocirc_chan_fns = {
78  .free_fn = ocirc_event_free,
79  .fmt_fn = ocirc_chan_fmt,
80 };
81 
82 static dispatch_typefns_t ocirc_cevent_fns = {
83  .free_fn = ocirc_event_free,
84  .fmt_fn = ocirc_cevent_fmt,
85 };
86 
87 int
88 ocirc_add_pubsub(struct pubsub_connector_t *connector)
89 {
90  if (DISPATCH_REGISTER_TYPE(connector, ocirc_state, &ocirc_state_fns))
91  return -1;
92  if (DISPATCH_REGISTER_TYPE(connector, ocirc_chan, &ocirc_chan_fns))
93  return -1;
94  if (DISPATCH_REGISTER_TYPE(connector, ocirc_cevent, &ocirc_cevent_fns))
95  return -1;
96  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_state))
97  return -1;
98  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_chan))
99  return -1;
100  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_cevent))
101  return -1;
102  return 0;
103 }
104 
105 void
106 ocirc_state_publish(ocirc_state_msg_t *msg)
107 {
108  PUBLISH(ocirc_state, msg);
109 }
110 
111 void
112 ocirc_chan_publish(ocirc_chan_msg_t *msg)
113 {
114  PUBLISH(ocirc_chan, msg);
115 }
116 
117 void
118 ocirc_cevent_publish(ocirc_cevent_msg_t *msg)
119 {
120  PUBLISH(ocirc_cevent, msg);
121 }
tor_free_
void tor_free_(void *mem)
Definition: malloc.c:227
ocirc_cevent_msg_t::reason
int reason
Definition: ocirc_event.h:60
ocirc_state_msg_t::gid
uint32_t gid
Definition: ocirc_event.h:29
cpath_build_state_st.h
Circuit-build-stse structure.
ocirc_state_msg_t
Definition: ocirc_event.h:28
DECLARE_PUBLISH
#define DECLARE_PUBLISH(messagename)
Definition: pubsub_macros.h:245
ocirc_chan_msg_t::chan
uint64_t chan
Definition: ocirc_event.h:46
msg_aux_data_t
Definition: msgtypes.h:42
subsys.h
Types used to declare a subsystem.
ocirc_chan_msg_t::gid
uint32_t gid
Definition: ocirc_event.h:45
ocirc_chan_msg_t::onehop
bool onehop
Definition: ocirc_event.h:47
PUBLISH
#define PUBLISH(messagename, arg)
Definition: pubsub_macros.h:359
ocirc_state_msg_t::state
int state
Definition: ocirc_event.h:30
dispatch_typefns_t::free_fn
void(* free_fn)(msg_aux_data_t)
Definition: msgtypes.h:74
DISPATCH_REGISTER_TYPE
#define DISPATCH_REGISTER_TYPE(con, type, fns)
Definition: pubsub_macros.h:366
ocirc_cevent_msg_t::evtype
int evtype
Definition: ocirc_event.h:59
ocirc_cevent_msg_t::gid
uint32_t gid
Definition: ocirc_event.h:58
ocirc_cevent_msg_t
Definition: ocirc_event.h:57
dispatch_typefns_t
Definition: msgtypes.h:72
DISPATCH_ADD_PUB
#define DISPATCH_ADD_PUB(connector, channel, messagename)
Definition: pubsub_macros.h:313
ocirc_chan_msg_t
Definition: ocirc_event.h:44
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
ocirc_cevent_msg_t::onehop
bool onehop
Definition: ocirc_event.h:61
or_sys.h
Header for core/or/or_sys.c.
origin_circuit_st.h
Origin circuit structure.
ocirc_state_msg_t::onehop
bool onehop
Definition: ocirc_event.h:31
ocirc_event.h
Header file for ocirc_event.c.
pubsub_connector_t
struct pubsub_connector_t pubsub_connector_t
Definition: pubsub_connect.h:28
or.h
Master header file for Tor-specific functionality.